Software APIs
example_roundtrip.c
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 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include "sw/device/lib/base/crc32.h"
10 #include "sw/device/lib/base/status.h"
11 #include "sw/device/lib/ujson/example.h"
12 
13 status_t stdio_getc(void *context) {
14  int ch = fgetc(stdin);
15  return ch == EOF ? RESOURCE_EXHAUSTED() : OK_STATUS(ch);
16 }
17 
18 status_t stdio_putbuf(void *context, const char *buf, size_t len) {
19  fwrite(buf, 1, len, stdout);
20  return OK_STATUS();
21 }
22 
23 status_t check_crc32(ujson_t *uj) {
24  uint32_t expected = ujson_crc32_finish(uj);
25  uint32_t actual;
26  scanf("%x", &actual);
27 
28  if (expected != actual) {
29  fprintf(stderr, "CRC32 Error: expected = %x, actual = %x\n", expected,
30  actual);
31  return DATA_LOSS();
32  };
33  return OK_STATUS();
34 }
35 
36 status_t roundtrip(const char *name) {
37  ujson_t uj = ujson_init(NULL, stdio_getc, stdio_putbuf);
38  if (!strcmp(name, "foo")) {
39  foo x = {0};
40  TRY(ujson_deserialize_foo(&uj, &x));
41  TRY(check_crc32(&uj));
42  ujson_crc32_reset(&uj);
43  TRY(ujson_serialize_foo(&uj, &x));
44  printf("\n%x", ujson_crc32_finish(&uj));
45  } else if (!strcmp(name, "rect")) {
46  rect x = {0};
47  TRY(ujson_deserialize_rect(&uj, &x));
48  TRY(check_crc32(&uj));
49  ujson_crc32_reset(&uj);
50  TRY(ujson_serialize_rect(&uj, &x));
51  printf("\n%x", ujson_crc32_finish(&uj));
52  } else if (!strcmp(name, "matrix")) {
53  matrix x = {0};
54  TRY(ujson_deserialize_matrix(&uj, &x));
55  TRY(check_crc32(&uj));
56  ujson_crc32_reset(&uj);
57  TRY(ujson_serialize_matrix(&uj, &x));
58  printf("\n%x", ujson_crc32_finish(&uj));
59  } else if (!strcmp(name, "direction")) {
60  direction x = {0};
61  TRY(ujson_deserialize_direction(&uj, &x));
62  TRY(check_crc32(&uj));
63  ujson_crc32_reset(&uj);
64  TRY(ujson_serialize_direction(&uj, &x));
65  printf("\n%x", ujson_crc32_finish(&uj));
66  } else if (!strcmp(name, "fuzzy_bool")) {
67  fuzzy_bool x = {0};
68  TRY(ujson_deserialize_fuzzy_bool(&uj, &x));
69  TRY(check_crc32(&uj));
70  ujson_crc32_reset(&uj);
71  TRY(ujson_serialize_fuzzy_bool(&uj, &x));
72  printf("\n%x", ujson_crc32_finish(&uj));
73  } else if (!strcmp(name, "fuzzy_bool_no_crc")) {
74  fuzzy_bool x = {0};
75  TRY(ujson_deserialize_fuzzy_bool(&uj, &x));
76  TRY(ujson_serialize_fuzzy_bool(&uj, &x));
77  } else if (!strcmp(name, "misc")) {
78  misc_t x = {0};
79  TRY(ujson_deserialize_misc_t(&uj, &x));
80  TRY(check_crc32(&uj));
81  ujson_crc32_reset(&uj);
82  TRY(ujson_serialize_misc_t(&uj, &x));
83  printf("\n%x", ujson_crc32_finish(&uj));
84  } else {
85  return INVALID_ARGUMENT();
86  }
87  return OK_STATUS();
88 }
89 
90 int main(int argc, char *argv[]) {
91  if (argc < 2) {
92  fprintf(stderr, "%s [struct-name]", argv[0]);
93  return EXIT_FAILURE;
94  }
95  status_t s = roundtrip(argv[1]);
96 
97  return status_ok(s) ? EXIT_SUCCESS : EXIT_FAILURE;
98 }