parameters.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_PARAMETERS_H
18 #define SCIP2_RESPONSE_PARAMETERS_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 ResponseParams : public Response
31 {
32 public:
33  using Callback = boost::function<void(
34  const boost::posix_time::ptime&,
35  const std::string&,
36  const std::string&,
37  const std::map<std::string, std::string>&)>;
38 
39 protected:
41 
42 public:
43  virtual std::string getCommandCode() const = 0;
44  void operator()(
45  const boost::posix_time::ptime& time_read,
46  const std::string& echo_back,
47  const std::string& status,
48  std::istream& stream)
49  {
50  std::map<std::string, std::string> params;
51  if (status != "00")
52  {
53  if (cb_)
54  cb_(time_read, echo_back, status, params);
55  return;
56  }
57  std::string line;
58  while (std::getline(stream, line))
59  {
60  if (line.size() == 0)
61  break;
62  const auto delm = std::find(line.begin(), line.end(), ':');
63  if (delm == line.end())
64  {
65  logger::error() << "Parameter decode error" << std::endl;
66  return;
67  }
68  const auto end = std::find(line.begin(), line.end(), ';');
69  if (end == line.end())
70  {
71  logger::error() << "Parameter decode error" << std::endl;
72  return;
73  }
74  const uint8_t checksum = line.back();
75  uint8_t sum = 0;
76  for (auto it = line.begin(); it != end; ++it)
77  {
78  sum += *it;
79  }
80  if ((sum & 0x3F) + 0x30 != checksum)
81  {
82  logger::error() << "Checksum mismatch; parameters dropped" << std::endl;
83  return;
84  }
85  const std::string key(line.begin(), delm);
86  const std::string value(delm + 1, end);
87  params[key] = value;
88  }
89  if (cb_)
90  cb_(time_read, echo_back, status, params);
91  }
93  {
94  cb_ = cb;
95  }
96 };
97 
98 class ResponsePP : public ResponseParams
99 {
100 public:
101  std::string getCommandCode() const
102  {
103  return std::string("PP");
104  }
105 };
106 
108 {
109 public:
110  std::string getCommandCode() const
111  {
112  return std::string("VV");
113  }
114 };
115 
117 {
118 public:
119  std::string getCommandCode() const
120  {
121  return std::string("II");
122  }
123 };
124 
125 } // namespace scip2
126 
127 #endif // SCIP2_RESPONSE_PARAMETERS_H
void registerCallback(Callback cb)
Definition: parameters.h:92
virtual std::string getCommandCode() const =0
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const std::map< std::string, std::string > &)> Callback
Definition: parameters.h:37
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream)
Definition: parameters.h:44
std::string getCommandCode() const
Definition: parameters.h:110
std::string getCommandCode() const
Definition: parameters.h:119
std::string getCommandCode() const
Definition: parameters.h:101
std::ostream & error()
Definition: logger.cpp:105


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