iwinfo: include ht_operation data only if available
[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_GCMP)
203 blobmsg_add_string(&buf, NULL, "gcmp");
204
205 if (ciph & IWINFO_CIPHER_WRAP)
206 blobmsg_add_string(&buf, NULL, "wrap");
207
208 if (ciph & IWINFO_CIPHER_AESOCB)
209 blobmsg_add_string(&buf, NULL, "aes-ocb");
210
211 if (ciph & IWINFO_CIPHER_CKIP)
212 blobmsg_add_string(&buf, NULL, "ckip");
213
214 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
215 blobmsg_add_string(&buf, NULL, "none");
216
217 blobmsg_close_array(&buf, d);
218 }
219
220 blobmsg_close_table(&buf, c);
221 }
222
223 static void
224 rpc_iwinfo_call_encryption(const char *name)
225 {
226 struct iwinfo_crypto_entry crypto = { 0 };
227
228 if (!iw->encryption(ifname, (char *)&crypto))
229 rpc_iwinfo_add_encryption(name, &crypto);
230 }
231
232 static void
233 rpc_iwinfo_call_htmodes(const char *name)
234 {
235 int modes;
236 void *c;
237
238 if (!iw->htmodelist(ifname, &modes))
239 {
240 c = blobmsg_open_array(&buf, name);
241
242 if (modes & IWINFO_HTMODE_HT20)
243 blobmsg_add_string(&buf, NULL, "HT20");
244
245 if (modes & IWINFO_HTMODE_HT40)
246 blobmsg_add_string(&buf, NULL, "HT40");
247
248 if (modes & IWINFO_HTMODE_VHT20)
249 blobmsg_add_string(&buf, NULL, "VHT20");
250
251 if (modes & IWINFO_HTMODE_VHT40)
252 blobmsg_add_string(&buf, NULL, "VHT40");
253
254 if (modes & IWINFO_HTMODE_VHT80)
255 blobmsg_add_string(&buf, NULL, "VHT80");
256
257 if (modes & IWINFO_HTMODE_VHT80_80)
258 blobmsg_add_string(&buf, NULL, "VHT80+80");
259
260 if (modes & IWINFO_HTMODE_VHT160)
261 blobmsg_add_string(&buf, NULL, "VHT160");
262
263 blobmsg_close_array(&buf, c);
264 }
265 }
266
267 static void
268 rpc_iwinfo_call_hwmodes(const char *name)
269 {
270 int modes;
271 void *c;
272
273 if (!iw->hwmodelist(ifname, &modes))
274 {
275 c = blobmsg_open_array(&buf, name);
276
277 if (modes & IWINFO_80211_AD)
278 blobmsg_add_string(&buf, NULL, "ad");
279
280 if (modes & IWINFO_80211_AC)
281 blobmsg_add_string(&buf, NULL, "ac");
282
283 if (modes & IWINFO_80211_A)
284 blobmsg_add_string(&buf, NULL, "a");
285
286 if (modes & IWINFO_80211_B)
287 blobmsg_add_string(&buf, NULL, "b");
288
289 if (modes & IWINFO_80211_G)
290 blobmsg_add_string(&buf, NULL, "g");
291
292 if (modes & IWINFO_80211_N)
293 blobmsg_add_string(&buf, NULL, "n");
294
295 blobmsg_close_array(&buf, c);
296 }
297 }
298
299 static void rpc_iwinfo_call_hw_ht_mode()
300 {
301 const char *hwmode_str;
302 const char *htmode_str;
303 int32_t htmode = 0;
304 int modes;
305
306 if (!iw->hwmodelist(ifname, &modes) && (modes == IWINFO_80211_AD)) {
307 blobmsg_add_string(&buf, "hwmode", "ad");
308 return;
309 }
310
311 if (iw->htmode(ifname, &htmode))
312 return;
313
314 switch (htmode) {
315 case IWINFO_HTMODE_HT20:
316 htmode_str = "HT20";
317 hwmode_str = "n";
318 break;
319 case IWINFO_HTMODE_HT40:
320 htmode_str = "HT40";
321 hwmode_str = "n";
322 break;
323 case IWINFO_HTMODE_VHT80:
324 htmode_str = "VHT80";
325 hwmode_str = "ac";
326 break;
327 case IWINFO_HTMODE_VHT80_80:
328 htmode_str = "VHT80+80";
329 hwmode_str = "ac";
330 break;
331 case IWINFO_HTMODE_VHT160:
332 htmode_str = "VHT160";
333 hwmode_str = "ac";
334 break;
335 case IWINFO_HTMODE_NOHT:
336 htmode_str = "20";
337 hwmode_str = "a/g";
338 break;
339 default:
340 htmode_str = hwmode_str = "unknown";
341 break;
342 }
343 blobmsg_add_string(&buf, "hwmode", hwmode_str);
344 blobmsg_add_string(&buf, "htmode", htmode_str);
345 }
346
347 static void
348 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
349 {
350 char rv[IWINFO_BUFSIZE] = { 0 };
351
352 if (!func(ifname, rv))
353 blobmsg_add_string(&buf, name, rv);
354 }
355
356 static int
357 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
358 struct ubus_request_data *req, const char *method,
359 struct blob_attr *msg)
360 {
361 int rv;
362 void *c;
363
364 rv = rpc_iwinfo_open(msg);
365
366 if (rv)
367 return rv;
368
369 blob_buf_init(&buf, 0);
370
371 rpc_iwinfo_call_str("phy", iw->phyname);
372
373 rpc_iwinfo_call_str("ssid", iw->ssid);
374 rpc_iwinfo_call_str("bssid", iw->bssid);
375 rpc_iwinfo_call_str("country", iw->country);
376
377 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
378 rpc_iwinfo_call_int("channel", iw->channel, NULL);
379 rpc_iwinfo_call_int("center_chan1", iw->center_chan1, NULL);
380 rpc_iwinfo_call_int("center_chan2", iw->center_chan2, NULL);
381
382 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
383 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
384
385 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
386 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
387
388 rpc_iwinfo_call_int("quality", iw->quality, NULL);
389 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
390
391 rpc_iwinfo_call_int("signal", iw->signal, NULL);
392 rpc_iwinfo_call_int("noise", iw->noise, NULL);
393
394 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
395
396 rpc_iwinfo_call_encryption("encryption");
397 rpc_iwinfo_call_htmodes("htmodes");
398 rpc_iwinfo_call_hwmodes("hwmodes");
399
400 rpc_iwinfo_call_hw_ht_mode();
401
402 c = blobmsg_open_table(&buf, "hardware");
403 rpc_iwinfo_call_hardware_id("id");
404 rpc_iwinfo_call_str("name", iw->hardware_name);
405 blobmsg_close_table(&buf, c);
406
407 ubus_send_reply(ctx, req, buf.head);
408
409 rpc_iwinfo_close();
410
411 return UBUS_STATUS_OK;
412 }
413
414 static int
415 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
416 struct ubus_request_data *req, const char *method,
417 struct blob_attr *msg)
418 {
419 int i, rv, len;
420 void *c, *d, *t;
421 char mac[18];
422 char res[IWINFO_BUFSIZE];
423 struct iwinfo_scanlist_entry *e;
424
425 rv = rpc_iwinfo_open(msg);
426
427 if (rv)
428 return rv;
429
430 blob_buf_init(&buf, 0);
431
432 c = blobmsg_open_array(&buf, "results");
433
434 if (!iw->scanlist(ifname, res, &len) && (len > 0))
435 {
436 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
437 {
438 e = (struct iwinfo_scanlist_entry *)&res[i];
439 d = blobmsg_open_table(&buf, NULL);
440
441 if (e->ssid[0])
442 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
443
444 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
445 e->mac[0], e->mac[1], e->mac[2],
446 e->mac[3], e->mac[4], e->mac[5]);
447
448 blobmsg_add_string(&buf, "bssid", mac);
449
450 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
451
452 blobmsg_add_u32(&buf, "channel", e->channel);
453 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
454
455 blobmsg_add_u32(&buf, "quality", e->quality);
456 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
457
458 if (e->ht_chan_info.primary_chan) {
459 t = blobmsg_open_table(&buf, "ht_operation");
460 blobmsg_add_u32(&buf, "primary_channel", e->ht_chan_info.primary_chan);
461 blobmsg_add_string(&buf, "secondary_channel_offset", ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
462 blobmsg_add_u32(&buf, "channel_width", ht_chan_width[e->ht_chan_info.chan_width]);
463 blobmsg_close_table(&buf, t);
464 }
465
466 if (e->vht_chan_info.center_chan_1) {
467 t = blobmsg_open_table(&buf, "vht_operation");
468 blobmsg_add_u32(&buf, "channel_width", vht_chan_width[e->vht_chan_info.chan_width]);
469 blobmsg_add_u32(&buf, "center_freq_1", e->vht_chan_info.center_chan_1);
470 blobmsg_add_u32(&buf, "center_freq_2", e->vht_chan_info.center_chan_2);
471 blobmsg_close_table(&buf, t);
472 }
473
474 rpc_iwinfo_add_encryption("encryption", &e->crypto);
475
476 blobmsg_close_table(&buf, d);
477 }
478 }
479
480 blobmsg_close_array(&buf, c);
481
482 ubus_send_reply(ctx, req, buf.head);
483
484 rpc_iwinfo_close();
485
486 return UBUS_STATUS_OK;
487 }
488
489 static void
490 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
491 {
492 blobmsg_add_u8(&buf, "ht", r->is_ht);
493 blobmsg_add_u8(&buf, "vht", r->is_vht);
494 blobmsg_add_u32(&buf, "mhz", r->mhz);
495 blobmsg_add_u32(&buf, "rate", r->rate);
496
497 if (r->is_ht) {
498 blobmsg_add_u32(&buf, "mcs", r->mcs);
499 blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
500 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
501 }
502 else if (r->is_vht) {
503 blobmsg_add_u32(&buf, "mcs", r->mcs);
504 blobmsg_add_u32(&buf, "nss", r->nss);
505 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
506 }
507 }
508
509 static int
510 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
511 struct ubus_request_data *req, const char *method,
512 struct blob_attr *msg)
513 {
514 int i, rv, len;
515 char mac[18];
516 char res[IWINFO_BUFSIZE];
517 struct iwinfo_assoclist_entry *a;
518 struct ether_addr *macaddr = NULL;
519 void *c, *d, *e;
520 struct blob_attr *tb[__RPC_A_MAX];
521 bool found = false;
522
523 blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
524 blob_data(msg), blob_len(msg));
525
526 rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
527 if (rv)
528 return rv;
529
530 if (tb[RPC_A_MACADDR])
531 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
532
533 blob_buf_init(&buf, 0);
534
535 if (!macaddr)
536 c = blobmsg_open_array(&buf, "results");
537
538 if (!iw->assoclist(ifname, res, &len) && (len > 0))
539 {
540 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
541 {
542 a = (struct iwinfo_assoclist_entry *)&res[i];
543
544 if (!macaddr)
545 d = blobmsg_open_table(&buf, NULL);
546 else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
547 continue;
548
549 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
550 a->mac[0], a->mac[1], a->mac[2],
551 a->mac[3], a->mac[4], a->mac[5]);
552
553 blobmsg_add_string(&buf, "mac", mac);
554 blobmsg_add_u32(&buf, "signal", a->signal);
555 blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
556 blobmsg_add_u32(&buf, "noise", a->noise);
557 blobmsg_add_u32(&buf, "inactive", a->inactive);
558 blobmsg_add_u32(&buf, "connected_time", a->connected_time);
559 blobmsg_add_u32(&buf, "thr", a->thr);
560 blobmsg_add_u8(&buf, "authorized", a->is_authorized);
561 blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
562 blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
563 blobmsg_add_u8(&buf, "wme", a->is_wme);
564 blobmsg_add_u8(&buf, "mfp", a->is_mfp);
565 blobmsg_add_u8(&buf, "tdls", a->is_tdls);
566
567 blobmsg_add_u16(&buf, "mesh llid", a->llid);
568 blobmsg_add_u16(&buf, "mesh plid", a->plid);
569 blobmsg_add_string(&buf, "mesh plink", a->plink_state);
570 blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
571 blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
572 blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
573
574 e = blobmsg_open_table(&buf, "rx");
575 blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
576 blobmsg_add_u32(&buf, "packets", a->rx_packets);
577 blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
578 rpc_iwinfo_add_rateinfo(&a->rx_rate);
579 blobmsg_close_table(&buf, e);
580
581 e = blobmsg_open_table(&buf, "tx");
582 blobmsg_add_u32(&buf, "failed", a->tx_failed);
583 blobmsg_add_u32(&buf, "retries", a->tx_retries);
584 blobmsg_add_u32(&buf, "packets", a->tx_packets);
585 blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
586 rpc_iwinfo_add_rateinfo(&a->tx_rate);
587 blobmsg_close_table(&buf, e);
588
589 found = true;
590 if (!macaddr)
591 blobmsg_close_table(&buf, d);
592 else
593 break;
594 }
595 }
596
597 if (!macaddr)
598 blobmsg_close_array(&buf, c);
599 else if (!found)
600 return UBUS_STATUS_NOT_FOUND;
601
602 ubus_send_reply(ctx, req, buf.head);
603
604 rpc_iwinfo_close();
605
606 return UBUS_STATUS_OK;
607 }
608
609 static int
610 rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
611 struct ubus_request_data *req, const char *method,
612 struct blob_attr *msg)
613 {
614 char res[IWINFO_BUFSIZE];
615 struct iwinfo_survey_entry *e;
616 void *c, *d;
617 int i, rv, len;
618
619 blob_buf_init(&buf, 0);
620
621 rv = rpc_iwinfo_open(msg);
622
623 c = blobmsg_open_array(&buf, "results");
624
625 if (rv || iw->survey(ifname, res, &len) || len < 0)
626 return UBUS_STATUS_OK;
627
628 for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
629 e = (struct iwinfo_survey_entry *)&res[i];
630
631 d = blobmsg_open_table(&buf, NULL);
632 blobmsg_add_u32(&buf, "mhz", e->mhz);
633 blobmsg_add_u32(&buf, "noise", e->noise);
634 blobmsg_add_u64(&buf, "active_time", e->active_time);
635 blobmsg_add_u64(&buf, "busy_time", e->busy_time);
636 blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
637 blobmsg_add_u64(&buf, "rx_time", e->rxtime);
638 blobmsg_add_u64(&buf, "tx_time", e->txtime);
639 blobmsg_close_table(&buf, d);
640 }
641
642 blobmsg_close_array(&buf, c);
643 ubus_send_reply(ctx, req, buf.head);
644 rpc_iwinfo_close();
645 return UBUS_STATUS_OK;
646 }
647
648 static int
649 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
650 struct ubus_request_data *req, const char *method,
651 struct blob_attr *msg)
652 {
653 int i, rv, len, ch;
654 char res[IWINFO_BUFSIZE];
655 struct iwinfo_freqlist_entry *f;
656 void *c, *d;
657
658 rv = rpc_iwinfo_open(msg);
659
660 if (rv)
661 return rv;
662
663 blob_buf_init(&buf, 0);
664
665 c = blobmsg_open_array(&buf, "results");
666
667 if (!iw->freqlist(ifname, res, &len) && (len > 0))
668 {
669 if (iw->channel(ifname, &ch))
670 ch = -1;
671
672 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
673 {
674 f = (struct iwinfo_freqlist_entry *)&res[i];
675 d = blobmsg_open_table(&buf, NULL);
676
677 blobmsg_add_u32(&buf, "channel", f->channel);
678 blobmsg_add_u32(&buf, "mhz", f->mhz);
679 blobmsg_add_u8(&buf, "restricted", f->restricted);
680
681 if (ch > -1)
682 blobmsg_add_u8(&buf, "active", f->channel == ch);
683
684 blobmsg_close_table(&buf, d);
685 }
686 }
687
688 blobmsg_close_array(&buf, c);
689
690 ubus_send_reply(ctx, req, buf.head);
691
692 rpc_iwinfo_close();
693
694 return UBUS_STATUS_OK;
695 }
696
697 static int
698 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
699 struct ubus_request_data *req, const char *method,
700 struct blob_attr *msg)
701 {
702 int i, rv, len, pwr, off;
703 char res[IWINFO_BUFSIZE];
704 struct iwinfo_txpwrlist_entry *t;
705 void *c, *d;
706
707 rv = rpc_iwinfo_open(msg);
708
709 if (rv)
710 return rv;
711
712 blob_buf_init(&buf, 0);
713
714 c = blobmsg_open_array(&buf, "results");
715
716 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
717 {
718 if (iw->txpower(ifname, &pwr))
719 pwr = -1;
720
721 if (iw->txpower_offset(ifname, &off))
722 off = 0;
723
724 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
725 {
726 t = (struct iwinfo_txpwrlist_entry *)&res[i];
727 d = blobmsg_open_table(&buf, NULL);
728
729 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
730 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
731
732 if (pwr > -1)
733 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
734
735 blobmsg_close_table(&buf, d);
736 }
737 }
738
739 blobmsg_close_array(&buf, c);
740
741 ubus_send_reply(ctx, req, buf.head);
742
743 rpc_iwinfo_close();
744
745 return UBUS_STATUS_OK;
746 }
747
748 static const char *
749 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
750 {
751 int i;
752 static char ccode[5];
753 struct iwinfo_country_entry *c;
754
755 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
756 {
757 c = (struct iwinfo_country_entry *)&buf[i];
758
759 if (c->iso3166 == iso3166)
760 {
761 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
762 return ccode;
763 }
764 }
765
766 return NULL;
767 }
768
769 static int
770 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
771 struct ubus_request_data *req, const char *method,
772 struct blob_attr *msg)
773 {
774 int rv, len;
775 char cur[3];
776 char iso3166[3];
777 char res[IWINFO_BUFSIZE] = {0};
778 const char *ccode;
779 const struct iwinfo_iso3166_label *l;
780 void *c, *d;
781
782 rv = rpc_iwinfo_open(msg);
783
784 if (rv)
785 return rv;
786
787 blob_buf_init(&buf, 0);
788
789 c = blobmsg_open_array(&buf, "results");
790
791 if (!iw->countrylist(ifname, res, &len) && (len > 0))
792 {
793 if (iw->country(ifname, cur))
794 memset(cur, 0, sizeof(cur));
795
796 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
797 {
798 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
799
800 if (!ccode)
801 continue;
802
803 d = blobmsg_open_table(&buf, NULL);
804
805 blobmsg_add_string(&buf, "code", ccode);
806 blobmsg_add_string(&buf, "country", (const char *)l->name);
807
808 snprintf(iso3166, sizeof(iso3166), "%c%c",
809 (l->iso3166 / 256), (l->iso3166 % 256));
810
811 blobmsg_add_string(&buf, "iso3166", iso3166);
812
813 if (cur[0])
814 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
815
816 blobmsg_close_table(&buf, d);
817 }
818 }
819
820 blobmsg_close_array(&buf, c);
821
822 ubus_send_reply(ctx, req, buf.head);
823
824 rpc_iwinfo_close();
825
826 return UBUS_STATUS_OK;
827 }
828
829 static int
830 rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
831 struct ubus_request_data *req, const char *method,
832 struct blob_attr *msg)
833 {
834 int i;
835 bool found = false;
836 char res[IWINFO_BUFSIZE];
837 const struct iwinfo_ops *ops;
838 struct blob_attr *tb[__RPC_U_MAX];
839 const char *backends[] = {
840 "nl80211",
841 "madwifi",
842 "wl"
843 };
844
845 blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
846 blob_data(msg), blob_len(msg));
847
848 if (!tb[RPC_U_SECTION])
849 return UBUS_STATUS_INVALID_ARGUMENT;
850
851 for (i = 0; i < ARRAY_SIZE(backends); i++)
852 {
853 ops = iwinfo_backend_by_name(backends[i]);
854
855 if (!ops || !ops->lookup_phy)
856 continue;
857
858 if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
859 {
860 found = true;
861 break;
862 }
863 }
864
865 if (found)
866 {
867 blob_buf_init(&buf, 0);
868 blobmsg_add_string(&buf, "phyname", res);
869
870 ubus_send_reply(ctx, req, buf.head);
871 }
872
873 rpc_iwinfo_close();
874
875 return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
876 }
877
878 static int
879 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
880 struct ubus_request_data *req, const char *method,
881 struct blob_attr *msg)
882 {
883 void *c;
884 struct dirent *e;
885 DIR *d;
886
887 d = opendir("/sys/class/net");
888
889 if (!d)
890 return UBUS_STATUS_UNKNOWN_ERROR;
891
892 blob_buf_init(&buf, 0);
893
894 c = blobmsg_open_array(&buf, "devices");
895
896 while ((e = readdir(d)) != NULL)
897 {
898 if (e->d_type != DT_LNK)
899 continue;
900
901 if (iwinfo_type(e->d_name))
902 blobmsg_add_string(&buf, NULL, e->d_name);
903 }
904
905 blobmsg_close_array(&buf, c);
906
907 closedir(d);
908
909 ubus_send_reply(ctx, req, buf.head);
910
911 rpc_iwinfo_close();
912
913 return UBUS_STATUS_OK;
914 }
915
916
917 static int
918 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
919 {
920 static const struct ubus_method iwinfo_methods[] = {
921 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
922 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
923 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
924 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
925 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
926 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
927 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
928 UBUS_METHOD("survey", rpc_iwinfo_survey, rpc_device_policy),
929 UBUS_METHOD("phyname", rpc_iwinfo_phyname, rpc_uci_policy),
930 };
931
932 static struct ubus_object_type iwinfo_type =
933 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
934
935 static struct ubus_object obj = {
936 .name = "iwinfo",
937 .type = &iwinfo_type,
938 .methods = iwinfo_methods,
939 .n_methods = ARRAY_SIZE(iwinfo_methods),
940 };
941
942 return ubus_add_object(ctx, &obj);
943 }
944
945 struct rpc_plugin rpc_plugin = {
946 .init = rpc_iwinfo_api_init
947 };