save work

This commit is contained in:
Thomas Kunze 2019-12-01 21:47:29 +01:00
parent 266a2e126d
commit 77a0d6fc2e
4 changed files with 134 additions and 10 deletions

49
src/knx/knx_ip_frame.cpp Normal file
View File

@ -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);
}

48
src/knx/knx_ip_frame.h Normal file
View File

@ -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;
};

View File

@ -47,6 +47,9 @@ void Memory::readMemory()
uint16_t memorySize = 0; uint16_t memorySize = 0;
buffer = popWord(memorySize, buffer); buffer = popWord(memorySize, buffer);
if (memorySize == 0)
continue;
// this works because TableObject saves a relative addr and restores it itself // this works because TableObject saves a relative addr and restores it itself
addNewUsedBlock(_tableObjects[i]->_data, memorySize); addNewUsedBlock(_tableObjects[i]->_data, memorySize);
} }
@ -69,6 +72,8 @@ void Memory::writeMemory()
buffer = _tableObjects[i]->save(buffer); buffer = _tableObjects[i]->save(buffer);
//save to size of the memoryblock for tableobject too, so that we can rebuild the usedList and freeList //save to size of the memoryblock for tableobject too, so that we can rebuild the usedList and freeList
if (_tableObjects[i]->_data != nullptr)
{
MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data); MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data);
if (block == nullptr) if (block == nullptr)
@ -78,6 +83,9 @@ void Memory::writeMemory()
} }
buffer = pushWord(block->size, buffer); buffer = pushWord(block->size, buffer);
} }
else
pushWord(0, buffer);
}
_platform.commitToEeprom(); _platform.commitToEeprom();
} }
@ -94,7 +102,7 @@ void Memory::addSaveRestore(SaveRestore* obj)
void Memory::addSaveRestore(TableObject* obj) void Memory::addSaveRestore(TableObject* obj)
{ {
if (_tableObjCount >= MAXTABLEOBJ - 1) if (_tableObjCount >= MAXTABLEOBJ)
return; return;
_tableObjects[_tableObjCount] = obj; _tableObjects[_tableObjCount] = obj;
@ -118,6 +126,8 @@ uint8_t* Memory::allocMemory(size_t size)
{ {
if (blockToUse != nullptr && (blockToUse->size - size) > (freeBlock->size - size)) if (blockToUse != nullptr && (blockToUse->size - size) > (freeBlock->size - size))
blockToUse = freeBlock; blockToUse = freeBlock;
else if (blockToUse == nullptr)
blockToUse = freeBlock;
} }
freeBlock = freeBlock->next; freeBlock = freeBlock->next;
} }
@ -254,12 +264,29 @@ void Memory::addToFreeList(MemoryBlock* block)
MemoryBlock* current = _freeList; MemoryBlock* current = _freeList;
while (current) 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; block->next = current->next;
current->next = block; current->next = block;
break; 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; current = current->next;
} }
// now check if we can merge the blocks // now check if we can merge the blocks

View File

@ -65,8 +65,8 @@ uint8_t TableObject::propertySize(PropertyID id)
TableObject::~TableObject() TableObject::~TableObject()
{ {
if (_data != 0) // if (_data != 0)
_memory.freeMemory(_data); // _memory.freeMemory(_data);
} }
LoadState TableObject::loadState() LoadState TableObject::loadState()