sys_watch_usb.c 13 KB

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