-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.hpp
More file actions
258 lines (221 loc) · 5.54 KB
/
Message.hpp
File metadata and controls
258 lines (221 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @file Message.hpp
* @brief The data format handled by SerialBridge is defined here.
* @author Taiyou Komazawa
* @date 2021/8/11
*
* @details
* @par Example of data format declaration:
* //Define the structure of the data.
* typedef struct{
* int32_t x;
* int32_t y;
* int32_t z;
* } Vector3_t;
*
* //Put the data format in the message type.
* //(Since the name is long, you can rename it with typedef as follows.)
* typedef sb::Message<Vector3_t> Vector3;
*
* @par How to use:
* Vector3 msg; //Declare a message.
*
* //Register this message frame in the SerialBridge instance
* // (assuming you declared it with the name obj this time).
* //The msg_id passed to the argument together has the role of
* // associating the data communicated with the msg declared earlier.
* obj.add_frame(0, &msg); //msg_id = 0
*
* //The data to be communicated conforms
* // to the contents of the object member's data structure.
* msg.data.x;
* msg.data.y;
* msg.data.z;
*/
#ifndef _SERIAL_BRIDGE_MESSAGE_HPP_
#define _SERIAL_BRIDGE_MESSAGE_HPP_
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/**
* @namespace sb
* @brief Namespace to indicate SerialBridge messages.
*/
namespace sb
{
/**
* @brief sb::MessageInterface interface class.
* @details
* This is for abstracting various sb::Messages so that they can be handled
* with a unified interface when used within SerialBridge.
*/
class MessageInterface
{
public:
uint8_t *ptr();
void packing(uint8_t id);
void unpacking();
/**
* @brief Returns the length of the current packet.
* @return int Current packet array length.
*/
virtual int size() = 0;
/**
* @brief Returns whether the message has been updated.
*
* @return true Message has been updated.
* @return false Message has not been updated since the last function call.
*/
virtual bool was_updated() = 0;
protected:
/**
* @brief A function that returns a data structure.
* @return *void Pointer to the data structure of the derived class.
*/
virtual void *_data_ptr() = 0;
/**
* @brief The length of the packet used to identify the data.
*/
enum{
CTRL_DATA_LEN = 2,
};
uint8_t *_all_packet;
volatile bool _unpacked;
};
/**
* @brief sb::MessageInterface interface class.
* @details
* This is for abstracting various sb::Messages so that they can be handled
* with a unified interface when used within SerialBridge.
*/
class CANMessageInterface
{
public:
uint8_t *ptr();
void packing();
void unpacking();
/**
* @brief Returns the length of the current packet.
* @return int Current packet array length.
*/
virtual int size() = 0;
/**
* @brief Returns whether the message has been updated.
*
* @return true Message has been updated.
* @return false Message has not been updated since the last function call.
*/
virtual bool was_updated() = 0;
protected:
/**
* @brief A function that returns a data structure.
* @return *void Pointer to the data structure of the derived class.
*/
virtual void *_data_ptr() = 0;
/**
* @brief The length of the packet used to identify the data.
*/
enum{
CTRL_DATA_LEN = 1,
};
uint8_t *_all_packet;
volatile bool _unpacked;
};
/**
* @brief A class that stores the data processed by the SerialBridge class.
* @tparam DataStruct Specify the type of data structure you want to handle with sb::Message.
*/
template <class DataStruct>
class Message : public MessageInterface
{
public:
/**
* @brief Data is exchanged through this member variable.
* You can specify any structure type here with template parameters.
*/
DataStruct data;
/**
* @brief Message class constructor.
*/
Message()
{
_all_packet = new uint8_t[sizeof(DataStruct) + CTRL_DATA_LEN];
}
/**
* @brief Message class destructor.
*/
~Message()
{
delete[] _all_packet;
}
virtual int size()
{
return sizeof(DataStruct) + CTRL_DATA_LEN;
}
virtual bool was_updated()
{
bool tmp = _unpacked;
_unpacked = false;
return tmp;
}
private:
virtual void *_data_ptr()
{
return &data;
}
};
/**
* @brief A class that stores the data processed by the SerialBridge class.
* @tparam DataStruct Specify the type of data structure you want to handle with sb::Message.
*/
template <class DataStruct>
class CANMessage : public CANMessageInterface
{
public:
/**
* @brief Data is exchanged through this member variable.
* You can specify any structure type here with template parameters.
*/
DataStruct data;
/**
* @brief Message class constructor.
*/
CANMessage()
: _error(false)
{
if (sizeof(DataStruct) > 64 - CTRL_DATA_LEN) {
_error = true;
}
_all_packet = new uint8_t[sizeof(DataStruct) + CTRL_DATA_LEN];
}
/**
* @brief Message class destructor.
*/
~CANMessage()
{
delete[] _all_packet;
}
virtual int size()
{
return sizeof(DataStruct) + CTRL_DATA_LEN;
}
virtual bool was_updated()
{
bool tmp = _unpacked;
_unpacked = false;
return tmp;
}
virtual bool is_error()
{
return _error;
}
private:
// error
bool _error;
virtual void *_data_ptr()
{
return &data;
}
};
}
#endif //#ifndef _SERIAL_BRIDGE_MESSAGE_HPP_