From bf573d82531b0d6605f420ab7bba7c2228bbdeab Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Sat, 21 Sep 2024 15:25:56 +0200 Subject: [PATCH] Reimplement Dpt15 --- src/CMakeLists.txt | 2 + src/knx/group_object/dpt/dpt.h | 1 - src/knx/group_object/dpt/dpt15.cpp | 110 ++++++++++++++++++++++++ src/knx/group_object/dpt/dpt15.h | 37 ++++++++ src/knx/group_object/dpt/dptconvert.cpp | 87 ------------------- src/knx/group_object/dpt/dptconvert.h | 2 - src/knx/group_object/dpt/dpts.h | 3 +- 7 files changed, 151 insertions(+), 91 deletions(-) create mode 100644 src/knx/group_object/dpt/dpt15.cpp create mode 100644 src/knx/group_object/dpt/dpt15.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e380494..b90e4a7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -66,6 +66,8 @@ set(SOURCES ./knx/group_object/dpt/dpt13.h ./knx/group_object/dpt/dpt14.cpp ./knx/group_object/dpt/dpt14.h +./knx/group_object/dpt/dpt15.cpp +./knx/group_object/dpt/dpt15.h ./knx/group_object/dpt/dptconvert.cpp ./knx/group_object/dpt/dptconvert.h ./knx/group_object/group_object.cpp diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index 90b9bba..b804dc5 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,7 +3,6 @@ #include "../group_object.h" namespace Knx { -#define DPT_Access_Data Dpt(15, 0) #define DPT_String_ASCII Dpt(16, 0) #define DPT_String_8859_1 Dpt(16, 1) #define DPT_SceneNumber Dpt(17, 1) diff --git a/src/knx/group_object/dpt/dpt15.cpp b/src/knx/group_object/dpt/dpt15.cpp new file mode 100644 index 0000000..2e02f40 --- /dev/null +++ b/src/knx/group_object/dpt/dpt15.cpp @@ -0,0 +1,110 @@ +#include "dpt15.h" + +#include "dptconvert.h" + +Knx::Go_SizeCode Knx::Dpt15::size() const +{ + return Go_4_Octets; +} + +void Knx::Dpt15::encode(uint8_t* data) const +{ + + for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10) + bcdToPayload(data, n, (_accessCode / factor) % 10); + + bitToPayload(data, 24, _detectionError); + bitToPayload(data, 25, _permission); + bitToPayload(data, 26, _readDirection == RightToLeft); + bitToPayload(data, 27, _encrypted); + bcdToPayload(data, 7, _index); +} + +bool Knx::Dpt15::decode(uint8_t* data) +{ + int32_t digits = 0; + + for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10) + { + unsigned char digit = bcdFromPayload(data, n); + + if (digit > 9) + return false; + + digits += digit * factor; + } + + _accessCode = digits; + _detectionError = bitFromPayload(data, 24); + _permission = bitFromPayload(data, 25); + _readDirection = bitFromPayload(data, 26) ? RightToLeft : LeftToRight; + _encrypted = bitFromPayload(data, 27); + _index = bcdFromPayload(data, 7); + return true; +} + +uint32_t Knx::Dpt15::accessCode() const +{ + return _accessCode; +} + +void Knx::Dpt15::accessCode(const uint32_t value) +{ + if (value > 999999) + return; + + _accessCode = value; +} + +bool Knx::Dpt15::detectionError() const +{ + return _detectionError; +} + +void Knx::Dpt15::detetionError(const bool value) +{ + _detectionError = value; +} + +bool Knx::Dpt15::permission() const +{ + return _permission; +} + +void Knx::Dpt15::permission(const bool value) +{ + _permission = value; +} + +Knx::Dpt15::ReadDirectionValue Knx::Dpt15::readDirection() const +{ + return _readDirection; +} + +void Knx::Dpt15::readDirection(const ReadDirectionValue value) +{ + _readDirection = value; +} + +bool Knx::Dpt15::encrypted() const +{ + return _encrypted; +} + +void Knx::Dpt15::encrypted(const bool value) +{ + _encrypted = value; +} + +uint8_t Knx::Dpt15::index() const +{ + return _index; +} + +void Knx::Dpt15::index(const uint8_t value) +{ + if (value > 15) + return; + + _index = value; +} diff --git a/src/knx/group_object/dpt/dpt15.h b/src/knx/group_object/dpt/dpt15.h new file mode 100644 index 0000000..2145c40 --- /dev/null +++ b/src/knx/group_object/dpt/dpt15.h @@ -0,0 +1,37 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt15: public Dpt + { + enum ReadDirectionValue { LeftToRight = 0, RightToLeft = 1}; + public: + Dpt15() {}; + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + + uint32_t accessCode() const; + void accessCode(const uint32_t value); + bool detectionError() const; + void detetionError(const bool value); + bool permission() const; + void permission(const bool value); + ReadDirectionValue readDirection() const; + void readDirection(const ReadDirectionValue value); + bool encrypted() const; + void encrypted(const bool value); + uint8_t index() const; + void index(const uint8_t value); + private: + uint32_t _accessCode; + bool _detectionError; + bool _permission; + ReadDirectionValue _readDirection; + bool _encrypted; + uint8_t _index; + }; + + typedef Dpt15 DPT_Access_Data; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dptconvert.cpp b/src/knx/group_object/dpt/dptconvert.cpp index d66e37e..cacf6a2 100644 --- a/src/knx/group_object/dpt/dptconvert.cpp +++ b/src/knx/group_object/dpt/dptconvert.cpp @@ -18,10 +18,6 @@ namespace Knx { if (payload_length > 0) { - // DPT 15.* - Access Data - if (datatype.mainGroup == 15 && !datatype.subGroup && datatype.index <= 5) - return busValueToAccess(payload, payload_length, datatype, value); - // DPT 16.* - String if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index) return busValueToString(payload, payload_length, datatype, value); @@ -108,10 +104,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { - // DPT 15.* - Access Data - if (datatype.mainGroup == 15 && !datatype.subGroup && datatype.index <= 5) - return valueToBusValueAccess(value, payload, payload_length, datatype); - // DPT 16.* - String if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index) return valueToBusValueString(value, payload, payload_length, datatype); @@ -202,45 +194,6 @@ namespace Knx return true; } - int busValueToAccess(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(4); - - switch (datatype.index) - { - case 0: - { - int32_t digits = 0; - - for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10) - { - unsigned char digit = bcdFromPayload(payload, n); - - if (digit > 9) - return false; - - digits += digit * factor; - } - - value = digits; - return true; - } - - case 1: - case 2: - case 3: - case 4: - value = bitFromPayload(payload, 23 + datatype.index); - return true; - - case 5: - value = bcdFromPayload(payload, 7); - return true; - } - - return false; - } - int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) { ASSERT_PAYLOAD(14); @@ -645,46 +598,6 @@ namespace Knx return true; } - int valueToBusValueAccess(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - switch (datatype.index) - { - case 0: - { - if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(999999)) - return false; - - ENSURE_PAYLOAD(4); - - for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10) - bcdToPayload(payload, n, ((uint64_t)value / factor) % 10); - - break; - } - - case 1: - case 2: - case 3: - case 4: - bitToPayload(payload, 23 + datatype.index, value); - break; - - case 5: - { - if ((uint64_t)value > INT64_C(15)) - return false; - - bcdToPayload(payload, 7, (uint64_t)value); - break; - } - - default: - return false; - } - - return true; - } - int valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { const char* strValue = value; diff --git a/src/knx/group_object/dpt/dptconvert.h b/src/knx/group_object/dpt/dptconvert.h index 7cde3eb..3d7b31b 100644 --- a/src/knx/group_object/dpt/dptconvert.h +++ b/src/knx/group_object/dpt/dptconvert.h @@ -49,7 +49,6 @@ namespace Knx //KNX to internal int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToAccess(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToScene(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSceneControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); @@ -72,7 +71,6 @@ namespace Knx //Internal to KNX int valueToBusValueUnsigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueAccess(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueScene(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSceneControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); diff --git a/src/knx/group_object/dpt/dpts.h b/src/knx/group_object/dpt/dpts.h index 3b6312c..3f783d9 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -13,4 +13,5 @@ #include "dpt11.h" #include "dpt12.h" #include "dpt13.h" -#include "dpt14.h" \ No newline at end of file +#include "dpt14.h" +#include "dpt15.h" \ No newline at end of file