]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil/native/FMUSimulator/fmi_util.c.txt
Fixed required environment to JavaSE-1.8 because compilation settings already require it.
[simantics/fmil.git] / org.simantics.fmil / native / FMUSimulator / fmi_util.c.txt
1 /*\r
2     Copyright (C) 2012 Modelon AB\r
3 \r
4     This program is free software: you can redistribute it and/or modify\r
5     it under the terms of the BSD style license.\r
6 \r
7      This program is distributed in the hope that it will be useful,\r
8     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
10     FMILIB_License.txt file for more details.\r
11 \r
12     You should have received a copy of the FMILIB_License.txt file\r
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.\r
14 */\r
15 \r
16 #include <assert.h>\r
17 #include <string.h>\r
18 #include <stdio.h>\r
19 #include <FMI/fmi_util.h>\r
20 #include <FMI/fmi_zip_unzip.h>\r
21 \r
22 char* fmi_construct_dll_dir_name(jm_callbacks* callbacks, const char* fmu_unzipped_path) {\r
23         char* dir_path;\r
24         size_t len;\r
25 \r
26         assert( fmu_unzipped_path && callbacks);\r
27 \r
28         len = \r
29                 strlen(fmu_unzipped_path) + strlen(FMI_FILE_SEP)\r
30                 + strlen(FMI_BINARIES) + strlen(FMI_FILE_SEP) \r
31                 + strlen(FMI_PLATFORM) + strlen(FMI_FILE_SEP) + 1;\r
32 \r
33         dir_path = (char*)callbacks->malloc(len);\r
34         if (dir_path == NULL) {\r
35                 jm_log_fatal(callbacks, "FMIUT", "Failed to allocate memory.");\r
36                 return NULL;\r
37         }\r
38 \r
39         sprintf(dir_path, "%s%s%s%s%s%s", fmu_unzipped_path, FMI_FILE_SEP, FMI_BINARIES, FMI_FILE_SEP, FMI_PLATFORM, FMI_FILE_SEP);/*safe */\r
40 \r
41         return dir_path;\r
42 }\r
43 \r
44 char* fmi_construct_dll_file_name(jm_callbacks* callbacks, const char* dll_dir_name, const char* model_identifier) {\r
45         char* fname;\r
46         size_t len;\r
47         assert(callbacks && model_identifier);\r
48         len = \r
49                 strlen(dll_dir_name) +\r
50                 strlen(model_identifier) \r
51                 + strlen(FMI_DLL_EXT) + 1;\r
52         fname = (char*)callbacks->malloc(len);\r
53         if (fname == NULL) {\r
54                 jm_log_fatal(callbacks, "FMIUT", "Failed to allocate memory.");\r
55                 return NULL;\r
56         }\r
57         sprintf(fname, "%s%s%s", dll_dir_name, model_identifier, FMI_DLL_EXT);/*safe */\r
58 \r
59         return fname;\r
60 }\r
61 \r
62 \r
63 ////\r
64 \r
65 #include <stdio.h>\r
66 #include <stdlib.h>\r
67 #include <stdarg.h>\r
68 #include <string.h>\r
69 #include <errno.h>\r
70 \r
71 #include <fmilib.h>\r
72 \r
73 #ifdef __cplusplus\r
74 extern "C" {\r
75 #endif\r
76 \r
77 typedef struct {\r
78         char *name;\r
79         long vr;\r
80 } FMIL_Variable;\r
81 \r
82 __declspec(dllexport) void* FMI1_CS_LOAD(const char *zipFilePath, const char *unzipFolder);\r
83 __declspec(dllexport) int FMI1_CS_UNLOAD(void* fmu);\r
84 __declspec(dllexport) FMIL_Variable *FMI1_CS_GET_VARIABLES(void* fmu, int *count);\r
85 __declspec(dllexport) int FMI1_CS_INSTANTIATE(void* fmu);\r
86 __declspec(dllexport) int FMI1_CS_INITIALIZE(void* fmu);\r
87 __declspec(dllexport) int FMI1_CS_STEP(void* fmu, double masterTime, double stepSize);\r
88 __declspec(dllexport) int FMI1_CS_SET_REAL(void* fmu, int vr, double value);\r
89 __declspec(dllexport) double FMI1_CS_GET_REAL(void* fmu, int vr);\r
90 __declspec(dllexport) int FMI1_CS_GET_REALS(void* fmu, int *vrs, double *values, int count);\r
91 \r
92 #ifdef __cplusplus\r
93 }\r
94 #endif\r
95 \r
96 #define BUFFER 4096\r
97 \r
98 /* Logger function used by the C-API */\r
99 void importlogger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message)\r
100 {\r
101         printf("module = %s, log level = %d: %s\n", module, log_level, message);\r
102 }\r
103 \r
104 /* Logger function used by the FMU internally */\r
105 void fmilogger(fmi1_component_t c, fmi1_string_t instanceName, fmi1_status_t status, fmi1_string_t category, fmi1_string_t message, ...)\r
106 {\r
107         char msg[BUFFER];\r
108     int len;\r
109         va_list argp;   \r
110         va_start(argp, message);\r
111         /*len=jm_vsnprintf(msg, BUFFER, message, argp);\r
112         printf("fmiStatus = %d;  %s (%s): %s\n", status, instanceName, category, msg);\r
113     if(len > BUFFER) {\r
114         printf("Warning: message was trancated");\r
115     }*/\r
116 }\r
117 \r
118 void *FMI1_CS_LOAD(char *zipFilePath, char *unzipFolder) {\r
119 \r
120         fmi1_callback_functions_t callBackFunctions;\r
121         fmi_import_context_t* context;\r
122         fmi1_fmu_kind_enu_t standard = fmi1_fmu_kind_enu_cs_standalone; \r
123         fmi_version_enu_t version;\r
124     jm_status_enu_t status;\r
125         fmi1_import_t *fmu;\r
126         jm_callbacks* callbacks;\r
127 \r
128         callbacks = (jm_callbacks *)calloc(1, sizeof(jm_callbacks));\r
129 \r
130         callbacks->malloc = malloc;\r
131     callbacks->calloc = calloc;\r
132     callbacks->realloc = realloc;\r
133     callbacks->free = free;\r
134     callbacks->logger = importlogger;\r
135     callbacks->log_level = jm_log_level_debug;\r
136     callbacks->context = 0;\r
137 \r
138         callBackFunctions.logger = fmilogger;\r
139         callBackFunctions.allocateMemory = calloc;\r
140         callBackFunctions.freeMemory = free;\r
141 \r
142         context = fmi_import_allocate_context(callbacks);\r
143 \r
144         version = fmi_import_get_fmi_version(context, zipFilePath, unzipFolder);\r
145 \r
146         fmu = fmi1_import_parse_xml(context, unzipFolder);\r
147 \r
148         status = fmi1_import_create_dllfmu(fmu, callBackFunctions, 0);\r
149 \r
150         fmi_import_free_context(context);\r
151 \r
152         return fmu;\r
153 \r
154 }\r
155 \r
156 int FMI1_CS_UNLOAD(void *fmu_) {\r
157 \r
158         fmi1_import_t *fmu = (fmi1_import_t *)fmu_;\r
159 \r
160         fmi1_import_destroy_dllfmu(fmu);\r
161 \r
162         fmi1_import_free(fmu);\r
163 \r
164         return 1;\r
165 \r
166 }\r
167 \r
168 \r
169 FMIL_Variable *FMI1_CS_GET_VARIABLES(void *fmu, int *count) {\r
170 \r
171     int i;\r
172         FMIL_Variable *result;\r
173     fmi1_import_variable_list_t* vl = fmi1_import_get_variable_list((fmi1_import_t *)fmu);\r
174 \r
175     count[0] = fmi1_import_get_variable_list_size(vl);\r
176 \r
177         result = (FMIL_Variable *)malloc(count[0]*sizeof(FMIL_Variable));\r
178 \r
179     for(i = 0; i < count[0]; i++) {\r
180 \r
181         fmi1_import_variable_t* var = fmi1_import_get_variable(vl, i);\r
182         if(!var) {\r
183 \r
184                         printf("Something wrong with variable %d \n",i);\r
185                         return 0;\r
186 \r
187                 } else {\r
188 \r
189                         fmi1_base_type_enu_t bt;\r
190                         result[i].name = fmi1_import_get_variable_name(var);\r
191                         result[i].vr = fmi1_import_get_variable_vr(var);\r
192 \r
193                 }\r
194 \r
195     }\r
196 \r
197     fmi1_import_free_variable_list(vl);\r
198 \r
199         return result;\r
200 \r
201 }\r
202 \r
203 #define INSTANCE_NAME           "kekkeli"\r
204 \r
205 int FMI1_CS_INSTANTIATE(void *fmu) {\r
206 \r
207         fmi1_string_t fmuLocation;\r
208         fmi1_string_t mimeType;\r
209         fmi1_real_t timeout;\r
210         fmi1_boolean_t visible;\r
211         fmi1_boolean_t interactive;\r
212         fmi1_boolean_t loggingOn;\r
213 \r
214         fmuLocation = "";\r
215         mimeType = "";\r
216         timeout = 0;\r
217         visible = fmi1_false;\r
218         interactive = fmi1_false;\r
219         loggingOn = fmi1_true;\r
220 \r
221         if (fmi1_import_instantiate_slave(fmu, INSTANCE_NAME, NULL, NULL, timeout, fmi1_false, fmi1_false) == NULL) {           \r
222                 printf("fmi1_capi_instantiate_slave:             Failed\n");\r
223                 return 0;\r
224         } else {\r
225                 printf("fmi1_capi_instantiate_slave:             Success\n");\r
226         }\r
227         return 0;\r
228 \r
229 }\r
230 \r
231 int FMI1_CS_INITIALIZE(void *fmu) {\r
232 \r
233         fmi1_status_t status;\r
234         fmi1_real_t tStart;\r
235         fmi1_real_t tStop;\r
236         fmi1_boolean_t StopTimeDefined;\r
237 \r
238         tStart = 0;\r
239         tStop = 10;\r
240         StopTimeDefined = fmi1_false;\r
241 \r
242         status = fmi1_import_initialize_slave((fmi1_import_t *)fmu, tStart, StopTimeDefined, tStop);\r
243         if (status == fmi1_status_error || status == fmi1_status_fatal) {\r
244                 printf("fmi1_capi_initialize_slave:              Failed\n");\r
245                 return 0;\r
246         } else {\r
247                 printf("fmi1_capi_initialize_slave:              Success\n");\r
248         }\r
249         return 0;\r
250 \r
251 }\r
252 \r
253 int FMI1_CS_STEP(void *fmu, double masterTime, double stepSize) {\r
254 \r
255     fmi1_status_t status;\r
256 \r
257         status = fmi1_import_do_step((fmi1_import_t *)fmu, (fmi1_real_t)masterTime, (fmi1_real_t)stepSize, fmi1_true);\r
258         if (status == fmi1_status_error || status == fmi1_status_fatal) {\r
259                 printf("fmi1_capi_do_step:                       Failed\n");\r
260                 return 0;\r
261         } else {\r
262                 printf("fmi1_capi_do_step:                       Success\n");\r
263         }\r
264 \r
265         return 0;\r
266 \r
267 }\r
268 \r
269 int FMI1_CS_SET_REAL(void *fmu, long valueId, double value) {\r
270 \r
271         fmi1_value_reference_t vr = valueId;\r
272         fmi1_import_set_real((fmi1_import_t *)fmu, &vr, 1, &value);\r
273 \r
274         return 0;\r
275 \r
276 }\r
277 \r
278 double FMI1_CS_GET_REAL(void *fmu, int valueReference) {\r
279 \r
280         fmi1_value_reference_t vr = valueReference;\r
281         fmi1_real_t value;\r
282         fmi1_import_get_real((fmi1_import_t *)fmu, &vr, 1, &value);\r
283         return value;\r
284 \r
285 }\r
286 \r
287 int FMI1_CS_GET_REALS(void *fmu, int *valueReferences, double *result, int count) {\r
288 \r
289         fmi1_value_reference_t *vrs = valueReferences;\r
290         fmi1_real_t value;\r
291         fmi1_import_get_real((fmi1_import_t *)fmu, vrs, count, result);\r
292         return 1;\r
293 \r
294 }\r