Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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();
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 }
00106
00107 #endif // SCIP2_RESPONSE_TIME_SYNC_H