netifd: Remove unnecessary default_config check in config_parse_interface
[project/netifd.git] / wireless.c
index 9a68e01..8bdfc4b 100644 (file)
@@ -51,9 +51,9 @@ static const struct uci_blob_param_list vif_param = {
 static void
 put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name)
 {
-       void *c = blobmsg_open_table(&b, name);
-       blob_put_raw(&b, blob_data(attr), blob_len(attr));
-       blobmsg_close_table(&b, c);
+       void *c = blobmsg_open_table(buf, name);
+       blob_put_raw(buf, blob_data(attr), blob_len(attr));
+       blobmsg_close_table(buf, c);
 }
 
 static void
@@ -202,6 +202,12 @@ static void wireless_interface_handle_link(struct wireless_interface *vif, bool
        if (!vif->network || !vif->ifname)
                return;
 
+       if (up) {
+               struct device *dev = device_get(vif->ifname, 2);
+               if (dev)
+                       dev->wireless = true;
+       }
+
        blobmsg_for_each_attr(cur, vif->network, rem) {
                network = blobmsg_data(cur);
 
@@ -209,7 +215,7 @@ static void wireless_interface_handle_link(struct wireless_interface *vif, bool
                if (!iface)
                        continue;
 
-               interface_handle_link(iface, vif->ifname, up);
+               interface_handle_link(iface, vif->ifname, up, true);
        }
 }
 
@@ -317,17 +323,6 @@ wireless_device_mark_down(struct wireless_device *wdev)
 }
 
 static void
-wireless_device_mark_up(struct wireless_device *wdev)
-{
-       struct wireless_interface *vif;
-
-       D(WIRELESS, "Wireless device '%s' is now up\n", wdev->name);
-       wdev->state = IFS_UP;
-       vlist_for_each_element(&wdev->interfaces, vif, node)
-               wireless_interface_handle_link(vif, true);
-}
-
-static void
 wireless_device_setup_timeout(struct uloop_timeout *timeout)
 {
        struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout);
@@ -361,6 +356,23 @@ __wireless_device_set_down(struct wireless_device *wdev)
 }
 
 static void
+wireless_device_mark_up(struct wireless_device *wdev)
+{
+       struct wireless_interface *vif;
+
+       if (wdev->cancel) {
+               wdev->cancel = false;
+               __wireless_device_set_down(wdev);
+               return;
+       }
+
+       D(WIRELESS, "Wireless device '%s' is now up\n", wdev->name);
+       wdev->state = IFS_UP;
+       vlist_for_each_element(&wdev->interfaces, vif, node)
+               wireless_interface_handle_link(vif, true);
+}
+
+static void
 wireless_device_retry_setup(struct wireless_device *wdev)
 {
        if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN || wdev->cancel)
@@ -417,7 +429,7 @@ wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new)
 
        free(wd_new);
 
-       if (blob_attr_equal(wdev->config, new_config) || wdev->disabled == disabled)
+       if (blob_attr_equal(wdev->config, new_config) && wdev->disabled == disabled)
                return;
 
        D(WIRELESS, "Update configuration of wireless device '%s'\n", wdev->name);
@@ -491,8 +503,7 @@ wireless_add_handler(const char *script, const char *name, json_object *obj)
        D(WIRELESS, "Add handler for script %s: %s\n", script, name);
 }
 
