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 blobmsg_close_array(&buf
, c
);
268 rpc_iwinfo_call_hwmodes(const char *name
)
273 if (!iw
->hwmodelist(ifname
, &modes
))
275 c
= blobmsg_open_array(&buf
, name
);
277 if (modes
& IWINFO_80211_AD
)
278 blobmsg_add_string(&buf
, NULL
, "ad");
280 if (modes
& IWINFO_80211_AC
)
281 blobmsg_add_string(&buf
, NULL
, "ac");
283 if (modes
& IWINFO_80211_A
)
284 blobmsg_add_string(&buf
, NULL
, "a");
286 if (modes
& IWINFO_80211_B
)
287 blobmsg_add_string(&buf
, NULL
, "b");
289 if (modes
& IWINFO_80211_G
)
290 blobmsg_add_string(&buf
, NULL
, "g");
292 if (modes
& IWINFO_80211_N
)
293 blobmsg_add_string(&buf
, NULL
, "n");
295 blobmsg_close_array(&buf
, c
);
299 static void rpc_iwinfo_call_hw_ht_mode()
301 const char *hwmode_str
;
302 const char *htmode_str
;
305 if (iw
->htmode(ifname
, &htmode
))
309 case IWINFO_HTMODE_HT20
:
313 case IWINFO_HTMODE_HT40
:
317 case IWINFO_HTMODE_VHT80
:
318 htmode_str
= "VHT80";
321 case IWINFO_HTMODE_VHT80_80
:
322 htmode_str
= "VHT80+80";
325 case IWINFO_HTMODE_VHT160
:
326 htmode_str
= "VHT160";
329 case IWINFO_HTMODE_NOHT
:
334 htmode_str
= hwmode_str
= "unknown";
337 blobmsg_add_string(&buf
, "hwmode", hwmode_str
);
338 blobmsg_add_string(&buf
, "htmode", htmode_str
);
342 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
344 char rv
[IWINFO_BUFSIZE
] = { 0 };
346 if (!func(ifname
, rv
))
347 blobmsg_add_string(&buf
, name
, rv
);
351 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
352 struct ubus_request_data
*req
, const char *method
,
353 struct blob_attr
*msg
)
358 rv
= rpc_iwinfo_open(msg
);
363 blob_buf_init(&buf
, 0);
365 rpc_iwinfo_call_str("phy", iw
->phyname
);
367 rpc_iwinfo_call_str("ssid", iw
->ssid
);
368 rpc_iwinfo_call_str("bssid", iw
->bssid
);
369 rpc_iwinfo_call_str("country", iw
->country
);
371 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
372 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
373 rpc_iwinfo_call_int("center_chan1", iw
->center_chan1
, NULL
);
374 rpc_iwinfo_call_int("center_chan2", iw
->center_chan2
, NULL
);
376 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
377 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
379 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
380 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
382 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
383 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
385 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
386 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
388 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
390 rpc_iwinfo_call_encryption("encryption");
391 rpc_iwinfo_call_htmodes("htmodes");
392 rpc_iwinfo_call_hwmodes("hwmodes");
394 rpc_iwinfo_call_hw_ht_mode();
396 c
= blobmsg_open_table(&buf
, "hardware");
397 rpc_iwinfo_call_hardware_id("id");
398 rpc_iwinfo_call_str("name", iw
->hardware_name
);
399 blobmsg_close_table(&buf
, c
);
401 ubus_send_reply(ctx
, req
, buf
.head
);
405 return UBUS_STATUS_OK
;
409 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
410 struct ubus_request_data
*req
, const char *method
,
411 struct blob_attr
*msg
)
416 char res
[IWINFO_BUFSIZE
];
417 struct iwinfo_scanlist_entry
*e
;
419 rv
= rpc_iwinfo_open(msg
);
424 blob_buf_init(&buf
, 0);
426 c
= blobmsg_open_array(&buf
, "results");
428 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
430 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
432 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
433 d
= blobmsg_open_table(&buf
, NULL
);
436 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
438 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
439 e
->mac
[0], e
->mac
[1], e
->mac
[2],
440 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
442 blobmsg_add_string(&buf
, "bssid", mac
);
444 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
446 blobmsg_add_u32(&buf
, "channel", e
->channel
);
447 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
449 blobmsg_add_u32(&buf
, "quality", e
->quality
);
450 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
452 t
= blobmsg_open_table(&buf
, "ht_operation");
453 blobmsg_add_u32(&buf
, "primary_channel", e
->ht_chan_info
.primary_chan
);
454 blobmsg_add_string(&buf
, "secondary_channel_offset", ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
455 blobmsg_add_u32(&buf
, "channel_width", ht_chan_width
[e
->ht_chan_info
.chan_width
]);
456 blobmsg_close_table(&buf
, t
);
458 if (e
->vht_chan_info
.center_chan_1
) {
459 t
= blobmsg_open_table(&buf
, "vht_operation");
460 blobmsg_add_u32(&buf
, "channel_width", vht_chan_width
[e
->vht_chan_info
.chan_width
]);
461 blobmsg_add_u32(&buf
, "center_freq_1", e
->vht_chan_info
.center_chan_1
);
462 blobmsg_add_u32(&buf
, "center_freq_2", e
->vht_chan_info
.center_chan_2
);
463 blobmsg_close_table(&buf
, t
);
466 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
468 blobmsg_close_table(&buf
, d
);
472 blobmsg_close_array(&buf
, c
);
474 ubus_send_reply(ctx
, req
, buf
.head
);
478 return UBUS_STATUS_OK
;
482 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry
*r
)
484 blobmsg_add_u8(&buf
, "ht", r
->is_ht
);
485 blobmsg_add_u8(&buf
, "vht", r
->is_vht
);
486 blobmsg_add_u32(&buf
, "mhz", r
->mhz
);
487 blobmsg_add_u32(&buf
, "rate", r
->rate
);
490 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
491 blobmsg_add_u8(&buf
, "40mhz", r
->is_40mhz
);
492 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
494 else if (r
->is_vht
) {
495 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
496 blobmsg_add_u32(&buf
, "nss", r
->nss
);
497 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
502 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
503 struct ubus_request_data
*req
, const char *method
,
504 struct blob_attr
*msg
)
508 char res
[IWINFO_BUFSIZE
];
509 struct iwinfo_assoclist_entry
*a
;
510 struct ether_addr
*macaddr
= NULL
;
512 struct blob_attr
*tb
[__RPC_A_MAX
];
515 blobmsg_parse(rpc_assoclist_policy
, __RPC_A_MAX
, tb
,
516 blob_data(msg
), blob_len(msg
));
518 rv
= __rpc_iwinfo_open(tb
[RPC_A_DEVICE
]);
522 if (tb
[RPC_A_MACADDR
])
523 macaddr
= ether_aton(blobmsg_data(tb
[RPC_A_MACADDR
]));
525 blob_buf_init(&buf
, 0);
528 c
= blobmsg_open_array(&buf
, "results");
530 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
532 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
534 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
537 d
= blobmsg_open_table(&buf
, NULL
);
538 else if (memcmp(macaddr
, a
->mac
, ETH_ALEN
) != 0)
541 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
542 a
->mac
[0], a
->mac
[1], a
->mac
[2],
543 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
545 blobmsg_add_string(&buf
, "mac", mac
);
546 blobmsg_add_u32(&buf
, "signal", a
->signal
);
547 blobmsg_add_u32(&buf
, "signal_avg", a
->signal_avg
);
548 blobmsg_add_u32(&buf
, "noise", a
->noise
);
549 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
550 blobmsg_add_u32(&buf
, "connected_time", a
->connected_time
);
551 blobmsg_add_u32(&buf
, "thr", a
->thr
);
552 blobmsg_add_u8(&buf
, "authorized", a
->is_authorized
);
553 blobmsg_add_u8(&buf
, "authenticated", a
->is_authenticated
);
554 blobmsg_add_string(&buf
, "preamble", a
->is_preamble_short
? "short" : "long");
555 blobmsg_add_u8(&buf
, "wme", a
->is_wme
);
556 blobmsg_add_u8(&buf
, "mfp", a
->is_mfp
);
557 blobmsg_add_u8(&buf
, "tdls", a
->is_tdls
);
559 blobmsg_add_u16(&buf
, "mesh llid", a
->llid
);
560 blobmsg_add_u16(&buf
, "mesh plid", a
->plid
);
561 blobmsg_add_string(&buf
, "mesh plink", a
->plink_state
);
562 blobmsg_add_string(&buf
, "mesh local PS", a
->local_ps
);
563 blobmsg_add_string(&buf
, "mesh peer PS", a
->peer_ps
);
564 blobmsg_add_string(&buf
, "mesh non-peer PS", a
->nonpeer_ps
);
566 e
= blobmsg_open_table(&buf
, "rx");
567 blobmsg_add_u64(&buf
, "drop_misc", a
->rx_drop_misc
);
568 blobmsg_add_u32(&buf
, "packets", a
->rx_packets
);
569 blobmsg_add_u32(&buf
, "bytes", a
->rx_bytes
);
570 rpc_iwinfo_add_rateinfo(&a
->rx_rate
);
571 blobmsg_close_table(&buf
, e
);
573 e
= blobmsg_open_table(&buf
, "tx");
574 blobmsg_add_u32(&buf
, "failed", a
->tx_failed
);
575 blobmsg_add_u32(&buf
, "retries", a
->tx_retries
);
576 blobmsg_add_u32(&buf
, "packets", a
->tx_packets
);
577 blobmsg_add_u32(&buf
, "bytes", a
->tx_bytes
);
578 rpc_iwinfo_add_rateinfo(&a
->tx_rate
);
579 blobmsg_close_table(&buf
, e
);
583 blobmsg_close_table(&buf
, d
);
590 blobmsg_close_array(&buf
, c
);
592 return UBUS_STATUS_NOT_FOUND
;
594 ubus_send_reply(ctx
, req
, buf
.head
);
598 return UBUS_STATUS_OK
;
602 rpc_iwinfo_survey(struct ubus_context
*ctx
, struct ubus_object
*obj
,
603 struct ubus_request_data
*req
, const char *method
,
604 struct blob_attr
*msg
)
606 char res
[IWINFO_BUFSIZE
];
607 struct iwinfo_survey_entry
*e
;
611 blob_buf_init(&buf
, 0);
613 rv
= rpc_iwinfo_open(msg
);
615 c
= blobmsg_open_array(&buf
, "results");
617 if (rv
|| iw
->survey(ifname
, res
, &len
) || len
< 0)
618 return UBUS_STATUS_OK
;
620 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_survey_entry
)) {
621 e
= (struct iwinfo_survey_entry
*)&res
[i
];
623 d
= blobmsg_open_table(&buf
, NULL
);
624 blobmsg_add_u32(&buf
, "mhz", e
->mhz
);
625 blobmsg_add_u32(&buf
, "noise", e
->noise
);
626 blobmsg_add_u64(&buf
, "active_time", e
->active_time
);
627 blobmsg_add_u64(&buf
, "busy_time", e
->busy_time
);
628 blobmsg_add_u64(&buf
, "busy_time_ext", e
->busy_time_ext
);
629 blobmsg_add_u64(&buf
, "rx_time", e
->rxtime
);
630 blobmsg_add_u64(&buf
, "tx_time", e
->txtime
);
631 blobmsg_close_table(&buf
, d
);
634 blobmsg_close_array(&buf
, c
);
635 ubus_send_reply(ctx
, req
, buf
.head
);
637 return UBUS_STATUS_OK
;
641 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
642 struct ubus_request_data
*req
, const char *method
,
643 struct blob_attr
*msg
)
646 char res
[IWINFO_BUFSIZE
];
647 struct iwinfo_freqlist_entry
*f
;
650 rv
= rpc_iwinfo_open(msg
);
655 blob_buf_init(&buf
, 0);
657 c
= blobmsg_open_array(&buf
, "results");
659 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
661 if (iw
->channel(ifname
, &ch
))
664 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
666 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
667 d
= blobmsg_open_table(&buf
, NULL
);
669 blobmsg_add_u32(&buf
, "channel", f
->channel
);
670 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
671 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
674 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
676 blobmsg_close_table(&buf
, d
);
680 blobmsg_close_array(&buf
, c
);
682 ubus_send_reply(ctx
, req
, buf
.head
);
686 return UBUS_STATUS_OK
;
690 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
691 struct ubus_request_data
*req
, const char *method
,
692 struct blob_attr
*msg
)
694 int i
, rv
, len
, pwr
, off
;
695 char res
[IWINFO_BUFSIZE
];
696 struct iwinfo_txpwrlist_entry
*t
;
699 rv
= rpc_iwinfo_open(msg
);
704 blob_buf_init(&buf
, 0);
706 c
= blobmsg_open_array(&buf
, "results");
708 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
710 if (iw
->txpower(ifname
, &pwr
))
713 if (iw
->txpower_offset(ifname
, &off
))
716 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
718 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
719 d
= blobmsg_open_table(&buf
, NULL
);
721 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
722 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
725 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
727 blobmsg_close_table(&buf
, d
);
731 blobmsg_close_array(&buf
, c
);
733 ubus_send_reply(ctx
, req
, buf
.head
);
737 return UBUS_STATUS_OK
;
741 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
744 static char ccode
[5];
745 struct iwinfo_country_entry
*c
;
747 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
749 c
= (struct iwinfo_country_entry
*)&buf
[i
];
751 if (c
->iso3166
== iso3166
)
753 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
762 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
763 struct ubus_request_data
*req
, const char *method
,
764 struct blob_attr
*msg
)
769 char res
[IWINFO_BUFSIZE
] = {0};
771 const struct iwinfo_iso3166_label
*l
;
774 rv
= rpc_iwinfo_open(msg
);
779 blob_buf_init(&buf
, 0);
781 c
= blobmsg_open_array(&buf
, "results");
783 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
785 if (iw
->country(ifname
, cur
))
786 memset(cur
, 0, sizeof(cur
));
788 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
790 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
795 d
= blobmsg_open_table(&buf
, NULL
);
797 blobmsg_add_string(&buf
, "code", ccode
);
798 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
800 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
801 (l
->iso3166
/ 256), (l
->iso3166
% 256));
803 blobmsg_add_string(&buf
, "iso3166", iso3166
);
806 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
808 blobmsg_close_table(&buf
, d
);
812 blobmsg_close_array(&buf
, c
);
814 ubus_send_reply(ctx
, req
, buf
.head
);
818 return UBUS_STATUS_OK
;
822 rpc_iwinfo_phyname(struct ubus_context
*ctx
, struct ubus_object
*obj
,
823 struct ubus_request_data
*req
, const char *method
,
824 struct blob_attr
*msg
)
828 char res
[IWINFO_BUFSIZE
];
829 const struct iwinfo_ops
*ops
;
830 struct blob_attr
*tb
[__RPC_U_MAX
];
831 const char *backends
[] = {
837 blobmsg_parse(rpc_uci_policy
, __RPC_U_MAX
, tb
,
838 blob_data(msg
), blob_len(msg
));
840 if (!tb
[RPC_U_SECTION
])
841 return UBUS_STATUS_INVALID_ARGUMENT
;
843 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++)
845 ops
= iwinfo_backend_by_name(backends
[i
]);
847 if (!ops
|| !ops
->lookup_phy
)
850 if (!ops
->lookup_phy(blobmsg_get_string(tb
[RPC_U_SECTION
]), res
))
859 blob_buf_init(&buf
, 0);
860 blobmsg_add_string(&buf
, "phyname", res
);
862 ubus_send_reply(ctx
, req
, buf
.head
);
867 return found
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
871 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
872 struct ubus_request_data
*req
, const char *method
,
873 struct blob_attr
*msg
)
879 d
= opendir("/sys/class/net");
882 return UBUS_STATUS_UNKNOWN_ERROR
;
884 blob_buf_init(&buf
, 0);
886 c
= blobmsg_open_array(&buf
, "devices");
888 while ((e
= readdir(d
)) != NULL
)
890 if (e
->d_type
!= DT_LNK
)
893 if (iwinfo_type(e
->d_name
))
894 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
897 blobmsg_close_array(&buf
, c
);
901 ubus_send_reply(ctx
, req
, buf
.head
);
905 return UBUS_STATUS_OK
;
910 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
912 static const struct ubus_method iwinfo_methods
[] = {
913 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
914 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
915 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
916 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_assoclist_policy
),
917 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
918 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
919 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
920 UBUS_METHOD("survey", rpc_iwinfo_survey
, rpc_device_policy
),
921 UBUS_METHOD("phyname", rpc_iwinfo_phyname
, rpc_uci_policy
),
924 static struct ubus_object_type iwinfo_type
=
925 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
927 static struct ubus_object obj
= {
929 .type
= &iwinfo_type
,
930 .methods
= iwinfo_methods
,
931 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
934 return ubus_add_object(ctx
, &obj
);
937 struct rpc_plugin rpc_plugin
= {
938 .init
= rpc_iwinfo_api_init