13 #ifndef __BAOSLIB_CONNECTION_H__
14 #define __BAOSLIB_CONNECTION_H__
22 #include "connection/BaosIndicationSender.h"
23 #include "ip/TcpDriver.h"
25 #include "wzcpp/features/concurrent/executor.h"
26 #include "wzcpp/features/logging.h"
27 #include "wzcpp/features/timer/timer.h"
52 InvalidConnectionHeader,
56 UnknownIndicationType,
60 std::string BAOSLIB_EXPORT
71 using Clock = std::chrono::system_clock;
74 using UPtr = std::unique_ptr<BaosConnection>;
161 std::uint16_t numberOfStrings);
188 std::uint16_t startDpId,
189 std::uint16_t numberOfValues,
206 void setDpValues(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
292 std::uint16_t startDpId,
293 std::uint16_t numberOfValues,
330 indSender_.setServerItemsIndCallback(callback);
339 indSender_.setDatapointValueIndCallback(callback);
351 return sendImpl(std::move(frame));
423 std::scoped_lock<std::mutex> lock{stateMutex_};
456 std::atomic<bool> shouldStop_{
false};
464 std::atomic<bool> isRunning_{
false};
465 wzcpp::ThreadPoolExecutor executor_{4};
476 std::uint64_t currentMessageId_{0};
483 WZLOGGER(
"BaosConnection", LVL_DEBUG);
484 State state_{State::STOPPED};
485 mutable std::mutex stateMutex_;
Global BAOS protocol defines and types.
Code regarding Input/Output Events.
Provides the structures for devices with IP capabilities.
Classes regarding response telegrams.
Specialization for a TCP IP v4 BAOSConnection.
Represents a connection to a BAOS device.
Definition: BaosConnection.h:69
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)
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:469
BaosMsgQueue bMsgQ_
Hold the event for the main connection loop.
Definition: BaosConnection.h:478
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.
Definition: BaosConnection.h:349
void setDpValue(std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
Send a SetDatapointValue request to the BAOS device for one datapoint.
protocol::BaosIndQueue bIndQ_
Holds the indication to be send out to clients.
Definition: BaosConnection.h:479
std::function< void(const protocol::DatapointValueStates &)> DatapointValueCb
Function signature for datapoint value indication callbacks.
Definition: BaosConnection.h:78
virtual protocol::ResponseFuture closeImplAsync()
The implementation function for closing a connection.
std::optional< protocol::DatapointDescription > getDpDescription(std::uint16_t dpId)
Get the datapoint description for one datapoint.
void setServerItemsIndCallback(ServerItemIndicationCb callback)
Definition: BaosConnection.h:328
protocol::ResponseFuture setServerItemsAsync(protocol::ServerItems &&serverItems)
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.
std::optional< protocol::ServerItem > getServerItem(protocol::ServerItemId id)
Get a single the ServerItem.
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.
std::optional< std::uint8_t > getParameterByte(std::uint16_t byteIndex)
Get a single parameter byte.
wzcpp::Timer< Clock > heartBeatTimer_
The actual timer objects for heartbeats.
Definition: BaosConnection.h:470
std::chrono::system_clock Clock
An alias for the clock internaly used.
Definition: BaosConnection.h:71
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:472
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:359
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:477
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:74
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)
std::optional< protocol::DatapointValueState > getDpValue(std::uint16_t dpId)
Get a single datapoint value with its associated state.
Buffer BaosFrame
An alias for the container type to store frame/packets/telegrams in.
Definition: BaosConnection.h:72
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.
Definition: BaosConnection.h:421
virtual bool isOpenImpl()
The implementation function for getting the opened state of the connection.
Definition: BaosConnection.h:444
virtual void stopImpl()
The implementation function for stopping the connection class.
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)
Definition: BaosConnection.h:337
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:73
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.
protocol::ResponseFuture getDpDescriptionStringAsync(std::uint16_t dpId)
Get the datapoint description string for one datapoint.
std::optional< protocol::DatapointDescriptionString > getDpDescriptionString(std::uint16_t dpId)
Get the datapoint description string for one datapoint.
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:373
protocol::ResponseFuture getParameterBytesAsync(std::uint16_t byteIndex, std::uint16_t numberOfBytes)
Get a list of parameter bytes.
wzcpp::TimerManager< Clock > timerManager_
Manages the timers for this connection.
Definition: BaosConnection.h:468
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:474
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:480
std::function< void(const protocol::ServerItems &)> ServerItemIndicationCb
Function signature for serveritem indication callbacks.
Definition: BaosConnection.h:76
Global types and configuration for the whole SDK.
BaosResponseCode
These are the BaosServerErrorCodes.
Definition: Defines.h:128
std::promise< BaseResponse::SPtr > ResponsePromise
Definition: Response.h:95
ServerItemId
An enum class to provide conveniant names to the serveritem ids.
Definition: Defines.h:198
std::future< BaseResponse::SPtr > ResponseFuture
Definition: Response.h:97
std::map< std::uint16_t, DatapointValueState > DatapointValueStates
Definition: Defines.h:588
std::vector< std::uint8_t > ParameterBytes
Strong type for a list of parameter bytes.
Definition: Defines.h:616
DatapointValueFilter
Requested datapoint value can be filtered by this criteria.
Definition: Defines.h:50
@ ALL
No filter, all datapoints.
std::map< std::uint16_t, DatapointDescription > DatapointDescriptions
Definition: Defines.h:610
wzcpp::ConcurrentDeque< IndicationEvent > BaosIndQueue
Alias for a wzcpp::ConcurrentDeque holding indication events.
Definition: Indication.h:104
std::map< std::uint16_t, DatapointDescriptionString > DatapointDescriptionStrings
Definition: Defines.h:614
std::vector< ServerItem > ServerItems
Type for a list of ServerItems.
Definition: Defines.h:421
Global BAOS sdk namespace.
Definition: config.h:62
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition: config.h:64
BaosParseStatus
The different result type of the BAOS binary parsing operation.
Definition: BaosConnection.h:48
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition: IOEvent.h:117
std::string BAOSLIB_EXPORT parseStatusToString(BaosParseStatus &parseStatus)
Human readable string of the parse status.
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:592
Structure to hold information about a serveritem.
Definition: Defines.h:403