scanner_reply_serialization_deserialization.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <boost/crc.hpp>
21 #include <sstream>
22 
24 
26 {
27 namespace data_conversion_layer
28 {
29 scanner_reply::CRCMismatch::CRCMismatch(const std::string& msg) : std::runtime_error(msg)
30 {
31 }
32 
33 RawData data_conversion_layer::scanner_reply::serialize(const uint32_t op_code, const uint32_t res_code)
34 {
35  std::ostringstream os;
36 
37  const uint32_t reserved{ 0 };
38 
39  boost::crc_32_type crc;
40  crc.process_bytes(&reserved, sizeof(reserved));
41  crc.process_bytes(&op_code, sizeof(op_code));
42  crc.process_bytes(&res_code, sizeof(res_code));
43 
44  raw_processing::write(os, static_cast<uint32_t>(crc.checksum()));
45  raw_processing::write(os, reserved);
46  raw_processing::write(os, op_code);
47  raw_processing::write(os, res_code);
48 
49  const std::string data_str(os.str());
50  assert(data_str.length() == data_conversion_layer::scanner_reply::Message::SIZE &&
51  "Message data of start reply has not the expected size");
52  return data_conversion_layer::RawData(data_str.cbegin(), data_str.cend());
53 }
54 
56 {
57  return serialize(static_cast<uint32_t>(reply.type()), static_cast<uint32_t>(reply.result()));
58 }
59 
61 {
62  std::istringstream is(std::string(data.data(), Message::SIZE));
63 
64  uint32_t crc;
65  uint32_t reserved;
66  uint32_t opcode;
67  uint32_t res_code;
68 
69  raw_processing::read(is, crc);
70  raw_processing::read(is, reserved);
71  raw_processing::read(is, opcode);
72  raw_processing::read(is, res_code);
73 
74  boost::crc_32_type crc_checked;
75  crc_checked.process_bytes(&reserved, sizeof(reserved));
76  crc_checked.process_bytes(&opcode, sizeof(opcode));
77  crc_checked.process_bytes(&res_code, sizeof(res_code));
78 
79  if (crc != crc_checked.checksum())
80  {
82  }
83 
85 }
86 
87 } // namespace data_conversion_layer
88 } // namespace psen_scan_v2_standalone
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message::result
constexpr OperationResult result() const
Definition: scanner_reply_msg.h:99
psen_scan_v2_standalone::data_conversion_layer::raw_processing::write
void write(std::ostringstream &os, const T &data)
Definition: raw_processing.h:47
scanner_reply_serialization_deserialization.h
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message::type
constexpr Type type() const
Definition: scanner_reply_msg.h:94
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::CRCMismatch
Exception thrown if an incorrect CRC is detected during deserialization of a data_conversion_layer::s...
Definition: scanner_reply_serialization_deserialization.h:36
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message
Higher level data type representing a reply message from the scanner.
Definition: scanner_reply_msg.h:33
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message::convertToReplyType
static constexpr Type convertToReplyType(const uint32_t &value)
Definition: scanner_reply_msg.h:70
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::serialize
RawData serialize(const Message &reply)
Definition: scanner_reply_serialization_deserialization.cpp:55
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::deserialize
Message deserialize(const data_conversion_layer::RawData &data)
Definition: scanner_reply_serialization_deserialization.cpp:60
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message::convertToOperationResult
static constexpr OperationResult convertToOperationResult(const uint32_t &value)
Definition: scanner_reply_msg.h:80
raw_processing.h
std
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
psen_scan_v2_standalone::data_conversion_layer::raw_processing::read
void read(std::istream &is, T &data)
Definition: raw_processing.h:53
psen_scan_v2_standalone::data_conversion_layer::RawData
std::vector< char > RawData
Definition: raw_scanner_data.h:25
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::Message::SIZE
static constexpr std::size_t SIZE
Definition: scanner_reply_msg.h:53
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::CRCMismatch::CRCMismatch
CRCMismatch(const std::string &msg="CRC did not match!")
Definition: scanner_reply_serialization_deserialization.cpp:29


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:12