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
297 
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)
static size_t feetech_reader_sync_write_get_payload_size(const feetech_reader_t *reader)
Get the packet items' payload size (SYNC_WRITE)
static uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (SYNC_WRITE)
static const uint8_t * feetech_reader_response_get_payload(const feetech_reader_t *reader)
Get the packet's payload (response)
static bool feetech_reader_check_start(const feetech_reader_t *reader)
Check if the packet begins with 2 FEETECH_START bits.
static uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (WRITE)
static uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
Get the packet's instruction code.
static void feetech_reader_init(feetech_reader_t *reader, const uint8_t *buffer, size_t size)
Initialize the Feetech packet reader.
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.
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)
static bool feetech_reader_check_minsize(const feetech_reader_t *reader)
Check if the packet has the minimum required size.
static uint8_t feetech_reader_get_id(const feetech_reader_t *reader)
Get the packet's device id.
static uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (READ)
static bool feetech_reader_check_sum(const feetech_reader_t *reader)
Check if the computed sum and the sum of the packet are equal.
uint8_t feetech_reader_compute_sum(const feetech_reader_t *reader)
Compute the packet's sum.
const uint8_t * buffer
data buffer
Feetech protocol definitions.
size_t feetech_reader_sync_write_get_items_count(const feetech_reader_t *reader)
Get the packet items' count (SYNC_WRITE)
Feetech packet reader struct.
size_t size
data buffer's size
static size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
Get the packet's READ size.
static size_t feetech_reader_response_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (response)
static const uint8_t * feetech_reader_write_get_payload(const feetech_reader_t *reader)
Get the packet's payload (WRITE)
stdlib.h wrapper for MSP430
bool feetech_reader_is_valid(const feetech_reader_t *reader)
Check if the packet is valid.
static size_t feetech_reader_write_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (WRITE)