Skip to content

Commit d242062

Browse files
timothytrippelpamaury
authored andcommitted
[ujson] add functions to pad device->host strings
This adds UJSON serialization functions to enable transmitting fixed size UJSON strings from the device --> host. This will be useful for provisioning flows in ATE environments. Signed-off-by: Tim Trippel <[email protected]> (cherry picked from commit 3b354ff)
1 parent c3b45a9 commit d242062

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

sw/device/lib/testing/test_framework/ujson_ottf.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ ujson_t ujson_ottf_console(void);
108108
OK_STATUS(); \
109109
})
110110

111+
/**
112+
* Respond with an OK result, JSON encoded data padded to specific size, and
113+
* empty CRC.
114+
*
115+
* @param responder_ A ujson serializer function for `data_`.
116+
* @param uj_ctx_ A `ujson_t` representing the IO context.
117+
* @param data_ A pointer to the data to send.
118+
* @param max_size_ Max size the payload should be padded to.
119+
*/
120+
#define RESP_OK_PADDED_NO_CRC(responder_, uj_ctx_, data_, max_size_) \
121+
({ \
122+
TRY(ujson_putbuf(uj_ctx_, "RESP_OK:", 8)); \
123+
TRY(responder_(uj_ctx_, data_, max_size_)); \
124+
RESP_NO_CRC(uj_ctx_); \
125+
OK_STATUS(); \
126+
})
127+
111128
/**
112129
* Respond with an OK result, JSON encoded data, and valid CRC.
113130
*

sw/device/lib/ujson/ujson.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void ujson_crc32_reset(ujson_t *uj) { crc32_init(&uj->crc32); }
2828
uint32_t ujson_crc32_finish(ujson_t *uj) { return crc32_finish(&uj->crc32); }
2929

3030
status_t ujson_putbuf(ujson_t *uj, const char *buf, size_t len) {
31+
uj->str_size += len;
3132
crc32_add(&uj->crc32, buf, len);
3233
return uj->putbuf(uj->io_context, buf, len);
3334
}

sw/device/lib/ujson/ujson_derive.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@
119119
if (--nfield) TRY(ujson_putbuf(uj, ",", 1)); \
120120
}
121121

122+
#define UJSON_IMPL_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
123+
status_t ujson_serialize_with_padding_##name_(ujson_t *uj, const name_ *self, size_t max_size) { \
124+
size_t nfield = decl_(ujson_count, ujson_count); \
125+
uj->str_size = 0; \
126+
TRY(ujson_putbuf(uj, "{", 1)); \
127+
decl_(ujson_ser_field, ujson_ser_string) \
128+
if (max_size > uj->str_size + 1) { \
129+
for (size_t i = 0; i < max_size - uj->str_size - 1; i++) { \
130+
TRY(ujson_putbuf(uj, " ", 1)); \
131+
} \
132+
} \
133+
TRY(ujson_putbuf(uj, "}", 1)); \
134+
return OK_STATUS(); \
135+
} \
136+
extern const int __never_referenced___here_to_eat_a_semicolon[]
137+
122138
#define UJSON_IMPL_SERIALIZE_STRUCT(name_, decl_) \
123139
status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) { \
124140
size_t nfield = decl_(ujson_count, ujson_count); \
@@ -266,6 +282,14 @@
266282
status_t ujson_serialize_##name_(ujson_t *uj, const name_ *self) \
267283
) /*endif*/
268284

285+
#define UJSON_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
286+
OT_IIF(UJSON_SERDE_IMPL) \
287+
( /*then*/ \
288+
UJSON_IMPL_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_) \
289+
, /*else*/ \
290+
status_t ujson_serialize_with_padding_##name_(ujson_t *uj, const name_ *self, size_t max_size) \
291+
) /*endif*/
292+
269293
#define UJSON_SERIALIZE_ENUM(formal_name_, name_, decl_, ...) \
270294
OT_IIF(UJSON_SERDE_IMPL) \
271295
( /*then*/ \
@@ -297,6 +321,7 @@
297321
#define UJSON_SERDE_STRUCT(formal_name_, name_, decl_, ...) \
298322
UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ##__VA_ARGS__); \
299323
UJSON_SERIALIZE_STRUCT(name_, decl_); \
324+
UJSON_SERIALIZE_STRUCT_WITH_PADDING(name_, decl_); \
300325
UJSON_DESERIALIZE_STRUCT(name_, decl_)
301326

302327
#define UJSON_SERDE_ENUM(formal_name_, name_, decl_, ...) \

sw/device/silicon_creator/manuf/tests/ujson_msg_padding_functest.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@ static status_t send_ujson_msgs(ujson_t *uj) {
8484
perso_blob_msg.body[i] = 0x5;
8585
}
8686

87-
// TX payloads to the host.
88-
RESP_OK_NO_CRC(ujson_serialize_serdes_sha256_hash_t, uj, &sha256_hash_msg);
89-
RESP_OK_NO_CRC(ujson_serialize_lc_token_hash_t, uj, &lc_token_hash_msg);
90-
RESP_OK_NO_CRC(ujson_serialize_manuf_certgen_inputs_t, uj,
91-
&certgen_inputs_msg);
92-
RESP_OK_NO_CRC(ujson_serialize_perso_blob_t, uj, &perso_blob_msg);
87+
// TX payloads to the host padding the payloads with whitespace.
88+
RESP_OK_PADDED_NO_CRC(ujson_serialize_with_padding_serdes_sha256_hash_t, uj,
89+
&sha256_hash_msg, kSerdesSha256HashSerializedMaxSize);
90+
RESP_OK_PADDED_NO_CRC(ujson_serialize_with_padding_lc_token_hash_t, uj,
91+
&lc_token_hash_msg, kLcTokenHashSerializedMaxSize);
92+
RESP_OK_PADDED_NO_CRC(ujson_serialize_with_padding_manuf_certgen_inputs_t, uj,
93+
&certgen_inputs_msg,
94+
kManufCertgenInputsSerializedMaxSize);
95+
RESP_OK_PADDED_NO_CRC(ujson_serialize_with_padding_perso_blob_t, uj,
96+
&perso_blob_msg, kPersoBlobSerializedMaxSize);
9397

9498
// RX payloads echoed back by host and check their sizes when received.
9599
TRY(ujson_deserialize_serdes_sha256_hash_t(uj, &sha256_hash_msg));

sw/host/provisioning/ujson_lib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pub mod provisioning_data;
1111
/// The are obtained by running the following FPGA test:
1212
/// bazel test --test_output=streamed \
1313
/// //sw/device/silicon_creator/manuf/tests:ujson_msg_size_functest
14+
///
15+
/// These should match the constants in:
16+
/// sw/device/lib/testing/json/provisioning_data.h
1417
pub const SERDES_SHA256_HASH_SERIALIZED_MAX_SIZE: usize = 98;
1518
pub const LC_TOKEN_HASH_SERIALIZED_MAX_SIZE: usize = 52;
1619
pub const MANUF_CERTGEN_INPUTS_SERIALIZED_MAX_SIZE: usize = 210;

0 commit comments

Comments
 (0)