2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 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>
23 #include <iwinfo/utils.h>
24 #include <net/ethernet.h>
27 #include <netinet/ether.h>
30 #include <rpcd/plugin.h>
33 static struct blob_buf buf
;
34 static const struct iwinfo_ops
*iw
;
35 static const char *ifname
;
42 static const struct blobmsg_policy rpc_device_policy
[__RPC_D_MAX
] = {
43 [RPC_D_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
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
},
62 static const struct blobmsg_policy rpc_uci_policy
[__RPC_U_MAX
] = {
63 [RPC_U_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
67 __rpc_iwinfo_open(struct blob_attr
*device
)
70 return UBUS_STATUS_INVALID_ARGUMENT
;
72 ifname
= blobmsg_data(device
);
73 iw
= iwinfo_backend(ifname
);
75 return iw
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
79 rpc_iwinfo_open(struct blob_attr
*msg
)
81 static struct blob_attr
*tb
[__RPC_D_MAX
];
83 blobmsg_parse(rpc_device_policy
, __RPC_D_MAX
, tb
,
84 blob_data(msg
), blob_len(msg
));
86 return __rpc_iwinfo_open(tb
[RPC_D_DEVICE
]);
90 rpc_iwinfo_close(void)
98 rpc_iwinfo_call_int(const char *name
, int (*func
)(const char *, int *),
103 if (!func(ifname
, &rv
))
106 blobmsg_add_u32(&buf
, name
, rv
);
108 blobmsg_add_string(&buf
, name
, map
[rv
]);
113 rpc_iwinfo_call_hardware_id(const char *name
)
115 struct iwinfo_hardware_id ids
;
118 if (!iw
->hardware_id(ifname
, (char *)&ids
))
120 c
= blobmsg_open_array(&buf
, name
);
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
);
127 blobmsg_close_array(&buf
, c
);
132 rpc_iwinfo_add_encryption(const char *name
, struct iwinfo_crypto_entry
*e
)
137 c
= blobmsg_open_table(&buf
, name
);
139 blobmsg_add_u8(&buf
, "enabled", e
->enabled
);
145 d
= blobmsg_open_array(&buf
, "wep");
147 if (e
->auth_algs
& IWINFO_AUTH_OPEN
)
148 blobmsg_add_string(&buf
, NULL
, "open");
150 if (e
->auth_algs
& IWINFO_AUTH_SHARED
)
151 blobmsg_add_string(&buf
, NULL
, "shared");
153 blobmsg_close_array(&buf
, d
);
157 d
= blobmsg_open_array(&buf
, "wpa");
159 if (e
->wpa_version
> 2)
161 blobmsg_add_u32(&buf
, NULL
, 1);
162 blobmsg_add_u32(&buf
, NULL
, 2);
166 blobmsg_add_u32(&buf
, NULL
, e
->wpa_version
);
169 blobmsg_close_array(&buf
, d
);
172 d
= blobmsg_open_array(&buf
, "authentication");
174 if (e
->auth_suites
& IWINFO_KMGMT_PSK
)
175 blobmsg_add_string(&buf
, NULL
, "psk");
177 if (e
->auth_suites
& IWINFO_KMGMT_8021x
)
178 blobmsg_add_string(&buf
, NULL
, "802.1x");
180 if (!e
->auth_suites
||
181 (e
->auth_suites
& IWINFO_KMGMT_NONE
))
182 blobmsg_add_string(&buf
, NULL
, "none");
184 blobmsg_close_array(&buf
, d
);
187 d
= blobmsg_open_array(&buf
, "ciphers");
188 ciph
= e
->pair_ciphers
| e
->group_ciphers
;
190 if (ciph
& IWINFO_CIPHER_WEP40
)
191 blobmsg_add_string(&buf
, NULL
, "wep-40");
193 if (ciph
& IWINFO_CIPHER_WEP104
)
194 blobmsg_add_string(&buf
, NULL
, "wep-104");
196 if (ciph
& IWINFO_CIPHER_TKIP
)
197 blobmsg_add_string(&buf
, NULL
, "tkip");
199 if (ciph
& IWINFO_CIPHER_CCMP
)
200 blobmsg_add_string(&buf
, NULL
, "ccmp");
202 if (ciph
& IWINFO_CIPHER_WRAP
)
203 blobmsg_add_string(&buf
, NULL
, "wrap");
205 if (ciph
& IWINFO_CIPHER_AESOCB
)
206 blobmsg_add_string(&buf
, NULL
, "aes-ocb");
208 if (ciph
& IWINFO_CIPHER_CKIP
)
209 blobmsg_add_string(&buf
, NULL
, "ckip");
211 if (!ciph
|| (ciph
& IWINFO_CIPHER_NONE
))
212 blobmsg_add_string(&buf
, NULL
, "none");
214 blobmsg_close_array(&buf
, d
);
217 blobmsg_close_table(&buf
, c
);
221 rpc_iwinfo_call_encryption(const char *name
)
223 struct iwinfo_crypto_entry crypto
= { 0 };
225 if (!iw
->encryption(ifname
, (char *)&crypto
))
226 rpc_iwinfo_add_encryption(name
, &crypto
);
230 rpc_iwinfo_call_htmodes(const char *name
)
235 if (!iw
->htmodelist(ifname
, &modes
))
237 c
= blobmsg_open_array(&buf
, name
);
239 if (modes
& IWINFO_HTMODE_HT20
)
240 blobmsg_add_string(&buf
, NULL
, "HT20");
242 if (modes
& IWINFO_HTMODE_HT40
)
243 blobmsg_add_string(&buf
, NULL
, "HT40");
245 if (modes
& IWINFO_HTMODE_VHT20
)
246 blobmsg_add_string(&buf
, NULL
, "VHT20");
248 if (modes
& IWINFO_HTMODE_VHT40
)
249 blobmsg_add_string(&buf
, NULL
, "VHT40");
251 if (modes
& IWINFO_HTMODE_VHT80
)
252 blobmsg_add_string(&buf
, NULL
, "VHT80");
254 if (modes
& IWINFO_HTMODE_VHT80_80
)
255 blobmsg_add_string(&buf
, NULL
, "VHT80+80");
257 if (modes
& IWINFO_HTMODE_VHT160
)
258 blobmsg_add_string(&buf
, NULL
, "VHT160");
260 blobmsg_close_array(&buf
, c
);
265 rpc_iwinfo_call_hwmodes(const char *name
)
270 if (!iw
->hwmodelist(ifname
, &modes
))
272 c
= blobmsg_open_array(&buf
, name
);
274 if (modes
& IWINFO_80211_AC
)
275 blobmsg_add_string(&buf
, NULL
, "ac");
277 if (modes
& IWINFO_80211_A
)
278 blobmsg_add_string(&buf
, NULL
, "a");
280 if (modes
& IWINFO_80211_B
)
281 blobmsg_add_string(&buf
, NULL
, "b");
283 if (modes
& IWINFO_80211_G
)
284 blobmsg_add_string(&buf
, NULL
, "g");
286 if (modes
& IWINFO_80211_N
)
287 blobmsg_add_string(&buf
, NULL
, "n");
289 blobmsg_close_array(&buf
, c
);
294 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
296 char rv
[IWINFO_BUFSIZE
] = { 0 };
298 if (!func(ifname
, rv
))
299 blobmsg_add_string(&buf
, name
, rv
);
303 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
304 struct ubus_request_data
*req
, const char *method
,
305 struct blob_attr
*msg
)
310 rv
= rpc_iwinfo_open(msg
);
315 blob_buf_init(&buf
, 0);
317 rpc_iwinfo_call_str("phy", iw
->phyname
);
319 rpc_iwinfo_call_str("ssid", iw
->ssid
);
320 rpc_iwinfo_call_str("bssid", iw
->bssid
);
321 rpc_iwinfo_call_str("country", iw
->country
);
323 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
324 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
326 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
327 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
329 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
330 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
332 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
333 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
335 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
336 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
338 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
340 rpc_iwinfo_call_encryption("encryption");
341 rpc_iwinfo_call_htmodes("htmodes");
342 rpc_iwinfo_call_hwmodes("hwmodes");
344 c
= blobmsg_open_table(&buf
, "hardware");
345 rpc_iwinfo_call_hardware_id("id");
346 rpc_iwinfo_call_str("name", iw
->hardware_name
);
347 blobmsg_close_table(&buf
, c
);
349 ubus_send_reply(ctx
, req
, buf
.head
);
353 return UBUS_STATUS_OK
;
357 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
358 struct ubus_request_data
*req
, const char *method
,
359 struct blob_attr
*msg
)
364 char res
[IWINFO_BUFSIZE
];
365 struct iwinfo_scanlist_entry
*e
;
367 rv
= rpc_iwinfo_open(msg
);
372 blob_buf_init(&buf
, 0);
374 c
= blobmsg_open_array(&buf
, "results");
376 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
378 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
380 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
381 d
= blobmsg_open_table(&buf
, NULL
);
384 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
386 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
387 e
->mac
[0], e
->mac
[1], e
->mac
[2],
388 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
390 blobmsg_add_string(&buf
, "bssid", mac
);
392 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
394 blobmsg_add_u32(&buf
, "channel", e
->channel
);
395 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
397 blobmsg_add_u32(&buf
, "quality", e
->quality
);
398 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
400 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
402 blobmsg_close_table(&buf
, d
);
406 blobmsg_close_array(&buf
, c
);
408 ubus_send_reply(ctx
, req
, buf
.head
);
412 return UBUS_STATUS_OK
;
416 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
417 struct ubus_request_data
*req
, const char *method
,
418 struct blob_attr
*msg
)
422 char res
[IWINFO_BUFSIZE
];
423 struct iwinfo_assoclist_entry
*a
;
424 struct ether_addr
*macaddr
= NULL
;
426 struct blob_attr
*tb
[__RPC_A_MAX
];
429 blobmsg_parse(rpc_assoclist_policy
, __RPC_A_MAX
, tb
,
430 blob_data(msg
), blob_len(msg
));
432 rv
= __rpc_iwinfo_open(tb
[RPC_A_DEVICE
]);
436 if (tb
[RPC_A_MACADDR
])
437 macaddr
= ether_aton(blobmsg_data(tb
[RPC_A_MACADDR
]));
439 blob_buf_init(&buf
, 0);
442 c
= blobmsg_open_array(&buf
, "results");
444 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
446 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
448 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
451 d
= blobmsg_open_table(&buf
, NULL
);
452 else if (memcmp(macaddr
, a
->mac
, ETH_ALEN
) != 0)
455 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
456 a
->mac
[0], a
->mac
[1], a
->mac
[2],
457 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
459 blobmsg_add_string(&buf
, "mac", mac
);
460 blobmsg_add_u32(&buf
, "signal", a
->signal
);
461 blobmsg_add_u32(&buf
, "signal_avg", a
->signal_avg
);
462 blobmsg_add_u32(&buf
, "noise", a
->noise
);
463 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
464 blobmsg_add_u32(&buf
, "connected_time", a
->connected_time
);
465 blobmsg_add_u32(&buf
, "thr", a
->thr
);
466 blobmsg_add_u8(&buf
, "authorized", a
->is_authorized
);
467 blobmsg_add_u8(&buf
, "authenticated", a
->is_authenticated
);
468 blobmsg_add_string(&buf
, "preamble", a
->is_preamble_short
? "short" : "long");
469 blobmsg_add_u8(&buf
, "wme", a
->is_wme
);
470 blobmsg_add_u8(&buf
, "mfp", a
->is_mfp
);
471 blobmsg_add_u8(&buf
, "tdls", a
->is_tdls
);
473 blobmsg_add_u16(&buf
, "mesh llid", a
->llid
);
474 blobmsg_add_u16(&buf
, "mesh plid", a
->plid
);
475 blobmsg_add_string(&buf
, "mesh plink", a
->plink_state
);
476 blobmsg_add_string(&buf
, "mesh local PS", a
->local_ps
);
477 blobmsg_add_string(&buf
, "mesh peer PS", a
->peer_ps
);
478 blobmsg_add_string(&buf
, "mesh non-peer PS", a
->nonpeer_ps
);
480 e
= blobmsg_open_table(&buf
, "rx");
481 blobmsg_add_u64(&buf
, "drop_misc", a
->rx_drop_misc
);
482 blobmsg_add_u32(&buf
, "packets", a
->rx_packets
);
483 blobmsg_add_u32(&buf
, "bytes", a
->rx_bytes
);
484 blobmsg_add_u32(&buf
, "rate", a
->rx_rate
.rate
);
485 blobmsg_add_u32(&buf
, "mcs", a
->rx_rate
.mcs
);
486 blobmsg_add_u8(&buf
, "40mhz", a
->rx_rate
.is_40mhz
);
487 blobmsg_add_u8(&buf
, "short_gi", a
->rx_rate
.is_short_gi
);
488 blobmsg_close_table(&buf
, e
);
490 e
= blobmsg_open_table(&buf
, "tx");
491 blobmsg_add_u32(&buf
, "failed", a
->tx_failed
);
492 blobmsg_add_u32(&buf
, "retries", a
->tx_retries
);
493 blobmsg_add_u32(&buf
, "packets", a
->tx_packets
);
494 blobmsg_add_u32(&buf
, "bytes", a
->tx_bytes
);
495 blobmsg_add_u32(&buf
, "rate", a
->tx_rate
.rate
);
496 blobmsg_add_u32(&buf
, "mcs", a
->tx_rate
.mcs
);
497 blobmsg_add_u8(&buf
, "40mhz", a
->tx_rate
.is_40mhz
);
498 blobmsg_add_u8(&buf
, "short_gi", a
->tx_rate
.is_short_gi
);
499 blobmsg_close_table(&buf
, e
);
503 blobmsg_close_table(&buf
, d
);
510 blobmsg_close_array(&buf
, c
);
512 return UBUS_STATUS_NOT_FOUND
;
514 ubus_send_reply(ctx
, req
, buf
.head
);
518 return UBUS_STATUS_OK
;
522 rpc_iwinfo_survey(struct ubus_context
*ctx
, struct ubus_object
*obj
,
523 struct ubus_request_data
*req
, const char *method
,
524 struct blob_attr
*msg
)
526 char res
[IWINFO_BUFSIZE
];
527 struct iwinfo_survey_entry
*e
;
531 blob_buf_init(&buf
, 0);
533 rv
= rpc_iwinfo_open(msg
);
535 c
= blobmsg_open_array(&buf
, "results");
537 if (rv
|| iw
->survey(ifname
, res
, &len
) || len
< 0)
538 return UBUS_STATUS_OK
;
540 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_survey_entry
)) {
541 e
= (struct iwinfo_survey_entry
*)&res
[i
];
543 d
= blobmsg_open_table(&buf
, NULL
);
544 blobmsg_add_u32(&buf
, "mhz", e
->mhz
);
545 blobmsg_add_u32(&buf
, "noise", e
->noise
);
546 blobmsg_add_u64(&buf
, "active_time", e
->active_time
);
547 blobmsg_add_u64(&buf
, "busy_time", e
->busy_time
);
548 blobmsg_add_u64(&buf
, "busy_time_ext", e
->busy_time_ext
);
549 blobmsg_add_u64(&buf
, "rx_time", e
->rxtime
);
550 blobmsg_add_u64(&buf
, "tx_time", e
->txtime
);
551 blobmsg_close_table(&buf
, d
);
554 blobmsg_close_array(&buf
, c
);
555 ubus_send_reply(ctx
, req
, buf
.head
);
557 return UBUS_STATUS_OK
;
561 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
562 struct ubus_request_data
*req
, const char *method
,
563 struct blob_attr
*msg
)
566 char res
[IWINFO_BUFSIZE
];
567 struct iwinfo_freqlist_entry
*f
;
570 rv
= rpc_iwinfo_open(msg
);
575 blob_buf_init(&buf
, 0);
577 c
= blobmsg_open_array(&buf
, "results");
579 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
581 if (iw
->channel(ifname
, &ch
))
584 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
586 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
587 d
= blobmsg_open_table(&buf
, NULL
);
589 blobmsg_add_u32(&buf
, "channel", f
->channel
);
590 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
591 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
594 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
596 blobmsg_close_table(&buf
, d
);
600 blobmsg_close_array(&buf
, c
);
602 ubus_send_reply(ctx
, req
, buf
.head
);
606 return UBUS_STATUS_OK
;
610 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
611 struct ubus_request_data
*req
, const char *method
,
612 struct blob_attr
*msg
)
614 int i
, rv
, len
, pwr
, off
;
615 char res
[IWINFO_BUFSIZE
];
616 struct iwinfo_txpwrlist_entry
*t
;
619 rv
= rpc_iwinfo_open(msg
);
624 blob_buf_init(&buf
, 0);
626 c
= blobmsg_open_array(&buf
, "results");
628 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
630 if (iw
->txpower(ifname
, &pwr
))
633 if (iw
->txpower_offset(ifname
, &off
))
636 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
638 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
639 d
= blobmsg_open_table(&buf
, NULL
);
641 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
642 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
645 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
647 blobmsg_close_table(&buf
, d
);
651 blobmsg_close_array(&buf
, c
);
653 ubus_send_reply(ctx
, req
, buf
.head
);
657 return UBUS_STATUS_OK
;
661 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
664 static char ccode
[5];
665 struct iwinfo_country_entry
*c
;
667 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
669 c
= (struct iwinfo_country_entry
*)&buf
[i
];
671 if (c
->iso3166
== iso3166
)
673 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
682 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
683 struct ubus_request_data
*req
, const char *method
,
684 struct blob_attr
*msg
)
689 char res
[IWINFO_BUFSIZE
] = {0};
691 const struct iwinfo_iso3166_label
*l
;
694 rv
= rpc_iwinfo_open(msg
);
699 blob_buf_init(&buf
, 0);
701 c
= blobmsg_open_array(&buf
, "results");
703 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
705 if (iw
->country(ifname
, cur
))
706 memset(cur
, 0, sizeof(cur
));
708 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
710 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
715 d
= blobmsg_open_table(&buf
, NULL
);
717 blobmsg_add_string(&buf
, "code", ccode
);
718 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
720 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
721 (l
->iso3166
/ 256), (l
->iso3166
% 256));
723 blobmsg_add_string(&buf
, "iso3166", iso3166
);
726 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
728 blobmsg_close_table(&buf
, d
);
732 blobmsg_close_array(&buf
, c
);
734 ubus_send_reply(ctx
, req
, buf
.head
);
738 return UBUS_STATUS_OK
;
742 rpc_iwinfo_phyname(struct ubus_context
*ctx
, struct ubus_object
*obj
,
743 struct ubus_request_data
*req
, const char *method
,
744 struct blob_attr
*msg
)
748 char res
[IWINFO_BUFSIZE
];
749 const struct iwinfo_ops
*ops
;
750 struct blob_attr
*tb
[__RPC_U_MAX
];
751 const char *backends
[] = {
757 blobmsg_parse(rpc_uci_policy
, __RPC_U_MAX
, tb
,
758 blob_data(msg
), blob_len(msg
));
760 if (!tb
[RPC_U_SECTION
])
761 return UBUS_STATUS_INVALID_ARGUMENT
;
763 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++)
765 ops
= iwinfo_backend_by_name(backends
[i
]);
767 if (!ops
|| !ops
->lookup_phy
)
770 if (!ops
->lookup_phy(blobmsg_get_string(tb
[RPC_U_SECTION
]), res
))
779 blob_buf_init(&buf
, 0);
780 blobmsg_add_string(&buf
, "phyname", res
);
782 ubus_send_reply(ctx
, req
, buf
.head
);
787 return found
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
791 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
792 struct ubus_request_data
*req
, const char *method
,
793 struct blob_attr
*msg
)
799 d
= opendir("/sys/class/net");
802 return UBUS_STATUS_UNKNOWN_ERROR
;
804 blob_buf_init(&buf
, 0);
806 c
= blobmsg_open_array(&buf
, "devices");
808 while ((e
= readdir(d
)) != NULL
)
810 if (e
->d_type
!= DT_LNK
)
813 if (iwinfo_type(e
->d_name
))
814 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
817 blobmsg_close_array(&buf
, c
);
821 ubus_send_reply(ctx
, req
, buf
.head
);
825 return UBUS_STATUS_OK
;
830 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
832 static const struct ubus_method iwinfo_methods
[] = {
833 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
834 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
835 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
836 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_assoclist_policy
),
837 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
838 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
839 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
840 UBUS_METHOD("survey", rpc_iwinfo_survey
, rpc_device_policy
),
841 UBUS_METHOD("phyname", rpc_iwinfo_phyname
, rpc_uci_policy
),
844 static struct ubus_object_type iwinfo_type
=
845 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
847 static struct ubus_object obj
= {
849 .type
= &iwinfo_type
,
850 .methods
= iwinfo_methods
,
851 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
854 return ubus_add_object(ctx
, &obj
);
857 struct rpc_plugin rpc_plugin
= {
858 .init
= rpc_iwinfo_api_init