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 <cstdint>
23 #include <string>
24 #include <map>
25 
27 #include <scip2/logger.h>
28 
29 namespace scip2
30 {
31 class Timestamp
32 {
33 public:
34  uint32_t timestamp_;
35 
37  : timestamp_(0)
38  {
39  }
40 };
41 
42 class ResponseTM : public Response
43 {
44 public:
45  using Callback = boost::function<void(
46  const boost::posix_time::ptime&,
47  const std::string&,
48  const std::string&,
49  const Timestamp&)>;
50 
51 protected:
53 
54 public:
55  std::string getCommandCode() const
56  {
57  return std::string("TM");
58  }
59  void operator()(
60  const boost::posix_time::ptime& time_read,
61  const std::string& echo_back,
62  const std::string& status,
63  std::istream& stream)
64  {
65  Timestamp timestamp;
66  if (status != "00")
67  {
68  if (cb_)
69  {
70  cb_(time_read, echo_back, status, timestamp);
71  }
72  readUntilEnd(stream);
73  return;
74  }
75  if (echo_back[2] == '1')
76  {
77  std::string stamp;
78  if (!std::getline(stream, stamp))
79  {
80  logger::error() << "Failed to get timestamp" << std::endl;
81  readUntilEnd(stream);
82  return;
83  }
84  const uint8_t checksum = stamp.back();
85  stamp.pop_back(); // remove checksum
86  if (stamp.size() < 4)
87  {
88  logger::error() << "Wrong timestamp format" << std::endl;
89  readUntilEnd(stream);
90  return;
91  }
92 
93  auto dec = Decoder<4>(stamp);
94  auto it = dec.begin();
95  timestamp.timestamp_ = *it;
96  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
97  {
98  logger::error() << "Checksum mismatch" << std::endl;
99  readUntilEnd(stream);
100  return;
101  }
102  }
103  if (cb_)
104  {
105  cb_(time_read, echo_back, status, timestamp);
106  }
107  readUntilEnd(stream);
108  }
110  {
111  cb_ = cb;
112  }
113 };
114 
115 } // namespace scip2
116 
117 #endif // SCIP2_RESPONSE_TIME_SYNC_H
scip2::Response
Definition: abstract.h:27
logger.h
scip2::Timestamp::timestamp_
uint32_t timestamp_
Definition: time_sync.h:34
scip2::ResponseTM::registerCallback
void registerCallback(Callback cb)
Definition: time_sync.h:109
scip2::ResponseTM::operator()
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:59
urg_sim::encode::checksum
std::string checksum(const std::string &a)
Definition: encode.cpp:28
scip2::readUntilEnd
void readUntilEnd(std::istream &stream)
Definition: abstract.h:39
scip2::ResponseTM
Definition: time_sync.h:42
scip2
Definition: connection.h:30
scip2::ResponseTM::getCommandCode
std::string getCommandCode() const
Definition: time_sync.h:55
scip2::ResponseTM::Callback
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const Timestamp &)> Callback
Definition: time_sync.h:49
scip2::Decoder
Definition: decode.h:44
scip2::ResponseTM::cb_
Callback cb_
Definition: time_sync.h:52
scip2::Timestamp
Definition: time_sync.h:31
abstract.h
scip2::Timestamp::Timestamp
Timestamp()
Definition: time_sync.h:36
scip2::logger::error
std::ostream & error()
Definition: logger.cpp:105


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57