rfkill: new applet
authorSouf Oued <souf_oued@yahoo.fr>
Sun, 2 May 2010 16:45:02 +0000 (18:45 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 2 May 2010 16:45:02 +0000 (18:45 +0200)
function                                             old     new   delta
rfkill_main                                            -     683    +683
packed_usage                                       26824   26966    +142
static.rfkill_types                                    -      42     +42
applet_names                                        2216    2223      +7
applet_main                                         1300    1304      +4
applet_nameofs                                       650     652      +2

Signed-off-by: Souf Oued <souf_oued@yahoo.fr>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/applets.h
include/usage.h
miscutils/Config.in
miscutils/Kbuild
miscutils/rfkill.c [new file with mode: 0644]

index 83c1792acaa5435e6f0880cbd50bfe3200ce694f..ef5dd781ddf188d9c7090bc888571bc626fe0bac 100644 (file)
@@ -321,6 +321,7 @@ IF_RENICE(APPLET(renice, _BB_DIR_USR_BIN, _BB_SUID_DROP))
 IF_RESET(APPLET(reset, _BB_DIR_USR_BIN, _BB_SUID_DROP))
 IF_RESIZE(APPLET(resize, _BB_DIR_USR_BIN, _BB_SUID_DROP))
 IF_RESTORECON(APPLET_ODDNAME(restorecon, setfiles, _BB_DIR_SBIN, _BB_SUID_DROP, restorecon))
+IF_RFKILL(APPLET(rfkill, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
 IF_RM(APPLET_NOFORK(rm, rm, _BB_DIR_BIN, _BB_SUID_DROP, rm))
 IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, _BB_DIR_BIN, _BB_SUID_DROP, rmdir))
 IF_RMMOD(APPLET(rmmod, _BB_DIR_SBIN, _BB_SUID_DROP))
index 38594811edad706970674239028c9a0559f04f5a..2c2a90d16a07e2466660f3804badd3d6db2ac06a 100644 (file)
      "\n               for customizable files, or the user section," \
      "\n               if it has changed" \
 
+#define rfkill_trivial_usage \
+       "COMMAND [INDEX|TYPE]"
+#define rfkill_full_usage "\n\n" \
+       "Enable/disable wireless devices\n" \
+       "\nCommands:" \
+     "\n       list [INDEX|TYPE]       List current state" \
+     "\n       block INDEX|TYPE        Disable device" \
+     "\n       unblock INDEX|TYPE      Enable device" \
+     "\n" \
+     "\n       TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband)," \
+     "\n               wimax, wwan, gps, fm" \
+
 #define rm_trivial_usage \
        "[OPTIONS] FILE..."
 #define rm_full_usage "\n\n" \
index 0469b639916c3b386737c7505c5ec0726ae1ef3e..7a69dd10fd08dc0438f36e9ba7aae28948b30613 100644 (file)
@@ -545,6 +545,17 @@ config READAHEAD
          As readahead(2) blocks until each file has been read, it is best to
          run this applet as a background job.
 
+config RFKILL
+       bool "rfkill"
+       default n
+       help
+         Enable/disable wireless devices.
+
+         rfkill list : list all wireless devices
+         rfkill list bluetooth : list all bluetooth devices
+         rfkill list 1 : list device corresponding to the given index
+         rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
+
 config RUNLEVEL
        bool "runlevel"
        default n
index bbfa93dc700b48fc23b6de76236b993091264c5c..3c8ce42ba95d848fcd3f1b7f4ab7789fc80e456e 100644 (file)
@@ -34,6 +34,7 @@ lib-$(CONFIG_MOUNTPOINT)  += mountpoint.o
 lib-$(CONFIG_MT)          += mt.o
 lib-$(CONFIG_RAIDAUTORUN) += raidautorun.o
 lib-$(CONFIG_READAHEAD)   += readahead.o
+lib-$(CONFIG_RFKILL)      += rfkill.o
 lib-$(CONFIG_RUNLEVEL)    += runlevel.o
 lib-$(CONFIG_RX)          += rx.o
 lib-$(CONFIG_SETSID)      += setsid.o
diff --git a/miscutils/rfkill.c b/miscutils/rfkill.c
new file mode 100644 (file)
index 0000000..0f5817b
--- /dev/null
@@ -0,0 +1,120 @@
+/* vi: set sw=4 ts=4: */
+/*
+* rfkill implementation for busybox
+*
+* Copyright (C) 2010  Malek Degachi <malek-degachi@laposte.net>
+*
+* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+*/
+#include "libbb.h"
+#include <linux/rfkill.h>
+
+enum {
+       OPT_b = (1 << 0), /* must be = 1 */
+       OPT_u = (1 << 1),
+       OPT_l = (1 << 2),
+};
+
+int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int rfkill_main(int argc UNUSED_PARAM, char **argv)
+{
+       struct rfkill_event event;
+       const char *rf_name;
+       int rf_fd;
+       int mode;
+       int rf_type;
+       int rf_idx;
+       unsigned rf_opt = 0;
+
+       argv++;
+       /* Must have one or two params */
+       if (!argv[0] || (argv[1] && argv[2]))
+               bb_show_usage();
+
+       mode = O_RDWR | O_NONBLOCK;
+       rf_name = argv[1];
+       if (strcmp(argv[0], "list") == 0) {
+               rf_opt |= OPT_l;
+               mode = O_RDONLY | O_NONBLOCK;
+       } else if (strcmp(argv[0], "block") == 0 && rf_name) {
+               rf_opt |= OPT_b;
+       } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
+               rf_opt |= OPT_u;
+       } else
+               bb_show_usage();
+
+       rf_type = RFKILL_TYPE_ALL;
+       rf_idx = -1;
+       if (rf_name) {
+               static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
+               if (strcmp(rf_name, "wifi") == 0)
+                       rf_name = "wlan";
+               if (strcmp(rf_name, "ultrawideband") == 0)
+                       rf_name = "uwb";
+               rf_type = index_in_strings(rfkill_types, rf_name);
+               if (rf_type < 0) {
+                       rf_idx = xatoi_u(rf_name);
+               }
+       }
+
+       rf_fd = device_open("/dev/rfkill", mode);
+       if (rf_fd < 0)
+               bb_perror_msg_and_die("/dev/rfkill");
+
+       if (rf_opt & OPT_l) {
+               while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
+                       parser_t *parser;
+                       char *tokens[2];
+                       char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
+                       char *name, *type;
+
+                       if (rf_type && rf_type != event.type && rf_idx < 0) {
+                               continue;
+                       }
+
+                       if (rf_idx >= 0 && event.idx != rf_idx) {
+                               continue;
+                       }
+
+                       name = NULL;
+                       type = NULL;
+                       sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
+                       parser = config_open2(rf_sysfs, fopen_for_read);
+                       while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
+                               if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
+                                       name = xstrdup(tokens[1]);
+                                       continue;
+                               }
+                               if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
+                                       type = xstrdup(tokens[1]);
+                                       continue;
+                               }
+                       }
+                       config_close(parser);
+
+                       printf("%u: %s: %s\n", event.idx, name, type);
+                       printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
+                       printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
+                       free(name);
+                       free(type);
+               }
+       } else {
+               memset(&event, 0, sizeof(event));
+               if (rf_type >= 0) {
+                       event.type = rf_type;
+                       event.op = RFKILL_OP_CHANGE_ALL;
+               }
+
+               if (rf_idx >= 0) {
+                       event.idx = rf_idx;
+                       event.op = RFKILL_OP_CHANGE;
+               }
+
+               /* Note: OPT_b == 1 */
+               event.soft = (rf_opt & OPT_b);
+
+               xwrite(rf_fd, &event, sizeof(event));
+       }
+
+       return EXIT_SUCCESS;
+}