FMI Library: part of JModelica.org
fmi2_import_test.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Modelon AB
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the BSD style license.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  FMILIB_License.txt file for more details.
11 
12  You should have received a copy of the FMILIB_License.txt file
13  along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 
20 #include <config_test.h>
21 #include <fmilib.h>
22 #include <JM/jm_portability.h>
23 
24 #define BUFFER 1000
25 
26 /* Logger function used by the FMU internally */
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, ...)
28 {
29  int len;
30  char msg[BUFFER];
31  va_list argp;
32  va_start(argp, message);
33  len = jm_vsnprintf(msg, BUFFER, message, argp);
34  printf("fmiStatus = %s; %s (%s): %s\n", fmi2_status_to_string(status), instanceName, category, msg);
35 }
36 
37 static void stepFinished(fmi2_component_environment_t env, fmi2_status_t status) {
38  printf("stepFinished is called wiht fmiStatus = %s\n", fmi2_status_to_string(status));
39 }
40 
41 int fmi2_test(fmi_import_context_t* context, const char* dirPath)
42 {
43  fmi2_callback_functions_t callBackFunctions;
44  const char* modelIdentifier;
45  const char* modelName;
46  const char* GUID;
47  jm_status_enu_t status;
48 
50  fmi2_fmu_kind_enu_t fmukind;
51 
52  callBackFunctions.logger = fmi2logger;
53  callBackFunctions.allocateMemory = calloc;
54  callBackFunctions.freeMemory = free;
55  callBackFunctions.stepFinished = stepFinished;
56  callBackFunctions.componentEnvironment = 0;
57 
58  fmu = fmi2_import_parse_xml(context, dirPath, 0);
59 
60  if(!fmu) {
61  printf("Error parsing XML, exiting\n");
62  return (CTEST_RETURN_FAIL);
63  }
64  modelName = fmi2_import_get_model_name(fmu);
65  GUID = fmi2_import_get_GUID(fmu);
66 
67  printf("Model name: %s\n", modelName);
69  modelIdentifier = fmi2_import_get_model_identifier_ME(fmu);
70  printf("Model identifier for ME: %s\n", modelIdentifier);
71  fmukind = fmi2_fmu_kind_me;
72  }
74  modelIdentifier = fmi2_import_get_model_identifier_CS(fmu);
75  printf("Model identifier for CS: %s\n", modelIdentifier);
76  fmukind = fmi2_fmu_kind_cs;
77  }
78  else {
79  printf("Unxepected FMU kind, exiting\n");
80  return (CTEST_RETURN_FAIL);
81  }
82  printf("Model GUID: %s\n", GUID);
83 
84  status = fmi2_import_create_dllfmu(fmu, fmukind, &callBackFunctions);
85  if (status == jm_status_error) {
86  printf("Could not create the DLL loading mechanism(C-API).\n");
87  return(CTEST_RETURN_FAIL);
88  }
89 
90  printf("Version returned from FMU: %s\n", fmi2_import_get_version(fmu));
91 
93 
94  fmi2_import_free(fmu);
95 
96  return (CTEST_RETURN_SUCCESS);
97 }
98 
99 
fmi2_step_finished_ft stepFinished
#define BUFFER
fmi2_callback_allocate_memory_ft allocateMemory
FMILIB_EXPORT const char * fmi2_import_get_version(fmi2_import_t *fmu)
Wrapper for the FMI function fmiGetVersion()
FMILIB_EXPORT fmi2_import_t * fmi2_import_parse_xml(fmi_import_context_t *context, const char *dirPath, fmi2_xml_callbacks_t *xml_callbacks)
Create fmi2_import_t structure and parse the FMI 2.0 XML file found in the directory dirName...
int fmi2_test(fmi_import_context_t *context, const char *dirPath)
FMILIB_EXPORT const char * fmi2_import_get_model_name(fmi2_import_t *fmu)
Get model name.
FMILIB_EXPORT fmi2_fmu_kind_enu_t fmi2_import_get_fmu_kind(fmi2_import_t *fmu)
Get the type of the FMU (model exchange or co-simulation)
static void stepFinished(fmi2_component_environment_t env, fmi2_status_t status)
fmi1_capi_t * fmu
FMILIB_EXPORT void fmi2_import_destroy_dllfmu(fmi2_import_t *fmu)
Free a C-API struct. All memory allocated since the struct was created is freed.
fmi2_status_t
fmi2_callback_logger_ft logger
Include file to be used in client applications of the FMI Library.
FMILIB_EXPORT const char * fmi2_import_get_model_identifier_CS(fmi2_import_t *fmu)
Get model identifier for CoSimulation.
fmi2_callback_free_memory_ft freeMemory
fmi2_fmu_kind_enu_t
FMU 2.0 kinds.
Definition: fmi2_enums.h:49
fmi2_component_environment_t componentEnvironment
FMILIB_EXPORT jm_status_enu_t fmi2_import_create_dllfmu(fmi2_import_t *fmu, fmi2_fmu_kind_enu_t fmuKind, const fmi2_callback_functions_t *callBackFunctions)
Create a C-API struct. The C-API struct is a placeholder for the FMI DLL functions.
static void fmi2logger(fmi2_component_environment_t env, fmi2_string_t instanceName, fmi2_status_t status, fmi2_string_t category, fmi2_string_t message,...)
FMILIB_EXPORT const char * fmi2_status_to_string(fmi2_status_t status)
struct fmi_xml_context_t fmi_import_context_t
FMI version independent library context. Opaque struct returned from fmi_import_allocate_context() ...
FMILIB_EXPORT void fmi2_import_free(fmi2_import_t *fmu)
Release the memory allocated.
FMILIB_EXPORT const char * fmi2_import_get_GUID(fmi2_import_t *fmu)
Get FMU GUID.
jm_status_enu_t
Return status codes.
Definition: jm_types.h:44
FMILIB_EXPORT const char * fmi2_import_get_model_identifier_ME(fmi2_import_t *fmu)
Get model identifier for ModelExchange.
struct fmi2_import_t fmi2_import_t
FMU version 2.0 object.
FMILIB_EXPORT int jm_vsnprintf(char *str, size_t size, const char *fmt, va_list al)
C89 compatible implementation of C99 vsnprintf.