timesync.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2019 Livox. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. #ifndef TIMESYNC_TIMESYNC_H_
  25. #define TIMESYNC_TIMESYNC_H_
  26. #include <thread>
  27. #include "comm_device.h"
  28. #include "comm_protocol.h"
  29. #include "user_uart.h"
  30. namespace livox_ros {
  31. typedef void (*FnReceiveSyncTimeCb)(const char *rmc, uint32_t rmc_length,
  32. void *client_data);
  33. enum FsmPollState { kOpenDev, kPrepareDev, kCheckDevState, kFsmDevUndef };
  34. typedef struct {
  35. CommDevConfig dev_config;
  36. ProtocolConfig protocol_config;
  37. } TimeSyncConfig;
  38. class TimeSync {
  39. public:
  40. static TimeSync *GetInstance() {
  41. static TimeSync time_sync;
  42. return &time_sync;
  43. }
  44. int32_t InitTimeSync(const TimeSyncConfig &config);
  45. int32_t DeInitTimeSync();
  46. void StartTimesync() {
  47. start_poll_state_ = true;
  48. start_poll_data_ = true;
  49. }
  50. int32_t SetReceiveSyncTimeCb(FnReceiveSyncTimeCb cb, void *data) {
  51. if ((cb != nullptr) || (data != nullptr)) {
  52. fn_cb_ = cb;
  53. client_data_ = data;
  54. return 0;
  55. } else {
  56. return -1;
  57. }
  58. }
  59. private:
  60. TimeSync();
  61. ~TimeSync();
  62. TimeSync(const TimeSync &) = delete;
  63. TimeSync &operator=(const TimeSync &) = delete;
  64. void PollStateLoop();
  65. void PollDataLoop();
  66. void StopTimesync();
  67. std::shared_ptr<std::thread> t_poll_state_;
  68. volatile bool exit_poll_state_;
  69. volatile bool start_poll_state_;
  70. std::shared_ptr<std::thread> t_poll_data_;
  71. volatile bool exit_poll_data_;
  72. volatile bool start_poll_data_;
  73. TimeSyncConfig config_;
  74. UserUart *uart_;
  75. CommProtocol *comm_;
  76. volatile uint32_t rx_bytes_;
  77. FnReceiveSyncTimeCb fn_cb_;
  78. void *client_data_;
  79. volatile uint8_t fsm_state_;
  80. std::chrono::steady_clock::time_point transfer_time_;
  81. void FsmTransferState(uint8_t new_state);
  82. void FsmOpenDev();
  83. void FsmPrepareDev();
  84. void FsmCheckDevState();
  85. };
  86. } // namespace livox_ros
  87. #endif