Eclipse SUMO - Simulation of Urban MObility
sumo2fmi_bridge.c
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2020-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Implementation of the FMI to SUMO bridge features
19 /****************************************************************************/
20 
21 #ifdef _MSC_VER
22 // Avoid warnings in windows build because of strcpy instead of strcpy_s,
23 // because the latter is not available on all platforms
24 #define _CRT_SECURE_NO_WARNINGS
25 #endif
26 
28 #include <string.h>
29 #include "libsumocpp2c.h"
30 #include "sumo2fmi_bridge.h"
31 
32 /* Explicit definition of unused parameters to avoid compiler warnings */
33 #define UNREFERENCED_PARAMETER(P) (P)
34 
35 void
37  comp->freeMemory(comp->libsumoCallOptions);
38 
39  char* defaultCallOptions = "-c tools/game/grid6.sumocfg";
40  comp->libsumoCallOptions = (char *)comp->allocateMemory(1 + strlen(defaultCallOptions), sizeof(char));
41  strcpy((char *)comp->libsumoCallOptions, (char *)defaultCallOptions);
42 }
43 
44 void
45 sumo2fmi_logError(ModelInstance *comp, const char *message, ...) {
46  if (!comp->logErrors) return;
47 
48  va_list args;
49  va_start(args, message);
50  sumo2fmi_logMessage(comp, fmi2Error, "logStatusError", message, args);
51  va_end(args);
52 }
53 
54 void
55 sumo2fmi_logMessage(ModelInstance *comp, int status, const char *category, const char *message, va_list args) {
56  va_list args1;
57  size_t len = 0;
58  char *buf = "";
59 
60  va_copy(args1, args);
61  len = vsnprintf(buf, len, message, args1);
62  va_end(args1);
63 
64  va_copy(args1, args);
65  buf = comp->allocateMemory(len + 1, sizeof(char));
66  vsnprintf(buf, len + 1, message, args);
67  va_end(args1);
68 
69  comp->logger(comp->componentEnvironment, comp->instanceName, status, category, buf);
70 
71  comp->freeMemory(buf);
72 }
73 
74 // Retrieve the integer value for a single variable
78 
79  // Do we need the pointer to comp here?
80  switch (vr) {
81  case 1:
82  *value = libsumo_vehicle_getIDCount();
83  return fmi2OK;
84  default:
85  return fmi2Error;
86  }
87 }
88 
90 sumo2fmi_getString(ModelInstance* comp, const fmi2ValueReference vr, const char* value) {
91  switch (vr) {
92  case 0:
93  value = comp->libsumoCallOptions;
94  return fmi2OK;
95  default:
96  return fmi2Error;
97  }
98 }
99 
100 fmi2Status
101 sumo2fmi_setString(ModelInstance* comp, fmi2ValueReference vr, const char* value) {
102  switch (vr) {
103  case 0:
104  comp->freeMemory(comp->libsumoCallOptions);
105  comp->libsumoCallOptions = (char *)comp->allocateMemory(1 + strlen(value), sizeof(char));
106  strcpy(comp->libsumoCallOptions, value);
107  return fmi2OK;
108  default:
109  return fmi2Error;
110  }
111 }
112 
113 fmi2Status
114 sumo2fmi_step(ModelInstance *comp, double tNext) {
116 
117  libsumo_step(tNext);
118  return fmi2OK;
119 }
fmi2Status
@ fmi2OK
@ fmi2Error
unsigned int fmi2ValueReference
int libsumo_vehicle_getIDCount()
void libsumo_step(double time)
void * componentEnvironment
allocateMemoryType allocateMemory
const char * instanceName
freeMemoryType freeMemory
loggerType logger
char * libsumoCallOptions
void sumo2fmi_set_startValues(ModelInstance *comp)
fmi2Status sumo2fmi_step(ModelInstance *comp, double tNext)
fmi2Status sumo2fmi_getInteger(ModelInstance *comp, const fmi2ValueReference vr, int *value)
void sumo2fmi_logError(ModelInstance *comp, const char *message,...)
#define UNREFERENCED_PARAMETER(P)
void sumo2fmi_logMessage(ModelInstance *comp, int status, const char *category, const char *message, va_list args)
fmi2Status sumo2fmi_setString(ModelInstance *comp, fmi2ValueReference vr, const char *value)
fmi2Status sumo2fmi_getString(ModelInstance *comp, const fmi2ValueReference vr, const char *value)