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. info->lowl->devinfo_free(info);
  18. free(info->product);
  19. free(info->serial);
  20. free(info);
  21. }
  22. void lowlevel_scan_free()
  23. {
  24. if (!devinfo_list)
  25. return;
  26. struct lowlevel_device_info *info, *tmp;
  27. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  28. {
  29. LL_DELETE(devinfo_list, info);
  30. lowlevel_devinfo_free(info);
  31. }
  32. }
  33. void lowlevel_scan()
  34. {
  35. struct lowlevel_device_info *devinfo_mid_list;
  36. lowlevel_scan_free();
  37. #ifdef USE_X6500
  38. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  39. LL_CONCAT(devinfo_list, devinfo_mid_list);
  40. #endif
  41. #ifdef USE_NANOFURY
  42. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  43. LL_CONCAT(devinfo_list, devinfo_mid_list);
  44. #endif
  45. }
  46. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles)
  47. {
  48. struct lowlevel_device_info *info, *tmp;
  49. int found = 0, i;
  50. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  51. {
  52. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  53. continue;
  54. if (product_needles[0] && !info->product)
  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. }