pyxcp 0.23.8__cp313-cp313-macosx_11_0_arm64.whl → 0.25.7__cp313-cp313-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. pyxcp/__init__.py +1 -1
  2. pyxcp/cmdline.py +14 -29
  3. pyxcp/config/__init__.py +1257 -1258
  4. pyxcp/cpp_ext/aligned_buffer.hpp +168 -0
  5. pyxcp/cpp_ext/bin.hpp +7 -6
  6. pyxcp/cpp_ext/cpp_ext.cpython-310-darwin.so +0 -0
  7. pyxcp/cpp_ext/cpp_ext.cpython-311-darwin.so +0 -0
  8. pyxcp/cpp_ext/cpp_ext.cpython-312-darwin.so +0 -0
  9. pyxcp/cpp_ext/cpp_ext.cpython-313-darwin.so +0 -0
  10. pyxcp/cpp_ext/daqlist.hpp +241 -73
  11. pyxcp/cpp_ext/extension_wrapper.cpp +123 -15
  12. pyxcp/cpp_ext/framing.hpp +360 -0
  13. pyxcp/cpp_ext/helper.hpp +280 -280
  14. pyxcp/cpp_ext/mcobject.hpp +248 -246
  15. pyxcp/cpp_ext/sxi_framing.hpp +332 -0
  16. pyxcp/daq_stim/__init__.py +145 -67
  17. pyxcp/daq_stim/optimize/binpacking.py +2 -2
  18. pyxcp/daq_stim/scheduler.cpp +8 -8
  19. pyxcp/errormatrix.py +2 -2
  20. pyxcp/examples/run_daq.py +5 -4
  21. pyxcp/examples/xcp_policy.py +6 -6
  22. pyxcp/examples/xcp_read_benchmark.py +2 -2
  23. pyxcp/examples/xcp_skel.py +1 -2
  24. pyxcp/examples/xcp_unlock.py +10 -12
  25. pyxcp/examples/xcp_user_supplied_driver.py +1 -2
  26. pyxcp/examples/xcphello.py +2 -15
  27. pyxcp/examples/xcphello_recorder.py +2 -2
  28. pyxcp/master/__init__.py +1 -0
  29. pyxcp/master/errorhandler.py +134 -4
  30. pyxcp/master/master.py +823 -252
  31. pyxcp/recorder/.idea/.gitignore +8 -0
  32. pyxcp/recorder/.idea/misc.xml +4 -0
  33. pyxcp/recorder/.idea/modules.xml +8 -0
  34. pyxcp/recorder/.idea/recorder.iml +6 -0
  35. pyxcp/recorder/.idea/sonarlint/issuestore/3/8/3808afc69ac1edb9d760000a2f137335b1b99728 +7 -0
  36. pyxcp/recorder/.idea/sonarlint/issuestore/9/a/9a2aa4db38d3115ed60da621e012c0efc0172aae +0 -0
  37. pyxcp/recorder/.idea/sonarlint/issuestore/b/4/b49006702b459496a8e8c94ebe60947108361b91 +0 -0
  38. pyxcp/recorder/.idea/sonarlint/issuestore/index.pb +7 -0
  39. pyxcp/recorder/.idea/sonarlint/securityhotspotstore/3/8/3808afc69ac1edb9d760000a2f137335b1b99728 +0 -0
  40. pyxcp/recorder/.idea/sonarlint/securityhotspotstore/9/a/9a2aa4db38d3115ed60da621e012c0efc0172aae +0 -0
  41. pyxcp/recorder/.idea/sonarlint/securityhotspotstore/b/4/b49006702b459496a8e8c94ebe60947108361b91 +0 -0
  42. pyxcp/recorder/.idea/sonarlint/securityhotspotstore/index.pb +7 -0
  43. pyxcp/recorder/.idea/vcs.xml +10 -0
  44. pyxcp/recorder/__init__.py +96 -98
  45. pyxcp/recorder/converter/__init__.py +4 -10
  46. pyxcp/recorder/reader.hpp +138 -139
  47. pyxcp/recorder/reco.py +1 -0
  48. pyxcp/recorder/rekorder.cpython-310-darwin.so +0 -0
  49. pyxcp/recorder/rekorder.cpython-311-darwin.so +0 -0
  50. pyxcp/recorder/rekorder.cpython-312-darwin.so +0 -0
  51. pyxcp/recorder/rekorder.cpython-313-darwin.so +0 -0
  52. pyxcp/recorder/rekorder.hpp +274 -274
  53. pyxcp/recorder/unfolder.hpp +1354 -1319
  54. pyxcp/recorder/wrap.cpp +184 -183
  55. pyxcp/recorder/writer.hpp +302 -302
  56. pyxcp/scripts/xcp_daq_recorder.py +54 -0
  57. pyxcp/scripts/xcp_fetch_a2l.py +2 -2
  58. pyxcp/scripts/xcp_id_scanner.py +1 -2
  59. pyxcp/scripts/xcp_info.py +66 -51
  60. pyxcp/scripts/xcp_profile.py +1 -2
  61. pyxcp/tests/test_daq.py +1 -1
  62. pyxcp/tests/test_framing.py +262 -0
  63. pyxcp/tests/test_master.py +210 -100
  64. pyxcp/tests/test_transport.py +138 -42
  65. pyxcp/timing.py +1 -1
  66. pyxcp/transport/__init__.py +8 -5
  67. pyxcp/transport/base.py +70 -180
  68. pyxcp/transport/can.py +58 -7
  69. pyxcp/transport/eth.py +32 -15
  70. pyxcp/transport/hdf5_policy.py +167 -0
  71. pyxcp/transport/sxi.py +126 -52
  72. pyxcp/transport/transport_ext.cpython-310-darwin.so +0 -0
  73. pyxcp/transport/transport_ext.cpython-311-darwin.so +0 -0
  74. pyxcp/transport/transport_ext.cpython-312-darwin.so +0 -0
  75. pyxcp/transport/transport_ext.cpython-313-darwin.so +0 -0
  76. pyxcp/transport/transport_ext.hpp +214 -0
  77. pyxcp/transport/transport_wrapper.cpp +249 -0
  78. pyxcp/transport/usb_transport.py +47 -31
  79. pyxcp/types.py +0 -13
  80. pyxcp/{utils.py → utils/__init__.py} +1 -2
  81. pyxcp/utils/cli.py +78 -0
  82. {pyxcp-0.23.8.dist-info → pyxcp-0.25.7.dist-info}/METADATA +4 -2
  83. pyxcp-0.25.7.dist-info/RECORD +158 -0
  84. {pyxcp-0.23.8.dist-info → pyxcp-0.25.7.dist-info}/WHEEL +1 -1
  85. pyxcp/examples/conf_sxi.json +0 -9
  86. pyxcp/examples/conf_sxi.toml +0 -7
  87. pyxcp-0.23.8.dist-info/RECORD +0 -135
  88. {pyxcp-0.23.8.dist-info → pyxcp-0.25.7.dist-info}/entry_points.txt +0 -0
  89. {pyxcp-0.23.8.dist-info → pyxcp-0.25.7.dist-info/licenses}/LICENSE +0 -0
