feetech_reader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
20 #ifndef FEETECH_READER_H
21 #define FEETECH_READER_H
22 
23 #include "feetech_protocol.h"
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define FEETECH_ACK_SIZE (6)
34 #define FEETECH_RESPONSE_SIZE(len) (6 + len)
35 
39 typedef struct {
40  const uint8_t *buffer;
41  size_t size;
43 
51 static inline void feetech_reader_init(feetech_reader_t *reader, const uint8_t *buffer, size_t size)
52 {
53  reader->buffer = buffer;
54  reader->size = size;
55 }
56 
64 uint8_t feetech_reader_compute_sum(const feetech_reader_t *reader);
65 
74 static inline bool feetech_reader_check_minsize(const feetech_reader_t *reader)
75 {
76  return 5 < reader->size;
77 }
78 
79 
88 static inline bool feetech_reader_check_start(const feetech_reader_t *reader)
89 {
90  return
91  reader->buffer[0] == FEETECH_START &&
92  reader->buffer[1] == FEETECH_START;
93 }
94 
103 static inline bool feetech_reader_check_size(const feetech_reader_t *reader)
104 {
105  return reader->size == (size_t)(reader->buffer[3] + 4);
106 }
107 
116 static inline bool feetech_reader_check_sum(const feetech_reader_t *reader)
117 {
118  return feetech_reader_compute_sum(reader) == reader->buffer[reader->size - 1];
119 }
120 
129 bool feetech_reader_is_valid(const feetech_reader_t *reader);
130 
138 static inline uint8_t feetech_reader_get_id(const feetech_reader_t *reader)
139 {
140  return reader->buffer[2];
141 }
142 
150 static inline uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
151 {
152  return reader->buffer[4];
153 }
154 
162 static inline const uint8_t *feetech_reader_response_get_payload(const feetech_reader_t *reader)
163 {
164  return &reader->buffer[5];
165 }
166 
175 {
176  return reader->buffer[3] - 2;
177 }
178 
186 static inline const uint8_t *feetech_reader_write_get_payload(const feetech_reader_t *reader)
187 {
188  return &reader->buffer[6];
189 }
190 
198 static inline size_t feetech_reader_write_get_payload_size(const feetech_reader_t *reader)
199 {
200  return reader->buffer[3] - 3;
201 }
202 
210 static inline uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
211 {
212  return reader->buffer[5];
213 }
214 
222 static inline size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
223 {
224  return reader->buffer[6];
225 }
226 
234 static inline uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
235 {
236  return reader->buffer[5];
237 }
238 
247 {
248  return reader->buffer[6];
249 }
250 
258 static inline uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
259 {
260  return reader->buffer[5];
261 }
262 
271 
280 uint8_t feetech_reader_sync_write_item_get_id(const feetech_reader_t *reader, uint8_t index);
281 
290 const uint8_t *feetech_reader_sync_write_item_get_payload(const feetech_reader_t *reader, uint8_t index);
291 
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 #endif /* FEETECH_READER_H */
297 
feetech_reader_check_start
static bool feetech_reader_check_start(const feetech_reader_t *reader)
Check if the packet begins with 2 FEETECH_START bits.
Definition: feetech_reader.h:88
feetech_reader_sync_write_get_reg
static uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (SYNC_WRITE)
Definition: feetech_reader.h:258
feetech_reader_response_get_payload_size
static size_t feetech_reader_response_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (response)
Definition: feetech_reader.h:174
feetech_reader_write_get_reg
static uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (WRITE)
Definition: feetech_reader.h:210
feetech_reader_init
static void feetech_reader_init(feetech_reader_t *reader, const uint8_t *buffer, size_t size)
Initialize the Feetech packet reader.
Definition: feetech_reader.h:51
feetech_reader_sync_write_get_payload_size
static size_t feetech_reader_sync_write_get_payload_size(const feetech_reader_t *reader)
Get the packet items' payload size (SYNC_WRITE)
Definition: feetech_reader.h:246
feetech_reader_get_instr
static uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
Get the packet's instruction code.
Definition: feetech_reader.h:150
feetech_reader_get_id
static uint8_t feetech_reader_get_id(const feetech_reader_t *reader)
Get the packet's device id.
Definition: feetech_reader.h:138
feetech_reader_t::size
size_t size
data buffer's size
Definition: feetech_reader.h:41
feetech_protocol.h
Feetech protocol definitions.
feetech_reader_check_minsize
static bool feetech_reader_check_minsize(const feetech_reader_t *reader)
Check if the packet has the minimum required size.
Definition: feetech_reader.h:74
feetech_reader_write_get_payload_size
static size_t feetech_reader_write_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (WRITE)
Definition: feetech_reader.h:198
feetech_reader_compute_sum
uint8_t feetech_reader_compute_sum(const feetech_reader_t *reader)
Compute the packet's sum.
feetech_reader_response_get_payload
static const uint8_t * feetech_reader_response_get_payload(const feetech_reader_t *reader)
Get the packet's payload (response)
Definition: feetech_reader.h:162
feetech_reader_sync_write_get_items_count
size_t feetech_reader_sync_write_get_items_count(const feetech_reader_t *reader)
Get the packet items' count (SYNC_WRITE)
feetech_reader_read_get_size
static size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
Get the packet's READ size.
Definition: feetech_reader.h:222
feetech_reader_t
Feetech packet reader struct.
Definition: feetech_reader.h:39
feetech_reader_sync_write_item_get_id
uint8_t feetech_reader_sync_write_item_get_id(const feetech_reader_t *reader, uint8_t index)
Get the packet item's device id (SYNC_WRITE)
feetech_reader_check_sum
static bool feetech_reader_check_sum(const feetech_reader_t *reader)
Check if the computed sum and the sum of the packet are equal.
Definition: feetech_reader.h:116
feetech_reader_write_get_payload
static const uint8_t * feetech_reader_write_get_payload(const feetech_reader_t *reader)
Get the packet's payload (WRITE)
Definition: feetech_reader.h:186
feetech_reader_is_valid
bool feetech_reader_is_valid(const feetech_reader_t *reader)
Check if the packet is valid.
feetech_reader_read_get_reg
static uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (READ)
Definition: feetech_reader.h:234
feetech_reader_t::buffer
const uint8_t * buffer
data buffer
Definition: feetech_reader.h:40
feetech_reader_check_size
static bool feetech_reader_check_size(const feetech_reader_t *reader)
Check if the packet's size is the same as the buffer's size.
Definition: feetech_reader.h:103
feetech_reader_sync_write_item_get_payload
const uint8_t * feetech_reader_sync_write_item_get_payload(const feetech_reader_t *reader, uint8_t index)
Get the packet item's payload (SYNC_WRITE)