Pavona Software APIs
ujson_derive.h
1// Copyright lowRISC contributors (OpenTitan project).
2// Copyright zeroRISC Inc.
3// Licensed under the Apache License, Version 2.0, see LICENSE for details.
4// SPDX-License-Identifier: Apache-2.0
5
6#ifndef OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_DERIVE_H_
7#define OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_DERIVE_H_
8// This is what we'll use as the Rust enumeration name for C-enum values
9// that do not have a symbolic name.
10#define RUST_ENUM_INTVALUE IntValue
11
12#ifndef RUST_PREPROCESSOR_EMIT
13#include <stdint.h>
14
15#include "sw/device/lib/base/adv_macros.h"
16#include "sw/device/lib/base/status.h"
17#include "sw/device/lib/ujson/ujson.h"
18
19// If there is a pre-existing Rust `with_unknown! { ... }` enum, you can
20// add this flag to UJSON_SERDE_ENUM(...) to cause the C-based serializer
21// and deserializer to emit/parse the correct format.
22#define WITH_UNKNOWN 1
23#define RUST_ENUM_INTVALUE_STR OT_STRINGIFY(RUST_ENUM_INTVALUE)
24
25// clang-format off
26// clang-format is turned off; as scary as these macros look, they look
27// even scarier after clang-format is done with them.
28#define ujson_get_flags(...) \
29 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
30 ( /*then*/ \
31 0 \
32 , /*else*/ \
33 __VA_ARGS__ \
34 ) /*endif*/
35
36#define ujson_struct_field_array_indirect() ujson_struct_field_array
37#define ujson_struct_field_array(nt_, sz_, ...) \
38 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
39 ( /*then*/ \
40 nt_[sz_] \
41 , /*else*/ \
42 OT_OBSTRUCT(ujson_struct_field_array_indirect)()(nt_[sz_], __VA_ARGS__) \
43 ) /*endif*/
44
45#define ujson_struct_field(name_, type_, ...) \
46 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
47 ( /*then*/ \
48 type_ name_; \
49 , /*else*/ \
50 OT_EVAL(ujson_struct_field_array(type_ name_, __VA_ARGS__)); \
51 ) /*endif*/
52
53#define ujson_struct_string(name_, size_, ...) \
54 ujson_struct_field(name_, char, ##__VA_ARGS__, size_)
55
56#define ujson_struct_bytes(name_, size_, len_) \
57 ujson_struct_field(name_, uint8_t, size_)
58
59#define UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ...) \
60 typedef struct formal_name_ { \
61 decl_(ujson_struct_field, ujson_struct_string, ujson_struct_bytes) \
62 } name_
63
64#define ujson_enum_value(formal_name_, name_, ...) \
65 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
66 ( /*then*/ \
67 k ##formal_name_ ## name_ \
68 , /*else*/ \
69 k ##formal_name_ ## name_ = __VA_ARGS__ \
70 ) /*endif*/ ,
71
72#define UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ...) \
73 typedef enum formal_name_ { \
74 decl_(formal_name_, ujson_enum_value) \
75 } name_
76
77
78// Helper to count number of fields.
79#define ujson_count(name_, type_, ...) +1
80
81//////////////////////////////////////////////////////////////////////
82// Serialize Implementation
83//////////////////////////////////////////////////////////////////////
84#define ujson_ser_loop_indirect() ujson_ser_loop
85#define ujson_ser_loop(expr, count, ...) \
86 TRY(ujson_putbuf(uj, "[", 1)); \
87 for(size_t x=0; x < count; ++x) { \
88 if (x) TRY(ujson_putbuf(uj, ",", 1)); \
89 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
90 ( /*then*/ \
91 expr; \
92 , /*else*/ \
93 OT_OBSTRUCT(ujson_ser_loop_indirect)()(expr, __VA_ARGS__) \
94 ) /*endif*/ \
95 } \
96 TRY(ujson_putbuf(uj, "]", 1));
97
98#define ujson_ser_field(name_, type_, ...) { \
99 TRY(ujson_serialize_string(uj, #name_)); \
100 TRY(ujson_putbuf(uj, ":", 1)); \
101 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
102 ( /*then*/ \
103 TRY(ujson_serialize_##type_(uj, &self->name_)); \
104 , /*else*/ \
105 const type_ *p = (const type_*)self->name_; \
106 OT_EVAL(ujson_ser_loop( \
107 TRY(ujson_serialize_##type_(uj, p++)), __VA_ARGS__)) \
108 ) /*endif*/ \
109 if (--nfield) TRY(ujson_putbuf(uj, ",", 1)); \
110 }
111
112#define ujson_ser_string(name_, size_, ...) { \
113 TRY(ujson_serialize_string(uj, #name_)); \
114 TRY(ujson_putbuf(uj, ":", 1)); \
115 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
116 ( /*then*/ \
117 TRY(ujson_serialize_string(uj, self->name_)); \
118 , /*else*/ \
119 const char *p = (const char*)self->name_; \
120 OT_EVAL(ujson_ser_loop( \
121 TRY(ujson_serialize_string(uj, p)); p+=size_, __VA_ARGS__)) \
122 ) /*endif*/ \
123 if (--nfield) TRY(ujson_putbuf(uj, ",", 1)); \
124 }
125
126#define ujson_ser_bytes(name_, size_, len_) { \
127 TRY(ujson_serialize_string(uj, #name_)); \
128 TRY(ujson_putbuf(uj, ":", 1)); \
129 size_t n = self->len_ < (size_t)size_ ? self->len_ : (size_t)size_; \
130 TRY(ujson_serialize_hex(uj, self->name_, n)); \
131 if (--nfield) TRY(ujson_putbuf(uj, ",", 1)); \
132 }
133
134#define UJSON_IMPL_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
135 status_t ujson_serialize_with_padding_##name_(ujson_t *uj, const name_ *self, size_t max_size) { \
136 size_t nfield = decl_(ujson_count, ujson_count, ujson_count); \
137 uj->str_size = 0; \
138 TRY(ujson_putbuf(uj, "{", 1)); \
139 decl_(ujson_ser_field, ujson_ser_string, ujson_ser_bytes) \
140 if (max_size > uj->str_size + 1) { \
141 for (size_t i = 0; i < max_size - uj->str_size - 1; i++) { \
142 TRY(ujson_putbuf(uj, " ", 1)); \
143 } \
144 } \
145 TRY(ujson_putbuf(uj, "}", 1)); \
146 return OK_STATUS(); \
147 } \
148 extern const int __never_referenced___here_to_eat_a_semicolon[]
149
150#define UJSON_IMPL_SERIALIZE_STRUCT(name_, decl_) \
151 status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) { \
152 size_t nfield = decl_(ujson_count, ujson_count, ujson_count); \
153 TRY(ujson_putbuf(uj, "{", 1)); \
154 decl_(ujson_ser_field, ujson_ser_string, ujson_ser_bytes) \
155 TRY(ujson_putbuf(uj, "}", 1)); \
156 return OK_STATUS(); \
157 } \
158 extern const int __never_referenced___here_to_eat_a_semicolon[]
159
160#define ujson_ser_enum(formal_name_, name_, ...) \
161 case k ##formal_name_ ## name_: \
162 TRY(ujson_serialize_string(uj, #name_)); break;
163
164#define UJSON_IMPL_SERIALIZE_ENUM(formal_name_, name_, decl_, ...) \
165 status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) { \
166 switch(*self) { \
167 decl_(formal_name_, ujson_ser_enum) \
168 default: { \
169 const uint32_t value = (uint32_t)(*self); \
170 if (ujson_get_flags(__VA_ARGS__) & WITH_UNKNOWN) { \
171 TRY(ujson_serialize_uint32_t(uj, &value)); \
172 } else { \
173 TRY(ujson_putbuf(uj, \
174 "{\"" RUST_ENUM_INTVALUE_STR "\":", \
175 sizeof("{\"" RUST_ENUM_INTVALUE_STR "\":") - 1)); \
176 TRY(ujson_serialize_uint32_t(uj, &value)); \
177 TRY(ujson_putbuf(uj, "}", 1)); \
178 } \
179 } \
180 } \
181 return OK_STATUS(); \
182 } \
183 extern const int __never_referenced___here_to_eat_a_semicolon[]
184
185//////////////////////////////////////////////////////////////////////
186// Deserialize Implementation
187//////////////////////////////////////////////////////////////////////
188#define ujson_de_loop_indirect() ujson_de_loop
189#define ujson_de_loop(mult, expr, count, ...) \
190 TRY(ujson_consume(uj, '[')); \
191 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
192 ( /*then*/ \
193 size_t i = 0; \
194 while(true) { \
195 if (TRY(ujson_consume_maybe(uj, ']'))) break; \
196 if (i) TRY(ujson_consume(uj, ',')); \
197 if (i < count) { expr; ++i; } \
198 } \
199 if (i < count) { p += (count - i) * mult; } \
200 , /*else*/ \
201 for(size_t x=0;; ++x) { \
202 if (TRY(ujson_consume_maybe(uj, ']'))) break; \
203 if (x) TRY(ujson_consume(uj, ',')); \
204 OT_OBSTRUCT(ujson_de_loop_indirect)()(mult, expr, __VA_ARGS__) \
205 } \
206 ) /*endif*/ \
207
208#define ujson_de_field(name_, type_, ...) \
209 else if (ujson_streq(key, #name_)) { \
210 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
211 ( /*then*/ \
212 TRY(ujson_deserialize_##type_(uj, &self->name_)); \
213 , /*else*/ \
214 type_ *p = (type_*)self->name_; \
215 OT_EVAL(ujson_de_loop(1, \
216 TRY(ujson_deserialize_##type_(uj, p++)), __VA_ARGS__)) \
217 ) /*endif*/ \
218 }
219
220#define ujson_de_string(name_, size_, ...) \
221 else if (ujson_streq(key, #name_)) { \
222 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
223 ( /*then*/ \
224 TRY(ujson_parse_qs(uj, self->name_, sizeof(self->name_))); \
225 , /*else*/ \
226 char *p = (char*)self->name_; \
227 OT_EVAL(ujson_de_loop(size_, \
228 TRY(ujson_parse_qs(uj, p, sizeof(self->name_))); p+=size_, __VA_ARGS__)) \
229 ) /*endif*/ \
230 }
231
232#define ujson_de_bytes(name_, size_, len_) \
233 else if (ujson_streq(key, #name_)) { \
234 TRY(ujson_deserialize_hex(uj, self->name_, size_, &self->len_)); \
235 }
236
237#define UJSON_IMPL_DESERIALIZE_STRUCT(name_, decl_) \
238 status_t ujson_deserialize_##name_(ujson_t *uj, name_ *self) { \
239 size_t nfield = 0; \
240 char key[128]; \
241 uj->str_size = 0; \
242 TRY(ujson_consume(uj, '{')); \
243 while(TRY(ujson_consume_maybe(uj, '}')) == 0) { \
244 if (nfield++ > 0) { \
245 TRY(ujson_consume(uj, ',')); \
246 } \
247 TRY(ujson_parse_qs(uj, key, sizeof(key))); \
248 TRY(ujson_consume(uj, ':')); \
249 if (0) {} \
250 decl_(ujson_de_field, ujson_de_string, ujson_de_bytes) \
251 else { \
252 return INVALID_ARGUMENT(); \
253 } \
254 } \
255 return OK_STATUS(); \
256 } \
257 extern const int __never_referenced___here_to_eat_a_semicolon[]
258
259#define ujson_de_enum(formal_name_, name_, ...) \
260 else if (ujson_streq(value, #name_)) { *self = k ##formal_name_ ## name_; }
261
262#define UJSON_IMPL_DESERIALIZE_ENUM(formal_name_, name_, decl_, ...) \
263 status_t ujson_deserialize_##name_(ujson_t *uj, name_ *self) { \
264 char value[128]; \
265 uj->str_size = 0; \
266 if (TRY(ujson_consume_maybe(uj, '"'))) { \
267 TRY(ujson_ungetc(uj, '"')); \
268 TRY(ujson_parse_qs(uj, value, sizeof(value))); \
269 if (0) {} \
270 decl_(formal_name_, ujson_de_enum) \
271 else { \
272 return INVALID_ARGUMENT(); \
273 } \
274 } else if(TRY(ujson_consume_maybe(uj, '{'))) { \
275 TRY(ujson_parse_qs(uj, value, sizeof(value))); \
276 TRY(ujson_consume(uj, ':')); \
277 if (ujson_streq(value, RUST_ENUM_INTVALUE_STR)) { \
278 TRY(ujson_deserialize_uint32_t(uj, (uint32_t*)self)); \
279 } else { \
280 return INVALID_ARGUMENT(); \
281 } \
282 TRY(ujson_consume(uj, '}')); \
283 } else { \
284 TRY(ujson_deserialize_uint32_t(uj, (uint32_t*)self)); \
285 } \
286 return OK_STATUS(); \
287 } \
288 extern const int __never_referenced___here_to_eat_a_semicolon[]
289
290#ifndef UJSON_SERDE_IMPL
291#define UJSON_SERDE_IMPL 0
292#endif
293
294#define UJSON_SERIALIZE_STRUCT(name_, decl_) \
295 OT_IIF(UJSON_SERDE_IMPL) \
296 ( /*then*/ \
297 UJSON_IMPL_SERIALIZE_STRUCT(name_, decl_) \
298 , /*else*/ \
299 status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) \
300 ) /*endif*/
301
302#define UJSON_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
303 OT_IIF(UJSON_SERDE_IMPL) \
304 ( /*then*/ \
305 UJSON_IMPL_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
306 , /*else*/ \
307 status_t ujson_serialize_with_padding_##name_(ujson_t *uj, const name_ *self, size_t max_size) \
308 ) /*endif*/
309
310#define UJSON_SERIALIZE_ENUM(formal_name_, name_, decl_, ...) \
311 OT_IIF(UJSON_SERDE_IMPL) \
312 ( /*then*/ \
313 UJSON_IMPL_SERIALIZE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__) \
314 , /*else*/ \
315 status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) \
316 ) /*endif*/
317
318#define UJSON_DESERIALIZE_STRUCT(name_, decl_) \
319 OT_IIF(UJSON_SERDE_IMPL) \
320 ( /*then*/ \
321 UJSON_IMPL_DESERIALIZE_STRUCT(name_, decl_) \
322 , /*else*/ \
323 status_t ujson_deserialize_##name_(ujson_t *uj, name_ *self) \
324 ) /*endif*/
325
326#define UJSON_DESERIALIZE_ENUM(formal_name_, name_, decl_, ...) \
327 OT_IIF(UJSON_SERDE_IMPL) \
328 ( /*then*/ \
329 UJSON_IMPL_DESERIALIZE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__) \
330 , /*else*/ \
331 status_t ujson_deserialize_##name_(ujson_t *uj, name_ *self) \
332 ) /*endif*/
333// clang-format on
334
335//////////////////////////////////////////////////////////////////////
336// Combined build-everything macros
337//////////////////////////////////////////////////////////////////////
338#define UJSON_SERDE_STRUCT(formal_name_, name_, decl_, ...) \
339 UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ##__VA_ARGS__); \
340 UJSON_SERIALIZE_STRUCT(name_, decl_); \
341 UJSON_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_); \
342 UJSON_DESERIALIZE_STRUCT(name_, decl_)
343
344#define UJSON_SERDE_ENUM(formal_name_, name_, decl_, ...) \
345 UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__); \
346 UJSON_SERIALIZE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__); \
347 UJSON_DESERIALIZE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__)
348
349#define C_ONLY(x) x
350#define RUST_ONLY(x) \
351 extern const int __never_referenced___here_to_eat_a_semicolon[]
352
353#else // RUST_PREPROCESSOR_EMIT
354#include "sw/device/lib/ujson/ujson_rust.h"
355#endif // RUST_PREPROCESSOR_EMIT
356#endif // OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_DERIVE_H_