net_backend_rfkill.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @file net_backend_rfkill.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Rfkill monitoring module.
  32. *
  33. * Synopsis: net.backend.rfkill(string type, string name)
  34. * Arguments:
  35. * type - method of determining the index of the rfkill device. "index" for
  36. * rfkill device index, "wlan" for wireless device. Be aware that, for
  37. * the wireless device method, the index is resloved at initialization,
  38. * and no attempt is made to refresh it if the device goes away. In other
  39. * words, you should probably put a "net.backend.waitdevice" statement
  40. * in front of the rfkill statement.
  41. * name - rfkill index or wireless device name
  42. */
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <inttypes.h>
  46. #include <stdio.h>
  47. #include <sys/types.h>
  48. #include <dirent.h>
  49. #include <misc/string_begins_with.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/NCDRfkillMonitor.h>
  52. #include <generated/blog_channel_ncd_net_backend_rfkill.h>
  53. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  54. struct instance {
  55. NCDModuleInst *i;
  56. uint32_t index;
  57. NCDRfkillMonitor monitor;
  58. int up;
  59. };
  60. static int find_wlan_rfill (const char *ifname, uint32_t *out_index)
  61. {
  62. char ieee_path[100];
  63. snprintf(ieee_path, sizeof(ieee_path), "/sys/class/net/%s/../../ieee80211", ifname);
  64. int res = 0;
  65. DIR *d = opendir(ieee_path);
  66. if (!d) {
  67. goto fail0;
  68. }
  69. struct dirent *e;
  70. while (e = readdir(d)) {
  71. if (!string_begins_with(e->d_name, "phy")) {
  72. continue;
  73. }
  74. char phy_path[150];
  75. snprintf(phy_path, sizeof(phy_path), "%s/%s", ieee_path, e->d_name);
  76. DIR *d2 = opendir(phy_path);
  77. if (!d2) {
  78. continue;
  79. }
  80. struct dirent *e2;
  81. while (e2 = readdir(d2)) {
  82. int index_pos;
  83. if (!(index_pos = string_begins_with(e2->d_name, "rfkill"))) {
  84. continue;
  85. }
  86. uint32_t index;
  87. if (sscanf(e2->d_name + index_pos, "%"SCNu32, &index) != 1) {
  88. continue;
  89. }
  90. res = 1;
  91. *out_index = index;
  92. }
  93. closedir(d2);
  94. }
  95. closedir(d);
  96. fail0:
  97. return res;
  98. }
  99. static void monitor_handler (struct instance *o, struct rfkill_event event)
  100. {
  101. if (event.idx != o->index) {
  102. return;
  103. }
  104. int was_up = o->up;
  105. o->up = (event.op != RFKILL_OP_DEL && !event.soft && !event.hard);
  106. if (o->up && !was_up) {
  107. NCDModuleInst_Backend_Up(o->i);
  108. }
  109. else if (!o->up && was_up) {
  110. NCDModuleInst_Backend_Down(o->i);
  111. }
  112. }
  113. static void func_new (NCDModuleInst *i)
  114. {
  115. // allocate instance
  116. struct instance *o = malloc(sizeof(*o));
  117. if (!o) {
  118. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  119. goto fail0;
  120. }
  121. NCDModuleInst_Backend_SetUser(i, o);
  122. // init arguments
  123. o->i = i;
  124. // check arguments
  125. NCDValue *type_arg;
  126. NCDValue *name_arg;
  127. if (!NCDValue_ListRead(i->args, 2, &type_arg, &name_arg)) {
  128. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  129. goto fail1;
  130. }
  131. if (NCDValue_Type(type_arg) != NCDVALUE_STRING || NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  132. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  133. goto fail1;
  134. }
  135. char *type = NCDValue_StringValue(type_arg);
  136. char *name = NCDValue_StringValue(name_arg);
  137. if (!strcmp(type, "index")) {
  138. if (sscanf(name, "%"SCNu32, &o->index) != 1) {
  139. ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
  140. goto fail1;
  141. }
  142. }
  143. else if (!strcmp(type, "wlan")) {
  144. if (!find_wlan_rfill(name, &o->index)) {
  145. ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
  146. goto fail1;
  147. }
  148. }
  149. else {
  150. ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
  151. goto fail1;
  152. }
  153. // init monitor
  154. if (!NCDRfkillMonitor_Init(&o->monitor, o->i->params->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
  155. ModuleLog(o->i, BLOG_ERROR, "monitor failed");
  156. goto fail1;
  157. }
  158. // set not up
  159. o->up = 0;
  160. return;
  161. fail1:
  162. free(o);
  163. fail0:
  164. NCDModuleInst_Backend_SetError(i);
  165. NCDModuleInst_Backend_Dead(i);
  166. }
  167. static void func_die (void *vo)
  168. {
  169. struct instance *o = vo;
  170. NCDModuleInst *i = o->i;
  171. // free monitor
  172. NCDRfkillMonitor_Free(&o->monitor);
  173. // free instance
  174. free(o);
  175. NCDModuleInst_Backend_Dead(i);
  176. }
  177. static const struct NCDModule modules[] = {
  178. {
  179. .type = "net.backend.rfkill",
  180. .func_new = func_new,
  181. .func_die = func_die
  182. }, {
  183. .type = NULL
  184. }
  185. };
  186. const struct NCDModuleGroup ncdmodule_net_backend_rfkill = {
  187. .modules = modules
  188. };