Skip to content

Commit 8458e6c

Browse files
authored
Split sbp_process() into two functions. (#444)
Add `sbp_process_payload()` which allows directly processing a payload.
1 parent 81da5c0 commit 8458e6c

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

c/include/libsbp/sbp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ void sbp_clear_callbacks(sbp_state_t* s);
7979
void sbp_state_init(sbp_state_t *s);
8080
void sbp_state_set_io_context(sbp_state_t *s, void* context);
8181
s8 sbp_process(sbp_state_t *s, u32 (*read)(u8 *buff, u32 n, void* context));
82+
s8 sbp_process_payload(sbp_state_t *s, u16 sender_id, u16 msg_type, u8 msg_len,
83+
u8 payload[]);
8284
s8 sbp_send_message(sbp_state_t *s, u16 msg_type, u16 sender_id, u8 len, u8 *payload,
8385
u32 (*write)(u8 *buff, u32 n, void* context));
8486

c/src/sbp.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,8 @@ s8 sbp_process(sbp_state_t *s, u32 (*read)(u8 *buff, u32 n, void *context))
372372
if (s->crc == crc) {
373373

374374
/* Message complete, process it. */
375-
s8 ret = SBP_OK_CALLBACK_UNDEFINED;
376-
sbp_msg_callbacks_node_t* node;
377-
for (node = s->sbp_msg_callbacks_head; node; node = node->next) {
378-
if (node->msg_type == s->msg_type) {
379-
(*node->cb)(s->sender_id, s->msg_len, s->msg_buff, node->context);
380-
ret = SBP_OK_CALLBACK_EXECUTED;
381-
}
382-
}
375+
s8 ret = sbp_process_payload(s, s->sender_id, s->msg_type, s->msg_len,
376+
s->msg_buff);
383377
return ret;
384378
} else {
385379
return SBP_CRC_ERROR;
@@ -395,6 +389,33 @@ s8 sbp_process(sbp_state_t *s, u32 (*read)(u8 *buff, u32 n, void *context))
395389
return SBP_OK;
396390
}
397391

392+
/** Directly process a SBP message.
393+
* If a SBP message has already been decoded (for example, from a binary
394+
* stream or from a JSON log file) use this function to directly process it.
395+
*
396+
* \param s State structure
397+
* \param sender_id SBP message sender id
398+
* \param msg_type SBP message type
399+
* \param msg_len SBP message length
400+
* \param payload SBP message payload
401+
* \return `SBP_OK_CALLBACK_EXECUTED` (1) if message decoded and callback executed,
402+
* `SBP_OK_CALLBACK_UNDEFINED` (2) if message decoded with no associated
403+
* callback.
404+
*/
405+
s8 sbp_process_payload(sbp_state_t *s, u16 sender_id, u16 msg_type, u8 msg_len,
406+
u8 payload[]) {
407+
s8 ret = SBP_OK_CALLBACK_UNDEFINED;
408+
sbp_msg_callbacks_node_t *node;
409+
for (node = s->sbp_msg_callbacks_head; node; node = node->next) {
410+
if (node->msg_type == msg_type) {
411+
(*node->cb)(sender_id, msg_len, payload, node->context);
412+
ret = SBP_OK_CALLBACK_EXECUTED;
413+
}
414+
}
415+
return ret;
416+
}
417+
418+
398419
/** Send SBP messages.
399420
* Takes an SBP message payload, type and sender ID then writes a message to
400421
* the output stream using the supplied `write` function with the correct

0 commit comments

Comments
 (0)