1#include <cmdline.h>
2#include <ndjson.h>
3#include <stdint.h>
4
5#include "internal.h"
6
7#define NDJSON_PORT_DEFAULT 0x2F8
8
9static uint64_t ndjson_port = NDJSON_PORT_DEFAULT;
10static bool ndjson_enabled = true;
11static bool ndjson_schema_dump = true;
12static bool ndjson_selftest_enabled = false;
13
14static CMDLINE_DECLARE(ndjson, .flags = CMDLINE_ENTRY_SYMBOLIC,
15 .desc = "NDJSON symbolic parent");
16
17CMDLINE_CHILDREN_DECLARE(
18 ndjson,
19 CMDLINE_INNER_VAR(enabled, ndjson_enabled, .desc = "Emit NDJSON records"),
20 CMDLINE_INNER_VAR(port, ndjson_port, .desc = "NDJSON carrier port",
21 .arg = "<io port>", .range = RANGE(0, UINT16_MAX)),
22 CMDLINE_INNER_VAR(schema, ndjson_schema_dump,
23 .desc = "Dump the schema at boot"),
24 CMDLINE_INNER_VAR(selftest, ndjson_selftest_enabled,
25 .desc = "Self test at boot"));
26
27/* Early init before cmdline is parsed, so faults still have somewhere to go */
28void ndjson_early_init(void) {
29 ndjson_carrier_init(NDJSON_PORT_DEFAULT);
30}
31
32void ndjson_init(void) {
33 if (!ndjson_enabled) {
34 ndjson_carrier_disable();
35 return;
36 }
37
38 if (ndjson_port != NDJSON_PORT_DEFAULT) {
39 ndjson_carrier_disable();
40 ndjson_carrier_init(port: (uint16_t) ndjson_port);
41 }
42
43 ndjson_check_duplicates();
44
45 if (ndjson_schema_dump)
46 ndjson_dump_schema();
47
48 if (ndjson_selftest_enabled)
49 ndjson_selftest();
50}
51
52NDJSON_DECLARE(ndjson_bye, NDJSON_SECTION_NDJSON, NDJSON_KIND_BYE, 1,
53 NDJSON_U64(code), NDJSON_STR(reason));
54
55/* Signal the end of kernel execution */
56void ndjson_bye(uint64_t code, const char *reason) {
57 ndjson_emit(ndjson_bye, .code = code, .reason = reason);
58}
59