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 <iomanip>
60 #ifdef _MSC_VER
61 #include <json/json.h>
62 #else
63 #include <jsoncpp/json/json.h>
64 #endif
65 
67 #include "sick_scan/utils.h"
68 
74 std::vector<std::string> sick_scan_xd::PcapngJsonParser::split(const std::string & s, char delimiter)
75 {
76  std::vector<std::string> substrings;
77  std::string part;
78  std::istringstream ss(s);
79  while (std::getline(ss, part, delimiter))
80  substrings.push_back(part);
81  return substrings;
82 }
83 
92 bool sick_scan_xd::PcapngJsonParser::parseJsonfile(const std::string & json_filename, const std::vector<std::string> & scandatatypes, double start_time, std::vector<sick_scan_xd::JsonScanData> & scandata)
93 {
94  try
95  {
96  // convert scandatatypes to hex string
97  std::vector<std::string> scandatatypes_hex;
98  scandatatypes_hex.reserve(scandatatypes.size());
99  for(int type_cnt = 0; type_cnt < scandatatypes.size(); type_cnt++)
100  {
101  const std::string & scandatatype = scandatatypes[type_cnt];
102  std::stringstream hex_stream;
103  for(int char_cnt = 0; char_cnt < scandatatype.size(); char_cnt++)
104  {
105  if(char_cnt > 0)
106  hex_stream << ":";
107  hex_stream << std::setfill('0') << std::setw(2) << std::hex << (int)(scandatatype[char_cnt] & 0xFF);
108  }
109  scandatatypes_hex.push_back(hex_stream.str());
110  }
111  // Read and parse json file
112  std::ifstream json_file(json_filename);
113  if(!json_file.is_open())
114  {
115  ROS_WARN_STREAM("## WARNING sick_scan_xd::PcapngJsonParser::parseJsonfile: error reading file \"" << json_filename << "\".");
116  return false;
117  }
118  ROS_INFO_STREAM("sick_scan_xd::PcapngJsonParser: parsing file \"" << json_filename << "\"...");
119  Json::Reader json_reader;
120  Json::Value json_root;
121  json_reader.parse(json_file, json_root);
122  int msg_cnt = 0;
123  // Loop over all json objects "tcp"
124  for (Json::Value::ArrayIndex json_idx = 0; json_idx != json_root.size(); json_idx++)
125  {
126  if(json_root[json_idx].isMember("_source") && json_root[json_idx]["_source"].isMember("layers") && json_root[json_idx]["_source"]["layers"].isMember("tcp"))
127  {
128  Json::Value & json_tcp = json_root[json_idx]["_source"]["layers"]["tcp"];
129  std::string tcp_timestamp, tcp_payload;
130  if(json_tcp.isMember("Timestamps") && json_tcp["Timestamps"].isMember("tcp.time_relative"))
131  tcp_timestamp = json_tcp["Timestamps"]["tcp.time_relative"].asString();
132  if(json_tcp.isMember("tcp.payload"))
133  tcp_payload = json_tcp["tcp.payload"].asString();
134  // Check payload against scandatatypes
135  bool type_check_passed = scandatatypes.empty(); // empty scandatatypes means all types
136  for(int type_cnt = 0; type_check_passed == false && type_cnt < scandatatypes_hex.size(); type_cnt++)
137  {
138  if(tcp_payload.find(scandatatypes_hex[type_cnt]) != std::string::npos)
139  type_check_passed = true;
140  }
141  if(!type_check_passed)
142  continue; // different scandatatype
143  if(tcp_timestamp.size() > 0 && tcp_payload.size() > 4)
144  {
145  // Parse timestamp and payload
146  double msg_timestamp = std::stod(tcp_timestamp) + start_time;
147  std::vector<uint8_t> msg_payload;
148  msg_payload.reserve(tcp_payload.size()/3 + 1);
149  std::string hexval;
150  std::istringstream payload_stream(tcp_payload);
151  while (std::getline(payload_stream, hexval, ':'))
152  {
153  msg_payload.push_back(std::stoul(hexval, 0, 16) & 0xFF);
154  }
155  scandata.push_back(sick_scan_xd::JsonScanData(msg_timestamp, msg_payload));
156  // std::cout << "msg_timestamp=" << msg_timestamp << ", msg_payload=" << sick_scan_xd::Utils::toHexString(msg_payload) << std::endl;
157  msg_cnt++;
158  }
159  }
160  }
161  ROS_INFO_STREAM("sick_scan_xd::PcapngJsonParser: " << msg_cnt << " messages in file \"" << json_filename << "\" successfully parsed.");
162  return true;
163  }
164  catch(const std::exception& e)
165  {
166  ROS_WARN_STREAM("## WARNING sick_scan_xd::PcapngJsonParser::parseJsonfile: exception \"" << e.what() << "\" in file \"" << json_filename << "\".");
167  std::cerr << e.what() << '\n';
168  }
169  return false;
170 }
s
XmlRpcServer s
utils.h
pcap_json_converter.json_filename
string json_filename
Definition: pcap_json_converter.py:124
sick_scan_xd::PcapngJsonParser::split
static std::vector< std::string > split(const std::string &s, char delimiter)
Splits a comma separated string into its parts.
Definition: pcapng_json_parser.cpp:74
sick_scan_xd::PcapngJsonParser::parseJsonfile
static bool parseJsonfile(const std::string &json_filename, const std::vector< std::string > &scandatatypes, double start_time, std::vector< sick_scan_xd::JsonScanData > &scandata)
Parses a jsonfile and returns a list of binary scandata messages of given type.
Definition: pcapng_json_parser.cpp:92
sopas_json_test_server.json_file
string json_file
Definition: sopas_json_test_server.py:164
sopas_json_test_server.tcp_payload
tcp_payload
Definition: sopas_json_test_server.py:203
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
Definition: sick_scan_logging.h:123
ROS_INFO_STREAM
#define ROS_INFO_STREAM(...)
Definition: sick_scan_ros2_example.cpp:71
ros_wrapper.h
pcapng_json_parser.h
sick_scan_xd::JsonScanData
class JsonScanData: utility container for binary scandata incl. timestamp
Definition: pcapng_json_parser.h:71


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:09