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
static constexpr OperationResult convertToOperationResult(const uint32_t &value)
static constexpr Type convertToReplyType(const uint32_t &value)
void write(std::ostringstream &os, const T &data)
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
Exception thrown if an incorrect CRC is detected during deserialization of a data_conversion_layer::s...
Higher level data type representing a reply message from the scanner.


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Nov 5 2022 02:13:36