blob: 03325c29eb43bc33b0366e62c0db0695fcae1f07 [file] [log] [blame]
Lawrence Tangb8fa2f72022-07-18 10:50:19 +01001/**
2 * A user-space application linking to the CPER-JSON conversion library which allows for easy
3 * conversion between CPER and CPER-JSON formats.
4 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include <stdio.h>
9#include <string.h>
10#include <libgen.h>
11#include <limits.h>
12#include "json.h"
13#include "../cper-parse.h"
14#include "../json-schema.h"
15
Lawrence Tange407b4c2022-07-21 13:54:01 +010016void cper_to_json(int argc, char *argv[]);
17void json_to_cper(int argc, char *argv[]);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010018void print_help(void);
19
Lawrence Tange407b4c2022-07-21 13:54:01 +010020int main(int argc, char *argv[])
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010021{
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 //Ensure at least one argument is present.
23 if (argc < 2) {
24 printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n");
25 return -1;
26 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010027
Lawrence Tange407b4c2022-07-21 13:54:01 +010028 //Run the requested command.
29 if (strcmp(argv[1], "to-json") == 0)
30 cper_to_json(argc, argv);
31 else if (strcmp(argv[1], "to-cper") == 0)
32 json_to_cper(argc, argv);
33 else if (strcmp(argv[1], "--help") == 0)
34 print_help();
35 else {
36 printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n",
37 argv[1]);
38 return -1;
39 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010040
Lawrence Tange407b4c2022-07-21 13:54:01 +010041 return 0;
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010042}
43
44//Command for converting a provided CPER log file into JSON.
Lawrence Tange407b4c2022-07-21 13:54:01 +010045void cper_to_json(int argc, char *argv[])
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010046{
Lawrence Tange407b4c2022-07-21 13:54:01 +010047 if (argc < 3) {
48 printf("Insufficient number of arguments for 'to-json'. See 'cper-convert --help' for command information.\n");
49 return;
50 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //Get a handle for the log file.
53 FILE *cper_file = fopen(argv[2], "r");
54 if (cper_file == NULL) {
55 printf("Could not open provided CPER file '%s', file handle returned null.\n",
56 argv[2]);
57 return;
58 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010059
Lawrence Tange407b4c2022-07-21 13:54:01 +010060 //Convert.
61 json_object *ir = cper_to_ir(cper_file);
62 fclose(cper_file);
63 const char *json_output =
64 json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010065
Lawrence Tange407b4c2022-07-21 13:54:01 +010066 //Check whether there is a "--out" argument, if there is, then output to file instead.
67 //Otherwise, just send to console.
68 if (argc != 5) {
69 printf("%s\n", json_output);
70 return;
71 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010072
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 //File out argument exists. Argument valid?
74 if (strcmp(argv[3], "--out") != 0) {
75 printf("Invalid argument '%s' for command 'to-json'. See 'cper-convert --help' for command information.\n",
76 argv[3]);
77 return;
78 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010079
Lawrence Tange407b4c2022-07-21 13:54:01 +010080 //Try to open a file handle to the desired output file.
81 FILE *json_file = fopen(argv[4], "w");
82 if (json_file == NULL) {
83 printf("Could not get a handle for output file '%s', file handle returned null.\n",
84 argv[4]);
85 return;
86 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010087
Lawrence Tange407b4c2022-07-21 13:54:01 +010088 //Write out to file.
89 fwrite(json_output, strlen(json_output), 1, json_file);
90 fclose(json_file);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010091}
92
93//Command for converting a provided CPER-JSON JSON file to CPER binary.
Lawrence Tange407b4c2022-07-21 13:54:01 +010094void json_to_cper(int argc, char *argv[])
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010095{
Lawrence Tange407b4c2022-07-21 13:54:01 +010096 if (argc < 5) {
97 printf("Insufficient number of arguments for 'to-cper'. See 'cper-convert --help' for command information.\n");
98 return;
99 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100100
Lawrence Tange407b4c2022-07-21 13:54:01 +0100101 //Read JSON IR from file.
102 json_object *ir = json_object_from_file(argv[2]);
103 if (ir == NULL) {
104 printf("Could not read JSON from file '%s', import returned null.\n",
105 argv[2]);
106 return;
107 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100108
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109 //Are we skipping validation?
110 int do_validate = 1;
111 if (argc >= 6 && argc < 8) {
112 if (strcmp(argv[5], "--no-validate") == 0) {
113 do_validate = 0;
114 }
115 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100116
Lawrence Tange407b4c2022-07-21 13:54:01 +0100117 //Validate the JSON against specification, unless otherwise specified.
118 if (do_validate) {
119 char *specification_path = NULL;
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100120
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 //Is there a specification file path?
122 if (argc >= 7 &&
123 strcmp(argv[argc - 2], "--specification") == 0) {
124 specification_path = argv[argc - 1];
125 } else {
126 //Make the specification path the assumed default (application directory + specification/cper-json.json).
127 specification_path = malloc(PATH_MAX);
128 char *dir = dirname(argv[0]);
129 strcpy(specification_path, dir);
130 strcat(specification_path,
131 "/specification/cper-json.json");
132 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100133
Lawrence Tange407b4c2022-07-21 13:54:01 +0100134 //Enable debug mode if indicated.
135 for (int i = 5; i < argc; i++) {
136 if (strcmp(argv[i], "--debug") == 0) {
137 validate_schema_debug_enable();
138 printf("debug enabled.\n");
139 break;
140 }
141 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Attempt to verify with the the specification.
144 char *error_message = malloc(JSON_ERROR_MSG_MAX_LEN);
145 int success = validate_schema_from_file(specification_path, ir,
146 error_message);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100147
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 //Free specification path (if necessary).
149 if (argc == 5)
150 free(specification_path);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100151
Lawrence Tange407b4c2022-07-21 13:54:01 +0100152 //If failed, early exit before conversion.
153 if (!success) {
154 printf("JSON format validation failed: %s\n",
155 error_message);
156 free(error_message);
157 return;
158 }
159 free(error_message);
160 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100161
Lawrence Tange407b4c2022-07-21 13:54:01 +0100162 //Attempt a conversion.
163 //Open a read for the output file.
164 FILE *cper_file = fopen(argv[4], "w");
165 if (cper_file == NULL) {
166 printf("Could not open output file '%s', file handle returned null.\n",
167 argv[4]);
168 return;
169 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100170
Lawrence Tange407b4c2022-07-21 13:54:01 +0100171 //Run the converter.
172 ir_to_cper(ir, cper_file);
173 fclose(cper_file);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100174}
175
176//Command for printing help information.
177void print_help(void)
178{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100179 printf(":: to-json cper.file [--out file.name]\n");
180 printf("\tConverts the provided CPER log file into JSON, by default outputting to console. If '--out' is specified,\n");
181 printf("\tThe outputted JSON will be written to the provided file name instead.\n");
182 printf("\n:: to-cper cper.json --out file.name [--no-validate] [--debug] [--specification some/spec/path.json]\n");
183 printf("\tConverts the provided CPER-JSON JSON file into CPER binary. An output file must be specified with '--out'.\n");
184 printf("\tBy default, the provided JSON will try to be validated against a specification. If no specification file path\n");
185 printf("\tis provided with '--specification', then it will default to 'argv[0] + /specification/cper-json.json'.\n");
186 printf("\tIf the '--no-validate' argument is set, then the provided JSON will not be validated. Be warned, this may cause\n");
187 printf("\tpremature exit/unexpected behaviour in CPER output.\n");
188 printf("\n:: --help\n");
189 printf("\tDisplays help information to the console.\n");
Lawrence Tang4ef0a0a2022-07-18 10:50:58 +0100190}