Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
usher_protocol.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#include "usher_protocol.h"
20
21/**
22 * @file usher_protocol.c
23 * @brief Incremental parser for USHER's UART framing protocol.
24 * @details The UART transport is a byte stream, so this parser keeps state across
25 * calls and accepts arbitrarily fragmented frames. It validates both Fletcher-style
26 * checksum bytes before publishing a message. On malformed input it discards only
27 * the current partial frame and resumes searching for the next start byte; this is
28 * deliberately resilient to line noise and lost bytes.
29 */
30
32
33
34/** Parser states; each corresponds to the next byte expected on the wire. */
35#define UNINIT 0
36#define GOT_STX 1
37#define GOT_LENGTH 2
38#define GOT_MSGID 3
39#define GOT_PAYLOAD 4
40#define GOT_CRC1 5
41
43
44/**
45 * @brief Consume one UART byte and advance a USHER transport parser.
46 * The public parameter and lifetime contract is documented in usher_protocol.h.
47 * @details Checksums cover the length, message ID, and payload bytes. A complete,
48 * valid frame sets @c t->msg_received; the caller must clear that flag after
49 * dispatching. Any framing or checksum error increments @c t->error and resets the
50 * state machine, avoiding a poisoned partial frame affecting later traffic.
51 * @warning @p t must remain valid and unchanged by another thread for the duration
52 * of a frame; this parser intentionally contains no synchronization.
53 */
54void parse_usher(struct usher_transport *t, uint8_t c)
55{
56//printf("%02X %d %d\n",c, t->status, t->error);
57
58 switch (t->status) {
59 case UNINIT:
60 if (c == STX) {
61 t->status++;
62 }
63 break;
64 case GOT_STX:
65 if (t->msg_received) {
66 t->error++;
67 goto error;
68 }
69 if (c < UsherSizeOf(0)) goto error;
70 t->payload_len = c - 5;
71 t->ck_a = t->ck_b = c;
72 t->status++;
73 t->payload_idx = 0;
74 break;
75 case GOT_LENGTH:
76 t->msg_id = c;
77 t->ck_a += c; t->ck_b += t->ck_a;
78 t->status++;
79 if (t->payload_len == 0) {
80 t->status++;
81 }
82 break;
83 case GOT_MSGID:
84 t->payload[t->payload_idx] = c;
85 t->ck_a += c; t->ck_b += t->ck_a;
86 t->payload_idx++;
87 if (t->payload_idx == t->payload_len) {
88 t->status++;
89 }
90 break;
91 case GOT_PAYLOAD:
92 if (c != t->ck_a) {
93 goto error;
94 }
95 t->status++;
96 break;
97 case GOT_CRC1:
98 if (c != t->ck_b) {
99 goto error;
100 }
101 t->msg_received = true;
102 goto restart;
103 default:
104 goto error;
105 }
106 return;
107error:
108 t->error++;
109restart:
110 t->status = UNINIT;
111 return;
112}
113
struct jpeg_error_mgr error
Definition lwir_image.c:60
Persistent state for incremental USHER UART frame parsing.
uint8_t payload[256]
struct usher_transport usher_protocol
#define GOT_MSGID
#define GOT_STX
#define GOT_PAYLOAD
#define GOT_LENGTH
#define UNINIT
#define GOT_CRC1
uint8_t usher_ck_b
uint8_t usher_ck_a
void parse_usher(struct usher_transport *t, uint8_t c)
Consume one UART byte and advance a USHER transport parser. The public parameter and lifetime contrac...
Shared FC/MORA byte-stream UART framing, messages and payload layouts.
#define STX
#define UsherSizeOf(_payload)