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>
25 #include <rpcd/plugin.h>
28 static struct blob_buf buf
;
29 static const struct iwinfo_ops
*iw
;
30 static const char *ifname
;
37 static const struct blobmsg_policy rpc_device_policy
[__RPC_D_MAX
] = {
38 [RPC_D_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
43 rpc_iwinfo_open(struct blob_attr
*msg
)
45 static struct blob_attr
*tb
[__RPC_D_MAX
];
47 blobmsg_parse(rpc_device_policy
, __RPC_D_MAX
, tb
,
48 blob_data(msg
), blob_len(msg
));
50 if (!tb
[RPC_D_DEVICE
])
51 return UBUS_STATUS_INVALID_ARGUMENT
;
53 ifname
= blobmsg_data(tb
[RPC_D_DEVICE
]);
54 iw
= iwinfo_backend(ifname
);
56 return iw
? UBUS_STATUS_OK
: UBUS_STATUS_NOT_FOUND
;
60 rpc_iwinfo_close(void)
68 rpc_iwinfo_call_int(const char *name
, int (*func
)(const char *, int *),
73 if (!func(ifname
, &rv
))
76 blobmsg_add_u32(&buf
, name
, rv
);
78 blobmsg_add_string(&buf
, name
, map
[rv
]);
83 rpc_iwinfo_call_hardware_id(const char *name
)
85 struct iwinfo_hardware_id ids
;
88 if (!iw
->hardware_id(ifname
, (char *)&ids
))
90 c
= blobmsg_open_array(&buf
, name
);
92 blobmsg_add_u32(&buf
, NULL
, ids
.vendor_id
);
93 blobmsg_add_u32(&buf
, NULL
, ids
.device_id
);
94 blobmsg_add_u32(&buf
, NULL
, ids
.subsystem_vendor_id
);
95 blobmsg_add_u32(&buf
, NULL
, ids
.subsystem_device_id
);
97 blobmsg_close_array(&buf
, c
);
102 rpc_iwinfo_add_encryption(const char *name
, struct iwinfo_crypto_entry
*e
)
107 c
= blobmsg_open_table(&buf
, name
);
109 blobmsg_add_u8(&buf
, "enabled", e
->enabled
);
115 d
= blobmsg_open_array(&buf
, "wep");
117 if (e
->auth_algs
& IWINFO_AUTH_OPEN
)
118 blobmsg_add_string(&buf
, NULL
, "open");
120 if (e
->auth_algs
& IWINFO_AUTH_SHARED
)
121 blobmsg_add_string(&buf
, NULL
, "shared");
123 blobmsg_close_array(&buf
, d
);
127 d
= blobmsg_open_array(&buf
, "wpa");
129 if (e
->wpa_version
> 2)
131 blobmsg_add_u32(&buf
, NULL
, 1);
132 blobmsg_add_u32(&buf
, NULL
, 2);
136 blobmsg_add_u32(&buf
, NULL
, e
->wpa_version
);
139 blobmsg_close_array(&buf
, d
);
142 d
= blobmsg_open_array(&buf
, "authentication");
144 if (e
->auth_suites
& IWINFO_KMGMT_PSK
)
145 blobmsg_add_string(&buf
, NULL
, "psk");
147 if (e
->auth_suites
& IWINFO_KMGMT_8021x
)
148 blobmsg_add_string(&buf
, NULL
, "802.1x");
150 if (!e
->auth_suites
||
151 (e
->auth_suites
& IWINFO_KMGMT_NONE
))
152 blobmsg_add_string(&buf
, NULL
, "none");
154 blobmsg_close_array(&buf
, d
);
157 d
= blobmsg_open_array(&buf
, "ciphers");
158 ciph
= e
->pair_ciphers
| e
->group_ciphers
;
160 if (ciph
& IWINFO_CIPHER_WEP40
)
161 blobmsg_add_string(&buf
, NULL
, "wep-40");
163 if (ciph
& IWINFO_CIPHER_WEP104
)
164 blobmsg_add_string(&buf
, NULL
, "wep-104");
166 if (ciph
& IWINFO_CIPHER_TKIP
)
167 blobmsg_add_string(&buf
, NULL
, "tkip");
169 if (ciph
& IWINFO_CIPHER_CCMP
)
170 blobmsg_add_string(&buf
, NULL
, "ccmp");
172 if (ciph
& IWINFO_CIPHER_WRAP
)
173 blobmsg_add_string(&buf
, NULL
, "wrap");
175 if (ciph
& IWINFO_CIPHER_AESOCB
)
176 blobmsg_add_string(&buf
, NULL
, "aes-ocb");
178 if (ciph
& IWINFO_CIPHER_CKIP
)
179 blobmsg_add_string(&buf
, NULL
, "ckip");
181 if (!ciph
|| (ciph
& IWINFO_CIPHER_NONE
))
182 blobmsg_add_string(&buf
, NULL
, "none");
184 blobmsg_close_array(&buf
, d
);
187 blobmsg_close_table(&buf
, c
);
191 rpc_iwinfo_call_encryption(const char *name
)
193 struct iwinfo_crypto_entry crypto
= { 0 };
195 if (!iw
->encryption(ifname
, (char *)&crypto
))
196 rpc_iwinfo_add_encryption(name
, &crypto
);
200 rpc_iwinfo_call_hwmodes(const char *name
)
205 if (!iw
->hwmodelist(ifname
, &modes
))
207 c
= blobmsg_open_array(&buf
, name
);
209 if (modes
& IWINFO_80211_AC
)
210 blobmsg_add_string(&buf
, NULL
, "ac");
212 if (modes
& IWINFO_80211_A
)
213 blobmsg_add_string(&buf
, NULL
, "a");
215 if (modes
& IWINFO_80211_B
)
216 blobmsg_add_string(&buf
, NULL
, "b");
218 if (modes
& IWINFO_80211_G
)
219 blobmsg_add_string(&buf
, NULL
, "g");
221 if (modes
& IWINFO_80211_N
)
222 blobmsg_add_string(&buf
, NULL
, "n");
224 blobmsg_close_array(&buf
, c
);
229 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
231 char rv
[IWINFO_BUFSIZE
] = { 0 };
233 if (!func(ifname
, rv
))
234 blobmsg_add_string(&buf
, name
, rv
);
238 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
239 struct ubus_request_data
*req
, const char *method
,
240 struct blob_attr
*msg
)
245 rv
= rpc_iwinfo_open(msg
);
250 blob_buf_init(&buf
, 0);
252 rpc_iwinfo_call_str("phy", iw
->phyname
);
254 rpc_iwinfo_call_str("ssid", iw
->ssid
);
255 rpc_iwinfo_call_str("bssid", iw
->bssid
);
256 rpc_iwinfo_call_str("country", iw
->country
);
258 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
259 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
261 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
262 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
264 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
265 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
267 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
268 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
270 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
271 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
273 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
275 rpc_iwinfo_call_encryption("encryption");
276 rpc_iwinfo_call_hwmodes("hwmodes");
278 c
= blobmsg_open_table(&buf
, "hardware");
279 rpc_iwinfo_call_hardware_id("id");
280 rpc_iwinfo_call_str("name", iw
->hardware_name
);
281 blobmsg_close_table(&buf
, c
);
283 ubus_send_reply(ctx
, req
, buf
.head
);
287 return UBUS_STATUS_OK
;
291 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
292 struct ubus_request_data
*req
, const char *method
,
293 struct blob_attr
*msg
)
298 char res
[IWINFO_BUFSIZE
];
299 struct iwinfo_scanlist_entry
*e
;
301 rv
= rpc_iwinfo_open(msg
);
306 blob_buf_init(&buf
, 0);
308 c
= blobmsg_open_array(&buf
, "results");
310 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
312 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
314 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
315 d
= blobmsg_open_table(&buf
, NULL
);
318 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
320 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
321 e
->mac
[0], e
->mac
[1], e
->mac
[2],
322 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
324 blobmsg_add_string(&buf
, "bssid", mac
);
326 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
328 blobmsg_add_u32(&buf
, "channel", e
->channel
);
329 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
331 blobmsg_add_u32(&buf
, "quality", e
->quality
);
332 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
334 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
336 blobmsg_close_table(&buf
, d
);
340 blobmsg_close_array(&buf
, c
);
342 ubus_send_reply(ctx
, req
, buf
.head
);
346 return UBUS_STATUS_OK
;
350 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
351 struct ubus_request_data
*req
, const char *method
,
352 struct blob_attr
*msg
)
356 char res
[IWINFO_BUFSIZE
];
357 struct iwinfo_assoclist_entry
*a
;
360 rv
= rpc_iwinfo_open(msg
);
365 blob_buf_init(&buf
, 0);
367 c
= blobmsg_open_array(&buf
, "results");
369 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
371 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
373 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
374 d
= blobmsg_open_table(&buf
, NULL
);
376 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
377 a
->mac
[0], a
->mac
[1], a
->mac
[2],
378 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
380 blobmsg_add_string(&buf
, "mac", mac
);
381 blobmsg_add_u32(&buf
, "signal", a
->signal
);
382 blobmsg_add_u32(&buf
, "noise", a
->noise
);
383 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
385 e
= blobmsg_open_table(&buf
, "rx");
386 blobmsg_add_u32(&buf
, "rate", a
->rx_rate
.rate
);
387 blobmsg_add_u32(&buf
, "mcs", a
->rx_rate
.mcs
);
388 blobmsg_add_u8(&buf
, "40mhz", a
->rx_rate
.is_40mhz
);
389 blobmsg_add_u8(&buf
, "short_gi", a
->rx_rate
.is_short_gi
);
390 blobmsg_close_table(&buf
, e
);
392 e
= blobmsg_open_table(&buf
, "tx");
393 blobmsg_add_u32(&buf
, "rate", a
->tx_rate
.rate
);
394 blobmsg_add_u32(&buf
, "mcs", a
->tx_rate
.mcs
);
395 blobmsg_add_u8(&buf
, "40mhz", a
->tx_rate
.is_40mhz
);
396 blobmsg_add_u8(&buf
, "short_gi", a
->tx_rate
.is_short_gi
);
397 blobmsg_close_table(&buf
, e
);
399 blobmsg_close_table(&buf
, d
);
403 blobmsg_close_array(&buf
, c
);
405 ubus_send_reply(ctx
, req
, buf
.head
);
409 return UBUS_STATUS_OK
;
413 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
414 struct ubus_request_data
*req
, const char *method
,
415 struct blob_attr
*msg
)
418 char res
[IWINFO_BUFSIZE
];
419 struct iwinfo_freqlist_entry
*f
;
422 rv
= rpc_iwinfo_open(msg
);
427 blob_buf_init(&buf
, 0);
429 c
= blobmsg_open_array(&buf
, "results");
431 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
433 if (iw
->channel(ifname
, &ch
))
436 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
438 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
439 d
= blobmsg_open_table(&buf
, NULL
);
441 blobmsg_add_u32(&buf
, "channel", f
->channel
);
442 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
443 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
446 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
448 blobmsg_close_table(&buf
, d
);
452 blobmsg_close_array(&buf
, c
);
454 ubus_send_reply(ctx
, req
, buf
.head
);
458 return UBUS_STATUS_OK
;
462 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
463 struct ubus_request_data
*req
, const char *method
,
464 struct blob_attr
*msg
)
466 int i
, rv
, len
, pwr
, off
;
467 char res
[IWINFO_BUFSIZE
];
468 struct iwinfo_txpwrlist_entry
*t
;
471 rv
= rpc_iwinfo_open(msg
);
476 blob_buf_init(&buf
, 0);
478 c
= blobmsg_open_array(&buf
, "results");
480 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
482 if (iw
->txpower(ifname
, &pwr
))
485 if (iw
->txpower_offset(ifname
, &off
))
488 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
490 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
491 d
= blobmsg_open_table(&buf
, NULL
);
493 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
494 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
497 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
499 blobmsg_close_table(&buf
, d
);
503 blobmsg_close_array(&buf
, c
);
505 ubus_send_reply(ctx
, req
, buf
.head
);
509 return UBUS_STATUS_OK
;
513 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
516 static char ccode
[5];
517 struct iwinfo_country_entry
*c
;
519 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
521 c
= (struct iwinfo_country_entry
*)&buf
[i
];
523 if (c
->iso3166
== iso3166
)
525 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
534 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
535 struct ubus_request_data
*req
, const char *method
,
536 struct blob_attr
*msg
)
541 char res
[IWINFO_BUFSIZE
];
543 const struct iwinfo_iso3166_label
*l
;
546 rv
= rpc_iwinfo_open(msg
);
551 blob_buf_init(&buf
, 0);
553 c
= blobmsg_open_array(&buf
, "results");
555 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
557 if (iw
->country(ifname
, cur
))
558 memset(cur
, 0, sizeof(cur
));
560 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
562 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
567 d
= blobmsg_open_table(&buf
, NULL
);
569 blobmsg_add_string(&buf
, "code", ccode
);
570 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
572 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
573 (l
->iso3166
/ 256), (l
->iso3166
% 256));
575 blobmsg_add_string(&buf
, "iso3166", iso3166
);
578 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
580 blobmsg_close_table(&buf
, d
);
584 blobmsg_close_array(&buf
, c
);
586 ubus_send_reply(ctx
, req
, buf
.head
);
590 return UBUS_STATUS_OK
;
594 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
595 struct ubus_request_data
*req
, const char *method
,
596 struct blob_attr
*msg
)
602 d
= opendir("/sys/class/net");
605 return UBUS_STATUS_UNKNOWN_ERROR
;
607 blob_buf_init(&buf
, 0);
609 c
= blobmsg_open_array(&buf
, "devices");
611 while ((e
= readdir(d
)) != NULL
)
613 if (e
->d_type
!= DT_LNK
)
616 if (iwinfo_type(e
->d_name
))
617 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
620 blobmsg_close_array(&buf
, c
);
624 ubus_send_reply(ctx
, req
, buf
.head
);
628 return UBUS_STATUS_OK
;
633 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
635 static const struct ubus_method iwinfo_methods
[] = {
636 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices
),
637 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
638 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
639 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_device_policy
),
640 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
641 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
642 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
645 static struct ubus_object_type iwinfo_type
=
646 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
648 static struct ubus_object obj
= {
650 .type
= &iwinfo_type
,
651 .methods
= iwinfo_methods
,
652 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
655 return ubus_add_object(ctx
, &obj
);
658 struct rpc_plugin rpc_plugin
= {
659 .init
= rpc_iwinfo_api_init