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 #include <scip2/logger.h>
27 
28 namespace scip2
29 {
30 class Timestamp
31 {
32 public:
33  uint32_t timestamp_;
34 
36  : timestamp_(0)
37  {
38  }
39 };
40 
41 class ResponseTM : public Response
42 {
43 public:
44  using Callback = boost::function<void(
45  const boost::posix_time::ptime&,
46  const std::string&,
47  const std::string&,
48  const Timestamp&)>;
49 
50 protected:
52 
53 public:
54  std::string getCommandCode() const
55  {
56  return std::string("TM");
57  }
58  void operator()(
59  const boost::posix_time::ptime& time_read,
60  const std::string& echo_back,
61  const std::string& status,
62  std::istream& stream)
63  {
64  Timestamp timestamp;
65  if (status != "00")
66  {
67  if (cb_)
68  cb_(time_read, echo_back, status, timestamp);
69  return;
70  }
71  if (echo_back[2] == '1')
72  {
73  std::string stamp;
74  if (!std::getline(stream, stamp))
75  {
76  logger::error() << "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  logger::error() << "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  logger::error() << "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:58
std::string getCommandCode() const
Definition: time_sync.h:54
uint32_t timestamp_
Definition: time_sync.h:33
Callback cb_
Definition: time_sync.h:51
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const Timestamp &)> Callback
Definition: time_sync.h:48
Iterator begin()
Definition: decode.h:115
std::ostream & error()
Definition: logger.cpp:105


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05