reader_service.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef RUNNER_READER_SERVICE_H_
  2. #define RUNNER_READER_SERVICE_H_
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <functional>
  7. #include <thread>
  8. #include <atomic>
  9. #include <chrono>
  10. #include <set>
  11. #include <mutex>
  12. extern "C"
  13. {
  14. #include "SWHidApi.h"
  15. }
  16. // Define data callback type to notify UI of received tag data
  17. typedef std::function<void(const std::string &)> DataCallback;
  18. // Service for handling USB scanning functionality
  19. class ReaderService
  20. {
  21. public:
  22. // Get singleton instance
  23. static ReaderService *GetInstance();
  24. // Scan USB devices and return list of device names
  25. std::vector<std::string> ScanUsb();
  26. // Connect to specified USB device by index
  27. bool Connect(int deviceIndex);
  28. // Disconnect
  29. void Disconnect();
  30. // Start reading in answer mode
  31. bool StartRead();
  32. // Stop reading in answer mode
  33. void StopRead();
  34. void AnswerMode();
  35. // Set data callback function to notify UI of received data
  36. void SetDataCallback(DataCallback callback) { data_callback_ = callback; }
  37. // Get device system information and return it as a string
  38. std::string GetDeviceInfo();
  39. // Get current power level (returns true on success)
  40. bool GetPower(unsigned char& power);
  41. // Set power level (returns true on success)
  42. bool SetPower(unsigned char power);
  43. private:
  44. ReaderService() = default;
  45. ~ReaderService();
  46. // Disable copy constructor and assignment operator
  47. ReaderService(const ReaderService &) = delete;
  48. ReaderService &operator=(const ReaderService &) = delete;
  49. // Static callback function for receiving device data
  50. static void DeviceCallback(int msg, int param1, unsigned char *param2, int param3, unsigned char *param4);
  51. // Function to run the timer loop for periodically calling AnswerMode
  52. void RunTimer();
  53. // Function to periodically send tag list to UI
  54. void SendTagList();
  55. // Member variables
  56. bool connected_ = false;
  57. bool reading_ = false;
  58. std::atomic<bool> timer_running_{false};
  59. std::unique_ptr<std::thread> timer_thread_;
  60. std::unique_ptr<std::thread> callback_thread_;
  61. DataCallback data_callback_;
  62. // Store unique tags
  63. std::set<std::string> tags_;
  64. std::mutex tags_mutex_;
  65. };
  66. #endif // RUNNER_READER_SERVICE_H_