time_sync.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_RESPONSE_TIME_SYNC_H
18 #define SCIP2_RESPONSE_TIME_SYNC_H
19 
20 #include <boost/asio.hpp>
21 
22 #include <string>
23 #include <map>
24 
26 
27 namespace scip2
28 {
29 class Timestamp
30 {
31 public:
32  uint32_t timestamp_;
33 
35  : timestamp_(0)
36  {
37  }
38 };
39 
40 class ResponseTM : public Response
41 {
42 public:
43  using Callback = boost::function<void(
44  const boost::posix_time::ptime &,
45  const std::string &,
46  const std::string &,
47  const Timestamp &)>;
48 
49 protected:
51 
52 public:
53  std::string getCommandCode() const
54  {
55  return std::string("TM");
56  }
57  void operator()(
58  const boost::posix_time::ptime &time_read,
59  const std::string &echo_back,
60  const std::string &status,
61  std::istream &stream)
62  {
63  Timestamp timestamp;
64  if (status != "00")
65  {
66  if (cb_)
67  cb_(time_read, echo_back, status, timestamp);
68  std::cout << echo_back << " errored with " << status << std::endl;
69  return;
70  }
71  if (echo_back[2] == '1')
72  {
73  std::string stamp;
74  if (!std::getline(stream, stamp))
75  {
76  std::cerr << "Failed to get timestamp" << std::endl;
77  return;
78  }
79  const uint8_t checksum = stamp.back();
80  stamp.pop_back(); // remove checksum
81  if (stamp.size() < 4)
82  {
83  std::cerr << "Wrong timestamp format" << std::endl;
84  return;
85  }
86 
87  auto dec = Decoder<4>(stamp);
88  auto it = dec.begin();
89  timestamp.timestamp_ = *it;
90  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
91  {
92  std::cerr << "Checksum mismatch" << std::endl;
93  return;
94  }
95  }
96  if (cb_)
97  cb_(time_read, echo_back, status, timestamp);
98  }
100  {
101  cb_ = cb;
102  }
103 };
104 
105 } // namespace scip2
106 
107 #endif // SCIP2_RESPONSE_TIME_SYNC_H
void registerCallback(Callback cb)
Definition: time_sync.h:99
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream)
Definition: time_sync.h:57
std::string getCommandCode() const
Definition: time_sync.h:53
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const Timestamp &)> Callback
Definition: time_sync.h:47
uint32_t timestamp_
Definition: time_sync.h:32
Callback cb_
Definition: time_sync.h:50
Iterator begin()
Definition: decode.h:115


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:59