iwinfo: export center channel for info ubus call
[project/rpcd.git] / iwinfo.c
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <sys/types.h>
20 #include <dirent.h>
21 #include <libubus.h>
22 #include <iwinfo.h>
23 #include <iwinfo/utils.h>
24 #include <net/ethernet.h>
25
26 #ifdef linux
27 #include <netinet/ether.h>
28 #endif
29
30 #include <rpcd/plugin.h>
31
32
33 static struct blob_buf buf;
34 static const struct iwinfo_ops *iw;
35 static const char *ifname;
36
37 enum {
38 RPC_D_DEVICE,
39 __RPC_D_MAX,
40 };
41
42 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
43 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
44 };
45
46 enum {
47 RPC_A_DEVICE,
48 RPC_A_MACADDR,
49 __RPC_A_MAX,
50 };
51
52 static const struct blobmsg_policy rpc_assoclist_policy[__RPC_A_MAX] = {
53 [RPC_A_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
54 [RPC_A_MACADDR] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
55 };
56
57 enum {
58 RPC_U_SECTION,
59 __RPC_U_MAX
60 };
61
62 static const struct blobmsg_policy rpc_uci_policy[__RPC_U_MAX] = {
63 [RPC_U_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
64 };
65
66 static int
67 __rpc_iwinfo_open(struct blob_attr *device)
68 {
69 if (!device)
70 return UBUS_STATUS_INVALID_ARGUMENT;
71
72 ifname = blobmsg_data(device);
73 iw = iwinfo_backend(ifname);
74
75 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
76 }
77
78 static int
79 rpc_iwinfo_open(struct blob_attr *msg)
80 {
81 static struct blob_attr *tb[__RPC_D_MAX];
82
83 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
84 blob_data(msg), blob_len(msg));
85
86 return __rpc_iwinfo_open(tb[RPC_D_DEVICE]);
87 }
88
89 static void
90 rpc_iwinfo_close(void)
91 {
92 iw = NULL;
93 ifname = NULL;
94 iwinfo_finish();
95 }
96
97 static void
98 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
99 const char **map)
100 {
101 int rv;
102
103 if (!func(ifname, &rv))
104 {
105 if (!map)
106 blobmsg_add_u32(&buf, name, rv);
107 else
108 blobmsg_add_string(&buf, name, map[rv]);
109 }
110 }
111
112 static void
113 rpc_iwinfo_call_hardware_id(const char *name)
114 {
115 struct iwinfo_hardware_id ids;
116 void *c;
117
118 if (!iw->hardware_id(ifname, (char *)&ids))
119 {
120 c = blobmsg_open_array(&buf, name);
121
122 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
123 blobmsg_add_u32(&buf, NULL, ids.device_id);
124 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
125 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
126
127 blobmsg_close_array(&buf, c);
128 }
129 }
130
131 static void
132 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
133 {
134 int ciph, wpa_version;
135 void *c, *d;
136
137 c = blobmsg_open_table(&buf, name);
138
139 blobmsg_add_u8(&buf, "enabled", e->enabled);
140
141 if (e->enabled)
142 {
143 if (!e->wpa_version)
144 {
145 d = blobmsg_open_array(&buf, "wep");
146
147 if (e->auth_algs & IWINFO_AUTH_OPEN)
148 blobmsg_add_string(&buf, NULL, "open");
149
150 if (e->auth_algs & IWINFO_AUTH_SHARED)
151 blobmsg_add_string(&buf, NULL, "shared");
152
153 blobmsg_close_array(&buf, d);
154 }
155 else
156 {
157 d = blobmsg_open_array(&buf, "wpa");
158
159 for (wpa_version = 1; wpa_version <= 3; wpa_version++)
160 if (e->wpa_version & (1 << (wpa_version - 1)))
161 blobmsg_add_u32(&buf, NULL, wpa_version);
162
163 blobmsg_close_array(&buf, d);
164
165
166 d = blobmsg_open_array(&buf, "authentication");
167
168 if (e->auth_suites & IWINFO_KMGMT_PSK)
169 blobmsg_add_string(&buf, NULL, "psk");
170
171 if (e->auth_suites & IWINFO_KMGMT_8021x)
172 blobmsg_add_string(&buf, NULL, "802.1x");
173
174 if (e->auth_suites & IWINFO_KMGMT_SAE)
175 blobmsg_add_string(&buf, NULL, "sae");
176
177 if (e->auth_suites & IWINFO_KMGMT_OWE)
178 blobmsg_add_string(&buf, NULL, "owe");
179
180 if (!e->auth_suites ||
181 (e->auth_suites & IWINFO_KMGMT_NONE))
182 blobmsg_add_string(&buf, NULL, "none");
183
184 blobmsg_close_array(&buf, d);
185 }
186
187 d = blobmsg_open_array(&buf, "ciphers");
188 ciph = e->pair_ciphers | e->group_ciphers;
189
190 if (ciph & IWINFO_CIPHER_WEP40)
191 blobmsg_add_string(&buf, NULL, "wep-40");
192
193 if (ciph & IWINFO_CIPHER_WEP104)
194 blobmsg_add_string(&buf, NULL, "wep-104");
195
196 if (ciph & IWINFO_CIPHER_TKIP)
197 blobmsg_add_string(&buf, NULL, "tkip");
198
199 if (ciph & IWINFO_CIPHER_CCMP)
200 blobmsg_add_string(&buf, NULL, "ccmp");
201
202 if (ciph & IWINFO_CIPHER_WRAP)
203 blobmsg_add_string(&buf, NULL, "wrap");
204
205 if (ciph & IWINFO_CIPHER_AESOCB)
206 blobmsg_add_string(&buf, NULL, "aes-ocb");
207
208 if (ciph & IWINFO_CIPHER_CKIP)
209 blobmsg_add_string(&buf, NULL, "ckip");
210
211 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
212 blobmsg_add_string(&buf, NULL, "none");
213
214 blobmsg_close_array(&buf, d);
215 }
216
217 blobmsg_close_table(&buf, c);
218 }
219
220 static void
221 rpc_iwinfo_call_encryption(const char *name)
222 {
223 struct iwinfo_crypto_entry crypto = { 0 };
224
225 if (!iw->encryption(ifname, (char *)&crypto))
226 rpc_iwinfo_add_encryption(name, &crypto);
227 }
228
229 static void
230 rpc_iwinfo_call_htmodes(const char *name)
231 {
232 int modes;
233 void *c;
234
235 if (!iw->htmodelist(ifname, &modes))
236 {
237 c = blobmsg_open_array(&buf, name);
238
239 if (modes & IWINFO_HTMODE_HT20)
240 blobmsg_add_string(&buf, NULL, "HT20");
241
242 if (modes & IWINFO_HTMODE_HT40)
243 blobmsg_add_string(&buf, NULL, "HT40");
244
245 if (modes & IWINFO_HTMODE_VHT20)
246 blobmsg_add_string(&buf, NULL, "VHT20");
247
248 if (modes & IWINFO_HTMODE_VHT40)
249 blobmsg_add_string(&buf, NULL, "VHT40");
250
251 if (modes & IWINFO_HTMODE_VHT80)
252 blobmsg_add_string(&buf, NULL, "VHT80");
253
254 if (modes & IWINFO_HTMODE_VHT80_80)
255 blobmsg_add_string(&buf, NULL, "VHT80+80");
256
257 if (modes & IWINFO_HTMODE_VHT160)
258 blobmsg_add_string(&buf, NULL, "VHT160");
259
260 blobmsg_close_array(&buf, c);
261 }
262 }
263
264 static void
265 rpc_iwinfo_call_hwmodes(const char *name)
266 {
267 int modes;
268 void *c;
269
270 if (!iw->hwmodelist(ifname, &modes))
271 {
272 c = blobmsg_open_array(&buf, name);
273
274 if (modes & IWINFO_80211_AC)
275 blobmsg_add_string(&buf, NULL, "ac");
276
277 if (modes & IWINFO_80211_A)
278 blobmsg_add_string(&buf, NULL, "a");
279
280 if (modes & IWINFO_80211_B)
281 blobmsg_add_string(&buf, NULL, "b");
282
283 if (modes & IWINFO_80211_G)
284 blobmsg_add_string(&buf, NULL, "g");
285
286 if (modes & IWINFO_80211_N)
287 blobmsg_add_string(&buf, NULL, "n");
288
289 blobmsg_close_array(&buf, c);
290 }
291 }
292
293 static void rpc_iwinfo_call_hw_ht_mode()
294 {
295 const char *hwmode_str;
296 const char *htmode_str;
297 int32_t htmode = 0;
298
299 if (iw->htmode(ifname, &htmode))
300 return;
301
302 switch (htmode) {
303 case IWINFO_HTMODE_HT20:
304 htmode_str = "HT20";
305 hwmode_str = "n";
306 break;
307 case IWINFO_HTMODE_HT40:
308 htmode_str = "HT40";
309 hwmode_str = "n";
310 break;
311 case IWINFO_HTMODE_VHT80:
312 htmode_str = "VHT80";
313 hwmode_str = "ac";
314 break;
315 case IWINFO_HTMODE_VHT80_80:
316 htmode_str = "VHT80+80";
317 hwmode_str = "ac";
318 break;
319 case IWINFO_HTMODE_VHT160:
320 htmode_str = "VHT160";
321 hwmode_str = "ac";
322 break;
323 case IWINFO_HTMODE_NOHT:
324 htmode_str = "20";
325 hwmode_str = "a/g";
326 break;
327 default:
328 htmode_str = hwmode_str = "unknown";
329 break;
330 }
331 blobmsg_add_string(&buf, "hwmode", hwmode_str);
332 blobmsg_add_string(&buf, "htmode", htmode_str);
333 }
334
335 static void
336 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
337 {
338 char rv[IWINFO_BUFSIZE] = { 0 };
339
340 if (!func(ifname, rv))
341 blobmsg_add_string(&buf, name, rv);
342 }
343
344 static int
345 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
346 struct ubus_request_data *req, const char *method,
347 struct blob_attr *msg)
348 {
349 int rv;
350 void *c;
351
352 rv = rpc_iwinfo_open(msg);
353
354 if (rv)
355 return rv;
356
357 blob_buf_init(&buf, 0);
358
359 rpc_iwinfo_call_str("phy", iw->phyname);
360
361 rpc_iwinfo_call_str("ssid", iw->ssid);
362 rpc_iwinfo_call_str("bssid", iw->bssid);
363 rpc_iwinfo_call_str("country", iw->country);
364
365 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
366 rpc_iwinfo_call_int("channel", iw->channel, NULL);
367 rpc_iwinfo_call_int("center_chan1", iw->center_chan1, NULL);
368 rpc_iwinfo_call_int("center_chan2", iw->center_chan2, NULL);
369
370 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
371 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
372
373 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
374 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
375
376 rpc_iwinfo_call_int("quality", iw->quality, NULL);
377 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
378
379 rpc_iwinfo_call_int("signal", iw->signal, NULL);
380 rpc_iwinfo_call_int("noise", iw->noise, NULL);
381
382 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
383
384 rpc_iwinfo_call_encryption("encryption");
385 rpc_iwinfo_call_htmodes("htmodes");
386 rpc_iwinfo_call_hwmodes("hwmodes");
387
388 rpc_iwinfo_call_hw_ht_mode();
389
390 c = blobmsg_open_table(&buf, "hardware");
391 rpc_iwinfo_call_hardware_id("id");
392 rpc_iwinfo_call_str("name", iw->hardware_name);
393 blobmsg_close_table(&buf, c);
394
395 ubus_send_reply(ctx, req, buf.head);
396
397 rpc_iwinfo_close();
398
399 return UBUS_STATUS_OK;
400 }
401
402 static int
403 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
404 struct ubus_request_data *req, const char *method,
405 struct blob_attr *msg)
406 {
407 int i, rv, len;
408 void *c, *d, *t;
409 char mac[18];
410 char res[IWINFO_BUFSIZE];
411 struct iwinfo_scanlist_entry *e;
412
413 rv = rpc_iwinfo_open(msg);
414
415 if (rv)
416 return rv;
417
418 blob_buf_init(&buf, 0);
419
420 c = blobmsg_open_array(&buf, "results");
421
422 if (!iw->scanlist(ifname, res, &len) && (len > 0))
423 {
424 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
425 {
426 e = (struct iwinfo_scanlist_entry *)&res[i];
427 d = blobmsg_open_table(&buf, NULL);
428
429 if (e->ssid[0])
430 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
431
432 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
433 e->mac[0], e->mac[1], e->mac[2],
434 e->mac[3], e->mac[4], e->mac[5]);
435
436 blobmsg_add_string(&buf, "bssid", mac);
437
438 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
439
440 blobmsg_add_u32(&buf, "channel", e->channel);
441 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
442
443 blobmsg_add_u32(&buf, "quality", e->quality);
444 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
445
446 t = blobmsg_open_table(&buf, "ht_operation");
447 blobmsg_add_u32(&buf, "primary_channel", e->ht_chan_info.primary_chan);
448 blobmsg_add_string(&buf, "secondary_channel_offset", ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
449 blobmsg_add_u32(&buf, "channel_width", ht_chan_width[e->ht_chan_info.chan_width]);
450 blobmsg_close_table(&buf, t);
451
452 if (e->vht_chan_info.center_chan_1) {
453 t = blobmsg_open_table(&buf, "vht_operation");
454 blobmsg_add_u32(&buf, "channel_width", vht_chan_width[e->vht_chan_info.chan_width]);
455 blobmsg_add_u32(&buf, "center_freq_1", e->vht_chan_info.center_chan_1);
456 blobmsg_add_u32(&buf, "center_freq_2", e->vht_chan_info.center_chan_2);
457 blobmsg_close_table(&buf, t);
458 }
459
460 rpc_iwinfo_add_encryption("encryption", &e->crypto);
461
462 blobmsg_close_table(&buf, d);
463 }
464 }
465
466 blobmsg_close_array(&buf, c);
467
468 ubus_send_reply(ctx, req, buf.head);
469
470 rpc_iwinfo_close();
471
472 return UBUS_STATUS_OK;
473 }
474
475 static void
476 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
477 {
478 blobmsg_add_u8(&buf, "ht", r->is_ht);
479 blobmsg_add_u8(&buf, "vht", r->is_vht);
480 blobmsg_add_u32(&buf, "mhz", r->mhz);
481 blobmsg_add_u32(&buf, "rate", r->rate);
482
483 if (r->is_ht) {
484 blobmsg_add_u32(&buf, "mcs", r->mcs);
485 blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
486 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
487 }
488 else if (r->is_vht) {
489 blobmsg_add_u32(&buf, "mcs", r->mcs);
490 blobmsg_add_u32(&buf, "nss", r->nss);
491 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
492 }
493 }
494
495 static int
496 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
497 struct ubus_request_data *req, const char *method,
498 struct blob_attr *msg)
499 {
500 int i, rv, len;
501 char mac[18];
502 char res[IWINFO_BUFSIZE];
503 struct iwinfo_assoclist_entry *a;
504 struct ether_addr *macaddr = NULL;
505 void *c, *d, *e;
506 struct blob_attr *tb[__RPC_A_MAX];
507 bool found = false;
508
509 blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
510 blob_data(msg), blob_len(msg));
511
512 rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
513 if (rv)
514 return rv;
515
516 if (tb[RPC_A_MACADDR])
517 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
518
519 blob_buf_init(&buf, 0);
520
521 if (!macaddr)
522 c = blobmsg_open_array(&buf, "results");
523
524 if (!iw->assoclist(ifname, res, &len) && (len > 0))
525 {
526 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
527 {
528 a = (struct iwinfo_assoclist_entry *)&res[i];
529
530 if (!macaddr)
531 d = blobmsg_open_table(&buf, NULL);
532 else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
533 continue;
534
535 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
536 a->mac[0], a->mac[1], a->mac[2],
537 a->mac[3], a->mac[4], a->mac[5]);
538
539 blobmsg_add_string(&buf, "mac", mac);
540 blobmsg_add_u32(&buf, "signal", a->signal);
541 blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
542 blobmsg_add_u32(&buf, "noise", a->noise);
543 blobmsg_add_u32(&buf, "inactive", a->inactive);
544 blobmsg_add_u32(&buf, "connected_time", a->connected_time);
545 blobmsg_add_u32(&buf, "thr", a->thr);
546 blobmsg_add_u8(&buf, "authorized", a->is_authorized);
547 blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
548 blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
549 blobmsg_add_u8(&buf, "wme", a->is_wme);
550 blobmsg_add_u8(&buf, "mfp", a->is_mfp);
551 blobmsg_add_u8(&buf, "tdls", a->is_tdls);
552
553 blobmsg_add_u16(&buf, "mesh llid", a->llid);
554 blobmsg_add_u16(&buf, "mesh plid", a->plid);
555 blobmsg_add_string(&buf, "mesh plink", a->plink_state);
556 blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
557 blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
558 blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
559
560 e = blobmsg_open_table(&buf, "rx");
561 blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
562 blobmsg_add_u32(&buf, "packets", a->rx_packets);
563 blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
564 rpc_iwinfo_add_rateinfo(&a->rx_rate);
565 blobmsg_close_table(&buf, e);
566
567 e = blobmsg_open_table(&buf, "tx");
568 blobmsg_add_u32(&buf, "failed", a->tx_failed);
569 blobmsg_add_u32(&buf, "retries", a->tx_retries);
570 blobmsg_add_u32(&buf, "packets", a->tx_packets);
571 blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
572 rpc_iwinfo_add_rateinfo(&a->tx_rate);
573 blobmsg_close_table(&buf, e);
574
575 found = true;
576 if (!macaddr)
577 blobmsg_close_table(&buf, d);
578 else
579 break;
580 }
581 }
582
583 if (!macaddr)
584 blobmsg_close_array(&buf, c);
585 else if (!found)
586 return UBUS_STATUS_NOT_FOUND;
587
588 ubus_send_reply(ctx, req, buf.head);
589
590 rpc_iwinfo_close();
591
592 return UBUS_STATUS_OK;
593 }
594
595 static int
596 rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
597 struct ubus_request_data *req, const char *method,
598 struct blob_attr *msg)
599 {
600 char res[IWINFO_BUFSIZE];
601 struct iwinfo_survey_entry *e;
602 void *c, *d;
603 int i, rv, len;
604
605 blob_buf_init(&buf, 0);
606
607 rv = rpc_iwinfo_open(msg);
608
609 c = blobmsg_open_array(&buf, "results");
610
611 if (rv || iw->survey(ifname, res, &len) || len < 0)
612 return UBUS_STATUS_OK;
613
614 for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
615 e = (struct iwinfo_survey_entry *)&res[i];
616
617 d = blobmsg_open_table(&buf, NULL);
618 blobmsg_add_u32(&buf, "mhz", e->mhz);
619 blobmsg_add_u32(&buf, "noise", e->noise);
620 blobmsg_add_u64(&buf, "active_time", e->active_time);
621 blobmsg_add_u64(&buf, "busy_time", e->busy_time);
622 blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
623 blobmsg_add_u64(&buf, "rx_time", e->rxtime);
624 blobmsg_add_u64(&buf, "tx_time", e->txtime);
625 blobmsg_close_table(&buf, d);
626 }
627
628 blobmsg_close_array(&buf, c);
629 ubus_send_reply(ctx, req, buf.head);
630 rpc_iwinfo_close();
631 return UBUS_STATUS_OK;
632 }
633
634 static int
635 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
636 struct ubus_request_data *req, const char *method,
637 struct blob_attr *msg)
638 {
639 int i, rv, len, ch;
640 char res[IWINFO_BUFSIZE];
641 struct iwinfo_freqlist_entry *f;
642 void *c, *d;
643
644 rv = rpc_iwinfo_open(msg);
645
646 if (rv)
647 return rv;
648
649 blob_buf_init(&buf, 0);
650
651 c = blobmsg_open_array(&buf, "results");
652
653 if (!iw->freqlist(ifname, res, &len) && (len > 0))
654 {
655 if (iw->channel(ifname, &ch))
656 ch = -1;
657
658 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
659 {
660 f = (struct iwinfo_freqlist_entry *)&res[i];
661 d = blobmsg_open_table(&buf, NULL);
662
663 blobmsg_add_u32(&buf, "channel", f->channel);
664 blobmsg_add_u32(&buf, "mhz", f->mhz);
665 blobmsg_add_u8(&buf, "restricted", f->restricted);
666
667 if (ch > -1)
668 blobmsg_add_u8(&buf, "active", f->channel == ch);
669
670 blobmsg_close_table(&buf, d);
671 }
672 }
673
674 blobmsg_close_array(&buf, c);
675
676 ubus_send_reply(ctx, req, buf.head);
677
678 rpc_iwinfo_close();
679
680 return UBUS_STATUS_OK;
681 }
682
683 static int
684 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
685 struct ubus_request_data *req, const char *method,
686 struct blob_attr *msg)
687 {
688 int i, rv, len, pwr, off;
689 char res[IWINFO_BUFSIZE];
690 struct iwinfo_txpwrlist_entry *t;
691 void *c, *d;
692
693 rv = rpc_iwinfo_open(msg);
694
695 if (rv)
696 return rv;
697
698 blob_buf_init(&buf, 0);
699
700 c = blobmsg_open_array(&buf, "results");
701
702 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
703 {
704 if (iw->txpower(ifname, &pwr))
705 pwr = -1;
706
707 if (iw->txpower_offset(ifname, &off))
708 off = 0;
709
710 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
711 {
712 t = (struct iwinfo_txpwrlist_entry *)&res[i];
713 d = blobmsg_open_table(&buf, NULL);
714
715 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
716 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
717
718 if (pwr > -1)
719 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
720
721 blobmsg_close_table(&buf, d);
722 }
723 }
724
725 blobmsg_close_array(&buf, c);
726
727 ubus_send_reply(ctx, req, buf.head);
728
729 rpc_iwinfo_close();
730
731 return UBUS_STATUS_OK;
732 }
733
734 static const char *
735 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
736 {
737 int i;
738 static char ccode[5];
739 struct iwinfo_country_entry *c;
740
741 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
742 {
743 c = (struct iwinfo_country_entry *)&buf[i];
744
745 if (c->iso3166 == iso3166)
746 {
747 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
748 return ccode;
749 }
750 }
751
752 return NULL;
753 }
754
755 static int
756 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
757 struct ubus_request_data *req, const char *method,
758 struct blob_attr *msg)
759 {
760 int rv, len;
761 char cur[3];
762 char iso3166[3];
763 char res[IWINFO_BUFSIZE] = {0};
764 const char *ccode;
765 const struct iwinfo_iso3166_label *l;
766 void *c, *d;
767
768 rv = rpc_iwinfo_open(msg);
769
770 if (rv)
771 return rv;
772
773 blob_buf_init(&buf, 0);
774
775 c = blobmsg_open_array(&buf, "results");
776
777 if (!iw->countrylist(ifname, res, &len) && (len > 0))
778 {
779 if (iw->country(ifname, cur))
780 memset(cur, 0, sizeof(cur));
781
782 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
783 {
784 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
785
786 if (!ccode)
787 continue;
788
789 d = blobmsg_open_table(&buf, NULL);
790
791 blobmsg_add_string(&buf, "code", ccode);
792 blobmsg_add_string(&buf, "country", (const char *)l->name);
793
794 snprintf(iso3166, sizeof(iso3166), "%c%c",
795 (l->iso3166 / 256), (l->iso3166 % 256));
796
797 blobmsg_add_string(&buf, "iso3166", iso3166);
798
799 if (cur[0])
800 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
801
802 blobmsg_close_table(&buf, d);
803 }
804 }
805
806 blobmsg_close_array(&buf, c);
807
808 ubus_send_reply(ctx, req, buf.head);
809
810 rpc_iwinfo_close();
811
812 return UBUS_STATUS_OK;
813 }
814
815 static int
816 rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
817 struct ubus_request_data *req, const char *method,
818 struct blob_attr *msg)
819 {
820 int i;
821 bool found = false;
822 char res[IWINFO_BUFSIZE];
823 const struct iwinfo_ops *ops;
824 struct blob_attr *tb[__RPC_U_MAX];
825 const char *backends[] = {
826 "nl80211",
827 "madwifi",
828 "wl"
829 };
830
831 blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
832 blob_data(msg), blob_len(msg));
833
834 if (!tb[RPC_U_SECTION])
835 return UBUS_STATUS_INVALID_ARGUMENT;
836
837 for (i = 0; i < ARRAY_SIZE(backends); i++)
838 {
839 ops = iwinfo_backend_by_name(backends[i]);
840
841 if (!ops || !ops->lookup_phy)
842 continue;
843
844 if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
845 {
846 found = true;
847 break;
848 }
849 }
850
851 if (found)
852 {
853 blob_buf_init(&buf, 0);
854 blobmsg_add_string(&buf, "phyname", res);
855
856 ubus_send_reply(ctx, req, buf.head);
857 }
858
859 rpc_iwinfo_close();
860
861 return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
862 }
863
864 static int
865 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
866 struct ubus_request_data *req, const char *method,
867 struct blob_attr *msg)
868 {
869 void *c;
870 struct dirent *e;
871 DIR *d;
872
873 d = opendir("/sys/class/net");
874
875 if (!d)
876 return UBUS_STATUS_UNKNOWN_ERROR;
877
878 blob_buf_init(&buf, 0);
879
880 c = blobmsg_open_array(&buf, "devices");
881
882 while ((e = readdir(d)) != NULL)
883 {
884 if (e->d_type != DT_LNK)
885 continue;
886
887 if (iwinfo_type(e->d_name))
888 blobmsg_add_string(&buf, NULL, e->d_name);
889 }
890
891 blobmsg_close_array(&buf, c);
892
893 closedir(d);
894
895 ubus_send_reply(ctx, req, buf.head);
896
897 rpc_iwinfo_close();
898
899 return UBUS_STATUS_OK;
900 }
901
902
903 static int
904 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
905 {
906 static const struct ubus_method iwinfo_methods[] = {
907 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
908 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
909 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
910 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
911 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
912 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
913 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
914 UBUS_METHOD("survey", rpc_iwinfo_survey, rpc_device_policy),
915 UBUS_METHOD("phyname", rpc_iwinfo_phyname, rpc_uci_policy),
916 };
917
918 static struct ubus_object_type iwinfo_type =
919 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
920
921 static struct ubus_object obj = {
922 .name = "iwinfo",
923 .type = &iwinfo_type,
924 .methods = iwinfo_methods,
925 .n_methods = ARRAY_SIZE(iwinfo_methods),
926 };
927
928 return ubus_add_object(ctx, &obj);
929 }
930
931 struct rpc_plugin rpc_plugin = {
932 .init = rpc_iwinfo_api_init
933 };