lowlevel.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <utlist.h>
  13. #include "lowlevel.h"
  14. static struct lowlevel_device_info *devinfo_list;
  15. void lowlevel_devinfo_free(struct lowlevel_device_info * const info)
  16. {
  17. if (info->lowl->devinfo_free)
  18. info->lowl->devinfo_free(info);
  19. free(info->product);
  20. free(info->serial);
  21. free(info->path);
  22. free(info);
  23. }
  24. void lowlevel_scan_free()
  25. {
  26. if (!devinfo_list)
  27. return;
  28. struct lowlevel_device_info *info, *tmp;
  29. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  30. {
  31. LL_DELETE(devinfo_list, info);
  32. lowlevel_devinfo_free(info);
  33. }
  34. }
  35. void lowlevel_scan()
  36. {
  37. struct lowlevel_device_info *devinfo_mid_list;
  38. lowlevel_scan_free();
  39. #ifdef USE_X6500
  40. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  41. LL_CONCAT(devinfo_list, devinfo_mid_list);
  42. #endif
  43. #ifdef USE_NANOFURY
  44. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  45. LL_CONCAT(devinfo_list, devinfo_mid_list);
  46. #endif
  47. }
  48. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles)
  49. {
  50. struct lowlevel_device_info *info, *tmp;
  51. int found = 0, i;
  52. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  53. {
  54. if (serial && strcmp(serial, info->serial))
  55. continue;
  56. for (i = 0; product_needles[i]; ++i)
  57. if (!strstr(info->product, product_needles[i]))
  58. goto next;
  59. if (!cb(info))
  60. continue;
  61. LL_DELETE(devinfo_list, info);
  62. ++found;
  63. next: ;
  64. }
  65. return found;
  66. }