terminal-parser.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include "terminal-parser.h"
5 #include <iostream>
6 
7 namespace librealsense
8 {
9 
10  using namespace std;
11 
13  {
14  if (!xml_content.empty())
15  {
16  parse_xml_from_memory(xml_content.c_str(), _cmd_xml);
17  update_format_type_to_lambda(_format_type_to_lambda);
18  }
19  }
20 
21 
22  std::vector<uint8_t> terminal_parser::parse_command(const std::string& line) const
23  {
25  vector<string> params;
26 
27  get_command_and_params_from_input(line, command, params);
28 
29  auto raw_data = build_raw_command_data(command, params);
30 
31  for (auto b : raw_data)
32  {
33  cout << hex << fixed << setfill('0') << setw(2) << (int)b << " ";
34  }
35  cout << endl;
36 
37  return raw_data;
38  }
39 
40  std::vector<uint8_t> terminal_parser::parse_response(const std::string& line,
41  const std::vector<uint8_t>& response) const
42  {
44  vector<string> params;
45 
46  get_command_and_params_from_input(line, command, params);
47 
48  unsigned returned_opcode = *response.data();
49  // check returned opcode
50  if (command.op_code != returned_opcode)
51  {
52  stringstream msg;
53  msg << "OpCodes do not match! Sent 0x" << hex << command.op_code << " but received 0x" << hex << (returned_opcode) << "!";
54  throw runtime_error(msg.str());
55  }
56 
57  if (command.is_read_command)
58  {
59  string data;
60  decode_string_from_raw_data(command, _cmd_xml.custom_formatters, response.data(), response.size(), data, _format_type_to_lambda);
61  vector<uint8_t> data_vec;
62  data_vec.insert(data_vec.begin(), data.begin(), data.end());
63  return data_vec;
64  }
65  else
66  {
67  return response;
68  }
69  }
70 
71  vector<uint8_t> terminal_parser::build_raw_command_data(const command_from_xml& command, const vector<string>& params) const
72  {
73  if (params.size() > command.parameters.size() && !command.is_cmd_write_data)
74  throw runtime_error("Input string was not in a correct format!");
75 
76  vector<parameter> vec_parameters;
77  for (auto param_index = 0; param_index < params.size(); ++param_index)
78  {
79  auto is_there_write_data = param_index >= int(command.parameters.size());
80  auto name = (is_there_write_data) ? "" : command.parameters[param_index].name;
81  auto is_reverse_bytes = (is_there_write_data) ? false : command.parameters[param_index].is_reverse_bytes;
82  auto is_decimal = (is_there_write_data) ? false : command.parameters[param_index].is_decimal;
83  auto format_length = (is_there_write_data) ? -1 : command.parameters[param_index].format_length;
84  vec_parameters.push_back(parameter(name, params[param_index], is_decimal, is_reverse_bytes, format_length));
85  }
86 
87  vector<uint8_t> raw_data;
88  encode_raw_data_command(command, vec_parameters, raw_data);
89  return raw_data;
90  }
91 
93  vector<string>& params) const
94  {
95  vector<string> tokens;
96  stringstream ss(line);
97  string word;
98  while (ss >> word)
99  {
100  stringstream converter;
101  converter << hex << word;
102  tokens.push_back(word);
103  }
104 
105  if (tokens.empty())
106  throw runtime_error("Wrong input!");
107 
108  auto command_str = tokens.front();
109  auto it = _cmd_xml.commands.find(command_str);
110  if (it == _cmd_xml.commands.end())
111  throw runtime_error("Command not found!");
112 
113  command = it->second;
114  for (auto i = 1; i < tokens.size(); ++i)
115  params.push_back(tokens[i]);
116  }
117 
118 }
void get_command_and_params_from_input(const std::string &line, command_from_xml &command, std::vector< std::string > &params) const
GLboolean GLboolean GLboolean b
GLuint const GLchar * name
std::vector< uint8_t > parse_command(const std::string &command) const
bool is_cmd_write_data
Definition: parser.hpp:173
void decode_string_from_raw_data(const command_from_xml &command, const std::map< std::string, custom_formatter > &custom_formatters, const uint8_t *raw_data_offset, size_t data_size, std::string &output, const std::map< std::string, xml_parser_function > &format_type_to_lambda)
Definition: parser.hpp:541
unsigned int op_code
Definition: parser.hpp:166
GLsizei const GLchar *const * string
vector< uint8_t > build_raw_command_data(const command &command, const vector< string > &params)
Definition: rs-terminal.cpp:19
void parse_xml_from_memory(const char *content, commands_xml &cmd_xml)
Definition: parser.hpp:243
std::ostream & cout()
std::vector< parameter > parameters
Definition: parser.hpp:181
void update_format_type_to_lambda(std::map< std::string, xml_parser_function > &format_type_to_lambda)
Definition: parser.hpp:387
GLenum const GLfloat * params
terminal_parser(const std::string &xml_content)
static auto it
std::vector< uint8_t > parse_response(const std::string &command, const std::vector< uint8_t > &response) const
int i
std::vector< uint8_t > build_raw_command_data(const command_from_xml &command, const std::vector< std::string > &params) const
GLboolean * data
bool is_read_command
Definition: parser.hpp:169
void encode_raw_data_command(const command_from_xml &xml_cmd_info, const std::vector< parameter > &params, std::vector< uint8_t > &raw_data)
Definition: parser.hpp:678


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11