pcapng_json_parser.cpp
Go to the documentation of this file.
1 /*
2  * @brief pcapng_json_parser parses jsonfiles converted from pcapng-files by pcap_json_converter.
3  *
4  * Copyright (C) 2021 Ing.-Buero Dr. Michael Lehning, Hildesheim
5  * Copyright (C) 2021 SICK AG, Waldkirch
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *
24  * * Redistributions of source code must retain the above copyright
25  * notice, this list of conditions and the following disclaimer.
26  * * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  * * Neither the name of SICK AG nor the names of its
30  * contributors may be used to endorse or promote products derived from
31  * this software without specific prior written permission
32  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
33  * contributors may be used to endorse or promote products derived from
34  * this software without specific prior written permission
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
37  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
40  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * Authors:
49  * Michael Lehning <michael.lehning@lehning.de>
50  *
51  * Copyright 2021 SICK AG
52  * Copyright 2021 Ing.-Buero Dr. Michael Lehning
53  *
54  */
55 #include "sick_scan/ros_wrapper.h"
56 #include <fstream>
57 #include <sstream>
58 #include <string.h>
59 #include <jsoncpp/json/json.h>
60 
62 #include "sick_scan/utils.h"
63 
69 std::vector<std::string> sick_scan::PcapngJsonParser::split(const std::string & s, char delimiter)
70 {
71  std::vector<std::string> substrings;
72  std::string part;
73  std::istringstream ss(s);
74  while (std::getline(ss, part, delimiter))
75  substrings.push_back(part);
76  return substrings;
77 }
78 
87 bool sick_scan::PcapngJsonParser::parseJsonfile(const std::string & json_filename, const std::vector<std::string> & scandatatypes, double start_time, std::vector<sick_scan::JsonScanData> & scandata)
88 {
89  try
90  {
91  // convert scandatatypes to hex string
92  std::vector<std::string> scandatatypes_hex;
93  scandatatypes_hex.reserve(scandatatypes.size());
94  for(int type_cnt = 0; type_cnt < scandatatypes.size(); type_cnt++)
95  {
96  const std::string & scandatatype = scandatatypes[type_cnt];
97  std::stringstream hex_stream;
98  for(int char_cnt = 0; char_cnt < scandatatype.size(); char_cnt++)
99  {
100  if(char_cnt > 0)
101  hex_stream << ":";
102  hex_stream << std::setfill('0') << std::setw(2) << std::hex << (int)(scandatatype[char_cnt] & 0xFF);
103  }
104  scandatatypes_hex.push_back(hex_stream.str());
105  }
106  // Read and parse json file
107  std::ifstream json_file(json_filename);
108  if(!json_file.is_open())
109  {
110  ROS_WARN_STREAM("## WARNING sick_scan::PcapngJsonParser::parseJsonfile: error reading file \"" << json_filename << "\".");
111  return false;
112  }
113  ROS_INFO_STREAM("sick_scan::PcapngJsonParser: parsing file \"" << json_filename << "\"...");
114  Json::Reader json_reader;
115  Json::Value json_root;
116  json_reader.parse(json_file, json_root);
117  int msg_cnt = 0;
118  // Loop over all json objects "tcp"
119  for (Json::Value::ArrayIndex json_idx = 0; json_idx != json_root.size(); json_idx++)
120  {
121  if(json_root[json_idx].isMember("_source") && json_root[json_idx]["_source"].isMember("layers") && json_root[json_idx]["_source"]["layers"].isMember("tcp"))
122  {
123  Json::Value & json_tcp = json_root[json_idx]["_source"]["layers"]["tcp"];
124  std::string tcp_timestamp, tcp_payload;
125  if(json_tcp.isMember("Timestamps") && json_tcp["Timestamps"].isMember("tcp.time_relative"))
126  tcp_timestamp = json_tcp["Timestamps"]["tcp.time_relative"].asString();
127  if(json_tcp.isMember("tcp.payload"))
128  tcp_payload = json_tcp["tcp.payload"].asString();
129  // Check payload against scandatatypes
130  bool type_check_passed = scandatatypes.empty(); // empty scandatatypes means all types
131  for(int type_cnt = 0; type_check_passed == false && type_cnt < scandatatypes_hex.size(); type_cnt++)
132  {
133  if(tcp_payload.find(scandatatypes_hex[type_cnt]) != std::string::npos)
134  type_check_passed = true;
135  }
136  if(!type_check_passed)
137  continue; // different scandatatype
138  if(tcp_timestamp.size() > 0 && tcp_payload.size() > 4)
139  {
140  // Parse timestamp and payload
141  double msg_timestamp = std::stod(tcp_timestamp) + start_time;
142  std::vector<uint8_t> msg_payload;
143  msg_payload.reserve(tcp_payload.size()/3 + 1);
144  std::string hexval;
145  std::istringstream payload_stream(tcp_payload);
146  while (std::getline(payload_stream, hexval, ':'))
147  {
148  msg_payload.push_back(std::stoul(hexval, 0, 16) & 0xFF);
149  }
150  scandata.push_back(sick_scan::JsonScanData(msg_timestamp, msg_payload));
151  // std::cout << "msg_timestamp=" << msg_timestamp << ", msg_payload=" << sick_scan::Utils::toHexString(msg_payload) << std::endl;
152  msg_cnt++;
153  }
154  }
155  }
156  ROS_INFO_STREAM("sick_scan::PcapngJsonParser: " << msg_cnt << " messages in file \"" << json_filename << "\" successfully parsed.");
157  return true;
158  }
159  catch(const std::exception& e)
160  {
161  ROS_WARN_STREAM("## WARNING sick_scan::PcapngJsonParser::parseJsonfile: exception \"" << e.what() << "\" in file \"" << json_filename << "\".");
162  std::cerr << e.what() << '\n';
163  }
164  return false;
165 }
class JsonScanData: utility container for binary scandata incl. timestamp
static bool parseJsonfile(const std::string &json_filename, const std::vector< std::string > &scandatatypes, double start_time, std::vector< sick_scan::JsonScanData > &scandata)
Parses a jsonfile and returns a list of binary scandata messages of given type.
#define ROS_WARN_STREAM(args)
#define ROS_INFO_STREAM(args)
static std::vector< std::string > split(const std::string &s, char delimiter)
Splits a comma separated string into its parts.
static sick_scan::SickScanCommonTcp * s


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed Sep 7 2022 02:25:06