source: MondoRescue/branches/3.2/mindi-busybox/miscutils/rfkill.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
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//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"
18//usage: "\n"
19//usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
20//usage: "\n wimax, wwan, gps, fm"
21
22#include "libbb.h"
23#include <linux/rfkill.h>
24
25enum {
26 OPT_b = (1 << 0), /* must be = 1 */
27 OPT_u = (1 << 1),
28 OPT_l = (1 << 2),
29};
30
31int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32int rfkill_main(int argc UNUSED_PARAM, char **argv)
33{
34 struct rfkill_event event;
35 const char *rf_name;
36 int rf_fd;
37 int mode;
38 int rf_type;
39 int rf_idx;
40 unsigned rf_opt = 0;
41
42 argv++;
43 /* Must have one or two params */
44 if (!argv[0] || (argv[1] && argv[2]))
45 bb_show_usage();
46
47 mode = O_RDWR | O_NONBLOCK;
48 rf_name = argv[1];
49 if (strcmp(argv[0], "list") == 0) {
50 rf_opt |= OPT_l;
51 mode = O_RDONLY | O_NONBLOCK;
52 } else if (strcmp(argv[0], "block") == 0 && rf_name) {
53 rf_opt |= OPT_b;
54 } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
55 rf_opt |= OPT_u;
56 } else
57 bb_show_usage();
58
59 rf_type = RFKILL_TYPE_ALL;
60 rf_idx = -1;
61 if (rf_name) {
62 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
63 if (strcmp(rf_name, "wifi") == 0)
64 rf_name = "wlan";
65 if (strcmp(rf_name, "ultrawideband") == 0)
66 rf_name = "uwb";
67 rf_type = index_in_strings(rfkill_types, rf_name);
68 if (rf_type < 0) {
69 rf_idx = xatoi_positive(rf_name);
70 }
71 }
72
73 rf_fd = device_open("/dev/rfkill", mode);
74 if (rf_fd < 0)
75 bb_perror_msg_and_die("/dev/rfkill");
76
77 if (rf_opt & OPT_l) {
78 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
79 parser_t *parser;
80 char *tokens[2];
81 char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
82 char *name, *type;
83
84 if (rf_type && rf_type != event.type && rf_idx < 0) {
85 continue;
86 }
87
88 if (rf_idx >= 0 && event.idx != rf_idx) {
89 continue;
90 }
91
92 name = NULL;
93 type = NULL;
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]);
99 continue;
100 }
101 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
102 type = xstrdup(tokens[1]);
103 continue;
104 }
105 }
106 config_close(parser);
107
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");
111 free(name);
112 free(type);
113 }
114 } else {
115 memset(&event, 0, sizeof(event));
116 if (rf_type >= 0) {
117 event.type = rf_type;
118 event.op = RFKILL_OP_CHANGE_ALL;
119 }
120
121 if (rf_idx >= 0) {
122 event.idx = rf_idx;
123 event.op = RFKILL_OP_CHANGE;
124 }
125
126 /* Note: OPT_b == 1 */
127 event.soft = (rf_opt & OPT_b);
128
129 xwrite(rf_fd, &event, sizeof(event));
130 }
131
132 return EXIT_SUCCESS;
133}
Note: See TracBrowser for help on using the repository browser.