rfkill.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
  21. //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
  22. //usage:#define rfkill_trivial_usage
  23. //usage: "COMMAND [INDEX|TYPE]"
  24. //usage:#define rfkill_full_usage "\n\n"
  25. //usage: "Enable/disable wireless devices\n"
  26. //usage: "\nCommands:"
  27. //usage: "\n list [INDEX|TYPE] List current state"
  28. //usage: "\n block INDEX|TYPE Disable device"
  29. //usage: "\n unblock INDEX|TYPE Enable device"
  30. //usage: "\n"
  31. //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
  32. //usage: "\n wimax, wwan, gps, fm"
  33. #include "libbb.h"
  34. #include <linux/rfkill.h>
  35. enum {
  36. OPT_b = (1 << 0), /* must be = 1 */
  37. OPT_u = (1 << 1),
  38. OPT_l = (1 << 2),
  39. };
  40. int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41. int rfkill_main(int argc UNUSED_PARAM, char **argv)
  42. {
  43. struct rfkill_event event;
  44. const char *rf_name;
  45. int rf_fd;
  46. int mode;
  47. int rf_type;
  48. int rf_idx;
  49. unsigned rf_opt = 0;
  50. argv++;
  51. /* Must have one or two params */
  52. if (!argv[0] || (argv[1] && argv[2]))
  53. bb_show_usage();
  54. mode = O_RDWR | O_NONBLOCK;
  55. rf_name = argv[1];
  56. if (strcmp(argv[0], "list") == 0) {
  57. rf_opt |= OPT_l;
  58. mode = O_RDONLY | O_NONBLOCK;
  59. } else if (strcmp(argv[0], "block") == 0 && rf_name) {
  60. rf_opt |= OPT_b;
  61. } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
  62. rf_opt |= OPT_u;
  63. } else
  64. bb_show_usage();
  65. rf_type = RFKILL_TYPE_ALL;
  66. rf_idx = -1;
  67. if (rf_name) {
  68. static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
  69. if (strcmp(rf_name, "wifi") == 0)
  70. rf_name = "wlan";
  71. if (strcmp(rf_name, "ultrawideband") == 0)
  72. rf_name = "uwb";
  73. rf_type = index_in_strings(rfkill_types, rf_name);
  74. if (rf_type < 0) {
  75. rf_idx = xatoi_positive(rf_name);
  76. }
  77. }
  78. rf_fd = device_open("/dev/rfkill", mode);
  79. if (rf_fd < 0)
  80. bb_simple_perror_msg_and_die("/dev/rfkill");
  81. if (rf_opt & OPT_l) {
  82. while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
  83. parser_t *parser;
  84. char *tokens[2];
  85. char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
  86. char *name, *type;
  87. if (rf_type && rf_type != event.type && rf_idx < 0) {
  88. continue;
  89. }
  90. if (rf_idx >= 0 && event.idx != rf_idx) {
  91. continue;
  92. }
  93. name = NULL;
  94. type = NULL;
  95. sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
  96. parser = config_open2(rf_sysfs, fopen_for_read);
  97. while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
  98. if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
  99. name = xstrdup(tokens[1]);
  100. continue;
  101. }
  102. if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
  103. type = xstrdup(tokens[1]);
  104. continue;
  105. }
  106. }
  107. config_close(parser);
  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. if (rf_idx >= 0) {
  121. event.idx = rf_idx;
  122. event.op = RFKILL_OP_CHANGE;
  123. }
  124. /* Note: OPT_b == 1 */
  125. event.soft = (rf_opt & OPT_b);
  126. xwrite(rf_fd, &event, sizeof(event));
  127. }
  128. return EXIT_SUCCESS;
  129. }