2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013 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_A
)
210 blobmsg_add_string(&buf
, NULL
, "a");
212 if (modes
& IWINFO_80211_B
)
213 blobmsg_add_string(&buf
, NULL
, "b");
215 if (modes
& IWINFO_80211_G
)
216 blobmsg_add_string(&buf
, NULL
, "g");
218 if (modes
& IWINFO_80211_N
)
219 blobmsg_add_string(&buf
, NULL
, "n");
221 blobmsg_close_array(&buf
, c
);
226 rpc_iwinfo_call_str(const char *name
, int (*func
)(const char *, char *))
228 char rv
[IWINFO_BUFSIZE
] = { 0 };
230 if (!func(ifname
, rv
))
231 blobmsg_add_string(&buf
, name
, rv
);
235 rpc_iwinfo_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
236 struct ubus_request_data
*req
, const char *method
,
237 struct blob_attr
*msg
)
242 rv
= rpc_iwinfo_open(msg
);
247 blob_buf_init(&buf
, 0);
249 rpc_iwinfo_call_str("phy", iw
->phyname
);
251 rpc_iwinfo_call_str("ssid", iw
->ssid
);
252 rpc_iwinfo_call_str("bssid", iw
->bssid
);
253 rpc_iwinfo_call_str("country", iw
->country
);
255 rpc_iwinfo_call_int("mode", iw
->mode
, IWINFO_OPMODE_NAMES
);
256 rpc_iwinfo_call_int("channel", iw
->channel
, NULL
);
258 rpc_iwinfo_call_int("frequency", iw
->frequency
, NULL
);
259 rpc_iwinfo_call_int("frequency_offset", iw
->frequency_offset
, NULL
);
261 rpc_iwinfo_call_int("txpower", iw
->txpower
, NULL
);
262 rpc_iwinfo_call_int("txpower_offset", iw
->txpower_offset
, NULL
);
264 rpc_iwinfo_call_int("quality", iw
->quality
, NULL
);
265 rpc_iwinfo_call_int("quality_max", iw
->quality_max
, NULL
);
267 rpc_iwinfo_call_int("signal", iw
->signal
, NULL
);
268 rpc_iwinfo_call_int("noise", iw
->noise
, NULL
);
270 rpc_iwinfo_call_int("bitrate", iw
->bitrate
, NULL
);
272 rpc_iwinfo_call_encryption("encryption");
273 rpc_iwinfo_call_hwmodes("hwmodes");
275 c
= blobmsg_open_table(&buf
, "hardware");
276 rpc_iwinfo_call_hardware_id("id");
277 rpc_iwinfo_call_str("name", iw
->hardware_name
);
278 blobmsg_close_table(&buf
, c
);
280 ubus_send_reply(ctx
, req
, buf
.head
);
284 return UBUS_STATUS_OK
;
288 rpc_iwinfo_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
289 struct ubus_request_data
*req
, const char *method
,
290 struct blob_attr
*msg
)
295 char res
[IWINFO_BUFSIZE
];
296 struct iwinfo_scanlist_entry
*e
;
298 rv
= rpc_iwinfo_open(msg
);
303 blob_buf_init(&buf
, 0);
305 c
= blobmsg_open_array(&buf
, "results");
307 if (!iw
->scanlist(ifname
, res
, &len
) && (len
> 0))
309 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
))
311 e
= (struct iwinfo_scanlist_entry
*)&res
[i
];
312 d
= blobmsg_open_table(&buf
, NULL
);
315 blobmsg_add_string(&buf
, "ssid", (const char *)e
->ssid
);
317 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
318 e
->mac
[0], e
->mac
[1], e
->mac
[2],
319 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
321 blobmsg_add_string(&buf
, "bssid", mac
);
323 blobmsg_add_string(&buf
, "mode", IWINFO_OPMODE_NAMES
[e
->mode
]);
325 blobmsg_add_u32(&buf
, "channel", e
->channel
);
326 blobmsg_add_u32(&buf
, "signal", (uint32_t)(e
->signal
- 0x100));
328 blobmsg_add_u32(&buf
, "quality", e
->quality
);
329 blobmsg_add_u32(&buf
, "quality_max", e
->quality_max
);
331 rpc_iwinfo_add_encryption("encryption", &e
->crypto
);
333 blobmsg_close_table(&buf
, d
);
337 blobmsg_close_array(&buf
, c
);
339 ubus_send_reply(ctx
, req
, buf
.head
);
343 return UBUS_STATUS_OK
;
347 rpc_iwinfo_assoclist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
348 struct ubus_request_data
*req
, const char *method
,
349 struct blob_attr
*msg
)
353 char res
[IWINFO_BUFSIZE
];
354 struct iwinfo_assoclist_entry
*a
;
357 rv
= rpc_iwinfo_open(msg
);
362 blob_buf_init(&buf
, 0);
364 c
= blobmsg_open_array(&buf
, "results");
366 if (!iw
->assoclist(ifname
, res
, &len
) && (len
> 0))
368 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
370 a
= (struct iwinfo_assoclist_entry
*)&res
[i
];
371 d
= blobmsg_open_table(&buf
, NULL
);
373 snprintf(mac
, sizeof(mac
), "%02X:%02X:%02X:%02X:%02X:%02X",
374 a
->mac
[0], a
->mac
[1], a
->mac
[2],
375 a
->mac
[3], a
->mac
[4], a
->mac
[5]);
377 blobmsg_add_string(&buf
, "mac", mac
);
378 blobmsg_add_u32(&buf
, "signal", a
->signal
);
379 blobmsg_add_u32(&buf
, "noise", a
->noise
);
380 blobmsg_add_u32(&buf
, "inactive", a
->inactive
);
382 e
= blobmsg_open_table(&buf
, "rx");
383 blobmsg_add_u32(&buf
, "rate", a
->rx_rate
.rate
);
384 blobmsg_add_u32(&buf
, "mcs", a
->rx_rate
.mcs
);
385 blobmsg_add_u8(&buf
, "40mhz", a
->rx_rate
.is_40mhz
);
386 blobmsg_add_u8(&buf
, "short_gi", a
->rx_rate
.is_short_gi
);
387 blobmsg_close_table(&buf
, e
);
389 e
= blobmsg_open_table(&buf
, "tx");
390 blobmsg_add_u32(&buf
, "rate", a
->tx_rate
.rate
);
391 blobmsg_add_u32(&buf
, "mcs", a
->tx_rate
.mcs
);
392 blobmsg_add_u8(&buf
, "40mhz", a
->tx_rate
.is_40mhz
);
393 blobmsg_add_u8(&buf
, "short_gi", a
->tx_rate
.is_short_gi
);
394 blobmsg_close_table(&buf
, e
);
396 blobmsg_close_table(&buf
, d
);
400 blobmsg_close_array(&buf
, c
);
402 ubus_send_reply(ctx
, req
, buf
.head
);
406 return UBUS_STATUS_OK
;
410 rpc_iwinfo_freqlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
411 struct ubus_request_data
*req
, const char *method
,
412 struct blob_attr
*msg
)
415 char res
[IWINFO_BUFSIZE
];
416 struct iwinfo_freqlist_entry
*f
;
419 rv
= rpc_iwinfo_open(msg
);
424 blob_buf_init(&buf
, 0);
426 c
= blobmsg_open_array(&buf
, "results");
428 if (!iw
->freqlist(ifname
, res
, &len
) && (len
> 0))
430 if (iw
->channel(ifname
, &ch
))
433 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
435 f
= (struct iwinfo_freqlist_entry
*)&res
[i
];
436 d
= blobmsg_open_table(&buf
, NULL
);
438 blobmsg_add_u32(&buf
, "channel", f
->channel
);
439 blobmsg_add_u32(&buf
, "mhz", f
->mhz
);
440 blobmsg_add_u8(&buf
, "restricted", f
->restricted
);
443 blobmsg_add_u8(&buf
, "active", f
->channel
== ch
);
445 blobmsg_close_table(&buf
, d
);
449 blobmsg_close_array(&buf
, c
);
451 ubus_send_reply(ctx
, req
, buf
.head
);
455 return UBUS_STATUS_OK
;
459 rpc_iwinfo_txpowerlist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
460 struct ubus_request_data
*req
, const char *method
,
461 struct blob_attr
*msg
)
463 int i
, rv
, len
, pwr
, off
;
464 char res
[IWINFO_BUFSIZE
];
465 struct iwinfo_txpwrlist_entry
*t
;
468 rv
= rpc_iwinfo_open(msg
);
473 blob_buf_init(&buf
, 0);
475 c
= blobmsg_open_array(&buf
, "results");
477 if (!iw
->txpwrlist(ifname
, res
, &len
) && (len
> 0))
479 if (iw
->txpower(ifname
, &pwr
))
482 if (iw
->txpower_offset(ifname
, &off
))
485 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
487 t
= (struct iwinfo_txpwrlist_entry
*)&res
[i
];
488 d
= blobmsg_open_table(&buf
, NULL
);
490 blobmsg_add_u32(&buf
, "dbm", t
->dbm
+ off
);
491 blobmsg_add_u32(&buf
, "mw", iwinfo_dbm2mw(t
->dbm
+ off
));
494 blobmsg_add_u8(&buf
, "active", t
->dbm
== pwr
);
496 blobmsg_close_table(&buf
, d
);
500 blobmsg_close_array(&buf
, c
);
502 ubus_send_reply(ctx
, req
, buf
.head
);
506 return UBUS_STATUS_OK
;
510 rpc_iwinfo_lookup_country(char *buf
, int len
, int iso3166
)
513 static char ccode
[5];
514 struct iwinfo_country_entry
*c
;
516 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
518 c
= (struct iwinfo_country_entry
*)&buf
[i
];
520 if (c
->iso3166
== iso3166
)
522 snprintf(ccode
, sizeof(ccode
), "%s", c
->ccode
);
531 rpc_iwinfo_countrylist(struct ubus_context
*ctx
, struct ubus_object
*obj
,
532 struct ubus_request_data
*req
, const char *method
,
533 struct blob_attr
*msg
)
538 char res
[IWINFO_BUFSIZE
];
540 const struct iwinfo_iso3166_label
*l
;
543 rv
= rpc_iwinfo_open(msg
);
548 blob_buf_init(&buf
, 0);
550 c
= blobmsg_open_array(&buf
, "results");
552 if (!iw
->countrylist(ifname
, res
, &len
) && (len
> 0))
554 if (iw
->country(ifname
, cur
))
555 memset(cur
, 0, sizeof(cur
));
557 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
559 ccode
= rpc_iwinfo_lookup_country(res
, len
, l
->iso3166
);
564 d
= blobmsg_open_table(&buf
, NULL
);
566 blobmsg_add_string(&buf
, "code", ccode
);
567 blobmsg_add_string(&buf
, "country", (const char *)l
->name
);
569 snprintf(iso3166
, sizeof(iso3166
), "%c%c",
570 (l
->iso3166
/ 256), (l
->iso3166
% 256));
572 blobmsg_add_string(&buf
, "iso3166", iso3166
);
575 blobmsg_add_u8(&buf
, "active", !strncmp(ccode
, cur
, 2));
577 blobmsg_close_table(&buf
, d
);
581 blobmsg_close_array(&buf
, c
);
583 ubus_send_reply(ctx
, req
, buf
.head
);
587 return UBUS_STATUS_OK
;
591 rpc_iwinfo_devices(struct ubus_context
*ctx
, struct ubus_object
*obj
,
592 struct ubus_request_data
*req
, const char *method
,
593 struct blob_attr
*msg
)
599 d
= opendir("/sys/class/net");
602 return UBUS_STATUS_UNKNOWN_ERROR
;
604 blob_buf_init(&buf
, 0);
606 c
= blobmsg_open_array(&buf
, "devices");
608 while ((e
= readdir(d
)) != NULL
)
610 if (e
->d_type
!= DT_LNK
)
613 if (iwinfo_type(e
->d_name
))
614 blobmsg_add_string(&buf
, NULL
, e
->d_name
);
617 blobmsg_close_array(&buf
, c
);
621 ubus_send_reply(ctx
, req
, buf
.head
);
625 return UBUS_STATUS_OK
;
630 rpc_iwinfo_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
632 static const struct ubus_method iwinfo_methods
[] = {
633 { .name
= "devices", .handler
= rpc_iwinfo_devices
},
634 UBUS_METHOD("info", rpc_iwinfo_info
, rpc_device_policy
),
635 UBUS_METHOD("scan", rpc_iwinfo_scan
, rpc_device_policy
),
636 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist
, rpc_device_policy
),
637 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist
, rpc_device_policy
),
638 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist
, rpc_device_policy
),
639 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist
, rpc_device_policy
),
642 static struct ubus_object_type iwinfo_type
=
643 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods
);
645 static struct ubus_object obj
= {
647 .type
= &iwinfo_type
,
648 .methods
= iwinfo_methods
,
649 .n_methods
= ARRAY_SIZE(iwinfo_methods
),
652 return ubus_add_object(ctx
, &obj
);
655 const struct rpc_plugin rpc_plugin
= {
656 .init
= rpc_iwinfo_api_init