+ blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
+
+ blobmsg_add_u32(&buf, "channel", e->channel);
+ blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
+
+ blobmsg_add_u32(&buf, "quality", e->quality);
+ blobmsg_add_u32(&buf, "quality_max", e->quality_max);
+
+ rpc_iwinfo_add_encryption("encryption", &e->crypto);
+
+ blobmsg_close_table(&buf, d);
+ }
+ }
+
+ blobmsg_close_array(&buf, c);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+static int
+rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ int i, rv, len;
+ char mac[18];
+ char res[IWINFO_BUFSIZE];
+ struct iwinfo_assoclist_entry *a;
+ struct ether_addr *macaddr = NULL;
+ void *c, *d, *e;
+ struct blob_attr *tb[__RPC_A_MAX];
+ bool found = false;
+
+ blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
+ if (rv)
+ return rv;
+
+ if (tb[RPC_A_MACADDR])
+ macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
+
+ blob_buf_init(&buf, 0);
+
+ if (!macaddr)
+ c = blobmsg_open_array(&buf, "results");
+
+ if (!iw->assoclist(ifname, res, &len) && (len > 0))
+ {
+ for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
+ {
+ a = (struct iwinfo_assoclist_entry *)&res[i];
+
+ if (!macaddr)
+ d = blobmsg_open_table(&buf, NULL);
+ else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
+ continue;
+
+ snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
+ a->mac[0], a->mac[1], a->mac[2],
+ a->mac[3], a->mac[4], a->mac[5]);
+
+ blobmsg_add_string(&buf, "mac", mac);
+ blobmsg_add_u32(&buf, "signal", a->signal);
+ blobmsg_add_u32(&buf, "noise", a->noise);
+ blobmsg_add_u32(&buf, "inactive", a->inactive);
+
+ e = blobmsg_open_table(&buf, "rx");
+ blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
+ blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
+ blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
+ blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
+ blobmsg_close_table(&buf, e);
+
+ e = blobmsg_open_table(&buf, "tx");
+ blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
+ blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
+ blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
+ blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
+ blobmsg_close_table(&buf, e);
+
+ found = true;
+ if (!macaddr)
+ blobmsg_close_table(&buf, d);
+ else
+ break;
+ }
+ }
+
+ if (!macaddr)
+ blobmsg_close_array(&buf, c);
+ else if (!found)
+ return UBUS_STATUS_NOT_FOUND;
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+static int
+rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ int i, rv, len, ch;
+ char res[IWINFO_BUFSIZE];
+ struct iwinfo_freqlist_entry *f;
+ void *c, *d;
+
+ rv = rpc_iwinfo_open(msg);
+
+ if (rv)
+ return rv;
+
+ blob_buf_init(&buf, 0);
+
+ c = blobmsg_open_array(&buf, "results");
+
+ if (!iw->freqlist(ifname, res, &len) && (len > 0))
+ {
+ if (iw->channel(ifname, &ch))
+ ch = -1;
+
+ for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
+ {
+ f = (struct iwinfo_freqlist_entry *)&res[i];
+ d = blobmsg_open_table(&buf, NULL);
+
+ blobmsg_add_u32(&buf, "channel", f->channel);
+ blobmsg_add_u32(&buf, "mhz", f->mhz);
+ blobmsg_add_u8(&buf, "restricted", f->restricted);
+
+ if (ch > -1)
+ blobmsg_add_u8(&buf, "active", f->channel == ch);
+
+ blobmsg_close_table(&buf, d);
+ }
+ }
+
+ blobmsg_close_array(&buf, c);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+static int
+rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ int i, rv, len, pwr, off;
+ char res[IWINFO_BUFSIZE];
+ struct iwinfo_txpwrlist_entry *t;
+ void *c, *d;
+
+ rv = rpc_iwinfo_open(msg);
+
+ if (rv)
+ return rv;
+
+ blob_buf_init(&buf, 0);
+
+ c = blobmsg_open_array(&buf, "results");
+
+ if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
+ {
+ if (iw->txpower(ifname, &pwr))
+ pwr = -1;
+
+ if (iw->txpower_offset(ifname, &off))
+ off = 0;
+
+ for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
+ {
+ t = (struct iwinfo_txpwrlist_entry *)&res[i];
+ d = blobmsg_open_table(&buf, NULL);
+
+ blobmsg_add_u32(&buf, "dbm", t->dbm + off);
+ blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
+
+ if (pwr > -1)
+ blobmsg_add_u8(&buf, "active", t->dbm == pwr);
+
+ blobmsg_close_table(&buf, d);
+ }
+ }
+
+ blobmsg_close_array(&buf, c);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+static const char *
+rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
+{
+ int i;
+ static char ccode[5];
+ struct iwinfo_country_entry *c;
+
+ for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
+ {
+ c = (struct iwinfo_country_entry *)&buf[i];
+
+ if (c->iso3166 == iso3166)
+ {
+ snprintf(ccode, sizeof(ccode), "%s", c->ccode);
+ return ccode;
+ }
+ }
+
+ return NULL;
+}
+
+static int
+rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ int rv, len;
+ char cur[3];
+ char iso3166[3];
+ char res[IWINFO_BUFSIZE] = {0};
+ const char *ccode;
+ const struct iwinfo_iso3166_label *l;
+ void *c, *d;
+
+ rv = rpc_iwinfo_open(msg);
+
+ if (rv)
+ return rv;
+
+ blob_buf_init(&buf, 0);
+
+ c = blobmsg_open_array(&buf, "results");
+
+ if (!iw->countrylist(ifname, res, &len) && (len > 0))
+ {
+ if (iw->country(ifname, cur))
+ memset(cur, 0, sizeof(cur));
+
+ for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
+ {
+ ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
+
+ if (!ccode)
+ continue;
+
+ d = blobmsg_open_table(&buf, NULL);
+
+ blobmsg_add_string(&buf, "code", ccode);
+ blobmsg_add_string(&buf, "country", (const char *)l->name);
+
+ snprintf(iso3166, sizeof(iso3166), "%c%c",
+ (l->iso3166 / 256), (l->iso3166 % 256));
+
+ blobmsg_add_string(&buf, "iso3166", iso3166);
+
+ if (cur[0])
+ blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
+
+ blobmsg_close_table(&buf, d);
+ }
+ }
+
+ blobmsg_close_array(&buf, c);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+static int
+rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ int i;
+ bool found = false;
+ char res[IWINFO_BUFSIZE];
+ const struct iwinfo_ops *ops;
+ struct blob_attr *tb[__RPC_U_MAX];
+ const char *backends[] = {
+ "nl80211",
+ "madwifi",
+ "wl"
+ };
+
+ blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (!tb[RPC_U_SECTION])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ for (i = 0; i < ARRAY_SIZE(backends); i++)
+ {
+ ops = iwinfo_backend_by_name(backends[i]);
+
+ if (!ops || !ops->lookup_phy)
+ continue;
+
+ if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ {
+ blob_buf_init(&buf, 0);
+ blobmsg_add_string(&buf, "phyname", res);
+
+ ubus_send_reply(ctx, req, buf.head);
+ }
+
+ rpc_iwinfo_close();
+
+ return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
+}
+
+static int
+rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ void *c;
+ struct dirent *e;
+ DIR *d;
+
+ d = opendir("/sys/class/net");
+
+ if (!d)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ blob_buf_init(&buf, 0);
+
+ c = blobmsg_open_array(&buf, "devices");
+
+ while ((e = readdir(d)) != NULL)
+ {
+ if (e->d_type != DT_LNK)
+ continue;
+
+ if (iwinfo_type(e->d_name))
+ blobmsg_add_string(&buf, NULL, e->d_name);
+ }
+
+ blobmsg_close_array(&buf, c);
+
+ closedir(d);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ rpc_iwinfo_close();
+
+ return UBUS_STATUS_OK;
+}
+
+
+static int
+rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)