summaryrefslogtreecommitdiff
path: root/firmware/src/data_link.c
blob: 330e9737a91fa2fa1b75c2df0f5893ac95bc42ce (plain)
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
#include <string.h>
#include <zephyr/drivers/uart.h>

#include "data_link.h"

ssize_t dl_encode_frame(uint8_t *payload, size_t len, uint8_t *dst) {
	// FIXME check for maximum size

	size_t flen = 8 + 8 + 2 + len + 4;

	memset(dst, 0, flen);
	memcpy((void *)(dst + 18), payload, len);

	return flen;
}

void dl_send_frame(const struct device *const uart_dev, uint8_t *buffer,
                   size_t len) {
	for (size_t i = 0; i < len; i++) {
		uint8_t octet = *(buffer + i);
		switch (octet) {
			case DL_SLIP_END:
				uart_poll_out(uart_dev, DL_SLIP_ESC);
				uart_poll_out(uart_dev, DL_SLIP_ESC_END);
				break;
			case DL_SLIP_ESC:
				uart_poll_out(uart_dev, DL_SLIP_ESC);
				uart_poll_out(uart_dev, DL_SLIP_ESC_ESC);
				break;
			default:
				uart_poll_out(uart_dev, octet);
				break;
		}
	}
	uart_poll_out(uart_dev, DL_SLIP_END);
}