From 6cd570e162d04069ce00bfa35e6cb1d608b4f24b Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Wed, 18 Dec 2019 21:27:54 +0100 Subject: [PATCH] refactore TableObject classes to use Property class --- src/knx/address_table_object.cpp | 39 ++----- src/knx/address_table_object.h | 3 - src/knx/application_program_object.cpp | 101 +++--------------- src/knx/application_program_object.h | 13 --- src/knx/association_table_object.cpp | 41 ++------ src/knx/association_table_object.h | 3 - src/knx/group_object_table_object.cpp | 40 ++----- src/knx/group_object_table_object.h | 3 - src/knx/interface_object.h | 2 +- src/knx/table_object.cpp | 138 +++++++++++-------------- src/knx/table_object.h | 7 +- 11 files changed, 100 insertions(+), 290 deletions(-) diff --git a/src/knx/address_table_object.cpp b/src/knx/address_table_object.cpp index 4e2cb3d..5377439 100644 --- a/src/knx/address_table_object.cpp +++ b/src/knx/address_table_object.cpp @@ -2,25 +2,19 @@ #include "address_table_object.h" #include "bits.h" +#include "data_property.h" using namespace std; AddressTableObject::AddressTableObject(Memory& memory) : TableObject(memory) { - -} - -void AddressTableObject::readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) -{ - switch (id) + Property* properties[] = { - case PID_OBJECT_TYPE: - pushWord(OT_ADDR_TABLE, data); - break; - default: - TableObject::readProperty(id, start, count, data); - } + new DataProperty(PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0, (uint16_t)OT_ADDR_TABLE) + }; + + TableObject::initializeProperties(sizeof(properties), properties); } uint16_t AddressTableObject::entryCount() @@ -78,24 +72,3 @@ void AddressTableObject::beforeStateChange(LoadState& newState) _groupAddresses = (uint16_t*)data(); } - -static PropertyDescription _propertyDescriptions[] = -{ - { PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0 }, - { PID_LOAD_STATE_CONTROL, true, PDT_CONTROL, 1, ReadLv3 | WriteLv3 }, - { PID_TABLE_REFERENCE, false, PDT_UNSIGNED_LONG, 1, ReadLv3 | WriteLv0 }, - { PID_ERROR_CODE, false, PDT_ENUM8, 1, ReadLv3 | WriteLv0 }, -}; - -static uint8_t _propertyDescriptionCount = sizeof(_propertyDescriptions) / sizeof(PropertyDescription); - -uint8_t AddressTableObject::propertyDescriptionCount() -{ - return _propertyDescriptionCount; -} - - -PropertyDescription* AddressTableObject::propertyDescriptions() -{ - return _propertyDescriptions; -} \ No newline at end of file diff --git a/src/knx/address_table_object.h b/src/knx/address_table_object.h index b610036..29a3f22 100644 --- a/src/knx/address_table_object.h +++ b/src/knx/address_table_object.h @@ -18,7 +18,6 @@ class AddressTableObject : public TableObject * @param memory This parameter is only passed to the custructor of TableObject an not used by this class. */ AddressTableObject(Memory& memory); - void readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) override; uint8_t* restore(uint8_t* buffer) override; /** @@ -52,8 +51,6 @@ class AddressTableObject : public TableObject protected: virtual void beforeStateChange(LoadState& newState) override; - uint8_t propertyDescriptionCount() override; - PropertyDescription* propertyDescriptions() override; private: uint16_t* _groupAddresses = 0; diff --git a/src/knx/application_program_object.cpp b/src/knx/application_program_object.cpp index 3b463a8..4ee6e2f 100644 --- a/src/knx/application_program_object.cpp +++ b/src/knx/application_program_object.cpp @@ -1,57 +1,27 @@ #include "application_program_object.h" #include "bits.h" +#include "data_property.h" +#include "callback_property.h" +#include ApplicationProgramObject::ApplicationProgramObject(Memory& memory) : TableObject(memory) { - -} - -void ApplicationProgramObject::readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) -{ - switch (id) + Property* properties[] = { - case PID_OBJECT_TYPE: - pushWord(OT_APPLICATION_PROG, data); - break; - case PID_PROG_VERSION: - pushByteArray(_programVersion, 5, data); - break; - case PID_PEI_TYPE: - pushByte(0x0, data); - break; - default: - TableObject::readProperty(id, start, count, data); - } -} + new DataProperty(PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0, (uint16_t)OT_APPLICATION_PROG), + new DataProperty(PID_PROG_VERSION, true, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv3), + new CallbackProperty(this, PID_PEI_TYPE, false, PDT_UNSIGNED_CHAR, 1, ReadLv3 | WriteLv0, + [](ApplicationProgramObject* io, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t { + if (start == 0) + return 1; -void ApplicationProgramObject::writeProperty(PropertyID id, uint16_t start, uint8_t* data, uint8_t& count) -{ - switch (id) - { - case PID_PROG_VERSION: - for (uint32_t i = 0; i < 5; i++) - { - _programVersion[i] = data[i]; - } - break; - default: - TableObject::writeProperty(id, start, data, count); - } -} + data[0] = 0; + return 1; + }) + }; -uint8_t ApplicationProgramObject::propertySize(PropertyID id) -{ - switch (id) - { - case PID_PEI_TYPE: - return 1; - case PID_OBJECT_TYPE: - return 2; - case PID_PROG_VERSION: - return 5; - } - return TableObject::propertySize(id); + TableObject::initializeProperties(sizeof(properties), properties); } uint8_t * ApplicationProgramObject::data(uint32_t addr) @@ -72,45 +42,4 @@ uint16_t ApplicationProgramObject::getWord(uint32_t addr) uint32_t ApplicationProgramObject::getInt(uint32_t addr) { return ::getInt(TableObject::data() + addr); -} - -uint8_t* ApplicationProgramObject::save(uint8_t* buffer) -{ - buffer = pushByteArray(_programVersion, 5, buffer); - - return TableObject::save(buffer); -} - -uint8_t* ApplicationProgramObject::restore(uint8_t* buffer) -{ - buffer = popByteArray(_programVersion, 5, buffer); - - return TableObject::restore(buffer); -} - -static PropertyDescription _propertyDescriptions[] = -{ - { PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0 }, - { PID_LOAD_STATE_CONTROL, true, PDT_CONTROL, 1, ReadLv3 | WriteLv3 }, - { PID_TABLE_REFERENCE, false, PDT_UNSIGNED_LONG, 1, ReadLv3 | WriteLv0 }, - { PID_ERROR_CODE, false, PDT_ENUM8, 1, ReadLv3 | WriteLv0 }, - { PID_PEI_TYPE, false, PDT_UNSIGNED_CHAR, 1, ReadLv3 | WriteLv0 }, - { PID_PROG_VERSION, true, PDT_GENERIC_05, 1, ReadLv3 | WriteLv3 }, -}; -static uint8_t _propertyDescriptionCount = sizeof(_propertyDescriptions) / sizeof(PropertyDescription); - -uint8_t ApplicationProgramObject::propertyDescriptionCount() -{ - return _propertyDescriptionCount; -} - - -PropertyDescription* ApplicationProgramObject::propertyDescriptions() -{ - return _propertyDescriptions; -} - -uint16_t ApplicationProgramObject::saveSize() -{ - return TableObject::saveSize() + 5; } \ No newline at end of file diff --git a/src/knx/application_program_object.h b/src/knx/application_program_object.h index 463f55e..071166a 100644 --- a/src/knx/application_program_object.h +++ b/src/knx/application_program_object.h @@ -6,21 +6,8 @@ class ApplicationProgramObject : public TableObject { public: ApplicationProgramObject(Memory& memory); - void readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) override; - void writeProperty(PropertyID id, uint16_t start, uint8_t* data, uint8_t& count) override; - uint8_t propertySize(PropertyID id) override; uint8_t* data(uint32_t addr); uint8_t getByte(uint32_t addr); uint16_t getWord(uint32_t addr); uint32_t getInt(uint32_t addr); - uint8_t* save(uint8_t* buffer) override; - uint8_t* restore(uint8_t* buffer) override; - uint16_t saveSize() override; - - protected: - uint8_t propertyDescriptionCount() override; - PropertyDescription* propertyDescriptions() override; - - private: - uint8_t _programVersion[5] = {0, 0, 0, 0, 0}; }; \ No newline at end of file diff --git a/src/knx/association_table_object.cpp b/src/knx/association_table_object.cpp index 84cd67d..5fecb0b 100644 --- a/src/knx/association_table_object.cpp +++ b/src/knx/association_table_object.cpp @@ -2,25 +2,19 @@ #include "association_table_object.h" #include "bits.h" +#include "data_property.h" using namespace std; AssociationTableObject::AssociationTableObject(Memory& memory) : TableObject(memory) { - -} - -void AssociationTableObject::readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) -{ - switch (id) + Property* properties[] = { - case PID_OBJECT_TYPE: - pushWord(OT_ASSOC_TABLE, data); - break; - default: - TableObject::readProperty(id, start, count, data); - } + new DataProperty(PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0, (uint16_t)OT_ASSOC_TABLE) + }; + + TableObject::initializeProperties(sizeof(properties), properties); } uint16_t AssociationTableObject::entryCount() @@ -63,8 +57,6 @@ int32_t AssociationTableObject::translateAsap(uint16_t asap) return -1; } - - void AssociationTableObject::beforeStateChange(LoadState& newState) { if (newState != LS_LOADED) @@ -73,27 +65,6 @@ void AssociationTableObject::beforeStateChange(LoadState& newState) _tableData = (uint16_t*)data(); } -static PropertyDescription _propertyDescriptions[] = -{ - { PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0 }, - { PID_TABLE, false, PDT_GENERIC_04, 65535, ReadLv3 | WriteLv0 }, - { PID_LOAD_STATE_CONTROL, true, PDT_CONTROL, 1, ReadLv3 | WriteLv3 }, - { PID_TABLE_REFERENCE, false, PDT_UNSIGNED_LONG, 1, ReadLv3 | WriteLv0 }, - { PID_ERROR_CODE, false, PDT_ENUM8, 1, ReadLv3 | WriteLv0 }, -}; -static uint8_t _propertyDescriptionCount = sizeof(_propertyDescriptions) / sizeof(PropertyDescription); - -uint8_t AssociationTableObject::propertyDescriptionCount() -{ - return _propertyDescriptionCount; -} - - -PropertyDescription* AssociationTableObject::propertyDescriptions() -{ - return _propertyDescriptions; -} - int32_t AssociationTableObject::nextAsap(uint16_t tsap, uint16_t& startIdx) { uint16_t entries = entryCount(); diff --git a/src/knx/association_table_object.h b/src/knx/association_table_object.h index 43a6e75..f3869c7 100644 --- a/src/knx/association_table_object.h +++ b/src/knx/association_table_object.h @@ -6,7 +6,6 @@ class AssociationTableObject : public TableObject { public: AssociationTableObject(Memory& memory); - void readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) override; uint8_t* restore(uint8_t* buffer) override; @@ -15,8 +14,6 @@ class AssociationTableObject : public TableObject protected: void beforeStateChange(LoadState& newState) override; - uint8_t propertyDescriptionCount() override; - PropertyDescription* propertyDescriptions() override; private: uint16_t entryCount(); diff --git a/src/knx/group_object_table_object.cpp b/src/knx/group_object_table_object.cpp index e0822f7..c777c6d 100644 --- a/src/knx/group_object_table_object.cpp +++ b/src/knx/group_object_table_object.cpp @@ -3,10 +3,16 @@ #include "group_object_table_object.h" #include "group_object.h" #include "bits.h" +#include "data_property.h" GroupObjectTableObject::GroupObjectTableObject(Memory& memory) : TableObject(memory) { + Property* properties[] + { + new DataProperty(PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0, (uint16_t)OT_GRP_OBJ_TABLE) + }; + TableObject::initializeProperties(sizeof(properties), properties); } GroupObjectTableObject::~GroupObjectTableObject() @@ -14,18 +20,6 @@ GroupObjectTableObject::~GroupObjectTableObject() freeGroupObjects(); } -void GroupObjectTableObject::readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) -{ - switch (id) - { - case PID_OBJECT_TYPE: - pushWord(OT_GRP_OBJ_TABLE, data); - break; - default: - TableObject::readProperty(id, start, count, data); - } -} - uint16_t GroupObjectTableObject::entryCount() { if (loadState() != LS_LOADED) @@ -34,8 +28,6 @@ uint16_t GroupObjectTableObject::entryCount() return ntohs(_tableData[0]); } - - GroupObject& GroupObjectTableObject::get(uint16_t asap) { return _groupObjects[asap - 1]; @@ -126,26 +118,6 @@ bool GroupObjectTableObject::initGroupObjects() return true; } -static PropertyDescription _propertyDescriptions[] = -{ - { PID_OBJECT_TYPE, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0 }, - { PID_LOAD_STATE_CONTROL, true, PDT_CONTROL, 1, ReadLv3 | WriteLv3 }, - { PID_TABLE_REFERENCE, false, PDT_UNSIGNED_LONG, 1, ReadLv3 | WriteLv0 }, - { PID_ERROR_CODE, false, PDT_ENUM8, 1, ReadLv3 | WriteLv0 }, -}; -static uint8_t _propertyDescriptionCount = sizeof(_propertyDescriptions) / sizeof(PropertyDescription); - -uint8_t GroupObjectTableObject::propertyDescriptionCount() -{ - return _propertyDescriptionCount; -} - - -PropertyDescription* GroupObjectTableObject::propertyDescriptions() -{ - return _propertyDescriptions; -} - void GroupObjectTableObject::freeGroupObjects() { if (_groupObjects) diff --git a/src/knx/group_object_table_object.h b/src/knx/group_object_table_object.h index 127dba7..490eaf6 100644 --- a/src/knx/group_object_table_object.h +++ b/src/knx/group_object_table_object.h @@ -10,7 +10,6 @@ class GroupObjectTableObject : public TableObject public: GroupObjectTableObject(Memory& memory); virtual ~GroupObjectTableObject(); - void readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) override; uint16_t entryCount(); GroupObject& get(uint16_t asap); GroupObject& nextUpdatedObject(bool& valid); @@ -20,8 +19,6 @@ class GroupObjectTableObject : public TableObject protected: void beforeStateChange(LoadState& newState) override; - uint8_t propertyDescriptionCount() override; - PropertyDescription* propertyDescriptions() override; private: void freeGroupObjects(); diff --git a/src/knx/interface_object.h b/src/knx/interface_object.h index 40a28e7..855cba6 100644 --- a/src/knx/interface_object.h +++ b/src/knx/interface_object.h @@ -148,7 +148,7 @@ class InterfaceObject : public SaveRestore /** * Intializes the Property-array the the supplied values. */ - void initializeProperties(size_t propertiesSize, Property** properties); + virtual void initializeProperties(size_t propertiesSize, Property** properties); Property** _properties = nullptr; uint8_t _propertyCount = 0; diff --git a/src/knx/table_object.cpp b/src/knx/table_object.cpp index 522da6f..f33e180 100644 --- a/src/knx/table_object.cpp +++ b/src/knx/table_object.cpp @@ -3,71 +3,15 @@ #include "table_object.h" #include "bits.h" #include "memory.h" +#include "callback_property.h" +#include "data_property.h" -TableObject::TableObject(Memory& memory): _memory(memory) -{ - -} - -void TableObject::readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) -{ - switch (id) - { - case PID_LOAD_STATE_CONTROL: - data[0] = _state; - break; - case PID_TABLE_REFERENCE: - if (_state == LS_UNLOADED) - pushInt(0, data); - else - pushInt(tableReference(), data); - break; - case PID_ERROR_CODE: - data[0] = _errorCode; - break; - default: - InterfaceObject::readProperty(id, start, count, data); - } -} - -void TableObject::writeProperty(PropertyID id, uint16_t start, uint8_t* data, uint8_t& count) -{ - switch (id) - { - case PID_LOAD_STATE_CONTROL: - loadEvent(data); - break; - - //case PID_MCB_TABLE: - // TODO - // break; - default: - InterfaceObject::writeProperty(id, start, data, count); - } -} - -uint8_t TableObject::propertySize(PropertyID id) -{ - switch (id) - { - case PID_LOAD_STATE_CONTROL: - return 1; - case PID_TABLE_REFERENCE: - return 4; - case PID_ERROR_CODE: - return 1; - case PID_OBJECT_TYPE: - return 2; - default: - return InterfaceObject::propertySize(id); - } -} +TableObject::TableObject(Memory& memory) + : _memory(memory) +{} TableObject::~TableObject() -{ -// if (_data != 0) -// _memory.freeMemory(_data); -} +{} LoadState TableObject::loadState() { @@ -86,7 +30,6 @@ void TableObject::loadState(LoadState newState) uint8_t* TableObject::save(uint8_t* buffer) { buffer = pushByte(_state, buffer); - buffer = pushByte(_errorCode, buffer); if (_data) buffer = pushInt(_memory.toRelative(_data), buffer); @@ -100,11 +43,8 @@ uint8_t* TableObject::save(uint8_t* buffer) uint8_t* TableObject::restore(uint8_t* buffer) { uint8_t state = 0; - uint8_t errorCode = 0; buffer = popByte(state, buffer); - buffer = popByte(errorCode, buffer); _state = (LoadState)state; - _errorCode = (ErrorCode)errorCode; uint32_t relativeAddress = 0; buffer = popInt(relativeAddress, buffer); @@ -180,7 +120,7 @@ void TableObject::loadEventUnloaded(uint8_t* data) break; default: loadState(LS_ERROR); - _errorCode = E_GOT_UNDEF_LOAD_CMD; + errorCode(E_GOT_UNDEF_LOAD_CMD); } } @@ -203,7 +143,7 @@ void TableObject::loadEventLoading(uint8_t* data) break; default: loadState(LS_ERROR); - _errorCode = E_GOT_UNDEF_LOAD_CMD; + errorCode(E_GOT_UNDEF_LOAD_CMD); } } @@ -229,11 +169,11 @@ void TableObject::loadEventLoaded(uint8_t* data) break; case LE_ADDITIONAL_LOAD_CONTROLS: loadState(LS_ERROR); - _errorCode = E_INVALID_OPCODE; + errorCode(E_INVALID_OPCODE); break; default: loadState(LS_ERROR); - _errorCode = E_GOT_UNDEF_LOAD_CMD; + errorCode(E_GOT_UNDEF_LOAD_CMD); } } @@ -252,7 +192,7 @@ void TableObject::loadEventError(uint8_t* data) break; default: loadState(LS_ERROR); - _errorCode = E_GOT_UNDEF_LOAD_CMD; + errorCode(E_GOT_UNDEF_LOAD_CMD); } } @@ -261,7 +201,7 @@ void TableObject::additionalLoadControls(uint8_t* data) if (data[1] != 0x0B) // Data Relative Allocation { loadState(LS_ERROR); - _errorCode = E_INVALID_OPCODE; + errorCode(E_INVALID_OPCODE); return; } @@ -271,7 +211,7 @@ void TableObject::additionalLoadControls(uint8_t* data) if (!allocTable(size, doFill, fillByte)) { loadState(LS_ERROR); - _errorCode = E_MAX_TABLE_LENGTH_EXEEDED; + errorCode(E_MAX_TABLE_LENGTH_EXEEDED); } } @@ -282,10 +222,58 @@ uint8_t* TableObject::data() void TableObject::errorCode(ErrorCode errorCode) { - _errorCode = errorCode; + uint8_t data = errorCode; + Property* prop = property(PID_ERROR_CODE); + prop->write(data); } uint16_t TableObject::saveSize() { - return 6; + return 5 + InterfaceObject::saveSize(); +} + +void TableObject::initializeProperties(size_t propertiesSize, Property** properties) +{ + Property* ownProperties[] = + { + new CallbackProperty(this, PID_LOAD_STATE_CONTROL, true, PDT_CONTROL, 1, ReadLv3 | WriteLv3, + [](TableObject* obj, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t { + if (start == 0) + return 1; + + data[0] = obj->_state; + return 1; + }, + [](TableObject* obj, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t { + obj->loadEvent(data); + return 1; + }), + new CallbackProperty(this, PID_TABLE_REFERENCE, false, PDT_UNSIGNED_LONG, 1, ReadLv3 | WriteLv0, + [](TableObject* obj, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t { + if (start == 0) + return 1; + + if (obj->_state == LS_UNLOADED) + pushInt(0, data); + else + pushInt(obj->tableReference(), data); + return 1; + }), + new DataProperty(PID_ERROR_CODE, false, PDT_ENUM8, 1, ReadLv3 | WriteLv0, (uint8_t)E_NO_FAULT) + }; + //TODO: missing + + // 23 PID_TABLE 3 / (3) + // 27 PID_MCB_TABLE 3 / 3 + + uint8_t ownPropertiesCount = sizeof(ownProperties) / sizeof(Property*); + + uint8_t propertyCount = propertiesSize / sizeof(Property*); + uint8_t allPropertiesCount = propertyCount + ownPropertiesCount; + + Property* allProperties[allPropertiesCount]; + memcpy(allProperties, properties, propertiesSize); + memcpy(allProperties + propertyCount, ownProperties, sizeof(ownProperties)); + + InterfaceObject::initializeProperties(sizeof(allProperties), allProperties); } diff --git a/src/knx/table_object.h b/src/knx/table_object.h index 9497751..3c15118 100644 --- a/src/knx/table_object.h +++ b/src/knx/table_object.h @@ -16,9 +16,7 @@ class TableObject: public InterfaceObject * @param memory The instance of the memory management class to use. */ TableObject(Memory& memory); - virtual void readProperty(PropertyID id, uint16_t start, uint8_t& count, uint8_t* data) override; - virtual void writeProperty(PropertyID id, uint16_t start, uint8_t* data, uint8_t& count) override; - virtual uint8_t propertySize(PropertyID id) override; + /** * The destructor. */ @@ -48,6 +46,8 @@ protected: */ void errorCode(ErrorCode errorCode); + void initializeProperties(size_t propertiesSize, Property** properties) override; + private: uint32_t tableReference(); bool allocTable(uint32_t size, bool doFill, uint8_t fillByte); @@ -68,5 +68,4 @@ protected: LoadState _state = LS_UNLOADED; Memory& _memory; uint8_t *_data = 0; - ErrorCode _errorCode = E_NO_FAULT; };