-static void __init
-wireless_init_list(void)
+void wireless_init(void)
 {
        vlist_init(&wireless_devices, avl_strcmp, wdev_update);
        wireless_devices.keep_old = true;
@@ -533,7 +544,8 @@ vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
                wdev = vif_new->wdev;
 
        if (vif_old && vif_new) {
-               vif_old->section = vif_new->section;
+               free((void *) vif_old->section);
+               vif_old->section = strdup(vif_new->section);
                if (blob_attr_equal(vif_old->config, vif_new->config)) {
                        free(vif_new);
                        return;
@@ -546,10 +558,12 @@ vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
                free(vif_new);
        } else if (vif_new) {
                D(WIRELESS, "Create new wireless interface %s on device %s\n", vif_new->name, wdev->name);
+               vif_new->section = strdup(vif_new->section);
                vif_new->config = blob_memdup(vif_new->config);
                wireless_interface_init_config(vif_new);
        } else if (vif_old) {
                D(WIRELESS, "Delete wireless interface %s on device %s\n", vif_old->name, wdev->name);
+               free((void *) vif_old->section);
                free(vif_old->config);
                free(vif_old);
        }
@@ -630,7 +644,6 @@ wireless_device_create(struct wireless_driver *drv, const char *name, struct blo
        INIT_LIST_HEAD(&wdev->script_proc);
        vlist_init(&wdev->interfaces, avl_strcmp, vif_update);
        wdev->interfaces.keep_old = true;
-       vlist_add(&wireless_devices, &wdev->node, wdev->name);
 
        wdev->timeout.cb = wireless_device_setup_timeout;
        wdev->script_task.cb = wireless_device_script_task_cb;
@@ -641,6 +654,8 @@ wireless_device_create(struct wireless_driver *drv, const char *name, struct blo
        wdev->script_proc_fd.cb = wireless_proc_poll_fd;
 
        wdev->script_check.cb = wireless_device_check_script_tasks;
+
+       vlist_add(&wireless_devices, &wdev->node, wdev->name);
 }
 
 void wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data, const char *section)
@@ -659,7 +674,8 @@ void wireless_interface_create(struct wireless_device *wdev, struct blob_attr *d
 
        sprintf(name, "%d", wdev->vif_idx++);
 
-       vif = calloc_a(sizeof(*vif), &name_buf, strlen(name) + 1);
+       vif = calloc_a(sizeof(*vif),
+                      &name_buf, strlen(name) + 1);
        vif->name = strcpy(name_buf, name);
        vif->wdev = wdev;
        vif->config = data;
@@ -677,8 +693,7 @@ wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
                blobmsg_add_string(b, "section", iface->section);
        if (iface->ifname)
                blobmsg_add_string(b, "ifname", iface->ifname);
-       if (iface->data)
-               blob_put_raw(b, blob_data(iface->data), blob_len(iface->data));
+       put_container(b, iface->config, "config");
        blobmsg_close_table(b, i);
 }
 
@@ -693,6 +708,7 @@ wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
        blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
        blobmsg_add_u8(b, "autostart", wdev->autostart);
        blobmsg_add_u8(b, "disabled", wdev->disabled);
+       put_container(b, wdev->config, "config");
 
        i = blobmsg_open_array(b, "interfaces");
        vlist_for_each_element(&wdev->interfaces, iface, node)
@@ -701,6 +717,30 @@ wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
        blobmsg_close_table(b, c);
 }
 
+void
+wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b)
+{
+       struct uci_blob_param_list *p;
+       void *c, *d;
+       int i;
+
+       c = blobmsg_open_table(b, wdev->name);
+
+       d = blobmsg_open_table(b, "device");
+       p = wdev->drv->device.config;
+       for (i = 0; i < p->n_params; i++)
+               blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
+       blobmsg_close_table(b, d);
+
+       d = blobmsg_open_table(b, "interface");
+       p = wdev->drv->interface.config;
+       for (i = 0; i < p->n_params; i++)
+               blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
+       blobmsg_close_table(b, d);
+
+       blobmsg_close_table(b, c);
+}
+
 static void
 wireless_interface_set_data(struct wireless_interface *vif)
 {
@@ -873,9 +913,6 @@ wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
                if (wdev->state != IFS_SETUP)
                        return UBUS_STATUS_PERMISSION_DENIED;
 
-               if (wdev->cancel)
-                       return 0;
-
                wireless_device_mark_up(wdev);
                break;
        case NOTIFY_CMD_SET_DATA: