Software APIs
example.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_UJSON_EXAMPLE_H_
6 #define OPENTITAN_SW_DEVICE_LIB_UJSON_EXAMPLE_H_
7 #include "sw/device/lib/ujson/ujson_derive.h"
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #define MODULE_ID MAKE_MODULE_ID('e', 'x', 'j')
13 
14 // clang-format off
15 /////////////////////////////////////////////////////////////////////////////
16 // Automatic generation of structs with serialize/deserialize functions:
17 //
18 // The follow creates a `struct Foo`:
19 // typedef struct Foo {
20 // int32_t foo;
21 // uint32_t bar;
22 // char message[20];
23 // } foo;
24 // status_t ujson_serialize_foo(ujson_t *context, const foo *self);
25 // status_t ujson_deserialize_foo(ujson_t *context, foo *self);
26 #define STRUCT_FOO(field, string) \
27  field(foo, int32_t) \
28  field(bar, uint32_t) \
29  string(message, 20)
30 UJSON_SERDE_STRUCT(Foo, foo, STRUCT_FOO);
31 
32 // The next two structs demonstrate struct nesting:
33 // typedef struct Coord { int32_t x; int32_t y; } coord;
34 //
35 // typedef struct Rect {
36 // coord top_left;
37 // coord bottom_right;
38 // } rect;
39 // status_t ujson_serialize_rect(ujson_t *context, const rect *self);
40 // status_t ujson_deserialize_rect(ujson_t *context, rect *self);
41 // (and yes: `coord` has serialize and deserialize functions as well).
42 #define STRUCT_COORD(field, string) \
43  field(x, int32_t) \
44  field(y, int32_t)
45 UJSON_SERDE_STRUCT(Coord, coord, STRUCT_COORD);
46 
47 #define STRUCT_RECT(field, string) \
48  field(top_left, coord) \
49  field(bottom_right, coord)
50 UJSON_SERDE_STRUCT(Rect, rect, STRUCT_RECT);
51 
52 // The next example demonstrates arrays within struct fields:
53 // struct Matrix {
54 // int32_t k[3][5];
55 // } matrix;
56 // (and serialize/deserialize functions).
57 //
58 // Arrays may have arbitrary dimension. However, ujson has no concept
59 // of a variable sized array:
60 // - Serialize will always emit _all_ elements.
61 // - Deserialize will _not_ initialize absent elements.
62 #define STRUCT_MATRIX(field, string) \
63  field(k, int32_t, 3, 5)
64 UJSON_SERDE_STRUCT(Matrix, matrix, STRUCT_MATRIX);
65 
66 /////////////////////////////////////////////////////////////////////////////
67 // Automatic generation of enums with serialize/deserialize functions:
68 //
69 // The following creates an `enum Direction`:
70 // typedef enum Direction {
71 // kDirectionNorth,
72 // kDirectionEast,
73 // kDirectionSouth,
74 // kDirectionWest,
75 // } direction;
76 // status_t ujson_serialize_direction(ujson_t *context, const direction *self);
77 // status_t ujson_deserialize_direction(ujson_t *context, direction *self);
78 #define ENUM_DIRECTION(_, value) \
79  value(_, North) \
80  value(_, East) \
81  value(_, South) \
82  value(_, West)
83 UJSON_SERDE_ENUM(Direction, direction, ENUM_DIRECTION);
84 
85 /////////////////////////////////////////////////////////////////////////////
86 // Automatic generation of C enums corresponding to rust `with_unknown!` enums
87 //
88 // The following creates an `enum FuzzyBool`:
89 // typedef enum FuzzyBool {
90 // kFuzzyBoolFalse = 0,
91 // FuzzyBoolTrue = 100,
92 // } fuzzy_bool;
93 // status_t ujson_serialize_fuzzy_bool(ujson_t *context, const fuzzy_bool *self);
94 // status_t ujson_deserialize_fuzzy_bool(ujson_t *context, fuzzy_bool *self);
95 #define ENUM_FUZZY_BOOL(_, value) \
96  value(_, False, 0) \
97  value(_, True, 100)
98 C_ONLY(UJSON_SERDE_ENUM(FuzzyBool, fuzzy_bool, ENUM_FUZZY_BOOL, WITH_UNKNOWN));
99 
100 /////////////////////////////////////////////////////////////////////////////
101 // Other miscellaneous supported types: bool and status_t
102 //
103 // status_t ujson_serialize_misc_t(ujson_t *context, const misc_t *self);
104 // status_t ujson_deserialize_misc_t(ujson_t *context, misc_t *self);
105 #define STRUCT_MISC(field, string) \
106  field(value, bool) \
107  field(status, status_t)
108 UJSON_SERDE_STRUCT(Misc, misc_t, STRUCT_MISC);
109 
110 #undef MODULE_ID
111 
112 // clang-format on
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 #endif // OPENTITAN_SW_DEVICE_LIB_UJSON_EXAMPLE_H_