|
39 | 39 |
|
40 | 40 | #include <stdint.h> |
41 | 41 | #include <stdlib.h> |
| 42 | +#include <string.h> |
42 | 43 |
|
43 | | -/** SerialBridge's namespace */ |
| 44 | +/** |
| 45 | +* @namespace sb |
| 46 | +* @brief Namespace to indicate SerialBridge messages. |
| 47 | +*/ |
44 | 48 | namespace sb |
45 | 49 | { |
46 | 50 |
|
47 | 51 | /** |
48 | | -* @brief sb::Message interface class. |
| 52 | +* @brief sb::MessageInterface interface class. |
49 | 53 | * @details |
50 | 54 | * This is for abstracting various sb::Messages so that they can be handled |
51 | 55 | * with a unified interface when used within SerialBridge. |
52 | 56 | */ |
53 | | -class _Message |
| 57 | +class MessageInterface |
54 | 58 | { |
55 | 59 | public: |
56 | | - /** |
57 | | - * @brief Returns a pointer to the current packet. |
58 | | - * @return uint8_t* Pointer indicating the packet array. |
59 | | - */ |
60 | | - virtual uint8_t *ptr() = 0; |
| 60 | + uint8_t *ptr(); |
| 61 | + |
| 62 | + void packing(uint8_t id); |
| 63 | + void unpacking(); |
61 | 64 |
|
62 | 65 | /** |
63 | | - * @brief Stores the data structure in a packet. |
64 | | - * This function is used internally by SerialBridge when sending. |
65 | | - * @param[in] id Message ID assigned when sending. |
66 | | - * @return None |
| 66 | + * @brief Returns the length of the current packet. |
| 67 | + * @return int Current packet array length. |
67 | 68 | */ |
68 | | - virtual void packing(uint8_t id) = 0; |
| 69 | + virtual int size() = 0; |
69 | 70 |
|
| 71 | +protected: |
70 | 72 | /** |
71 | | - * @brief Extract the data structure from the packet. |
72 | | - * This function is used internally by SerialBridge on reception. |
73 | | - * @return None |
| 73 | + * @brief A function that returns a data structure. |
| 74 | + * @return *void Pointer to the data structure of the derived class. |
74 | 75 | */ |
75 | | - virtual void unpacking() = 0; |
| 76 | + virtual void *_data_ptr() = 0; |
76 | 77 |
|
77 | 78 | /** |
78 | | - * @brief Returns the length of the current packet. |
79 | | - * @return int Current packet array length. |
| 79 | + * @brief The length of the packet used to identify the data. |
80 | 80 | */ |
81 | | - virtual int size() = 0; |
| 81 | + enum{ |
| 82 | + CTRL_DATA_LEN = 2, |
| 83 | + }; |
| 84 | + |
| 85 | + uint8_t *_all_packet; |
82 | 86 | }; |
83 | 87 |
|
84 | 88 | /** |
85 | 89 | * @brief A class that stores the data processed by the SerialBridge class. |
86 | 90 | * @tparam DataStruct Specify the type of data structure you want to handle with sb::Message. |
87 | 91 | */ |
88 | 92 | template <class DataStruct> |
89 | | -class Message : public _Message |
| 93 | +class Message : public MessageInterface |
90 | 94 | { |
91 | 95 | public: |
92 | | - |
93 | | - /** Data is passed using this member structure. */ |
94 | | - DataStruct data; |
95 | | - |
96 | 96 | /** |
97 | | - * @deprecated The following functions are used for internal processing |
98 | | - * and do not support direct user calls. |
| 97 | + * @brief Data is exchanged through this member variable. |
| 98 | + * You can specify any structure type here with template parameters. |
99 | 99 | */ |
100 | | - virtual uint8_t *ptr() |
101 | | - { |
102 | | - return _all_packet; |
103 | | - } |
| 100 | + DataStruct data; |
104 | 101 |
|
105 | 102 | /** |
106 | | - * @deprecated The following functions are used for internal processing |
107 | | - * and do not support direct user calls. |
| 103 | + * @brief Message class constructor. |
108 | 104 | */ |
109 | | - virtual void packing(uint8_t id) |
| 105 | + Message() |
110 | 106 | { |
111 | | - _all_packet[0] = id; |
112 | | - _Packet_t tmp; |
113 | | - tmp.data = data; |
114 | | - |
115 | | - uint32_t sum = 0; |
116 | | - for(int i = 0; i < size()-1; i++){ |
117 | | - if(i >= 1) |
118 | | - _all_packet[i] = tmp.all[i-1]; |
119 | | - sum += _all_packet[i]; |
120 | | - } |
121 | | - _all_packet[size()-1] = (uint8_t)(sum & 0xFF); |
| 107 | + _all_packet = new uint8_t[sizeof(DataStruct) + CTRL_DATA_LEN]; |
122 | 108 | } |
123 | 109 |
|
124 | 110 | /** |
125 | | - * @deprecated The following functions are used for internal processing |
126 | | - * and do not support direct user calls. |
| 111 | + * @brief Message class destructor. |
127 | 112 | */ |
128 | | - virtual void unpacking() |
| 113 | + ~Message() |
129 | 114 | { |
130 | | - _Packet_t tmp; |
131 | | - for(int i = 0; i < (int)sizeof(DataStruct); i++){ |
132 | | - tmp.all[i] = _all_packet[i+1]; |
133 | | - } |
134 | | - data = tmp.data; |
| 115 | + delete[] _all_packet; |
135 | 116 | } |
136 | 117 |
|
137 | 118 | virtual int size() |
138 | 119 | { |
139 | | - return sizeof(DataStruct) + 2; |
| 120 | + return sizeof(DataStruct) + CTRL_DATA_LEN; |
140 | 121 | } |
| 122 | + |
141 | 123 | private: |
142 | | - typedef union{ |
143 | | - uint8_t all[sizeof(DataStruct)]; |
144 | | - DataStruct data; |
145 | | - }_Packet_t; |
146 | 124 |
|
147 | | - uint8_t _all_packet[sizeof(DataStruct)+2]; |
| 125 | + virtual void *_data_ptr() |
| 126 | + { |
| 127 | + return &data; |
| 128 | + } |
148 | 129 | }; |
149 | 130 |
|
150 | 131 | }; |
|
0 commit comments