diff --git a/knxPython/CMakeLists.txt b/knxPython/CMakeLists.txt index 1e0f970..fbcd90f 100644 --- a/knxPython/CMakeLists.txt +++ b/knxPython/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 2.7) project(knx) set(LIBRARIES_FROM_REFERENCES "") -add_library(knx SHARED ../src/knx/address_table_object.cpp ../src/knx/apdu.cpp ../src/knx/application_layer.cpp ../src/knx/application_program_object.cpp ../src/knx/association_table_object.cpp ../src/knx/bau.cpp ../src/knx/bau07B0.cpp ../src/knx/bau57B0.cpp ../src/knx/bau_systemB.cpp ../src/knx/bits.cpp ../src/knx/cemi_frame.cpp ../src/knx/datapoint_types.cpp ../src/knx/data_link_layer.cpp ../src/knx/device_object.cpp ../src/knx/group_object.cpp ../src/knx/group_object_table_object.cpp ../src/knx/interface_object.cpp ../src/knx/ip_data_link_layer.cpp ../src/knx/ip_parameter_object.cpp ../src/knx/memory.cpp ../src/knx/network_layer.cpp ../src/knx/npdu.cpp ../src/knx/table_object.cpp ../src/knx/tpdu.cpp ../src/knx/tpuart_data_link_layer.cpp ../src/knx/transport_layer.cpp ../src/linux_platform.cpp knxmodule.cpp) +add_library(knx SHARED ../src/knx/address_table_object.cpp ../src/knx/apdu.cpp ../src/knx/application_layer.cpp ../src/knx/application_program_object.cpp ../src/knx/association_table_object.cpp ../src/knx/bau.cpp ../src/knx/bau07B0.cpp ../src/knx/bau57B0.cpp ../src/knx/bau_systemB.cpp ../src/knx/bits.cpp ../src/knx/cemi_frame.cpp ../src/knx/datapoint_types.cpp ../src/knx/data_link_layer.cpp ../src/knx/device_object.cpp ../src/knx/group_object.cpp ../src/knx/group_object_table_object.cpp ../src/knx/interface_object.cpp ../src/knx/ip_data_link_layer.cpp ../src/knx/ip_parameter_object.cpp ../src/knx/memory.cpp ../src/knx/network_layer.cpp ../src/knx/npdu.cpp ../src/knx/platform.cpp ../src/knx/table_object.cpp ../src/knx/tpdu.cpp ../src/knx/tpuart_data_link_layer.cpp ../src/knx/transport_layer.cpp ../src/linux_platform.cpp knxmodule.cpp) target_link_libraries(knx python3.5m "${LIBRARIES_FROM_REFERENCES}") include_directories(/usr/include/python3.5m ../src) SET_TARGET_PROPERTIES(knx PROPERTIES PREFIX "") diff --git a/knxPython/CMakeLists.txt.old b/knxPython/CMakeLists.txt.old index 8c103a7..1e0f970 100644 --- a/knxPython/CMakeLists.txt.old +++ b/knxPython/CMakeLists.txt.old @@ -7,5 +7,5 @@ project(knx) set(LIBRARIES_FROM_REFERENCES "") add_library(knx SHARED ../src/knx/address_table_object.cpp ../src/knx/apdu.cpp ../src/knx/application_layer.cpp ../src/knx/application_program_object.cpp ../src/knx/association_table_object.cpp ../src/knx/bau.cpp ../src/knx/bau07B0.cpp ../src/knx/bau57B0.cpp ../src/knx/bau_systemB.cpp ../src/knx/bits.cpp ../src/knx/cemi_frame.cpp ../src/knx/datapoint_types.cpp ../src/knx/data_link_layer.cpp ../src/knx/device_object.cpp ../src/knx/group_object.cpp ../src/knx/group_object_table_object.cpp ../src/knx/interface_object.cpp ../src/knx/ip_data_link_layer.cpp ../src/knx/ip_parameter_object.cpp ../src/knx/memory.cpp ../src/knx/network_layer.cpp ../src/knx/npdu.cpp ../src/knx/table_object.cpp ../src/knx/tpdu.cpp ../src/knx/tpuart_data_link_layer.cpp ../src/knx/transport_layer.cpp ../src/linux_platform.cpp knxmodule.cpp) target_link_libraries(knx python3.5m "${LIBRARIES_FROM_REFERENCES}") -include_directories(/usr/include/python3.5m) +include_directories(/usr/include/python3.5m ../src) SET_TARGET_PROPERTIES(knx PROPERTIES PREFIX "") diff --git a/knxPython/knxPython.vcxproj b/knxPython/knxPython.vcxproj index 7170bc0..71c1fb4 100644 --- a/knxPython/knxPython.vcxproj +++ b/knxPython/knxPython.vcxproj @@ -128,6 +128,7 @@ + diff --git a/knxPython/knxPython.vcxproj.filters b/knxPython/knxPython.vcxproj.filters index ac7acbf..1fdeb19 100644 --- a/knxPython/knxPython.vcxproj.filters +++ b/knxPython/knxPython.vcxproj.filters @@ -122,6 +122,9 @@ Source files\knx + + Source files\knx + diff --git a/src/esp_platform.cpp b/src/esp_platform.cpp index f68b4c0..0d99ca7 100644 --- a/src/esp_platform.cpp +++ b/src/esp_platform.cpp @@ -171,4 +171,4 @@ size_t EspPlatform::readBytesUart(uint8_t *buffer, size_t length) printHex("p>", buffer, length); return length; } -#endif \ No newline at end of file +#endif diff --git a/src/knx/address_table_object.cpp b/src/knx/address_table_object.cpp index 407be44..6f67a5c 100644 --- a/src/knx/address_table_object.cpp +++ b/src/knx/address_table_object.cpp @@ -6,7 +6,8 @@ using namespace std; -AddressTableObject::AddressTableObject(uint8_t* memoryReference): TableObject(memoryReference) +AddressTableObject::AddressTableObject(Platform& platform) + : TableObject(platform) { } diff --git a/src/knx/address_table_object.h b/src/knx/address_table_object.h index 970e438..a4b39a9 100644 --- a/src/knx/address_table_object.h +++ b/src/knx/address_table_object.h @@ -5,11 +5,8 @@ class AddressTableObject: public TableObject { public: - AddressTableObject(uint8_t* memoryReference); - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Woverloaded-virtual" + AddressTableObject(Platform& platform); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); - #pragma GCC diagnostic pop uint16_t entryCount(); uint16_t getGa(uint16_t tsap); uint16_t getTsap(uint16_t ga); diff --git a/src/knx/application_program_object.cpp b/src/knx/application_program_object.cpp index 9753045..c6dee8a 100644 --- a/src/knx/application_program_object.cpp +++ b/src/knx/application_program_object.cpp @@ -1,7 +1,8 @@ #include "application_program_object.h" #include "bits.h" -ApplicationProgramObject::ApplicationProgramObject(uint8_t* memoryReference): TableObject(memoryReference) +ApplicationProgramObject::ApplicationProgramObject(Platform& platform) + : TableObject(platform) { } diff --git a/src/knx/application_program_object.h b/src/knx/application_program_object.h index e90b13e..22d8b3a 100644 --- a/src/knx/application_program_object.h +++ b/src/knx/application_program_object.h @@ -5,11 +5,8 @@ class ApplicationProgramObject: public TableObject { public: - ApplicationProgramObject(uint8_t* memoryReference); - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Woverloaded-virtual" + ApplicationProgramObject(Platform& platform); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); - #pragma GCC diagnostic pop void writeProperty(PropertyID id, uint8_t start, uint8_t* data, uint8_t count); uint8_t propertySize(PropertyID id); uint8_t* data(uint32_t addr); diff --git a/src/knx/association_table_object.cpp b/src/knx/association_table_object.cpp index c8c2458..7f3aa95 100644 --- a/src/knx/association_table_object.cpp +++ b/src/knx/association_table_object.cpp @@ -6,7 +6,8 @@ using namespace std; -AssociationTableObject::AssociationTableObject(uint8_t* memoryReference): TableObject(memoryReference) +AssociationTableObject::AssociationTableObject(Platform& platform) + : TableObject(platform) { } diff --git a/src/knx/association_table_object.h b/src/knx/association_table_object.h index 23a8048..80a133d 100644 --- a/src/knx/association_table_object.h +++ b/src/knx/association_table_object.h @@ -5,11 +5,8 @@ class AssociationTableObject: public TableObject { public: - AssociationTableObject(uint8_t* memoryReference); - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Woverloaded-virtual" + AssociationTableObject(Platform& platform); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); - #pragma GCC diagnostic pop uint16_t entryCount(); uint16_t operator[](uint16_t idx); uint8_t* save(uint8_t* buffer); diff --git a/src/knx/bau_systemB.cpp b/src/knx/bau_systemB.cpp index 9cd66d0..0dceee4 100644 --- a/src/knx/bau_systemB.cpp +++ b/src/knx/bau_systemB.cpp @@ -2,8 +2,8 @@ #include #include -BauSystemB::BauSystemB(Platform& platform): _memoryReference((uint8_t*)&_deviceObj), _memory(platform), _addrTable(_memoryReference), - _assocTable(_memoryReference), _groupObjTable(_memoryReference), _appProgram(_memoryReference), +BauSystemB::BauSystemB(Platform& platform): _memory(platform), _addrTable(platform), + _assocTable(platform), _groupObjTable(platform), _appProgram(platform), _platform(platform), _appLayer(_assocTable, *this), _transLayer(_appLayer, _addrTable, _platform), _netLayer(_transLayer) { @@ -138,7 +138,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(_memoryReference + memoryAddress, data, number); + memcpy(_platform.memoryReference() + memoryAddress, data, number); _memory.memoryModified(); if (_deviceObj.verifyMode()) @@ -149,7 +149,7 @@ void BauSystemB::memoryReadIndication(Priority priority, HopCountType hopType, u uint16_t memoryAddress) { _appLayer.memoryReadResponse(AckRequested, priority, hopType, asap, number, memoryAddress, - _memoryReference + memoryAddress); + _platform.memoryReference() + memoryAddress); } void BauSystemB::restartRequestIndication(Priority priority, HopCountType hopType, uint16_t asap) @@ -160,6 +160,7 @@ void BauSystemB::restartRequestIndication(Priority priority, HopCountType hopTyp _platform.restart(); // for platforms that don't really restart + _memory.readMemory(); _deviceObj.progMode(false); _configured = true; } @@ -172,12 +173,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, - _memoryReference + memoryAddress); + _platform.memoryReference() + memoryAddress); } void BauSystemB::userMemoryWriteIndication(Priority priority, HopCountType hopType, uint16_t asap, uint8_t number, uint32_t memoryAddress, uint8_t* data) { - memcpy(_memoryReference + memoryAddress, data, number); + memcpy(_platform.memoryReference() + memoryAddress, data, number); _memory.memoryModified(); if (_deviceObj.verifyMode()) diff --git a/src/knx/bau_systemB.h b/src/knx/bau_systemB.h index bc2d246..ed80b7f 100644 --- a/src/knx/bau_systemB.h +++ b/src/knx/bau_systemB.h @@ -62,9 +62,6 @@ protected: void updateGroupObject(GroupObject& go, uint8_t* data, uint8_t length); DeviceObject _deviceObj; - // pointer to first private variable as reference to memory read/write commands - // addesses are to big for memory read/write calls on 64bit systems otherwise - uint8_t* _memoryReference; Memory _memory; AddressTableObject _addrTable; AssociationTableObject _assocTable; diff --git a/src/knx/group_object_table_object.cpp b/src/knx/group_object_table_object.cpp index 21182f2..543c73f 100644 --- a/src/knx/group_object_table_object.cpp +++ b/src/knx/group_object_table_object.cpp @@ -4,7 +4,8 @@ #include "group_object.h" #include "bits.h" -GroupObjectTableObject::GroupObjectTableObject(uint8_t* memoryReference): TableObject(memoryReference) +GroupObjectTableObject::GroupObjectTableObject(Platform& platform) + : TableObject(platform) { _groupObjects = 0; _groupObjectCount = 0; diff --git a/src/knx/group_object_table_object.h b/src/knx/group_object_table_object.h index 8cd3bdf..c3c742e 100644 --- a/src/knx/group_object_table_object.h +++ b/src/knx/group_object_table_object.h @@ -8,11 +8,8 @@ class GroupObjectTableObject: public TableObject friend class GroupObject; public: - GroupObjectTableObject(uint8_t* memoryReference); - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Woverloaded-virtual" + GroupObjectTableObject(Platform& platform); void readProperty(PropertyID id, uint32_t start, uint32_t& count, uint8_t* data); - #pragma GCC diagnostic pop uint16_t entryCount(); GroupObject& get(uint16_t asap); GroupObject& nextUpdatedObject(bool& valid); diff --git a/src/knx/platform.cpp b/src/knx/platform.cpp new file mode 100644 index 0000000..19cfcad --- /dev/null +++ b/src/knx/platform.cpp @@ -0,0 +1,23 @@ +#include "platform.h" + +#include + +uint8_t* Platform::memoryReference() +{ + return _memoryReference; +} + + +uint8_t* Platform::allocMemory(size_t size) +{ + uint8_t* address = (uint8_t*)malloc(size); + if (_memoryReference == 0) + _memoryReference = address; + + return address; +} + +void Platform::freeMemory(uint8_t* ptr) +{ + free(ptr); +} diff --git a/src/knx/platform.h b/src/knx/platform.h index 78273fc..1fba4ac 100644 --- a/src/knx/platform.h +++ b/src/knx/platform.h @@ -32,4 +32,10 @@ public: virtual uint8_t* getEepromBuffer(uint16_t size) = 0; virtual void commitToEeprom() = 0; + + virtual uint8_t* memoryReference(); + virtual uint8_t* allocMemory(size_t size); + virtual void freeMemory(uint8_t* ptr); +protected: + uint8_t* _memoryReference = 0; }; \ No newline at end of file diff --git a/src/knx/table_object.cpp b/src/knx/table_object.cpp index eaa014f..65d7446 100644 --- a/src/knx/table_object.cpp +++ b/src/knx/table_object.cpp @@ -1,10 +1,9 @@ -#include #include #include "table_object.h" #include "bits.h" -TableObject::TableObject(uint8_t* memoryReference): _memoryReference(memoryReference) +TableObject::TableObject(Platform& platform): _platform(platform) { } @@ -61,7 +60,7 @@ uint8_t TableObject::propertySize(PropertyID id) TableObject::~TableObject() { if (_data != 0) - free(_data); + _platform.freeMemory(_data); } LoadState TableObject::loadState() @@ -101,9 +100,9 @@ uint8_t* TableObject::restore(uint8_t* buffer) buffer = popInt(_size, buffer); if (_data) - free(_data); + _platform.freeMemory(_data); - _data = (uint8_t*) malloc(_size); + _data = _platform.allocMemory(_size); buffer = popByteArray(_data, _size, buffer); @@ -112,21 +111,22 @@ uint8_t* TableObject::restore(uint8_t* buffer) uint32_t TableObject::tableReference() { - return (uint32_t)(_data - _memoryReference); + return (uint32_t)(_data - _platform.memoryReference()); } bool TableObject::allocTable(uint32_t size, bool doFill, uint8_t fillByte) { if (_data) { - free(_data); + _platform.freeMemory(_data); + _data = 0; _size = 0; } if (size == 0) return true; - _data = (uint8_t*)malloc(size); + _data = _platform.allocMemory(size); if (!_data) return false; diff --git a/src/knx/table_object.h b/src/knx/table_object.h index 1bf517e..76bb526 100644 --- a/src/knx/table_object.h +++ b/src/knx/table_object.h @@ -1,11 +1,12 @@ #pragma once #include "interface_object.h" +#include "platform.h" class TableObject: public InterfaceObject { public: - TableObject(uint8_t* memoryReference); + TableObject(Platform& platform); 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); @@ -29,5 +30,5 @@ private: void additionalLoadControls(uint8_t* data); void loadState(LoadState newState); LoadState _state = LS_UNLOADED; - uint8_t* _memoryReference; + Platform& _platform; }; diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp index 229f4c0..a17d92e 100644 --- a/src/linux_platform.cpp +++ b/src/linux_platform.cpp @@ -1,13 +1,16 @@ #include "linux_platform.h" #ifdef __linux__ -#include +#include #include -#include +#include +#include +#include +#include + #include #include #include #include -#include #include #include #include @@ -23,9 +26,13 @@ #include "knx/application_program_object.h" #include "knx/ip_parameter_object.h" +#define MAX_MEM 4096 + LinuxPlatform::LinuxPlatform() { doMemoryMapping(); + Platform::_memoryReference = (uint8_t*)malloc(MAX_MEM); + _currentMaxMem = Platform::_memoryReference; } uint32_t LinuxPlatform::currentIpAddress() @@ -72,7 +79,13 @@ void LinuxPlatform::macAddress(uint8_t* data) void LinuxPlatform::restart() { - // do nothing + // clear alocated memory + if(_memoryReference != 0) + { + free(_memoryReference); + Platform::_memoryReference = (uint8_t*)malloc(MAX_MEM); + _currentMaxMem = Platform::_memoryReference; + } } void LinuxPlatform::fatalError() @@ -276,4 +289,23 @@ void LinuxPlatform::closeUart() void LinuxPlatform::setupUart() { } -#endif \ No newline at end of file + +/* + * On linux the memory addresses from malloc may be to big for usermermory_write. + * So we allocate some memory at the beginning and use it for address table, group object table etc. + * + **/ +uint8_t* LinuxPlatform::allocMemory(size_t size) +{ + uint8_t* addr = _currentMaxMem; + _currentMaxMem += size; + if ((_currentMaxMem - Platform::_memoryReference) > MAX_MEM) + throw std::overflow_error("MAX_MEM was to small"); + return addr; +} +#endif + +void LinuxPlatform::freeMemory(uint8_t* ptr) +{ + /* do nothing. Memory is freed on restart()*/ +} diff --git a/src/linux_platform.h b/src/linux_platform.h index 710b9fa..cf7c174 100644 --- a/src/linux_platform.h +++ b/src/linux_platform.h @@ -6,6 +6,7 @@ class LinuxPlatform: public Platform { + using Platform::_memoryReference; public: LinuxPlatform(); @@ -39,6 +40,8 @@ public: //memory uint8_t* getEepromBuffer(uint16_t size); void commitToEeprom(); + uint8_t* allocMemory(size_t size); + void freeMemory(uint8_t* ptr); private: uint32_t _multicastAddr; uint16_t _port; @@ -46,6 +49,7 @@ private: void doMemoryMapping(); uint8_t* _mappedFile; int _fd; + uint8_t* _currentMaxMem = 0; }; #endif \ No newline at end of file diff --git a/src/samd_platform.cpp b/src/samd_platform.cpp index 3e72a23..102515c 100644 --- a/src/samd_platform.cpp +++ b/src/samd_platform.cpp @@ -153,5 +153,4 @@ size_t SamdPlatform::readBytesUart(uint8_t *buffer, size_t length) //printHex("p>", buffer, length); return length; } - #endif \ No newline at end of file