parser.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include <string>
5 #include <map>
6 #include <fstream>
7 #include <sstream>
8 #include <vector>
9 #include <functional>
10 #include <cstring>
11 
12 #ifdef ANDROID
13 #include "android_helpers.h"
14 #endif
15 
16 #include "../third-party/rapidxml/rapidxml.hpp"
17 
18 #define MAX_PARAMS 4
19 
20 struct section;
21 typedef std::function<void(const uint8_t*, const section&, std::stringstream&)> xml_parser_function;
22 
24 
25 #pragma pack(push, 1)
26 typedef struct
27 {
34 
35 typedef struct
36 {
39  uint8_t Version_spare[2];
41 
42 typedef struct
43 {
48 }HexByte;
49 
50 typedef struct
51 {
54  uint8_t Version_spare[2];
56 
57 typedef struct
58 {
60 }Bool;
61 
62 typedef struct
63 {
66 
67 typedef struct
68 {
71  uint8_t Version_spare[2];
73 
74 typedef struct
75 {
77 }Ascii;
78 
79 typedef struct
80 {
82 }DecByte;
83 
84 typedef struct
85 {
90 }HexNumber;
91 
92 typedef struct
93 {
99 
100 typedef struct
101 {
111 
112 typedef struct
113 {
114  double number;
115 }DoubleNumber;
116 #pragma pack(pop)
117 
118 struct parameter {
122 
124  bool in_is_decimal, bool in_is_reverse_bytes,
125  int in_format_length)
126  : name(in_name),
127  format_length(in_format_length),
128  is_decimal(in_is_decimal),
129  is_reverse_bytes(in_is_reverse_bytes),
130  data(in_data)
131  {}
133  : name(""),
134  format_length(2),
135  is_decimal(false),
136  is_reverse_bytes(false),
137  data("")
138  {}
139 };
140 
141 struct section {
143  std::string title = "";
144  std::string format_type = "";
146  int offset = 0;
147  int size = 0;
148 };
149 
150 struct data {
151  std::vector<section> sections;
152 };
153 
154 struct kvp {
156 };
157 
159  std::string name, key_size;
160  std::vector<kvp> kv;
161 };
162 
163 typedef struct command
164 {
166  unsigned int op_code;
168  bool is_write_only = false;
169  bool is_read_command = false;
171  int time_out = 20000;
172  int num_of_parameters = 0;
173  bool is_cmd_write_data = false;
179 
181  std::vector<parameter> parameters;
183 
184 // units of nibbles
186  Byte = 2,
187  Word = 4,
188  Double = 8
189 };
190 
192 {
193  std::map<std::string, command_from_xml> commands;
194  std::map<std::string, custom_formatter> custom_formatters;
195 };
196 
197 inline std::string hexify(unsigned char n)
198 {
200 
201  do
202  {
203  res += "0123456789ABCDEF"[n % 16];
204  n >>= 4;
205  } while (n);
206 
207  std::reverse(res.begin(), res.end());
208 
209  if (res.size() == 1)
210  {
211  res.insert(0, "0");
212  }
213 
214  return res;
215 }
216 
218 {
219  transform(x.begin(), x.end(), x.begin(), tolower);
220  return x;
221 }
222 
223 inline bool string_to_bool(const std::string& x)
224 {
225  return (to_lower(x) == "true");
226 }
227 
228 inline unsigned int string_to_hex(std::string str)
229 {
230  std::string delimiter = "x";
231 
232  auto pos = str.find(delimiter);
233  str.erase(0, pos + delimiter.length());
234 
235  std::stringstream ss;
236  unsigned int value;
237  ss << str;
238  ss >> std::hex >> value;
239 
240  return value;
241 }
242 
243 inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
244 {
245  auto doc = std::make_shared<rapidxml::xml_document<>>();
246  doc->parse<0>(doc->allocate_string(content));
247 
248  auto node = doc->first_node("Commands");
249  if (!node)
250  throw std::logic_error("Invalid XML file - expecting Commands node to be present");
251 
252  for (auto NodeI = node->first_node(); NodeI; NodeI = NodeI->next_sibling())
253  {
254  if (!strcmp(NodeI->name(), "Command"))
255  {
257  for (auto AttI = NodeI->first_attribute(); AttI; AttI = AttI->next_attribute())
258  {
259  std::string value = AttI->value();
260  std::string att_name = AttI->name();
261  if (att_name == "Name") { cmd.name = value; }
262  else if (att_name == "Opcode") { cmd.op_code = string_to_hex(value); }
263  else if (att_name == "IsWriteOnly") { cmd.is_write_only = string_to_bool(value); }
264  else if (att_name == "ReadFormat") { cmd.read_format = value; }
265  else if (att_name == "IsReadCommand") { cmd.is_read_command = string_to_bool(value); }
266  else if (att_name == "CmdPermission") { cmd.cmd_permission = value; }
267  else if (att_name == "TimeOut") { cmd.time_out = stoi(value); }
268  else if (att_name == "Description") { cmd.description = value; }
269  else if (att_name == "CmdInterface") { cmd.cmd_interface = value; }
270  else if (att_name == "CmdPipe") { cmd.cmd_pipe = value; }
271  else if (att_name == "I2cRegOffset") { cmd.i2c_reg_offset = value; }
272  else if (att_name == "I2cCmdType") { cmd.i2c_cmd_type = value; }
273  }
274 
275  for (auto SubNodeI = NodeI->first_node(); SubNodeI; SubNodeI = SubNodeI->next_sibling()) // Add commands
276  {
277  std::string str_name = SubNodeI->name();
278  if (str_name.find("Parameter") != std::string::npos)
279  {
281  ++cmd.num_of_parameters;
282  for (auto paramAtt = SubNodeI->first_attribute(); paramAtt; paramAtt = paramAtt->next_attribute())
283  {
284  std::string value = paramAtt->value();
285  std::string att_name = paramAtt->name();
286  if (att_name == "Name") { param.name = value; }
287  else if (att_name == "IsDecimal") { param.is_decimal = string_to_bool(value); }
288  }
289  cmd.parameters.push_back(param);
290  }
291  else if (str_name == "Data")
292  {
294  param.name = "Data";
295  cmd.is_cmd_write_data = true;
296  for (auto dataAtt = SubNodeI->first_attribute(); dataAtt; dataAtt = dataAtt->next_attribute())
297  {
298  std::string value = dataAtt->value();
299  std::string data_att_name = dataAtt->name();
300 
301  if (data_att_name == "Name") { param.name = value; }
302  else if (data_att_name == "IsReverseBytes") { param.is_reverse_bytes = string_to_bool(value); }
303  else if (data_att_name == "FormatLength") { param.format_length = stoi(value); }
304  }
305  cmd.parameters.push_back(param);
306  }
307  else if (str_name == "ReadData")
308  {
309  for (auto sectionNode = SubNodeI->first_node(); sectionNode; sectionNode = sectionNode->next_sibling()) // Add CMD Parameters
310  {
311  section sec;
312  for (auto sectionAtt = sectionNode->first_attribute(); sectionAtt; sectionAtt = sectionAtt->next_attribute())
313  {
314  std::string value = sectionAtt->value();
315  std::string section_att_name = sectionAtt->name();
316  if (section_att_name == "Name") { sec.name = value; }
317  else if (section_att_name == "Title") { sec.title = value; }
318  else if (section_att_name == "Offset") { sec.offset = stoi(value); }
319  else if (section_att_name == "Size") { sec.size = stoi(value); }
320  else if (section_att_name == "FormatType") { sec.format_type = value; }
321  }
322  cmd.read_data.sections.push_back(sec);
323  }
324  }
325  }
326  cmd_xml.commands.insert(std::pair<std::string, command_from_xml>(to_lower(cmd.name), cmd));
327  }
328  else if (!strcmp(NodeI->name(), "CustomFormatter"))
329  {
330  custom_formatter customFormatter;
331  for (auto AttI = NodeI->first_attribute(); AttI; AttI = AttI->next_attribute())
332  {
333  std::string value = AttI->value();
334  std::string atti_name = AttI->name();
335  if (atti_name == "KeySize") { customFormatter.key_size = value; }
336  else if (atti_name == "Name") { customFormatter.name = value; }
337  }
338 
339  for (auto SubNodeI = NodeI->first_node(); SubNodeI; SubNodeI = SubNodeI->next_sibling()) // Add Kvp
340  {
341  std::string sub_nodei_name = SubNodeI->name();
342  if (sub_nodei_name.find("KVP") != std::string::npos)
343  {
344  kvp kvp;
345  for (auto paramAtt = SubNodeI->first_attribute(); paramAtt; paramAtt = paramAtt->next_attribute())
346  {
347  std::string value = paramAtt->value();
348  std::string param_att_name = paramAtt->name();
349  if (param_att_name == "Key") { kvp.key = value; }
350  else if (param_att_name == "Value") { kvp.value = value; }
351  }
352  customFormatter.kv.push_back(kvp);
353  }
354 
355  }
356  cmd_xml.custom_formatters.insert(std::pair<std::string, custom_formatter>(to_lower(customFormatter.name), customFormatter));
357  }
358  }
359 }
360 
361 inline bool parse_xml_from_file(const std::string& xml_full_file_path, commands_xml& cmd_xml)
362 {
363  std::ifstream fin(xml_full_file_path);
364 
365  if (!fin)
366  return false;
367 
368  std::stringstream ss;
369  ss << fin.rdbuf();
370  auto xml = ss.str();
371 
372  parse_xml_from_memory(xml.c_str(), cmd_xml);
373 
374  return true;
375 }
376 
377 inline void check_section_size(unsigned section_size, unsigned struct_size, const std::string& section_name, const std::string& struct_name)
378 {
379  std::string exception_str("Size of section %s is bigger than %s struct.");
380  if (section_size > struct_size)
381  {
382  auto msg = "Size of section " + section_name + " is bigger than " + struct_name + " struct!";
383  throw std::runtime_error(msg);
384  }
385 }
386 
387 inline void update_format_type_to_lambda(std::map<std::string, xml_parser_function>& format_type_to_lambda)
388 {
389  format_type_to_lambda.insert(std::make_pair("ChangeSetVersion", [](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
390  check_section_size(sec.size, sizeof(ChangeSetVersion), sec.name.c_str(), "ChangeSetVersion");
391  auto changeSetVersion = reinterpret_cast<const ChangeSetVersion*>(data_offset + sec.offset);
392  tempStr << static_cast<int>(changeSetVersion->Version_Major) <<
393  ((sec.size >= 2) ? ("." + std::to_string(static_cast<int>(changeSetVersion->Version_Minor))) : "") <<
394  ((sec.size >= 3) ? ("." + std::to_string(static_cast<int>(changeSetVersion->Version_Number))) : "") <<
395  ((sec.size >= 4) ? ("." + std::to_string(static_cast<int>(changeSetVersion->Version_Revision))) : "") <<
396  ((sec.size >= 5) ? (" (" + std::to_string(changeSetVersion->Version_spare)) + ")" : "");
397  }));
398 
399  format_type_to_lambda.insert(std::make_pair("MajorMinorVersion", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
400  check_section_size(sec.size, sizeof(MajorMinorVersion), sec.name.c_str(), "MajorMinorVersion");
401  auto majorMinorVersion = reinterpret_cast<const MajorMinorVersion*>(data_offset + sec.offset);;
402  tempStr << static_cast<int>(majorMinorVersion->Version_Major) <<
403  ((sec.size >= 2) ? ("." + std::to_string(static_cast<int>(majorMinorVersion->Version_Minor))) : "");
404  }));
405 
406  format_type_to_lambda.insert(std::make_pair("HexByte", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
407  check_section_size(sec.size, sizeof(HexByte), sec.name.c_str(), "HexByte");
408  auto hexByte = reinterpret_cast<const HexByte*>(data_offset + sec.offset);
409  tempStr << hexify(hexByte->Version4);
410  }));
411 
412  format_type_to_lambda.insert(std::make_pair("LiguriaVersion", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
413  check_section_size(sec.size, sizeof(LiguriaVersion), sec.name.c_str(), "LiguriaVersion");
414  auto liguriaVersion = reinterpret_cast<const LiguriaVersion*>(data_offset + sec.offset);
415  tempStr << static_cast<int>(liguriaVersion->Version1) <<
416  ((sec.size >= 2) ? ("." + std::to_string(static_cast<int>(liguriaVersion->Version2))) : "");
417  }));
418 
419  format_type_to_lambda.insert(std::make_pair("Bool", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
420  check_section_size(sec.size, sizeof(Bool), sec.name.c_str(), "Bool");
421  auto BooL = reinterpret_cast<const Bool*>(data_offset + sec.offset);
422  tempStr << ((static_cast<int>(BooL->sts)) ? "TRUE" : "FALSE");
423  }));
424 
425  format_type_to_lambda.insert(std::make_pair("HwTypeNumber", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
426  check_section_size(sec.size, sizeof(HwTypeNumber), sec.name.c_str(), "HwTypeNumber");
427  auto hwTypeNumber = reinterpret_cast<const HwTypeNumber*>(data_offset + sec.offset);
428  tempStr << static_cast<int>(hwTypeNumber->num);
429  }));
430 
431  format_type_to_lambda.insert(std::make_pair("Ascii", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
432  check_section_size(sec.size, sizeof(Ascii), sec.name.c_str(), "Ascii");
433  auto ascii = reinterpret_cast<const Ascii*>(data_offset + sec.offset);
434  auto temp = new char[sec.size + 1];
435  memcpy(temp, ascii->value, sec.size);
436  temp[sec.size] = '\0';
437  tempStr << temp;
438  delete[] temp;
439  }));
440 
441  format_type_to_lambda.insert(std::make_pair("DecByte", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
442  check_section_size(sec.size, sizeof(DecByte), sec.name.c_str(), "DecByte");
443  auto decByte = reinterpret_cast<const DecByte*>(data_offset + sec.offset);
444  tempStr << static_cast<int>(decByte->version);
445  }));
446 
447  format_type_to_lambda.insert(std::make_pair("HexNumber", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
448  check_section_size(sec.size, sizeof(HexNumber), sec.name.c_str(), "HexNumber");
449  auto hexNumber = reinterpret_cast<const HexNumber*>(data_offset + sec.offset);
450  tempStr << hexify(hexNumber->number1) <<
451  ((sec.size >= 2) ? hexify(hexNumber->number2) : "") <<
452  ((sec.size >= 3) ? hexify(hexNumber->number3) : "") <<
453  ((sec.size >= 4) ? hexify(hexNumber->number4) : "");
454  }));
455 
456  format_type_to_lambda.insert(std::make_pair("HexNumberTwoBytes", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
457  check_section_size(sec.size, sizeof(HexNumber), sec.name.c_str(), "HexNumber");
458  auto hexNumber = reinterpret_cast<const HexNumber*>(data_offset + sec.offset);
459  tempStr << hexify(hexNumber->number2) <<
460  ((sec.size >= 2) ? hexify(hexNumber->number1) : "");
461  }));
462 
463  format_type_to_lambda.insert(std::make_pair("HexNumberReversed", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
464  check_section_size(sec.size, sizeof(HexNumberReversed), sec.name.c_str(), "HexNumberReversed");
465  auto hexNumberReversed = reinterpret_cast<const HexNumberReversed*>(data_offset + sec.offset);
466  tempStr << hexify(hexNumberReversed->number4) <<
467  ((sec.size >= 2) ? hexify(hexNumberReversed->number3) : "") <<
468  ((sec.size >= 3) ? hexify(hexNumberReversed->number2) : "") <<
469  ((sec.size >= 4) ? hexify(hexNumberReversed->number1) : "");
470  }));
471 
472  format_type_to_lambda.insert(std::make_pair("BarCodeSerial12Char", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
473  check_section_size(sec.size, sizeof(BarCodeSerial12Char), sec.name.c_str(), "BarCodeSerial12Char");
474  auto barCodeSerial12Char = reinterpret_cast<const BarCodeSerial12Char*>(data_offset + sec.offset);
475  tempStr << hexify(barCodeSerial12Char->number1) <<
476  ((sec.size >= 2) ? hexify(barCodeSerial12Char->number2) : "") <<
477  ((sec.size >= 3) ? hexify(barCodeSerial12Char->number3) : "") <<
478  ((sec.size >= 4) ? hexify(barCodeSerial12Char->number4) : "") <<
479  ((sec.size >= 5) ? hexify(barCodeSerial12Char->number5) : "") <<
480  ((sec.size >= 6) ? hexify(barCodeSerial12Char->number6) : "") <<
481  ((sec.size >= 7) ? hexify(barCodeSerial12Char->number7) : "") <<
482  ((sec.size >= 8) ? hexify(barCodeSerial12Char->number8) : "");
483  }));
484 
485  format_type_to_lambda.insert(std::make_pair("WideMajorMinorVersion", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
486  check_section_size(sec.size, sizeof(WideMajorMinorVersion), sec.name.c_str(), "WideMajorMinorVersion");
487  auto wideMajorMinorVersion = reinterpret_cast<const WideMajorMinorVersion*>(data_offset + sec.offset);
488  tempStr << static_cast<int>(wideMajorMinorVersion->Version_Minor) <<
489  ((sec.size >= 2) ? ("." + std::to_string(static_cast<int>(wideMajorMinorVersion->Version_Major))) : "");
490  }));
491 
492  format_type_to_lambda.insert(std::make_pair("Double", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
493  check_section_size(sec.size, sizeof(DoubleNumber), sec.name.c_str(), "DoubleNumber");
494  auto number = reinterpret_cast<const DoubleNumber*>(data_offset + sec.offset);
495  tempStr << std::setprecision(10) << number->number;
496  }));
497 }
498 
499 inline void update_sections_data(const uint8_t* data_offset, std::vector<section>& sections, const std::map<std::string, custom_formatter>& custom_formatters, const std::map<std::string, xml_parser_function>& format_type_to_lambda)
500 {
501  for (auto& sec : sections)
502  {
503  std::stringstream tempStr;
504  auto pair = format_type_to_lambda.find(sec.format_type);
505  if (pair != format_type_to_lambda.end())
506  {
507  pair->second(data_offset, sec, tempStr);
508  }
509  else
510  {
511  // FormatType not found. searching in the custom format list
512  auto it = custom_formatters.find(to_lower(sec.format_type));
513  if (it != custom_formatters.end())
514  {
515  if (it->second.key_size == "Byte")
516  {
517  auto key = uint8_t(*(data_offset + sec.offset));
518  for (auto& elem : it->second.kv)
519  {
520  auto keyStr = std::to_string(int(key));
521  if (elem.key == keyStr)
522  {
523  tempStr << elem.value;
524  break;
525  }
526  }
527  }
528  }
529  }
530 
531  std::vector<uint8_t> raw_data;
532  if (sec.size == 0)
533  throw std::runtime_error(std::string("Size of section " + sec.name + " is 0!").c_str());
534 
535  raw_data.resize(sec.size);
536  memcpy(raw_data.data(), data_offset + sec.offset, sec.size);
537  sec.data = tempStr.str();
538  }
539 }
540 
541 inline 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)
542 {
543  auto data_offset = raw_data_offset + 4;
544  data_size -= 4;
545  if (data_size == 0)
546  throw std::runtime_error("Raw data is empty!");
547 
548  std::stringstream ss_output;
549  ss_output.str("");
550  auto num_of_bytes_for_space = 1;
551  auto num_of_bytes_for_new_line = 16;
552  auto read_format = command.read_format;
553  if (read_format == "Bytes")
554  num_of_bytes_for_space = 1;
555  else if (read_format == "Words")
556  num_of_bytes_for_space = 2;
557  else if (read_format == "Doubles")
558  num_of_bytes_for_space = 4;
559 
560 
561  if (command.read_data.sections.empty())
562  {
563  std::vector<std::string> buffer;
564  auto bytes = 0;
565  auto bytes_per_line = 0;
566  auto start_address = "0";
567  std::string curr_address = start_address;
568  std::stringstream dec_to_hex;
569  auto end_address = std::stoi(start_address) + data_size;
570  auto num_of_zeros = 1;
571 
572  if (curr_address.size() == 1)
573  {
574  auto temp_curr_address = curr_address;
575  auto temp_end_address = end_address;
576  auto temp_bytes_per_line = bytes_per_line;
577 
578  for (size_t offset = 0; offset < data_size; ++offset)
579  {
580  ++temp_bytes_per_line;
581  if (temp_bytes_per_line == num_of_bytes_for_new_line)
582  {
583 
584  unsigned int next_add = stoi(temp_curr_address) + num_of_bytes_for_new_line;
585 
586  if (next_add >= temp_end_address)
587  break;
588 
589  dec_to_hex.str("");
590  dec_to_hex << std::hex << next_add;
591  temp_curr_address = dec_to_hex.str();
592 
593  if (temp_curr_address.size() == 1)
594  temp_curr_address.insert(0, "0");
595 
596  temp_curr_address = std::to_string(next_add);
597  temp_bytes_per_line = 0;
598  }
599  }
600 
601  num_of_zeros = int(dec_to_hex.str().length());
602  ss_output << "Offset: 0x";
603  dec_to_hex.str("");
604 
605  for (auto i = 1; i < num_of_zeros; ++i)
606  {
607  ss_output << "0";
608  }
609  }
610  else
611  ss_output << "Offset: 0x";
612 
613  ss_output << std::hex << stoi(curr_address) << " => ";
614 
615  for (size_t offset = 0; offset < data_size; ++offset)
616  {
617  ++bytes_per_line;
618  ++bytes;
619  buffer.push_back(hexify(static_cast<unsigned char>(*(data_offset + offset))));
620 
621  if (bytes == num_of_bytes_for_space)
622  {
623  bytes = 0;
624  reverse(buffer.begin(), buffer.end());
625 
626  for (auto str : buffer)
627  ss_output << str;
628 
629  buffer.clear();
630 
631  if (bytes_per_line == num_of_bytes_for_new_line)
632  {
633  unsigned int next_add = stoi(curr_address) + num_of_bytes_for_new_line;
634 
635  if (next_add >= end_address)
636  break;
637 
638  dec_to_hex.str("");
639  dec_to_hex << std::hex << next_add;
640  curr_address = dec_to_hex.str();
641 
642  auto putZeros = num_of_zeros - int(curr_address.size());
643  ss_output << "\nOffset: 0x";
644 
645  for (auto i = 0; i < putZeros; ++i)
646  {
647  ss_output << "0";
648  }
649 
650  ss_output << curr_address << " => ";
651  curr_address = std::to_string(next_add);
652  bytes_per_line = 0;
653  }
654  else
655  {
656  if ((offset + 1) < data_size)
657  ss_output << " ";
658  }
659  }
660  }
661  }
662  else
663  {
664  auto sections = command.read_data.sections;
665  update_sections_data(data_offset, sections, custom_formatters, format_type_to_lambda);
666  unsigned max_line_len = 0;
667  for (auto& elem : sections)
668  max_line_len = ((elem.name.size() > max_line_len) ? unsigned(elem.name.size()) : max_line_len);
669 
670  const int spaces = 3; // number of spaces between section name to section data
671  for (auto& elem : sections)
672  ss_output << elem.name << ":" << std::setw((max_line_len + spaces) - elem.name.size() + elem.data.length()) << std::right << elem.data << std::endl;
673  }
674 
675  output = ss_output.str();
676 }
677 
678 inline void encode_raw_data_command(const command_from_xml& xml_cmd_info, const std::vector<parameter>& params, std::vector<uint8_t>& raw_data)
679 {
680  auto cmd_op_code = xml_cmd_info.op_code;
681  auto is_cmd_writes_data = xml_cmd_info.is_cmd_write_data;
682  auto parameters = params;
683 
684  auto num_of_required_parameters = xml_cmd_info.num_of_parameters + ((xml_cmd_info.is_cmd_write_data) ? 1 : 0);
685  if (int(params.size()) < num_of_required_parameters)
686  throw std::runtime_error("Number of given parameters is less than required!"); // TODO: consider to print the command info
687 
688  auto format_length = Byte;
689  if (is_cmd_writes_data)
690  format_length = ((params[xml_cmd_info.num_of_parameters].format_length == -1) ? format_length : static_cast<FormatSize>(params[xml_cmd_info.num_of_parameters].format_length));
691 
692  const uint16_t pre_header_data = 0xcdab; // magic number
693  raw_data.resize(HW_MONITOR_BUFFER_SIZE); // TODO: resize std::vector to the right size
694  auto write_ptr = raw_data.data();
695  auto header_size = 4;
696 
697 
698  auto cur_index = 2;
699  *reinterpret_cast<uint16_t *>(write_ptr + cur_index) = pre_header_data;
700  cur_index += sizeof(uint16_t);
701  *reinterpret_cast<unsigned int *>(write_ptr + cur_index) = cmd_op_code;
702  cur_index += sizeof(unsigned int);
703 
704  // Parameters
705  for (auto param_index = 0; param_index < MAX_PARAMS; ++param_index)
706  {
707  if (param_index < xml_cmd_info.num_of_parameters)
708  {
709  unsigned decimal;
710  std::stringstream ss;
711  ss << params[param_index].data;
712  ss >> std::hex >> decimal;
713  *reinterpret_cast<unsigned*>(write_ptr + cur_index) = decimal;
714  cur_index += sizeof(unsigned);
715  }
716  else
717  {
718  *reinterpret_cast<unsigned *>(write_ptr + cur_index) = 0;
719  cur_index += sizeof(unsigned);
720  }
721  }
722 
723  // Data
724  if (is_cmd_writes_data)
725  {
726  for (auto j = xml_cmd_info.num_of_parameters; j < int(params.size()); ++j)
727  {
728  unsigned decimal;
729  std::stringstream ss;
730  ss << params[j].data;
731  ss >> std::hex >> decimal;
732  switch (format_length)
733  {
734  case Byte:
735  *reinterpret_cast<uint8_t*>(write_ptr + cur_index) = static_cast<uint8_t>(decimal);
736  cur_index += sizeof(uint8_t);
737  break;
738 
739  case Word:
740  *reinterpret_cast<short *>(write_ptr + cur_index) = static_cast<short>(decimal);
741  cur_index += sizeof(short);
742  break;
743 
744  case Double:
745  *reinterpret_cast<unsigned *>(write_ptr + cur_index) = static_cast<unsigned>(decimal);
746  cur_index += sizeof(unsigned);
747  break;
748 
749  default:
750  *reinterpret_cast<uint8_t*>(write_ptr + cur_index) = static_cast<uint8_t>(decimal);
751  cur_index += sizeof(uint8_t);
752  break;
753  }
754  }
755  }
756 
757  *reinterpret_cast<uint16_t*>(raw_data.data()) = static_cast<uint16_t>(cur_index - header_size);// Length doesn't include hdr.
758  raw_data.resize(cur_index); // TODO:
759 }
760 
uint8_t Version2
Definition: parser.hpp:45
uint8_t number3
Definition: parser.hpp:96
std::string to_lower(std::string x)
Definition: parser.hpp:217
GLuint const GLchar * name
parameter()
Definition: parser.hpp:132
uint8_t Version1
Definition: parser.hpp:44
bool parse_xml_from_file(const std::string &xml_full_file_path, commands_xml &cmd_xml)
Definition: parser.hpp:361
const uint16_t HW_MONITOR_BUFFER_SIZE
Definition: parser.hpp:23
unsigned int string_to_hex(std::string str)
Definition: parser.hpp:228
std::string cmd_interface
Definition: parser.hpp:175
uint8_t sts
Definition: parser.hpp:59
int time_out
Definition: parser.hpp:171
std::string hexify(unsigned char n)
Definition: parser.hpp:197
Definition: parser.hpp:74
parameter(std::string in_name, std::string in_data, bool in_is_decimal, bool in_is_reverse_bytes, int in_format_length)
Definition: parser.hpp:123
std::string format_type
Definition: parser.hpp:144
GLfloat value
struct command command_from_xml
std::string data
Definition: parser.hpp:119
bool is_cmd_write_data
Definition: parser.hpp:173
void update_sections_data(const uint8_t *data_offset, std::vector< section > &sections, const std::map< std::string, custom_formatter > &custom_formatters, const std::map< std::string, xml_parser_function > &format_type_to_lambda)
Definition: parser.hpp:499
uint8_t Version_Number
Definition: parser.hpp:29
unsigned Bool
Definition: sqlite3.c:17453
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
uint8_t Version2
Definition: parser.hpp:52
std::string cmd_permission
Definition: parser.hpp:174
#define MAX_PARAMS
Definition: parser.hpp:18
unsigned short uint16_t
Definition: stdint.h:79
unsigned int op_code
Definition: parser.hpp:166
GLsizei const GLchar *const * string
Definition: parser.hpp:57
std::vector< section > sections
Definition: parser.hpp:151
uint8_t number1
Definition: parser.hpp:94
bool is_reverse_bytes
Definition: parser.hpp:120
GLdouble n
Definition: glext.h:1966
unsigned char uint8_t
Definition: stdint.h:78
GLenum GLfloat * buffer
std::vector< kvp > kv
Definition: parser.hpp:160
uint8_t number2
Definition: parser.hpp:87
GLuint64 key
Definition: glext.h:8966
Definition: parser.hpp:154
std::string i2c_cmd_type
Definition: parser.hpp:178
bool is_decimal
Definition: parser.hpp:120
double number
Definition: parser.hpp:114
bool is_write_only
Definition: parser.hpp:168
GLsizeiptr size
void parse_xml_from_memory(const char *content, commands_xml &cmd_xml)
Definition: parser.hpp:243
Definition: parser.hpp:186
std::vector< parameter > parameters
Definition: parser.hpp:181
Definition: parser.hpp:187
GLdouble x
std::string description
Definition: parser.hpp:170
uint8_t Version_Major
Definition: parser.hpp:31
std::string read_format
Definition: parser.hpp:167
data read_data
Definition: parser.hpp:180
void check_section_size(unsigned section_size, unsigned struct_size, const std::string &section_name, const std::string &struct_name)
Definition: parser.hpp:377
uint8_t Version4
Definition: parser.hpp:47
void update_format_type_to_lambda(std::map< std::string, xml_parser_function > &format_type_to_lambda)
Definition: parser.hpp:387
std::string value
Definition: parser.hpp:155
GLint j
GLenum const GLfloat * params
std::map< std::string, command_from_xml > commands
Definition: parser.hpp:193
uint8_t Version1
Definition: parser.hpp:53
std::string key_size
Definition: parser.hpp:159
uint8_t Version_Minor
Definition: parser.hpp:30
std::string cmd_pipe
Definition: parser.hpp:176
std::string title
Definition: parser.hpp:143
uint8_t number2
Definition: parser.hpp:95
FormatSize
Definition: parser.hpp:185
std::function< void(const uint8_t *, const section &, std::stringstream &)> xml_parser_function
Definition: parser.hpp:20
uint8_t Version_Revision
Definition: parser.hpp:28
std::string name
Definition: parser.hpp:165
uint8_t Version_Major
Definition: parser.hpp:70
int num_of_parameters
Definition: parser.hpp:172
uint8_t number4
Definition: parser.hpp:97
GLdouble right
static auto it
uint8_t number1
Definition: parser.hpp:86
GLenum GLfloat param
std::string key
Definition: parser.hpp:155
std::string name
Definition: parser.hpp:142
uint8_t version
Definition: parser.hpp:81
uint8_t Version3
Definition: parser.hpp:46
float rs2_vector::* pos
int stoi(const std::string &value)
int size
Definition: parser.hpp:147
int i
GLuint res
Definition: glext.h:8856
std::string name
Definition: parser.hpp:159
GLuint GLenum GLenum transform
Definition: glext.h:11553
bool string_to_bool(const std::string &x)
Definition: parser.hpp:223
int offset
Definition: parser.hpp:146
int format_length
Definition: parser.hpp:121
uint8_t number3
Definition: parser.hpp:88
uint8_t num
Definition: parser.hpp:64
uint8_t Version_Major
Definition: parser.hpp:38
std::map< std::string, custom_formatter > custom_formatters
Definition: parser.hpp:194
uint8_t Version_Minor
Definition: parser.hpp:37
std::string i2c_reg_offset
Definition: parser.hpp:177
std::string name
Definition: parser.hpp:119
uint8_t number4
Definition: parser.hpp:89
Definition: parser.hpp:150
bool is_read_command
Definition: parser.hpp:169
uint8_t Version_Minor
Definition: parser.hpp:69
GLintptr offset
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
std::string to_string(T value)


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