From 3221372d42d4dc32a45eaf0a573e8b378d6e196d Mon Sep 17 00:00:00 2001 From: Jakob Olsson <jakobols@kth.se> Date: Fri, 1 Jun 2018 10:10:25 +0200 Subject: [PATCH] publish dynamic object when add to device list --- common.h | 1 + dongle.c | 16 ++++++++-------- dongle_infrastructure.c | 29 +++++++++++++++++++---------- dongle_infrastructure.h | 1 + 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/common.h b/common.h index d051c0e..51ee55f 100644 --- a/common.h +++ b/common.h @@ -23,6 +23,7 @@ #include <libubus.h> extern int debug; +extern struct ubus_context *global_ctx; #define debug_print(...) \ do { \ diff --git a/dongle.c b/dongle.c index a4b238a..459c815 100644 --- a/dongle.c +++ b/dongle.c @@ -6,7 +6,7 @@ #include "dongle_network.h" #include "dongle_infrastructure.h" -struct ubus_context *ctx; +struct ubus_context *global_ctx; int debug; struct uloop_timeout timeout = { .cb = devices_status }; @@ -51,12 +51,12 @@ fail: void init_ubus(void) { - ctx = ubus_connect(NULL); - if (!ctx) { + global_ctx = ubus_connect(NULL); + if (!global_ctx) { perror("ubus"); exit(1); } - ubus_add_uloop(ctx); + ubus_add_uloop(global_ctx); } int main(int argc, char **argv) @@ -72,19 +72,19 @@ int main(int argc, char **argv) uloop_timeout_set(&timeout, 0); uloop_timeout_add(&timeout); /* - rv = expose_apn_object(ctx); + rv = expose_apn_object(global_ctx); if (rv < 0) goto fail; - rv = expose_pin_object(ctx); + rv = expose_pin_object(global_ctx); if (rv < 0) goto fail; - rv = expose_network_object(ctx); + rv = expose_network_object(global_ctx); if (rv < 0) goto fail; */ - rv = expose_infrastructure_object(ctx); + rv = expose_infrastructure_object(global_ctx); if (rv < 0) goto fail; uloop_run(); diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index d257b1d..f346dc0 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -74,26 +74,29 @@ int tag_missing_devices(void) return 0; } -int add_device(struct device *node) +int add_device(struct device *new_dev) { struct device *dev; - dev = search_list(node->usb.if_name); + dev = search_list(new_dev->usb.if_name); if (dev) { - /* Nicer implementation */ if (dev->ip) free(dev->ip); - dev->ip = node->ip; + dev->ip = new_dev->ip; dev->present = true; goto fail; } - node->present = true; + new_dev->present = true; if (list_empty(&devices)) INIT_LIST_HEAD(&devices); - list_add_tail(&node->list, &devices); - //ubus_add_obj + + list_add_tail(&new_dev->list, &devices); + if (new_dev->ip) { + new_dev->ubus_obj = dongle_create_dynamic_object(new_dev); + publish_ubus_object(global_ctx, new_dev->ubus_obj); + } return 0; fail: @@ -123,8 +126,8 @@ int delete_device_by_name(char *name) list_for_each_entry_safe(dev, tmp, &devices, list) { if (strncmp(dev->usb.if_name, name, 128) != 0) continue; + delete_device_by_obj(dev); - //ubus_remove_obj return 0; } @@ -133,6 +136,7 @@ int delete_device_by_name(char *name) int delete_device_by_obj(struct device *dev) { + unpublish_ubus_object(global_ctx, dev->ubus_obj); free(dev->usb.if_name); free(dev->ip); list_del(&dev->list); @@ -531,7 +535,12 @@ int expose_infrastructure_object(struct ubus_context *ctx) return 0; } -struct ubus_method dynamic_object_methods[] = {}; +struct ubus_method dynamic_object_methods[] = { + UBUS_METHOD_NOARG("test", test), + UBUS_METHOD_NOARG("list", print_list), + UBUS_METHOD_NOARG("clear", clear), + UBUS_METHOD("remove_device", remove_device, dev_policy) +}; struct ubus_object_type dynamic_object_type = UBUS_OBJECT_TYPE("dongle", dynamic_object_methods); @@ -547,7 +556,7 @@ struct ubus_object *dongle_create_dynamic_object(struct device *dev_instance) char dynamic_dongle_name[DYNAMIC_OBJ_NAME_SIZE]; //create dynmaic object name.. - snprintf(dynamic_dongle_name, DYNAMIC_OBJ_NAME_SIZE, "dongle.%s", dev_instance->usb.product_id); + snprintf(dynamic_dongle_name, DYNAMIC_OBJ_NAME_SIZE, "dongle.%s", dev_instance->usb.if_name); printf("dynamic_dongle_name: %s\n", dynamic_dongle_name); //create ubus object instance.. diff --git a/dongle_infrastructure.h b/dongle_infrastructure.h index 90b1464..b3a6512 100644 --- a/dongle_infrastructure.h +++ b/dongle_infrastructure.h @@ -12,6 +12,7 @@ struct USB { }; struct device { + struct ubus_object *ubus_obj; struct list_head list; struct USB usb; char *ip; -- GitLab