OMCompiler/SimulationRuntime/c/simulation/solver/embedded_server.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "../../util/omc_init.h" | ||
| 29 | #include "embedded_server.h" | ||
| 30 | |||
| 31 | #if defined(__MINGW32__) || defined(_MSC_VER) | ||
| 32 | #include "util/omc_msvc.h" | ||
| 33 | #define UPC_DA | ||
| 34 | #define DLL_EXT ".dll" | ||
| 35 | #elif defined(__APPLE__) | ||
| 36 | #include <dlfcn.h> | ||
| 37 | #define DLL_EXT ".dylib" | ||
| 38 | #else | ||
| 39 | #include <dlfcn.h> | ||
| 40 | #define DLL_EXT ".so" | ||
| 41 | #endif | ||
| 42 | |||
| 43 | 1 | void* no_embedded_server_init(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) | |
| 44 | { | ||
| 45 | 1 | return NULL; | |
| 46 | } | ||
| 47 | |||
| 48 | 1 | void no_wait_for_step(void *handle) | |
| 49 | { | ||
| 50 | 1 | } | |
| 51 | |||
| 52 | 1 | void no_embedded_server_deinit(void *handle) | |
| 53 | { | ||
| 54 | 1 | } | |
| 55 | |||
| 56 | 1 | int no_embedded_server_update(void *handle, double tout, int *terminate) | |
| 57 | { | ||
| 58 | 1 | return 0; | |
| 59 | } | ||
| 60 | |||
| 61 | void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) = no_embedded_server_init; | ||
| 62 | void (*wait_for_step)(void*) = no_wait_for_step; | ||
| 63 | void (*embedded_server_deinit)(void*) = no_embedded_server_deinit; | ||
| 64 | // Tells the embedded server that a simulation step has passed; the server | ||
| 65 | // can read/write values from/to the simulator | ||
| 66 | int (*embedded_server_update)(void*, double tout, int*) = no_embedded_server_update; | ||
| 67 | |||
| 68 | 1 | void* embedded_server_load_functions(const char *server_name) | |
| 69 | { | ||
| 70 | void *dll, *funcInit, *funcWaitForStep, *funcDeinit, *funcUpdate; | ||
| 71 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (NULL==server_name || 0==strcmp("none", server_name)) { |
| 72 | return NULL; | ||
| 73 | } | ||
| 74 | ✗ | if (0==strcmp("opc-ua", server_name)) { | |
| 75 | server_name = "libomopcua" DLL_EXT; | ||
| 76 | ✗ | } else if (0==strcmp("opc-da", server_name)) { | |
| 77 | #if defined(UPC_DA) | ||
| 78 | server_name = "libomopcda" DLL_EXT; | ||
| 79 | #else | ||
| 80 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "OPC DA interface is not available on this platform (requires WIN32)"); | |
| 81 | ✗ | OMC_THROW(); | |
| 82 | #endif | ||
| 83 | } | ||
| 84 | ✗ | infoStreamPrint(OMC_LOG_DEBUG, 0, "Try to load embedded server %s", server_name); | |
| 85 | ✗ | dll = dlopen(server_name, RTLD_LAZY); | |
| 86 | |||
| 87 | ✗ | if (dll == NULL) { | |
| 88 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load shared object %s: %s\n", server_name, dlerror()); | |
| 89 | ✗ | OMC_THROW(); | |
| 90 | } | ||
| 91 | |||
| 92 | ✗ | funcInit = dlsym(dll, "omc_embedded_server_init"); | |
| 93 | ✗ | if (!funcInit) { | |
| 94 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load function omc_embedded_server_init: %s\n", dlerror()); | |
| 95 | ✗ | OMC_THROW(); | |
| 96 | } | ||
| 97 | ✗ | funcWaitForStep = dlsym(dll, "omc_wait_for_step"); | |
| 98 | ✗ | if (!funcWaitForStep) { | |
| 99 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load function omc_wait_for_step: %s\n", dlerror()); | |
| 100 | ✗ | OMC_THROW(); | |
| 101 | } | ||
| 102 | ✗ | funcDeinit = dlsym(dll, "omc_embedded_server_deinit"); | |
| 103 | ✗ | if (!funcDeinit) { | |
| 104 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load function omc_embedded_server_deinit: %s\n", dlerror()); | |
| 105 | ✗ | OMC_THROW(); | |
| 106 | } | ||
| 107 | ✗ | funcUpdate = dlsym(dll, "omc_embedded_server_update"); | |
| 108 | ✗ | if (!funcUpdate) { | |
| 109 | ✗ | errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load function omc_embedded_server_update: %s\n", dlerror()); | |
| 110 | ✗ | OMC_THROW(); | |
| 111 | } | ||
| 112 | |||
| 113 | // ISO C forbids assignment between function pointer and ‘void *’ [-Wpedantic] | ||
| 114 | // dlsym with -pedantic is tricky, the following workaround is from the dlsym man page but still a hack | ||
| 115 | ✗ | *(void**)(&embedded_server_init) = funcInit; | |
| 116 | ✗ | *(void**)(&wait_for_step) = funcWaitForStep; | |
| 117 | ✗ | *(void**)(&embedded_server_deinit) = funcDeinit; | |
| 118 | ✗ | *(void**)(&embedded_server_update) = funcUpdate; | |
| 119 | |||
| 120 | ✗ | return dll; | |
| 121 | } | ||
| 122 | |||
| 123 | 1 | void embedded_server_unload_functions(void *dllHandle) | |
| 124 | { | ||
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (dllHandle) { |
| 126 | ✗ | dlclose(dllHandle); | |
| 127 | } | ||
| 128 | 1 | } | |
| 129 |