2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
24 static struct blob_buf buf;
25 static const struct iwinfo_ops *iw;
26 static const char *ifname;
33 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
34 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
39 rpc_iwinfo_open(struct blob_attr *msg)
41 static struct blob_attr *tb[__RPC_D_MAX];
43 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
44 blob_data(msg), blob_len(msg));
46 if (!tb[RPC_D_DEVICE])
47 return UBUS_STATUS_INVALID_ARGUMENT;
49 ifname = blobmsg_data(tb[RPC_D_DEVICE]);
50 iw = iwinfo_backend(ifname);
52 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
56 rpc_iwinfo_close(void)
64 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
69 if (!func(ifname, &rv))
72 blobmsg_add_u32(&buf, name, rv);
74 blobmsg_add_string(&buf, name, map[rv]);
79 rpc_iwinfo_call_hardware_id(const char *name)
81 struct iwinfo_hardware_id ids;
84 if (!iw->hardware_id(ifname, (char *)&ids))
86 c = blobmsg_open_array(&buf, name);
88 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
89 blobmsg_add_u32(&buf, NULL, ids.device_id);
90 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
91 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
93 blobmsg_close_array(&buf, c);
98 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
103 c = blobmsg_open_table(&buf, name);
105 blobmsg_add_u8(&buf, "enabled", e->enabled);
111 d = blobmsg_open_array(&buf, "wep");
113 if (e->auth_algs & IWINFO_AUTH_OPEN)
114 blobmsg_add_string(&buf, NULL, "open");
116 if (e->auth_algs & IWINFO_AUTH_SHARED)
117 blobmsg_add_string(&buf, NULL, "shared");
119 blobmsg_close_array(&buf, d);
123 d = blobmsg_open_array(&buf, "wpa");
125 if (e->wpa_version > 2)
127 blobmsg_add_u32(&buf, NULL, 1);
128 blobmsg_add_u32(&buf, NULL, 2);
132 blobmsg_add_u32(&buf, NULL, e->wpa_version);
135 blobmsg_close_array(&buf, d);
138 d = blobmsg_open_array(&buf, "authentication");
140 if (e->auth_suites & IWINFO_KMGMT_PSK)
141 blobmsg_add_string(&buf, NULL, "psk");
143 if (e->auth_suites & IWINFO_KMGMT_8021x)
144 blobmsg_add_string(&buf, NULL, "802.1x");
146 if (!e->auth_suites ||
147 (e->auth_suites & IWINFO_KMGMT_NONE))
148 blobmsg_add_string(&buf, NULL, "none");
150 blobmsg_close_array(&buf, d);
153 d = blobmsg_open_array(&buf, "ciphers");
154 ciph = e->pair_ciphers | e->group_ciphers;
156 if (ciph & IWINFO_CIPHER_WEP40)
157 blobmsg_add_string(&buf, NULL, "wep-40");
159 if (ciph & IWINFO_CIPHER_WEP104)
160 blobmsg_add_string(&buf, NULL, "wep-104");
162 if (ciph & IWINFO_CIPHER_TKIP)
163 blobmsg_add_string(&buf, NULL, "tkip");
165 if (ciph & IWINFO_CIPHER_CCMP)
166 blobmsg_add_string(&buf, NULL, "ccmp");
168 if (ciph & IWINFO_CIPHER_WRAP)
169 blobmsg_add_string(&buf, NULL, "wrap");
171 if (ciph & IWINFO_CIPHER_AESOCB)
172 blobmsg_add_string(&buf, NULL, "aes-ocb");
174 if (ciph & IWINFO_CIPHER_CKIP)
175 blobmsg_add_string(&buf, NULL, "ckip");
177 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
178 blobmsg_add_string(&buf, NULL, "none");
180 blobmsg_close_array(&buf, d);
183 blobmsg_close_table(&buf, c);
187 rpc_iwinfo_call_encryption(const char *name)
189 struct iwinfo_crypto_entry crypto = { 0 };
191 if (!iw->encryption(ifname, (char *)&crypto))
192 rpc_iwinfo_add_encryption(name, &crypto);
196 rpc_iwinfo_call_hwmodes(const char *name)
201 if (!iw->hwmodelist(ifname, &modes))
203 c = blobmsg_open_array(&buf, name);
205 if (modes & IWINFO_80211_A)
206 blobmsg_add_string(&buf, NULL, "a");
208 if (modes & IWINFO_80211_B)
209 blobmsg_add_string(&buf, NULL, "b");
211 if (modes & IWINFO_80211_G)
212 blobmsg_add_string(&buf, NULL, "g");
214 if (modes & IWINFO_80211_N)
215 blobmsg_add_string(&buf, NULL, "n");
217 blobmsg_close_array(&buf, c);
222 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
224 char rv[IWINFO_BUFSIZE] = { 0 };
226 if (!func(ifname, rv))
227 blobmsg_add_string(&buf, name, rv);
231 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
232 struct ubus_request_data *req, const char *method,
233 struct blob_attr *msg)
238 rv = rpc_iwinfo_open(msg);
243 blob_buf_init(&buf, 0);
245 rpc_iwinfo_call_str("phy", iw->phyname);
247 rpc_iwinfo_call_str("ssid", iw->ssid);
248 rpc_iwinfo_call_str("bssid", iw->bssid);
249 rpc_iwinfo_call_str("country", iw->country);
251 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
252 rpc_iwinfo_call_int("channel", iw->channel, NULL);
254 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
255 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
257 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
258 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
260 rpc_iwinfo_call_int("quality", iw->quality, NULL);
261 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
263 rpc_iwinfo_call_int("signal", iw->signal, NULL);
264 rpc_iwinfo_call_int("noise", iw->noise, NULL);
266 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
268 rpc_iwinfo_call_encryption("encryption");
269 rpc_iwinfo_call_hwmodes("hwmodes");
271 c = blobmsg_open_table(&buf, "hardware");
272 rpc_iwinfo_call_hardware_id("id");
273 rpc_iwinfo_call_str("name", iw->hardware_name);
274 blobmsg_close_table(&buf, c);
276 ubus_send_reply(ctx, req, buf.head);
280 return UBUS_STATUS_OK;
284 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
285 struct ubus_request_data *req, const char *method,
286 struct blob_attr *msg)
291 char res[IWINFO_BUFSIZE];
292 struct iwinfo_scanlist_entry *e;
294 rv = rpc_iwinfo_open(msg);
299 blob_buf_init(&buf, 0);
301 c = blobmsg_open_array(&buf, "results");
303 if (!iw->scanlist(ifname, res, &len) && (len > 0))
305 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
307 e = (struct iwinfo_scanlist_entry *)&res[i];
308 d = blobmsg_open_table(&buf, NULL);
311 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
313 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
314 e->mac[0], e->mac[1], e->mac[2],
315 e->mac[3], e->mac[4], e->mac[5]);
317 blobmsg_add_string(&buf, "bssid", mac);
319 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
321 blobmsg_add_u32(&buf, "channel", e->channel);
322 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
324 blobmsg_add_u32(&buf, "quality", e->quality);
325 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
327 rpc_iwinfo_add_encryption("encryption", &e->crypto);
329 blobmsg_close_table(&buf, d);
333 blobmsg_close_array(&buf, c);
335 ubus_send_reply(ctx, req, buf.head);
339 return UBUS_STATUS_OK;
343 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
344 struct ubus_request_data *req, const char *method,
345 struct blob_attr *msg)
349 char res[IWINFO_BUFSIZE];
350 struct iwinfo_assoclist_entry *a;
353 rv = rpc_iwinfo_open(msg);
358 blob_buf_init(&buf, 0);
360 c = blobmsg_open_array(&buf, "results");
362 if (!iw->assoclist(ifname, res, &len) && (len > 0))
364 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
366 a = (struct iwinfo_assoclist_entry *)&res[i];
367 d = blobmsg_open_table(&buf, NULL);
369 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
370 a->mac[0], a->mac[1], a->mac[2],
371 a->mac[3], a->mac[4], a->mac[5]);
373 blobmsg_add_string(&buf, "mac", mac);
374 blobmsg_add_u32(&buf, "signal", a->signal);
375 blobmsg_add_u32(&buf, "noise", a->noise);
376 blobmsg_add_u32(&buf, "inactive", a->inactive);
378 e = blobmsg_open_table(&buf, "rx");
379 blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
380 blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
381 blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
382 blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
383 blobmsg_close_table(&buf, e);
385 e = blobmsg_open_table(&buf, "tx");
386 blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
387 blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
388 blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
389 blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
390 blobmsg_close_table(&buf, e);
392 blobmsg_close_table(&buf, d);
396 blobmsg_close_array(&buf, c);
398 ubus_send_reply(ctx, req, buf.head);
402 return UBUS_STATUS_OK;
406 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
407 struct ubus_request_data *req, const char *method,
408 struct blob_attr *msg)
411 char res[IWINFO_BUFSIZE];
412 struct iwinfo_freqlist_entry *f;
415 rv = rpc_iwinfo_open(msg);
420 blob_buf_init(&buf, 0);
422 c = blobmsg_open_array(&buf, "results");
424 if (!iw->freqlist(ifname, res, &len) && (len > 0))
426 if (iw->channel(ifname, &ch))
429 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
431 f = (struct iwinfo_freqlist_entry *)&res[i];
432 d = blobmsg_open_table(&buf, NULL);
434 blobmsg_add_u32(&buf, "channel", f->channel);
435 blobmsg_add_u32(&buf, "mhz", f->mhz);
436 blobmsg_add_u8(&buf, "restricted", f->restricted);
439 blobmsg_add_u8(&buf, "active", f->channel == ch);
441 blobmsg_close_table(&buf, d);
445 blobmsg_close_array(&buf, c);
447 ubus_send_reply(ctx, req, buf.head);
451 return UBUS_STATUS_OK;
455 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
456 struct ubus_request_data *req, const char *method,
457 struct blob_attr *msg)
459 int i, rv, len, pwr, off;
460 char res[IWINFO_BUFSIZE];
461 struct iwinfo_txpwrlist_entry *t;
464 rv = rpc_iwinfo_open(msg);
469 blob_buf_init(&buf, 0);
471 c = blobmsg_open_array(&buf, "results");
473 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
475 if (iw->txpower(ifname, &pwr))
478 if (iw->txpower_offset(ifname, &off))
481 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
483 t = (struct iwinfo_txpwrlist_entry *)&res[i];
484 d = blobmsg_open_table(&buf, NULL);
486 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
487 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
490 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
492 blobmsg_close_table(&buf, d);
496 blobmsg_close_array(&buf, c);
498 ubus_send_reply(ctx, req, buf.head);
502 return UBUS_STATUS_OK;
506 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
509 static char ccode[5];
510 struct iwinfo_country_entry *c;
512 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
514 c = (struct iwinfo_country_entry *)&buf[i];
516 if (c->iso3166 == iso3166)
518 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
527 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
528 struct ubus_request_data *req, const char *method,
529 struct blob_attr *msg)
534 char res[IWINFO_BUFSIZE];
536 const struct iwinfo_iso3166_label *l;
539 rv = rpc_iwinfo_open(msg);
544 blob_buf_init(&buf, 0);
546 c = blobmsg_open_array(&buf, "results");
548 if (!iw->countrylist(ifname, res, &len) && (len > 0))
550 if (iw->country(ifname, cur))
551 memset(cur, 0, sizeof(cur));
553 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
555 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
560 d = blobmsg_open_table(&buf, NULL);
562 blobmsg_add_string(&buf, "code", ccode);
563 blobmsg_add_string(&buf, "country", (const char *)l->name);
565 snprintf(iso3166, sizeof(iso3166), "%c%c",
566 (l->iso3166 / 256), (l->iso3166 % 256));
568 blobmsg_add_string(&buf, "iso3166", iso3166);
571 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
573 blobmsg_close_table(&buf, d);
577 blobmsg_close_array(&buf, c);
579 ubus_send_reply(ctx, req, buf.head);
583 return UBUS_STATUS_OK;
587 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
588 struct ubus_request_data *req, const char *method,
589 struct blob_attr *msg)
595 d = opendir("/sys/class/net");
598 return UBUS_STATUS_UNKNOWN_ERROR;
600 blob_buf_init(&buf, 0);
602 c = blobmsg_open_array(&buf, "devices");
604 while ((e = readdir(d)) != NULL)
606 if (e->d_type != DT_LNK)
609 if (iwinfo_type(e->d_name))
610 blobmsg_add_string(&buf, NULL, e->d_name);
613 blobmsg_close_array(&buf, c);
617 ubus_send_reply(ctx, req, buf.head);
621 return UBUS_STATUS_OK;
625 int rpc_iwinfo_api_init(struct ubus_context *ctx)
627 static const struct ubus_method iwinfo_methods[] = {
628 { .name = "devices", .handler = rpc_iwinfo_devices },
629 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
630 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
631 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_device_policy),
632 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
633 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
634 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
637 static struct ubus_object_type iwinfo_type =
638 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
640 static struct ubus_object obj = {
642 .type = &iwinfo_type,
643 .methods = iwinfo_methods,
644 .n_methods = ARRAY_SIZE(iwinfo_methods),
647 return ubus_add_object(ctx, &obj);