BAOS SDK v2  1.2.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
BaosDevice.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_INTERFACE_H__
14 #define __BAOSLIB_INTERFACE_H__
15 
16 #include "baos/config.h"
17 
20 #include "baos/protocol/Defines.h"
21 
22 #include "BlockReader.h"
23 
24 #include "wzcpp/features/algorithm/append.h"
25 #include "wzcpp/features/utils/ByteArray.h"
26 #include "wzcpp/features/utils/Memory.h"
27 
29 #include <memory>
30 #include <optional>
31 #include <utility>
33 
39 namespace wz::baos
40 {
41 // Forward declaration
42 class BaosConnection;
43 using BaosConnection_UPtr = std::unique_ptr<BaosConnection>;
44 
45 
46 /**********************************************
47  * Helper functions
48  **********************************************/
49 
53 BAOSLIB_EXPORT std::uint16_t getAvailableBufferSize(const DeviceStaticInfo& staticInfo, const DeviceConfigInfo& configInfo);
54 
58 BAOSLIB_EXPORT std::uint16_t getAvailablePayloadSize(const DeviceStaticInfo& staticInfo, const DeviceConfigInfo& configInfo);
59 
60 /**************************************************
61  * BaosDevice
62  * ***********************************************/
63 
71 class BAOSLIB_EXPORT BaosDevice
72 {
73 public:
74  using ConnectionEventCb = std::function<void(ConnectionEvent&)>;
75  using ServerItemIndicationCb = std::function<void(
76  const protocol::ServerItems&)>;
77  using DatapointValueCb = std::function<void(
78  const protocol::DatapointValues&)>;
79 
80  BaosDevice() = default;
81  virtual ~BaosDevice() = default;
82 
83  virtual void close();
85  /************************ Information about the device ************************/
91 
98  DeviceStaticInfo getStaticInfo(bool reload = false);
99 
100 
108  DeviceConfigInfo getConfigInfo(bool reload = false);
109 
117  DeviceRuntimeInfo getRuntimeInfo(bool reload = false);
118 
126 
128 
129  /************************ Parameter bytes ***************************************/
135 
144  protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex = 1,
145  std::uint16_t numberOfBytes = UINT16_MAX,
146  bool reload = false);
147 
159 
160  /**************************** Datapoints *****************************************/
166  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~ Datapoint configurations ~~~~~~~~~~~~~~~~~~~~~~~~~~*/
167 
176  std::optional<DatapointConfig> getDpConfiguration(std::uint16_t dpId = 1,
177  bool withDescStrings = true,
178  bool reload = false);
179 
189  DatapointConfigurations getDpConfigurations(std::uint16_t startDpId = 1,
190  std::uint16_t endDpId = UINT16_MAX,
191  bool withDescStrings = true,
192  bool reload = false);
193 
194 
204  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~ Datapoint values ~~~~~~~~~~~~~~~~~~~~~~~~~~*/
205 
213  void setDpValue(std::uint16_t dpId,
214  Buffer dpData,
216 
223  void setDpValues(std::map<std::uint16_t, Buffer>&& values,
225 
231  void readDpValueFromBus(std::uint16_t dpId);
232 
238  void readDpValuesFromBus(std::vector<std::uint16_t>&& dpIds);
239 
245  void readDpValuesFromBus(const std::vector<std::uint16_t>& dpIds);
246 
247 
255  std::optional<protocol::DatapointValue> getDpValueRaw(std::uint16_t dpId, bool reload = false);
256 
264  template<class T>
265  std::optional<T> getDatapointValue(std::uint16_t dpId, bool reload = false)
266  {
267  auto value = getDpValueRaw(dpId);
268  if (value.has_value())
269  {
270  return T(value);
271  }
272  else
273  {
274  return {};
275  }
276  }
277 
285  protocol::DatapointValues getDpValuesRaw(std::vector<std::uint16_t> dpIds, bool reload = false);
286 
297  std::uint16_t startDpId,
298  std::uint16_t endDpId,
299  bool reload = false,
301 
313 
315  /*************************************** Configuration of this class *****************************/
316 
324 
333  bool enableCaching(bool state);
334 
340  bool isCachingEnabled() const
341  {
342  return config_.cacheEnabled;
343  };
344 
348  void clearCaches();
349 
355  bool wasCacheHit()
356  {
357  return cacheHit_;
358  }
359 
365  std::optional<DeviceMeta> getMetaInfo();
366 
376  void setMetaInfo(DeviceMeta&& meta);
377 
378 
379  /*************************************** Callbacks on events *************************************/
380 
385 
390 
395 
396 
397  /************************************ Underlying BaosConnection *********************************/
398 
405 
407 
413  void setInternalConnection(BaosConnection_UPtr& conn)
414  {
415  baosCon_ = std::move(conn);
416  }
417 
424  BaosConnection& getInternalConnection()
425  {
426  if (!baosCon_)
427  {
428  throw WzSocketException(WZ_SL, "No internal connection available");
429  }
430  return *baosCon_;
431  }
433 
434 
435 protected:
441 
447  void onServerItemsUpdate(const protocol::ServerItems& serverItems);
448 
455 
460 
466 
483 
495  template<class TGetter>
496  auto getElements(std::uint16_t start, std::uint16_t count, std::uint16_t maxLengthElement, TGetter cb)
497  {
498  if (config_.blockReadEnabled)
499  {
500  // Get current buffer size
501  if (!deviceInfoLoaded_)
502  {
503  getConfigInfo();
504  }
505  assert(configInfo_.currentBufferSize > 16);
506  // maxLengthElement: bytes per payload element
507  std::uint16_t payloadAvailSize = getAvailablePayloadSize(getStaticInfo(), getConfigInfo()) /
508  maxLengthElement;
509  assert(payloadAvailSize > 0);
510 
511  std::uint16_t blockSize = payloadAvailSize;
512  return utility::BlockReader::read(start, count, std::min(payloadAvailSize, blockSize), cb);
513  }
514  else
515  {
516  return cb(start, count);
517  }
518  }
519 
520 
521 protected:
524  std::optional<DeviceMeta> metaInfo_;
525 
526  bool cacheHit_{false};
527 
528  // cached variables
529  bool deviceInfoLoaded_{false};
533 
534  std::map<std::uint16_t, Buffer> dpCache_;
535  bool dpCacheFilled_{false};
537 
539 
540  std::vector<ServerItemIndicationCb> serverItemsCallbacks;
541  std::vector<DatapointValueCb> dpValueCallbacks;
542  std::vector<ConnectionEventCb> connectionEventCallbacks;
543 
545 
546 };
547 
548 } // namespace wz::baos
549 
550 #endif // __BAOSLIB_INTERFACE_H__
Specific datatypes used by the device classes.
Provides the structures for additional meta information regarding the various devices.
Global BAOS protocol defines and types.
Represents a connection to a BAOS device.
Definition: BaosConnection.h:67
This class represents a BAOS device.
Definition: BaosDevice.h:72
std::vector< ConnectionEventCb > connectionEventCallbacks
List of registered connection lifecycle callbacks.
Definition: BaosDevice.h:542
std::function< void(ConnectionEvent &)> ConnectionEventCb
Function signature for connection events callbacks.
Definition: BaosDevice.h:74
DeviceStaticInfo getStaticInfo(bool reload=false)
Get the information about a connected BAOS device that is never changed. Like serial number.
void registerConnectionEventCallback(ConnectionEventCb callback)
Register one or more functions as callbacks, invoked on connection lifecycle events.
void onConnectionEvent(ConnectionEvent event)
Forwarded callback from underlying connection for lifecycle events.
auto getElements(std::uint16_t start, std::uint16_t count, std::uint16_t maxLengthElement, TGetter cb)
Helper function to retrieve the elements(datapoint descriptions, string and parameter bytes) from the...
Definition: BaosDevice.h:496
protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex=1, std::uint16_t numberOfBytes=UINT16_MAX, bool reload=false)
Get the parameter bytes If caching is enabled, the result of this call will be stored in cache.
DeviceConfigInfo getConfigInfo(bool reload=false)
Get the information about a connected BAOS device that is typically only changed during configuration...
BaosConnection_UPtr baosCon_
The internal connection used to coomunicate with the BAOS device.
Definition: BaosDevice.h:544
DeviceRuntimeInfo getRuntimeInfo(bool reload=false)
Get the information about a connected BAOS device that may change during runtime.
protocol::DatapointValues getDpValuesRaw(std::vector< std::uint16_t > dpIds, bool reload=false)
Get the values of multiple datapoints as raw bytes.
DeviceRuntimeInfo runtimeInfo_
Runtime information about the BAOS device.
Definition: BaosDevice.h:532
virtual void close()
DeviceConfigInfo configInfo_
Config information about the BAOS device.
Definition: BaosDevice.h:531
bool enableCaching(bool state)
Enable or Disable the internal caching for datapoint and parameter bytes.
std::vector< DatapointValueCb > dpValueCallbacks
List of registered callback functions.
Definition: BaosDevice.h:541
void onServerItemsUpdate(const protocol::ServerItems &serverItems)
An update may occur as a response to a getServerItem request or via an indication.
void setDpValues(std::map< std::uint16_t, Buffer > &&values, protocol::DatapointSetCommand command=protocol::DatapointSetCommand::SetNewValueAndSendOnBus)
Set the value of multiple datapoints.
std::vector< ServerItemIndicationCb > serverItemsCallbacks
List of registered callback functions.
Definition: BaosDevice.h:540
std::map< std::uint16_t, Buffer > dpCache_
Cache to hold the last valid values for the datapoints.
Definition: BaosDevice.h:534
void readDpValueFromBus(std::uint16_t dpId)
Triggers a read request for the datapoint on the bus.
std::optional< DeviceMeta > metaInfo_
Meta information set by the user.
Definition: BaosDevice.h:524
DatapointConfigurations dpConfig_
Configuration information about the BAOS datapoints.
Definition: BaosDevice.h:538
void setBoolServerItem(protocol::ServerItemId id, bool value)
Set the a bool value to a server item.
std::function< void(const protocol::DatapointValues &)> DatapointValueCb
The function signature for the callback on changed datapoint values.
Definition: BaosDevice.h:78
protocol::DatapointValues getDpValuesRaw(std::uint16_t startDpId, std::uint16_t endDpId, bool reload=false, protocol::DatapointValueFilter filter=protocol::DatapointValueFilter::VALID)
Get the values of multiple datapoints as raw bytes.
std::optional< T > getDatapointValue(std::uint16_t dpId, bool reload=false)
Get the value of a single datapoint as type T.
Definition: BaosDevice.h:265
std::function< void(const protocol::ServerItems &)> ServerItemIndicationCb
The function signature for the callback on changed server items.
Definition: BaosDevice.h:76
void readDpValuesFromBus(std::vector< std::uint16_t > &&dpIds)
Triggers read requests for a set of datapoints on the bus.
void clearDeviceConfigs()
Reset the device information, retrieved from serveritems and meta data to their default values.
protocol::DatapointValues getAllDpValuesRaw(bool reload=false, protocol::DatapointValueFilter filter=protocol::DatapointValueFilter::VALID)
Load all the datapoint values and cache them.
void onDpValueUpdate(const protocol::DatapointValueStates &dpValueStates)
An update may occur as a response to a getDatapointValue request or via an indication.
protocol::ParameterBytes paramBytes_
Cache to hold the parameter bytes.
Definition: BaosDevice.h:536
void clearCaches()
Clear the internal caches for datapoints and parameter bytes.
DatapointConfigurations getDpConfigurations(std::uint16_t startDpId=1, std::uint16_t endDpId=UINT16_MAX, bool withDescStrings=true, bool reload=false)
Get the datapoint configurations If caching is enabled, the result of this call will be stored in cac...
protocol::ParameterBytes getAllParameterBytes(bool reload=false)
Load all the parameter bytes and cache them.
std::optional< protocol::DatapointValue > getDpValueRaw(std::uint16_t dpId, bool reload=false)
Get the value of a single datapoint as raw bytes.
DatapointConfigurations getAllDpConfigurations(bool reload=false)
Load all the datapoint configurations and cache them.
std::optional< DeviceMeta > getMetaInfo()
Get the metadata set for this device if available.
void setMetaInfo(DeviceMeta &&meta)
Set the meta data for this device.
virtual BaosConnection_UPtr getConnection()=0
Pure virtual function to get a BaosConnection to the device.
bool wasCacheHit()
Returns if the last request could be statisfied by the cache or not.
Definition: BaosDevice.h:355
void ensureConnection()
Make sure that a connection to the underlying device exists and is open or throw an exception if this...
void readDpValuesFromBus(const std::vector< std::uint16_t > &dpIds)
Triggers read requests for a set of datapoints on the bus.
bool isCachingEnabled() const
Return the current caching state.
Definition: BaosDevice.h:340
protocol::ServerItems getAllServerItems()
Get all the serveritem values from the device.
ServerItemReadStrategy setServerItemReadStrategy(ServerItemReadStrategy strategy)
Set the strategy to use for reading the serveritems.
void registerServerItemCallback(ServerItemIndicationCb)
Register one or more functions ascallbacks, invoked on serveritem changes.
DeviceStaticInfo staticInfo_
Static information about the BAOS device.
Definition: BaosDevice.h:530
void setDpValue(std::uint16_t dpId, Buffer dpData, protocol::DatapointSetCommand command=protocol::DatapointSetCommand::SetNewValueAndSendOnBus)
Set the value of a single datapoint.
void registerDPValueCallback(DatapointValueCb)
Register one or more functions ascallbacks, invoked on datapoint value changes.
std::optional< DatapointConfig > getDpConfiguration(std::uint16_t dpId=1, bool withDescStrings=true, bool reload=false)
Get the datapoint configuration of a single datapoint If caching is enabled, the result of this call ...
Global types and configuration for the whole SDK.
std::map< std::uint16_t, DatapointValue > DatapointValues
A map with datapoint ids as keys and DatapointValue as values.
Definition: Defines.h:36
ServerItemId
An enum class to provide conveniant names to the serveritem ids.
Definition: Defines.h:199
std::map< std::uint16_t, DatapointValueState > DatapointValueStates
Definition: Defines.h:607
DatapointSetCommand
Setting a datapoint value there are different modes supported. This enum class gives those modes name...
Definition: Defines.h:61
@ SetNewValueAndSendOnBus
The default: Set the new value in the object server and send it to the bus.
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::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
std::map< std::uint16_t, DatapointConfig > DatapointConfigurations
Definition: BaosDeviceDataTypes.h:212
std::unique_ptr< BaosConnection > BaosConnection_UPtr
Alias for a std::unique pointer of a BaosConnection.
Definition: BaosDevice.h:43
ServerItemReadStrategy
Strategy type on how to handle reading the serveritems.
Definition: BaosDeviceDataTypes.h:216
BAOSLIB_EXPORT std::uint16_t getAvailableBufferSize(const DeviceStaticInfo &staticInfo, const DeviceConfigInfo &configInfo)
BAOSLIB_EXPORT std::uint16_t getAvailablePayloadSize(const DeviceStaticInfo &staticInfo, const DeviceConfigInfo &configInfo)
Definition: ConnectionEvent.h:37
Configuration options for the cpp device class behaviour.
Definition: BaosDeviceDataTypes.h:226
A collection of fields representing those serveritems which may change if the configuration of the de...
Definition: BaosDeviceDataTypes.h:94
Meta information about a device group, which is required be the device class.
Definition: BaosDevicesMeta.h:99
A collection of fields representing those serveritems which may change during "normal" operation of t...
Definition: BaosDeviceDataTypes.h:174
A collection of fields representing those serveritems which won't ever change in a device.
Definition: BaosDeviceDataTypes.h:49