reader_service.h 1.9 KB

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