Pavona Software APIs
ujson.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_H_
7#define OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_H_
8#include <stdint.h>
9
10#include "sw/device/lib/base/status.h"
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/**
16 * Input/Output context for ujson.
17 */
18typedef struct ujson {
19 /** A generic pointer holding context for the IO routines. */
21 /** A pointer to an IO function for writing data to the output. */
22 status_t (*putbuf)(void *, const char *, size_t);
23 /** A pointer to an IO function for flusing buffered data to the output. */
24 status_t (*flushbuf)(void *);
25 /** A pointer to an IO function for reading data from the input. */
26 status_t (*getc)(void *);
27 /** An internal single character buffer for ungetting a character. */
28 int16_t buffer;
29 /** Holds the rolling CRC32 of characters that are sent and received.*/
30 uint32_t crc32;
31 /** Holds size counter for strings sent or received.*/
32 size_t str_size;
33} ujson_t;
34
35// clang-format off
36#define UJSON_INIT(context_, getc_, putbuf_, flushbuf_) \
37 { \
38 .io_context = (void *)(context_), \
39 .putbuf_ = (putbuf_), \
40 .flushbuf_ = (flushbuf_), \
41 .getc = (getc_), \
42 .buffer = -1, \
43 .crc32 = UINT32_MAX, \
44 }
45// clang-format on
46
47/**
48 * Initializes and returns a ujson context.
49 *
50 * @param context An IO context for the `getc` and `putbuf` functions.
51 * @param getc A function to read a character from the input.
52 * @param putbuf A function to write a buffer to the output.
53 * @return An initialized ujson_t context.
54 */
55ujson_t ujson_init(void *context, status_t (*getc)(void *),
56 status_t (*putbuf)(void *, const char *, size_t),
57 status_t (*flushbuf)(void *));
58
59/**
60 * Gets a single character from the input.
61 *
62 * @param uj A ujson IO context.
63 * @return The next character or an error.
64 */
65status_t ujson_getc(ujson_t *uj);
66
67/**
68 * Pushes a single character back to the input.
69 *
70 * @param uj A ujson IO context.
71 * @param ch The character to put back to the input buffer.
72 * @return OK or an error.
73 */
74status_t ujson_ungetc(ujson_t *uj, char ch);
75
76/**
77 * Writes a buffer to the output.
78 *
79 * @param uj A ujson IO context.
80 * @param buf The buffer to write to the output.
81 * @param len The length of the buffer.
82 * @return OK or an error.
83 */
84status_t ujson_putbuf(ujson_t *uj, const char *buf, size_t len);
85
86/**
87 * Flush a UJSON buffer to the output.
88 *
89 * Some console implementations e.g. the SPI console stage data in a
90 * software buffer before flushing it out to the low-level printf driver, as the
91 * driver code uses a framing protocol that is more efficient if used with bulk
92 * data transfers as opposed to being used with single character writes.
93 *
94 * @param uj A ujson IO context.
95 * @return OK or an error.
96 */
97status_t ujson_flushbuf(ujson_t *uj);
98
99/**
100 * Resets the CRC32 calculation to an initial state.
101 *
102 * @param uj A ujson IO context.
103 */
104void ujson_crc32_reset(ujson_t *uj);
105
106/**
107 * Returns the finished value of a CRC32 calculation.
108 *
109 * Note the state is un-altered by this function.
110 * One must reset before starting a new calculation.
111 *
112 * @param uj A ujson IO context.
113 * @return The final value for the CRC32 calculation.
114 */
115uint32_t ujson_crc32_finish(ujson_t *uj);
116
117/**
118 * Compares two strings for equality.
119 *
120 * @param a A string.
121 * @param b A string.
122 * @return true of the strings are equal; false otherwise.
123 */
124bool ujson_streq(const char *a, const char *b);
125
126/**
127 * Consumes a character from the input.
128 *
129 * Consume all whitespace until a non-whitespace character is found.
130 * The non-whitespace character must be `ch`.
131 *
132 * @param uj A ujson IO context.
133 * @param ch The character to consume.
134 * @return OK or an error.
135 */
136status_t ujson_consume(ujson_t *uj, char ch);
137
138/**
139 * Find and consume a character from the input.
140 *
141 * Consume all whitespace until a non-whitespace character is found.
142 * If the character is `ch`, return OK(1).
143 * If the character is not `ch`, unget the character and return OK(0).
144 *
145 * @param uj A ujson IO context.
146 * @param ch The character to consume.
147 * @return OK or an error.
148 */
149status_t ujson_consume_maybe(ujson_t *uj, char ch);
150
151/**
152 * Parse a JSON quoted string.
153 *
154 * Consume whitespace until finding a double-quote. Consume all characters
155 * (obeying json escape sequences) until the next double-quote. If the
156 * input string exceeds the length of the user buffer, the user
157 * buffer will contain a truncated string and the entire input json string
158 * will be consumed.
159 *
160 * @param uj A ujson IO context.
161 * @param str A buffer to write the string into.
162 * @param len The length of the target buffer.
163 * @return OK or an error.
164 */
165status_t ujson_parse_qs(ujson_t *uj, char *str, size_t len);
166
167/**
168 * Parse a JSON quoted string of hex digit pairs into a byte buffer.
169 *
170 * @param uj A ujson IO context.
171 * @param buf A buffer to write the bytes into.
172 * @param max_len The length of the target buffer.
173 * @param[out] len Receives the number of bytes written.
174 * @return OK or an error.
175 */
176status_t ujson_deserialize_hex(ujson_t *uj, uint8_t *buf, size_t max_len,
177 size_t *len);
178
179/**
180 * Parse a JSON integer.
181 *
182 * @param uj A ujson IO context.
183 * @param result: The parsed integer.
184 * @param rsz: The size of the integer (in bytes).
185 * @return OK or an error.
186 */
187status_t ujson_parse_integer(ujson_t *uj, void *result, size_t rsz);
188
189/**
190 * The following functions parse integers of specific sizes.
191 */
192status_t ujson_deserialize_uint64_t(ujson_t *uj, uint64_t *value);
193status_t ujson_deserialize_uint32_t(ujson_t *uj, uint32_t *value);
194status_t ujson_deserialize_uint16_t(ujson_t *uj, uint16_t *value);
195status_t ujson_deserialize_uint8_t(ujson_t *uj, uint8_t *value);
196status_t ujson_deserialize_size_t(ujson_t *uj, size_t *value);
197status_t ujson_deserialize_int64_t(ujson_t *uj, int64_t *value);
198status_t ujson_deserialize_int32_t(ujson_t *uj, int32_t *value);
199status_t ujson_deserialize_int16_t(ujson_t *uj, int16_t *value);
200status_t ujson_deserialize_int8_t(ujson_t *uj, int8_t *value);
201
202/**
203 * Deserialize a boolean.
204 *
205 * @param uj A ujson IO context.
206 * @param value Pointer to the value to deserialize into.
207 * @return OK or an error.
208 */
209status_t ujson_deserialize_bool(ujson_t *uj, bool *value);
210
211/**
212 * Deserialize a status_t.
213 *
214 * @param uj A ujson IO context.
215 * @param value Pointer to the value to deserialize into.
216 * @return OK or an error.
217 */
218status_t ujson_deserialize_status_t(ujson_t *uj, status_t *value);
219
220/**
221 * Serialize a string.
222 *
223 * @param uj A ujson IO context.
224 * @param buf The string to serialize.
225 * @return OK or an error.
226 */
227status_t ujson_serialize_string(ujson_t *uj, const char *buf);
228
229/**
230 * Serialize a byte buffer as a JSON quoted string of hex digit pairs.
231 *
232 * @param uj A ujson IO context.
233 * @param buf The bytes to serialize.
234 * @param len The number of bytes to serialize.
235 * @return OK or an error.
236 */
237status_t ujson_serialize_hex(ujson_t *uj, const uint8_t *buf, size_t len);
238
239/**
240 * Serialize an integer.
241 *
242 * @param uj A ujson IO context.
243 * @param value A pointer to the integer to serialize.
244 * @return OK or an error.
245 */
246status_t ujson_serialize_uint64_t(ujson_t *uj, const uint64_t *value);
247status_t ujson_serialize_uint32_t(ujson_t *uj, const uint32_t *value);
248status_t ujson_serialize_uint16_t(ujson_t *uj, const uint16_t *value);
249status_t ujson_serialize_uint8_t(ujson_t *uj, const uint8_t *value);
250status_t ujson_serialize_size_t(ujson_t *uj, const size_t *value);
251status_t ujson_serialize_int64_t(ujson_t *uj, const int64_t *value);
252status_t ujson_serialize_int32_t(ujson_t *uj, const int32_t *value);
253status_t ujson_serialize_int16_t(ujson_t *uj, const int16_t *value);
254status_t ujson_serialize_int8_t(ujson_t *uj, const int8_t *value);
255
256/**
257 * Serialize a boolean.
258 *
259 * @param uj A ujson IO context.
260 * @param value Pointer to the value to serialize.
261 * @return OK or an error.
262 */
263status_t ujson_serialize_bool(ujson_t *uj, const bool *value);
264
265/**
266 * Serialize a status_t.
267 *
268 * @param uj A ujson IO context.
269 * @param value Pointer to the value to serialize.
270 * @return OK or an error.
271 */
272status_t ujson_serialize_status_t(ujson_t *uj, const status_t *value);
273
274#ifdef __cplusplus
275}
276#endif
277#endif // OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_H_