diff --git a/common.h b/common.h index d051c0e3e229ea52b52bdd72304749f18d423ed8..51ee55f4fcee7f2f1eb13d1bd4852cf62e4e360d 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 a4b238a709b4f6cb8d3f799429f17eeded76e219..459c815a124047f9d7c28af0726ccf989cebd021 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 d257b1d10ca404f668e678dfda29df0c768b4a88..f346dc07a90bb4b1070da7bd488666b4f477ee6b 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 90b14640036fba74c9beb981d89aec69942093fb..b3a65123cc0532f2192c253c350ab1400fa34253 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;