sys_watch_usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. * @file sys_watch_usb.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. * USB device watcher.
  32. *
  33. * Synopsis: sys.watch_usb()
  34. * Description: reports USB device events. Transitions up when an event is detected, and
  35. * goes down waiting for the next event when ->nextevent() is called.
  36. * On startup, "added" events are reported for existing USB devices.
  37. *
  38. * Variables:
  39. * string event_type - what happened with the USB device: "added" or "removed"
  40. * string devname - device node path, e.g. /dev/bus/usb/XXX/YYY
  41. * string vendor_id - vendor ID, e.g. 046d
  42. * string model_id - model ID, e.g. c03e
  43. *
  44. * Synopsis: sys.watch_usb::nextevent()
  45. * Description: makes the watch_usb module transition down in order to report the next event.
  46. */
  47. #include <inttypes.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <stdio.h>
  51. #include <misc/debug.h>
  52. #include <misc/offset.h>
  53. #include <misc/parse_number.h>
  54. #include <structure/LinkedList1.h>
  55. #include <udevmonitor/NCDUdevManager.h>
  56. #include <ncd/NCDModule.h>
  57. #include <ncd/modules/event_template.h>
  58. #include <generated/blog_channel_ncd_sys_watch_usb.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. struct device {
  61. char *devname;
  62. char *devpath;
  63. uint16_t vendor_id;
  64. uint16_t model_id;
  65. BStringMap removed_map;
  66. LinkedList1Node devices_list_node;
  67. };
  68. struct instance {
  69. NCDModuleInst *i;
  70. NCDUdevClient client;
  71. LinkedList1 devices_list;
  72. event_template templ;
  73. };
  74. static void templ_func_free (struct instance *o);
  75. static struct device * find_device_by_devname (struct instance *o, const char *devname)
  76. {
  77. for (LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list); list_node; list_node = LinkedList1Node_Next(list_node)) {
  78. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  79. if (!strcmp(device->devname, devname)) {
  80. return device;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  86. {
  87. for (LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list); list_node; list_node = LinkedList1Node_Next(list_node)) {
  88. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  89. if (!strcmp(device->devpath, devpath)) {
  90. return device;
  91. }
  92. }
  93. return NULL;
  94. }
  95. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  96. {
  97. // remove from devices list
  98. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  99. // free removed map
  100. if (have_removed_map) {
  101. BStringMap_Free(&device->removed_map);
  102. }
  103. // free devpath
  104. free(device->devpath);
  105. // free devname
  106. free(device->devname);
  107. // free structure
  108. free(device);
  109. }
  110. static int make_event_map (struct instance *o, int added, const char *devname, uint16_t vendor_id, uint16_t model_id, BStringMap *out_map)
  111. {
  112. // init map
  113. BStringMap map;
  114. BStringMap_Init(&map);
  115. // set type
  116. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  117. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  118. goto fail1;
  119. }
  120. // set devname
  121. if (!BStringMap_Set(&map, "devname", devname)) {
  122. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  123. goto fail1;
  124. }
  125. // set vendor ID
  126. char vendor_id_str[5];
  127. sprintf(vendor_id_str, "%04"PRIx16, vendor_id);
  128. if (!BStringMap_Set(&map, "vendor_id", vendor_id_str)) {
  129. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  130. goto fail1;
  131. }
  132. // set model ID
  133. char model_id_str[5];
  134. sprintf(model_id_str, "%04"PRIx16, model_id);
  135. if (!BStringMap_Set(&map, "model_id", model_id_str)) {
  136. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  137. goto fail1;
  138. }
  139. *out_map = map;
  140. return 1;
  141. fail1:
  142. BStringMap_Free(&map);
  143. return 0;
  144. }
  145. static void queue_event (struct instance *o, BStringMap map)
  146. {
  147. // pass event to template
  148. int was_empty;
  149. event_template_queue(&o->templ, map, &was_empty);
  150. // if event queue was empty, stop receiving udev events
  151. if (was_empty) {
  152. NCDUdevClient_Pause(&o->client);
  153. }
  154. }
  155. static void add_device (struct instance *o, const char *devname, const char *devpath, uint16_t vendor_id, uint16_t model_id)
  156. {
  157. ASSERT(!find_device_by_devname(o, devname))
  158. ASSERT(!find_device_by_devpath(o, devpath))
  159. // allocate structure
  160. struct device *device = malloc(sizeof(*device));
  161. if (!device) {
  162. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  163. goto fail0;
  164. }
  165. // init devname
  166. if (!(device->devname = strdup(devname))) {
  167. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  168. goto fail1;
  169. }
  170. // init devpath
  171. if (!(device->devpath = strdup(devpath))) {
  172. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  173. goto fail2;
  174. }
  175. // set vendor and model ID
  176. device->vendor_id = vendor_id;
  177. device->model_id = model_id;
  178. // init removed map
  179. if (!make_event_map(o, 0, devname, vendor_id, model_id, &device->removed_map)) {
  180. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  181. goto fail3;
  182. }
  183. // init added map
  184. BStringMap added_map;
  185. if (!make_event_map(o, 1, devname, vendor_id, model_id, &added_map)) {
  186. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  187. goto fail4;
  188. }
  189. // insert to devices list
  190. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  191. // queue event
  192. queue_event(o, added_map);
  193. return;
  194. fail4:
  195. BStringMap_Free(&device->removed_map);
  196. fail3:
  197. free(device->devpath);
  198. fail2:
  199. free(device->devname);
  200. fail1:
  201. free(device);
  202. fail0:
  203. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", devname);
  204. }
  205. static void remove_device (struct instance *o, struct device *device)
  206. {
  207. queue_event(o, device->removed_map);
  208. free_device(o, device, 0);
  209. }
  210. static void next_event (struct instance *o)
  211. {
  212. ASSERT(event_template_is_enabled(&o->templ))
  213. // order template to finish the current event
  214. int is_empty;
  215. event_template_dequeue(&o->templ, &is_empty);
  216. // if template has no events, continue udev events
  217. if (is_empty) {
  218. NCDUdevClient_Continue(&o->client);
  219. }
  220. }
  221. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  222. {
  223. // lookup existing device with this devpath
  224. struct device *ex_device = find_device_by_devpath(o, devpath);
  225. // lookup cache entry
  226. const BStringMap *cache_map = NCDUdevManager_Query(o->i->params->iparams->umanager, devpath);
  227. if (!cache_map) {
  228. if (ex_device) {
  229. remove_device(o, ex_device);
  230. }
  231. goto out;
  232. }
  233. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  234. const char *devname = BStringMap_Get(cache_map, "DEVNAME");
  235. const char *devtype = BStringMap_Get(cache_map, "DEVTYPE");
  236. const char *vendor_id_str = BStringMap_Get(cache_map, "ID_VENDOR_ID");
  237. const char *model_id_str = BStringMap_Get(cache_map, "ID_MODEL_ID");
  238. uintmax_t vendor_id;
  239. uintmax_t model_id;
  240. if (!(subsystem && !strcmp(subsystem, "usb") &&
  241. devname &&
  242. devtype && !strcmp(devtype, "usb_device") &&
  243. vendor_id_str && parse_unsigned_hex_integer(vendor_id_str, &vendor_id) &&
  244. model_id_str && parse_unsigned_hex_integer(model_id_str, &model_id)
  245. )) {
  246. if (ex_device) {
  247. remove_device(o, ex_device);
  248. }
  249. goto out;
  250. }
  251. if (ex_device && (
  252. strcmp(ex_device->devname, devname) ||
  253. ex_device->vendor_id != vendor_id || ex_device->model_id != model_id
  254. )) {
  255. remove_device(o, ex_device);
  256. ex_device = NULL;
  257. }
  258. if (!ex_device) {
  259. struct device *ex_devname_device = find_device_by_devname(o, devname);
  260. if (ex_devname_device) {
  261. remove_device(o, ex_devname_device);
  262. }
  263. add_device(o, devname, devpath, vendor_id, model_id);
  264. }
  265. out:
  266. free(devpath);
  267. if (have_map) {
  268. BStringMap_Free(&map);
  269. }
  270. }
  271. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  272. {
  273. struct instance *o = vo;
  274. o->i = i;
  275. // check arguments
  276. if (!NCDVal_ListRead(params->args, 0)) {
  277. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  278. goto fail0;
  279. }
  280. // init client
  281. NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);
  282. // init devices list
  283. LinkedList1_Init(&o->devices_list);
  284. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  285. return;
  286. fail0:
  287. NCDModuleInst_Backend_SetError(i);
  288. NCDModuleInst_Backend_Dead(i);
  289. }
  290. static void templ_func_free (struct instance *o)
  291. {
  292. // free devices
  293. while (!LinkedList1_IsEmpty(&o->devices_list)) {
  294. struct device *device = UPPER_OBJECT(LinkedList1_GetFirst(&o->devices_list), struct device, devices_list_node);
  295. free_device(o, device, 1);
  296. }
  297. // free client
  298. NCDUdevClient_Free(&o->client);
  299. NCDModuleInst_Backend_Dead(o->i);
  300. }
  301. static void func_die (void *vo)
  302. {
  303. struct instance *o = vo;
  304. event_template_die(&o->templ);
  305. }
  306. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  307. {
  308. struct instance *o = vo;
  309. return event_template_getvar(&o->templ, name, mem, out);
  310. }
  311. static void nextevent_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  312. {
  313. // check arguments
  314. if (!NCDVal_ListRead(params->args, 0)) {
  315. ModuleLog(i, BLOG_ERROR, "wrong arity");
  316. goto fail0;
  317. }
  318. // get method object
  319. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  320. // make sure we are currently reporting an event
  321. if (!event_template_is_enabled(&mo->templ)) {
  322. ModuleLog(i, BLOG_ERROR, "not reporting an event");
  323. goto fail0;
  324. }
  325. // signal up.
  326. // Do it before finishing the event so our process does not advance any further if
  327. // we would be killed the event provider going down.
  328. NCDModuleInst_Backend_Up(i);
  329. // wait for next event
  330. next_event(mo);
  331. return;
  332. fail0:
  333. NCDModuleInst_Backend_SetError(i);
  334. NCDModuleInst_Backend_Dead(i);
  335. }
  336. static struct NCDModule modules[] = {
  337. {
  338. .type = "sys.watch_usb",
  339. .func_new2 = func_new,
  340. .func_die = func_die,
  341. .func_getvar = func_getvar,
  342. .alloc_size = sizeof(struct instance)
  343. }, {
  344. .type = "sys.watch_usb::nextevent",
  345. .func_new2 = nextevent_func_new
  346. }, {
  347. .type = NULL
  348. }
  349. };
  350. const struct NCDModuleGroup ncdmodule_sys_watch_usb = {
  351. .modules = modules
  352. };