BAOS SDK v2  1.2.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
Loading...
Searching...
No Matches
TcpIO.h
Go to the documentation of this file.
1//
2// Copyright (c) 2002-2026 WEINZIERL ENGINEERING GmbH
3// All rights reserved.
4//
5// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
8// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
9// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
11//
12
13#ifndef __BAOSLIB_TCP_IO_H__
14#define __BAOSLIB_TCP_IO_H__
15
16#include "baos/BaosLib_export.h"
17#include "baos/config.h"
19
20#include "wzcpp/features/concurrent/executor.h"
21
23#include <array>
24#include <atomic>
25#include <memory>
26#include <string>
28
29
35namespace wz::baos::ip
36{
37
38class Ip4Driver;
39
46{
47public:
48 using UPtr = std::unique_ptr<PacketizerBase>;
49 PacketizerBase() = delete;
50
58 PacketizerBase(const Buffer& startPattern, std::uint16_t sizePosition, std::uint16_t sizeBytes);
59
60 virtual ~PacketizerBase() = default;
61
69 virtual bool packetize(const Buffer& buf,
70 std::uint16_t bufferSize,
71 std::int32_t& offset,
72 std::uint16_t& length) = 0;
73
74protected:
76 const std::int32_t sizePosition_{-1};
77 const std::uint16_t sizeBytes_{0};
78};
79
84{
85public:
86 using UPtr = std::unique_ptr<KnxNetIPPacketizer>;
87
93
94 bool packetize(const Buffer& buf, std::uint16_t bufferSize, std::int32_t& offset, std::uint16_t& length) override;
95
96 void setAcceptedProtocolVersions(const std::vector<std::uint8_t>& versions);
97
98protected:
99 constexpr static std::uint8_t HEADER_SIZE{6};
100 constexpr static std::uint8_t PROTOCOL_VERSION_POSITION{1};
101 std::vector<std::uint8_t> acceptedProtocolVersions_{0x10,0x20};
102};
103
104
105/******************************************************************
106 * TcpIO
107 ******************************************************************/
108
115{
116public:
117 using UPtr = std::unique_ptr<TcpIO>;
118 using UniqueLock = std::unique_lock<std::mutex>;
119
126 TcpIO(BaosMsgQueue& msgQueue, wzcpp::ExecutorBase& executor);
127
129
136 TcpIO(BaosMsgQueue& msgQueue, std::unique_ptr<Ip4Driver>& driver, wzcpp::ExecutorBase& executor);
137
145
146 ~TcpIO();
147
153 void send(const Buffer& buf);
154
161 void open(const std::string& ip4Address, std::uint16_t port);
162
166 void close();
167
172
178 bool isOpen();
179
180private:
181 void rxHandler();
182
183protected:
184 static std::atomic<std::uint64_t> msgId;
185 std::atomic<bool> rxShouldStop_{false};
186 std::condition_variable cvFinished_;
187 mutable std::mutex cvFinished_mut_;
188 bool stopped_{false};
190
191 std::atomic<bool> isOpen_{false};
193 wzcpp::ExecutorBase& executor_;
194 std::unique_ptr<Ip4Driver> driver_;
196};
197
198} // namespace wz::baos::ip
199
200#endif //__BAOSLIB_TCP_IO_H__
Code regarding Input/Output Events.
A specialization for (connection oriented) connection to the BAOS device via a TCP connection.
Definition BaosTcpConnection.h:182
Specializaton for a Packetizer which detects KnxNet/Ip packets.
Definition TcpIO.h:84
bool packetize(const Buffer &buf, std::uint16_t bufferSize, std::int32_t &offset, std::uint16_t &length) override
KnxNetIPPacketizer()
Construct a new Knx Net IP Packetizer object with given pattern. size position and length.
std::unique_ptr< KnxNetIPPacketizer > UPtr
Alias for a std::unique_ptr to this class.
Definition TcpIO.h:86
To find packets in the TCP byte stream a pattern and the following toatl packet length are used.
Definition TcpIO.h:46
const Buffer startPattern_
The pattern which marks the beginning of a packet in the byte stream.
Definition TcpIO.h:75
PacketizerBase(const Buffer &startPattern, std::uint16_t sizePosition, std::uint16_t sizeBytes)
Construct a new Packetizer Base object from a given pattern.
virtual bool packetize(const Buffer &buf, std::uint16_t bufferSize, std::int32_t &offset, std::uint16_t &length)=0
std::unique_ptr< PacketizerBase > UPtr
Alias for a std::unique_ptr to this class.
Definition TcpIO.h:48
Base class for a TCP input output connection.
Definition TcpIO.h:115
bool isOpen()
Get the open state of the TCP connection.
void close()
Close the TCP connection.
std::unique_lock< std::mutex > UniqueLock
Alias for unique lock used by this class.
Definition TcpIO.h:118
BaosMsgQueue & msgQueue_
Reference to the message queue in which events will be posted by this class.
Definition TcpIO.h:192
void open(const std::string &ip4Address, std::uint16_t port)
Open the TCP connection to the remote device.
TcpIO(BaosMsgQueue &msgQueue, wzcpp::ExecutorBase &executor)
Construct a new TcpIO object with a given message queue and executor.
std::condition_variable cvFinished_
Condition variable which is set when the receiver worker has stopped.
Definition TcpIO.h:186
void waitForFinished()
Wait till the worker of this class has finished.
std::unique_ptr< TcpIO > UPtr
Alias for a std::unique_ptr to this class.
Definition TcpIO.h:117
void send(const Buffer &buf)
Send a byte list out.
std::mutex cvFinished_mut_
The correponding mutex for the cvFinished_ condition variable.
Definition TcpIO.h:187
std::unique_ptr< Ip4Driver > driver_
The driver to use for actual sending and receiving.
Definition TcpIO.h:194
wzcpp::ExecutorBase & executor_
Reference to the executor to use for running the receive(RX) worker.
Definition TcpIO.h:193
static std::atomic< std::uint64_t > msgId
Used to generate a continuous unique message id.
Definition TcpIO.h:184
PacketizerBase::UPtr packetizer_
TCP stream packetizer used by this class.
Definition TcpIO.h:195
Global types and configuration for the whole SDK.
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition config.h:71
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition IOEvent.h:96