13 #ifndef __BAOSLIB_CONNECTION_H__
14 #define __BAOSLIB_CONNECTION_H__
22 #include "connection/BaosIndicationSender.h"
24 #include "wzcpp/features/concurrent/executor.h"
25 #include "wzcpp/features/timer/timer.h"
50 InvalidConnectionHeader,
54 UnknownIndicationType,
58 std::string BAOSLIB_EXPORT
69 using Clock = std::chrono::system_clock;
72 using UPtr = std::unique_ptr<BaosConnection>;
160 std::uint16_t numberOfStrings);
187 std::uint16_t startDpId,
188 std::uint16_t numberOfValues,
205 void setDpValues(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
291 std::uint16_t startDpId,
292 std::uint16_t numberOfValues,
454 void emitConnectionEvent(
State oldState,
State newState);
455 void emitConnectionEvent(ConnectionEvent::Type event);
459 ConnectionEvent::Type getConnectionEvent(
State state);
462 std::atomic<bool> shouldStop_{
false};
470 std::atomic<bool> isRunning_{
false};
471 wzcpp::ThreadPoolExecutor executor_{5};
482 std::uint64_t currentMessageId_{0};
489 State state_{State::STOPPED};
490 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.
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.
std::optional< protocol::DatapointDescription > getDpDescription(std::uint16_t dpId)
Get the datapoint description for one datapoint.
void setServerItemsIndCallback(ServerItemIndicationCb callback)
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:476
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)
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: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)
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.
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.
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.
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
@ ALL
No filter, all datapoints.
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::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
std::string BAOSLIB_EXPORT parseStatusToString(BaosParseStatus parseStatus)
Human readable string of the parse status.
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