|
4 | 4 | #include "pdu_primitive_ctypes.h" |
5 | 5 | #include "ros_primitive_types.hpp" |
6 | 6 | #include "pdu_primitive_ctypes_conv.hpp" |
| 7 | +#include "pdu_dynamic_memory.hpp" |
7 | 8 | /* |
8 | 9 | * Dependent pdu data |
9 | 10 | */ |
|
22 | 23 | * PDU ==> ROS2 |
23 | 24 | * |
24 | 25 | ***************************/ |
25 | | -static inline int hako_convert_pdu2ros_Time(Hako_Time &src, builtin_interfaces::msg::Time &dst) |
| 26 | + |
| 27 | +static inline int _pdu2ros_Time(const char* heap_ptr, Hako_Time &src, builtin_interfaces::msg::Time &dst) |
26 | 28 | { |
27 | | - //primitive convert |
| 29 | + // primitive convert |
28 | 30 | hako_convert_pdu2ros(src.sec, dst.sec); |
29 | | - //primitive convert |
| 31 | + // primitive convert |
30 | 32 | hako_convert_pdu2ros(src.nanosec, dst.nanosec); |
| 33 | + (void)heap_ptr; |
31 | 34 | return 0; |
32 | 35 | } |
33 | 36 |
|
34 | | -template<int _src_len, int _dst_len> |
35 | | -int hako_convert_pdu2ros_array_Time(Hako_Time src[], std::array<builtin_interfaces::msg::Time, _dst_len> &dst) |
| 37 | +static inline int hako_convert_pdu2ros_Time(Hako_Time &src, builtin_interfaces::msg::Time &dst) |
36 | 38 | { |
37 | | - int ret = 0; |
38 | | - int len = _dst_len; |
39 | | - if (_dst_len > _src_len) { |
40 | | - len = _src_len; |
41 | | - ret = -1; |
| 39 | + void* base_ptr = (void*)&src; |
| 40 | + void* heap_ptr = hako_get_heap_ptr_pdu(base_ptr); |
| 41 | + // Validate magic number and version |
| 42 | + if (heap_ptr == nullptr) { |
| 43 | + return -1; // Invalid PDU metadata |
42 | 44 | } |
43 | | - for (int i = 0; i < len; i++) { |
44 | | - (void)hako_convert_pdu2ros_Time(src[i], dst[i]); |
| 45 | + else { |
| 46 | + return _pdu2ros_Time((char*)heap_ptr, src, dst); |
45 | 47 | } |
46 | | - return ret; |
47 | | -} |
48 | | -template<int _src_len, int _dst_len> |
49 | | -int hako_convert_pdu2ros_array_Time(Hako_Time src[], std::vector<builtin_interfaces::msg::Time> &dst) |
50 | | -{ |
51 | | - dst.resize(_src_len); |
52 | | - for (int i = 0; i < _src_len; i++) { |
53 | | - (void)hako_convert_pdu2ros_Time(src[i], dst[i]); |
54 | | - } |
55 | | - return 0; |
56 | 48 | } |
57 | 49 |
|
58 | 50 | /*************************** |
59 | 51 | * |
60 | 52 | * ROS2 ==> PDU |
61 | 53 | * |
62 | 54 | ***************************/ |
63 | | -static inline int hako_convert_ros2pdu_Time(builtin_interfaces::msg::Time &src, Hako_Time &dst) |
| 55 | + |
| 56 | +static inline bool _ros2pdu_Time(builtin_interfaces::msg::Time &src, Hako_Time &dst, PduDynamicMemory &dynamic_memory) |
64 | 57 | { |
65 | | - //primitive convert |
66 | | - hako_convert_ros2pdu(src.sec, dst.sec); |
67 | | - //primitive convert |
68 | | - hako_convert_ros2pdu(src.nanosec, dst.nanosec); |
69 | | - return 0; |
| 58 | + try { |
| 59 | + // primitive convert |
| 60 | + hako_convert_ros2pdu(src.sec, dst.sec); |
| 61 | + // primitive convert |
| 62 | + hako_convert_ros2pdu(src.nanosec, dst.nanosec); |
| 63 | + } catch (const std::runtime_error& e) { |
| 64 | + std::cerr << "convertor error: " << e.what() << std::endl; |
| 65 | + return false; |
| 66 | + } |
| 67 | + (void)dynamic_memory; |
| 68 | + return true; |
70 | 69 | } |
71 | 70 |
|
72 | | -template<int _src_len, int _dst_len> |
73 | | -int hako_convert_ros2pdu_array_Time(std::array<builtin_interfaces::msg::Time, _src_len> &src, Hako_Time dst[]) |
| 71 | +static inline int hako_convert_ros2pdu_Time(builtin_interfaces::msg::Time &src, Hako_Time** dst) |
74 | 72 | { |
75 | | - int ret = 0; |
76 | | - int len = _dst_len; |
77 | | - if (_dst_len > _src_len) { |
78 | | - len = _src_len; |
79 | | - ret = -1; |
| 73 | + PduDynamicMemory dynamic_memory; |
| 74 | + Hako_Time out; |
| 75 | + if (!_ros2pdu_Time(src, out, dynamic_memory)) { |
| 76 | + return -1; |
80 | 77 | } |
81 | | - for (int i = 0; i < len; i++) { |
82 | | - (void)hako_convert_ros2pdu_Time(src[i], dst[i]); |
| 78 | + int heap_size = dynamic_memory.get_total_size(); |
| 79 | + void* base_ptr = hako_create_empty_pdu(sizeof(Hako_Time), heap_size); |
| 80 | + if (base_ptr == nullptr) { |
| 81 | + return -1; |
83 | 82 | } |
84 | | - return ret; |
| 83 | + // Copy out on base data |
| 84 | + memcpy(base_ptr, (void*)&out, sizeof(Hako_Time)); |
| 85 | + |
| 86 | + // Copy dynamic part and set offsets |
| 87 | + void* heap_ptr = hako_get_heap_ptr_pdu(base_ptr); |
| 88 | + dynamic_memory.copy_to_pdu((char*)heap_ptr); |
| 89 | + |
| 90 | + *dst = (Hako_Time*)base_ptr; |
| 91 | + return hako_get_pdu_meta_data(base_ptr)->total_size; |
85 | 92 | } |
86 | | -template<int _src_len, int _dst_len> |
87 | | -int hako_convert_ros2pdu_array_Time(std::vector<builtin_interfaces::msg::Time> &src, Hako_Time dst[]) |
| 93 | + |
| 94 | +static inline Hako_Time* hako_create_empty_pdu_Time(int heap_size) |
88 | 95 | { |
89 | | - int ret = 0; |
90 | | - int len = _dst_len; |
91 | | - if (_dst_len > _src_len) { |
92 | | - len = _src_len; |
93 | | - ret = -1; |
94 | | - } |
95 | | - for (int i = 0; i < len; i++) { |
96 | | - (void)hako_convert_ros2pdu_Time(src[i], dst[i]); |
| 96 | + // Allocate PDU memory |
| 97 | + char* base_ptr = (char*)hako_create_empty_pdu(sizeof(Hako_Time), heap_size); |
| 98 | + if (base_ptr == nullptr) { |
| 99 | + return nullptr; |
97 | 100 | } |
98 | | - return ret; |
| 101 | + return (Hako_Time*)base_ptr; |
99 | 102 | } |
100 | | - |
101 | 103 | #endif /* _PDU_CTYPE_CONV_HAKO_builtin_interfaces_Time_HPP_ */ |
0 commit comments