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.
22 static struct blob_buf buf;
23 static const struct iwinfo_ops *iw;
24 static const char *ifname;
31 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
32 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
37 rpc_iwinfo_open(struct blob_attr *msg)
39 static struct blob_attr *tb[__RPC_D_MAX];
41 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
42 blob_data(msg), blob_len(msg));
44 if (!tb[RPC_D_DEVICE])
45 return UBUS_STATUS_INVALID_ARGUMENT;
47 ifname = blobmsg_data(tb[RPC_D_DEVICE]);
48 iw = iwinfo_backend(ifname);
50 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
54 rpc_iwinfo_close(void)
62 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
67 if (!func(ifname, &rv))
70 blobmsg_add_u32(&buf, name, rv);
72 blobmsg_add_string(&buf, name, map[rv]);
77 rpc_iwinfo_call_hardware_id(const char *name)
79 struct iwinfo_hardware_id ids;
82 if (!iw->hardware_id(ifname, (char *)&ids))
84 c = blobmsg_open_array(&buf, name);
86 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
87 blobmsg_add_u32(&buf, NULL, ids.device_id);
88 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
89 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
91 blobmsg_close_array(&buf, c);
96 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
101 c = blobmsg_open_table(&buf, name);
103 blobmsg_add_u8(&buf, "enabled", e->enabled);
109 d = blobmsg_open_array(&buf, "wep");
111 if (e->auth_algs & IWINFO_AUTH_OPEN)
112 blobmsg_add_string(&buf, NULL, "open");
114 if (e->auth_algs & IWINFO_AUTH_SHARED)
115 blobmsg_add_string(&buf, NULL, "shared");
117 blobmsg_close_array(&buf, d);
121 d = blobmsg_open_array(&buf, "wpa");
123 if (e->wpa_version > 2)
125 blobmsg_add_u32(&buf, NULL, 1);
126 blobmsg_add_u32(&buf, NULL, 2);
130 blobmsg_add_u32(&buf, NULL, e->wpa_version);
133 blobmsg_close_array(&buf, d);
136 d = blobmsg_open_array(&buf, "authentication");
138 if (e->auth_suites & IWINFO_KMGMT_PSK)
139 blobmsg_add_string(&buf, NULL, "psk");
141 if (e->auth_suites & IWINFO_KMGMT_8021x)
142 blobmsg_add_string(&buf, NULL, "802.1x");
144 if (!e->auth_suites ||
145 (e->auth_suites & IWINFO_KMGMT_NONE))
146 blobmsg_add_string(&buf, NULL, "none");
148 blobmsg_close_array(&buf, d);
151 d = blobmsg_open_array(&buf, "ciphers");
152 ciph = e->pair_ciphers | e->group_ciphers;
154 if (ciph & IWINFO_CIPHER_WEP40)
155 blobmsg_add_string(&buf, NULL, "wep-40");
157 if (ciph & IWINFO_CIPHER_WEP104)
158 blobmsg_add_string(&buf, NULL, "wep-104");
160 if (ciph & IWINFO_CIPHER_TKIP)
161 blobmsg_add_string(&buf, NULL, "tkip");
163 if (ciph & IWINFO_CIPHER_CCMP)
164 blobmsg_add_string(&buf, NULL, "ccmp");
166 if (ciph & IWINFO_CIPHER_WRAP)
167 blobmsg_add_string(&buf, NULL, "wrap");
169 if (ciph & IWINFO_CIPHER_AESOCB)
170 blobmsg_add_string(&buf, NULL, "aes-ocb");
172 if (ciph & IWINFO_CIPHER_CKIP)
173 blobmsg_add_string(&buf, NULL, "ckip");
175 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
176 blobmsg_add_string(&buf, NULL, "none");
178 blobmsg_close_array(&buf, d);
181 blobmsg_close_table(&buf, c);
185 rpc_iwinfo_call_encryption(const char *name)
187 struct iwinfo_crypto_entry crypto = { 0 };
189 if (!iw->encryption(ifname, (char *)&crypto))
190 rpc_iwinfo_add_encryption(name, &crypto);
194 rpc_iwinfo_call_hwmodes(const char *name)
199 if (!iw->hwmodelist(ifname, &modes))
201 c = blobmsg_open_array(&buf, name);
203 if (modes & IWINFO_80211_A)
204 blobmsg_add_string(&buf, NULL, "a");
206 if (modes & IWINFO_80211_B)
207 blobmsg_add_string(&buf, NULL, "b");
209 if (modes & IWINFO_80211_G)
210 blobmsg_add_string(&buf, NULL, "g");
212 if (modes & IWINFO_80211_N)
213 blobmsg_add_string(&buf, NULL, "n");
215 blobmsg_close_array(&buf, c);
220 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
222 char rv[IWINFO_BUFSIZE] = { 0 };
224 if (!func(ifname, rv))
225 blobmsg_add_string(&buf, name, rv);
229 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
230 struct ubus_request_data *req, const char *method,
231 struct blob_attr *msg)
236 rv = rpc_iwinfo_open(msg);
241 blob_buf_init(&buf, 0);
243 rpc_iwinfo_call_str("phy", iw->phyname);
245 rpc_iwinfo_call_str("ssid", iw->ssid);
246 rpc_iwinfo_call_str("bssid", iw->bssid);
247 rpc_iwinfo_call_str("country", iw->country);
249 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
250 rpc_iwinfo_call_int("channel", iw->channel, NULL);
252 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
253 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
255 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
256 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
258 rpc_iwinfo_call_int("quality", iw->quality, NULL);
259 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
261 rpc_iwinfo_call_int("signal", iw->signal, NULL);
262 rpc_iwinfo_call_int("noise", iw->noise, NULL);
264 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
266 rpc_iwinfo_call_encryption("encryption");
267 rpc_iwinfo_call_hwmodes("hwmodes");
269 c = blobmsg_open_table(&buf, "hardware");
270 rpc_iwinfo_call_hardware_id("id");
271 rpc_iwinfo_call_str("name", iw->hardware_name);
272 blobmsg_close_table(&buf, c);
274 ubus_send_reply(ctx, req, buf.head);
278 return UBUS_STATUS_OK;
282 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
283 struct ubus_request_data *req, const char *method,
284 struct blob_attr *msg)
289 char res[IWINFO_BUFSIZE];
290 struct iwinfo_scanlist_entry *e;
292 rv = rpc_iwinfo_open(msg);
297 blob_buf_init(&buf, 0);
299 c = blobmsg_open_array(&buf, "results");
301 if (!iw->scanlist(ifname, res, &len) && (len > 0))
303 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
305 e = (struct iwinfo_scanlist_entry *)&res[i];
306 d = blobmsg_open_table(&buf, NULL);
309 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
311 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
312 e->mac[0], e->mac[1], e->mac[2],
313 e->mac[3], e->mac[4], e->mac[5]);
315 blobmsg_add_string(&buf, "bssid", mac);
317 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
319 blobmsg_add_u32(&buf, "channel", e->channel);
320 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
322 blobmsg_add_u32(&buf, "quality", e->quality);
323 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
325 rpc_iwinfo_add_encryption("encryption", &e->crypto);
327 blobmsg_close_table(&buf, d);
331 blobmsg_close_array(&buf, c);
333 ubus_send_reply(ctx, req, buf.head);
337 return UBUS_STATUS_OK;
341 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
342 struct ubus_request_data *req, const char *method,
343 struct blob_attr *msg)
347 char res[IWINFO_BUFSIZE];
348 struct iwinfo_assoclist_entry *a;
351 rv = rpc_iwinfo_open(msg);
356 blob_buf_init(&buf, 0);
358 c = blobmsg_open_array(&buf, "results");
360 if (!iw->assoclist(ifname, res, &len) && (len > 0))
362 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
364 a = (struct iwinfo_assoclist_entry *)&res[i];
365 d = blobmsg_open_table(&buf, NULL);
367 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
368 a->mac[0], a->mac[1], a->mac[2],
369 a->mac[3], a->mac[4], a->mac[5]);
371 blobmsg_add_string(&buf, "mac", mac);
372 blobmsg_add_u32(&buf, "signal", a->signal);
373 blobmsg_add_u32(&buf, "noise", a->noise);
374 blobmsg_add_u32(&buf, "inactive", a->inactive);
376 e = blobmsg_open_table(&buf, "rx");
377 blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
378 blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
379 blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
380 blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
381 blobmsg_close_table(&buf, e);
383 e = blobmsg_open_table(&buf, "tx");
384 blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
385 blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
386 blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
387 blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
388 blobmsg_close_table(&buf, e);
390 blobmsg_close_table(&buf, d);
394 blobmsg_close_array(&buf, c);
396 ubus_send_reply(ctx, req, buf.head);
400 return UBUS_STATUS_OK;
404 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
405 struct ubus_request_data *req, const char *method,
406 struct blob_attr *msg)
409 char res[IWINFO_BUFSIZE];
410 struct iwinfo_freqlist_entry *f;
413 rv = rpc_iwinfo_open(msg);
418 blob_buf_init(&buf, 0);
420 c = blobmsg_open_array(&buf, "results");
422 if (!iw->freqlist(ifname, res, &len) && (len > 0))
424 if (iw->channel(ifname, &ch))
427 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
429 f = (struct iwinfo_freqlist_entry *)&res[i];
430 d = blobmsg_open_table(&buf, NULL);
432 blobmsg_add_u32(&buf, "channel", f->channel);
433 blobmsg_add_u32(&buf, "mhz", f->mhz);
434 blobmsg_add_u8(&buf, "restricted", f->restricted);
437 blobmsg_add_u8(&buf, "active", f->channel == ch);
439 blobmsg_close_table(&buf, d);
443 blobmsg_close_array(&buf, c);
445 ubus_send_reply(ctx, req, buf.head);
449 return UBUS_STATUS_OK;
453 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
454 struct ubus_request_data *req, const char *method,
455 struct blob_attr *msg)
457 int i, rv, len, pwr, off;
458 char res[IWINFO_BUFSIZE];
459 struct iwinfo_txpwrlist_entry *t;
462 rv = rpc_iwinfo_open(msg);
467 blob_buf_init(&buf, 0);
469 c = blobmsg_open_array(&buf, "results");
471 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
473 if (iw->txpower(ifname, &pwr))
476 if (iw->txpower_offset(ifname, &off))
479 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
481 t = (struct iwinfo_txpwrlist_entry *)&res[i];
482 d = blobmsg_open_table(&buf, NULL);
484 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
485 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
488 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
490 blobmsg_close_table(&buf, d);
494 blobmsg_close_array(&buf, c);
496 ubus_send_reply(ctx, req, buf.head);
500 return UBUS_STATUS_OK;
504 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
507 static char ccode[5];
508 struct iwinfo_country_entry *c;
510 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
512 c = (struct iwinfo_country_entry *)&buf[i];
514 if (c->iso3166 == iso3166)
516 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
525 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
526 struct ubus_request_data *req, const char *method,
527 struct blob_attr *msg)
532 char res[IWINFO_BUFSIZE];
534 const struct iwinfo_iso3166_label *l;
537 rv = rpc_iwinfo_open(msg);
542 blob_buf_init(&buf, 0);
544 c = blobmsg_open_array(&buf, "results");
546 if (!iw->countrylist(ifname, res, &len) && (len > 0))
548 if (iw->country(ifname, cur))
549 memset(cur, 0, sizeof(cur));
551 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
553 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
558 d = blobmsg_open_table(&buf, NULL);
560 blobmsg_add_string(&buf, "code", ccode);
561 blobmsg_add_string(&buf, "country", (const char *)l->name);
563 snprintf(iso3166, sizeof(iso3166), "%c%c",
564 (l->iso3166 / 256), (l->iso3166 % 256));
566 blobmsg_add_string(&buf, "iso3166", iso3166);
569 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
571 blobmsg_close_table(&buf, d);
575 blobmsg_close_array(&buf, c);
577 ubus_send_reply(ctx, req, buf.head);
581 return UBUS_STATUS_OK;
585 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
586 struct ubus_request_data *req, const char *method,
587 struct blob_attr *msg)
593 d = opendir("/sys/class/net");
596 return UBUS_STATUS_UNKNOWN_ERROR;
598 blob_buf_init(&buf, 0);
600 c = blobmsg_open_array(&buf, "devices");
602 while ((e = readdir(d)) != NULL)
604 if (e->d_type != DT_LNK)
607 if (iwinfo_type(e->d_name))
608 blobmsg_add_string(&buf, NULL, e->d_name);
611 blobmsg_close_array(&buf, c);
615 ubus_send_reply(ctx, req, buf.head);
619 return UBUS_STATUS_OK;
624 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
626 static const struct ubus_method iwinfo_methods[] = {
627 { .name = "devices", .handler = rpc_iwinfo_devices },
628 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
629 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
630 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_device_policy),
631 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
632 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
633 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
636 static struct ubus_object_type iwinfo_type =
637 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
639 static struct ubus_object obj = {
641 .type = &iwinfo_type,
642 .methods = iwinfo_methods,
643 .n_methods = ARRAY_SIZE(iwinfo_methods),
646 return ubus_add_object(ctx, &obj);
649 const struct rpc_plugin rpc_plugin = {
650 .init = rpc_iwinfo_api_init