SMACC2
Loading...
Searching...
No Matches
cp_modbus_connection.hpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#pragma once
21
22#include <modbus/modbus.h>
23
24#include <functional>
25#include <mutex>
26#include <smacc2/smacc.hpp>
27#include <string>
28
29namespace cl_modbus_tcp_relay
30{
31
32// Forward declarations for events
33template <typename TSource, typename TOrthogonal>
35
36template <typename TSource, typename TOrthogonal>
38
57{
58public:
60 virtual ~CpModbusConnection();
61
62 void onInitialize() override;
63
67 template <typename TOrthogonal, typename TClient>
76
77 // Connection management
78 bool connect();
79 void disconnect();
80 bool reconnect();
81 bool isConnected() const;
82
83 // Thread-safe context access
84 modbus_t * getContext();
85 std::mutex & getMutex();
86
87 // Configuration accessors
88 std::string getIpAddress() const { return ip_address_; }
89 int getPort() const { return port_; }
90 int getSlaveId() const { return slave_id_; }
91
92 // Signals for connection state changes
95 smacc2::SmaccSignal<void(const std::string &)> onConnectionError_;
96
97 // Signal connection helpers
98 template <typename T>
99 smacc2::SmaccSignalConnection onConnectionLost(void (T::*callback)(), T * object)
100 {
101 return this->getStateMachine()->createSignalConnection(onConnectionLost_, callback, object);
102 }
103
104 template <typename T>
105 smacc2::SmaccSignalConnection onConnectionRestored(void (T::*callback)(), T * object)
106 {
107 return this->getStateMachine()->createSignalConnection(onConnectionRestored_, callback, object);
108 }
109
110 template <typename T>
112 void (T::*callback)(const std::string &), T * object)
113 {
114 return this->getStateMachine()->createSignalConnection(onConnectionError_, callback, object);
115 }
116
117protected:
118 void update() override; // Heartbeat check
119
120private:
121 // Configuration loaded from YAML in onInitialize()
122 std::string ip_address_;
123 int port_;
127
128 // Modbus context
129 modbus_t * ctx_;
131 mutable std::mutex mutex_;
132
133 // Event posting functions
134 std::function<void()> postConnectionLostEvent_;
135 std::function<void()> postConnectionRestoredEvent_;
136
137 // Helper for parameter loading
138 template <typename T>
139 void declareAndLoadParam(const std::string & name, T & value, const T & default_val);
140};
141
142} // namespace cl_modbus_tcp_relay
Component that manages Modbus TCP connection lifecycle and heartbeat monitoring.
smacc2::SmaccSignalConnection onConnectionError(void(T::*callback)(const std::string &), T *object)
smacc2::SmaccSignalConnection onConnectionLost(void(T::*callback)(), T *object)
smacc2::SmaccSignal< void()> onConnectionLost_
void declareAndLoadParam(const std::string &name, T &value, const T &default_val)
smacc2::SmaccSignal< void()> onConnectionRestored_
void onStateOrthogonalAllocation()
Configure component for event posting during orthogonal allocation.
smacc2::SmaccSignalConnection onConnectionRestored(void(T::*callback)(), T *object)
smacc2::SmaccSignal< void(const std::string &)> onConnectionError_
ISmaccStateMachine * getStateMachine()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection SmaccSignalConnection