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
BaosConnection.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_CONNECTION_H__
14#define __BAOSLIB_CONNECTION_H__
15
16#include "baos/config.h"
18#include "baos/ip/IpGeneral.h"
21
22#include "connection/BaosIndicationSender.h"
23
24#include "wzcpp/features/concurrent/executor.h"
25#include "wzcpp/features/timer/timer.h"
26
28#include <future>
29#include <map>
30#include <memory>
31#include <mutex>
32#include <optional>
33#include <string>
35
41namespace wz::baos
42{
43
46{
47 Success,
48 SizeMismatch,
49 InvalidKnxHeader,
50 InvalidConnectionHeader,
51 InvalidMsgData,
52 InvalidMainService,
53 InvalidSubService,
54 UnknownIndicationType,
55 MalformedResponse
56};
57
58std::string BAOSLIB_EXPORT
60
66class BAOSLIB_EXPORT BaosConnection
67{
68public:
69 using Clock = std::chrono::system_clock;
70 using BaosFrame = Buffer;
71 using BaosFrame_UPtr = std::unique_ptr<BaosFrame>;
72 using UPtr = std::unique_ptr<BaosConnection>;
73 using ConnectionEventCb = std::function<void(ConnectionEvent&)>;
75 std::function<void(const protocol::ServerItems&)>;
76 using DatapointValueCb = std::function<void(
78
80 virtual ~BaosConnection();
81
82 void start();
83 void stop();
84
85 virtual void reopen() = 0;
86 void close();
87
93 bool isOpen();
94
95
101
108 std::optional<protocol::ServerItem> getServerItem(protocol::ServerItemId id);
117 protocol::ServerItems getServerItems(std::uint16_t startItem, std::uint16_t numberOfItems);
118
119 void setServerItem(const protocol::ServerItem& serverItem);
130 std::optional<protocol::DatapointDescription> getDpDescription(std::uint16_t dpId);
131
140 protocol::DatapointDescriptions getDpDescriptions(std::uint16_t startDpId, std::uint16_t numberOfItems);
141
149 std::optional<protocol::DatapointDescriptionString> getDpDescriptionString(std::uint16_t dpId);
150
160 std::uint16_t numberOfStrings);
161
162 std::optional<std::uint8_t> getParameterByte(std::uint16_t byteIndex);
163
172 protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex, std::uint16_t numberOfBytes);
173
174 std::optional<protocol::DatapointValueState> getDpValue(
175 std::uint16_t dpId);
176
187 std::uint16_t startDpId,
188 std::uint16_t numberOfValues,
189 protocol::DatapointValueFilter filter = protocol::DatapointValueFilter::ALL);
190
197 void setDpValue(std::uint16_t dpId, protocol::DatapointChangeValue&& dpValue);
198
199
205 void setDpValues(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
207
208 /******************************************************************************************************************/
225 protocol::ResponseFuture getServerItemsAsync(std::uint16_t startItem, std::uint16_t numberOfItems);
226
237
246 protocol::ResponseFuture getDpDescriptionsAsync(std::uint16_t startDpId, std::uint16_t numberOfItems);
247
255
264 protocol::ResponseFuture getDpDescriptionStringsAsync(std::uint16_t startDpId, std::uint16_t numberOfStrings);
265
267
276 protocol::ResponseFuture getParameterBytesAsync(std::uint16_t byteIndex, std::uint16_t numberOfBytes);
277
279 std::uint16_t dpId);
280
291 std::uint16_t startDpId,
292 std::uint16_t numberOfValues,
293 protocol::DatapointValueFilter filter = protocol::DatapointValueFilter::ALL);
294
295
304
311 protocol::ResponseFuture setDpValuesAsync(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
312
319
320
322
328
334
340
341protected:
349
350protected:
354 enum class State
355 {
356 // States for our worker
357 RUNNING,
358 STOPPING,
359 STOPPED,
360
361 // States for baos transport connection
362
363 CONOPENING,
364
365 SENDING,
366 SENDCOMPLETE,
367 WAITRESPONSE,
368 IDLE,
369
370 CONCLOSING,
371 CONERROR,
372 CONCLOSED,
373 CONCLOSED_BY_ERROR,
374 FATALERROR
375 };
376
378 enum class PromiseState
379 {
380 UNUSED,
381 SET,
382 RESOLVED
383 };
384
388 static std::string stateToString(State state);
389
397
405
413
421
428
436
438 virtual void startImpl();
439 virtual void stopImpl();
440
446 virtual bool isOpenImpl();
447
449 BaosFrame&& frame) = 0;
450
451 virtual void onStateChange(State oldState,
452 State newState);
453
454 void emitConnectionEvent(State oldState, State newState);
455 void emitConnectionEvent(ConnectionEvent::Type event);
456
457 virtual void onTimerExpired(std::vector<std::uint64_t> ids);
458
459 ConnectionEvent::Type getConnectionEvent(State state);
460
461protected:
462 std::atomic<bool> shouldStop_{false};
463
470 std::atomic<bool> isRunning_{false};
471 wzcpp::ThreadPoolExecutor executor_{5};
473
474 wzcpp::TimerManager<Clock> timerManager_;
475 wzcpp::Timer<Clock> respTimeoutTimer_;
476 wzcpp::Timer<Clock> heartBeatTimer_;
477
481 PromiseState responsePromiseState_{PromiseState::UNUSED};
482 std::uint64_t currentMessageId_{0};
483 std::queue<IOEvent> txQueue_;
485 BaosIndQueue bIndQ_;
486 BaosIndicationSender indSender_;
487
488private:
489 State state_{State::STOPPED};
490 mutable std::mutex stateMutex_;
491};
492
493} // namespace wz::baos
494
495#endif // __BAOSLIB_CONNECTION_H__
Global BAOS protocol defines and types.
Code regarding Input/Output Events.
Provides the structures for devices with IP capabilities.
Classes regarding response telegrams.
Represents a connection to a BAOS device.
Definition BaosConnection.h:67
protocol::ResponseFuture getDpValuesAsync(std::uint16_t startDpId, std::uint16_t numberOfValues, protocol::DatapointValueFilter filter=protocol::DatapointValueFilter::ALL)
Get a map with the datapoint id as key and the DatapointValueState as value.
protocol::ResponseFuture setServerItemAsync(protocol::ServerItem &&serverItem)
std::function< void(ConnectionEvent &)> ConnectionEventCb
Function signature for connection events callbacks.
Definition BaosConnection.h:73
protocol::ResponseFuture getServerItemAsync(protocol::ServerItemId id)
BaosConnection()
Default constructor.
void setDpValues(std::map< std::uint16_t, protocol::DatapointChangeValue > &&dpValues)
Send a SetDatapointValue request to the BAOS device with given datapoint values.
BaosParseStatus handleBaosFrame(BaosFrame_UPtr &&frame)
For every received BAOS frame this handler is internally called.
protocol::BaosBaseResponse::SPtr decodeResponse(BaosFrame_UPtr &&frame)
Decodes the baos response.
wzcpp::Timer< Clock > respTimeoutTimer_
The actual timer objects for respone timeouts.
Definition BaosConnection.h:475
BaosMsgQueue bMsgQ_
Hold the event for the main connection loop.
Definition BaosConnection.h:484
protocol::BaosResponseCode handleBaosResponse(BaosFrame_UPtr &&frame)
For every received BAOS response this handler is internally called.
protocol::ResponseFuture send(BaosFrame &&frame)
Sends a generic frame( a byte list) to the BAOS device.
void setDpValue(std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
Send a SetDatapointValue request to the BAOS device for one datapoint.
std::function< void(const protocol::DatapointValueStates &)> DatapointValueCb
Function signature for datapoint value indication callbacks.
Definition BaosConnection.h:77
virtual protocol::ResponseFuture closeImplAsync()
The implementation function for closing a connection.
void setServerItemsIndCallback(ServerItemIndicationCb callback)
protocol::ResponseFuture setServerItemsAsync(protocol::ServerItems &&serverItems)
std::optional< protocol::ServerItem > getServerItem(protocol::ServerItemId id)
Get a single the ServerItem.
State setState(State newState)
Set the internal state of this connections state machine.
protocol::DatapointValueStates getDpValues(std::uint16_t startDpId, std::uint16_t numberOfValues, protocol::DatapointValueFilter filter=protocol::DatapointValueFilter::ALL)
Get a map with the datapoint id as key and the DatapointValueState as value.
bool isOpen()
Returns the open state of the connection.
virtual ~BaosConnection()
Default destructor.
protocol::ResponseFuture getServerItemsAsync(std::uint16_t startItem, std::uint16_t numberOfItems)
Get a list of serveritems from the device.
protocol::ResponseFuture getDpValueAsync(std::uint16_t dpId)
Get a single datapoint value with its associated state.
wzcpp::Timer< Clock > heartBeatTimer_
The actual timer objects for heartbeats.
Definition BaosConnection.h:476
std::optional< protocol::DatapointDescription > getDpDescription(std::uint16_t dpId)
Get the datapoint description for one datapoint.
std::chrono::system_clock Clock
An alias for the clock internaly used.
Definition BaosConnection.h:69
protocol::ResponseFuture setDpValuesAsync(std::map< std::uint16_t, protocol::DatapointChangeValue > &&dpValues)
Send a SetDatapointValue request to the BAOS device with given datapoint values.
Buffer waitResponse_
While waiting for a response this holds the pattern of the response we expect.
Definition BaosConnection.h:478
void setServerItem(protocol::ServerItem &&serverItem)
void close()
Close the connection to the BAOS device.
State
Represents the internal state of this connection.
Definition BaosConnection.h:355
void start()
Start the client side of the connection.
std::queue< IOEvent > txQueue_
Holds the packets to send waiting to be processed.
Definition BaosConnection.h:483
virtual void onStateChange(State oldState, State newState)
Invoked if the internal connection state should be changed.
std::unique_ptr< BaosConnection > UPtr
An alias for an unqiue pointer to this class.
Definition BaosConnection.h:72
void setServerItem(const protocol::ServerItem &serverItem)
virtual protocol::ResponseFuture sendImpl(BaosFrame &&frame)=0
The implementation function for sending bytes to the connected device.
void setServerItems(protocol::ServerItems &&serverItems)
Buffer BaosFrame
An alias for the container type to store frame/packets/telegrams in.
Definition BaosConnection.h:70
protocol::ResponseFuture getDpDescriptionsAsync(std::uint16_t startDpId, std::uint16_t numberOfItems)
Get a list of DatapointDescriptions.
void stop()
Stop the client side of the connection.
virtual void onTimerExpired(std::vector< std::uint64_t > ids)
Invoked if on of the timers has expired.
State getState() const
Get the current internal state of this connections state machine.
virtual bool isOpenImpl()
The implementation function for getting the opened state of the connection.
virtual void stopImpl()
The implementation function for stopping the connection class.
void setConnectionEventCallback(ConnectionEventCb callback)
std::optional< protocol::DatapointValueState > getDpValue(std::uint16_t dpId)
Get a single datapoint value with its associated state.
protocol::DatapointDescriptions getDpDescriptions(std::uint16_t startDpId, std::uint16_t numberOfItems)
Get a list of DatapointDescriptions.
virtual void startImpl()
The implementation function for starting the connection class.
protocol::ResponseFuture getParameterByteAsync(std::uint16_t byteIndex)
Get a single parameter byte.
protocol::ResponseFuture getDpDescriptionStringsAsync(std::uint16_t startDpId, std::uint16_t numberOfStrings)
Get a list of DatapointDescriptionsStrings.
protocol::ResponseFuture closeAsync()
Close the connection.
void setDatapointValueIndCallback(DatapointValueCb callback)
BaosParseStatus handleBaosIndication(BaosFrame_UPtr &&frame)
For every received BAOS indication this handler is internally called.
std::unique_ptr< BaosFrame > BaosFrame_UPtr
An alias for a std::unique_ptr holding a BaosFrame.
Definition BaosConnection.h:71
protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex, std::uint16_t numberOfBytes)
Get a list of parameter bytes.
protocol::ResponseFuture setDpValueAsync(std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
Send a SetDatapointValue request to the BAOS device for one datapoint.
std::optional< std::uint8_t > getParameterByte(std::uint16_t byteIndex)
Get a single parameter byte.
protocol::ResponseFuture getDpDescriptionStringAsync(std::uint16_t dpId)
Get the datapoint description string for one datapoint.
BaosIndQueue bIndQ_
Holds the indication to be send out to clients.
Definition BaosConnection.h:485
virtual void reopen()=0
Reopens the underlying connection, has to be implemented by derived classes.
PromiseState
The asynchronous functions return a promise to keep internal track of their state this enum is used.
Definition BaosConnection.h:379
protocol::ResponseFuture getParameterBytesAsync(std::uint16_t byteIndex, std::uint16_t numberOfBytes)
Get a list of parameter bytes.
std::optional< protocol::DatapointDescriptionString > getDpDescriptionString(std::uint16_t dpId)
Get the datapoint description string for one datapoint.
wzcpp::TimerManager< Clock > timerManager_
Manages the timers for this connection.
Definition BaosConnection.h:474
static std::string stateToString(State state)
Get human readable string from internal state.
protocol::ResponsePromise responsePromise_
If a packet is send out via handleTxTcp the corresponding promise is stored here.
Definition BaosConnection.h:480
protocol::ServerItems getServerItems(std::uint16_t startItem, std::uint16_t numberOfItems)
Get a list of serveritems from the device.
protocol::ResponseFuture getDpDescriptionAsync(std::uint16_t dpId)
Get the datapoint description for one datapoint.
protocol::DatapointDescriptionStrings getDpDescriptionStrings(std::uint16_t startDpId, std::uint16_t numberOfStrings)
Get a list of DatapointDescriptionsStrings.
BaosIndicationSender indSender_
Responsible for sending out the indications.
Definition BaosConnection.h:486
std::function< void(const protocol::ServerItems &)> ServerItemIndicationCb
Function signature for serveritem indication callbacks.
Definition BaosConnection.h:75
Global types and configuration for the whole SDK.
BaosResponseCode
These are the BaosServerErrorCodes.
Definition Defines.h:129
std::promise< BaseResponse::SPtr > ResponsePromise
Definition Response.h:95
ServerItemId
An enum class to provide conveniant names to the serveritem ids.
Definition Defines.h:199
std::future< BaseResponse::SPtr > ResponseFuture
Definition Response.h:97
std::map< std::uint16_t, DatapointValueState > DatapointValueStates
Definition Defines.h:607
std::vector< std::uint8_t > ParameterBytes
Strong type for a list of parameter bytes.
Definition Defines.h:635
DatapointValueFilter
Requested datapoint value can be filtered by this criteria.
Definition Defines.h:51
std::map< std::uint16_t, DatapointDescription > DatapointDescriptions
Definition Defines.h:629
std::map< std::uint16_t, DatapointDescriptionString > DatapointDescriptionStrings
Definition Defines.h:633
std::vector< ServerItem > ServerItems
Type for a list of ServerItems.
Definition Defines.h:440
Global BAOS sdk namespace.
Definition config.h:69
std::string BAOSLIB_EXPORT parseStatusToString(BaosParseStatus parseStatus)
Human readable string of the parse status.
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition config.h:71
BaosParseStatus
The different result type of the BAOS binary parsing operation.
Definition BaosConnection.h:46
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition IOEvent.h:96
Definition ConnectionEvent.h:37
std::shared_ptr< BaosBaseResponse > SPtr
An alias for a std::shared pointer holding an BaosBaseResponse class.
Definition Response.h:46
A pair of the DatapointSetCommand and the Datapoint value for changing datapoint values.
Definition Defines.h:611
Structure to hold information about a serveritem.
Definition Defines.h:404