a752c7ab
elopes
add first test an...
|
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
* Copyright (C) 2017 Inria
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup drivers_feetech
*
* @{
*
* @file
* @brief Interface definition for Feetech packet reader
*
* @author Loïc Dauphin <loic.dauphin@inria.fr>
*/
#ifndef FEETECH_READER_H
#define FEETECH_READER_H
#include "feetech_protocol.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#define FEETECH_ACK_SIZE (6)
#define FEETECH_RESPONSE_SIZE(len) (6 + len)
/**
* @brief Feetech packet reader struct
*/
typedef struct {
const uint8_t *buffer; /**< data buffer */
size_t size; /**< data buffer's size */
} feetech_reader_t;
/**
* @brief Initialize the Feetech packet reader
*
* @param[out] reader the packet reader
* @param[in] buffer the buffer used to store data
* @param[in] size the size of the buffer
*/
static inline void feetech_reader_init(feetech_reader_t *reader, const uint8_t *buffer, size_t size)
{
reader->buffer = buffer;
reader->size = size;
}
/**
* @brief Compute the packet's sum
*
* @param[in] reader the packet reader
*
* @return the sum of the packet
*/
uint8_t feetech_reader_compute_sum(const feetech_reader_t *reader);
/**
* @brief Check if the packet has the minimum required size
*
* @param[in] reader the packet reader
*
* @return true if the packet has the minimum required size
* @return false otherwise
*/
static inline bool feetech_reader_check_minsize(const feetech_reader_t *reader)
{
return 5 < reader->size;
}
/**
* @brief Check if the packet begins with 2 FEETECH_START bits
*
* @param[in] reader the packet reader
*
* @return true if the packet begins with 2 FEETECH_START bits
* @return false otherwise
*/
static inline bool feetech_reader_check_start(const feetech_reader_t *reader)
{
return
reader->buffer[0] == FEETECH_START &&
reader->buffer[1] == FEETECH_START;
}
/**
* @brief Check if the packet's size is the same as the buffer's size
*
* @param[in] reader the packet reader
*
* @return true if the packet's size is the same as the buffer's size
* @return false otherwise
*/
static inline bool feetech_reader_check_size(const feetech_reader_t *reader)
{
return reader->size == (size_t)(reader->buffer[3] + 4);
}
/**
* @brief Check if the computed sum and the sum of the packet are equal
*
* @param[in] reader the packet reader
*
* @return true if the computed sum and the sum of the packet are equal
* @return false otherwise
*/
static inline bool feetech_reader_check_sum(const feetech_reader_t *reader)
{
return feetech_reader_compute_sum(reader) == reader->buffer[reader->size - 1];
}
/**
* @brief Check if the packet is valid
*
* @param[in] reader the packet reader
*
* @return true if the packet is valid
* @return false otherwise
*/
bool feetech_reader_is_valid(const feetech_reader_t *reader);
/**
* @brief Get the packet's device id
*
* @param[in] reader the packet reader
*
* @return the packet's device id
*/
static inline uint8_t feetech_reader_get_id(const feetech_reader_t *reader)
{
return reader->buffer[2];
}
/**
* @brief Get the packet's instruction code
*
* @param[in] reader the packet reader
*
* @return the packet's instruction code
*/
static inline uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
{
return reader->buffer[4];
}
/**
* @brief Get the packet's payload (response)
*
* @param[in] reader the packet reader
*
* @return the addess of the begining of the payload
*/
static inline const uint8_t *feetech_reader_response_get_payload(const feetech_reader_t *reader)
{
return &reader->buffer[5];
}
/**
* @brief Get the packet's payload size (response)
*
* @param[in] reader the packet reader
*
* @return the size of the payload
*/
static inline size_t feetech_reader_response_get_payload_size(const feetech_reader_t *reader)
{
return reader->buffer[3] - 2;
}
/**
* @brief Get the packet's payload (WRITE)
*
* @param[in] reader the packet reader
*
* @return the begining addess of the payload
*/
static inline const uint8_t *feetech_reader_write_get_payload(const feetech_reader_t *reader)
{
return &reader->buffer[6];
}
/**
* @brief Get the packet's payload size (WRITE)
*
* @param[in] reader the packet reader
*
* @return the size of the payload
*/
static inline size_t feetech_reader_write_get_payload_size(const feetech_reader_t *reader)
{
return reader->buffer[3] - 3;
}
/**
* @brief Get the packet's target register address (WRITE)
*
* @param[in] reader the packet reader
*
* @return the register address
*/
static inline uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
{
return reader->buffer[5];
}
/**
* @brief Get the packet's READ size
*
* @param[in] reader the packet reader
*
* @return the READ size
*/
static inline size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
{
return reader->buffer[6];
}
/**
* @brief Get the packet's target register address (READ)
*
* @param[in] reader the packet reader
*
* @return the register address
*/
static inline uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
{
return reader->buffer[5];
}
/**
* @brief Get the packet items' payload size (SYNC_WRITE)
*
* @param[in] reader the packet reader
*
* @return the size of the items' payload
*/
static inline size_t feetech_reader_sync_write_get_payload_size(const feetech_reader_t *reader)
{
return reader->buffer[6];
}
/**
* @brief Get the packet's target register address (SYNC_WRITE)
*
* @param[in] reader the packet reader
*
* @return the register address
*/
static inline uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
{
return reader->buffer[5];
}
/**
* @brief Get the packet items' count (SYNC_WRITE)
*
* @param[in] reader the packet reader
*
* @return the number of items in the packet
*/
size_t feetech_reader_sync_write_get_items_count(const feetech_reader_t *reader);
/**
* @brief Get the packet item's device id (SYNC_WRITE)
*
* @param[in] reader the packet reader
* @param[in] index the item index
*
* @return the item's device id
*/
uint8_t feetech_reader_sync_write_item_get_id(const feetech_reader_t *reader, uint8_t index);
/**
* @brief Get the packet item's payload (SYNC_WRITE)
*
* @param[in] reader the packet reader
* @param[in] index the item index
*
* @return the begining addess of the payload
*/
const uint8_t *feetech_reader_sync_write_item_get_payload(const feetech_reader_t *reader, uint8_t index);
#ifdef __cplusplus
}
#endif
#endif /* FEETECH_READER_H */
/** @} */
|