busybox/miscutils/rfkill.c
<<
>>
Prefs
   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//config:config RFKILL
  10//config:       bool "rfkill (4.4 kb)"
  11//config:       default n # doesn't build on Ubuntu 9.04
  12//config:       help
  13//config:       Enable/disable wireless devices.
  14//config:
  15//config:       rfkill list : list all wireless devices
  16//config:       rfkill list bluetooth : list all bluetooth devices
  17//config:       rfkill list 1 : list device corresponding to the given index
  18//config:       rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
  19//config:
  20
  21//applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
  22
  23//kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
  24
  25//usage:#define rfkill_trivial_usage
  26//usage:       "COMMAND [INDEX|TYPE]"
  27//usage:#define rfkill_full_usage "\n\n"
  28//usage:       "Enable/disable wireless devices\n"
  29//usage:       "\nCommands:"
  30//usage:     "\n        list [INDEX|TYPE]       List current state"
  31//usage:     "\n        block INDEX|TYPE        Disable device"
  32//usage:     "\n        unblock INDEX|TYPE      Enable device"
  33//usage:     "\n"
  34//usage:     "\n        TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
  35//usage:     "\n                wimax, wwan, gps, fm"
  36
  37#include "libbb.h"
  38#include <linux/rfkill.h>
  39
  40enum {
  41        OPT_b = (1 << 0), /* must be = 1 */
  42        OPT_u = (1 << 1),
  43        OPT_l = (1 << 2),
  44};
  45
  46int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47int rfkill_main(int argc UNUSED_PARAM, char **argv)
  48{
  49        struct rfkill_event event;
  50        const char *rf_name;
  51        int rf_fd;
  52        int mode;
  53        int rf_type;
  54        int rf_idx;
  55        unsigned rf_opt = 0;
  56
  57        argv++;
  58        /* Must have one or two params */
  59        if (!argv[0] || (argv[1] && argv[2]))
  60                bb_show_usage();
  61
  62        mode = O_RDWR | O_NONBLOCK;
  63        rf_name = argv[1];
  64        if (strcmp(argv[0], "list") == 0) {
  65                rf_opt |= OPT_l;
  66                mode = O_RDONLY | O_NONBLOCK;
  67        } else if (strcmp(argv[0], "block") == 0 && rf_name) {
  68                rf_opt |= OPT_b;
  69        } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
  70                rf_opt |= OPT_u;
  71        } else
  72                bb_show_usage();
  73
  74        rf_type = RFKILL_TYPE_ALL;
  75        rf_idx = -1;
  76        if (rf_name) {
  77                static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
  78                if (strcmp(rf_name, "wifi") == 0)
  79                        rf_name = "wlan";
  80                if (strcmp(rf_name, "ultrawideband") == 0)
  81                        rf_name = "uwb";
  82                rf_type = index_in_strings(rfkill_types, rf_name);
  83                if (rf_type < 0) {
  84                        rf_idx = xatoi_positive(rf_name);
  85                }
  86        }
  87
  88        rf_fd = device_open("/dev/rfkill", mode);
  89        if (rf_fd < 0)
  90                bb_simple_perror_msg_and_die("/dev/rfkill");
  91
  92        if (rf_opt & OPT_l) {
  93                while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
  94                        parser_t *parser;
  95                        char *tokens[2];
  96                        char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
  97                        char *name, *type;
  98
  99                        if (rf_type && rf_type != event.type && rf_idx < 0) {
 100                                continue;
 101                        }
 102
 103                        if (rf_idx >= 0 && event.idx != rf_idx) {
 104                                continue;
 105                        }
 106
 107                        name = NULL;
 108                        type = NULL;
 109                        sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
 110                        parser = config_open2(rf_sysfs, fopen_for_read);
 111                        while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
 112                                if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
 113                                        name = xstrdup(tokens[1]);
 114                                        continue;
 115                                }
 116                                if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
 117                                        type = xstrdup(tokens[1]);
 118                                        continue;
 119                                }
 120                        }
 121                        config_close(parser);
 122
 123                        printf("%u: %s: %s\n", event.idx, name, type);
 124                        printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
 125                        printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
 126                        free(name);
 127                        free(type);
 128                }
 129        } else {
 130                memset(&event, 0, sizeof(event));
 131                if (rf_type >= 0) {
 132                        event.type = rf_type;
 133                        event.op = RFKILL_OP_CHANGE_ALL;
 134                }
 135
 136                if (rf_idx >= 0) {
 137                        event.idx = rf_idx;
 138                        event.op = RFKILL_OP_CHANGE;
 139                }
 140
 141                /* Note: OPT_b == 1 */
 142                event.soft = (rf_opt & OPT_b);
 143
 144                xwrite(rf_fd, &event, sizeof(event));
 145        }
 146
 147        return EXIT_SUCCESS;
 148}
 149