-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedded.cpp
More file actions
312 lines (261 loc) · 10.7 KB
/
Copy pathembedded.cpp
File metadata and controls
312 lines (261 loc) · 10.7 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Embedded Systems Example: JSON/CBOR Roundtrip Verification
//
// Demonstrates:
// - Fixed-size models (std::array / std::optional) — no dynamic containers in the schema
// - Validation constraints (range, min_items/max_items, required fields)
// - JSON roundtrip (parse -> serialize -> parse)
// - JSON -> CBOR -> model roundtrip (parse JSON -> serialize CBOR -> parse CBOR)
//
// Notes:
// - JSON inputs are string literals for readability; real embedded input is typically a byte stream.
// - Output buffers are fixed-size and explicitly NUL-terminated for convenience.
//
#include <JsonFusion/parser.hpp>
#include <JsonFusion/serializer.hpp>
#include <JsonFusion/validators.hpp>
#include <JsonFusion/cbor.hpp>
#include <array>
#include <optional>
#include <cstdint>
using namespace JsonFusion;
using namespace JsonFusion::options;
using namespace JsonFusion::validators;
// ============================================================================
// Models
// ============================================================================
struct EmbeddedConfig_ {
std::array<char, 32> device_name{};
std::uint16_t version_major{};
std::uint16_t version_minor{};
struct Network {
std::array<char, 16> name{};
std::array<char, 24> address{};
A<std::uint16_t, range<1024, 65535>> port{2000};
bool enabled{false};
};
// With empty not_required list, all fields are required
A<Network, not_required<>> network{};
std::optional<Network> fallback_network{};
struct Motor {
std::uint8_t id{0};
std::array<char, 16> name{};
// Fixed-size position vector: [x,y,z], each component range-validated.
std::array<A<double, range<-1000, 1000>>, 3> position{};
bool active{false};
};
// Fixed-size motor table, but still show min/max constraints API.
A<std::array<Motor, 4>, min_items<1>, max_items<4>> motors{};
};
// "Required" fields validator
// - device_name must be present
// - network must be present
using EmbeddedConfig = A<EmbeddedConfig_, required<"device_name", "network">>;
struct RpcCommand_ {
std::array<char, 16> command_id{};
std::uint64_t timestamp_us{0};
A<std::uint8_t, range<0, 10>> priority{0};
struct Target {
std::array<char, 16> device_id{};
};
A<std::array<Target, 4>, min_items<1>, max_items<4>> targets{};
struct Parameter {
std::array<char, 16> key{};
std::optional<std::int64_t> int_value{};
// Optional float with range constraint when present.
A<std::optional<double>, range<-1000000, 1000000>> float_value{};
};
A<std::array<Parameter, 8>, min_items<1>, max_items<8>> params{};
};
// At least command_id must be present.
using RpcCommand = A<RpcCommand_, required<"command_id">>;
// ============================================================================
// Forward declarations (keep checks below the demo)
// ============================================================================
constexpr bool verify_config_initial_parse(const EmbeddedConfig_& config);
constexpr bool verify_config_roundtrip(const EmbeddedConfig_& c1, const EmbeddedConfig_& c2);
constexpr bool verify_rpc_initial_parse(const RpcCommand_& cmd);
constexpr bool verify_rpc_roundtrip(const RpcCommand_& cmd1, const RpcCommand_& cmd2);
template<std::size_t N>
constexpr bool str_eq(const std::array<char, N>& arr, const char* s);
// ============================================================================
// Demo / tests
// ============================================================================
constexpr bool test_json_roundtrip_config() {
// Use a literal array for maximal constexpr portability.
constexpr const char json_input[] = R"({
"device_name": "Controller-01",
"version_major": 1,
"version_minor": 2,
"network": {
"name": "eth0",
"address": "192.168.1.100",
"port": 8080,
"enabled": true
},
"fallback_network": {
"name": "wlan0",
"address": "192.168.2.100",
"port": 8081,
"enabled": false
},
"motors": [
{
"id": 1,
"name": "Motor-A",
"position": [10.5, 20.3, -5.7],
"active": true
},
{
"id": 2,
"name": "Motor-B",
"position": [-15.2, 30.1, 8.9],
"active": false
}
]
})";
// Parse JSON
EmbeddedConfig config1{};
if (!Parse(config1, json_input)) return false;
if (!verify_config_initial_parse(config1)) return false;
// Serialize back to JSON (leave 1 byte for '\0')
std::array<char, 512> json_output{};
auto res = Serialize(config1, json_output.data(), json_output.data() + (json_output.size() - 1));
if (!res) return false;
const std::size_t n = res.bytesWritten();
if (n >= json_output.size()) return false; // paranoia guard
json_output[n] = '\0';
// Parse serialized JSON (roundtrip verification)
EmbeddedConfig config2{};
if (!Parse(config2, json_output.data())) return false;
if (!verify_config_roundtrip(config1, config2)) return false;
return true;
}
constexpr bool test_json_to_cbor_roundtrip_rpc() {
constexpr const char json_input[] = R"({
"command_id": "SET_MOTOR",
"timestamp_us": 1234567890,
"priority": 5,
"targets": [
{"device_id": "MOTOR-01"},
{"device_id": "MOTOR-02"}
],
"params": [
{"key": "speed", "int_value": 1500},
{"key": "position", "float_value": 45.5}
]
})";
// Parse from JSON
RpcCommand cmd1{};
if (!Parse(cmd1, json_input)) return false;
if (!verify_rpc_initial_parse(cmd1)) return false;
// Serialize to CBOR (fixed buffer)
std::array<std::uint8_t, 512> cbor_buffer{};
auto serialize_result = SerializeWithWriter(cmd1, CborWriter(cbor_buffer.data(), cbor_buffer.data() + cbor_buffer.size()));
if (!serialize_result) return false;
// Parse from CBOR
RpcCommand cmd2{};
if (!ParseWithReader(cmd2, CborReader(cbor_buffer.data(), cbor_buffer.data() + serialize_result.bytesWritten()))) return false;
if (!verify_rpc_roundtrip(cmd1, cmd2)) return false;
return true;
}
constexpr bool test_validation_fail_port_range() {
// Invalid: port 100 < 1024 (below minimum)
constexpr const char invalid_json[] = R"({
"device_name": "Test",
"version_major": 1,
"version_minor": 0,
"network": {
"name": "eth0",
"address": "192.168.1.1",
"port": 100,
"enabled": true
},
"motors": [
{"id": 1, "name": "M1", "position": [0, 0, 0], "active": true}
]
})";
EmbeddedConfig config{};
return !Parse(config, invalid_json);
}
constexpr bool test_validation_fail_motor_position_range() {
// Invalid: position[2] = 2000 > 1000 (above maximum)
constexpr const char invalid_json[] = R"({
"device_name": "Test",
"version_major": 1,
"version_minor": 0,
"network": {
"name": "eth0",
"address": "192.168.1.1",
"port": 8080,
"enabled": true
},
"motors": [
{"id": 1, "name": "M1", "position": [0, 0, 2000], "active": true}
]
})";
EmbeddedConfig config{};
return !Parse(config, invalid_json);
}
// ============================================================================
// Checks / helpers (kept below the main demo)
// ============================================================================
// Compare fixed-size char arrays against a NUL-terminated string.
// Never reads past the end of `s`.
template<std::size_t N>
constexpr bool str_eq(const std::array<char, N>& arr, const char* s) {
for (std::size_t i = 0;; ++i) {
if (i == N) {
return s[i] == '\0';
}
if (arr[i] != s[i]) return false;
if (s[i] == '\0') return true;
}
}
constexpr bool verify_config_initial_parse(const EmbeddedConfig_& config) {
if (!str_eq(config.device_name, "Controller-01")) return false;
if (config.version_major != 1) return false;
if (config.version_minor != 2) return false;
if (!str_eq(config.network->name, "eth0")) return false;
if (!str_eq(config.network->address, "192.168.1.100")) return false;
if (config.network->port != 8080) return false;
if (!config.network->enabled) return false;
if (!config.fallback_network.has_value()) return false;
if (!str_eq(config.fallback_network->name, "wlan0")) return false;
if (config.fallback_network->port != 8081) return false;
if (config.motors[0].id != 1) return false;
if (!str_eq(config.motors[0].name, "Motor-A")) return false;
if (config.motors[0].position[0] != 10.5) return false;
if (config.motors[1].active) return false;
return true;
}
constexpr bool verify_config_roundtrip(const EmbeddedConfig_& c1, const EmbeddedConfig_& c2) {
if (c2.version_major != c1.version_major) return false;
if (c2.version_minor != c1.version_minor) return false;
if (c2.network->port != c1.network->port) return false;
if (c2.motors[0].position[2] != c1.motors[0].position[2]) return false;
return true;
}
constexpr bool verify_rpc_initial_parse(const RpcCommand_& cmd) {
if (!str_eq(cmd.command_id, "SET_MOTOR")) return false;
if (cmd.timestamp_us != 1234567890ULL) return false;
if (cmd.priority != 5) return false;
if (!str_eq(cmd.targets[0].device_id, "MOTOR-01")) return false;
if (!str_eq(cmd.targets[1].device_id, "MOTOR-02")) return false;
if (!str_eq(cmd.params[0].key, "speed")) return false;
if (!cmd.params[0].int_value.has_value() || *cmd.params[0].int_value != 1500) return false;
if (!str_eq(cmd.params[1].key, "position")) return false;
if (!cmd.params[1].float_value->has_value() || cmd.params[1].float_value != 45.5) return false;
return true;
}
constexpr bool verify_rpc_roundtrip(const RpcCommand_& cmd1, const RpcCommand_& cmd2) {
if (cmd2.timestamp_us != cmd1.timestamp_us) return false;
if (cmd2.priority != cmd1.priority) return false;
if (cmd2.params[0].int_value != cmd1.params[0].int_value) return false;
if (cmd2.params[1].float_value != cmd1.params[1].float_value) return false;
return true;
}
// Run all tests at compile time
static_assert(test_json_roundtrip_config(), "JSON roundtrip test failed");
static_assert(test_json_to_cbor_roundtrip_rpc(), "JSON->CBOR->parse roundtrip test failed");
static_assert(test_validation_fail_port_range(), "Expected port-range validation failure");
static_assert(test_validation_fail_motor_position_range(), "Expected motor position validation failure");