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
)
134 int ciph
, wpa_version
;
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 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
);
163 blobmsg_close_array(&buf
, d
);
166 d
= blobmsg_open_array(&buf
, "authentication");
168 if (e
->auth_suites
& IWINFO_KMGMT_PSK
)
169 blobmsg_add_string(&buf
, NULL
, "psk");
171 if (e
->auth_suites
& IWINFO_KMGMT_8021x
)
172 blobmsg_add_string(&buf
, NULL
, "802.1x");
174 if (e
->auth_suites
& IWINFO_KMGMT_SAE
)
175 blobmsg_add_string(&buf
, NULL
, "sae");
177 if (e
->auth_suites
& IWINFO_KMGMT_OWE
)
178 blobmsg_add_string(&buf
, NULL
, "owe");
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_GCMP
)
203 blobmsg_add_string(&buf
, NULL
, "gcmp");
205 if (ciph
& IWINFO_CIPHER_WRAP
)
206 blobmsg_add_string(&buf
, NULL
, "wrap");
208 if (ciph
& IWINFO_CIPHER_AESOCB
)
209 blobmsg_add_string(&buf
, NULL
, "aes-ocb");
211 if (ciph
& IWINFO_CIPHER_CKIP
)
212 blobmsg_add_string(&buf
, NULL
, "ckip");
214 if (!ciph
|| (ciph
& IWINFO_CIPHER_NONE
))
215 blobmsg_add_string(&buf
, NULL
, "none");
217 blobmsg_close_array(&buf
, d
);
220 blobmsg_close_table(&buf
, c
);
224 rpc_iwinfo_call_encryption(const char *name
)
226 struct iwinfo_crypto_entry crypto
= { 0 };
228 if (!iw
->encryption(ifname
, (char *)&crypto
))
229 rpc_iwinfo_add_encryption(name
, &crypto
);
233 rpc_iwinfo_call_htmodes(const char *name
)
238 if (!iw
->htmodelist(ifname
, &modes
))
240 c
= blobmsg_open_array(&buf
, name
);
242 if (modes
& IWINFO_HTMODE_HT20
)
243 blobmsg_add_string(&buf
, NULL
, "HT20");
245 if (modes
& IWINFO_HTMODE_HT40
)
246 blobmsg_add_string(&buf
, NULL
, "HT40");
248 if (modes
& IWINFO_HTMODE_VHT20
)
249 blobmsg_add_string(&buf
, NULL
, "VHT20");
251 if (modes
& IWINFO_HTMODE_VHT40
)
252 blobmsg_add_string(&buf
, NULL
, "VHT40");
254 if (modes
& IWINFO_HTMODE_VHT80
)
255 blobmsg_add_string(&buf
, NULL
, "VHT80");
257 if (modes
& IWINFO_HTMODE_VHT80_80
)
258 blobmsg_add_string(&buf
, NULL
, "VHT80+80");
260 if (modes
& IWINFO_HTMODE_VHT160
)
261 blobmsg_add_string(&buf
, NULL
, "VHT160");
263 if (modes
& IWINFO_HTMODE_HE20
)
264 blobmsg_add_string(&buf
, NULL
, "HE20");
266 if (modes
& IWINFO_HTMODE_HE40
)
267 blobmsg_add_string(&buf
, NULL
, "HE40");
269 if (modes
& IWINFO_HTMODE_HE80
)
270 blobmsg_add_string(&buf
, NULL
, "HE80");
272 if (modes
& IWINFO_HTMODE_HE80_80
)
273 blobmsg_add_string(&buf
, NULL
, "HE80+80");
275 if (modes
& IWINFO_HTMODE_HE160
)
276 blobmsg_add_string(&buf
, NULL
, "HE160");
278 blobmsg_close_array(&buf
, c
);
283 rpc_iwinfo_call_hwmodes(const char *name
)
288 if (!iw
->hwmodelist(ifname
, &modes
))
290 c
= blobmsg_open_array(&buf
, name
);
292 if (modes
& IWINFO_80211_AD
)
293 blobmsg_add_string(&buf
, NULL
, "ad");
295 if (modes
& IWINFO_80211_AC
)
296 blobmsg_add_string(&buf
, NULL
, "ac");
298 if (modes
& IWINFO_80211_AX
)
299 blobmsg_add_string(&buf
, NULL
, "ax");
301 if (modes
& IWINFO_80211_A
)
302 blobmsg_add_string(&buf
, NULL
, "a");
304 if (modes
& IWINFO_80211_B
)
305 blobmsg_add_string(&buf
, NULL
, "b");
307 if (modes
& IWINFO_80211_G
)
308 blobmsg_add_string(&buf
, NULL
, "g");
310 if (modes
& IWINFO_80211_N
)
311 blobmsg_add_string(&buf
, NULL
, "n");
313 blobmsg_close_array(&buf
, c
);
317 static void rpc_iwinfo_call_hw_ht_mode()
319 const char *hwmode_str
;
320 const char *htmode_str
;
324 if (!iw
->hwmodelist(ifname
, &modes
) && (modes
== IWINFO_80211_AD
)) {
325 blobmsg_add_string(&buf
, "hwmode", "ad");
329 if (iw
->htmode(ifname
, &htmode
))
333 case IWINFO_HTMODE_HT20
:
337 case IWINFO_HTMODE_HT40
:
341 case IWINFO_HTMODE_VHT80
:
342 htmode_str
= "VHT80";
345 case IWINFO_HTMODE_VHT80_80
:
346 htmode_str
= "VHT80+80";
349 case IWINFO_HTMODE_VHT160
:
350 htmode_str
= "VHT160";
353 case IWINFO_HTMODE_HE20
:
357 case IWINFO_HTMODE_HE40
:
361 case IWINFO_HTMODE_HE80
:
365 case IWINFO_HTMODE_HE80_80
:
366 htmode_str
= "HE80+80";
369 case IWINFO_HTMODE_HE160
:
370 htmode_str
= "HE160";
373 case IWINFO_HTMODE_NOHT
:
378 htmode_str
= hwmode_str
= "unknown";
381 blobmsg_add_string(&buf
, "hwmode", hwmode_str
);
382 blobmsg_add_string(&buf
, "htmode", htmode_str
);
386 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
388 char rv
[IWINFO_BUFSIZE
] = { 0 };
390 if (!func(ifname
, rv
))
391 blobmsg_add_string(&buf
, name
, rv
);
395 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
396 struct ubus_request_data
*req
, const char *method
,
397 struct blob_attr
*msg
)
402 rv
= rpc_iwinfo_open(msg
);
407 blob_buf_init(&buf
, 0);
409 rpc_iwinfo_call_str("phy", iw
->phyname
);
411 rpc_iwinfo_call_str("ssid", iw
->ssid
);
412 rpc_iwinfo_call_str("bssid", iw
->bssid
);
413 rpc_iwinfo_call_str("country", iw
->country
);
415 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
416 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
417 rpc_iwinfo_call_int("center_chan1", iw
->center_chan1
, NULL
);
418 rpc_iwinfo_call_int("center_chan2", iw
->center_chan2
, NULL
);
420 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
421 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
423 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
424 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
426 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
427 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
429 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
430 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
432 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
434 rpc_iwinfo_call_encryption("encryption");
435 rpc_iwinfo_call_htmodes("htmodes");
436 rpc_iwinfo_call_hwmodes("hwmodes");
438 rpc_iwinfo_call_hw_ht_mode();
440 c
= blobmsg_open_table(&buf
, "hardware");
441 rpc_iwinfo_call_hardware_id("id");
442 rpc_iwinfo_call_str("name", iw
->hardware_name
);
443 blobmsg_close_table(&buf
, c
);
445 ubus_send_reply(ctx
, req
, buf
.head
);
449 return UBUS_STATUS_OK
;
453 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
454 struct ubus_request_data
*req
, const char *method
,
455 struct blob_attr
*msg
)
460 char res
[IWINFO_BUFSIZE
];
461 struct iwinfo_scanlist_entry
*e
;
463 rv
= rpc_iwinfo_open(msg
);
468 blob_buf_init(&buf
, 0);
470 c
= blobmsg_open_array(&buf
, "results");
472 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
474 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
476 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
477 d
= blobmsg_open_table(&buf
, NULL
);
480 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
482 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
483 e
->mac
[0], e
->mac
[1], e
->mac
[2],
484 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
486 blobmsg_add_string(&buf
, "bssid", mac
);
488 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
490 blobmsg_add_u32(&buf
, "channel", e
->channel
);
491 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
493 blobmsg_add_u32(&buf
, "quality", e
->quality
);
494 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
496 if (e
->ht_chan_info
.primary_chan
) {
497 t
= blobmsg_open_table(&buf
, "ht_operation");
498 blobmsg_add_u32(&buf
, "primary_channel", e
->ht_chan_info
.primary_chan
);
499 blobmsg_add_string(&buf
, "secondary_channel_offset", ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
500 blobmsg_add_u32(&buf
, "channel_width", ht_chan_width
[e
->ht_chan_info
.chan_width
]);
501 blobmsg_close_table(&buf
, t
);
504 if (e
->vht_chan_info
.center_chan_1
) {
505 t
= blobmsg_open_table(&buf
, "vht_operation");
506 blobmsg_add_u32(&buf
, "channel_width", vht_chan_width
[e
->vht_chan_info
.chan_width
]);
507 blobmsg_add_u32(&buf
, "center_freq_1", e
->vht_chan_info
.center_chan_1
);
508 blobmsg_add_u32(&buf
, "center_freq_2", e
->vht_chan_info
.center_chan_2
);
509 blobmsg_close_table(&buf
, t
);
512 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
514 blobmsg_close_table(&buf
, d
);
518 blobmsg_close_array(&buf
, c
);
520 ubus_send_reply(ctx
, req
, buf
.head
);
524 return UBUS_STATUS_OK
;
528 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry
*r
)
530 blobmsg_add_u8(&buf
, "ht", r
->is_ht
);
531 blobmsg_add_u8(&buf
, "vht", r
->is_vht
);
532 blobmsg_add_u8(&buf
, "he", r
->is_he
);
533 blobmsg_add_u32(&buf
, "mhz", r
->mhz
);
534 blobmsg_add_u32(&buf
, "rate", r
->rate
);
537 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
538 blobmsg_add_u8(&buf
, "40mhz", r
->is_40mhz
);
539 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
541 else if (r
->is_vht
) {
542 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
543 blobmsg_add_u32(&buf
, "nss", r
->nss
);
544 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
547 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
548 blobmsg_add_u32(&buf
, "nss", r
->nss
);
549 blobmsg_add_u32(&buf
, "he_gi", r
->he_gi
);
550 blobmsg_add_u32(&buf
, "he_dcm", r
->he_dcm
);
555 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
556 struct ubus_request_data
*req
, const char *method
,
557 struct blob_attr
*msg
)
561 char res
[IWINFO_BUFSIZE
];
562 struct iwinfo_assoclist_entry
*a
;
563 struct ether_addr
*macaddr
= NULL
;
565 struct blob_attr
*tb
[__RPC_A_MAX
];
568 blobmsg_parse(rpc_assoclist_policy
, __RPC_A_MAX
, tb
,
569 blob_data(msg
), blob_len(msg
));
571 rv
= __rpc_iwinfo_open(tb
[RPC_A_DEVICE
]);
575 if (tb
[RPC_A_MACADDR
])
576 macaddr
= ether_aton(blobmsg_data(tb
[RPC_A_MACADDR
]));
578 blob_buf_init(&buf
, 0);
581 c
= blobmsg_open_array(&buf
, "results");
583 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
585 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
587 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
590 d
= blobmsg_open_table(&buf
, NULL
);
591 else if (memcmp(macaddr
, a
->mac
, ETH_ALEN
) != 0)
594 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
595 a
->mac
[0], a
->mac
[1], a
->mac
[2],
596 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
598 blobmsg_add_string(&buf
, "mac", mac
);
599 blobmsg_add_u32(&buf
, "signal", a
->signal
);
600 blobmsg_add_u32(&buf
, "signal_avg", a
->signal_avg
);
601 blobmsg_add_u32(&buf
, "noise", a
->noise
);
602 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
603 blobmsg_add_u32(&buf
, "connected_time", a
->connected_time
);
604 blobmsg_add_u32(&buf
, "thr", a
->thr
);
605 blobmsg_add_u8(&buf
, "authorized", a
->is_authorized
);
606 blobmsg_add_u8(&buf
, "authenticated", a
->is_authenticated
);
607 blobmsg_add_string(&buf
, "preamble", a
->is_preamble_short
? "short" : "long");
608 blobmsg_add_u8(&buf
, "wme", a
->is_wme
);
609 blobmsg_add_u8(&buf
, "mfp", a
->is_mfp
);
610 blobmsg_add_u8(&buf
, "tdls", a
->is_tdls
);
612 blobmsg_add_u16(&buf
, "mesh llid", a
->llid
);
613 blobmsg_add_u16(&buf
, "mesh plid", a
->plid
);
614 blobmsg_add_string(&buf
, "mesh plink", a
->plink_state
);
615 blobmsg_add_string(&buf
, "mesh local PS", a
->local_ps
);
616 blobmsg_add_string(&buf
, "mesh peer PS", a
->peer_ps
);
617 blobmsg_add_string(&buf
, "mesh non-peer PS", a
->nonpeer_ps
);
619 e
= blobmsg_open_table(&buf
, "rx");
620 blobmsg_add_u64(&buf
, "drop_misc", a
->rx_drop_misc
);
621 blobmsg_add_u32(&buf
, "packets", a
->rx_packets
);
622 blobmsg_add_u32(&buf
, "bytes", a
->rx_bytes
);
623 rpc_iwinfo_add_rateinfo(&a
->rx_rate
);
624 blobmsg_close_table(&buf
, e
);
626 e
= blobmsg_open_table(&buf
, "tx");
627 blobmsg_add_u32(&buf
, "failed", a
->tx_failed
);
628 blobmsg_add_u32(&buf
, "retries", a
->tx_retries
);
629 blobmsg_add_u32(&buf
, "packets", a
->tx_packets
);
630 blobmsg_add_u32(&buf
, "bytes", a
->tx_bytes
);
631 rpc_iwinfo_add_rateinfo(&a
->tx_rate
);
632 blobmsg_close_table(&buf
, e
);
636 blobmsg_close_table(&buf
, d
);
643 blobmsg_close_array(&buf
, c
);
645 return UBUS_STATUS_NOT_FOUND
;
647 ubus_send_reply(ctx
, req
, buf
.head
);
651 return UBUS_STATUS_OK
;
655 rpc_iwinfo_survey(struct ubus_context
*ctx
, struct ubus_object
*obj
,
656 struct ubus_request_data
*req
, const char *method
,
657 struct blob_attr
*msg
)
659 char res
[IWINFO_BUFSIZE
];
660 struct iwinfo_survey_entry
*e
;
664 blob_buf_init(&buf
, 0);
666 rv
= rpc_iwinfo_open(msg
);
668 c
= blobmsg_open_array(&buf
, "results");
670 if (rv
|| iw
->survey(ifname
, res
, &len
) || len
< 0)
671 return UBUS_STATUS_OK
;
673 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_survey_entry
)) {
674 e
= (struct iwinfo_survey_entry
*)&res
[i
];
676 d
= blobmsg_open_table(&buf
, NULL
);
677 blobmsg_add_u32(&buf
, "mhz", e
->mhz
);
678 blobmsg_add_u32(&buf
, "noise", e
->noise
);
679 blobmsg_add_u64(&buf
, "active_time", e
->active_time
);
680 blobmsg_add_u64(&buf
, "busy_time", e
->busy_time
);
681 blobmsg_add_u64(&buf
, "busy_time_ext", e
->busy_time_ext
);
682 blobmsg_add_u64(&buf
, "rx_time", e
->rxtime
);
683 blobmsg_add_u64(&buf
, "tx_time", e
->txtime
);
684 blobmsg_close_table(&buf
, d
);
687 blobmsg_close_array(&buf
, c
);
688 ubus_send_reply(ctx
, req
, buf
.head
);
690 return UBUS_STATUS_OK
;
694 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
695 struct ubus_request_data
*req
, const char *method
,
696 struct blob_attr
*msg
)
699 char res
[IWINFO_BUFSIZE
];
700 struct iwinfo_freqlist_entry
*f
;
703 rv
= rpc_iwinfo_open(msg
);
708 blob_buf_init(&buf
, 0);
710 c
= blobmsg_open_array(&buf
, "results");
712 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
714 if (iw
->channel(ifname
, &ch
))
717 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
719 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
720 d
= blobmsg_open_table(&buf
, NULL
);
722 blobmsg_add_u32(&buf
, "channel", f
->channel
);
723 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
724 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
727 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
729 blobmsg_close_table(&buf
, d
);
733 blobmsg_close_array(&buf
, c
);
735 ubus_send_reply(ctx
, req
, buf
.head
);
739 return UBUS_STATUS_OK
;
743 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
744 struct ubus_request_data
*req
, const char *method
,
745 struct blob_attr
*msg
)
747 int i
, rv
, len
, pwr
, off
;
748 char res
[IWINFO_BUFSIZE
];
749 struct iwinfo_txpwrlist_entry
*t
;
752 rv
= rpc_iwinfo_open(msg
);
757 blob_buf_init(&buf
, 0);
759 c
= blobmsg_open_array(&buf
, "results");
761 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
763 if (iw
->txpower(ifname
, &pwr
))
766 if (iw
->txpower_offset(ifname
, &off
))
769 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
771 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
772 d
= blobmsg_open_table(&buf
, NULL
);
774 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
775 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
778 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
780 blobmsg_close_table(&buf
, d
);
784 blobmsg_close_array(&buf
, c
);
786 ubus_send_reply(ctx
, req
, buf
.head
);
790 return UBUS_STATUS_OK
;
794 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
797 static char ccode
[5];
798 struct iwinfo_country_entry
*c
;
800 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
802 c
= (struct iwinfo_country_entry
*)&buf
[i
];
804 if (c
->iso3166
== iso3166
)
806 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
815 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
816 struct ubus_request_data
*req
, const char *method
,
817 struct blob_attr
*msg
)
822 char res
[IWINFO_BUFSIZE
] = {0};
824 const struct iwinfo_iso3166_label
*l
;
827 rv
= rpc_iwinfo_open(msg
);
832 blob_buf_init(&buf
, 0);
834 c
= blobmsg_open_array(&buf
, "results");
836 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
838 if (iw
->country(ifname
, cur
))
839 memset(cur
, 0, sizeof(cur
));
841 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
843 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
848 d
= blobmsg_open_table(&buf
, NULL
);
850 blobmsg_add_string(&buf
, "code", ccode
);
851 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
853 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
854 (l
->iso3166
/ 256), (l
->iso3166
% 256));
856 blobmsg_add_string(&buf
, "iso3166", iso3166
);
859 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
861 blobmsg_close_table(&buf
, d
);
865 blobmsg_close_array(&buf
, c
);
867 ubus_send_reply(ctx
, req
, buf
.head
);
871 return UBUS_STATUS_OK
;
875 rpc_iwinfo_phyname(struct ubus_context
*ctx
, struct ubus_object
*obj
,
876 struct ubus_request_data
*req
, const char *method
,
877 struct blob_attr
*msg
)
881 char res
[IWINFO_BUFSIZE
];
882 const struct iwinfo_ops
*ops
;
883 struct blob_attr
*tb
[__RPC_U_MAX
];
884 const char *backends
[] = {
890 blobmsg_parse(rpc_uci_policy
, __RPC_U_MAX
, tb
,
891 blob_data(msg
), blob_len(msg
));
893 if (!tb
[RPC_U_SECTION
])
894 return UBUS_STATUS_INVALID_ARGUMENT
;
896 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++)
898 ops
= iwinfo_backend_by_name(backends
[i
]);
900 if (!ops
|| !ops
->lookup_phy
)
903 if (!ops
->lookup_phy(blobmsg_get_string(tb
[RPC_U_SECTION
]), res
))
912 blob_buf_init(&buf
, 0);
913 blobmsg_add_string(&buf
, "phyname", res
);
915 ubus_send_reply(ctx
, req
, buf
.head
);
920 return found
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
924 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
925 struct ubus_request_data
*req
, const char *method
,
926 struct blob_attr
*msg
)
932 d
= opendir("/sys/class/net");
935 return UBUS_STATUS_UNKNOWN_ERROR
;
937 blob_buf_init(&buf
, 0);
939 c
= blobmsg_open_array(&buf
, "devices");
941 while ((e
= readdir(d
)) != NULL
)
943 if (e
->d_type
!= DT_LNK
)
946 if (iwinfo_type(e
->d_name
))
947 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
950 blobmsg_close_array(&buf
, c
);
954 ubus_send_reply(ctx
, req
, buf
.head
);
958 return UBUS_STATUS_OK
;
963 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
965 static const struct ubus_method iwinfo_methods
[] = {
966 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
967 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
968 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
969 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_assoclist_policy
),
970 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
971 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
972 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
973 UBUS_METHOD("survey", rpc_iwinfo_survey
, rpc_device_policy
),
974 UBUS_METHOD("phyname", rpc_iwinfo_phyname
, rpc_uci_policy
),
977 static struct ubus_object_type iwinfo_type
=
978 UBUS_OBJECT_TYPE("rpcd-plugin-iwinfo", iwinfo_methods
);
980 static struct ubus_object obj
= {
982 .type
= &iwinfo_type
,
983 .methods
= iwinfo_methods
,
984 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
987 return ubus_add_object(ctx
, &obj
);
990 struct rpc_plugin rpc_plugin
= {
991 .init
= rpc_iwinfo_api_init