uci: allow setting zero-length values (to delete an option)
[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;
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                         if (e->wpa_version > 2)
160                         {
161                                 blobmsg_add_u32(&buf, NULL, 1);
162                                 blobmsg_add_u32(&buf, NULL, 2);
163                         }
164                         else
165                         {
166                                 blobmsg_add_u32(&buf, NULL, e->wpa_version);
167                         }
168
169                         blobmsg_close_array(&buf, d);
170
171
172                         d = blobmsg_open_array(&buf, "authentication");
173
174                         if (e->auth_suites & IWINFO_KMGMT_PSK)
175                                 blobmsg_add_string(&buf, NULL, "psk");
176
177                         if (e->auth_suites & IWINFO_KMGMT_8021x)
178                                 blobmsg_add_string(&buf, NULL, "802.1x");
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_hwmodes(const char *name)
231 {
232         int modes;
233         void *c;
234
235         if (!iw->hwmodelist(ifname, &modes))
236         {
237                 c = blobmsg_open_array(&buf, name);
238
239                 if (modes & IWINFO_80211_AC)
240                         blobmsg_add_string(&buf, NULL, "ac");
241
242                 if (modes & IWINFO_80211_A)
243                         blobmsg_add_string(&buf, NULL, "a");
244
245                 if (modes & IWINFO_80211_B)
246                         blobmsg_add_string(&buf, NULL, "b");
247
248                 if (modes & IWINFO_80211_G)
249                         blobmsg_add_string(&buf, NULL, "g");
250
251                 if (modes & IWINFO_80211_N)
252                         blobmsg_add_string(&buf, NULL, "n");
253
254                 blobmsg_close_array(&buf, c);
255         }
256 }
257
258 static void
259 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
260 {
261         char rv[IWINFO_BUFSIZE] = { 0 };
262
263         if (!func(ifname, rv))
264                 blobmsg_add_string(&buf, name, rv);
265 }
266
267 static int
268 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
269                 struct ubus_request_data *req, const char *method,
270                 struct blob_attr *msg)
271 {
272         int rv;
273         void *c;
274
275         rv = rpc_iwinfo_open(msg);
276
277         if (rv)
278                 return rv;
279
280         blob_buf_init(&buf, 0);
281
282         rpc_iwinfo_call_str("phy", iw->phyname);
283
284         rpc_iwinfo_call_str("ssid", iw->ssid);
285         rpc_iwinfo_call_str("bssid", iw->bssid);
286         rpc_iwinfo_call_str("country", iw->country);
287
288         rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
289         rpc_iwinfo_call_int("channel", iw->channel, NULL);
290
291         rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
292         rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
293
294         rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
295         rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
296
297         rpc_iwinfo_call_int("quality", iw->quality, NULL);
298         rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
299
300         rpc_iwinfo_call_int("signal", iw->signal, NULL);
301         rpc_iwinfo_call_int("noise", iw->noise, NULL);
302
303         rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
304
305         rpc_iwinfo_call_encryption("encryption");
306         rpc_iwinfo_call_hwmodes("hwmodes");
307
308         c = blobmsg_open_table(&buf, "hardware");
309         rpc_iwinfo_call_hardware_id("id");
310         rpc_iwinfo_call_str("name", iw->hardware_name);
311         blobmsg_close_table(&buf, c);
312
313         ubus_send_reply(ctx, req, buf.head);
314
315         rpc_iwinfo_close();
316
317         return UBUS_STATUS_OK;
318 }
319
320 static int
321 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
322                 struct ubus_request_data *req, const char *method,
323                 struct blob_attr *msg)
324 {
325         int i, rv, len;
326         void *c, *d;
327         char mac[18];
328         char res[IWINFO_BUFSIZE];
329         struct iwinfo_scanlist_entry *e;
330
331         rv = rpc_iwinfo_open(msg);
332
333         if (rv)
334                 return rv;
335
336         blob_buf_init(&buf, 0);
337
338         c = blobmsg_open_array(&buf, "results");
339
340         if (!iw->scanlist(ifname, res, &len) && (len > 0))
341         {
342                 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
343                 {
344                         e = (struct iwinfo_scanlist_entry *)&res[i];
345                         d = blobmsg_open_table(&buf, NULL);
346
347                         if (e->ssid[0])
348                                 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
349
350                         snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
351                                          e->mac[0], e->mac[1], e->mac[2],
352                                          e->mac[3], e->mac[4], e->mac[5]);
353
354                         blobmsg_add_string(&buf, "bssid", mac);
355
356                         blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
357
358                         blobmsg_add_u32(&buf, "channel", e->channel);
359                         blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
360
361                         blobmsg_add_u32(&buf, "quality", e->quality);
362                         blobmsg_add_u32(&buf, "quality_max", e->quality_max);
363
364                         rpc_iwinfo_add_encryption("encryption", &e->crypto);
365
366                         blobmsg_close_table(&buf, d);
367                 }
368         }
369
370         blobmsg_close_array(&buf, c);
371
372         ubus_send_reply(ctx, req, buf.head);
373
374         rpc_iwinfo_close();
375
376         return UBUS_STATUS_OK;
377 }
378
379 static int
380 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
381                      struct ubus_request_data *req, const char *method,
382                      struct blob_attr *msg)
383 {
384         int i, rv, len;
385         char mac[18];
386         char res[IWINFO_BUFSIZE];
387         struct iwinfo_assoclist_entry *a;
388         struct ether_addr *macaddr = NULL;
389         void *c, *d, *e;
390         struct blob_attr *tb[__RPC_A_MAX];
391         bool found = false;
392
393         blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
394                       blob_data(msg), blob_len(msg));
395
396         rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
397         if (rv)
398                 return rv;
399
400         if (tb[RPC_A_MACADDR])
401                 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
402
403         blob_buf_init(&buf, 0);
404
405         if (!macaddr)
406                 c = blobmsg_open_array(&buf, "results");
407
408         if (!iw->assoclist(ifname, res, &len) && (len > 0))
409         {
410                 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
411                 {
412                         a = (struct iwinfo_assoclist_entry *)&res[i];
413
414                         if (!macaddr)
415                                 d = blobmsg_open_table(&buf, NULL);
416                         else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
417                                 continue;
418
419                         snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
420                                          a->mac[0], a->mac[1], a->mac[2],
421                                          a->mac[3], a->mac[4], a->mac[5]);
422
423                         blobmsg_add_string(&buf, "mac", mac);
424                         blobmsg_add_u32(&buf, "signal", a->signal);
425                         blobmsg_add_u32(&buf, "noise", a->noise);
426                         blobmsg_add_u32(&buf, "inactive", a->inactive);
427
428                         e = blobmsg_open_table(&buf, "rx");
429                         blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
430                         blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
431                         blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
432                         blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
433                         blobmsg_close_table(&buf, e);
434
435                         e = blobmsg_open_table(&buf, "tx");
436                         blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
437                         blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
438                         blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
439                         blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
440                         blobmsg_close_table(&buf, e);
441
442                         found = true;
443                         if (!macaddr)
444                                 blobmsg_close_table(&buf, d);
445                         else
446                                 break;
447                 }
448         }
449
450         if (!macaddr)
451                 blobmsg_close_array(&buf, c);
452         else if (!found)
453                 return UBUS_STATUS_NOT_FOUND;
454
455         ubus_send_reply(ctx, req, buf.head);
456
457         rpc_iwinfo_close();
458
459         return UBUS_STATUS_OK;
460 }
461
462 static int
463 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
464                     struct ubus_request_data *req, const char *method,
465                     struct blob_attr *msg)
466 {
467         int i, rv, len, ch;
468         char res[IWINFO_BUFSIZE];
469         struct iwinfo_freqlist_entry *f;
470         void *c, *d;
471
472         rv = rpc_iwinfo_open(msg);
473
474         if (rv)
475                 return rv;
476
477         blob_buf_init(&buf, 0);
478
479         c = blobmsg_open_array(&buf, "results");
480
481         if (!iw->freqlist(ifname, res, &len) && (len > 0))
482         {
483                 if (iw->channel(ifname, &ch))
484                         ch = -1;
485
486                 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
487                 {
488                         f = (struct iwinfo_freqlist_entry *)&res[i];
489                         d = blobmsg_open_table(&buf, NULL);
490
491                         blobmsg_add_u32(&buf, "channel", f->channel);
492                         blobmsg_add_u32(&buf, "mhz", f->mhz);
493                         blobmsg_add_u8(&buf, "restricted", f->restricted);
494
495                         if (ch > -1)
496                                 blobmsg_add_u8(&buf, "active", f->channel == ch);
497
498                         blobmsg_close_table(&buf, d);
499                 }
500         }
501
502         blobmsg_close_array(&buf, c);
503
504         ubus_send_reply(ctx, req, buf.head);
505
506         rpc_iwinfo_close();
507
508         return UBUS_STATUS_OK;
509 }
510
511 static int
512 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
513                        struct ubus_request_data *req, const char *method,
514                        struct blob_attr *msg)
515 {
516         int i, rv, len, pwr, off;
517         char res[IWINFO_BUFSIZE];
518         struct iwinfo_txpwrlist_entry *t;
519         void *c, *d;
520
521         rv = rpc_iwinfo_open(msg);
522
523         if (rv)
524                 return rv;
525
526         blob_buf_init(&buf, 0);
527
528         c = blobmsg_open_array(&buf, "results");
529
530         if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
531         {
532                 if (iw->txpower(ifname, &pwr))
533                         pwr = -1;
534
535                 if (iw->txpower_offset(ifname, &off))
536                         off = 0;
537
538                 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
539                 {
540                         t = (struct iwinfo_txpwrlist_entry *)&res[i];
541                         d = blobmsg_open_table(&buf, NULL);
542
543                         blobmsg_add_u32(&buf, "dbm", t->dbm + off);
544                         blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
545
546                         if (pwr > -1)
547                                 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
548
549                         blobmsg_close_table(&buf, d);
550                 }
551         }
552
553         blobmsg_close_array(&buf, c);
554
555         ubus_send_reply(ctx, req, buf.head);
556
557         rpc_iwinfo_close();
558
559         return UBUS_STATUS_OK;
560 }
561
562 static const char *
563 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
564 {
565         int i;
566         static char ccode[5];
567         struct iwinfo_country_entry *c;
568
569         for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
570         {
571                 c = (struct iwinfo_country_entry *)&buf[i];
572
573                 if (c->iso3166 == iso3166)
574                 {
575                         snprintf(ccode, sizeof(ccode), "%s", c->ccode);
576                         return ccode;
577                 }
578         }
579
580         return NULL;
581 }
582
583 static int
584 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
585                        struct ubus_request_data *req, const char *method,
586                        struct blob_attr *msg)
587 {
588         int rv, len;
589         char cur[3];
590         char iso3166[3];
591         char res[IWINFO_BUFSIZE];
592         const char *ccode;
593         const struct iwinfo_iso3166_label *l;
594         void *c, *d;
595
596         rv = rpc_iwinfo_open(msg);
597
598         if (rv)
599                 return rv;
600
601         blob_buf_init(&buf, 0);
602
603         c = blobmsg_open_array(&buf, "results");
604
605         if (!iw->countrylist(ifname, res, &len) && (len > 0))
606         {
607                 if (iw->country(ifname, cur))
608                         memset(cur, 0, sizeof(cur));
609
610                 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
611                 {
612                         ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
613
614                         if (!ccode)
615                                 continue;
616
617                         d = blobmsg_open_table(&buf, NULL);
618
619                         blobmsg_add_string(&buf, "code", ccode);
620                         blobmsg_add_string(&buf, "country", (const char *)l->name);
621
622                         snprintf(iso3166, sizeof(iso3166), "%c%c",
623                                  (l->iso3166 / 256), (l->iso3166 % 256));
624
625                         blobmsg_add_string(&buf, "iso3166", iso3166);
626
627                         if (cur[0])
628                                 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
629
630                         blobmsg_close_table(&buf, d);
631                 }
632         }
633
634         blobmsg_close_array(&buf, c);
635
636         ubus_send_reply(ctx, req, buf.head);
637
638         rpc_iwinfo_close();
639
640         return UBUS_STATUS_OK;
641 }
642
643 static int
644 rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
645                    struct ubus_request_data *req, const char *method,
646                    struct blob_attr *msg)
647 {
648         int i;
649         bool found = false;
650         char res[IWINFO_BUFSIZE];
651         const struct iwinfo_ops *ops;
652         struct blob_attr *tb[__RPC_U_MAX];
653         const char *backends[] = {
654                 "nl80211",
655                 "madwifi",
656                 "wl"
657         };
658
659         blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
660                       blob_data(msg), blob_len(msg));
661
662         if (!tb[RPC_U_SECTION])
663                 return UBUS_STATUS_INVALID_ARGUMENT;
664
665         for (i = 0; i < ARRAY_SIZE(backends); i++)
666         {
667                 ops = iwinfo_backend_by_name(backends[i]);
668
669                 if (!ops || !ops->lookup_phy)
670                         continue;
671
672                 if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
673                 {
674                         found = true;
675                         break;
676                 }
677         }
678
679         if (found)
680         {
681                 blob_buf_init(&buf, 0);
682                 blobmsg_add_string(&buf, "phyname", res);
683
684                 ubus_send_reply(ctx, req, buf.head);
685         }
686
687         rpc_iwinfo_close();
688
689         return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
690 }
691
692 static int
693 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
694                    struct ubus_request_data *req, const char *method,
695                    struct blob_attr *msg)
696 {
697         void *c;
698         struct dirent *e;
699         DIR *d;
700
701         d = opendir("/sys/class/net");
702
703         if (!d)
704                 return UBUS_STATUS_UNKNOWN_ERROR;
705
706         blob_buf_init(&buf, 0);
707
708         c = blobmsg_open_array(&buf, "devices");
709
710         while ((e = readdir(d)) != NULL)
711         {
712                 if (e->d_type != DT_LNK)
713                         continue;
714
715                 if (iwinfo_type(e->d_name))
716                         blobmsg_add_string(&buf, NULL, e->d_name);
717         }
718
719         blobmsg_close_array(&buf, c);
720
721         closedir(d);
722
723         ubus_send_reply(ctx, req, buf.head);
724
725         rpc_iwinfo_close();
726
727         return UBUS_STATUS_OK;
728 }
729
730
731 static int
732 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
733 {
734         static const struct ubus_method iwinfo_methods[] = {
735                 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
736                 UBUS_METHOD("info",        rpc_iwinfo_info,        rpc_device_policy),
737                 UBUS_METHOD("scan",        rpc_iwinfo_scan,        rpc_device_policy),
738                 UBUS_METHOD("assoclist",   rpc_iwinfo_assoclist,   rpc_assoclist_policy),
739                 UBUS_METHOD("freqlist",    rpc_iwinfo_freqlist,    rpc_device_policy),
740                 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
741                 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
742                 UBUS_METHOD("phyname",     rpc_iwinfo_phyname,     rpc_uci_policy),
743         };
744
745         static struct ubus_object_type iwinfo_type =
746                 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
747
748         static struct ubus_object obj = {
749                 .name = "iwinfo",
750                 .type = &iwinfo_type,
751                 .methods = iwinfo_methods,
752                 .n_methods = ARRAY_SIZE(iwinfo_methods),
753         };
754
755         return ubus_add_object(ctx, &obj);
756 }
757
758 struct rpc_plugin rpc_plugin = {
759         .init = rpc_iwinfo_api_init
760 };