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_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
);
293 static void rpc_iwinfo_call_hw_ht_mode()
295 const char *hwmode_str
;
296 const char *htmode_str
;
299 if (iw
->htmode(ifname
, &htmode
))
303 case IWINFO_HTMODE_HT20
:
307 case IWINFO_HTMODE_HT40
:
311 case IWINFO_HTMODE_VHT80
:
312 htmode_str
= "VHT80";
315 case IWINFO_HTMODE_VHT80_80
:
316 htmode_str
= "VHT80+80";
319 case IWINFO_HTMODE_VHT160
:
320 htmode_str
= "VHT160";
323 case IWINFO_HTMODE_NOHT
:
328 htmode_str
= hwmode_str
= "unknown";
331 blobmsg_add_string(&buf
, "hwmode", hwmode_str
);
332 blobmsg_add_string(&buf
, "htmode", htmode_str
);
336 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
338 char rv
[IWINFO_BUFSIZE
] = { 0 };
340 if (!func(ifname
, rv
))
341 blobmsg_add_string(&buf
, name
, rv
);
345 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
346 struct ubus_request_data
*req
, const char *method
,
347 struct blob_attr
*msg
)
352 rv
= rpc_iwinfo_open(msg
);
357 blob_buf_init(&buf
, 0);
359 rpc_iwinfo_call_str("phy", iw
->phyname
);
361 rpc_iwinfo_call_str("ssid", iw
->ssid
);
362 rpc_iwinfo_call_str("bssid", iw
->bssid
);
363 rpc_iwinfo_call_str("country", iw
->country
);
365 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
366 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
368 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
369 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
371 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
372 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
374 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
375 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
377 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
378 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
380 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
382 rpc_iwinfo_call_encryption("encryption");
383 rpc_iwinfo_call_htmodes("htmodes");
384 rpc_iwinfo_call_hwmodes("hwmodes");
386 rpc_iwinfo_call_hw_ht_mode();
388 c
= blobmsg_open_table(&buf
, "hardware");
389 rpc_iwinfo_call_hardware_id("id");
390 rpc_iwinfo_call_str("name", iw
->hardware_name
);
391 blobmsg_close_table(&buf
, c
);
393 ubus_send_reply(ctx
, req
, buf
.head
);
397 return UBUS_STATUS_OK
;
401 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
402 struct ubus_request_data
*req
, const char *method
,
403 struct blob_attr
*msg
)
408 char res
[IWINFO_BUFSIZE
];
409 struct iwinfo_scanlist_entry
*e
;
411 rv
= rpc_iwinfo_open(msg
);
416 blob_buf_init(&buf
, 0);
418 c
= blobmsg_open_array(&buf
, "results");
420 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
422 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
424 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
425 d
= blobmsg_open_table(&buf
, NULL
);
428 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
430 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
431 e
->mac
[0], e
->mac
[1], e
->mac
[2],
432 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
434 blobmsg_add_string(&buf
, "bssid", mac
);
436 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
438 blobmsg_add_u32(&buf
, "channel", e
->channel
);
439 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
441 blobmsg_add_u32(&buf
, "quality", e
->quality
);
442 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
444 t
= blobmsg_open_table(&buf
, "ht_operation");
445 blobmsg_add_u32(&buf
, "primary_channel", e
->ht_chan_info
.primary_chan
);
446 blobmsg_add_string(&buf
, "secondary_channel_offset", ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
447 blobmsg_add_u32(&buf
, "channel_width", ht_chan_width
[e
->ht_chan_info
.chan_width
]);
448 blobmsg_close_table(&buf
, t
);
450 if (e
->vht_chan_info
.center_chan_1
) {
451 t
= blobmsg_open_table(&buf
, "vht_operation");
452 blobmsg_add_u32(&buf
, "channel_width", vht_chan_width
[e
->vht_chan_info
.chan_width
]);
453 blobmsg_add_u32(&buf
, "center_freq_1", e
->vht_chan_info
.center_chan_1
);
454 blobmsg_add_u32(&buf
, "center_freq_2", e
->vht_chan_info
.center_chan_2
);
455 blobmsg_close_table(&buf
, t
);
458 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
460 blobmsg_close_table(&buf
, d
);
464 blobmsg_close_array(&buf
, c
);
466 ubus_send_reply(ctx
, req
, buf
.head
);
470 return UBUS_STATUS_OK
;
474 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry
*r
)
476 blobmsg_add_u8(&buf
, "ht", r
->is_ht
);
477 blobmsg_add_u8(&buf
, "vht", r
->is_vht
);
478 blobmsg_add_u32(&buf
, "mhz", r
->mhz
);
479 blobmsg_add_u32(&buf
, "rate", r
->rate
);
482 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
483 blobmsg_add_u8(&buf
, "40mhz", r
->is_40mhz
);
484 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
486 else if (r
->is_vht
) {
487 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
488 blobmsg_add_u32(&buf
, "nss", r
->nss
);
489 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
494 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
495 struct ubus_request_data
*req
, const char *method
,
496 struct blob_attr
*msg
)
500 char res
[IWINFO_BUFSIZE
];
501 struct iwinfo_assoclist_entry
*a
;
502 struct ether_addr
*macaddr
= NULL
;
504 struct blob_attr
*tb
[__RPC_A_MAX
];
507 blobmsg_parse(rpc_assoclist_policy
, __RPC_A_MAX
, tb
,
508 blob_data(msg
), blob_len(msg
));
510 rv
= __rpc_iwinfo_open(tb
[RPC_A_DEVICE
]);
514 if (tb
[RPC_A_MACADDR
])
515 macaddr
= ether_aton(blobmsg_data(tb
[RPC_A_MACADDR
]));
517 blob_buf_init(&buf
, 0);
520 c
= blobmsg_open_array(&buf
, "results");
522 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
524 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
526 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
529 d
= blobmsg_open_table(&buf
, NULL
);
530 else if (memcmp(macaddr
, a
->mac
, ETH_ALEN
) != 0)
533 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
534 a
->mac
[0], a
->mac
[1], a
->mac
[2],
535 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
537 blobmsg_add_string(&buf
, "mac", mac
);
538 blobmsg_add_u32(&buf
, "signal", a
->signal
);
539 blobmsg_add_u32(&buf
, "signal_avg", a
->signal_avg
);
540 blobmsg_add_u32(&buf
, "noise", a
->noise
);
541 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
542 blobmsg_add_u32(&buf
, "connected_time", a
->connected_time
);
543 blobmsg_add_u32(&buf
, "thr", a
->thr
);
544 blobmsg_add_u8(&buf
, "authorized", a
->is_authorized
);
545 blobmsg_add_u8(&buf
, "authenticated", a
->is_authenticated
);
546 blobmsg_add_string(&buf
, "preamble", a
->is_preamble_short
? "short" : "long");
547 blobmsg_add_u8(&buf
, "wme", a
->is_wme
);
548 blobmsg_add_u8(&buf
, "mfp", a
->is_mfp
);
549 blobmsg_add_u8(&buf
, "tdls", a
->is_tdls
);
551 blobmsg_add_u16(&buf
, "mesh llid", a
->llid
);
552 blobmsg_add_u16(&buf
, "mesh plid", a
->plid
);
553 blobmsg_add_string(&buf
, "mesh plink", a
->plink_state
);
554 blobmsg_add_string(&buf
, "mesh local PS", a
->local_ps
);
555 blobmsg_add_string(&buf
, "mesh peer PS", a
->peer_ps
);
556 blobmsg_add_string(&buf
, "mesh non-peer PS", a
->nonpeer_ps
);
558 e
= blobmsg_open_table(&buf
, "rx");
559 blobmsg_add_u64(&buf
, "drop_misc", a
->rx_drop_misc
);
560 blobmsg_add_u32(&buf
, "packets", a
->rx_packets
);
561 blobmsg_add_u32(&buf
, "bytes", a
->rx_bytes
);
562 rpc_iwinfo_add_rateinfo(&a
->rx_rate
);
563 blobmsg_close_table(&buf
, e
);
565 e
= blobmsg_open_table(&buf
, "tx");
566 blobmsg_add_u32(&buf
, "failed", a
->tx_failed
);
567 blobmsg_add_u32(&buf
, "retries", a
->tx_retries
);
568 blobmsg_add_u32(&buf
, "packets", a
->tx_packets
);
569 blobmsg_add_u32(&buf
, "bytes", a
->tx_bytes
);
570 rpc_iwinfo_add_rateinfo(&a
->tx_rate
);
571 blobmsg_close_table(&buf
, e
);
575 blobmsg_close_table(&buf
, d
);
582 blobmsg_close_array(&buf
, c
);
584 return UBUS_STATUS_NOT_FOUND
;
586 ubus_send_reply(ctx
, req
, buf
.head
);
590 return UBUS_STATUS_OK
;
594 rpc_iwinfo_survey(struct ubus_context
*ctx
, struct ubus_object
*obj
,
595 struct ubus_request_data
*req
, const char *method
,
596 struct blob_attr
*msg
)
598 char res
[IWINFO_BUFSIZE
];
599 struct iwinfo_survey_entry
*e
;
603 blob_buf_init(&buf
, 0);
605 rv
= rpc_iwinfo_open(msg
);
607 c
= blobmsg_open_array(&buf
, "results");
609 if (rv
|| iw
->survey(ifname
, res
, &len
) || len
< 0)
610 return UBUS_STATUS_OK
;
612 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_survey_entry
)) {
613 e
= (struct iwinfo_survey_entry
*)&res
[i
];
615 d
= blobmsg_open_table(&buf
, NULL
);
616 blobmsg_add_u32(&buf
, "mhz", e
->mhz
);
617 blobmsg_add_u32(&buf
, "noise", e
->noise
);
618 blobmsg_add_u64(&buf
, "active_time", e
->active_time
);
619 blobmsg_add_u64(&buf
, "busy_time", e
->busy_time
);
620 blobmsg_add_u64(&buf
, "busy_time_ext", e
->busy_time_ext
);
621 blobmsg_add_u64(&buf
, "rx_time", e
->rxtime
);
622 blobmsg_add_u64(&buf
, "tx_time", e
->txtime
);
623 blobmsg_close_table(&buf
, d
);
626 blobmsg_close_array(&buf
, c
);
627 ubus_send_reply(ctx
, req
, buf
.head
);
629 return UBUS_STATUS_OK
;
633 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
634 struct ubus_request_data
*req
, const char *method
,
635 struct blob_attr
*msg
)
638 char res
[IWINFO_BUFSIZE
];
639 struct iwinfo_freqlist_entry
*f
;
642 rv
= rpc_iwinfo_open(msg
);
647 blob_buf_init(&buf
, 0);
649 c
= blobmsg_open_array(&buf
, "results");
651 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
653 if (iw
->channel(ifname
, &ch
))
656 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
658 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
659 d
= blobmsg_open_table(&buf
, NULL
);
661 blobmsg_add_u32(&buf
, "channel", f
->channel
);
662 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
663 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
666 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
668 blobmsg_close_table(&buf
, d
);
672 blobmsg_close_array(&buf
, c
);
674 ubus_send_reply(ctx
, req
, buf
.head
);
678 return UBUS_STATUS_OK
;
682 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
683 struct ubus_request_data
*req
, const char *method
,
684 struct blob_attr
*msg
)
686 int i
, rv
, len
, pwr
, off
;
687 char res
[IWINFO_BUFSIZE
];
688 struct iwinfo_txpwrlist_entry
*t
;
691 rv
= rpc_iwinfo_open(msg
);
696 blob_buf_init(&buf
, 0);
698 c
= blobmsg_open_array(&buf
, "results");
700 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
702 if (iw
->txpower(ifname
, &pwr
))
705 if (iw
->txpower_offset(ifname
, &off
))
708 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
710 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
711 d
= blobmsg_open_table(&buf
, NULL
);
713 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
714 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
717 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
719 blobmsg_close_table(&buf
, d
);
723 blobmsg_close_array(&buf
, c
);
725 ubus_send_reply(ctx
, req
, buf
.head
);
729 return UBUS_STATUS_OK
;
733 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
736 static char ccode
[5];
737 struct iwinfo_country_entry
*c
;
739 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
741 c
= (struct iwinfo_country_entry
*)&buf
[i
];
743 if (c
->iso3166
== iso3166
)
745 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
754 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
755 struct ubus_request_data
*req
, const char *method
,
756 struct blob_attr
*msg
)
761 char res
[IWINFO_BUFSIZE
] = {0};
763 const struct iwinfo_iso3166_label
*l
;
766 rv
= rpc_iwinfo_open(msg
);
771 blob_buf_init(&buf
, 0);
773 c
= blobmsg_open_array(&buf
, "results");
775 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
777 if (iw
->country(ifname
, cur
))
778 memset(cur
, 0, sizeof(cur
));
780 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
782 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
787 d
= blobmsg_open_table(&buf
, NULL
);
789 blobmsg_add_string(&buf
, "code", ccode
);
790 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
792 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
793 (l
->iso3166
/ 256), (l
->iso3166
% 256));
795 blobmsg_add_string(&buf
, "iso3166", iso3166
);
798 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
800 blobmsg_close_table(&buf
, d
);
804 blobmsg_close_array(&buf
, c
);
806 ubus_send_reply(ctx
, req
, buf
.head
);
810 return UBUS_STATUS_OK
;
814 rpc_iwinfo_phyname(struct ubus_context
*ctx
, struct ubus_object
*obj
,
815 struct ubus_request_data
*req
, const char *method
,
816 struct blob_attr
*msg
)
820 char res
[IWINFO_BUFSIZE
];
821 const struct iwinfo_ops
*ops
;
822 struct blob_attr
*tb
[__RPC_U_MAX
];
823 const char *backends
[] = {
829 blobmsg_parse(rpc_uci_policy
, __RPC_U_MAX
, tb
,
830 blob_data(msg
), blob_len(msg
));
832 if (!tb
[RPC_U_SECTION
])
833 return UBUS_STATUS_INVALID_ARGUMENT
;
835 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++)
837 ops
= iwinfo_backend_by_name(backends
[i
]);
839 if (!ops
|| !ops
->lookup_phy
)
842 if (!ops
->lookup_phy(blobmsg_get_string(tb
[RPC_U_SECTION
]), res
))
851 blob_buf_init(&buf
, 0);
852 blobmsg_add_string(&buf
, "phyname", res
);
854 ubus_send_reply(ctx
, req
, buf
.head
);
859 return found
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
863 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
864 struct ubus_request_data
*req
, const char *method
,
865 struct blob_attr
*msg
)
871 d
= opendir("/sys/class/net");
874 return UBUS_STATUS_UNKNOWN_ERROR
;
876 blob_buf_init(&buf
, 0);
878 c
= blobmsg_open_array(&buf
, "devices");
880 while ((e
= readdir(d
)) != NULL
)
882 if (e
->d_type
!= DT_LNK
)
885 if (iwinfo_type(e
->d_name
))
886 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
889 blobmsg_close_array(&buf
, c
);
893 ubus_send_reply(ctx
, req
, buf
.head
);
897 return UBUS_STATUS_OK
;
902 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
904 static const struct ubus_method iwinfo_methods
[] = {
905 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
906 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
907 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
908 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_assoclist_policy
),
909 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
910 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
911 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
912 UBUS_METHOD("survey", rpc_iwinfo_survey
, rpc_device_policy
),
913 UBUS_METHOD("phyname", rpc_iwinfo_phyname
, rpc_uci_policy
),
916 static struct ubus_object_type iwinfo_type
=
917 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
919 static struct ubus_object obj
= {
921 .type
= &iwinfo_type
,
922 .methods
= iwinfo_methods
,
923 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
926 return ubus_add_object(ctx
, &obj
);
929 struct rpc_plugin rpc_plugin
= {
930 .init
= rpc_iwinfo_api_init