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 //usage:#define rfkill_trivial_usage
11 //usage: "COMMAND [INDEX|TYPE]"
12 //usage:#define rfkill_full_usage "\n\n"
13 //usage: "Enable/disable wireless devices\n"
14 //usage: "\nCommands:"
15 //usage: "\n list [INDEX|TYPE] List current state"
16 //usage: "\n block INDEX|TYPE Disable device"
17 //usage: "\n unblock INDEX|TYPE Enable device"
19 //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
20 //usage: "\n wimax, wwan, gps, fm"
23 #include <linux/rfkill.h>
26 OPT_b = (1 << 0), /* must be = 1 */
31 int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32 int rfkill_main(int argc UNUSED_PARAM, char **argv)
34 struct rfkill_event event;
43 /* Must have one or two params */
44 if (!argv[0] || (argv[1] && argv[2]))
47 mode = O_RDWR | O_NONBLOCK;
49 if (strcmp(argv[0], "list") == 0) {
51 mode = O_RDONLY | O_NONBLOCK;
52 } else if (strcmp(argv[0], "block") == 0 && rf_name) {
54 } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
59 rf_type = RFKILL_TYPE_ALL;
62 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
63 if (strcmp(rf_name, "wifi") == 0)
65 if (strcmp(rf_name, "ultrawideband") == 0)
67 rf_type = index_in_strings(rfkill_types, rf_name);
69 rf_idx = xatoi_positive(rf_name);
73 rf_fd = device_open("/dev/rfkill", mode);
75 bb_perror_msg_and_die("/dev/rfkill");
78 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
81 char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
84 if (rf_type && rf_type != event.type && rf_idx < 0) {
88 if (rf_idx >= 0 && event.idx != rf_idx) {
94 sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
95 parser = config_open2(rf_sysfs, fopen_for_read);
96 while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
97 if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
98 name = xstrdup(tokens[1]);
101 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
102 type = xstrdup(tokens[1]);
106 config_close(parser);
108 printf("%u: %s: %s\n", event.idx, name, type);
109 printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
110 printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
115 memset(&event, 0, sizeof(event));
117 event.type = rf_type;
118 event.op = RFKILL_OP_CHANGE_ALL;
123 event.op = RFKILL_OP_CHANGE;
126 /* Note: OPT_b == 1 */
127 event.soft = (rf_opt & OPT_b);
129 xwrite(rf_fd, &event, sizeof(event));