lowlevel.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  42. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles)
  43. {
  44. struct lowlevel_device_info *info, *tmp;
  45. int found = 0, i;
  46. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  47. {
  48. if (serial && strcmp(serial, info->serial))
  49. continue;
  50. for (i = 0; product_needles[i]; ++i)
  51. if (!strstr(info->product, product_needles[i]))
  52. goto next;
  53. if (!cb(info))
  54. continue;
  55. LL_DELETE(devinfo_list, info);
  56. ++found;
  57. next: ;
  58. }
  59. return found;
  60. }