Souf Oued | 43afd75 | 2010-05-02 18:45:02 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 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 | */ |
Denys Vlasenko | 2635369 | 2013-11-20 14:20:07 +0100 | [diff] [blame] | 9 | //config:config RFKILL |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 10 | //config: bool "rfkill (5.3 kb)" |
Denys Vlasenko | 2635369 | 2013-11-20 14:20:07 +0100 | [diff] [blame] | 11 | //config: default n # doesn't build on Ubuntu 9.04 |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Enable/disable wireless devices. |
Denys Vlasenko | 2635369 | 2013-11-20 14:20:07 +0100 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 2635369 | 2013-11-20 14:20:07 +0100 | [diff] [blame] | 20 | //config: |
| 21 | |
| 22 | //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 23 | |
| 24 | //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o |
| 25 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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" |
| 34 | //usage: "\n" |
| 35 | //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband)," |
| 36 | //usage: "\n wimax, wwan, gps, fm" |
| 37 | |
Souf Oued | 43afd75 | 2010-05-02 18:45:02 +0200 | [diff] [blame] | 38 | #include "libbb.h" |
| 39 | #include <linux/rfkill.h> |
| 40 | |
| 41 | enum { |
| 42 | OPT_b = (1 << 0), /* must be = 1 */ |
| 43 | OPT_u = (1 << 1), |
| 44 | OPT_l = (1 << 2), |
| 45 | }; |
| 46 | |
| 47 | int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 48 | int rfkill_main(int argc UNUSED_PARAM, char **argv) |
| 49 | { |
| 50 | struct rfkill_event event; |
| 51 | const char *rf_name; |
| 52 | int rf_fd; |
| 53 | int mode; |
| 54 | int rf_type; |
| 55 | int rf_idx; |
| 56 | unsigned rf_opt = 0; |
| 57 | |
| 58 | argv++; |
| 59 | /* Must have one or two params */ |
| 60 | if (!argv[0] || (argv[1] && argv[2])) |
| 61 | bb_show_usage(); |
| 62 | |
| 63 | mode = O_RDWR | O_NONBLOCK; |
| 64 | rf_name = argv[1]; |
| 65 | if (strcmp(argv[0], "list") == 0) { |
| 66 | rf_opt |= OPT_l; |
| 67 | mode = O_RDONLY | O_NONBLOCK; |
| 68 | } else if (strcmp(argv[0], "block") == 0 && rf_name) { |
| 69 | rf_opt |= OPT_b; |
| 70 | } else if (strcmp(argv[0], "unblock") == 0 && rf_name) { |
| 71 | rf_opt |= OPT_u; |
| 72 | } else |
| 73 | bb_show_usage(); |
| 74 | |
| 75 | rf_type = RFKILL_TYPE_ALL; |
| 76 | rf_idx = -1; |
| 77 | if (rf_name) { |
| 78 | static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0"; |
| 79 | if (strcmp(rf_name, "wifi") == 0) |
| 80 | rf_name = "wlan"; |
| 81 | if (strcmp(rf_name, "ultrawideband") == 0) |
| 82 | rf_name = "uwb"; |
| 83 | rf_type = index_in_strings(rfkill_types, rf_name); |
| 84 | if (rf_type < 0) { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 85 | rf_idx = xatoi_positive(rf_name); |
Souf Oued | 43afd75 | 2010-05-02 18:45:02 +0200 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | rf_fd = device_open("/dev/rfkill", mode); |
| 90 | if (rf_fd < 0) |
| 91 | bb_perror_msg_and_die("/dev/rfkill"); |
| 92 | |
| 93 | if (rf_opt & OPT_l) { |
| 94 | while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) { |
| 95 | parser_t *parser; |
| 96 | char *tokens[2]; |
| 97 | char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3]; |
| 98 | char *name, *type; |
| 99 | |
| 100 | if (rf_type && rf_type != event.type && rf_idx < 0) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | if (rf_idx >= 0 && event.idx != rf_idx) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | name = NULL; |
| 109 | type = NULL; |
| 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]); |
| 115 | continue; |
| 116 | } |
| 117 | if (strcmp(tokens[0], "RFKILL_TYPE") == 0) { |
| 118 | type = xstrdup(tokens[1]); |
| 119 | continue; |
| 120 | } |
| 121 | } |
| 122 | config_close(parser); |
| 123 | |
| 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"); |
| 127 | free(name); |
| 128 | free(type); |
| 129 | } |
| 130 | } else { |
| 131 | memset(&event, 0, sizeof(event)); |
| 132 | if (rf_type >= 0) { |
| 133 | event.type = rf_type; |
| 134 | event.op = RFKILL_OP_CHANGE_ALL; |
| 135 | } |
| 136 | |
| 137 | if (rf_idx >= 0) { |
| 138 | event.idx = rf_idx; |
| 139 | event.op = RFKILL_OP_CHANGE; |
| 140 | } |
| 141 | |
| 142 | /* Note: OPT_b == 1 */ |
| 143 | event.soft = (rf_opt & OPT_b); |
| 144 | |
| 145 | xwrite(rf_fd, &event, sizeof(event)); |
| 146 | } |
| 147 | |
| 148 | return EXIT_SUCCESS; |
| 149 | } |