Gateway API Reference
Gateway Model
When implemented, a Gateway Model makes it possible for a foreign Simulation Environment to connect with a Dynamic Simulation Environment. The two simulation environments can then exchange signals and maintain synchronisation.
Component Diagram
@startuml gateway-model
title Gateway Model
node "Dynamic Simulation Environment" {
component "Model" as m1
component "Model" as m2
interface "SimBus" as SBif
m1 -left-> SBif
m2 -right-> SBif
}
package "Gateway Model" {
component "ModelC Lib" as ModelC
component "Model"
}
SBif <-down- ModelC
Model -up-> ModelC :model_gw_setup()
Model -up-> ModelC :model_gw_sync()
Model -up-> ModelC :model_gw_exit()
center footer Dynamic Simulation Environment
@enduml
Example
// Copyright 2023 Robert Bosch GmbH
//
// SPDX-License-Identifier: Apache-2.0
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dse/modelc/gateway.h>
#include <dse/logger.h>
/**
* Gateway Model.
*
* A normal gateway would exchange signals with the host simulation, however
* in this example a simple conversion is applied to each signal (to
* demonstrate the effect).
*
* Example
* -------
* ./gateway 0.005 0.02 gateway.yaml
*/
int main(int argc, char** argv)
{
if (argc < 4) {
log_fatal(
"Missing arguments! (gateway <step_size> <end_time> [yaml ...])");
}
double model_time = 0.0;
double step_size = atof(argv[1]);
double end_time = atof(argv[2]);
const char** yaml_files = calloc(argc - 3 + 1, sizeof(char*));
memcpy(yaml_files, &argv[3], ((argc - 3) * sizeof(char*)));
/* Setup the gateway. */
ModelGatewayDesc gw;
model_gw_setup(&gw, "gateway", yaml_files, LOG_INFO, step_size, end_time);
/* Run the simulation. */
while (model_time <= end_time) {
int rc = model_gw_sync(&gw, model_time);
if (rc == ETIME) {
log_notice(
"Gateway is behind simulation time, advancing gateway time.");
model_time += step_size;
continue;
}
/* Process the signal vectors. */
SignalVector* sv = gw.sv;
while (sv && sv->name) {
if (sv->is_binary) {
/* Binary vector. */
for (uint32_t i = 0; i < sv->count; i++) {
log_info("[%f] %s[%d] = <%d:%d>%s (%s)", model_time,
sv->name, i, sv->length[i], sv->buffer_size[i],
sv->binary[i], sv->signal[i]);
/* Exchange/update the gateway signal. */
uint8_t buffer[100];
snprintf((char*)buffer, sizeof(buffer), "st=%f,index=%d",
model_time, i);
signal_reset(sv, i);
signal_append(sv, i, buffer, strlen((char*)buffer) + 1);
}
} else {
/* Scalar vector. */
for (uint32_t i = 0; i < sv->count; i++) {
log_info("[%f] %s[%d] = %f (%s)", model_time, sv->name, i,
sv->scalar[i], sv->signal[i]);
/* Exchange/update the gateway signal. */
sv->scalar[i] = sv->scalar[i] + ((i + 1) << 2);
}
}
/* Next signal vector. */
sv++;
}
/* Next step. */
model_time += step_size;
}
/* Exit the simulation. */
model_gw_exit(&gw);
free(yaml_files);
return 0;
}
Typedefs
ModelGatewayDesc
typedef struct ModelGatewayDesc {
int* sim;
int* mi;
int* sv;
const char** argv;
char* name_arg;
double clock_epsilon;
uint64_t [4] __reserved__;
}
Functions
model_gw_exit
Terminates the Gateway Model and releases all objects referenced by the ModelGatewayDesc object. The object itself is not affected and should be released by the caller (if necessary).
Parameters
- gw (ModelGatewayDesc*)
- A gateway descriptor object, holds references to various ModelC objects.
Returns
- 0
- Success.
- +ve
- Failure, inspect errno for the failing condition.
model_gw_setup
Parameters
- gw (ModelGatewayDesc*)
- A gateway descriptor object, holds references to various ModelC objects.
- name (const char*)
- Name of the gateway model. Used when parsing the provided YAML files to select the relevant configuration items (i.e. Model and SignalGroup schemas).
- yaml_files (const char*)
- A list of YAML files where the relevant gateway configuration objects should be found.
- log_level (int)
- The log level to apply to the gateway model. Common values include; LOG_NOTICE (default), LOG_INFO, LOG_QUIET (only errors) or LOG_DEBUG. Set to a negative number to use the default log level.
- step_size (double)
- Step size for interactions with the Simbus.
- end_time (double)
- End time for the simulation (acts as guard against “forever” simulations).
Returns
- 0
- Success.
- +ve
- Failure, inspect errno for the failing condition.
model_gw_sync
Parameters
- gw (ModelGatewayDesc*)
- A gateway descriptor object, holds references to various ModelC objects.
- model_time (double)
- The current simulation time of the gateway model for which the Gateway API should synchronise with.
Returns
- 0
- Success.
- E_GATEWAYBEHIND
- The specified model_time is behind the simulation time. The time should be advanced by the caller and then retry this call until the condition clears.
- +ve
- Failure, inspect errno for the failing condition.