timesync.cpp
Go to the documentation of this file.
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 
25 #include "timesync.h"
26 
27 #include <stdint.h>
28 #include <string.h>
29 #include <chrono>
30 #include <cstdio>
31 #include <functional>
32 #include <thread>
33 
34 namespace livox_ros {
35 using namespace std;
36 
38  : exit_poll_state_(false),
39  start_poll_state_(false),
40  exit_poll_data_(false),
41  start_poll_data_(false) {
43  uart_ = nullptr;
44  comm_ = nullptr;
45  fn_cb_ = nullptr;
46  client_data_ = nullptr;
47  rx_bytes_ = 0;
48 }
49 
51 
53  config_ = config;
54 
56  uint8_t baudrate_index = config_.dev_config.config.uart.baudrate;
57  uint8_t parity_index = config_.dev_config.config.uart.parity;
58 
59  if ((baudrate_index < BRUnkown) && (parity_index < ParityUnkown)) {
60  uart_ = new UserUart(baudrate_index, parity_index);
61  } else {
62  printf("Uart parameter error, please check the configuration file!\n");
63  return -1;
64  }
65  } else {
66  printf("Device type not supported, now only uart is supported!\n");
67  return -1;
68  }
69 
72 
74  std::make_shared<std::thread>(std::bind(&TimeSync::PollStateLoop, this));
75  t_poll_data_ =
76  std::make_shared<std::thread>(std::bind(&TimeSync::PollDataLoop, this));
77 
78  return 0;
79 }
80 
82  StopTimesync();
83 
84  if (uart_) delete uart_;
85  if (comm_) delete comm_;
86 
87  fn_cb_ = nullptr;
88  client_data_ = nullptr;
89  return 0;
90 }
91 
93  start_poll_state_ = false;
94  start_poll_data_ = false;
95  exit_poll_state_ = true;
96  exit_poll_data_ = true;
97  if (t_poll_state_) {
98  t_poll_state_->join();
99  t_poll_state_ = nullptr;
100  }
101 
102  if (t_poll_data_) {
103  t_poll_data_->join();
104  t_poll_data_ = nullptr;
105  }
106 }
107 
109  while (!start_poll_state_) {
110  /* waiting to start */
111  }
112 
113  while (!exit_poll_state_) {
114  if (fsm_state_ == kOpenDev) {
115  FsmOpenDev();
116  } else if (fsm_state_ == kPrepareDev) {
117  FsmPrepareDev();
118  } else if (fsm_state_ == kCheckDevState) {
120  }
121  std::this_thread::sleep_for(std::chrono::milliseconds(50));
122  }
123 }
124 
126  while (!start_poll_data_) {
127  /* waiting to start */
128  }
129 
130  while (!exit_poll_data_) {
131  if (uart_->IsOpen()) {
132  uint32_t get_buf_size;
133  uint8_t *cache_buf = comm_->FetchCacheFreeSpace(&get_buf_size);
134  if (get_buf_size) {
135  uint32_t read_data_size;
136  read_data_size = uart_->Read((char *)cache_buf, get_buf_size);
137  if (read_data_size) {
138  comm_->UpdateCacheWrIdx(read_data_size);
139  rx_bytes_ += read_data_size;
140  CommPacket packet;
141  memset(&packet, 0, sizeof(packet));
142  while ((kParseSuccess == comm_->ParseCommStream(&packet))) {
143  if ((fn_cb_ != nullptr) || (client_data_ != nullptr)) {
144  fn_cb_((const char *)packet.data, packet.data_len, client_data_);
145  }
146  }
147  }
148  }
149  } else {
150  std::this_thread::sleep_for(std::chrono::milliseconds(50));
151  }
152  }
153 }
154 
156  if (new_state < kFsmDevUndef) {
157  fsm_state_ = new_state;
158  }
159  transfer_time_ = chrono::steady_clock::now();
160 }
161 
163  if (!uart_->IsOpen()) {
164  if (!uart_->Open(config_.dev_config.name)) {
166  }
167  } else {
169  }
170 }
171 
173  chrono::steady_clock::time_point t = chrono::steady_clock::now();
174  chrono::milliseconds time_gap =
175  chrono::duration_cast<chrono::milliseconds>(t - transfer_time_);
177  if (time_gap.count() > 3000) {
179  }
180 }
181 
183  static uint32_t last_rx_bytes = 0;
184  static chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
185 
186  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
187  chrono::milliseconds time_gap =
188  chrono::duration_cast<chrono::milliseconds>(t2 - t1);
189 
190  if (time_gap.count() > 2000) { /* period : 2.5s */
191  if (last_rx_bytes == rx_bytes_) {
192  uart_->Close();
194  printf("Uart is disconnected, close it\n");
195  }
196  last_rx_bytes = rx_bytes_;
197  t1 = t2;
198  }
199 }
200 
201 } // namespace livox_ros
int32_t InitTimeSync(const TimeSyncConfig &config)
Definition: timesync.cpp:52
void FsmTransferState(uint8_t new_state)
Definition: timesync.cpp:155
volatile bool exit_poll_data_
Definition: timesync.h:85
CommDevUartConfig uart
Definition: comm_device.h:64
union livox_ros::CommDevConfig::@0 config
std::shared_ptr< std::thread > t_poll_state_
Definition: timesync.h:80
volatile uint8_t fsm_state_
Definition: timesync.h:96
std::chrono::steady_clock::time_point transfer_time_
Definition: timesync.h:97
unsigned char uint8_t
Definition: stdint.h:125
CommProtocol * comm_
Definition: timesync.h:90
int32_t DeInitTimeSync()
Definition: timesync.cpp:81
ProtocolConfig protocol_config
Definition: timesync.h:42
int Open(const char *filename)
Definition: user_uart.cpp:53
uint8_t * FetchCacheFreeSpace(uint32_t *o_len)
volatile uint32_t rx_bytes_
Definition: timesync.h:91
unsigned int uint32_t
Definition: stdint.h:127
std::shared_ptr< std::thread > t_poll_data_
Definition: timesync.h:84
volatile bool start_poll_state_
Definition: timesync.h:82
ssize_t Read(char *buffer, size_t size)
Definition: user_uart.cpp:186
int32_t UpdateCacheWrIdx(uint32_t used_size)
CommDevConfig dev_config
Definition: timesync.h:41
volatile bool exit_poll_state_
Definition: timesync.h:81
char name[kDevNameLengthMax]
Definition: comm_device.h:62
void * client_data_
Definition: timesync.h:94
signed int int32_t
Definition: stdint.h:124
TimeSyncConfig config_
Definition: timesync.h:88
int32_t ParseCommStream(CommPacket *o_pack)
UserUart * uart_
Definition: timesync.h:89
volatile bool start_poll_data_
Definition: timesync.h:86
FnReceiveSyncTimeCb fn_cb_
Definition: timesync.h:93


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46