sys_watch_usb.c 12 KB

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