supla-device
Loading...
Searching...
No Matches
storage.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#ifndef SRC_SUPLA_SUPLET_STORAGE_H_
20#define SRC_SUPLA_SUPLET_STORAGE_H_
21
22#include <stdint.h>
23#include <stddef.h>
24#include <supla/suplet/channel_map.h>
25#include <supla/suplet/config.h>
26
27namespace Supla {
28
29class Config;
30
31namespace Suplet {
32
33struct InstanceRecord {
34 InstanceRecord();
35 InstanceRecord(const InstanceRecord &other);
36 InstanceRecord &operator=(const InstanceRecord &other);
37 ~InstanceRecord();
38
39 bool setConfig(const uint8_t *data, uint16_t size);
40 void clearConfig();
41
42 uint8_t instanceId = 0;
43 uint32_t definitionId = 0;
44 uint16_t definitionVersion = 0;
45 uint8_t subDeviceId = 0;
46 uint16_t configSize = 0;
47 ChannelMap channelMap = {};
48 uint8_t *config = nullptr;
49};
50
51class InstanceTable {
52 public:
53 InstanceTable();
54 InstanceTable(const InstanceTable &) = delete;
55 InstanceTable &operator=(const InstanceTable &) = delete;
56 ~InstanceTable();
57
58 uint8_t getCount() const;
59 void clear();
60
61 bool add(const InstanceRecord &record);
62 bool removeByInstanceId(uint8_t instanceId);
63 InstanceRecord *findByInstanceId(uint8_t instanceId);
64 const InstanceRecord *findByInstanceId(uint8_t instanceId) const;
65 InstanceRecord *findBySubDeviceId(uint8_t subDeviceId);
66 const InstanceRecord *findBySubDeviceId(uint8_t subDeviceId) const;
67 InstanceRecord *getRecord(uint8_t index);
68 const InstanceRecord *getRecord(uint8_t index) const;
69
70 private:
71 bool resize(uint8_t newCount);
72
73 InstanceRecord *records = nullptr;
74 uint8_t count = 0;
75};
76
77class Storage {
78 public:
79 explicit Storage(Supla::Config *config);
80
81 bool load(InstanceTable *table);
82 bool loadIndex(InstanceTable *table);
83 bool loadInstance(uint8_t instanceId, InstanceRecord *record);
84 bool save(const InstanceTable &table);
85 bool erase();
86
87 private:
88#pragma pack(push, 1)
89 struct StoredInstanceHeader {
90 uint8_t version = 0;
91 uint8_t reserved = 0;
92 uint32_t definitionId = 0;
93 uint16_t definitionVersion = 0;
94 uint8_t channelCount = 0;
95 uint16_t configSize = 0;
96 };
97
98 struct StoredChannelMapping {
99 uint8_t channelId = 0;
100 uint8_t channelNumber = 0;
101 };
102#pragma pack(pop)
103
104 bool loadVariant(uint8_t instanceId, uint8_t variant, InstanceRecord *record)
105 const;
106 bool loadVariant(uint8_t instanceId,
107 uint8_t variant,
108 InstanceRecord *record,
109 bool loadConfig) const;
110 bool loadActiveVariant(uint8_t instanceId,
111 uint8_t *activeVariant,
112 InstanceRecord *record,
113 bool loadConfig,
114 bool cleanup);
115 bool saveVariant(const InstanceRecord &record, uint8_t variant);
116 bool eraseVariant(uint8_t instanceId, uint8_t variant);
117 bool eraseInstance(uint8_t instanceId);
118 bool cleanupLoadedInstance(uint8_t instanceId,
119 uint8_t activeVariant,
120 uint8_t loadedVariant);
121 bool slotExists(uint8_t instanceId) const;
122 void makeActKey(uint8_t instanceId, char *output) const;
123 void makeHeaderKey(uint8_t instanceId, uint8_t variant, char *output) const;
124 void makeChannelMapKey(uint8_t instanceId,
125 uint8_t variant,
126 char *output) const;
127 void makeConfigKey(uint8_t instanceId, uint8_t variant, char *output) const;
128 bool readBlobExact(const char *key, char *output, size_t expectedSize) const;
129
130 Supla::Config *config = nullptr;
131};
132
133} // namespace Suplet
134} // namespace Supla
135
136#endif // SRC_SUPLA_SUPLET_STORAGE_H_
Definition config.h:59
Definition channel_map.h:40
Definition storage.h:51
Definition storage.h:33