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: bool "rfkill (5.3 kb)"
11 //config: default n # doesn't build on Ubuntu 9.04
12 //config: select PLATFORM_LINUX
14 //config: Enable/disable wireless devices.
16 //config: rfkill list : list all wireless devices
17 //config: rfkill list bluetooth : list all bluetooth devices
18 //config: rfkill list 1 : list device corresponding to the given index
19 //config: rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
22 //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
24 //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
26 //usage:#define rfkill_trivial_usage
27 //usage: "COMMAND [INDEX|TYPE]"
28 //usage:#define rfkill_full_usage "\n\n"
29 //usage: "Enable/disable wireless devices\n"
30 //usage: "\nCommands:"
31 //usage: "\n list [INDEX|TYPE] List current state"
32 //usage: "\n block INDEX|TYPE Disable device"
33 //usage: "\n unblock INDEX|TYPE Enable device"
35 //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
36 //usage: "\n wimax, wwan, gps, fm"
39 #include <linux/rfkill.h>
42 OPT_b = (1 << 0), /* must be = 1 */
47 int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48 int rfkill_main(int argc UNUSED_PARAM, char **argv)
50 struct rfkill_event event;
59 /* Must have one or two params */
60 if (!argv[0] || (argv[1] && argv[2]))
63 mode = O_RDWR | O_NONBLOCK;
65 if (strcmp(argv[0], "list") == 0) {
67 mode = O_RDONLY | O_NONBLOCK;
68 } else if (strcmp(argv[0], "block") == 0 && rf_name) {
70 } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
75 rf_type = RFKILL_TYPE_ALL;
78 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
79 if (strcmp(rf_name, "wifi") == 0)
81 if (strcmp(rf_name, "ultrawideband") == 0)
83 rf_type = index_in_strings(rfkill_types, rf_name);
85 rf_idx = xatoi_positive(rf_name);
89 rf_fd = device_open("/dev/rfkill", mode);
91 bb_perror_msg_and_die("/dev/rfkill");
94 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
97 char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
100 if (rf_type && rf_type != event.type && rf_idx < 0) {
104 if (rf_idx >= 0 && event.idx != rf_idx) {
110 sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
111 parser = config_open2(rf_sysfs, fopen_for_read);
112 while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
113 if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
114 name = xstrdup(tokens[1]);
117 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
118 type = xstrdup(tokens[1]);
122 config_close(parser);
124 printf("%u: %s: %s\n", event.idx, name, type);
125 printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
126 printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
131 memset(&event, 0, sizeof(event));
133 event.type = rf_type;
134 event.op = RFKILL_OP_CHANGE_ALL;
139 event.op = RFKILL_OP_CHANGE;
142 /* Note: OPT_b == 1 */
143 event.soft = (rf_opt & OPT_b);
145 xwrite(rf_fd, &event, sizeof(event));