diff --git a/dongle.c b/dongle.c index 2c855ca9d2c5265972f517ba62e6aa74349ef8c2..a573c75785e54ed1164e70d349355a4d7a4f362f 100644 --- a/dongle.c +++ b/dongle.c @@ -31,6 +31,15 @@ const struct blobmsg_policy dev_policy[__DEV_MAX] = { [DEV] = {.name = "dev", .type = BLOBMSG_TYPE_STRING}, }; +enum { + USB_PATH, + __USB_MAX +}; + +const struct blobmsg_policy usb_policy[__USB_MAX] = { + [USB_PATH] = {.name = "path", .type = BLOBMSG_TYPE_STRING}, +}; + LIST_HEAD(devices); LIST_HEAD(stack); LIST_HEAD(visited); @@ -378,6 +387,61 @@ fail_dr: return NULL; } +int get_devices_from_path(char *input_path) +{ + char product_path[PATH_MAX], path[PATH_MAX], *name; + struct dirent *de; + DIR *dir; + struct stat st; + struct device *dev; + int rv; + + //strncpy(usb_path, "/sys/bus/usb/devices/", PATH_MAX); + dir = opendir(input_path); + if (!dir) { + perror("opendir"); + goto fail_dr; + } + + while ((de = readdir(dir)) != NULL) { + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + if (strstr(de->d_name, ":") || strstr(de->d_name, "usb")) + continue; + snprintf(product_path, PATH_MAX, "%s%s/product", input_path, de->d_name); + memset(&st, 0, sizeof(st)); + rv = stat(product_path, &st); + if (rv < 0) { + //perror("stat"); + continue; + } + snprintf(path, PATH_MAX, "%s%s/", input_path, de->d_name); + name = get_device_name(path); + if (!name) + continue; + printf("found device inside _from_path %s\n", name); + dev = (struct device *)calloc(1, sizeof(*dev)); + if (!dev) { + perror("calloc"); + goto fail_dev; + } + dev->usb.product = get_usb_stat(input_path, de->d_name, "product"); + dev->usb.product_id = get_usb_stat(input_path, de->d_name, "idProduct"); + dev->usb.vendor_id = get_usb_stat(input_path, de->d_name, "idVendor"); + dev->usb.if_name = name; + dev->ip = get_device_ip(dev->usb.if_name); + + add_device(dev); + } + closedir(dir); + return 0; +fail_dev: + closedir(dir); +fail_dr: + return -1; +} + + int get_devices(void) { char usb_path[PATH_MAX], product_path[PATH_MAX], path[PATH_MAX], *name; @@ -571,6 +635,25 @@ int test(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +int alert(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__USB_MAX]; + char *usb_path; + + blobmsg_parse(dev_policy, __USB_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[USB_PATH]) + goto fail; + usb_path = (char *)blobmsg_data(tb[USB_PATH]); + + get_devices_from_path(usb_path); + return 0; +fail: + return UBUS_STATUS_INVALID_ARGUMENT; +} + int remove_device(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) @@ -595,7 +678,9 @@ struct ubus_method dongle_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)}; + UBUS_METHOD("remove_device", remove_device, dev_policy), + UBUS_METHOD("alert", alert, usb_policy) +}; struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods); diff --git a/dongle.h b/dongle.h index ce39be696a2da7c01adbcc4611bf21c6ec983744..69c42a2c66be5af10d3dd1b2aebbc93cc8cab94a 100644 --- a/dongle.h +++ b/dongle.h @@ -22,6 +22,7 @@ struct device { }; */ void uloop_add_get_devices(struct uloop_timeout *t); +int get_devices_from_path(char *input_path); int get_devices(void); int devices_status(struct uloop_timeout *t); int tag_missing_devices(void); diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index c6dfb749f3a4405e317e69cb0ae0cf7b2c2d4dfb..7bb82b8cf9183d1f225113371de7f60532be8aba 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -439,7 +439,6 @@ int show_current_apn(struct ubus_context *ctx, struct ubus_object *obj, struct json_object *response; response = mobile_get_current_apn(global_dev); - if (!response) goto fail_unknown; @@ -504,7 +503,6 @@ int modem_state(struct ubus_context *ctx, struct ubus_object *obj, struct json_object *response; response = mobile_get_modem_state_zte(global_dev->ip); - if (!response) goto fail_unknown; @@ -521,7 +519,6 @@ int enable_roaming(struct ubus_context *ctx, struct ubus_object *obj, struct json_object *response; response = mobile_enable_roaming_zte(global_dev->ip); - if (!response) goto fail_unknown; @@ -538,7 +535,6 @@ int disable_roaming(struct ubus_context *ctx, struct ubus_object *obj, struct json_object *response; response = mobile_disable_roaming_zte(global_dev->ip); - if (!response) goto fail_unknown;