forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetConfiguration.cpp
More file actions
150 lines (124 loc) · 4.77 KB
/
GetConfiguration.cpp
File metadata and controls
150 lines (124 loc) · 4.77 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Operations/GetConfiguration.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Debug.h>
using MicroOcpp::Ocpp16::GetConfiguration;
using MicroOcpp::JsonDoc;
GetConfiguration::GetConfiguration() : MemoryManaged("v16.Operation.", "GetConfiguration"), keys{makeVector<String>(getMemoryTag())} {
}
const char* GetConfiguration::getOperationType(){
return "GetConfiguration";
}
void GetConfiguration::processReq(JsonObject payload) {
JsonArray requestedKeys = payload["key"];
for (size_t i = 0; i < requestedKeys.size(); i++) {
keys.push_back(makeString(getMemoryTag(), requestedKeys[i].as<const char*>()));
}
}
std::unique_ptr<JsonDoc> GetConfiguration::createConf(){
Vector<Configuration*> configurations = makeVector<Configuration*>(getMemoryTag());
Vector<const char*> unknownKeys = makeVector<const char*>(getMemoryTag());
auto containers = getConfigurationContainersPublic();
if (keys.empty()) {
//return all existing keys
for (auto container : containers) {
for (size_t i = 0; i < container->size(); i++) {
if (!container->getConfiguration(i)->getKey()) {
MO_DBG_ERR("invalid config");
continue;
}
if (!container->getConfiguration(i)->isReadable()) {
continue;
}
configurations.push_back(container->getConfiguration(i));
}
}
} else {
//only return keys that were searched using the "key" parameter
for (auto& key : keys) {
Configuration *res = nullptr;
for (auto container : containers) {
if ((res = container->getConfiguration(key.c_str()).get())) {
break;
}
}
if (res && res->isReadable()) {
configurations.push_back(res);
} else {
unknownKeys.push_back(key.c_str());
}
}
}
#define VALUE_BUFSIZE 30
//capacity of the resulting document
size_t jcapacity = JSON_OBJECT_SIZE(2); //document root: configurationKey, unknownKey
jcapacity += JSON_ARRAY_SIZE(configurations.size()) + configurations.size() * JSON_OBJECT_SIZE(3); //configurationKey: [{"key":...},{"key":...}]
for (auto config : configurations) {
//need to store ints by copied string: measure necessary capacity
if (config->getType() == TConfig::Int) {
char vbuf [VALUE_BUFSIZE];
auto ret = snprintf(vbuf, VALUE_BUFSIZE, "%i", config->getInt());
if (ret < 0 || ret >= VALUE_BUFSIZE) {
continue;
}
jcapacity += ret + 1;
}
}
jcapacity += JSON_ARRAY_SIZE(unknownKeys.size());
MO_DBG_DEBUG("GetConfiguration capacity: %zu", jcapacity);
std::unique_ptr<JsonDoc> doc;
if (jcapacity <= MO_MAX_JSON_CAPACITY) {
doc = makeJsonDoc(getMemoryTag(), jcapacity);
}
if (!doc || doc->capacity() < jcapacity) {
if (doc) {
MO_DBG_ERR("OOM");
}
errorCode = "InternalError";
errorDescription = "Query too big. Try fewer keys";
return nullptr;
}
JsonObject payload = doc->to<JsonObject>();
JsonArray jsonConfigurationKey = payload.createNestedArray("configurationKey");
for (auto config : configurations) {
char vbuf [VALUE_BUFSIZE];
const char *v = "";
switch (config->getType()) {
case TConfig::Int: {
auto ret = snprintf(vbuf, VALUE_BUFSIZE, "%i", config->getInt());
if (ret < 0 || ret >= VALUE_BUFSIZE) {
MO_DBG_ERR("value error");
continue;
}
v = vbuf;
break;
}
case TConfig::Bool:
v = config->getBool() ? "true" : "false";
break;
case TConfig::String:
v = config->getString();
break;
}
JsonObject jconfig = jsonConfigurationKey.createNestedObject();
jconfig["key"] = config->getKey();
jconfig["readonly"] = config->isReadOnly();
if (v == vbuf) {
//value points to buffer on stack, needs to be copied into JSON memory pool
jconfig["value"] = (char*) v;
} else {
//value is static, no-copy mode
jconfig["value"] = v;
}
}
if (!unknownKeys.empty()) {
JsonArray jsonUnknownKey = payload.createNestedArray("unknownKey");
for (auto key : unknownKeys) {
MO_DBG_DEBUG("Unknown key: %s", key);
jsonUnknownKey.add(key);
}
}
return doc;
}