-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbp.h
117 lines (104 loc) · 3.32 KB
/
sbp.h
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
/*
* Copyright (C) 2011-2014 Swift Navigation Inc.
* Contact: Fergus Noble <[email protected]>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifndef LIBSBP_SBP_H
#define LIBSBP_SBP_H
#include "common.h"
/** \addtogroup sbp
* \{ */
/** Return value indicating success. */
#define SBP_OK 0
/** Return value indicating message decoded and callback executed by sbp_process. */
#define SBP_OK_CALLBACK_EXECUTED 1
/** Return value indicating message decoded with no associated callback in sbp_process. */
#define SBP_OK_CALLBACK_UNDEFINED 2
/** Return value indicating an error with the callback (function defined). */
#define SBP_CALLBACK_ERROR -1
/** Return value indicating a CRC error. */
#define SBP_CRC_ERROR -2
/** Return value indicating an error occured whilst sending an SBP message. */
#define SBP_SEND_ERROR -3
/** Return value indicating an error occured because an argument was NULL. */
#define SBP_NULL_ERROR -4
/** Default sender ID. Intended for messages sent from the host to the device. */
#define SBP_SENDER_ID 0x42
/** SBP callback function prototype definition. */
typedef void (*sbp_msg_callback_t)(
u16 sender_id,
u8 len,
u8 msg[],
void* context
);
/** SBP callback node.
* Forms a linked list of callbacks.
* \note Must be statically allocated for use with sbp_register_callback().
*/
typedef struct sbp_msg_callbacks_node {
u16 msg_type; /**< Message ID associated with callback. */
sbp_msg_callback_t cb; /**< Pointer to callback function. */
void* context; /**< Pointer to a context */
struct sbp_msg_callbacks_node*
next; /**< Pointer to next node in list. */
} sbp_msg_callbacks_node_t;
/** State structure for processing SBP messages. */
typedef struct {
enum {
WAITING = 0,
GET_TYPE,
GET_SENDER,
GET_LEN,
GET_MSG,
GET_CRC
} state;
u16 msg_type;
u16 sender_id;
u16 crc;
u8 msg_len;
u8 n_read;
u8 msg_buff[256];
void* io_context;
sbp_msg_callbacks_node_t* sbp_msg_callbacks_head;
} sbp_state_t;
/** \} */
s8 sbp_register_callback(
sbp_state_t* s,
u16 msg_type,
sbp_msg_callback_t cb,
void* context,
sbp_msg_callbacks_node_t* node
);
s8 sbp_remove_callback(sbp_state_t* s, sbp_msg_callbacks_node_t* node);
void sbp_clear_callbacks(sbp_state_t* s);
void sbp_state_init(sbp_state_t* s);
void sbp_state_set_io_context(sbp_state_t* s, void* context);
s8 sbp_process(sbp_state_t* s, u32 (*read)(u8* buff, u32 n, void* context));
s8 sbp_process_payload(
sbp_state_t* s,
u16 sender_id,
u16 msg_type,
u8 msg_len,
u8 payload[]
);
s8 sbp_send_message(
sbp_state_t* s,
u16 msg_type,
u16 sender_id,
u8 len,
u8* payload,
u32 (*write)(u8* buff, u32 n, void* context)
);
#ifdef __cplusplus
}
#endif
#endif /* LIBSBP_SBP_H */