23#ifndef ZWAVE_FRAME_PARSER_HPP
24#define ZWAVE_FRAME_PARSER_HPP
29#include "attribute_store.h"
30#include "attribute.hpp"
42using zwave_report_bitmask_t = uint32_t;
113class zwave_frame_parser
116 using zwave_parser_bitmask_result = std::map<uint8_t, uint8_t>;
121 struct bitmask_data {
125 uint8_t bitmask = 0b00000000;
130 attribute_store_node_t destination_node = ATTRIBUTE_STORE_INVALID_NODE;
139 explicit zwave_frame_parser(
const uint8_t *data, uint16_t length);
140 ~zwave_frame_parser() =
default;
154 bool is_frame_size_valid(uint8_t min_size, uint8_t max_size = 0)
const;
179 uint8_t read_byte(attribute_store_node_t node);
221 zwave_parser_bitmask_result
222 read_byte_with_bitmask(
const std::vector<bitmask_data> &bitmask_data);
236 zwave_parser_bitmask_result
237 read_byte_with_bitmask(
const bitmask_data &bitmask_data);
260 zwave_report_bitmask_t read_bitmask(uint8_t bitmask_length = 0);
285 zwave_report_bitmask_t read_bitmask(attribute_store_node_t node,
286 uint8_t bitmask_length = 0);
306 std::string read_string();
327 std::string read_string(attribute_store_node_t node);
344 template<
typename T> T read_sequential(uint8_t bytes_to_read)
346 static_assert(std::is_integral<T>::value
347 || std::is_same<T, std::string>::value
348 || std::is_same<T, std::vector<uint8_t>>::value,
352 if constexpr (std::is_integral<T>::value) {
353 value_from_frame = 0;
355 for (uint8_t i = 0; i < bytes_to_read; i++) {
356 T current_byte = zwave_report_frame.at(current_index);
361 uint8_t offset = (bytes_to_read - 1 - i) * 8;
362 value_from_frame |= current_byte << offset;
370 if constexpr (std::is_signed<T>::value) {
371 switch (bytes_to_read) {
373 value_from_frame =
static_cast<int8_t
>(value_from_frame);
376 value_from_frame =
static_cast<int16_t
>(value_from_frame);
379 value_from_frame =
static_cast<int32_t
>(value_from_frame);
382 throw std::runtime_error(
"Unsupported size for signed value, only "
383 "1, 2 or 4 are supported");
386 }
else if constexpr (std::is_same<T, std::string>::value
387 || std::is_same<T, std::vector<uint8_t>>::value) {
388 value_from_frame.resize(bytes_to_read);
389 for (uint8_t i = 0; i < bytes_to_read; i++) {
390 value_from_frame[i] = read_byte();
393 return value_from_frame;
413 T read_sequential(uint8_t bytes_to_read, attribute_store_node_t node)
415 T value = read_sequential<T>(bytes_to_read);
416 helper_store_value(node, value);
432 void helper_store_value(attribute_store_node_t node, T value)
434 attribute_store::attribute current_attribute(node);
436 if (current_attribute.is_valid()) {
437 current_attribute.set_reported<T>(value);
438 sl_log_debug(
"zwave_frame_parser",
439 "Attribute updated : %s",
440 current_attribute.value_to_string().c_str());
443 "zwave_frame_parser",
444 "Node does not exist in the attribute store. Not updating value");
462 uint8_t get_trailing_zeroes(
const std::bitset<8> &bitset)
const;
465 std::vector<uint8_t> zwave_report_frame;
468 uint8_t current_index = 2;