Skip to content
Snippets Groups Projects
Commit c66b7bc3 authored by Suru Dissanaike's avatar Suru Dissanaike
Browse files

after review

parent 0d0e0d1d
No related branches found
No related tags found
No related merge requests found
/* /*
* main.c - * main.c -
* unixtime that returns current time in epoch format. * unixtime that returns current time in epoch format.
* *
* Copyright (C) 2020 iopsys Software Solutions AB. All rights reserved. * Copyright (C) 2020 iopsys Software Solutions AB. All rights reserved.
...@@ -33,15 +33,14 @@ ...@@ -33,15 +33,14 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
// static char *ubus_json_file_path = #define API_PATH "/usr/share/api/"
// "/home/sudi/Workspace/git-userspace/ubus-api-validator/ubus.json";
struct globalArgs_t { struct global_args_t {
char *ubus_json_file_path; /* -l option */ char *path;
const char *ubus_json_directory_path; /* -o option */ const char *dir_path;
} globalArgs; } global_args;
static const char *optString = "f:d:h"; static const char *opt_string = "f:d:h";
static void show_usage(char *application_name) { static void show_usage(char *application_name) {
...@@ -57,15 +56,15 @@ 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[]) { static void parse_command_line_args(int argc, char *argv[]) {
int opt; int opt;
while ((opt = getopt(argc, argv, optString)) != -1) { while ((opt = getopt(argc, argv, opt_string)) != -1) {
switch (opt) { switch (opt) {
case 'f': case 'f':
globalArgs.ubus_json_file_path = optarg; global_args.path = optarg;
break; break;
case 'd': case 'd':
globalArgs.ubus_json_directory_path = optarg; global_args.dir_path = optarg;
break; break;
case 'h': case 'h':
...@@ -83,48 +82,51 @@ static void parse_command_line_args(int argc, char *argv[]) { ...@@ -83,48 +82,51 @@ static void parse_command_line_args(int argc, char *argv[]) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
DIR *d; DIR *d;
struct dirent *dir; struct dirent *dir;
int rv;
parse_command_line_args(argc, argv); parse_command_line_args(argc, argv);
if (schema_validator_init()) { rv = schema_validator_init();
if (rv) {
perror("schema_validator_init failed"); perror("schema_validator_init failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (ubus_init()) { rv = ubus_init();
if (rv) {
perror("ubus registration succesfull"); perror("ubus registration succesfull");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (globalArgs.ubus_json_file_path != NULL) if (global_args.path != NULL)
validate_ubus_object(globalArgs.ubus_json_file_path); validate_ubus_object(global_args.path);
if (globalArgs.ubus_json_file_path == NULL && if (global_args.path == NULL &&
globalArgs.ubus_json_directory_path != NULL) { global_args.dir_path != NULL) {
printf("\n working with directory: %s \n ", 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) { if (d) {
while ((dir = readdir(d)) != NULL) { while ((dir = readdir(d)) != NULL) {
char *res; char *res;
if (-1 == asprintf(&res, "%s%s", globalArgs.ubus_json_directory_path, rv = asprintf(&res, "%s%s", global_args.dir_path,dir->d_name);
dir->d_name)) { if (rv == -1) {
fprintf(stderr, "asprintf() failed: insufficient memory!\n"); fprintf(stderr, "asprintf() failed: insufficient memory!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (dir->d_type == DT_REG) { if (dir->d_type == DT_REG) {
printf("Result: '%s'\n", res); printf("Result: '%s'\n", res);
validate_ubus_object(res); validate_ubus_object(res);
free(res);
} }
free(res);
} }
closedir(d); closedir(d);
} }
} else if (globalArgs.ubus_json_file_path == NULL && } else if (global_args.path == NULL &&
globalArgs.ubus_json_directory_path == NULL) { global_args.dir_path == NULL) {
validate_ubus_object("./ubus.json"); validate_ubus_object("./ubus.json");
} }
......
[ [
{ {
"object": "demo", "object": "demo",
"call": "unixtime" "call": "unixtime",
"args": {
"iface": "eth1",
"mac": "11:22:33:44:55:66"
}
}, },
{ {
"object": "hello", "object": "hello",
......
...@@ -26,103 +26,31 @@ ...@@ -26,103 +26,31 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static struct json_object *parse_json_file(const char *json_file_content); static int validate_jobj(struct json_object *jobj);
static int invoke_and_validate_ubus_object(struct json_object *jobj);
static char *open_file(char *file_name);
int validate_path(char *path) {
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;
json_object *jobj; json_object *jobj;
int result = -1; int result = -1;
buff = open_file(file_name); jobj = json_object_from_file(path);
jobj = parse_json_file(buff); 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); printf("\nresult: \n %d \n", result);
free(buff);
free(jobj); free(jobj);
return result; return result;
} }
static struct json_object *parse_json_file(const char *json_file_content) { static int validate_jobj(struct json_object *jobj) {
struct json_object *jobj;
jobj = json_tokener_parse(json_file_content);
return jobj;
}
static int invoke_and_validate_ubus_object(struct json_object *jobj) {
struct json_object *tmp; struct json_object *tmp;
struct json_object *ubus_json_result = NULL; struct json_object *result = NULL;
/*Now printing the json object*/ /*Now printing the json object*/
printf("\n json object: %s \n", json_object_to_json_string(jobj)); 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) { ...@@ -133,6 +61,9 @@ static int invoke_and_validate_ubus_object(struct json_object *jobj) {
struct json_object *ubus_object; struct json_object *ubus_object;
struct json_object *ubus_method; struct json_object *ubus_method;
const char *object;
const char *method;
int rv;
tmp = json_object_array_get_idx(jobj, i); tmp = json_object_array_get_idx(jobj, i);
printf("json-array[%d] = %s\n", i, json_object_to_json_string(tmp)); 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) { ...@@ -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, "object", &ubus_object);
json_object_object_get_ex(tmp, "call", &ubus_method); json_object_object_get_ex(tmp, "call", &ubus_method);
printf("object: %s\n", json_object_get_string(ubus_object)); object = json_object_get_string(ubus_object);
printf("call: %s\n", json_object_get_string(ubus_method)); method = json_object_get_string(ubus_method);
ubus_json_result = ubus_call(json_object_get_string(ubus_object), printf("object: %s\n", object);
json_object_get_string(ubus_method)); printf("call: %s\n", method);
if(ubus_json_result == NULL){ result = ubus_call(object, method);
printf("\nubus call failed \n"); if (!result) {
return -1; printf("\nubus call failed \n");
return -1;
} }
printf("\n json_object_to_json_string of ubus_json_result: %s \n", printf("\n json_object_to_json_string of result: %s \n",
json_object_to_json_string(ubus_json_result)); json_object_to_json_string(result));
if (schema_validator_validate_jobj( rv = schema_validator_validate_jobj(result, object, method, SCHEMA_OUTPUT_CALL);
ubus_json_result, json_object_get_string(ubus_object), if (rv)
json_object_get_string(ubus_method), SCHEMA_OUTPUT_CALL)) {
printf("\n ok \n"); printf("\n ok \n");
} else { else {
perror("\n failed \n"); perror("\n failed \n");
return -1; return -1;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <json-c/json.h> #include <json-c/json.h>
int validate_ubus_object(char *file_name); int validate_ubus_object(char *path);
#endif /* UBUS_API_VALIDATOR_H */ #endif /* UBUS_API_VALIDATOR_H */
\ No newline at end of file
...@@ -47,32 +47,34 @@ __ret: ...@@ -47,32 +47,34 @@ __ret:
return EXIT_FAILURE; 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}; struct json_object *root;
int rv = -1; struct json_object **json_ubus_result = req->priv;
uint32_t obj_id; char *json_str;
struct json_object *json_ubus_result;
auto void parse_ubus_cb(struct ubus_request * req, int type, json_str = blobmsg_format_json(msg, true);
struct blob_attr *msg) { if (!json_str)
return;
struct json_object *root; root = json_tokener_parse(json_str);
json_ubus_result = req->priv;
char *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); json_object *ubus_call(const char *client, const char *method) {
free(json_str);
} 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, client, &obj_id);
...@@ -92,7 +94,7 @@ json_object *ubus_call(const char *client, const char *method) { ...@@ -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_data_handler_t)parse_ubus_cb, &json_ubus_result,
UBUS_DEFAULT_TIMEOUT); UBUS_DEFAULT_TIMEOUT);
if (rv){ if (rv) {
fprintf(stderr, "ubus_invoke error code %d \n", rv); fprintf(stderr, "ubus_invoke error code %d \n", rv);
json_ubus_result = NULL; json_ubus_result = NULL;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment