Commit 6bd06929 authored by Anjan Chanda's avatar Anjan Chanda

move event registration function out of main()

parent 905c0db8
Pipeline #458 passed with stage
in 27 seconds
......@@ -40,6 +40,7 @@
#include "utils.h"
#include "wifimngr.h"
#define DEFAULT_WIFI_EVMAP_JSON_FILE "/etc/wifi.json"
#define MAX_CHILD 16
int child[MAX_CHILD];
const char *ubus_socket;
......@@ -98,7 +99,6 @@ static int wifimngr_cmd_main(struct ubus_context *ctx)
}
for (i = 0; i < num_ifs; i++) {
//ret = wifimngr_add_object(wmngr, ctx, ifs[i].iface, interface);
ret = wifimngr_add_interface_object(wmngr, ctx, &ifs[i]);
if (ret) {
fprintf(stderr,
......@@ -115,6 +115,59 @@ out_exit:
return -1;
}
static int wifimngr_event_main(const char *evmap_file)
{
struct json_object *jevs, *jev_array;
int evproc = 0;
int len = 0;
int ret = 0;
int i, j;
/* read from json file about events to listen for */
jevs = json_object_from_file(evmap_file);
if (!jevs) {
fprintf(stderr, "Failed to open '%s'\n", evmap_file);
return -1;
}
if (!json_object_is_type(jevs, json_type_object))
goto out_json;
json_object_object_get_ex(jevs, "events", &jev_array);
len = json_object_array_length(jev_array);
for (i = 0; i < len; i++) {
struct json_object *jev;
struct json_object *jev_family, *jev_ifname;
struct json_object *jev_group_array, *jev_grp;
const char *ifname, *family, *group;
int grplen = 0;
jev = json_object_array_get_idx(jev_array, i);
json_object_object_get_ex(jev, "ifname", &jev_ifname);
ifname = json_object_get_string(jev_ifname);
json_object_object_get_ex(jev, "family", &jev_family);
family = json_object_get_string(jev_family);
json_object_object_get_ex(jev, "group", &jev_group_array);
grplen = json_object_array_length(jev_group_array);
for (j = 0; j < grplen; j++) {
jev_grp = json_object_array_get_idx(jev_group_array, j);
group = json_object_get_string(jev_grp);
fprintf(stderr, "Setup event (%s, %s, %s)\n",
ifname, family, group);
ret = wifimngr_setup_event(ifname, family, group);
if (ret > 0)
child[evproc++] = ret;
}
}
out_json:
json_object_put(jevs);
return ret;
}
void wifimngr_clean_exit(void)
{
int i;
......@@ -151,17 +204,12 @@ static void wifimngr_sighandler(int sig)
int main(int argc, char **argv)
{
const char *evmap_file = DEFAULT_WIFI_EVMAP_JSON_FILE;
struct ubus_context *ctx = NULL;
int i = 0;
int ret;
int ch;
int j;
struct json_object *jevs, *jev_array;
int len = 0;
int evproc = 0;
while ((ch = getopt(argc, argv, "vhs:")) != -1) {
while ((ch = getopt(argc, argv, "vhs:e:")) != -1) {
switch (ch) {
case 'v':
wifimngr_version();
......@@ -169,6 +217,9 @@ int main(int argc, char **argv)
case 's':
ubus_socket = optarg;
break;
case 'e':
evmap_file = optarg;
break;
default:
break;
}
......@@ -186,50 +237,8 @@ int main(int argc, char **argv)
ubus_add_uloop(ctx);
///////////////////////////////////////////////////
/* read from json file about events to listen for */
jevs = json_object_from_file("/etc/wifi.json");
if (!jevs) {
fprintf(stderr, "Failed to open wifi.json\n");
goto skip_event;
}
if (!json_object_is_type(jevs, json_type_object))
goto out_json;
json_object_object_get_ex(jevs, "events", &jev_array);
len = json_object_array_length(jev_array);
for (i = 0; i < len; i++) {
struct json_object *jev;
struct json_object *jev_family, *jev_ifname;
struct json_object *jev_group_array, *jev_grp;
const char *ifname, *family, *group;
int grplen = 0;
jev = json_object_array_get_idx(jev_array, i);
json_object_object_get_ex(jev, "ifname", &jev_ifname);
ifname = json_object_get_string(jev_ifname);
json_object_object_get_ex(jev, "family", &jev_family);
family = json_object_get_string(jev_family);
json_object_object_get_ex(jev, "group", &jev_group_array);
grplen = json_object_array_length(jev_group_array);
for (j = 0; j < grplen; j++) {
jev_grp = json_object_array_get_idx(jev_group_array, j);
group = json_object_get_string(jev_grp);
fprintf(stderr, "Setup event (%s, %s, %s)\n",
ifname, family, group);
ret = wifimngr_setup_event(ifname, family, group);
if (ret > 0)
child[evproc++] = ret;
}
}
/////////////////////////////////////////////////
out_json:
json_object_put(jevs);
wifimngr_event_main(evmap_file);
skip_event:
ret = wifimngr_cmd_main(ctx);
if (ret) {
fprintf(stderr,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment