time_sync.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The urg_stamped Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef SCIP2_RESPONSE_TIME_SYNC_H
00018 #define SCIP2_RESPONSE_TIME_SYNC_H
00019 
00020 #include <boost/asio.hpp>
00021 
00022 #include <string>
00023 #include <map>
00024 
00025 #include <scip2/response/abstract.h>
00026 
00027 namespace scip2
00028 {
00029 class Timestamp
00030 {
00031 public:
00032   uint32_t timestamp_;
00033 
00034   Timestamp()
00035     : timestamp_(0)
00036   {
00037   }
00038 };
00039 
00040 class ResponseTM : public Response
00041 {
00042 public:
00043   using Callback = boost::function<void(
00044       const boost::posix_time::ptime &,
00045       const std::string &,
00046       const std::string &,
00047       const Timestamp &)>;
00048 
00049 protected:
00050   Callback cb_;
00051 
00052 public:
00053   std::string getCommandCode() const
00054   {
00055     return std::string("TM");
00056   }
00057   void operator()(
00058       const boost::posix_time::ptime &time_read,
00059       const std::string &echo_back,
00060       const std::string &status,
00061       std::istream &stream)
00062   {
00063     Timestamp timestamp;
00064     if (status != "00")
00065     {
00066       if (cb_)
00067         cb_(time_read, echo_back, status, timestamp);
00068       std::cout << echo_back << " errored with " << status << std::endl;
00069       return;
00070     }
00071     if (echo_back[2] == '1')
00072     {
00073       std::string stamp;
00074       if (!std::getline(stream, stamp))
00075       {
00076         std::cerr << "Failed to get timestamp" << std::endl;
00077         return;
00078       }
00079       const uint8_t checksum = stamp.back();
00080       stamp.pop_back();  // remove checksum
00081       if (stamp.size() < 4)
00082       {
00083         std::cerr << "Wrong timestamp format" << std::endl;
00084         return;
00085       }
00086 
00087       auto dec = Decoder<4>(stamp);
00088       auto it = dec.begin();
00089       timestamp.timestamp_ = *it;
00090       if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
00091       {
00092         std::cerr << "Checksum mismatch" << std::endl;
00093         return;
00094       }
00095     }
00096     if (cb_)
00097       cb_(time_read, echo_back, status, timestamp);
00098   }
00099   void registerCallback(Callback cb)
00100   {
00101     cb_ = cb;
00102   }
00103 };
00104 
00105 }  // namespace scip2
00106 
00107 #endif  // SCIP2_RESPONSE_TIME_SYNC_H


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 18:59:51