BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
BaosConnection.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2023 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"
19 #include "baos/ip/TcpIO.h"
20 #include "baos/protocol/Defines.h"
21 #include "baos/protocol/Response.h"
22 #include "connection/BaosIndicationSender.h"
23 #include "ip/TcpDriver.h"
24 
25 #include "wzcpp/features/concurrent/executor.h"
26 #include "wzcpp/features/logging.h"
27 #include "wzcpp/features/timer/timer.h"
28 
30 #include <future>
31 #include <map>
32 #include <memory>
33 #include <mutex>
34 #include <optional>
35 #include <string>
37 
43 namespace wz::baos
44 {
45 
47 enum class BaosParseStatus
48 {
49  Success,
50  SizeMismatch,
51  InvalidKnxHeader,
52  InvalidConnectionHeader,
53  InvalidMsgData,
54  InvalidMainService,
55  InvalidSubService,
56  UnknownIndicationType,
57  MalformedResponse
58 };
59 
60 std::string BAOSLIB_EXPORT
62 
68 class BAOSLIB_EXPORT BaosConnection
69 {
70 public:
71  using Clock = std::chrono::system_clock;
72  using BaosFrame = Buffer;
73  using BaosFrame_UPtr = std::unique_ptr<BaosFrame>;
74  using UPtr = std::unique_ptr<BaosConnection>;
76  std::function<void(const protocol::ServerItems&)>;
77  using DatapointValueCb = std::function<void(
79 
81  virtual ~BaosConnection();
82 
83  void start();
84  void stop();
85 
86  virtual void reopen() = 0;
87  void close();
88 
94  bool isOpen();
95 
96 
102 
109  std::optional<protocol::ServerItem> getServerItem(protocol::ServerItemId id);
118  protocol::ServerItems getServerItems(std::uint16_t startItem, std::uint16_t numberOfItems);
119 
120  void setServerItem(const protocol::ServerItem& serverItem);
121  void setServerItem(protocol::ServerItem&& serverItem);
122  void setServerItems(protocol::ServerItems&& serverItems);
131  std::optional<protocol::DatapointDescription> getDpDescription(std::uint16_t dpId);
132 
141  protocol::DatapointDescriptions getDpDescriptions(std::uint16_t startDpId, std::uint16_t numberOfItems);
142 
150  std::optional<protocol::DatapointDescriptionString> getDpDescriptionString(std::uint16_t dpId);
151 
161  std::uint16_t numberOfStrings);
162 
163  std::optional<std::uint8_t> getParameterByte(std::uint16_t byteIndex);
164 
173  protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex, std::uint16_t numberOfBytes);
174 
175  std::optional<protocol::DatapointValueState> getDpValue(
176  std::uint16_t dpId);
177 
188  std::uint16_t startDpId,
189  std::uint16_t numberOfValues,
191 
198  void setDpValue(std::uint16_t dpId, protocol::DatapointChangeValue&& dpValue);
199 
200 
206  void setDpValues(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
208 
209  /******************************************************************************************************************/
226  protocol::ResponseFuture getServerItemsAsync(std::uint16_t startItem, std::uint16_t numberOfItems);
227 
238 
247  protocol::ResponseFuture getDpDescriptionsAsync(std::uint16_t startDpId, std::uint16_t numberOfItems);
248 
256 
265  protocol::ResponseFuture getDpDescriptionStringsAsync(std::uint16_t startDpId, std::uint16_t numberOfStrings);
266 
268 
277  protocol::ResponseFuture getParameterBytesAsync(std::uint16_t byteIndex, std::uint16_t numberOfBytes);
278 
280  std::uint16_t dpId);
281 
292  std::uint16_t startDpId,
293  std::uint16_t numberOfValues,
295 
296 
305 
312  protocol::ResponseFuture setDpValuesAsync(std::map<std::uint16_t, protocol::DatapointChangeValue>&& dpValues);
313 
320 
321 
323 
329  {
330  indSender_.setServerItemsIndCallback(callback);
331  }
332 
338  {
339  indSender_.setDatapointValueIndCallback(callback);
340  }
341 
342 protected:
350  {
351  return sendImpl(std::move(frame));
352  };
353 
354 protected:
358  enum class State
359  {
360  RUNNING,
361  STOPPING,
362  STOPPED,
363  SENDING,
364  SENDCOMPLETE,
365  WAITRESPONSE,
366  IDLE,
367  CONERROR,
368  CONCLOSED
369  };
370 
372  enum class PromiseState
373  {
374  UNUSED,
375  SET,
376  RESOLVED
377  };
378 
382  static std::string stateToString(State state);
383 
391 
399 
407 
414  State setState(State newState);
415 
421  State getState() const
422  {
423  std::scoped_lock<std::mutex> lock{stateMutex_};
424  return state_;
425  };
426 
434 
436  virtual void startImpl();
437  virtual void stopImpl();
438 
444  virtual bool isOpenImpl()
445  {
446  return false;
447  };
449  BaosFrame&& frame) = 0;
450 
451  virtual void onStateChange(State oldState,
452  State newState);
453  virtual void onTimerExpired(std::vector<std::uint64_t> ids);
454 
455 protected:
456  std::atomic<bool> shouldStop_{false};
457 
464  std::atomic<bool> isRunning_{false};
465  wzcpp::ThreadPoolExecutor executor_{4};
467 
468  wzcpp::TimerManager<Clock> timerManager_;
469  wzcpp::Timer<Clock> respTimeoutTimer_;
470  wzcpp::Timer<Clock> heartBeatTimer_;
471 
475  PromiseState responsePromiseState_{PromiseState::UNUSED};
476  std::uint64_t currentMessageId_{0};
477  std::queue<IOEvent> txQueue_;
480  BaosIndicationSender indSender_;
481 
482 private:
483  WZLOGGER("BaosConnection", LVL_DEBUG);
484  State state_{State::STOPPED};
485  mutable std::mutex stateMutex_;
486 };
487 
488 } // namespace wz::baos
489 
490 #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.
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