@@ -4,9 +4,11 @@
4
4
  #include <pybind11/numpy.h>
5
5
  #include <pybind11/pybind11.h>
6
6
  #include <pybind11/stl.h>
7
+ #include <memory>
7
8
 
8
9
  #include <cstdint>
9
10
 
11
+ #include "aligned_buffer.hpp"
10
12
  #include "bin.hpp"
11
13
  #include "daqlist.hpp"
12
14
  #include "mcobject.hpp"
@@ -20,6 +22,22 @@ class PyTimestampInfo : public TimestampInfo {
20
22
  using TimestampInfo::TimestampInfo;
21
23
  };
22
24
 
25
+ py::dict mcobject_asdict(const McObject& self) {
26
+ py::dict d;
27
+ d["name"] = self.get_name();
28
+ d["address"] = self.get_address();
29
+ d["ext"] = self.get_ext();
30
+ d["length"] = self.get_length();
31
+ d["data_type"] = self.get_data_type();
32
+ py::list components_list;
33
+ for (const auto& component : self.get_components()) {
34
+ components_list.append(mcobject_asdict(component));
35
+ }
36
+ d["components"] = components_list;
37
+ return d;
38
+ }
39
+
40
+
23
41
  PYBIND11_MODULE(cpp_ext, m) {
24
42
  m.doc() = "C++ extensions for pyXCP.";
25
43
 
@@ -40,6 +58,7 @@ PYBIND11_MODULE(cpp_ext, m) {
40
58
  .def_property_readonly("components", &McObject::get_components)
41
59
 
42
60
  .def("add_component", &McObject::add_component, "component"_a)
61
+ .def("asdict", &mcobject_asdict)
43
62
  .def("__eq__", [](const McObject& self, const McObject& other) { return self == other; })
44
63
  .def("__repr__", [](const McObject& self) { return to_string(self); })
45
64
  .def("__hash__", [](const McObject& self) { return self.get_hash(); })
@@ -51,32 +70,120 @@ PYBIND11_MODULE(cpp_ext, m) {
51
70
  .def_property("residual_capacity", &Bin::get_residual_capacity, &Bin::set_residual_capacity)
52
71
  .def_property("entries", &Bin::get_entries, nullptr)
53
72
  .def("append", &Bin::append)
54
-
73
+ .def("asdict", [](const Bin& self) {
74
+ py::dict d;
75
+ d["size"] = self.get_size();
76
+ d["residual_capacity"] = self.get_residual_capacity();
77
+ py::list entries_list;
78
+ for (const auto& entry : self.get_entries()) {
79
+ entries_list.append(mcobject_asdict(entry));
80
+ }
81
+ d["entries"] = entries_list;
82
+ return d;
83
+ })
55
84
  .def("__repr__", [](const Bin& self) { return to_string(self); })
56
-
57
85
  .def("__eq__", [](const Bin& self, const Bin& other) { return self == other; })
58
-
59
86
  .def("__len__", [](const Bin& self) { return std::size(self.get_entries()); });
60
87
 
61
- py::class_<DaqList>(m, "DaqList")
88
+ py::class_<DaqListBase, std::shared_ptr<DaqListBase>>(m, "DaqListBase")
89
+ .def_property("name", &DaqListBase::get_name, nullptr)
90
+ .def_property("event_num", &DaqListBase::get_event_num, &DaqListBase::set_event_num)
91
+ .def_property("priority", &DaqListBase::get_priority, nullptr)
92
+ .def_property("prescaler", &DaqListBase::get_prescaler, nullptr)
93
+ .def_property("stim", &DaqListBase::get_stim, nullptr)
94
+ .def_property("enable_timestamps", &DaqListBase::get_enable_timestamps, nullptr)
95
+ .def_property("measurements_opt", &DaqListBase::get_measurements_opt, &DaqListBase::set_measurements_opt)
96
+ .def_property("headers", &DaqListBase::get_headers, nullptr)
97
+ .def_property("odt_count", &DaqListBase::get_odt_count, nullptr)
98
+ .def_property("total_entries", &DaqListBase::get_total_entries, nullptr)
99
+ .def_property("total_length", &DaqListBase::get_total_length, nullptr)
100
+ .def("asdict", [](const DaqListBase& self) {
101
+ py::dict d;
102
+ d["name"] = self.get_name();
103
+ d["event_num"] = self.get_event_num();
104
+ d["priority"] = self.get_priority();
105
+ d["prescaler"] = self.get_prescaler();
106
+ d["stim"] = self.get_stim();
107
+ d["enable_timestamps"] = self.get_enable_timestamps();
108
+ d["measurements_opt"] = self.get_measurements_opt();
109
+ d["headers"] = self.get_headers();
110
+ d["odt_count"] = self.get_odt_count();
111
+ d["total_entries"] = self.get_total_entries();
112
+ d["total_length"] = self.get_total_length();
113
+ return d;
114
+ });
115
+
116
+ py::class_<DaqList, DaqListBase, std::shared_ptr<DaqList>>(m, "DaqList")
62
117
  .def(
63
118
  py::init<std::string_view, std::uint16_t, bool, bool, const std::vector<DaqList::daq_list_initialzer_t>&,
64
119
  std::uint8_t, std::uint8_t>(), "name"_a, "event_num"_a, "stim"_a, "enable_timestamps"_a, "measurements"_a,
65
120
  "priority"_a=0, "prescaler"_a=1
66
121
  )
67
122
  .def("__repr__", [](const DaqList& self) { return self.to_string(); })
68
- .def_property("name", &DaqList::get_name, nullptr)
69
- .def_property("event_num", &DaqList::get_event_num, &DaqList::set_event_num)
70
- .def_property("priority", &DaqList::get_priority, nullptr)
71
- .def_property("prescaler", &DaqList::get_prescaler, nullptr)
72
- .def_property("stim", &DaqList::get_stim, nullptr)
73
- .def_property("enable_timestamps", &DaqList::get_enable_timestamps, nullptr)
74
123
  .def_property("measurements", &DaqList::get_measurements, nullptr)
75
- .def_property("measurements_opt", &DaqList::get_measurements_opt, &DaqList::set_measurements_opt)
76
- .def_property("headers", &DaqList::get_headers, nullptr)
77
- .def_property("odt_count", &DaqList::get_odt_count, nullptr)
78
- .def_property("total_entries", &DaqList::get_total_entries, nullptr)
79
- .def_property("total_length", &DaqList::get_total_length, nullptr);
124
+ .def("asdict", [](const DaqList& self) {
125
+ py::dict d;
126
+ d["name"] = self.get_name();
127
+ d["event_num"] = self.get_event_num();
128
+ d["priority"] = self.get_priority();
129
+ d["prescaler"] = self.get_prescaler();
130
+ d["stim"] = self.get_stim();
131
+ d["enable_timestamps"] = self.get_enable_timestamps();
132
+ d["measurements_opt"] = self.get_measurements_opt();
133
+ d["headers"] = self.get_headers();
134
+ d["odt_count"] = self.get_odt_count();
135
+ d["total_entries"] = self.get_total_entries();
136
+ d["total_length"] = self.get_total_length();
137
+ py::list measurements_list;
138
+ for (const auto& measurement : self.get_measurements()) {
139
+ measurements_list.append(mcobject_asdict(measurement));
140
+ }
141
+ d["measurements"] = measurements_list;
142
+ return d;
143
+ });
144
+
145
+ py::class_<PredefinedDaqList, DaqListBase, std::shared_ptr<PredefinedDaqList>>(m, "PredefinedDaqList")
146
+ .def(
147
+ py::init<std::string_view, std::uint16_t, bool, bool, const PredefinedDaqList::predefined_daq_list_initializer_t&,
148
+ std::uint8_t, std::uint8_t>(), "name"_a, "event_num"_a, "stim"_a, "enable_timestamps"_a, "odts"_a,
149
+ "priority"_a=0, "prescaler"_a=1
150
+ )
151
+ .def("__repr__", [](const PredefinedDaqList& self) {
152
+ try {
153
+ return self.to_string();
154
+ } catch (const std::exception& e) {
155
+ return std::string("PredefinedDaqList(<repr error: ") + e.what() + ">)";
156
+ } catch (...) {
157
+ return std::string("PredefinedDaqList(<repr error: unknown>)");
158
+ }
159
+ })
160
+ .def("asdict", [](const PredefinedDaqList& self) {
161
+ py::dict d;
162
+ d["name"] = self.get_name();
163
+ d["event_num"] = self.get_event_num();
164
+ d["priority"] = self.get_priority();
165
+ d["prescaler"] = self.get_prescaler();
166
+ d["stim"] = self.get_stim();
167
+ d["enable_timestamps"] = self.get_enable_timestamps();
168
+ d["measurements_opt"] = self.get_measurements_opt();
169
+ d["headers"] = self.get_headers();
170
+ d["odt_count"] = self.get_odt_count();
171
+ d["total_entries"] = self.get_total_entries();
172
+ d["total_length"] = self.get_total_length();
173
+ return d;
174
+ })
175
+ ;
176
+
177
+ // Aligned buffer utility
178
+ py::class_<AlignedBuffer>(m, "AlignedBuffer")
179
+ .def(py::init<std::size_t>(), py::arg("size") = 0xffff)
180
+ .def("reset", &AlignedBuffer::reset)
181
+ .def("append", &AlignedBuffer::append, py::arg("value"))
182
+ .def("extend", py::overload_cast<const py::bytes&>(&AlignedBuffer::extend))
183
+ .def("extend", py::overload_cast<const std::vector<std::uint8_t>&>(&AlignedBuffer::extend))
184
+ .def("__len__", [](const AlignedBuffer& self) { return self.size(); })
185
+ .def("__getitem__", [](const AlignedBuffer& self, py::object index) { return self.get_item(index); });
186
+
80
187
 
81
188
  py::enum_<TimestampType>(m, "TimestampType")
82
189
  .value("ABSOLUTE_TS", TimestampType::ABSOLUTE_TS)
@@ -97,4 +204,5 @@ PYBIND11_MODULE(cpp_ext, m) {
97
204
  .def_property("utc_offset", &TimestampInfo::get_utc_offset, &TimestampInfo::set_utc_offset)
98
205
  .def_property("dst_offset", &TimestampInfo::get_dst_offset, &TimestampInfo::set_dst_offset)
99
206
  .def_property("timezone", &TimestampInfo::get_timezone, &TimestampInfo::set_timezone);
207
+
100
208
  }
@@ -0,0 +1,360 @@
1
+
2
+ #if !defined(__FRAMING_HPP)
3
+ #define __FRAMING_HPP
4
+
5
+ #include <cstdint>
6
+
7
+ #include <atomic>
8
+ #include <bit>
9
+ #include <optional>
10
+ #include <iostream>
11
+ #include <map>
12
+ #include <mutex>
13
+ #include <optional>
14
+ #include <set>
15
+ #include <thread>
16
+ #include <tuple>
17
+ #include <variant>
18
+ #include <vector>
19
+ #include <cstring>
20
+
21
+ namespace py = pybind11;
22
+
23
+ using FrameType = py::bytes;
24
+ using CommandType = std::variant<std::monostate, std::uint8_t, std::uint16_t, std::uint32_t>;
25
+
26
+ std::uint8_t find_msb(std::uint32_t val) {
27
+ std::uint8_t position = 0;
28
+
29
+ if (val == 0) {
30
+ return 1;
31
+ }
32
+ while ((val >>= 1)) {
33
+ ++position;
34
+ }
35
+
36
+ return position + 1;
37
+ }
38
+
39
+ std::uint8_t byte_count(std::uint32_t val) {
40
+ std::uint8_t count = 1;
41
+
42
+ auto high_bit = find_msb(val);
43
+ if (high_bit > 24) {
44
+ count = 4;
45
+ } else if (high_bit > 16) {
46
+ count = 3;
47
+ } else if (high_bit > 8) {
48
+ count = 2;
49
+ }
50
+
51
+ return count;
52
+ }
53
+
54
+ std::vector<std::uint8_t> serialize_cmd_value(std::uint32_t value) {
55
+ std::vector<std::uint8_t> result;
56
+
57
+ auto bc = byte_count(value);
58
+ result.reserve(bc);
59
+
60
+ switch (bc) {
61
+ case 4:
62
+ result.push_back(static_cast<std::uint8_t>(static_cast<std::uint32_t>(((value & 0xff000000UL)) >> 24)));
63
+ [[fallthrough]];
64
+ case 3:
65
+ result.push_back(static_cast<std::uint8_t>(static_cast<std::uint32_t>(((value & 0xff0000UL)) >> 16)));
66
+ [[fallthrough]];
67
+ case 2:
68
+ result.push_back(static_cast<std::uint8_t>(static_cast<std::uint16_t>(((value & 0xff00UL)) >> 8)));
69
+ [[fallthrough]];
70
+ case 1:
71
+ result.push_back(static_cast<std::uint8_t>(value & 0xffUL));
72
+ break;
73
+ }
74
+
75
+ return result;
76
+ }
77
+
78
+ std::vector<std::uint8_t> serialize_word_le(std::uint16_t value) {
79
+ std::vector<std::uint8_t> result;
80
+ result.reserve(2);
81
+
82
+ result.push_back(static_cast<std::uint8_t>(value & 0xffUL));
83
+ result.push_back(static_cast<std::uint8_t>(static_cast<std::uint16_t>(((value & 0xff00UL)) >> 8)));
84
+ result.resize(2);
85
+ return result;
86
+ }
87
+
88
+ std::string_view bytes_as_string_view(const py::bytes& data) {
89
+ // Zero-copy view into Python bytes; lifetime is tied to 'data' which
90
+ // outlives this function call.
91
+ char* buf = nullptr;
92
+ Py_ssize_t len = 0;
93
+ // PyBytes_AsStringAndSize returns 0 on success.
94
+ if (PyBytes_AsStringAndSize(data.ptr(), &buf, &len) != 0 || buf == nullptr || len < 0) {
95
+ // Fallback: empty view on error (should be rare)
96
+ return std::string_view{};
97
+ }
98
+ return std::string_view(buf, static_cast<std::size_t>(len));
99
+ }
100
+
101
+ enum class ChecksumType : std::uint8_t {
102
+ NO_CHECKSUM = 0,
103
+ BYTE_CHECKSUM = 1,
104
+ WORD_CHECKSUM = 2
105
+ };
106
+
107
+ enum class XcpTransportLayerType : std::uint8_t {
108
+ CAN,
109
+ ETH,
110
+ SXI,
111
+ USB
112
+ };
113
+
114
+ struct XcpFramingConfig {
115
+ XcpTransportLayerType transport_layer_type;
116
+ std::uint8_t header_len;
117
+ std::uint8_t header_ctr;
118
+ std::uint8_t header_fill;
119
+
120
+ bool tail_fill;
121
+ ChecksumType tail_cs;
122
+
123
+ };
124
+
125
+ class XcpFraming {
126
+ public:
127
+
128
+ XcpFraming(const XcpFramingConfig& framing_type) : m_counter_send(0),
129
+ m_framing_type(framing_type) {
130
+
131
+ m_send_buffer = new std::uint8_t[0xff + 8];
132
+ reset_send_buffer_pointer();
133
+ }
134
+
135
+ XcpFraming() = delete;
136
+ XcpFraming(const XcpFraming&) = delete;
137
+ XcpFraming(XcpFraming&&) = delete;
138
+
139
+ FrameType prepare_request(std::uint32_t cmd, py::args data) {
140
+
141
+ std::vector<std::uint8_t> command_bytes{};
142
+ std::uint8_t frame_header_size{0};
143
+ std::uint8_t frame_tail_size{0};
144
+
145
+ reset_send_buffer_pointer();
146
+
147
+ command_bytes = serialize_cmd_value(cmd);
148
+
149
+ auto xcp_packet_size = data.size() + command_bytes.size();
150
+
151
+ if (m_framing_type.header_len > 0) {
152
+ frame_header_size += m_framing_type.header_len;
153
+ if (m_framing_type.header_len == 1) {
154
+ set_send_buffer(static_cast<std::uint8_t>(xcp_packet_size & 0xff));
155
+ } else {
156
+ auto packet_size_bytes = serialize_word_le(xcp_packet_size);
157
+ set_send_buffer(packet_size_bytes);
158
+ }
159
+ }
160
+ if (m_framing_type.header_ctr > 0) {
161
+ frame_header_size += m_framing_type.header_ctr;
162
+ if (m_framing_type.header_ctr == 1) {
163
+ set_send_buffer(static_cast<std::uint8_t>(m_counter_send & 0xff));
164
+ } else {
165
+ auto counter_bytes = serialize_word_le(m_counter_send);
166
+ set_send_buffer(counter_bytes);
167
+ }
168
+ }
169
+ if (m_framing_type.header_fill > 0) {
170
+ fill_send_buffer(m_framing_type.header_fill);
171
+ frame_header_size += m_framing_type.header_fill;
172
+ }
173
+
174
+ set_send_buffer(command_bytes);
175
+ set_send_buffer(data);
176
+
177
+ if (m_framing_type.transport_layer_type == XcpTransportLayerType::SXI) {
178
+ if (m_framing_type.tail_fill == true) {
179
+ // Align to a word boundary if word checksum is used
180
+ if (m_framing_type.tail_cs == ChecksumType::WORD_CHECKSUM && (current_send_buffer_pointer() % 2 != 0)) {
181
+ fill_send_buffer(1);
182
+ }
183
+ }
184
+
185
+ if (m_framing_type.tail_cs != ChecksumType::NO_CHECKSUM) {
186
+ if (m_framing_type.tail_cs == ChecksumType::BYTE_CHECKSUM) {
187
+ auto cs = checksum_byte(0, current_send_buffer_pointer());
188
+ set_send_buffer(cs);
189
+ frame_tail_size += 1;
190
+ }
191
+ else if (m_framing_type.tail_cs == ChecksumType::WORD_CHECKSUM) {
192
+ // Align to a word boundary before calculating word checksum
193
+ if (current_send_buffer_pointer() % 2 != 0) {
194
+ fill_send_buffer(1);
195
+ }
196
+ auto cs = checksum_word(0, current_send_buffer_pointer());
197
+ auto cs_bytes = serialize_word_le(cs);
198
+ set_send_buffer(cs_bytes);
199
+ frame_tail_size += 2;
200
+ }
201
+ }
202
+ }
203
+
204
+ m_counter_send++;
205
+ py::bytes result(reinterpret_cast<const char*>(m_send_buffer), current_send_buffer_pointer());
206
+ return result;
207
+ }
208
+
209
+ FrameType prepare_request(std::uint32_t cmd, const std::vector<std::uint8_t>& data_vec) {
210
+ py::tuple data_tuple(data_vec.size());
211
+ for (size_t i = 0; i < data_vec.size(); ++i) {
212
+ data_tuple[i] = py::int_(data_vec[i]);
213
+ }
214
+ return prepare_request(cmd, data_tuple);
215
+ }
216
+
217
+ std::optional<std::tuple<std::uint16_t, std::uint16_t>> unpack_header(const py::bytes& data, std::uint16_t initial_offset=0) const noexcept {
218
+ auto data_view = bytes_as_string_view(data);
219
+ if (std::size(data_view) >= (get_header_size() + initial_offset)) {
220
+ auto offset = initial_offset;
221
+ std::uint16_t length = 0U;
222
+ std::uint16_t counter = 0U;
223
+
224
+ // Read length field starting at current offset (if present)
225
+ if (m_framing_type.header_len > 0) {
226
+ if (m_framing_type.header_len == 1) {
227
+ length = static_cast<std::uint16_t>(static_cast<std::uint8_t>(data_view[offset]));
228
+ } else {
229
+ auto b0 = static_cast<std::uint8_t>(data_view[offset]);
230
+ auto b1 = static_cast<std::uint8_t>(data_view[offset + 1]);
231
+ length = static_cast<std::uint16_t>(static_cast<std::uint16_t>(b0) | (static_cast<std::uint16_t>(b1) << 8));
232
+ }
233
+ offset += m_framing_type.header_len;
234
+ }
235
+ // Read counter field starting after length (if present)
236
+ if (m_framing_type.header_ctr > 0) {
237
+ if (m_framing_type.header_ctr == 1) {
238
+ counter = static_cast<std::uint16_t>(static_cast<std::uint8_t>(data_view[offset]));
239
+ } else {
240
+ auto c0 = static_cast<std::uint8_t>(data_view[offset]);
241
+ auto c1 = static_cast<std::uint8_t>(data_view[offset + 1]);
242
+ counter = static_cast<std::uint16_t>(static_cast<std::uint16_t>(c0) | (static_cast<std::uint16_t>(c1) << 8));
243
+ }
244
+ }
245
+ return std::make_tuple(length, counter);
246
+ }
247
+ return std::nullopt;
248
+ }
249
+
250
+ bool verify_checksum(const py::bytes& data) const noexcept {
251
+ if (m_framing_type.transport_layer_type != XcpTransportLayerType::SXI || m_framing_type.tail_cs == ChecksumType::NO_CHECKSUM) {
252
+ return true; // No checksum verification needed
253
+ }
254
+
255
+ auto data_view = bytes_as_string_view(data);
256
+ auto data_size = std::size(data_view);
257
+
258
+ if (m_framing_type.tail_cs == ChecksumType::BYTE_CHECKSUM) {
259
+ if (data_size < 1) return false;
260
+ std::uint8_t received_cs = static_cast<std::uint8_t>(data_view[data_size - 1]);
261
+ std::uint8_t calculated_cs = 0;
262
+ for (size_t i = 0; i < data_size - 1; ++i) {
263
+ calculated_cs += static_cast<std::uint8_t>(data_view[i]);
264
+ }
265
+ return received_cs == calculated_cs;
266
+ }
267
+ else if (m_framing_type.tail_cs == ChecksumType::WORD_CHECKSUM) {
268
+ if (data_size < 2 || data_size % 2 != 0) return false; // Must have even length for word checksum
269
+
270
+ std::uint16_t received_cs = static_cast<std::uint16_t>(
271
+ static_cast<std::uint8_t>(data_view[data_size - 2]) |
272
+ (static_cast<std::uint16_t>(static_cast<std::uint8_t>(data_view[data_size - 1])) << 8)
273
+ );
274
+
275
+ std::uint16_t calculated_cs = 0;
276
+ for (size_t i = 0; i < data_size - 2; i += 2) {
277
+ calculated_cs += static_cast<std::uint16_t>(
278
+ static_cast<std::uint8_t>(data_view[i]) |
279
+ (static_cast<std::uint16_t>(static_cast<std::uint8_t>(data_view[i + 1])) << 8)
280
+ );
281
+ }
282
+ return received_cs == calculated_cs;
283
+ }
284
+
285
+ return true; // Should not be reached if NO_CHECKSUM is handled
286
+ }
287
+
288
+ std::uint16_t get_header_size() const noexcept {
289
+ return m_framing_type.header_len + m_framing_type.header_ctr + m_framing_type.header_fill;
290
+ }
291
+
292
+ std::uint16_t get_counter_send() const noexcept {
293
+ return m_counter_send;
294
+ }
295
+
296
+ void set_counter_send(std::uint16_t counter) noexcept {
297
+ m_counter_send = counter;
298
+ }
299
+
300
+
301
+ private:
302
+ void set_send_buffer(std::uint8_t value) noexcept {
303
+ m_send_buffer[m_send_buffer_offset] = value;
304
+ m_send_buffer_offset++;
305
+ }
306
+
307
+ void set_send_buffer(const std::vector<std::uint8_t>& values) noexcept {
308
+ if (!values.empty()) {
309
+ std::memcpy(m_send_buffer + m_send_buffer_offset, values.data(), values.size());
310
+ m_send_buffer_offset += static_cast<std::uint16_t>(values.size());
311
+ }
312
+ }
313
+
314
+ void set_send_buffer(const py::args& values) noexcept {
315
+ for (auto idx=0; idx < values.size(); ++idx) {
316
+ m_send_buffer[m_send_buffer_offset] = values[idx].cast<std::uint8_t>();
317
+ m_send_buffer_offset++;
318
+ }
319
+ }
320
+
321
+ void fill_send_buffer(uint16_t n) noexcept {
322
+ for (auto idx=0; idx < n; ++idx) {
323
+ m_send_buffer[m_send_buffer_offset] = 0;
324
+ m_send_buffer_offset++;
325
+ }
326
+ }
327
+
328
+ void reset_send_buffer_pointer() noexcept {
329
+ m_send_buffer_offset = 0UL;
330
+ }
331
+
332
+ std::uint16_t current_send_buffer_pointer() const noexcept {
333
+ return m_send_buffer_offset;
334
+ }
335
+
336
+ std::uint8_t checksum_byte(std::uint16_t begin, std::uint16_t end) const noexcept {
337
+ uint8_t cs = 0;
338
+ for (auto idx = begin; idx < end; ++idx) {
339
+ cs += m_send_buffer[idx];
340
+ }
341
+ return cs;
342
+ }
343
+
344
+ std::uint16_t checksum_word(std::uint16_t begin, std::uint16_t end) const noexcept {
345
+ std::uint16_t cs = 0UL;
346
+
347
+ for (auto idx = begin; idx < end; idx+=2) {
348
+ cs += static_cast<std::uint16_t>(m_send_buffer[idx] | (m_send_buffer[idx + 1] << 8));
349
+ }
350
+ return cs;
351
+ }
352
+
353
+ private:
354
+ std::uint16_t m_counter_send;
355
+ XcpFramingConfig m_framing_type;
356
+ std::uint8_t * m_send_buffer = nullptr;
357
+ std::uint16_t m_send_buffer_offset = 0UL;
358
+ };
359
+
360
+ #endif // __FRAMING_HPP