Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 1 | /** |
| 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 Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 16 | void cper_to_json(int argc, char *argv[]); |
| 17 | void json_to_cper(int argc, char *argv[]); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 18 | void print_help(void); |
| 19 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 20 | int main(int argc, char *argv[]) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 21 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 27 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 28 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 40 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 41 | return 0; |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //Command for converting a provided CPER log file into JSON. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | void cper_to_json(int argc, char *argv[]) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 46 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 47 | if (argc < 3) { |
| 48 | printf("Insufficient number of arguments for 'to-json'. See 'cper-convert --help' for command information.\n"); |
| 49 | return; |
| 50 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 51 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 52 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 59 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 65 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 66 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 72 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 73 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 79 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 87 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 88 | //Write out to file. |
| 89 | fwrite(json_output, strlen(json_output), 1, json_file); |
| 90 | fclose(json_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | //Command for converting a provided CPER-JSON JSON file to CPER binary. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 94 | void json_to_cper(int argc, char *argv[]) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 95 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 96 | if (argc < 5) { |
| 97 | printf("Insufficient number of arguments for 'to-cper'. See 'cper-convert --help' for command information.\n"); |
| 98 | return; |
| 99 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 100 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 101 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 108 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 109 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 116 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 117 | //Validate the JSON against specification, unless otherwise specified. |
| 118 | if (do_validate) { |
| 119 | char *specification_path = NULL; |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 120 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 121 | //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 Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 133 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 134 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 142 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 143 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 147 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | //Free specification path (if necessary). |
| 149 | if (argc == 5) |
| 150 | free(specification_path); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 151 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 152 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 161 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 162 | //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 Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 170 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 171 | //Run the converter. |
| 172 | ir_to_cper(ir, cper_file); |
| 173 | fclose(cper_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | //Command for printing help information. |
| 177 | void print_help(void) |
| 178 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 179 | 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 Tang | 4ef0a0a | 2022-07-18 10:50:58 +0100 | [diff] [blame] | 190 | } |