-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializer.cpp
75 lines (62 loc) · 1.77 KB
/
serializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* author : Shuichi TAKANO
* since : Sun Feb 11 2024 17:41:30
*/
#include "serializer.h"
#include <hardware/flash.h>
#include <hardware/sync.h>
#include <cassert>
#include "debug.h"
namespace
{
constexpr uint32_t getFlashOfs()
{
return 1536 * 1024; // flash 先頭から 1.5MiB
}
constexpr const uint8_t *getFlashAddr()
{
return reinterpret_cast<const uint8_t *>(getFlashOfs() + XIP_BASE);
}
}
Serializer::Serializer(size_t size, size_t margin)
{
auto alignedSize = (size + FLASH_SECTOR_SIZE - 1) & ~(FLASH_SECTOR_SIZE - 1);
data_.reserve(alignedSize);
data_.resize(sizeof(SerializeHeader));
limitSize_ = alignedSize - margin;
}
void Serializer::flash()
{
auto actualSize = data_.size();
data_.resize((data_.size() + FLASH_SECTOR_SIZE - 1) & ~(FLASH_SECTOR_SIZE - 1));
SerializeHeader *p = reinterpret_cast<SerializeHeader *>(data_.data());
*p = {};
p->size = actualSize;
auto dstOfs = getFlashOfs();
{
auto save = save_and_disable_interrupts();
flash_range_erase(dstOfs, data_.size());
flash_range_program(dstOfs, data_.data(), data_.size());
restore_interrupts(save);
}
DPRINT(("flash: %d bytes. actual %d bytes.\n", data_.size(), actualSize));
}
///////////////////
Deserializer::Deserializer()
{
p_ = reinterpret_cast<const uint8_t *>(getFlashAddr());
header_ = reinterpret_cast<const SerializeHeader *>(p_);
if (header_->magic != SerializeHeader::MAGIC ||
header_->version < SerializeHeader::MIN_ENABLED_VER)
{
p_ = nullptr;
return;
}
tail_ = p_ + header_->size;
DPRINT(("Deserialize: %d bytes.\n", header_->size));
p_ += sizeof(SerializeHeader);
}
Deserializer::~Deserializer()
{
assert(p_ <= tail_);
}