From d8f55eb9abd516c13660a8749ae491cf9ea08f4c Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:03:58 +0200 Subject: [PATCH 1/6] Enable use of client/server config with ZeroMQ Signed-off-by: ClemensLinnhoff --- README.md | 13 +++++++------ src/OSMP.cpp | 38 +++++++++++++++++++++++++++++++------ src/OSMP.h | 5 ++++- src/modelDescription.in.xml | 3 +++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6b5f44b..fa6b347 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,13 @@ The following FMI parameters can be set. Either `sender` or `receiver` have to be set to _true_. Otherwise, the FMU will return with an error. -| Type | Parameter | Default | Description | -|---------|------------|-------------|--------------------------------------------| -| Boolean | `sender` | _true_ | Set if Proxy shall send data via TCP/IP | -| Boolean | `receiver` | _false_ | Set if Proxy shall receive data via TCP/IP | -| String | `ip` | _127.0.0.1_ | IP address of TCP connection | -| String | `port` | _3456_ | Port of TCP connection | +| Type | Parameter | Default | Description | +|---------|------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Boolean | `sender` | _true_ | Set if Proxy shall send data via TCP/IP | +| Boolean | `receiver` | _false_ | Set if Proxy shall receive data via TCP/IP | +| Boolean | `pushpull` | _false_ | If true, use [Push/Pull](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pushpull.html) ZeroMQ configuration, if false, use [Client/Server](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/client_server.html) config. | +| String | `ip` | _127.0.0.1_ | IP address of TCP connection | +| String | `port` | _3456_ | Port of TCP connection | ## Interface diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 8da5406..80af864 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -132,11 +132,10 @@ fmi2Status OSMP::DoInit() // Set default values SetFmiSender(1); SetFmiReceiver(0); + SetFmiPushPull(1); SetFmiIp("127.0.0.1"); SetFmiPort("3456"); - // todo: set default values - return fmi2OK; } @@ -156,22 +155,36 @@ fmi2Status OSMP::DoExitInitializationMode() string address = "tcp://" + FmiIp() + ":" + FmiPort(); std::cout << address << std::endl; const char* protocol = address.c_str(); + if (FmiSender() != 0) { - socket_ = zmq::socket_t(context_, ZMQ_PUSH); + if (FmiPushPull()) // Push/Pull configuration + { + socket_ = zmq::socket_t(context_, ZMQ_PUSH); + } + else // Server/Client configuration + { + socket_ = zmq::socket_t(context_, ZMQ_REP); + } const int wait_time_ms = 5000; zmq_setsockopt(socket_, ZMQ_SNDTIMEO, &wait_time_ms, sizeof(wait_time_ms)); socket_.bind(protocol); - std::cout << "push" << std::endl; } else { - socket_ = zmq::socket_t(context_, ZMQ_PULL); + if (FmiPushPull()) // Push/Pull configuration + { + socket_ = zmq::socket_t(context_, ZMQ_PULL); + } + else // Server/Client configuration + { + socket_ = zmq::socket_t(context_, ZMQ_REQ); + } const int wait_time_ms = 5000; zmq_setsockopt(socket_, ZMQ_RCVTIMEO, &wait_time_ms, sizeof(wait_time_ms)); socket_.connect(protocol); - std::cout << "pull" << std::endl; } + return fmi2OK; } @@ -193,6 +206,11 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica if (FmiSender() != 0) { + if (!FmiPushPull()) // Server/Client configuration + { + zmq::message_t request_message; + socket_.recv(request_message, zmq::recv_flags::none); + } zmq::message_t send_message(buffer, buffer_size, nullptr); auto success = socket_.send(send_message, zmq::send_flags::none); if (success.has_value()) @@ -207,6 +225,13 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica } else if (FmiReceiver() != 0) { + if (!FmiPushPull()) // Server/Client configuration + { + std::stringstream s; + s << "ready"; + zmq::message_t send_message(s.str().data(), s.str().size()); + socket_.send(send_message, zmq::send_flags::none); + } zmq::message_t rec_message; auto success = socket_.recv(rec_message, zmq::recv_flags::none); if (success.has_value()) @@ -224,6 +249,7 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica NormalLog("OSMP", "Either sender or receiver has to be set to true."); output_status = fmi2Error; } + return output_status; } diff --git a/src/OSMP.h b/src/OSMP.h index 72044e0..e1983e5 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -42,7 +42,8 @@ #define FMI_BOOLEAN_VALID_IDX 0 #define FMI_BOOLEAN_SENDER_IDX 1 #define FMI_BOOLEAN_RECEIVER_IDX 2 -#define FMI_BOOLEAN_LAST_IDX FMI_BOOLEAN_RECEIVER_IDX +#define FMI_BOOLEAN_PUSHPULL_IDX 3 +#define FMI_BOOLEAN_LAST_IDX FMI_BOOLEAN_PUSHPULL_IDX #define FMI_BOOLEAN_VARS (FMI_BOOLEAN_LAST_IDX + 1) /* Integer Variables */ @@ -233,6 +234,8 @@ class OSMP void SetFmiReceiver(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX] = value; } fmi2Boolean FmiSender() { return boolean_vars_[FMI_BOOLEAN_SENDER_IDX]; } void SetFmiSender(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_SENDER_IDX] = value; } + fmi2Boolean FmiPushPull() { return boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX]; } + void SetFmiPushPull(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX] = value; } string FmiIp() { return string_vars_[FMI_STRING_IP_IDX]; } void SetFmiIp(fmi2String value) { string_vars_[FMI_STRING_IP_IDX] = value; } string FmiPort() { return string_vars_[FMI_STRING_PORT_IDX]; } diff --git a/src/modelDescription.in.xml b/src/modelDescription.in.xml index b5b946a..3df024c 100644 --- a/src/modelDescription.in.xml +++ b/src/modelDescription.in.xml @@ -51,6 +51,9 @@ + + + From 24748c62d9a190f2c1c324f3d6c763c0ce967eed Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:11:03 +0200 Subject: [PATCH 2/6] Fix srmd validator action Signed-off-by: ClemensLinnhoff --- .github/workflows/srmd-validator.yml | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/srmd-validator.yml b/.github/workflows/srmd-validator.yml index 86d86fe..cf2279d 100644 --- a/.github/workflows/srmd-validator.yml +++ b/.github/workflows/srmd-validator.yml @@ -8,25 +8,25 @@ jobs: name: SRMD Validator runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@master + - name: Checkout + uses: actions/checkout@master - - name: Get Sensor Model Testing - run: git clone https://github.com/openMSL/sl-1-5-sensor-model-testing.git + - name: Get SRMD Validator + run: git clone https://github.com/openMSL/sl-5-1-srmd-validator.git - - name: Init Submodules - working-directory: sl-1-5-sensor-model-testing - run: git submodule update --init --recursive + - name: Init Submodules + working-directory: sl-5-1-srmd-validator + run: git submodule update --init --recursive - - name: Install Dependencies - run: pip3 install xmlschema + - name: Install Dependencies + run: pip3 install xmlschema - - name: Validate SRMD - working-directory: sl-1-5-sensor-model-testing/src/srmd-validator - run: python3 srmd-validator.py + - name: Validate SRMD + working-directory: sl-5-1-srmd-validator + run: python3 srmd-validator.py - - name: Commit ID - working-directory: sl-1-5-sensor-model-testing - run: | - echo "Commit ID: " - echo $(git rev-parse --short HEAD) + - name: Commit ID + working-directory: sl-5-1-srmd-validator + run: | + echo "Commit ID: " + echo $(git rev-parse --short HEAD) From b3b598b716f075e4dc1d289a084e3d822cff3d53 Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:29:55 +0200 Subject: [PATCH 3/6] Switched fmpy to working version Signed-off-by: ClemensLinnhoff --- .github/workflows/fmu_checker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/fmu_checker.yml b/.github/workflows/fmu_checker.yml index 9d75c0a..aae10c2 100644 --- a/.github/workflows/fmu_checker.yml +++ b/.github/workflows/fmu_checker.yml @@ -17,7 +17,7 @@ jobs: key: ${{ runner.os }}-model-fmu - name: Install FMPy - run: python -m pip install fmpy[complete] + run: python -m pip install fmpy==0.3.16 - name: Run FMPy Validate working-directory: /tmp/model_fmu From 22023148adf9b057d46b448743934af07317fcee Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:31:49 +0200 Subject: [PATCH 4/6] Formatting Signed-off-by: ClemensLinnhoff --- src/OSMP.cpp | 12 ++++++------ src/OSMP.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 80af864..6c6b5f9 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -158,11 +158,11 @@ fmi2Status OSMP::DoExitInitializationMode() if (FmiSender() != 0) { - if (FmiPushPull()) // Push/Pull configuration + if (FmiPushPull() != 0) // Push/Pull configuration { socket_ = zmq::socket_t(context_, ZMQ_PUSH); } - else // Server/Client configuration + else // Server/Client configuration { socket_ = zmq::socket_t(context_, ZMQ_REP); } @@ -172,11 +172,11 @@ fmi2Status OSMP::DoExitInitializationMode() } else { - if (FmiPushPull()) // Push/Pull configuration + if (FmiPushPull() != 0) // Push/Pull configuration { socket_ = zmq::socket_t(context_, ZMQ_PULL); } - else // Server/Client configuration + else // Server/Client configuration { socket_ = zmq::socket_t(context_, ZMQ_REQ); } @@ -206,7 +206,7 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica if (FmiSender() != 0) { - if (!FmiPushPull()) // Server/Client configuration + if (FmiPushPull() == 0) // Server/Client configuration { zmq::message_t request_message; socket_.recv(request_message, zmq::recv_flags::none); @@ -225,7 +225,7 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica } else if (FmiReceiver() != 0) { - if (!FmiPushPull()) // Server/Client configuration + if (FmiPushPull() == 0) // Server/Client configuration { std::stringstream s; s << "ready"; diff --git a/src/OSMP.h b/src/OSMP.h index e1983e5..cbdd5fe 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -178,7 +178,7 @@ class OSMP } #endif #ifdef PUBLIC_LOGGING - if (logging_on_ && (logging_categories_.count(category) != 0u)) + if (logging_on_ && (logging_categories_.count(category) != 0U)) { functions_.logger(functions_.componentEnvironment, instance_name_.c_str(), fmi2OK, category, buffer); } From 1c14f1b6377568fb0f05ba9338ff71a2373d516f Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:39:32 +0200 Subject: [PATCH 5/6] Formatting Signed-off-by: ClemensLinnhoff --- src/OSMP.h | 60 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/src/OSMP.h b/src/OSMP.h index cbdd5fe..f289c08 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -228,16 +228,52 @@ class OSMP zmq::socket_t socket_; /* Simple Accessors */ - fmi2Boolean FmiValid() { return boolean_vars_[FMI_BOOLEAN_VALID_IDX]; } - void SetFmiValid(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_VALID_IDX] = value; } - fmi2Boolean FmiReceiver() { return boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX]; } - void SetFmiReceiver(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX] = value; } - fmi2Boolean FmiSender() { return boolean_vars_[FMI_BOOLEAN_SENDER_IDX]; } - void SetFmiSender(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_SENDER_IDX] = value; } - fmi2Boolean FmiPushPull() { return boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX]; } - void SetFmiPushPull(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX] = value; } - string FmiIp() { return string_vars_[FMI_STRING_IP_IDX]; } - void SetFmiIp(fmi2String value) { string_vars_[FMI_STRING_IP_IDX] = value; } - string FmiPort() { return string_vars_[FMI_STRING_PORT_IDX]; } - void SetFmiPort(fmi2String value) { string_vars_[FMI_STRING_PORT_IDX] = value; } + fmi2Boolean FmiValid() + { + return boolean_vars_[FMI_BOOLEAN_VALID_IDX]; + } + void SetFmiValid(fmi2Boolean value) + { + boolean_vars_[FMI_BOOLEAN_VALID_IDX] = value; + } + fmi2Boolean FmiReceiver() + { + return boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX]; + } + void SetFmiReceiver(fmi2Boolean value) + { + boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX] = value; + } + fmi2Boolean FmiSender() + { + return boolean_vars_[FMI_BOOLEAN_SENDER_IDX]; + } + void SetFmiSender(fmi2Boolean value) + { + boolean_vars_[FMI_BOOLEAN_SENDER_IDX] = value; + } + fmi2Boolean FmiPushPull() + { + return boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX]; + } + void SetFmiPushPull(fmi2Boolean value) + { + boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX] = value; + } + string FmiIp() + { + return string_vars_[FMI_STRING_IP_IDX]; + } + void SetFmiIp(fmi2String value) + { + string_vars_[FMI_STRING_IP_IDX] = value; + } + string FmiPort() + { + return string_vars_[FMI_STRING_PORT_IDX]; + } + void SetFmiPort(fmi2String value) + { + string_vars_[FMI_STRING_PORT_IDX] = value; + } }; From e3c72a331e6da13db264cee903b0cd307257cdf6 Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Tue, 10 Oct 2023 18:45:51 +0200 Subject: [PATCH 6/6] Adjust copyright Signed-off-by: ClemensLinnhoff --- src/OSMP.cpp | 1 + src/OSMP.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 6c6b5f9..80316c3 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -1,6 +1,7 @@ // // Copyright 2016 -- 2018 PMSF IT Consulting Pierre R. Mai // Copyright 2023 BMW AG +// Copyright 2023 Persival GmbH // SPDX-License-Identifier: MPL-2.0 // diff --git a/src/OSMP.h b/src/OSMP.h index f289c08..b90dc39 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -1,6 +1,7 @@ // // Copyright 2016 -- 2018 PMSF IT Consulting Pierre R. Mai // Copyright 2023 BMW AG +// Copyright 2023 Persival GmbH // SPDX-License-Identifier: MPL-2.0 //