net_backend_rfkill.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/extra/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 (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  114. {
  115. struct instance *o = vo;
  116. o->i = i;
  117. // check arguments
  118. NCDValRef type_arg;
  119. NCDValRef name_arg;
  120. if (!NCDVal_ListRead(params->args, 2, &type_arg, &name_arg)) {
  121. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  122. goto fail0;
  123. }
  124. if (!NCDVal_IsString(type_arg) || !NCDVal_IsStringNoNulls(name_arg)) {
  125. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  126. goto fail0;
  127. }
  128. // null terminate name
  129. NCDValNullTermString name_nts;
  130. if (!NCDVal_StringNullTerminate(name_arg, &name_nts)) {
  131. ModuleLog(o->i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  132. goto fail0;
  133. }
  134. if (NCDVal_StringEquals(type_arg, "index")) {
  135. if (sscanf(name_nts.data, "%"SCNu32, &o->index) != 1) {
  136. ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
  137. goto fail1;
  138. }
  139. }
  140. else if (NCDVal_StringEquals(type_arg, "wlan")) {
  141. if (!find_wlan_rfill(name_nts.data, &o->index)) {
  142. ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
  143. goto fail1;
  144. }
  145. }
  146. else {
  147. ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
  148. goto fail1;
  149. }
  150. // init monitor
  151. if (!NCDRfkillMonitor_Init(&o->monitor, o->i->params->iparams->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
  152. ModuleLog(o->i, BLOG_ERROR, "monitor failed");
  153. goto fail1;
  154. }
  155. // set not up
  156. o->up = 0;
  157. // free name nts
  158. NCDValNullTermString_Free(&name_nts);
  159. return;
  160. fail1:
  161. NCDValNullTermString_Free(&name_nts);
  162. fail0:
  163. NCDModuleInst_Backend_SetError(i);
  164. NCDModuleInst_Backend_Dead(i);
  165. }
  166. static void func_die (void *vo)
  167. {
  168. struct instance *o = vo;
  169. // free monitor
  170. NCDRfkillMonitor_Free(&o->monitor);
  171. NCDModuleInst_Backend_Dead(o->i);
  172. }
  173. static struct NCDModule modules[] = {
  174. {
  175. .type = "net.backend.rfkill",
  176. .func_new2 = func_new,
  177. .func_die = func_die,
  178. .alloc_size = sizeof(struct instance)
  179. }, {
  180. .type = NULL
  181. }
  182. };
  183. const struct NCDModuleGroup ncdmodule_net_backend_rfkill = {
  184. .modules = modules
  185. };