fmi2_import_t* fmi2_import_parse_xml( fmi_import_context_t* context, const char* dirPath, fmi2_xml_callbacks_t* xml_callbacks) { char* xmlPath; char absPath[FILENAME_MAX + 2]; fmi2_import_t* fmu = 0; if(strlen(dirPath) + 20 > FILENAME_MAX) { jm_log_fatal(context->callbacks, module, "Directory path for FMU is too long"); return 0; } xmlPath = fmi_import_get_model_description_path(dirPath, context->callbacks); fmu = fmi2_import_allocate(context->callbacks); if(!fmu) { context->callbacks->free(xmlPath); return 0; } if(jm_get_dir_abspath(context->callbacks, dirPath, absPath, FILENAME_MAX + 2)) { size_t len = strlen(absPath); strcpy(absPath + len, FMI_FILE_SEP "resources"); fmu->resourceLocation = fmi_import_create_URL_from_abs_path(context->callbacks, absPath); } fmu->dirPath = context->callbacks->malloc(strlen(dirPath) + 1); if (!fmu->dirPath || !fmu->resourceLocation) { jm_log_fatal( context->callbacks, "FMILIB", "Could not allocated memory"); fmi2_import_free(fmu); context->callbacks->free(xmlPath); return 0; } strcpy(fmu->dirPath, dirPath); jm_log_verbose( context->callbacks, "FMILIB", "Parsing model description XML"); if(fmi2_xml_parse_model_description( fmu->md, xmlPath, xml_callbacks)) { fmi2_import_free(fmu); fmu = 0; } context->callbacks->free(xmlPath); if(fmu) jm_log_verbose( context->callbacks, "FMILIB", "Parsing finished successfully"); return fmu; }
int fmi2_test(fmi_import_context_t* context, const char* dirPath) { fmi2_callback_functions_t callBackFunctions; const char* modelIdentifier; const char* modelName; const char* GUID; jm_status_enu_t status; fmi2_import_t* fmu; fmi2_fmu_kind_enu_t fmukind; callBackFunctions.logger = fmi2logger; callBackFunctions.allocateMemory = calloc; callBackFunctions.freeMemory = free; callBackFunctions.stepFinished = stepFinished; callBackFunctions.componentEnvironment = 0; fmu = fmi2_import_parse_xml(context, dirPath, 0); if(!fmu) { printf("Error parsing XML, exiting\n"); return (CTEST_RETURN_FAIL); } modelName = fmi2_import_get_model_name(fmu); GUID = fmi2_import_get_GUID(fmu); printf("Model name: %s\n", modelName); if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_cs) { modelIdentifier = fmi2_import_get_model_identifier_ME(fmu); printf("Model identifier for ME: %s\n", modelIdentifier); fmukind = fmi2_fmu_kind_me; } else if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_me) { modelIdentifier = fmi2_import_get_model_identifier_CS(fmu); printf("Model identifier for CS: %s\n", modelIdentifier); fmukind = fmi2_fmu_kind_cs; } else { printf("Unxepected FMU kind, exiting\n"); return (CTEST_RETURN_FAIL); } printf("Model GUID: %s\n", GUID); status = fmi2_import_create_dllfmu(fmu, fmukind, &callBackFunctions); if (status == jm_status_error) { printf("Could not create the DLL loading mechanism(C-API).\n"); return(CTEST_RETURN_FAIL); } printf("Version returned from FMU: %s\n", fmi2_import_get_version(fmu)); fmi2_import_destroy_dllfmu(fmu); fmi2_import_free(fmu); return (CTEST_RETURN_SUCCESS); }
void FMI2ModelExchangeDestructor_OMC(void* in_fmi2me) { FMI2ModelExchange* FMI2ME = (FMI2ModelExchange*)in_fmi2me; fmi2_import_terminate(FMI2ME->FMIImportInstance); fmi2_import_free_instance(FMI2ME->FMIImportInstance); fmi2_import_destroy_dllfmu(FMI2ME->FMIImportInstance); fmi2_import_free(FMI2ME->FMIImportInstance); fmi_import_free_context(FMI2ME->FMIImportContext); free(FMI2ME->FMIWorkingDirectory); free(FMI2ME->FMIInstanceName); free(FMI2ME->FMIEventInfo); }
int main(int argc, char *argv[]) { fmi2_callback_functions_t callBackFunctions; const char* FMUPath; const char* tmpPath; jm_callbacks callbacks; fmi_import_context_t* context; fmi_version_enu_t version; jm_status_enu_t status; fmi2_import_t* fmu; if(argc < 3) { printf("Usage: %s <fmu_file> <temporary_dir>\n", argv[0]); do_exit(CTEST_RETURN_FAIL); } FMUPath = argv[1]; tmpPath = argv[2]; callbacks.malloc = malloc; callbacks.calloc = calloc; callbacks.realloc = realloc; callbacks.free = free; callbacks.logger = jm_default_logger; callbacks.log_level = jm_log_level_debug; callbacks.context = 0; #ifdef FMILIB_GENERATE_BUILD_STAMP printf("Library build stamp:\n%s\n", fmilib_get_build_stamp()); #endif context = fmi_import_allocate_context(&callbacks); version = fmi_import_get_fmi_version(context, FMUPath, tmpPath); if(version != fmi_version_2_0_enu) { printf("Only version 2.0 is supported by this code\n"); do_exit(CTEST_RETURN_FAIL); } fmu = fmi2_import_parse_xml(context, tmpPath,0); if(!fmu) { printf("Error parsing XML, exiting\n"); do_exit(CTEST_RETURN_FAIL); } if(fmi2_import_get_fmu_kind(fmu) == fmi2_fmu_kind_cs) { printf("Only ME 2.0 is supported by this code\n"); do_exit(CTEST_RETURN_FAIL); } callBackFunctions.logger = fmi2_log_forwarding; callBackFunctions.allocateMemory = calloc; callBackFunctions.freeMemory = free; callBackFunctions.componentEnvironment = fmu; status = fmi2_import_create_dllfmu(fmu, fmi2_fmu_kind_me, &callBackFunctions); if (status == jm_status_error) { printf("Could not create the DLL loading mechanism(C-API test).\n"); do_exit(CTEST_RETURN_FAIL); } test_simulate_me(fmu); fmi2_import_destroy_dllfmu(fmu); fmi2_import_free(fmu); fmi_import_free_context(context); printf("Everything seems to be OK since you got this far=)!\n"); do_exit(CTEST_RETURN_SUCCESS); return 0; }