Bump to version 1.22.1
[platform/upstream/busybox.git] / miscutils / rfkill.c
1 /* vi: set sw=4 ts=4: */
2 /*
3 * rfkill implementation for busybox
4 *
5 * Copyright (C) 2010  Malek Degachi <malek-degachi@laposte.net>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10 //config:config RFKILL
11 //config:       bool "rfkill"
12 //config:       default n # doesn't build on Ubuntu 9.04
13 //config:       select PLATFORM_LINUX
14 //config:       help
15 //config:         Enable/disable wireless devices.
16 //config:
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
21 //config:
22
23 //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
24
25 //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
26
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"
35 //usage:     "\n"
36 //usage:     "\n        TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
37 //usage:     "\n                wimax, wwan, gps, fm"
38
39 #include "libbb.h"
40 #include <linux/rfkill.h>
41
42 enum {
43         OPT_b = (1 << 0), /* must be = 1 */
44         OPT_u = (1 << 1),
45         OPT_l = (1 << 2),
46 };
47
48 int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int rfkill_main(int argc UNUSED_PARAM, char **argv)
50 {
51         struct rfkill_event event;
52         const char *rf_name;
53         int rf_fd;
54         int mode;
55         int rf_type;
56         int rf_idx;
57         unsigned rf_opt = 0;
58
59         argv++;
60         /* Must have one or two params */
61         if (!argv[0] || (argv[1] && argv[2]))
62                 bb_show_usage();
63
64         mode = O_RDWR | O_NONBLOCK;
65         rf_name = argv[1];
66         if (strcmp(argv[0], "list") == 0) {
67                 rf_opt |= OPT_l;
68                 mode = O_RDONLY | O_NONBLOCK;
69         } else if (strcmp(argv[0], "block") == 0 && rf_name) {
70                 rf_opt |= OPT_b;
71         } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
72                 rf_opt |= OPT_u;
73         } else
74                 bb_show_usage();
75
76         rf_type = RFKILL_TYPE_ALL;
77         rf_idx = -1;
78         if (rf_name) {
79                 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
80                 if (strcmp(rf_name, "wifi") == 0)
81                         rf_name = "wlan";
82                 if (strcmp(rf_name, "ultrawideband") == 0)
83                         rf_name = "uwb";
84                 rf_type = index_in_strings(rfkill_types, rf_name);
85                 if (rf_type < 0) {
86                         rf_idx = xatoi_positive(rf_name);
87                 }
88         }
89
90         rf_fd = device_open("/dev/rfkill", mode);
91         if (rf_fd < 0)
92                 bb_perror_msg_and_die("/dev/rfkill");
93
94         if (rf_opt & OPT_l) {
95                 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
96                         parser_t *parser;
97                         char *tokens[2];
98                         char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
99                         char *name, *type;
100
101                         if (rf_type && rf_type != event.type && rf_idx < 0) {
102                                 continue;
103                         }
104
105                         if (rf_idx >= 0 && event.idx != rf_idx) {
106                                 continue;
107                         }
108
109                         name = NULL;
110                         type = NULL;
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]);
116                                         continue;
117                                 }
118                                 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
119                                         type = xstrdup(tokens[1]);
120                                         continue;
121                                 }
122                         }
123                         config_close(parser);
124
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");
128                         free(name);
129                         free(type);
130                 }
131         } else {
132                 memset(&event, 0, sizeof(event));
133                 if (rf_type >= 0) {
134                         event.type = rf_type;
135                         event.op = RFKILL_OP_CHANGE_ALL;
136                 }
137
138                 if (rf_idx >= 0) {
139                         event.idx = rf_idx;
140                         event.op = RFKILL_OP_CHANGE;
141                 }
142
143                 /* Note: OPT_b == 1 */
144                 event.soft = (rf_opt & OPT_b);
145
146                 xwrite(rf_fd, &event, sizeof(event));
147         }
148
149         return EXIT_SUCCESS;
150 }