BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
BaosConnection Class Referenceabstract

Represents a connection to a BAOS device. More...

#include <baos/connection/BaosConnection.h>

+ Inheritance diagram for BaosConnection:

Public Types

using Clock = std::chrono::system_clock
 An alias for the clock internaly used.
 
using BaosFrame = Buffer
 An alias for the container type to store frame/packets/telegrams in.
 
using BaosFrame_UPtr = std::unique_ptr< BaosFrame >
 An alias for a std::unique_ptr holding a BaosFrame.
 
using UPtr = std::unique_ptr< BaosConnection >
 An alias for an unqiue pointer to this class.
 
using ServerItemIndicationCb = std::function< void(const protocol::ServerItems &)>
 Function signature for serveritem indication callbacks.
 
using DatapointValueCb = std::function< void(const protocol::DatapointValueStates &)>
 Function signature for datapoint value indication callbacks.
 

Public Member Functions

 BaosConnection ()
 Default constructor.
 
virtual ~BaosConnection ()
 Default destructor.
 
void start ()
 Start the client side of the connection.
 
void stop ()
 Stop the client side of the connection.
 
virtual void reopen ()=0
 Reopens the underlying connection, has to be implemented by derived classes.
 
void close ()
 Close the connection to the BAOS device.
 
bool isOpen ()
 Returns the open state of the connection. More...
 
void setServerItemsIndCallback (ServerItemIndicationCb callback)
 
void setDatapointValueIndCallback (DatapointValueCb callback)
 
Synchronous API

Set of functions to provide the typical BAOS interaction in a synchronous way

std::optional< protocol::ServerItemgetServerItem (protocol::ServerItemId id)
 Get a single the ServerItem. More...
 
protocol::ServerItems getServerItems (std::uint16_t startItem, std::uint16_t numberOfItems)
 Get a list of serveritems from the device. More...
 
void setServerItem (const protocol::ServerItem &serverItem)
 
void setServerItem (protocol::ServerItem &&serverItem)
 
void setServerItems (protocol::ServerItems &&serverItems)
 
std::optional< protocol::DatapointDescriptiongetDpDescription (std::uint16_t dpId)
 Get the datapoint description for one datapoint. More...
 
protocol::DatapointDescriptions getDpDescriptions (std::uint16_t startDpId, std::uint16_t numberOfItems)
 Get a list of DatapointDescriptions. More...
 
std::optional< protocol::DatapointDescriptionStringgetDpDescriptionString (std::uint16_t dpId)
 Get the datapoint description string for one datapoint. More...
 
protocol::DatapointDescriptionStrings getDpDescriptionStrings (std::uint16_t startDpId, std::uint16_t numberOfStrings)
 Get a list of DatapointDescriptionsStrings. More...
 
std::optional< std::uint8_t > getParameterByte (std::uint16_t byteIndex)
 Get a single parameter byte.
 
protocol::ParameterBytes getParameterBytes (std::uint16_t byteIndex, std::uint16_t numberOfBytes)
 Get a list of parameter bytes. More...
 
std::optional< protocol::DatapointValueStategetDpValue (std::uint16_t dpId)
 Get a single datapoint value with its associated state.
 
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. More...
 
