/** * A user-space application linking to the CPER-JSON conversion library which allows for easy * conversion between CPER and CPER-JSON formats. * * Author: Lawrence.Tang@arm.com **/ #include #include #include #include #include "json.h" #include "../cper-parse.h" #include "../json-schema.h" void cper_to_json(int argc, char* argv[]); void json_to_cper(int argc, char* argv[]); void print_help(void); int main(int argc, char* argv[]) { //Ensure at least one argument is present. if (argc < 2) { printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n"); return -1; } //Run the requested command. if (strcmp(argv[1], "to-json") == 0) cper_to_json(argc, argv); else if (strcmp(argv[1], "to-cper") == 0) json_to_cper(argc, argv); else if (strcmp(argv[1], "--help") == 0) print_help(); else { printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n", argv[1]); return -1; } return 0; } //Command for converting a provided CPER log file into JSON. void cper_to_json(int argc, char* argv[]) { if (argc < 3) { printf("Insufficient number of arguments for 'to-json'. See 'cper-convert --help' for command information.\n"); return; } //Get a handle for the log file. FILE* cper_file = fopen(argv[2], "r"); if (cper_file == NULL) { printf("Could not open provided CPER file '%s', file handle returned null.\n", argv[2]); return; } //Convert. json_object* ir = cper_to_ir(cper_file); fclose(cper_file); const char* json_output = json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY); //Check whether there is a "--out" argument, if there is, then output to file instead. //Otherwise, just send to console. if (argc != 5) { printf("%s\n", json_output); return; } //File out argument exists. Argument valid? if (strcmp(argv[3], "--out") != 0) { printf("Invalid argument '%s' for command 'to-json'. See 'cper-convert --help' for command information.\n", argv[3]); return; } //Try to open a file handle to the desired output file. FILE* json_file = fopen(argv[4], "w"); if (json_file == NULL) { printf("Could not get a handle for output file '%s', file handle returned null.\n", argv[4]); return; } //Write out to file. fwrite(json_output, strlen(json_output), 1, json_file); fclose(json_file); } //Command for converting a provided CPER-JSON JSON file to CPER binary. void json_to_cper(int argc, char* argv[]) { if (argc < 5) { printf("Insufficient number of arguments for 'to-cper'. See 'cper-convert --help' for command information.\n"); return; } //Read JSON IR from file. json_object* ir = json_object_from_file(argv[2]); if (ir == NULL) { printf("Could not read JSON from file '%s', import returned null.\n", argv[2]); return; } //Are we skipping validation? int do_validate = 1; if (argc >= 6 && argc < 8) { if (strcmp(argv[5], "--no-validate") == 0) { do_validate = 0; } } //Validate the JSON against specification, unless otherwise specified. if (do_validate) { char* specification_path = NULL; //Is there a specification file path? if (argc >= 7 && strcmp(argv[argc - 2], "--specification") == 0) { specification_path = argv[argc - 1]; } else { //Make the specification path the assumed default (application directory + specification/cper-json.json). specification_path = malloc(PATH_MAX); char* dir = dirname(argv[0]); strcpy(specification_path, dir); strcat(specification_path, "/specification/cper-json.json"); } //Enable debug mode if indicated. for (int i=5; i