Skip to content

Commit e30c852

Browse files
committed
Fix SLIP header parsing logic
1 parent 5d76e54 commit e30c852

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

docs/arduino-uart.png

22.6 KB
Loading

firmware/chipignite/polysat/README.md

Whitespace-only changes.

firmware/chipignite/polysat/include/slip.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ typedef enum slip_cmd_t {
4444

4545
/* Represents a SLIP packet header (stored in big-endian/network-order) */
4646
typedef struct __attribute__((packed)) slip_header_t {
47-
/* The length of the payload in bytes */
48-
uint16_t length;
49-
/* The CRC-CCITT-FALSE of the payload */
50-
uint16_t crc;
51-
/* The command code */
52-
uint8_t cmd;
53-
/* The packet ID */
54-
uint16_t id;
47+
/* The length of the payload in bytes */
48+
uint16_t length;
49+
/* The CRC-CCITT-FALSE of the payload */
50+
uint16_t crc;
51+
/* The command code */
52+
uint8_t cmd;
53+
/* The packet ID */
54+
uint16_t id;
5555
} slip_header_t;
5656

5757
/* Represents a SLIP packet */
5858
typedef struct slip_packet_t {
59-
/* The header of the packet */
60-
slip_header_t header;
61-
/* The data payload of the packet (static buffer) */
62-
uint8_t payload[SLIP_MAX_PAYLOAD_LEN];
59+
/* The header of the packet */
60+
slip_header_t header;
61+
/* The data payload of the packet (static buffer) */
62+
uint8_t payload[SLIP_MAX_PAYLOAD_LEN];
6363
} slip_packet_t;
6464

6565
/* Begin function prototype declarations */

firmware/chipignite/polysat/src/polysat.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ void main() {
151151

152152
// Main loop - echo received characters and blink LED and
153153
while (1) {
154-
// // Check if data is available to read
155-
// if (uart_read_available()) {
156-
// // Read and echo back the character
157-
// uint8_t ch = uart_read();
158-
// uart_write(ch);
159-
// slip_send_packet(c, 1, SLIP_CMD_DATA, uart_write);
160-
// }
161-
162154
if (!pulse) {
163155
led_off();
164156
// Set GPIO 33 low (bit 1)

firmware/chipignite/polysat/src/slip.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ void slip_send_packet(const uint8_t* data, uint16_t data_len, uint8_t cmd, void
3131
hdr.length = (uint16_t)data_len;
3232
hdr.crc = crc16_ccitt_false(data, data_len);
3333
hdr.cmd = (slip_cmd_t)cmd;
34-
hdr.id = packet_count++;
34+
35+
/* Check if payload length exceeds buffer size */
36+
if (data_len > SLIP_MAX_PAYLOAD_LEN) { return; }
3537

3638
/* Write header to buffer */
3739
uint8_t* buffer = (uint8_t*)&hdr;
38-
for (uint16_t i = 0; i < sizeof(hdr); i++) { send_byte(buffer[i]); }
40+
for (uint16_t byte = 0; byte < sizeof(hdr); byte++) { send_byte(buffer[byte]); }
41+
42+
/* Update packet count */
43+
packet_count++;
3944

4045
/* SLIP-encode the payload */
4146
for (uint16_t i = 0; i < data_len; i++) {
@@ -84,12 +89,12 @@ uint16_t crc16_ccitt_false(const uint8_t* data, uint16_t len) {
8489
void slip_receive_packet(uint8_t input_byte, slip_packet_t* decoded_packet, uint8_t (*read_byte)(void)) {
8590
if (!decoded_packet || !read_byte) { return; }
8691

87-
/* Read header fields */
88-
decoded_packet->header.length = read_byte() | (read_byte() << 8);
89-
decoded_packet->header.crc = read_byte() | (read_byte() << 8);
90-
decoded_packet->header.cmd = (slip_cmd_t)read_byte();
91-
packet_count = decoded_packet->header.id = read_byte() | (read_byte() << 8);
92-
packet_count++;
92+
/* Read header from buffer */
93+
uint8_t* buffer = (uint8_t*)decoded_packet;
94+
for (uint16_t byte = 0; byte < sizeof(*decoded_packet); byte++) { buffer[byte] = read_byte(); }
95+
96+
/* Update packet count */
97+
packet_count = decoded_packet->header.id + 1;
9398

9499
/* Check if payload length exceeds buffer size */
95100
if (decoded_packet->header.length > SLIP_MAX_PAYLOAD_LEN) {

0 commit comments

Comments
 (0)