]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/Test/FMI2/fmi2_import_test.c
Add FMILibrary-2.0.3 to org.simantics.fmil.core\native.
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / Test / FMI2 / fmi2_import_test.c
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.
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 <stdio.h>\r
17 #include <stdlib.h>\r
18 #include <stdarg.h>\r
19 \r
20 #include <config_test.h>\r
21 #include <fmilib.h>\r
22 #include <JM/jm_portability.h>\r
23 \r
24 #define BUFFER 1000\r
25 \r
26 /* Logger function used by the FMU internally */\r
27 static void fmi2logger(fmi2_component_environment_t env, fmi2_string_t instanceName, fmi2_status_t status, fmi2_string_t category, fmi2_string_t message, ...)\r
28 {\r
29     int len;\r
30         char msg[BUFFER];\r
31         va_list argp;   \r
32         va_start(argp, message);\r
33         len = jm_vsnprintf(msg, BUFFER, message, argp);\r
34         printf("fmiStatus = %s;  %s (%s): %s\n", fmi2_status_to_string(status), instanceName, category, msg);\r
35 }\r
36 \r
37 static void stepFinished(fmi2_component_environment_t env, fmi2_status_t status) {\r
38         printf("stepFinished is called wiht fmiStatus = %s\n", fmi2_status_to_string(status));\r
39 }\r
40            \r
41 int fmi2_test(fmi_import_context_t* context, const char* dirPath)\r
42 {\r
43         fmi2_callback_functions_t callBackFunctions;\r
44         const char* modelIdentifier;\r
45         const char* modelName;\r
46         const char*  GUID;\r
47         jm_status_enu_t status;\r
48 \r
49         fmi2_import_t* fmu;     \r
50         fmi2_fmu_kind_enu_t fmukind;\r
51 \r
52         callBackFunctions.logger = fmi2logger;\r
53         callBackFunctions.allocateMemory = calloc;\r
54         callBackFunctions.freeMemory = free;\r
55         callBackFunctions.stepFinished = stepFinished;\r
56         callBackFunctions.componentEnvironment = 0;\r
57 \r
58         fmu = fmi2_import_parse_xml(context, dirPath, 0);\r
59 \r
60         if(!fmu) {\r
61                 printf("Error parsing XML, exiting\n");\r
62                 return (CTEST_RETURN_FAIL);\r
63         }\r
64         modelName = fmi2_import_get_model_name(fmu);\r
65         GUID = fmi2_import_get_GUID(fmu);\r
66 \r
67         printf("Model name: %s\n", modelName);\r
68         if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_cs) {\r
69                 modelIdentifier = fmi2_import_get_model_identifier_ME(fmu);\r
70                 printf("Model identifier for ME: %s\n", modelIdentifier);\r
71                 fmukind = fmi2_fmu_kind_me;\r
72         }\r
73         else if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_me) {\r
74                 modelIdentifier = fmi2_import_get_model_identifier_CS(fmu);\r
75                 printf("Model identifier for CS: %s\n", modelIdentifier);\r
76                 fmukind = fmi2_fmu_kind_cs;\r
77         }\r
78         else {\r
79                 printf("Unxepected FMU kind, exiting\n");\r
80                 return (CTEST_RETURN_FAIL);\r
81         }\r
82     printf("Model GUID: %s\n", GUID);\r
83 \r
84         status = fmi2_import_create_dllfmu(fmu, fmukind, &callBackFunctions);\r
85         if (status == jm_status_error) {\r
86                 printf("Could not create the DLL loading mechanism(C-API).\n");\r
87                 return(CTEST_RETURN_FAIL);\r
88         }\r
89 \r
90         printf("Version returned from FMU:   %s\n", fmi2_import_get_version(fmu));\r
91 \r
92         fmi2_import_destroy_dllfmu(fmu);\r
93 \r
94         fmi2_import_free(fmu);\r
95 \r
96         return (CTEST_RETURN_SUCCESS);\r
97 }\r
98 \r
99 \r