lowlevel.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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->manufacturer);
  20. free(info->product);
  21. free(info->serial);
  22. free(info->path);
  23. free(info);
  24. }
  25. void lowlevel_scan_free()
  26. {
  27. if (!devinfo_list)
  28. return;
  29. struct lowlevel_device_info *info, *tmp;
  30. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  31. {
  32. LL_DELETE(devinfo_list, info);
  33. lowlevel_devinfo_free(info);
  34. }
  35. }
  36. void lowlevel_scan()
  37. {
  38. struct lowlevel_device_info *devinfo_mid_list;
  39. lowlevel_scan_free();
  40. #ifdef USE_X6500
  41. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  42. LL_CONCAT(devinfo_list, devinfo_mid_list);
  43. #endif
  44. #ifdef USE_NANOFURY
  45. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  46. LL_CONCAT(devinfo_list, devinfo_mid_list);
  47. #endif
  48. }
  49. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles)
  50. {
  51. struct lowlevel_device_info *info, *tmp;
  52. int found = 0, i;
  53. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  54. {
  55. if (serial && strcmp(serial, info->serial))
  56. continue;
  57. for (i = 0; product_needles[i]; ++i)
  58. if (!strstr(info->product, product_needles[i]))
  59. goto next;
  60. if (!cb(info))
  61. continue;
  62. LL_DELETE(devinfo_list, info);
  63. ++found;
  64. next: ;
  65. }
  66. return found;
  67. }