forked from ag-prn/ArduinoOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationStore.cpp
More file actions
208 lines (168 loc) · 5.68 KB
/
Copy pathOperationStore.cpp
File metadata and controls
208 lines (168 loc) · 5.68 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2022
// MIT License
#include <ArduinoOcpp/Core/OperationStore.h>
#include <ArduinoOcpp/Core/FilesystemAdapter.h>
#include <ArduinoOcpp/Core/FilesystemUtils.h>
#include <ArduinoOcpp/Core/Configuration.h>
#include <ArduinoOcpp/Debug.h>
#ifndef AO_OPSTORE_DIR
#define AO_OPSTORE_DIR AO_FILENAME_PREFIX "/"
#endif
#define AO_OPSTORE_FN AO_FILENAME_PREFIX "/opstore.cnf"
#define AO_OPHISTORY_SIZE 3
using namespace ArduinoOcpp;
bool StoredOperationHandler::commit() {
if (isPersistent) {
AO_DBG_ERR("cannot call two times");
return false;
}
if (!filesystem) {
AO_DBG_DEBUG("filesystem");
return false;
}
if (!rpc || !payload) {
AO_DBG_ERR("unitialized");
return false;
}
opNr = context.reserveOpNr();
char fn [MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MAX_PATH_SIZE, AO_OPSTORE_DIR "op" "-%u.jsn", opNr);
if (ret < 0 || ret >= MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", ret);
return false;
}
DynamicJsonDocument doc {JSON_OBJECT_SIZE(2) + rpc->capacity() + payload->capacity()};
doc["rpc"] = *rpc;
doc["payload"] = *payload;
if (!FilesystemUtils::storeJson(filesystem, fn, doc)) {
AO_DBG_ERR("FS error");
return false;
}
isPersistent = true;
return true;
}
bool StoredOperationHandler::restore(unsigned int opNrToLoad) {
if (isPersistent) {
AO_DBG_ERR("cannot restore after commit");
return false;
}
if (!filesystem) {
AO_DBG_DEBUG("filesystem");
return false;
}
opNr = opNrToLoad;
char fn [MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MAX_PATH_SIZE, AO_OPSTORE_DIR "op" "-%u.jsn", opNr);
if (ret < 0 || ret >= MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", ret);
return false;
}
size_t msize;
if (filesystem->stat(fn, &msize) != 0) {
AO_DBG_VERBOSE("operation %u does not exist", opNr);
return false;
}
auto doc = FilesystemUtils::loadJson(filesystem, fn);
if (!doc) {
AO_DBG_ERR("FS error");
return false;
}
JsonVariant rpc_restore = (*doc)["rpc"];
JsonVariant payload_restore = (*doc)["payload"];
rpc = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(rpc_restore.memoryUsage()));
payload = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(payload_restore.memoryUsage()));
*rpc = rpc_restore;
*payload = payload_restore;
isPersistent = true;
return true;
}
OperationStore::OperationStore(std::shared_ptr<FilesystemAdapter> filesystem) : filesystem(filesystem) {
opBegin = declareConfiguration<int>("AO_opBegin", 0, AO_OPSTORE_FN, false, false, true, false);
if (!opBegin || *opBegin < 0) {
AO_DBG_ERR("init failure");
} else if (filesystem) {
opEnd = *opBegin;
unsigned int misses = 0;
unsigned int i = opEnd;
while (misses < 3) {
char fn [MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MAX_PATH_SIZE, AO_OPSTORE_DIR "op" "-%u.jsn", i);
if (ret < 0 || ret >= MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", ret);
misses++;
i = (i + 1) % AO_MAX_OPNR;
continue;
}
size_t msize;
if (filesystem->stat(fn, &msize) != 0) {
AO_DBG_DEBUG("operation %u does not exist", i);
misses++;
i = (i + 1) % AO_MAX_OPNR;
continue;
}
//file exists
misses = 0;
i = (i + 1) % AO_MAX_OPNR;
opEnd = i;
}
}
}
std::unique_ptr<StoredOperationHandler> OperationStore::makeOpHandler() {
return std::unique_ptr<StoredOperationHandler>(new StoredOperationHandler(*this, filesystem));
}
unsigned int OperationStore::reserveOpNr() {
AO_DBG_DEBUG("reserved opNr %u", opEnd);
auto res = opEnd;
opEnd++;
opEnd %= AO_MAX_OPNR;
return res;
}
void OperationStore::advanceOpNr(unsigned int oldOpNr) {
if (!opBegin || *opBegin < 0) {
AO_DBG_ERR("init failure");
return;
}
if (oldOpNr != (unsigned int) *opBegin) {
if ((oldOpNr + AO_MAX_OPNR - (unsigned int) *opBegin) % AO_MAX_OPNR < 100) {
AO_DBG_ERR("synchronization failure - try to fix");
(void)0;
} else {
AO_DBG_ERR("synchronization failure");
return;
}
}
unsigned int opNr = (oldOpNr + 1) % AO_MAX_OPNR;
//delete range [*opBegin ... opNr)
unsigned int rangeSize = (opNr + AO_MAX_OPNR - (unsigned int) *opBegin) % AO_MAX_OPNR;
AO_DBG_DEBUG("delete %u operations", rangeSize);
for (unsigned int i = 0; i < rangeSize; i++) {
unsigned int op = ((unsigned int) *opBegin + i + AO_MAX_OPNR - AO_OPHISTORY_SIZE) % AO_MAX_OPNR;
char fn [MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MAX_PATH_SIZE, AO_OPSTORE_DIR "op" "-%u.jsn", op);
if (ret < 0 || ret >= MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", ret);
break;
}
size_t msize;
if (filesystem->stat(fn, &msize) != 0) {
AO_DBG_DEBUG("operation %u does not exist", i);
continue;
}
bool success = filesystem->remove(fn);
if (!success) {
AO_DBG_ERR("error deleting %s", fn);
(void)0;
}
}
AO_DBG_DEBUG("advance opBegin: %u", opNr);
*opBegin = opNr;
configuration_save();
}
unsigned int OperationStore::getOpBegin() {
if (!opBegin || *opBegin < 0) {
AO_DBG_ERR("invalid state");
return 0;
}
return (unsigned int) *opBegin;
}