Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
ubus-api-validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IOPSYS
ubus-api-validator
Commits
c66b7bc3
Commit
c66b7bc3
authored
5 years ago
by
Suru Dissanaike
Browse files
Options
Downloads
Patches
Plain Diff
after review
parent
0d0e0d1d
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
main.c
+26
-24
26 additions, 24 deletions
main.c
ubus.json
+5
-1
5 additions, 1 deletion
ubus.json
ubus_api_validator.c
+26
-95
26 additions, 95 deletions
ubus_api_validator.c
ubus_api_validator.h
+1
-1
1 addition, 1 deletion
ubus_api_validator.h
ubus_invoke.c
+21
-19
21 additions, 19 deletions
ubus_invoke.c
with
79 additions
and
140 deletions
main.c
+
26
−
24
View file @
c66b7bc3
/*
/*
* 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
global
A
rgs_t
{
struct
global
_a
rgs_t
{
char
*
ubus_json_file_path
;
/* -l option */
char
*
path
;
const
char
*
ubus_json_directory_path
;
/* -o option */
const
char
*
dir_path
;
}
global
A
rgs
;
}
global
_a
rgs
;
static
const
char
*
opt
S
tring
=
"f:d:h"
;
static
const
char
*
opt
_s
tring
=
"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
,
opt
S
tring
))
!=
-
1
)
{
while
((
opt
=
getopt
(
argc
,
argv
,
opt
_s
tring
))
!=
-
1
)
{
switch
(
opt
)
{
switch
(
opt
)
{
case
'f'
:
case
'f'
:
global
A
rgs
.
ubus_json_file_
path
=
optarg
;
global
_a
rgs
.
path
=
optarg
;
break
;
break
;
case
'd'
:
case
'd'
:
global
A
rgs
.
ubus_json_directory
_path
=
optarg
;
global
_a
rgs
.
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
(
global
A
rgs
.
ubus_json_file_
path
!=
NULL
)
if
(
global
_a
rgs
.
path
!=
NULL
)
validate_ubus_object
(
global
A
rgs
.
ubus_json_file_
path
);
validate_ubus_object
(
global
_a
rgs
.
path
);
if
(
global
A
rgs
.
ubus_json_file_
path
==
NULL
&&
if
(
global
_a
rgs
.
path
==
NULL
&&
global
A
rgs
.
ubus_json_directory
_path
!=
NULL
)
{
global
_a
rgs
.
dir
_path
!=
NULL
)
{
printf
(
"
\n
working with directory: %s
\n
"
,
printf
(
"
\n
working with directory: %s
\n
"
,
global
A
rgs
.
ubus_json_directory
_path
);
global
_a
rgs
.
dir
_path
);
d
=
opendir
(
global
A
rgs
.
ubus_json_directory
_path
);
d
=
opendir
(
global
_a
rgs
.
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"
,
global
A
rgs
.
ubus_json_directory_path
,
rv
=
asprintf
(
&
res
,
"%s%s"
,
global
_a
rgs
.
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
(
global
A
rgs
.
ubus_json_file_
path
==
NULL
&&
}
else
if
(
global
_a
rgs
.
path
==
NULL
&&
global
A
rgs
.
ubus_json_directory
_path
==
NULL
)
{
global
_a
rgs
.
dir
_path
==
NULL
)
{
validate_ubus_object
(
"./ubus.json"
);
validate_ubus_object
(
"./ubus.json"
);
}
}
...
...
This diff is collapsed.
Click to expand it.
ubus.json
+
5
−
1
View file @
c66b7bc3
[
[
{
{
"object"
:
"demo"
,
"object"
:
"demo"
,
"call"
:
"unixtime"
"call"
:
"unixtime"
,
"args"
:
{
"iface"
:
"eth1"
,
"mac"
:
"11:22:33:44:55:66"
}
},
},
{
{
"object"
:
"hello"
,
"object"
:
"hello"
,
...
...
This diff is collapsed.
Click to expand it.
ubus_api_validator.c
+
26
−
95
View file @
c66b7bc3
...
@@ -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
,
"
\n
Failed 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
(
"
\n
result:
\n
%d
\n
"
,
result
);
printf
(
"
\n
result:
\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
(
"
\n
ubus call failed
\n
"
);
if
(
!
result
)
{
return
-
1
;
printf
(
"
\n
ubus 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
;
}
}
...
...
This diff is collapsed.
Click to expand it.
ubus_api_validator.h
+
1
−
1
View file @
c66b7bc3
...
@@ -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
This diff is collapsed.
Click to expand it.
ubus_invoke.c
+
21
−
19
View file @
c66b7bc3
...
@@ -47,32 +47,34 @@ __ret:
...
@@ -47,32 +47,34 @@ __ret:
return
EXIT_FAILURE
;
return
EXIT_FAILURE
;
}
}
json_obje
ct
*
ubus_
call
(
const
char
*
client
,
const
char
*
method
)
{
void
parse_ubus_cb
(
stru
ct
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
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment