From 94b91278bd57cdf57ed62509ba661b0607710d6f Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Tue, 29 Oct 2019 21:15:12 +0100 Subject: [PATCH] access and allocate memory for interface-objects trough the memory class --- src/knx/address_table_object.cpp | 5 ++--- src/knx/address_table_object.h | 4 ++-- src/knx/application_program_object.cpp | 4 ++-- src/knx/application_program_object.h | 2 +- src/knx/association_table_object.cpp | 5 ++--- src/knx/association_table_object.h | 2 +- src/knx/bau_systemB.cpp | 14 +++++------- src/knx/group_object_table_object.cpp | 4 ++-- src/knx/group_object_table_object.h | 2 +- src/knx/memory.cpp | 31 ++++++++++++++++++++++++++ src/knx/memory.h | 9 +++++++- src/knx/table_object.cpp | 16 ++++++------- src/knx/table_object.h | 8 +++---- 13 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/knx/address_table_object.cpp b/src/knx/address_table_object.cpp index c5ea3ec..c0d8ec8 100644 --- a/src/knx/address_table_object.cpp +++ b/src/knx/address_table_object.cpp @@ -5,9 +5,8 @@ using namespace std; - -AddressTableObject::AddressTableObject(Platform& platform) - : TableObject(platform) +AddressTableObject::AddressTableObject(Memory& memory) + : TableObject(memory) { } diff --git a/src/knx/address_table_object.h b/src/knx/address_table_object.h index 28ce2bc..c46ba07 100644 --- a/src/knx/address_table_object.h +++ b/src/knx/address_table_object.h @@ -15,9 +15,9 @@ class AddressTableObject : public TableObject /** * The contructor. * - * @param platform This parameter is only passed to the custructor of TableObject an not used by this class. + * @param memory This parameter is only passed to the custructor of TableObject an not used by this class. */ - AddressTableObject(Platform& platform); + AddressTableObject(Memory& memory); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); uint8_t* save(uint8_t* buffer); uint8_t* restore(uint8_t* buffer); diff --git a/src/knx/application_program_object.cpp b/src/knx/application_program_object.cpp index 7269ff4..4c183b6 100644 --- a/src/knx/application_program_object.cpp +++ b/src/knx/application_program_object.cpp @@ -1,8 +1,8 @@ #include "application_program_object.h" #include "bits.h" -ApplicationProgramObject::ApplicationProgramObject(Platform& platform) - : TableObject(platform) +ApplicationProgramObject::ApplicationProgramObject(Memory& memory) + : TableObject(memory) { } diff --git a/src/knx/application_program_object.h b/src/knx/application_program_object.h index e7ac83f..abd8e04 100644 --- a/src/knx/application_program_object.h +++ b/src/knx/application_program_object.h @@ -5,7 +5,7 @@ class ApplicationProgramObject : public TableObject { public: - ApplicationProgramObject(Platform& platform); + ApplicationProgramObject(Memory& memory); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); void writeProperty(PropertyID id, uint8_t start, uint8_t* data, uint8_t count); uint8_t propertySize(PropertyID id); diff --git a/src/knx/association_table_object.cpp b/src/knx/association_table_object.cpp index ce428ae..b640219 100644 --- a/src/knx/association_table_object.cpp +++ b/src/knx/association_table_object.cpp @@ -5,9 +5,8 @@ using namespace std; - -AssociationTableObject::AssociationTableObject(Platform& platform) - : TableObject(platform) +AssociationTableObject::AssociationTableObject(Memory& memory) + : TableObject(memory) { } diff --git a/src/knx/association_table_object.h b/src/knx/association_table_object.h index b56587c..fcf45cd 100644 --- a/src/knx/association_table_object.h +++ b/src/knx/association_table_object.h @@ -5,7 +5,7 @@ class AssociationTableObject : public TableObject { public: - AssociationTableObject(Platform& platform); + AssociationTableObject(Memory& memory); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); uint8_t* save(uint8_t* buffer); diff --git a/src/knx/bau_systemB.cpp b/src/knx/bau_systemB.cpp index 9840c0d..75d0938 100644 --- a/src/knx/bau_systemB.cpp +++ b/src/knx/bau_systemB.cpp @@ -11,8 +11,8 @@ enum NmReadSerialNumberType NM_Read_SerialNumber_By_ManufacturerSpecific = 0xFE, }; -BauSystemB::BauSystemB(Platform& platform): _memory(platform), _addrTable(platform), - _assocTable(platform), _groupObjTable(platform), _appProgram(platform), +BauSystemB::BauSystemB(Platform& platform): _memory(platform), _addrTable(_memory), + _assocTable(_memory), _groupObjTable(_memory), _appProgram(_memory), _platform(platform), _appLayer(_assocTable, *this), _transLayer(_appLayer, _addrTable), _netLayer(_transLayer) { @@ -149,8 +149,7 @@ void BauSystemB::deviceDescriptorReadIndication(Priority priority, HopCountType void BauSystemB::memoryWriteIndication(Priority priority, HopCountType hopType, uint16_t asap, uint8_t number, uint16_t memoryAddress, uint8_t * data) { - memcpy(_platform.memoryReference() + memoryAddress, data, number); - _memory.memoryModified(); + _memory.writeMemory(memoryAddress, number, data); if (_deviceObj.verifyMode()) memoryReadIndication(priority, hopType, asap, number, memoryAddress); @@ -160,7 +159,7 @@ void BauSystemB::memoryReadIndication(Priority priority, HopCountType hopType, u uint16_t memoryAddress) { _appLayer.memoryReadResponse(AckRequested, priority, hopType, asap, number, memoryAddress, - _platform.memoryReference() + memoryAddress); + _memory.toAbsolute(memoryAddress)); } void BauSystemB::restartRequestIndication(Priority priority, HopCountType hopType, uint16_t asap) @@ -178,13 +177,12 @@ void BauSystemB::authorizeIndication(Priority priority, HopCountType hopType, ui void BauSystemB::userMemoryReadIndication(Priority priority, HopCountType hopType, uint16_t asap, uint8_t number, uint32_t memoryAddress) { _appLayer.userMemoryReadResponse(AckRequested, priority, hopType, asap, number, memoryAddress, - _platform.memoryReference() + memoryAddress); + _memory.toAbsolute(memoryAddress)); } void BauSystemB::userMemoryWriteIndication(Priority priority, HopCountType hopType, uint16_t asap, uint8_t number, uint32_t memoryAddress, uint8_t* data) { - memcpy(_platform.memoryReference() + memoryAddress, data, number); - _memory.memoryModified(); + _memory.writeMemory(memoryAddress, number, data); if (_deviceObj.verifyMode()) userMemoryReadIndication(priority, hopType, asap, number, memoryAddress); diff --git a/src/knx/group_object_table_object.cpp b/src/knx/group_object_table_object.cpp index 5b40e85..3e159d6 100644 --- a/src/knx/group_object_table_object.cpp +++ b/src/knx/group_object_table_object.cpp @@ -4,8 +4,8 @@ #include "group_object.h" #include "bits.h" -GroupObjectTableObject::GroupObjectTableObject(Platform& platform) - : TableObject(platform) +GroupObjectTableObject::GroupObjectTableObject(Memory& memory) + : TableObject(memory) { } diff --git a/src/knx/group_object_table_object.h b/src/knx/group_object_table_object.h index bd9bdd3..4fb7a62 100644 --- a/src/knx/group_object_table_object.h +++ b/src/knx/group_object_table_object.h @@ -8,7 +8,7 @@ class GroupObjectTableObject : public TableObject friend class GroupObject; public: - GroupObjectTableObject(Platform& platform); + GroupObjectTableObject(Memory& memory); virtual ~GroupObjectTableObject(); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); uint16_t entryCount(); diff --git a/src/knx/memory.cpp b/src/knx/memory.cpp index 975ecc0..be96714 100644 --- a/src/knx/memory.cpp +++ b/src/knx/memory.cpp @@ -1,4 +1,5 @@ #include "memory.h" +#include Memory::Memory(Platform & platform): _platform(platform) { @@ -54,3 +55,33 @@ void Memory::addSaveRestore(SaveRestore * obj) _saveRestores[_saveCount] = obj; _saveCount += 1; } + + +uint8_t* Memory::allocMemory(size_t size) +{ + return _platform.allocMemory(size); +} + + +void Memory::freeMemory(uint8_t* ptr) +{ + return _platform.freeMemory(ptr); +} + +void Memory::writeMemory(uint32_t relativeAddress, size_t size, uint8_t* data) +{ + memcpy(toAbsolute(relativeAddress), data, size); + memoryModified(); +} + + +uint8_t* Memory::toAbsolute(uint32_t relativeAddress) +{ + return _platform.memoryReference() + (ptrdiff_t)relativeAddress; +} + + +uint32_t Memory::toRelative(uint8_t* absoluteAddress) +{ + return absoluteAddress - _platform.memoryReference(); +} diff --git a/src/knx/memory.h b/src/knx/memory.h index b9831ec..c299d22 100644 --- a/src/knx/memory.h +++ b/src/knx/memory.h @@ -15,7 +15,14 @@ public: void readMemory(); void writeMemory(); void addSaveRestore(SaveRestore* obj); -private: + + uint8_t* allocMemory(size_t size); + void freeMemory(uint8_t* ptr); + void writeMemory(uint32_t relativeAddress, size_t size, uint8_t* data); + uint8_t* toAbsolute(uint32_t relativeAddress); + uint32_t toRelative(uint8_t* absoluteAddress); + + private: Platform& _platform; bool _modified = false; SaveRestore* _saveRestores[MAXSAVE] = {0}; diff --git a/src/knx/table_object.cpp b/src/knx/table_object.cpp index c9aaa1b..0e3fc12 100644 --- a/src/knx/table_object.cpp +++ b/src/knx/table_object.cpp @@ -3,7 +3,7 @@ #include "table_object.h" #include "bits.h" -TableObject::TableObject(Platform& platform): _platform(platform) +TableObject::TableObject(Memory& memory): _memory(memory) { } @@ -65,7 +65,7 @@ uint8_t TableObject::propertySize(PropertyID id) TableObject::~TableObject() { if (_data != 0) - _platform.freeMemory(_data); + _memory.freeMemory(_data); } LoadState TableObject::loadState() @@ -105,10 +105,10 @@ uint8_t* TableObject::restore(uint8_t* buffer) buffer = popInt(_size, buffer); if (_data) - _platform.freeMemory(_data); + _memory.freeMemory(_data); if (_size > 0) - _data = _platform.allocMemory(_size); + _data = _memory.allocMemory(_size); else _data = 0; @@ -119,22 +119,22 @@ uint8_t* TableObject::restore(uint8_t* buffer) uint32_t TableObject::tableReference() { - return (uint32_t)(_data - _platform.memoryReference()); + return (uint32_t)_memory.toRelative(_data); } bool TableObject::allocTable(uint32_t size, bool doFill, uint8_t fillByte) { if (_data) { - _platform.freeMemory(_data); + _memory.freeMemory(_data); _data = 0; _size = 0; } if (size == 0) return true; - - _data = _platform.allocMemory(size); + + _data = _memory.allocMemory(size); if (!_data) return false; diff --git a/src/knx/table_object.h b/src/knx/table_object.h index d30fb70..8fd64d6 100644 --- a/src/knx/table_object.h +++ b/src/knx/table_object.h @@ -1,7 +1,7 @@ #pragma once #include "interface_object.h" -#include "platform.h" +#include "memory.h" /** * This class provides common functionality for interface objects that are configured by ETS with MemorWrite. @@ -11,9 +11,9 @@ class TableObject: public InterfaceObject public: /** * The constuctor. - * @param platform the Platform on which the software runs. The class uses the memory management features of Platform. + * @param memory The instance of the memory management class to use. */ - TableObject(Platform& platform); + TableObject(Memory& memory); virtual void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); virtual void writeProperty(PropertyID id, uint8_t start, uint8_t* data, uint8_t count); virtual uint8_t propertySize(PropertyID id); @@ -67,7 +67,7 @@ protected: */ void loadState(LoadState newState); LoadState _state = LS_UNLOADED; - Platform& _platform; + Memory& _memory; uint8_t *_data = 0; uint32_t _size = 0; ErrorCode _errorCode = E_NO_FAULT;