diff --git a/main.c b/main.c index 7e778b65096656738f1a40f0249600cee87c1245..f54813a2c9f7e68cf76b8cf0191b2a2c6cc7d179 100644 --- a/main.c +++ b/main.c @@ -99,35 +99,32 @@ int main(int argc, char *argv[]) { } if (global_args.path != NULL) - validate_ubus_object(global_args.path); + validate_path(global_args.path); - if (global_args.path == NULL && - global_args.dir_path != NULL) { + if (global_args.path == NULL && global_args.dir_path != NULL) { - printf("\n working with directory: %s \n ", - global_args.dir_path); + printf("\n working with directory: %s \n ", global_args.dir_path); d = opendir(global_args.dir_path); if (d) { while ((dir = readdir(d)) != NULL) { char *res; - rv = asprintf(&res, "%s%s", global_args.dir_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); + validate_path(res); } - free(res); + free(res); } closedir(d); } - } else if (global_args.path == NULL && - global_args.dir_path == NULL) { - validate_ubus_object("./ubus.json"); + } else if (global_args.path == NULL && global_args.dir_path == NULL) { + validate_path("./ubus.json"); } return 0; diff --git a/ubus.json b/ubus.json index 27841af7230a00afae37f135800f7d590802c87e..c301a2857573962640b0ffd9657d72a70f2a461a 100644 --- a/ubus.json +++ b/ubus.json @@ -1,14 +1,15 @@ -[ - { - "object": "demo", - "call": "unixtime", - "args": { - "iface": "eth1", - "mac": "11:22:33:44:55:66" +{ + "object": "demo", + "calls": [ + { + "call": "unixtime", + "args": { + "format": "%d--%d--%d %d::%d::%d" + } + }, + { + "call": "unixtime" } - }, - { - "object": "hello", - "call": "unixtime" - } -] + ] +} + diff --git a/ubus_api_validator.c b/ubus_api_validator.c index 9fdf650f01ac743f98ddec73e083ea8f3fd58acf..57a5e29dcde6a8e7cdac1f5f139219f660a48340 100644 --- a/ubus_api_validator.c +++ b/ubus_api_validator.c @@ -51,33 +51,41 @@ static int validate_jobj(struct json_object *jobj) { struct json_object *tmp; struct json_object *result = NULL; + struct json_object *ubus_object; + struct json_object *ubus_calls; + const char *object; /*Now printing the json object*/ printf("\n json object: %s \n", json_object_to_json_string(jobj)); + json_object_object_get_ex(jobj, "object", &ubus_object); + json_object_object_get_ex(jobj, "calls", &ubus_calls); int i = 0; + object = json_object_get_string(ubus_object); - for (i = 0; i < json_object_array_length(jobj); i++) { + for (i = 0; i < json_object_array_length(ubus_calls); i++) { - struct json_object *ubus_object; struct json_object *ubus_method; - const char *object; + struct json_object *ubus_args; const char *method; + const char *args; + int rv; - tmp = json_object_array_get_idx(jobj, i); + tmp = json_object_array_get_idx(ubus_calls, i); printf("json-array[%d] = %s\n", i, json_object_to_json_string(tmp)); - json_object_object_get_ex(tmp, "object", &ubus_object); json_object_object_get_ex(tmp, "call", &ubus_method); + json_object_object_get_ex(tmp, "args", &ubus_args); - object = json_object_get_string(ubus_object); method = json_object_get_string(ubus_method); + args = json_object_get_string(ubus_args); printf("object: %s\n", object); printf("call: %s\n", method); + printf("args: %s\n", args); - result = ubus_call(object, method); + result = ubus_call(object, method, args); if (!result) { printf("\nubus call failed \n"); return -1; @@ -86,7 +94,8 @@ static int validate_jobj(struct json_object *jobj) { printf("\n json_object_to_json_string of result: %s \n", json_object_to_json_string(result)); - rv = schema_validator_validate_jobj(result, object, method, SCHEMA_OUTPUT_CALL); + rv = schema_validator_validate_jobj(result, object, method, + SCHEMA_OUTPUT_CALL); if (rv) printf("\n ok \n"); else { diff --git a/ubus_api_validator.h b/ubus_api_validator.h index 16f22f8650ec96652883ecca172c26b0ac5b39bf..47bf55065534e10b5e35e40bcd4fb107c34efff4 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 *path); +int validate_path(char *path); #endif /* UBUS_API_VALIDATOR_H */ \ No newline at end of file diff --git a/ubus_invoke.c b/ubus_invoke.c index 41ed2c74e1783c0b244bb315463e4208289b5e9d..825900ba5fc3009cda2bdd68b1724476fe5b7a57 100644 --- a/ubus_invoke.c +++ b/ubus_invoke.c @@ -59,24 +59,24 @@ void parse_ubus_cb(struct ubus_request *req, int type, struct blob_attr *msg) { root = json_tokener_parse(json_str); - if(!root) + if (!root) return; - *json_ubus_result = json_object_get(root); json_object_put(root); free(json_str); } -json_object *ubus_call(const char *client, const char *method) { +json_object *ubus_call(const char *object, const char *method, + const char *args) { 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); + ubus_lookup_id(ctx, object, &obj_id); if (!obj_id) { fprintf(stderr, "ubus lookup_id error \n"); @@ -90,6 +90,14 @@ json_object *ubus_call(const char *client, const char *method) { return NULL; } + if (args) { + rv = blobmsg_add_json_from_string(&buff, args); + if (!rv) { + perror("could not convert arg to json \n"); + goto out; + } + } + rv = ubus_invoke(ctx, obj_id, method, buff.head, (ubus_data_handler_t)parse_ubus_cb, &json_ubus_result, UBUS_DEFAULT_TIMEOUT); @@ -99,6 +107,7 @@ json_object *ubus_call(const char *client, const char *method) { json_ubus_result = NULL; } +out: blob_buf_free(&buff); return json_ubus_result; diff --git a/ubus_invoke.h b/ubus_invoke.h index 329f75cf3a64bdee1a9c7e43a1538425a08823f2..cd9308701d4db9de1934463e39927ebc8e5c3783 100644 --- a/ubus_invoke.h +++ b/ubus_invoke.h @@ -9,7 +9,7 @@ int ubus_init(void); -json_object *ubus_call(const char *client, const char *method); +json_object *ubus_call(const char *object, const char *method, const char *args);