From 77a0d6fc2e5b9f3f86deb0628986dea7c4371112 Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Sun, 1 Dec 2019 21:47:29 +0100 Subject: [PATCH] save work --- src/knx/knx_ip_frame.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ src/knx/knx_ip_frame.h | 48 +++++++++++++++++++++++++++++++++++++++ src/knx/memory.cpp | 43 ++++++++++++++++++++++++++++------- src/knx/table_object.cpp | 4 ++-- 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 src/knx/knx_ip_frame.cpp create mode 100644 src/knx/knx_ip_frame.h diff --git a/src/knx/knx_ip_frame.cpp b/src/knx/knx_ip_frame.cpp new file mode 100644 index 0000000..33b6b04 --- /dev/null +++ b/src/knx/knx_ip_frame.cpp @@ -0,0 +1,49 @@ +#include "knx_ip_frame.h" +#include "bits.h" + +#define KNXIP_HEADER_LEN 0x6 +#define KNXIP_PROTOCOL_VERSION 0x10 + +KnxIpFrame::KnxIpFrame(uint8_t* data, + uint16_t length) +{ + _data = data; +} + +uint8_t KnxIpFrame::headerLength() const +{ + return _data[0]; +} + +void KnxIpFrame::headerLength(uint8_t length) +{ + _data[0] = length; +} + +KnxIpVersion KnxIpFrame::protocolVersion() const +{ + return (KnxIpVersion)_data[1]; +} + +void KnxIpFrame::protocolVersion(KnxIpVersion version) +{ + _data[1] = (uint8_t)version; +} + +uint16_t KnxIpFrame::serviceTypeIdentifier() const +{ +} + +void KnxIpFrame::serviceTypeIdentifier(uint16_t identifier) +{ +} + +uint16_t KnxIpFrame::totalLength() const +{ + return getWord(_data + 2); +} + +void KnxIpFrame::totalLength(uint16_t length) +{ + pushWord(length, _data + 2); +} \ No newline at end of file diff --git a/src/knx/knx_ip_frame.h b/src/knx/knx_ip_frame.h new file mode 100644 index 0000000..fafe05c --- /dev/null +++ b/src/knx/knx_ip_frame.h @@ -0,0 +1,48 @@ +#pragma once + +#include "cemi_frame.h" + +enum KnxIpVersion +{ + KnxIp1_0 +}; + +enum KnxIpServiceType +{ + SearchRequest = 0x201, + SearchResponse = 0x202, + DescriptionRequest = 0x203, + DescriptionResponse = 0x204, + ConnectRequest = 0x205, + ConnectResponse = 0x206, + ConnectionStateRequest = 0x207, + ConnectionStateResponse = 0x208, + DisconnectRequest = 0x209, + DisconnectResponse = 0x20A, + DeviceConfigurationRequest = 0x310, + DeviceConfigurationAck = 0x311, + TunnelingRequest = 0x420, + TunnelingAck = 0x421, + RoutingIndication = 0x530, + RoutingLostMessage = 0x531, + + + +}; + +class KnxIpFrame +{ + public: + KnxIpFrame(uint8_t* data, uint16_t length); + uint8_t headerLength() const; + void headerLength(uint8_t length); + KnxIpVersion protocolVersion() const; + void protocolVersion(KnxIpVersion version); + uint16_t serviceTypeIdentifier() const; + void serviceTypeIdentifier(uint16_t identifier); + uint16_t totalLength() const; + void totalLength(uint16_t length); + + private: + uint8_t* _data = 0; +}; \ No newline at end of file diff --git a/src/knx/memory.cpp b/src/knx/memory.cpp index 58e846a..006a78f 100644 --- a/src/knx/memory.cpp +++ b/src/knx/memory.cpp @@ -47,6 +47,9 @@ void Memory::readMemory() uint16_t memorySize = 0; buffer = popWord(memorySize, buffer); + if (memorySize == 0) + continue; + // this works because TableObject saves a relative addr and restores it itself addNewUsedBlock(_tableObjects[i]->_data, memorySize); } @@ -69,14 +72,19 @@ void Memory::writeMemory() buffer = _tableObjects[i]->save(buffer); //save to size of the memoryblock for tableobject too, so that we can rebuild the usedList and freeList - - MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data); - if (block == nullptr) + if (_tableObjects[i]->_data != nullptr) { - println("_data of TableObject not in errorlist"); - _platform.fatalError(); + + MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data); + if (block == nullptr) + { + println("_data of TableObject not in errorlist"); + _platform.fatalError(); + } + buffer = pushWord(block->size, buffer); } - buffer = pushWord(block->size, buffer); + else + pushWord(0, buffer); } _platform.commitToEeprom(); @@ -94,7 +102,7 @@ void Memory::addSaveRestore(SaveRestore* obj) void Memory::addSaveRestore(TableObject* obj) { - if (_tableObjCount >= MAXTABLEOBJ - 1) + if (_tableObjCount >= MAXTABLEOBJ) return; _tableObjects[_tableObjCount] = obj; @@ -118,6 +126,8 @@ uint8_t* Memory::allocMemory(size_t size) { if (blockToUse != nullptr && (blockToUse->size - size) > (freeBlock->size - size)) blockToUse = freeBlock; + else if (blockToUse == nullptr) + blockToUse = freeBlock; } freeBlock = freeBlock->next; } @@ -254,12 +264,29 @@ void Memory::addToFreeList(MemoryBlock* block) MemoryBlock* current = _freeList; while (current) { - if (current->address <= block->address && (block->next == nullptr || block->address < current->next->address)) + if (current->address <= block->address && (current->next == nullptr || block->address < current->next->address)) { + //add after current block->next = current->next; current->next = block; break; } + else if (current->address > block->address) + { + //add before current + block->next = current; + + if (current == _freeList) + _freeList = block; + + // swap current and block for merge + MemoryBlock* tmp = current; + current = block; + block = tmp; + + break; + } + current = current->next; } // now check if we can merge the blocks diff --git a/src/knx/table_object.cpp b/src/knx/table_object.cpp index 16cc7f0..7fa606a 100644 --- a/src/knx/table_object.cpp +++ b/src/knx/table_object.cpp @@ -65,8 +65,8 @@ uint8_t TableObject::propertySize(PropertyID id) TableObject::~TableObject() { - if (_data != 0) - _memory.freeMemory(_data); +// if (_data != 0) +// _memory.freeMemory(_data); } LoadState TableObject::loadState()