diff --git a/main.c b/main.c index 560dfb0669c244136fd239097e65b2287df22bc4..7e778b65096656738f1a40f0249600cee87c1245 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /* - * main.c - + * main.c - * unixtime that returns current time in epoch format. * * Copyright (C) 2020 iopsys Software Solutions AB. All rights reserved. @@ -33,15 +33,14 @@ #include <string.h> #include <unistd.h> -// static char *ubus_json_file_path = -// "/home/sudi/Workspace/git-userspace/ubus-api-validator/ubus.json"; +#define API_PATH "/usr/share/api/" -struct globalArgs_t { - char *ubus_json_file_path; /* -l option */ - const char *ubus_json_directory_path; /* -o option */ -} globalArgs; +struct global_args_t { + char *path; + const char *dir_path; +} global_args; -static const char *optString = "f:d:h"; +static const char *opt_string = "f:d:h"; static void show_usage(char *application_name) { @@ -57,15 +56,15 @@ static void show_usage(char *application_name) { static void parse_command_line_args(int argc, char *argv[]) { int opt; - while ((opt = getopt(argc, argv, optString)) != -1) { + while ((opt = getopt(argc, argv, opt_string)) != -1) { switch (opt) { case 'f': - globalArgs.ubus_json_file_path = optarg; + global_args.path = optarg; break; case 'd': - globalArgs.ubus_json_directory_path = optarg; + global_args.dir_path = optarg; break; case 'h': @@ -83,48 +82,51 @@ static void parse_command_line_args(int argc, char *argv[]) { int main(int argc, char *argv[]) { DIR *d; struct dirent *dir; + int rv; parse_command_line_args(argc, argv); - if (schema_validator_init()) { + rv = schema_validator_init(); + if (rv) { perror("schema_validator_init failed"); return EXIT_FAILURE; } - if (ubus_init()) { + rv = ubus_init(); + if (rv) { perror("ubus registration succesfull"); return EXIT_FAILURE; } - if (globalArgs.ubus_json_file_path != NULL) - validate_ubus_object(globalArgs.ubus_json_file_path); + if (global_args.path != NULL) + validate_ubus_object(global_args.path); - if (globalArgs.ubus_json_file_path == NULL && - globalArgs.ubus_json_directory_path != NULL) { + if (global_args.path == NULL && + global_args.dir_path != NULL) { printf("\n working with directory: %s \n ", - globalArgs.ubus_json_directory_path); + global_args.dir_path); - d = opendir(globalArgs.ubus_json_directory_path); + d = opendir(global_args.dir_path); if (d) { while ((dir = readdir(d)) != NULL) { char *res; - if (-1 == asprintf(&res, "%s%s", globalArgs.ubus_json_directory_path, - dir->d_name)) { + rv = asprintf(&res, "%s%s", global_args.dir_path,dir->d_name); + if (rv == -1) { fprintf(stderr, "asprintf() failed: insufficient memory!\n"); return EXIT_FAILURE; } if (dir->d_type == DT_REG) { printf("Result: '%s'\n", res); validate_ubus_object(res); - free(res); } + free(res); } closedir(d); } - } else if (globalArgs.ubus_json_file_path == NULL && - globalArgs.ubus_json_directory_path == NULL) { + } else if (global_args.path == NULL && + global_args.dir_path == NULL) { validate_ubus_object("./ubus.json"); } diff --git a/ubus.json b/ubus.json index 74115d3f18696195e88957dce133614995f7efba..27841af7230a00afae37f135800f7d590802c87e 100644 --- a/ubus.json +++ b/ubus.json @@ -1,7 +1,11 @@ [ { "object": "demo", - "call": "unixtime" + "call": "unixtime", + "args": { + "iface": "eth1", + "mac": "11:22:33:44:55:66" + } }, { "object": "hello", diff --git a/ubus_api_validator.c b/ubus_api_validator.c index 0cb4665b508507403819afd7d0f642d47423969d..9fdf650f01ac743f98ddec73e083ea8f3fd58acf 100644 --- a/ubus_api_validator.c +++ b/ubus_api_validator.c @@ -26,103 +26,31 @@ #include <stdio.h> #include <stdlib.h> -static struct json_object *parse_json_file(const char *json_file_content); -static int invoke_and_validate_ubus_object(struct json_object *jobj); -static char *open_file(char *file_name); +static int validate_jobj(struct json_object *jobj); - -static char *open_file(char *file_name) { - - FILE *fp; - size_t fsz; - long off_end; - int rc; - char *buffer; - - fp = fopen(file_name, "r"); // read mode - - if (fp == NULL) { - perror("Error while opening the file.\n"); - exit(EXIT_FAILURE); - } - - /* Seek to end of file */ - rc = fseek(fp, 0, SEEK_END); - - if (0 != rc) { - fprintf(stderr, "\nFailed to read file %s \n", file_name); - goto out_close; - } - - // Printing position of pointer - // printf("The bytes to read: %ld \n", ftell(fp)); - - /* Byte offset to the end of the file (size) */ - if (0 > (off_end = ftell(fp))) { - goto out_close; - } - fsz = (size_t)off_end; - - /* Allocate a buffer to hold the whole file */ - buffer = malloc(fsz); - if (NULL == buffer) { - goto out_close; - } - - /* Rewind file pointer to start of file */ - rewind(fp); - - /* Slurp file into buffer */ - if (fsz != fread(buffer, 1, fsz, fp)) { - free(buffer); - goto out; - } - - /* Close the file */ - if (EOF == fclose(fp)) { - free(buffer); - goto out; - } - - /* Return the file size */ - return buffer; - -out_close: - fclose(fp); -out: - return NULL; -} - -int validate_ubus_object(char *file_name) { - char *buff; +int validate_path(char *path) { json_object *jobj; int result = -1; - buff = open_file(file_name); - jobj = parse_json_file(buff); + jobj = json_object_from_file(path); + if (jobj == NULL) { + perror("could not read json file"); + return result; + } - result = invoke_and_validate_ubus_object(jobj); + result = validate_jobj(jobj); printf("\nresult: \n %d \n", result); - free(buff); free(jobj); return result; } -static struct json_object *parse_json_file(const char *json_file_content) { - struct json_object *jobj; - - jobj = json_tokener_parse(json_file_content); - - return jobj; -} - -static int invoke_and_validate_ubus_object(struct json_object *jobj) { +static int validate_jobj(struct json_object *jobj) { struct json_object *tmp; - struct json_object *ubus_json_result = NULL; + struct json_object *result = NULL; /*Now printing the json object*/ printf("\n json object: %s \n", json_object_to_json_string(jobj)); @@ -133,6 +61,9 @@ static int invoke_and_validate_ubus_object(struct json_object *jobj) { struct json_object *ubus_object; struct json_object *ubus_method; + const char *object; + const char *method; + int rv; tmp = json_object_array_get_idx(jobj, i); printf("json-array[%d] = %s\n", i, json_object_to_json_string(tmp)); @@ -140,25 +71,25 @@ static int invoke_and_validate_ubus_object(struct json_object *jobj) { json_object_object_get_ex(tmp, "object", &ubus_object); json_object_object_get_ex(tmp, "call", &ubus_method); - printf("object: %s\n", json_object_get_string(ubus_object)); - printf("call: %s\n", json_object_get_string(ubus_method)); + object = json_object_get_string(ubus_object); + method = json_object_get_string(ubus_method); - ubus_json_result = ubus_call(json_object_get_string(ubus_object), - json_object_get_string(ubus_method)); + printf("object: %s\n", object); + printf("call: %s\n", method); - if(ubus_json_result == NULL){ - printf("\nubus call failed \n"); - return -1; + result = ubus_call(object, method); + if (!result) { + printf("\nubus call failed \n"); + return -1; } - printf("\n json_object_to_json_string of ubus_json_result: %s \n", - json_object_to_json_string(ubus_json_result)); + printf("\n json_object_to_json_string of result: %s \n", + json_object_to_json_string(result)); - if (schema_validator_validate_jobj( - ubus_json_result, json_object_get_string(ubus_object), - json_object_get_string(ubus_method), SCHEMA_OUTPUT_CALL)) { + rv = schema_validator_validate_jobj(result, object, method, SCHEMA_OUTPUT_CALL); + if (rv) printf("\n ok \n"); - } else { + else { perror("\n failed \n"); return -1; } diff --git a/ubus_api_validator.h b/ubus_api_validator.h index 3c65146022104c6c4ceda8bf3e4c7953f4a22834..16f22f8650ec96652883ecca172c26b0ac5b39bf 100644 --- a/ubus_api_validator.h +++ b/ubus_api_validator.h @@ -3,7 +3,7 @@ #include <json-c/json.h> -int validate_ubus_object(char *file_name); +int validate_ubus_object(char *path); #endif /* UBUS_API_VALIDATOR_H */ \ No newline at end of file diff --git a/ubus_invoke.c b/ubus_invoke.c index 4da14d390493439db1b4684480ce8330a6ccdd2c..41ed2c74e1783c0b244bb315463e4208289b5e9d 100644 --- a/ubus_invoke.c +++ b/ubus_invoke.c @@ -47,32 +47,34 @@ __ret: return EXIT_FAILURE; } -json_object *ubus_call(const char *client, const char *method) { +void parse_ubus_cb(struct ubus_request *req, int type, struct blob_attr *msg) { - struct blob_buf buff = {0}; - int rv = -1; - uint32_t obj_id; - struct json_object *json_ubus_result; + struct json_object *root; + struct json_object **json_ubus_result = req->priv; + char *json_str; - auto void parse_ubus_cb(struct ubus_request * req, int type, - struct blob_attr *msg) { + json_str = blobmsg_format_json(msg, true); + if (!json_str) + return; - struct json_object *root; - json_ubus_result = req->priv; - char *json_str; + root = json_tokener_parse(json_str); - json_str = blobmsg_format_json(msg, true); + if(!root) + return; - if (!json_str) - return; - root = json_tokener_parse(json_str); + *json_ubus_result = json_object_get(root); - json_ubus_result = json_object_get(root); + json_object_put(root); + free(json_str); +} - json_object_put(root); - free(json_str); - } +json_object *ubus_call(const char *client, const char *method) { + + struct blob_buf buff = {0}; + int rv = -1; + uint32_t obj_id; + struct json_object *json_ubus_result; ubus_lookup_id(ctx, client, &obj_id); @@ -92,7 +94,7 @@ json_object *ubus_call(const char *client, const char *method) { (ubus_data_handler_t)parse_ubus_cb, &json_ubus_result, UBUS_DEFAULT_TIMEOUT); - if (rv){ + if (rv) { fprintf(stderr, "ubus_invoke error code %d \n", rv); json_ubus_result = NULL; }