void setDpValue (std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
 Send a SetDatapointValue request to the BAOS device for one datapoint. More...
 
void setDpValues (std::map< std::uint16_t, protocol::DatapointChangeValue > &&dpValues)
 Send a SetDatapointValue request to the BAOS device with given datapoint values. More...
 
Asynchronous API

Set of functions to provide the typical BAOS interaction in a asynchronous way. Every function returns a future.

protocol::ResponseFuture getServerItemAsync (protocol::ServerItemId id)
 
protocol::ResponseFuture getServerItemsAsync (std::uint16_t startItem, std::uint16_t numberOfItems)
 Get a list of serveritems from the device. More...
 
protocol::ResponseFuture setServerItemAsync (protocol::ServerItem &&serverItem)
 
protocol::ResponseFuture setServerItemsAsync (protocol::ServerItems &&serverItems)
 
protocol::ResponseFuture getDpDescriptionAsync (std::uint16_t dpId)
 Get the datapoint description for one datapoint. More...
 
protocol::ResponseFuture getDpDescriptionsAsync (std::uint16_t startDpId, std::uint16_t numberOfItems)
 Get a list of DatapointDescriptions. More...
 
protocol::ResponseFuture getDpDescriptionStringAsync (std::uint16_t dpId)
 Get the datapoint description string for one datapoint. More...
 
protocol::ResponseFuture getDpDescriptionStringsAsync (std::uint16_t startDpId, std::uint16_t numberOfStrings)
 Get a list of DatapointDescriptionsStrings. More...
 
protocol::ResponseFuture getParameterByteAsync (std::uint16_t byteIndex)
 Get a single parameter byte.
 
protocol::ResponseFuture getParameterBytesAsync (std::uint16_t byteIndex, std::uint16_t numberOfBytes)
 Get a list of parameter bytes. More...
 
protocol::ResponseFuture getDpValueAsync (std::uint16_t dpId)
 Get a single datapoint value with its associated state.
 
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. More...
 
protocol::ResponseFuture setDpValueAsync (std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
 Send a SetDatapointValue request to the BAOS device for one datapoint. More...
 
protocol::ResponseFuture setDpValuesAsync (std::map< std::uint16_t, protocol::DatapointChangeValue > &&dpValues)
 Send a SetDatapointValue request to the BAOS device with given datapoint values. More...
 
protocol::ResponseFuture closeAsync ()
 Close the connection. More...
 

Protected Types

enum class  State {
  RUNNING ,
  STOPPING ,
  STOPPED ,
  SENDING ,
  SENDCOMPLETE ,
  WAITRESPONSE ,
  IDLE ,
  CONERROR ,
  CONCLOSED
}
 Represents the internal state of this connection. More...
 
enum class  PromiseState {
  UNUSED ,
  SET ,
  RESOLVED
}
 The asynchronous functions return a promise to keep internal track of their state this enum is used.
 

Protected Member Functions

protocol::ResponseFuture send (BaosFrame &&frame)
 Sends a generic frame( a byte list) to the BAOS device. More...
 
BaosParseStatus handleBaosFrame (BaosFrame_UPtr &&frame)
 For every received BAOS frame this handler is internally called. More...
 
BaosParseStatus handleBaosIndication (BaosFrame_UPtr &&frame)
 For every received BAOS indication this handler is internally called. More...
 
protocol::BaosResponseCode handleBaosResponse (BaosFrame_UPtr &&frame)
 For every received BAOS response this handler is internally called. More...
 
State setState (State newState)
 Set the internal state of this connections state machine. More...
 
State getState () const
 Get the current internal state of this connections state machine. More...
 
protocol::BaosBaseResponse::SPtr decodeResponse (BaosFrame_UPtr &&frame)
 Decodes the baos response. More...
 
virtual protocol::ResponseFuture closeImplAsync ()
 The implementation function for closing a connection.
 
virtual void startImpl ()
 The implementation function for starting the connection class.
 
virtual void stopImpl ()
 The implementation function for stopping the connection class.
 
virtual bool isOpenImpl ()
 The implementation function for getting the opened state of the connection. More...
 
virtual protocol::ResponseFuture sendImpl (BaosFrame &&frame)=0
 The implementation function for sending bytes to the connected device.
 
virtual void onStateChange (State oldState, State newState)
 Invoked if the internal connection state should be changed.
 
virtual void onTimerExpired (std::vector< std::uint64_t > ids)
 Invoked if on of the timers has expired.
 

Static Protected Member Functions

static std::string stateToString (State state)
 Get human readable string from internal state.
 

Protected Attributes

std::atomic< bool > shouldStop_ {false}
 Flag to indicate that the connection should stop at the next opportunity.
 
std::atomic< bool > isRunning_ {false}
 Keep track if the connection is in running state or stopping / stopped. More...
 
wzcpp::ThreadPoolExecutor executor_ {4}
 
wzcpp::TimerManager< ClocktimerManager_
 Manages the timers for this connection.
 
wzcpp::Timer< ClockrespTimeoutTimer_
 The actual timer objects for respone timeouts.
 
wzcpp::Timer< ClockheartBeatTimer_
 The actual timer objects for heartbeats.
 
Buffer waitResponse_
 While waiting for a response this holds the pattern of the response we expect.
 
protocol::ResponsePromise responsePromise_
 If a packet is send out via handleTxTcp the corresponding promise is stored here.
 
PromiseState responsePromiseState_ {PromiseState::UNUSED}
 The state of the responsePromise_.
 
std::uint64_t currentMessageId_ {0}
 For internal use a unique message id.
 
std::queue< IOEventtxQueue_
 Holds the packets to send waiting to be processed.
 
BaosMsgQueue bMsgQ_
 Hold the event for the main connection loop.
 
protocol::BaosIndQueue bIndQ_
 Holds the indication to be send out to clients.
 
BaosIndicationSender indSender_
 Responsible for sending out the indications.
 

Detailed Description

Represents a connection to a BAOS device.

The connection runs in a separate thread and receives events from either the user or from underlying socket.

Member Enumeration Documentation

◆ State

enum State
strongprotected

Represents the internal state of this connection.

Enumerator
RUNNING 

The worker is started but no connection to the device is established.

STOPPING 

A stop was requested but it is not done with stopping.

STOPPED 

It is now actualy stopped.

SENDING 

It is in the process of sending out bytes.

SENDCOMPLETE 

A complete packet was send.

WAITRESPONSE 

Waiting for a response.

IDLE 

Nothing to do but connection to device is established and worker is started.

CONERROR 

An error occured during communication with the remote device.

CONCLOSED 

The underlying connection is actually closed.

Member Function Documentation

◆ isOpen()

bool isOpen ( )

Returns the open state of the connection.

Returns
true if the connection is open, false otherwise

◆ getServerItem()

std::optional<protocol::ServerItem> getServerItem ( protocol::ServerItemId  id)

Get a single the ServerItem.

Parameters
idThe ServerItem id
Returns
The protocol::ServerItem value or an empty std::optional if no value this ServerItemId could be retrieved

◆ getServerItems()

protocol::ServerItems getServerItems ( std::uint16_t  startItem,
std::uint16_t  numberOfItems 
)

Get a list of serveritems from the device.

Parameters
startItemThe id of the first serveritem to load
numberOfItemsMaximal number of items to return
Returns
protocol::ServerItems a list of server items in the interval [startItem … startItem+numberOfItems-1]

◆ setServerItem() [1/2]

void setServerItem ( const protocol::ServerItem serverItem)

Set a single serveritem

◆ setServerItem() [2/2]

void setServerItem ( protocol::ServerItem &&  serverItem)

Set a single serveritem

◆ setServerItems()

void setServerItems ( protocol::ServerItems &&  serverItems)

Set a list of serveritems

◆ getDpDescription()

std::optional<protocol::DatapointDescription> getDpDescription ( std::uint16_t  dpId)

Get the datapoint description for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
Returns
The protocol::DatapointDescription value or an empty std::optional if no DatapointDescription for this dpId could be retrieved

◆ getDpDescriptions()

protocol::DatapointDescriptions getDpDescriptions ( std::uint16_t  startDpId,
std::uint16_t  numberOfItems 
)

Get a list of DatapointDescriptions.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfItemsMaximal number of descriptions to return
Returns
A map with datapoint id as key and DatapointDescription as value in the interval [startDpId … startDpId + numberOfItems - 1]

◆ getDpDescriptionString()

std::optional<protocol::DatapointDescriptionString> getDpDescriptionString ( std::uint16_t  dpId)

Get the datapoint description string for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
Returns
The protocol::DatapointDescriptionString value or an empty std::optional if no DatapointDescriptionString for this dpId could be retrieved

◆ getDpDescriptionStrings()

protocol::DatapointDescriptionStrings getDpDescriptionStrings ( std::uint16_t  startDpId,
std::uint16_t  numberOfStrings 
)

Get a list of DatapointDescriptionsStrings.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfStringsMaximal number of descriptions to return
Returns
A map with datapoint id as key and DatapointDescriptionsString as value in the interval [startDpId … startDpId + numberOfItems - 1]

◆ getParameterBytes()

protocol::ParameterBytes getParameterBytes ( std::uint16_t  byteIndex,
std::uint16_t  numberOfBytes 
)

Get a list of parameter bytes.

Parameters
byteIndexThe 1 based index of the first parameter byte to read
numberOfBytesMaximum number of bytes to return
Returns
protocol::ParameterBytes a list of parameter bytes in the interval [byteIndex … byteIndex+numberOfBytes-1]

◆ getDpValues()

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.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfValuesMaximal number of value to return
filter[optional] Filter for the requested datapoints
Returns
A map with datapoint id as key and DatapointValueState as value in the interval [startDpId...startDpId+numberOfValues-1]

◆ setDpValue()

void setDpValue ( std::uint16_t  dpId,
protocol::DatapointChangeValue &&  dpValue 
)

Send a SetDatapointValue request to the BAOS device for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
dpValueThe new value to set and the command for it

◆ setDpValues()

void setDpValues ( std::map< std::uint16_t, protocol::DatapointChangeValue > &&  dpValues)

Send a SetDatapointValue request to the BAOS device with given datapoint values.

Parameters
dpValuesa map with datapoint ids as keys and command and new value as value

◆ getServerItemAsync()

Get a single serveritem specified by its id from the device

◆ getServerItemsAsync()

protocol::ResponseFuture getServerItemsAsync ( std::uint16_t  startItem,
std::uint16_t  numberOfItems 
)

Get a list of serveritems from the device.

Parameters
startItemThe id of the first serveritem to load
numberOfItemsMaximal number of items to return
Returns
A ReponseFuture with the payload containing a list of server items in the interval [startItem … startItem+numberOfItems-1]

◆ setServerItemAsync()

protocol::ResponseFuture setServerItemAsync ( protocol::ServerItem &&  serverItem)

Set a single serveritem

◆ setServerItemsAsync()

protocol::ResponseFuture setServerItemsAsync ( protocol::ServerItems &&  serverItems)

Set a list of serveritems

◆ getDpDescriptionAsync()

protocol::ResponseFuture getDpDescriptionAsync ( std::uint16_t  dpId)

Get the datapoint description for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
Returns
A ReponseFuture with the payload containing the protocol::DatapointDescription

◆ getDpDescriptionsAsync()

protocol::ResponseFuture getDpDescriptionsAsync ( std::uint16_t  startDpId,
std::uint16_t  numberOfItems 
)

Get a list of DatapointDescriptions.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfItemsMaximal number of descriptions to return
Returns
A ReponseFuture with the payload containing a map with datapoint id as key and DatapointDescription as value in the interval [startDpId … startDpId + numberOfItems - 1]

◆ getDpDescriptionStringAsync()

protocol::ResponseFuture getDpDescriptionStringAsync ( std::uint16_t  dpId)

Get the datapoint description string for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
Returns
A ReponseFuture with the payload containing the protocol::DatapointDescriptionString

◆ getDpDescriptionStringsAsync()

protocol::ResponseFuture getDpDescriptionStringsAsync ( std::uint16_t  startDpId,
std::uint16_t  numberOfStrings 
)

Get a list of DatapointDescriptionsStrings.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfStringsMaximal number of descriptions to return
Returns
A ReponseFuture with the payload containing a map with datapoint id as key and DatapointDescriptionsString as value in the interval [startDpId … startDpId + numberOfItems - 1]

◆ getParameterBytesAsync()

protocol::ResponseFuture getParameterBytesAsync ( std::uint16_t  byteIndex,
std::uint16_t  numberOfBytes 
)

Get a list of parameter bytes.

Parameters
byteIndexThe 1 based index of the first parameter byte to read
numberOfBytesMaximum number of bytes to return
Returns
A ReponseFuture with the payload containing a list of parameter bytes in the interval [byteIndex … byteIndex+numberOfBytes-1]

◆ getDpValuesAsync()

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.

Parameters
startDpIdThe 1 based id of the first datapoint
numberOfValuesMaximal number of value to return
filter[optional] Filter for the requested datapoints
Returns
A ReponseFuture with the payload containing a map with datapoint id as key and DatapointValueState as value in the interval [startDpId...startDpId+numberOfValues-1]

◆ setDpValueAsync()

protocol::ResponseFuture setDpValueAsync ( std::uint16_t  dpId,
protocol::DatapointChangeValue &&  dpValue 
)

Send a SetDatapointValue request to the BAOS device for one datapoint.

Parameters
dpIdThe 1 based id of the datapoint
dpValueThe new value to set and the command for it
Returns
A ResponseFuture with no payload

◆ setDpValuesAsync()

protocol::ResponseFuture setDpValuesAsync ( std::map< std::uint16_t, protocol::DatapointChangeValue > &&  dpValues)

Send a SetDatapointValue request to the BAOS device with given datapoint values.

Parameters
dpValuesa map with datapoint ids as keys and command and new value as value
Returns
A ResponseFuture with no payload

◆ closeAsync()

protocol::ResponseFuture closeAsync ( )

Close the connection.

Returns
protocol::ResponseFuture which completes if the connection is acutally closed

◆ setServerItemsIndCallback()

void setServerItemsIndCallback ( ServerItemIndicationCb  callback)
inline

Sets the function callback to be invoked in every serveritem indication from the device

Parameters
callbackstrong typed std::function object

◆ setDatapointValueIndCallback()

void setDatapointValueIndCallback ( DatapointValueCb  callback)
inline

Sets the function callback to be invoked in every datapoint value indication from the device

Parameters
callbackstrong typed std::function object

◆ send()

protocol::ResponseFuture send ( BaosFrame &&  frame)
inlineprotected

Sends a generic frame( a byte list) to the BAOS device.

Parameters
framea generic list of bytes
Returns
protocol::ResponseFuture completes if the request got its matching response from the device

◆ handleBaosFrame()

BaosParseStatus handleBaosFrame ( BaosFrame_UPtr &&  frame)
protected

For every received BAOS frame this handler is internally called.

Parameters
framean rvalue of the frame to handle
Returns
BaosParseStatus

◆ handleBaosIndication()

BaosParseStatus handleBaosIndication ( BaosFrame_UPtr &&  frame)
protected

For every received BAOS indication this handler is internally called.

Parameters
framean rvalue of the frame to handle
Returns
BaosParseStatus

◆ handleBaosResponse()

protocol::BaosResponseCode handleBaosResponse ( BaosFrame_UPtr &&  frame)
protected

For every received BAOS response this handler is internally called.

Parameters
framean rvalue of the frame to handle
Returns
BaosParseStatus

◆ setState()

State setState ( State  newState)
protected

Set the internal state of this connections state machine.

Parameters
newStateThe new state to set
Returns
State The old state

◆ getState()

State getState ( ) const
inlineprotected

Get the current internal state of this connections state machine.

Returns
State The current state

◆ decodeResponse()

protocol::BaosBaseResponse::SPtr decodeResponse ( BaosFrame_UPtr &&  frame)
protected

Decodes the baos response.

Parameters
framean rvalue of the frame to decode
Returns
protocol::BaosBaseResponse::SPtr a shared pointer with the ResponseObject.

◆ isOpenImpl()

virtual bool isOpenImpl ( )
inlineprotectedvirtual

The implementation function for getting the opened state of the connection.

Returns
Always false in the base class. Derived classes should overwrite this

Reimplemented in BaosTcpConnection.

Member Data Documentation

◆ isRunning_

std::atomic<bool> isRunning_ {false}
protected

Keep track if the connection is in running state or stopping / stopped.

It is also used be start and stop function to protect against starting a running or stopping a stopped connection.

◆ executor_

wzcpp::ThreadPoolExecutor executor_ {4}
protected

The main executor thread pool with 4 threads : The read thread and the indication thread, the connection thread and the timer thread


The documentation for this class was generated from the following file: