Loading...
Searching...
No Matches
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
30extern "C" {
31#endif
32
33#define FEETECH_ACK_SIZE (6)
34#define FEETECH_RESPONSE_SIZE(len) (6 + len)
35
39typedef struct {
40 const uint8_t *buffer;
41 size_t size;
43
51static 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
65
74static inline bool feetech_reader_check_minsize(const feetech_reader_t *reader)
75{
76 return 5 < reader->size;
77}
78
87static inline bool feetech_reader_check_start(const feetech_reader_t *reader)
88{
89 return
90 reader->buffer[0] == FEETECH_START &&
91 reader->buffer[1] == FEETECH_START;
92}
93
102static inline bool feetech_reader_check_size(const feetech_reader_t *reader)
103{
104 return reader->size == (size_t)(reader->buffer[3] + 4);
105}
106
115static inline bool feetech_reader_check_sum(const feetech_reader_t *reader)
116{
117 return feetech_reader_compute_sum(reader) == reader->buffer[reader->size - 1];
118}
119
129
137static inline uint8_t feetech_reader_get_id(const feetech_reader_t *reader)
138{
139 return reader->buffer[2];
140}
141
149static inline uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
150{
151 return reader->buffer[4];
152}
153
161static inline const uint8_t *feetech_reader_response_get_payload(const feetech_reader_t *reader)
162{
163 return &reader->buffer[5];
164}
165
174{
175 return reader->buffer[3] - 2;
176}
177
185static inline const uint8_t *feetech_reader_write_get_payload(const feetech_reader_t *reader)
186{
187 return &reader->buffer[6];
188}
189
198{
199 return reader->buffer[3] - 3;
200}
201
209static inline uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
210{
211 return reader->buffer[5];
212}
213
221static inline size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
222{
223 return reader->buffer[6];
224}
225
233static inline uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
234{
235 return reader->buffer[5];
236}
237
246{
247 return reader->buffer[6];
248}
249
257static inline uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
258{
259 return reader->buffer[5];
260}
261
270
279uint8_t feetech_reader_sync_write_item_get_id(const feetech_reader_t *reader, uint8_t index);
280
289const uint8_t *feetech_reader_sync_write_item_get_payload(const feetech_reader_t *reader, uint8_t index);
290
291#ifdef __cplusplus
292}
293#endif
294
295#endif /* FEETECH_READER_H */
Feetech protocol definitions.
static uint8_t feetech_reader_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (WRITE)
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 const uint8_t * feetech_reader_write_get_payload(const feetech_reader_t *reader)
Get the packet's payload (WRITE)
size_t feetech_reader_sync_write_get_items_count(const feetech_reader_t *reader)
Get the packet items' count (SYNC_WRITE)
static bool feetech_reader_check_sum(const feetech_reader_t *reader)
Check if the computed sum and the sum of the packet are equal.
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_get_id(const feetech_reader_t *reader)
Get the packet's device id.
static bool feetech_reader_check_start(const feetech_reader_t *reader)
Check if the packet begins with 2 FEETECH_START bits.
bool feetech_reader_is_valid(const feetech_reader_t *reader)
Check if the packet is valid.
static uint8_t feetech_reader_read_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (READ)
static uint8_t feetech_reader_sync_write_get_reg(const feetech_reader_t *reader)
Get the packet's target register address (SYNC_WRITE)
static size_t feetech_reader_read_get_size(const feetech_reader_t *reader)
Get the packet's READ size.
static void feetech_reader_init(feetech_reader_t *reader, const uint8_t *buffer, size_t size)
Initialize the Feetech packet reader.
static const uint8_t * feetech_reader_response_get_payload(const feetech_reader_t *reader)
Get the packet's payload (response)
static uint8_t feetech_reader_get_instr(const feetech_reader_t *reader)
Get the packet's instruction code.
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.
static size_t feetech_reader_response_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (response)
static bool feetech_reader_check_minsize(const feetech_reader_t *reader)
Check if the packet has the minimum required size.
static size_t feetech_reader_write_get_payload_size(const feetech_reader_t *reader)
Get the packet's payload size (WRITE)
uint8_t feetech_reader_compute_sum(const feetech_reader_t *reader)
Compute the packet's sum.
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)
Feetech packet reader struct.
const uint8_t * buffer
data buffer
size_t size
data buffer's size