pfsdp_base.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
5 
6 PFSDPBase::PFSDPBase(std::shared_ptr<HandleInfo> info, std::shared_ptr<ScanConfig> config,
7  std::shared_ptr<ScanParameters> params)
8  : http_interface(new HTTPInterface(info->hostname, "cmd")), info_(info), config_(config), params_(params)
9 {
10 }
11 
12 const std::map<std::string, std::string> PFSDPBase::get_request(const std::string& command,
13  const std::vector<std::string>& json_keys,
14  const std::initializer_list<param_type>& query)
15 {
16  return get_request(command, json_keys, param_map_type(query.begin(), query.end()));
17 }
18 
19 const std::map<std::string, std::string> PFSDPBase::get_request(const std::string& command,
20  const std::vector<std::string>& json_keys,
21  const param_map_type& query)
22 {
23  const std::string err_code = "error_code";
24  const std::string err_text = "error_text";
25  const std::string err_http = "error_http";
26  std::vector<std::string> keys = { err_code, err_text };
27  keys.insert(keys.end(), json_keys.begin(), json_keys.end());
28  std::map<std::string, std::string> json_resp = http_interface->get(keys, command, query);
29 
30  if (!check_error(json_resp, err_code, err_text, err_http))
31  {
32  return std::map<std::string, std::string>();
33  }
34 
35  return json_resp;
36 }
37 
38 bool PFSDPBase::get_request_bool(const std::string& command, const std::vector<std::string>& json_keys,
39  const std::initializer_list<param_type>& query)
40 {
41  std::map<std::string, std::string> resp = get_request(command, json_keys, query);
42  if (resp.empty())
43  {
44  return false;
45  }
46  return true;
47 }
48 
49 bool PFSDPBase::is_connection_failure(const std::string& http_error)
50 {
51  std::string error_1 = "Failed to connect to ";
52  std::string error_2 = "No route to host";
53 
54  if (http_error.find(error_1) != std::string::npos && http_error.find(error_2) != std::string::npos)
55  {
56  return true;
57  }
58  return false;
59 }
60 
61 bool PFSDPBase::check_error(std::map<std::string, std::string>& mp, const std::string& err_code,
62  const std::string& err_text, const std::string& err_http)
63 {
64  const std::string http_error = mp[err_http];
65  const std::string code = mp[err_code];
66  const std::string text = mp[err_text];
67 
68  // remove error related key-value pairs
69  mp.erase(err_http);
70  mp.erase(err_code);
71  mp.erase(err_text);
72 
73  // check if HTTP has an error
74  if (http_error.compare(std::string("OK")))
75  {
76  std::cerr << "HTTP ERROR: " << http_error << std::endl;
77  if (is_connection_failure(http_error))
78  {
80  {
82  }
83  }
84  return false;
85  }
86 
87  // check if 'error_code' and 'error_text' does not exist in the response
88  // this happens in case of invalid command
89  if (!code.compare("--COULD NOT RETRIEVE VALUE--") || !text.compare("--COULD NOT RETRIEVE VALUE--"))
90  {
91  std::cout << "Invalid command or parameter requested." << std::endl;
92  return false;
93  }
94  // check for error messages in protocol response
95  if (code.compare("0") || text.compare("success"))
96  {
97  std::cout << "protocol error: " << code << " " << text << std::endl;
98  return false;
99  }
100  return true;
101 }
102 
103 void PFSDPBase::set_connection_failure_cb(std::function<void()> callback)
104 {
105  handle_connection_failure = callback;
106 }
107 
108 const std::vector<std::string> PFSDPBase::list_parameters()
109 {
110  auto resp = get_request("list_parameters", { "parameters" });
111  return parser_utils::split(resp["parameters"]);
112 }
113 
115 {
116  return get_request_bool("reboot_device");
117 }
118 
120 {
121  get_request("factory_reset");
122 }
123 
124 bool PFSDPBase::release_handle(const std::string& handle)
125 {
126  get_request("release_handle", { "" }, { KV("handle", handle) });
127  return true;
128 }
129 
131 {
132  ProtocolInfo opi;
133  auto resp = get_request("get_protocol_info", { "protocol_name", "version_major", "version_minor", "commands" });
134  if (resp.empty())
135  {
136  opi.isError = true;
137  return opi;
138  }
139 
140  opi.version_major = atoi(resp["version_major"].c_str());
141  opi.version_minor = atoi(resp["version_minor"].c_str());
142  opi.protocol_name = resp["protocol_name"];
143  opi.device_family = get_parameter_int("device_family");
144 
145  return opi;
146 }
147 
148 int64_t PFSDPBase::get_parameter_int(const std::string& param)
149 {
150  std::map<std::string, std::string> resp = get_parameter(param);
151  if (resp.empty())
152  {
153  return std::numeric_limits<std::int64_t>::quiet_NaN();
154  }
155  return parser_utils::to_long(resp[param]);
156 }
157 
158 float PFSDPBase::get_parameter_float(const std::string& param)
159 {
160  std::map<std::string, std::string> resp = get_parameter(param);
161  if (resp.empty())
162  {
163  return std::numeric_limits<float>::quiet_NaN();
164  }
165  return parser_utils::to_float(resp[param]);
166 }
167 
168 std::string PFSDPBase::get_parameter_str(const std::string& param)
169 {
170  std::map<std::string, std::string> resp = get_parameter(param);
171  if (resp.empty())
172  {
173  return std::string("");
174  }
175  return resp[param];
176 }
177 
178 void PFSDPBase::request_handle_tcp(const std::string& port, const std::string& packet_type)
179 {
180  param_map_type query;
181  if (!packet_type.empty())
182  {
183  query["packet_type"] = packet_type;
184  }
185  else
186  {
187  query["packet_type"] = config_->packet_type;
188  }
189  auto resp = get_request("request_handle_tcp", { "handle", "port" }, query);
190 
191  info_->handle = resp["handle"];
192  info_->port = resp["port"];
193 
194  // TODO: port and pkt_type should be updated in config_
195 }
196 
197 void PFSDPBase::request_handle_udp(const std::string& packet_type)
198 {
199  param_map_type query = { KV("address", info_->endpoint), KV("port", info_->port) };
200  if (!packet_type.empty())
201  {
202  query["packet_type"] = packet_type;
203  }
204  else
205  {
206  query["packet_type"] = config_->packet_type;
207  }
208  auto resp = get_request("request_handle_udp", { "handle", "port" }, query);
209  info_->handle = resp["handle"];
210 }
211 
212 void PFSDPBase::get_scanoutput_config(const std::string& handle)
213 {
214  auto resp =
215  get_request("get_scanoutput_config",
216  { "start_angle", "packet_type", "watchdogtimeout", "skip_scans", "watchdog", "max_num_points_scan" },
217  { KV("handle", handle) });
218  config_->packet_type = resp["packet_type"];
219  config_->start_angle = parser_utils::to_long(resp["start_angle"]);
220  config_->watchdogtimeout = parser_utils::to_long(resp["watchdogtimeout"]);
221  config_->watchdog = (resp["watchdog"] == "off") ? false : true;
222  config_->skip_scans = parser_utils::to_long(resp["skip_scans"]);
223  config_->max_num_points_scan = parser_utils::to_long(resp["max_num_points_scan"]);
224 }
225 
226 bool PFSDPBase::set_scanoutput_config(const std::string& handle, const ScanConfig& config)
227 {
228  param_map_type query = { KV("handle", handle),
229  KV("start_angle", config.start_angle),
230  KV("packet_type", config.packet_type),
231  KV("max_num_points_scan", config.max_num_points_scan),
232  KV("watchdogtimeout", config.watchdogtimeout),
233  KV("skip_scans", config.skip_scans),
234  KV("watchdog", config.watchdog ? "on" : "off") };
235  auto resp = get_request("set_scanoutput_config", { "" }, query);
236 
237  // update global config_
238  get_scanoutput_config(handle);
240  return true;
241 }
242 
244 {
245  param_map_type query = { KV("handle", info_->handle),
246  KV("start_angle", config_->start_angle),
247  KV("packet_type", config_->packet_type),
248  KV("max_num_points_scan", config_->max_num_points_scan),
249  KV("watchdogtimeout", config_->watchdogtimeout),
250  KV("skip_scans", config_->skip_scans),
251  KV("watchdog", config_->watchdog ? "on" : "off") };
252  auto resp = get_request("set_scanoutput_config", { "" }, query);
253 
254  // recalculate scan params
256  return true;
257 }
258 
260 {
261  get_request("start_scanoutput", { "" }, { { "handle", info_->handle } });
262  return true;
263 }
264 
265 bool PFSDPBase::stop_scanoutput(const std::string& handle)
266 {
267  return get_request_bool("stop_scanoutput", { "" }, { { "handle", handle } });
268 }
269 
270 std::string PFSDPBase::get_scanoutput_config(const std::string& param, const std::string& handle)
271 {
272  auto resp = get_request("get_scanoutput_config", { param }, { KV("handle", handle) });
273  return resp[param];
274 }
275 
276 bool PFSDPBase::feed_watchdog(const std::string& handle)
277 {
278  return get_request_bool("feed_watchdog", { "" }, { { "handle", handle } });
279 }
280 
282 {
283  return std::string("");
284 }
285 
286 std::string PFSDPBase::get_part()
287 {
288  return std::string("");
289 }
290 
292 {
293 }
294 
296 {
297 }
int start_angle
Definition: scan_config.h:10
float to_float(const std::string &s)
void set_connection_failure_cb(std::function< void()> callback)
Definition: pfsdp_base.cpp:103
const std::vector< std::string > split(const std::string &str, const char delim=';')
Definition: parser_utils.cpp:6
bool is_connection_failure(const std::string &http_error)
Definition: pfsdp_base.cpp:49
void factory_reset()
Definition: pfsdp_base.cpp:119
bool get_request_bool(const std::string &command, const std::vector< std::string > &json_keys=std::vector< std::string >(), const std::initializer_list< param_type > &query=std::initializer_list< param_type >())
Definition: pfsdp_base.cpp:38
uint16_t device_family
Definition: protocol_info.h:15
const std::map< std::string, std::string > get_request(const std::string &command, const std::vector< std::string > &json_keys, const std::initializer_list< param_type > &query)
Definition: pfsdp_base.cpp:12
bool reboot_device()
Definition: pfsdp_base.cpp:114
virtual void request_handle_udp(const std::string &packet_type="")
Definition: pfsdp_base.cpp:197
std::string protocol_name
Definition: protocol_info.h:9
virtual std::string get_part()
Definition: pfsdp_base.cpp:286
virtual void setup_param_server()
Definition: pfsdp_base.cpp:295
std::string get_parameter_str(const std::string &param)
Definition: pfsdp_base.cpp:168
std::map< std::string, std::string > param_map_type
const std::vector< std::string > list_parameters()
Definition: pfsdp_base.cpp:108
bool watchdog
Definition: scan_config.h:7
std::map< std::string, std::string > get_parameter(const Ts &... ts)
Definition: pfsdp_base.h:83
bool stop_scanoutput(const std::string &handle)
Definition: pfsdp_base.cpp:265
virtual std::string get_product()
Definition: pfsdp_base.cpp:281
PFSDPBase(std::shared_ptr< HandleInfo > info, std::shared_ptr< ScanConfig > config, std::shared_ptr< ScanParameters > params)
Definition: pfsdp_base.cpp:6
bool feed_watchdog(const std::string &handle)
Definition: pfsdp_base.cpp:276
bool check_error(std::map< std::string, std::string > &mp, const std::string &err_code, const std::string &err_text, const std::string &err_http)
Definition: pfsdp_base.cpp:61
std::int64_t get_parameter_int(const std::string &param)
Definition: pfsdp_base.cpp:148
std::shared_ptr< HandleInfo > info_
Definition: pfsdp_base.h:56
Definition: kv.h:9
uint skip_scans
Definition: scan_config.h:12
HTTPInterfacePtr http_interface
Definition: pfsdp_base.h:36
std::string packet_type
Definition: scan_config.h:9
bool start_scanoutput()
Definition: pfsdp_base.cpp:259
std::int64_t to_long(const std::string &s)
bool update_scanoutput_config()
Definition: pfsdp_base.cpp:243
float get_parameter_float(const std::string &param)
Definition: pfsdp_base.cpp:158
std::function< void()> handle_connection_failure
Definition: pfsdp_base.h:37
virtual void get_scanoutput_config(const std::string &handle)
Definition: pfsdp_base.cpp:212
virtual void get_scan_parameters()
Definition: pfsdp_base.cpp:291
bool set_scanoutput_config(const std::string &handle, const ScanConfig &config)
Definition: pfsdp_base.cpp:226
ProtocolInfo get_protocol_info()
Definition: pfsdp_base.cpp:130
std::shared_ptr< ScanConfig > config_
Definition: pfsdp_base.h:57
uint watchdogtimeout
Definition: scan_config.h:8
void request_handle_tcp(const std::string &port="", const std::string &packet_type="")
Definition: pfsdp_base.cpp:178
bool release_handle(const std::string &handle)
Definition: pfsdp_base.cpp:124
uint max_num_points_scan
Definition: scan_config.h:11


pf_driver
Author(s): Harsh Deshpande
autogenerated on Fri Feb 24 2023 03:59:35