1 /* vi: set sw=4 ts=4: */
3 * rfkill implementation for busybox
5 * Copyright (C) 2010 Malek Degachi <malek-degachi@laposte.net>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config:config RFKILL
11 //config: bool "rfkill (5.3 kb)"
12 //config: default n # doesn't build on Ubuntu 9.04
13 //config: select PLATFORM_LINUX
15 //config: Enable/disable wireless devices.
17 //config: rfkill list : list all wireless devices
18 //config: rfkill list bluetooth : list all bluetooth devices
19 //config: rfkill list 1 : list device corresponding to the given index
20 //config: rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
23 //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
25 //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
27 //usage:#define rfkill_trivial_usage
28 //usage: "COMMAND [INDEX|TYPE]"
29 //usage:#define rfkill_full_usage "\n\n"
30 //usage: "Enable/disable wireless devices\n"
31 //usage: "\nCommands:"
32 //usage: "\n list [INDEX|TYPE] List current state"
33 //usage: "\n block INDEX|TYPE Disable device"
34 //usage: "\n unblock INDEX|TYPE Enable device"
36 //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
37 //usage: "\n wimax, wwan, gps, fm"
40 #include <linux/rfkill.h>
43 OPT_b = (1 << 0), /* must be = 1 */
48 int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int rfkill_main(int argc UNUSED_PARAM, char **argv)
51 struct rfkill_event event;
60 /* Must have one or two params */
61 if (!argv[0] || (argv[1] && argv[2]))
64 mode = O_RDWR | O_NONBLOCK;
66 if (strcmp(argv[0], "list") == 0) {
68 mode = O_RDONLY | O_NONBLOCK;
69 } else if (strcmp(argv[0], "block") == 0 && rf_name) {
71 } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
76 rf_type = RFKILL_TYPE_ALL;
79 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
80 if (strcmp(rf_name, "wifi") == 0)
82 if (strcmp(rf_name, "ultrawideband") == 0)
84 rf_type = index_in_strings(rfkill_types, rf_name);
86 rf_idx = xatoi_positive(rf_name);
90 rf_fd = device_open("/dev/rfkill", mode);
92 bb_perror_msg_and_die("/dev/rfkill");
95 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
98 char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
101 if (rf_type && rf_type != event.type && rf_idx < 0) {
105 if (rf_idx >= 0 && event.idx != rf_idx) {
111 sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
112 parser = config_open2(rf_sysfs, fopen_for_read);
113 while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
114 if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
115 name = xstrdup(tokens[1]);
118 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
119 type = xstrdup(tokens[1]);
123 config_close(parser);
125 printf("%u: %s: %s\n", event.idx, name, type);
126 printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
127 printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
132 memset(&event, 0, sizeof(event));
134 event.type = rf_type;
135 event.op = RFKILL_OP_CHANGE_ALL;
140 event.op = RFKILL_OP_CHANGE;
143 /* Note: OPT_b == 1 */
144 event.soft = (rf_opt & OPT_b);
146 xwrite(rf_fd, &event, sizeof(event));