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
;
306 if (!iw
->hwmodelist(ifname
, &modes
) && (modes
== IWINFO_80211_AD
)) {
307 blobmsg_add_string(&buf
, "hwmode", "ad");
311 if (iw
->htmode(ifname
, &htmode
))
315 case IWINFO_HTMODE_HT20
:
319 case IWINFO_HTMODE_HT40
:
323 case IWINFO_HTMODE_VHT80
:
324 htmode_str
= "VHT80";
327 case IWINFO_HTMODE_VHT80_80
:
328 htmode_str
= "VHT80+80";
331 case IWINFO_HTMODE_VHT160
:
332 htmode_str
= "VHT160";
335 case IWINFO_HTMODE_NOHT
:
340 htmode_str
= hwmode_str
= "unknown";
343 blobmsg_add_string(&buf
, "hwmode", hwmode_str
);
344 blobmsg_add_string(&buf
, "htmode", htmode_str
);
348 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
350 char rv
[IWINFO_BUFSIZE
] = { 0 };
352 if (!func(ifname
, rv
))
353 blobmsg_add_string(&buf
, name
, rv
);
357 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
358 struct ubus_request_data
*req
, const char *method
,
359 struct blob_attr
*msg
)
364 rv
= rpc_iwinfo_open(msg
);
369 blob_buf_init(&buf
, 0);
371 rpc_iwinfo_call_str("phy", iw
->phyname
);
373 rpc_iwinfo_call_str("ssid", iw
->ssid
);
374 rpc_iwinfo_call_str("bssid", iw
->bssid
);
375 rpc_iwinfo_call_str("country", iw
->country
);
377 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
378 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
379 rpc_iwinfo_call_int("center_chan1", iw
->center_chan1
, NULL
);
380 rpc_iwinfo_call_int("center_chan2", iw
->center_chan2
, NULL
);
382 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
383 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
385 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
386 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
388 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
389 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
391 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
392 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
394 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
396 rpc_iwinfo_call_encryption("encryption");
397 rpc_iwinfo_call_htmodes("htmodes");
398 rpc_iwinfo_call_hwmodes("hwmodes");
400 rpc_iwinfo_call_hw_ht_mode();
402 c
= blobmsg_open_table(&buf
, "hardware");
403 rpc_iwinfo_call_hardware_id("id");
404 rpc_iwinfo_call_str("name", iw
->hardware_name
);
405 blobmsg_close_table(&buf
, c
);
407 ubus_send_reply(ctx
, req
, buf
.head
);
411 return UBUS_STATUS_OK
;
415 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
416 struct ubus_request_data
*req
, const char *method
,
417 struct blob_attr
*msg
)
422 char res
[IWINFO_BUFSIZE
];
423 struct iwinfo_scanlist_entry
*e
;
425 rv
= rpc_iwinfo_open(msg
);
430 blob_buf_init(&buf
, 0);
432 c
= blobmsg_open_array(&buf
, "results");
434 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
436 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
438 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
439 d
= blobmsg_open_table(&buf
, NULL
);
442 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
444 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
445 e
->mac
[0], e
->mac
[1], e
->mac
[2],
446 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
448 blobmsg_add_string(&buf
, "bssid", mac
);
450 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
452 blobmsg_add_u32(&buf
, "channel", e
->channel
);
453 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
455 blobmsg_add_u32(&buf
, "quality", e
->quality
);
456 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
458 t
= blobmsg_open_table(&buf
, "ht_operation");
459 blobmsg_add_u32(&buf
, "primary_channel", e
->ht_chan_info
.primary_chan
);
460 blobmsg_add_string(&buf
, "secondary_channel_offset", ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
461 blobmsg_add_u32(&buf
, "channel_width", ht_chan_width
[e
->ht_chan_info
.chan_width
]);
462 blobmsg_close_table(&buf
, t
);
464 if (e
->vht_chan_info
.center_chan_1
) {
465 t
= blobmsg_open_table(&buf
, "vht_operation");
466 blobmsg_add_u32(&buf
, "channel_width", vht_chan_width
[e
->vht_chan_info
.chan_width
]);
467 blobmsg_add_u32(&buf
, "center_freq_1", e
->vht_chan_info
.center_chan_1
);
468 blobmsg_add_u32(&buf
, "center_freq_2", e
->vht_chan_info
.center_chan_2
);
469 blobmsg_close_table(&buf
, t
);
472 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
474 blobmsg_close_table(&buf
, d
);
478 blobmsg_close_array(&buf
, c
);
480 ubus_send_reply(ctx
, req
, buf
.head
);
484 return UBUS_STATUS_OK
;
488 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry
*r
)
490 blobmsg_add_u8(&buf
, "ht", r
->is_ht
);
491 blobmsg_add_u8(&buf
, "vht", r
->is_vht
);
492 blobmsg_add_u32(&buf
, "mhz", r
->mhz
);
493 blobmsg_add_u32(&buf
, "rate", r
->rate
);
496 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
497 blobmsg_add_u8(&buf
, "40mhz", r
->is_40mhz
);
498 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
500 else if (r
->is_vht
) {
501 blobmsg_add_u32(&buf
, "mcs", r
->mcs
);
502 blobmsg_add_u32(&buf
, "nss", r
->nss
);
503 blobmsg_add_u8(&buf
, "short_gi", r
->is_short_gi
);
508 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
509 struct ubus_request_data
*req
, const char *method
,
510 struct blob_attr
*msg
)
514 char res
[IWINFO_BUFSIZE
];
515 struct iwinfo_assoclist_entry
*a
;
516 struct ether_addr
*macaddr
= NULL
;
518 struct blob_attr
*tb
[__RPC_A_MAX
];
521 blobmsg_parse(rpc_assoclist_policy
, __RPC_A_MAX
, tb
,
522 blob_data(msg
), blob_len(msg
));
524 rv
= __rpc_iwinfo_open(tb
[RPC_A_DEVICE
]);
528 if (tb
[RPC_A_MACADDR
])
529 macaddr
= ether_aton(blobmsg_data(tb
[RPC_A_MACADDR
]));
531 blob_buf_init(&buf
, 0);
534 c
= blobmsg_open_array(&buf
, "results");
536 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
538 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
540 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
543 d
= blobmsg_open_table(&buf
, NULL
);
544 else if (memcmp(macaddr
, a
->mac
, ETH_ALEN
) != 0)
547 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
548 a
->mac
[0], a
->mac
[1], a
->mac
[2],
549 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
551 blobmsg_add_string(&buf
, "mac", mac
);
552 blobmsg_add_u32(&buf
, "signal", a
->signal
);
553 blobmsg_add_u32(&buf
, "signal_avg", a
->signal_avg
);
554 blobmsg_add_u32(&buf
, "noise", a
->noise
);
555 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
556 blobmsg_add_u32(&buf
, "connected_time", a
->connected_time
);
557 blobmsg_add_u32(&buf
, "thr", a
->thr
);
558 blobmsg_add_u8(&buf
, "authorized", a
->is_authorized
);
559 blobmsg_add_u8(&buf
, "authenticated", a
->is_authenticated
);
560 blobmsg_add_string(&buf
, "preamble", a
->is_preamble_short
? "short" : "long");
561 blobmsg_add_u8(&buf
, "wme", a
->is_wme
);
562 blobmsg_add_u8(&buf
, "mfp", a
->is_mfp
);
563 blobmsg_add_u8(&buf
, "tdls", a
->is_tdls
);
565 blobmsg_add_u16(&buf
, "mesh llid", a
->llid
);
566 blobmsg_add_u16(&buf
, "mesh plid", a
->plid
);
567 blobmsg_add_string(&buf
, "mesh plink", a
->plink_state
);
568 blobmsg_add_string(&buf
, "mesh local PS", a
->local_ps
);
569 blobmsg_add_string(&buf
, "mesh peer PS", a
->peer_ps
);
570 blobmsg_add_string(&buf
, "mesh non-peer PS", a
->nonpeer_ps
);
572 e
= blobmsg_open_table(&buf
, "rx");
573 blobmsg_add_u64(&buf
, "drop_misc", a
->rx_drop_misc
);
574 blobmsg_add_u32(&buf
, "packets", a
->rx_packets
);
575 blobmsg_add_u32(&buf
, "bytes", a
->rx_bytes
);
576 rpc_iwinfo_add_rateinfo(&a
->rx_rate
);
577 blobmsg_close_table(&buf
, e
);
579 e
= blobmsg_open_table(&buf
, "tx");
580 blobmsg_add_u32(&buf
, "failed", a
->tx_failed
);
581 blobmsg_add_u32(&buf
, "retries", a
->tx_retries
);
582 blobmsg_add_u32(&buf
, "packets", a
->tx_packets
);
583 blobmsg_add_u32(&buf
, "bytes", a
->tx_bytes
);
584 rpc_iwinfo_add_rateinfo(&a
->tx_rate
);
585 blobmsg_close_table(&buf
, e
);
589 blobmsg_close_table(&buf
, d
);
596 blobmsg_close_array(&buf
, c
);
598 return UBUS_STATUS_NOT_FOUND
;
600 ubus_send_reply(ctx
, req
, buf
.head
);
604 return UBUS_STATUS_OK
;
608 rpc_iwinfo_survey(struct ubus_context
*ctx
, struct ubus_object
*obj
,
609 struct ubus_request_data
*req
, const char *method
,
610 struct blob_attr
*msg
)
612 char res
[IWINFO_BUFSIZE
];
613 struct iwinfo_survey_entry
*e
;
617 blob_buf_init(&buf
, 0);
619 rv
= rpc_iwinfo_open(msg
);
621 c
= blobmsg_open_array(&buf
, "results");
623 if (rv
|| iw
->survey(ifname
, res
, &len
) || len
< 0)
624 return UBUS_STATUS_OK
;
626 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_survey_entry
)) {
627 e
= (struct iwinfo_survey_entry
*)&res
[i
];
629 d
= blobmsg_open_table(&buf
, NULL
);
630 blobmsg_add_u32(&buf
, "mhz", e
->mhz
);
631 blobmsg_add_u32(&buf
, "noise", e
->noise
);
632 blobmsg_add_u64(&buf
, "active_time", e
->active_time
);
633 blobmsg_add_u64(&buf
, "busy_time", e
->busy_time
);
634 blobmsg_add_u64(&buf
, "busy_time_ext", e
->busy_time_ext
);
635 blobmsg_add_u64(&buf
, "rx_time", e
->rxtime
);
636 blobmsg_add_u64(&buf
, "tx_time", e
->txtime
);
637 blobmsg_close_table(&buf
, d
);
640 blobmsg_close_array(&buf
, c
);
641 ubus_send_reply(ctx
, req
, buf
.head
);
643 return UBUS_STATUS_OK
;
647 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
648 struct ubus_request_data
*req
, const char *method
,
649 struct blob_attr
*msg
)
652 char res
[IWINFO_BUFSIZE
];
653 struct iwinfo_freqlist_entry
*f
;
656 rv
= rpc_iwinfo_open(msg
);
661 blob_buf_init(&buf
, 0);
663 c
= blobmsg_open_array(&buf
, "results");
665 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
667 if (iw
->channel(ifname
, &ch
))
670 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
672 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
673 d
= blobmsg_open_table(&buf
, NULL
);
675 blobmsg_add_u32(&buf
, "channel", f
->channel
);
676 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
677 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
680 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
682 blobmsg_close_table(&buf
, d
);
686 blobmsg_close_array(&buf
, c
);
688 ubus_send_reply(ctx
, req
, buf
.head
);
692 return UBUS_STATUS_OK
;
696 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
697 struct ubus_request_data
*req
, const char *method
,
698 struct blob_attr
*msg
)
700 int i
, rv
, len
, pwr
, off
;
701 char res
[IWINFO_BUFSIZE
];
702 struct iwinfo_txpwrlist_entry
*t
;
705 rv
= rpc_iwinfo_open(msg
);
710 blob_buf_init(&buf
, 0);
712 c
= blobmsg_open_array(&buf
, "results");
714 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
716 if (iw
->txpower(ifname
, &pwr
))
719 if (iw
->txpower_offset(ifname
, &off
))
722 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
724 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
725 d
= blobmsg_open_table(&buf
, NULL
);
727 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
728 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
731 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
733 blobmsg_close_table(&buf
, d
);
737 blobmsg_close_array(&buf
, c
);
739 ubus_send_reply(ctx
, req
, buf
.head
);
743 return UBUS_STATUS_OK
;
747 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
750 static char ccode
[5];
751 struct iwinfo_country_entry
*c
;
753 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
755 c
= (struct iwinfo_country_entry
*)&buf
[i
];
757 if (c
->iso3166
== iso3166
)
759 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
768 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
769 struct ubus_request_data
*req
, const char *method
,
770 struct blob_attr
*msg
)
775 char res
[IWINFO_BUFSIZE
] = {0};
777 const struct iwinfo_iso3166_label
*l
;
780 rv
= rpc_iwinfo_open(msg
);
785 blob_buf_init(&buf
, 0);
787 c
= blobmsg_open_array(&buf
, "results");
789 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
791 if (iw
->country(ifname
, cur
))
792 memset(cur
, 0, sizeof(cur
));
794 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
796 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
801 d
= blobmsg_open_table(&buf
, NULL
);
803 blobmsg_add_string(&buf
, "code", ccode
);
804 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
806 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
807 (l
->iso3166
/ 256), (l
->iso3166
% 256));
809 blobmsg_add_string(&buf
, "iso3166", iso3166
);
812 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
814 blobmsg_close_table(&buf
, d
);
818 blobmsg_close_array(&buf
, c
);
820 ubus_send_reply(ctx
, req
, buf
.head
);
824 return UBUS_STATUS_OK
;
828 rpc_iwinfo_phyname(struct ubus_context
*ctx
, struct ubus_object
*obj
,
829 struct ubus_request_data
*req
, const char *method
,
830 struct blob_attr
*msg
)
834 char res
[IWINFO_BUFSIZE
];
835 const struct iwinfo_ops
*ops
;
836 struct blob_attr
*tb
[__RPC_U_MAX
];
837 const char *backends
[] = {
843 blobmsg_parse(rpc_uci_policy
, __RPC_U_MAX
, tb
,
844 blob_data(msg
), blob_len(msg
));
846 if (!tb
[RPC_U_SECTION
])
847 return UBUS_STATUS_INVALID_ARGUMENT
;
849 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++)
851 ops
= iwinfo_backend_by_name(backends
[i
]);
853 if (!ops
|| !ops
->lookup_phy
)
856 if (!ops
->lookup_phy(blobmsg_get_string(tb
[RPC_U_SECTION
]), res
))
865 blob_buf_init(&buf
, 0);
866 blobmsg_add_string(&buf
, "phyname", res
);
868 ubus_send_reply(ctx
, req
, buf
.head
);
873 return found
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
877 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
878 struct ubus_request_data
*req
, const char *method
,
879 struct blob_attr
*msg
)
885 d
= opendir("/sys/class/net");
888 return UBUS_STATUS_UNKNOWN_ERROR
;
890 blob_buf_init(&buf
, 0);
892 c
= blobmsg_open_array(&buf
, "devices");
894 while ((e
= readdir(d
)) != NULL
)
896 if (e
->d_type
!= DT_LNK
)
899 if (iwinfo_type(e
->d_name
))
900 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
903 blobmsg_close_array(&buf
, c
);
907 ubus_send_reply(ctx
, req
, buf
.head
);
911 return UBUS_STATUS_OK
;
916 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
918 static const struct ubus_method iwinfo_methods
[] = {
919 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
920 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
921 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
922 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_assoclist_policy
),
923 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
924 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
925 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
926 UBUS_METHOD("survey", rpc_iwinfo_survey
, rpc_device_policy
),
927 UBUS_METHOD("phyname", rpc_iwinfo_phyname
, rpc_uci_policy
),
930 static struct ubus_object_type iwinfo_type
=
931 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
933 static struct ubus_object obj
= {
935 .type
= &iwinfo_type
,
936 .methods
= iwinfo_methods
,
937 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
940 return ubus_add_object(ctx
, &obj
);
943 struct rpc_plugin rpc_plugin
= {
944 .init
= rpc_iwinfo_api_init