minipix_uart_interface
A library and examples for the MiniPIX UART interface
llcp.h
Go to the documentation of this file.
1 #ifndef LLCP_H
2 #define LLCP_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdbool.h>
11 
12 // | ------------------- BEGIN: USER CONFIG ------------------- |
13 
14 // If LLCP_COMM_HEXADECIMAL == 1, the protocol encodes data into HEXADECIMAL
15 // ASCII characters, which do not spooke serial line drivers. It is also
16 // much easier to debug, if all the data is human-readible.
17 #ifndef LLCP_COMM_HEXADECIMAL
18 #define LLCP_COMM_HEXADECIMAL 0
19 #endif
20 
21 // should we send '\n' after each packet?
22 #ifndef LLCP_APPEND_ENDL
23 #define LLCP_APPEND_ENDL 0
24 #endif
25 
26 #ifndef LLCP_DEBUG_PRINT
27 #define LLCP_DEBUG_PRINT 0
28 #endif
29 
30 // max payload length in bytes, max 255
31 #define MAX_PAYLOAD_LEN 255
32 
33 #ifndef LLCP_CHECK_CHECKSUM
34 #define LLCP_CHECK_CHECKSUM 0
35 #endif
36 
37 // | -------------------- END: USER CONFIG -------------------- |
38 
39 /* automatically-defined configs //{ */
40 
41 // definition of the byte lenghts of the various section of the comm message
42 #define INIT_LEN 1
43 #define PAYLOAD_SIZE_LEN 1
44 #define CHECKSUM_LEN 1
45 
46 #if LLCP_APPEND_ENDL == 1
47 #define ENDL_LEN 1
48 #else
49 #define ENDL_LEN 0
50 #endif
51 
52 // the total max size of the message defines the size of the buffer we need
53 #if LLCP_COMM_HEXADECIMAL == 0
54 #define LLCP_RX_TX_BUFFER_SIZE (INIT_LEN + PAYLOAD_SIZE_LEN + MAX_PAYLOAD_LEN + CHECKSUM_LEN + ENDL_LEN)
55 #else
56 #define LLCP_RX_TX_BUFFER_SIZE (INIT_LEN + (PAYLOAD_SIZE_LEN + MAX_PAYLOAD_LEN + CHECKSUM_LEN) * 2 + ENDL_LEN)
57 #endif
58 
59 #if LLCP_DEBUG_PRINT == 1
60 #include <stdio.h>
61 #endif
62 
63 //}
64 
65 // | ------------------------- structs ------------------------ |
66 
67 /* struct LLCP_ReceiverState_t //{ */
68 
69 #if LLCP_COMM_HEXADECIMAL == 0
70 
73 typedef enum
74 {
80 #else
81 
84 typedef enum
85 {
87  EXPECTING_SIZE = 1,
88  EXPECTING_SIZE_2 = 2,
91  EXPECTING_CHECKSUM_2 = 5,
93 #endif
94 
95 //}
96 
97 /* struct LLCP_Receiver_t //{ */
98 
99 #if LLCP_COMM_HEXADECIMAL == 0
100 typedef struct __attribute__((packed))
101 {
102  LLCP_ReceiverState_t state;
103  uint16_t payload_size;
104  uint8_t rx_buffer[LLCP_RX_TX_BUFFER_SIZE];
105  uint16_t buffer_counter;
106  uint8_t checksum;
108 #else
109 typedef struct __attribute__((packed))
110 {
111  LLCP_ReceiverState_t state;
112  uint16_t payload_size;
113  uint8_t rx_buffer[LLCP_RX_TX_BUFFER_SIZE];
114  uint16_t buffer_counter;
115  uint8_t checksum;
116  uint8_t hexmem; // for remembering the first one-byt of the 2-byte HEX byte
118 #endif
119 
120 //}
121 
122 /* struct LLCP_Message_t //{ */
123 
124 typedef struct __attribute__((packed))
125 {
126  uint8_t id;
127  uint8_t payload[MAX_PAYLOAD_LEN];
128  uint8_t checksum_matched;
130 
131 //}
132 
133 // | ----------------------- hex <-> bin ---------------------- |
134 
135 uint8_t llcp_hex2bin(const uint8_t* ptr);
136 
137 void llcp_bin2hex(const uint8_t byte, uint8_t* buffer);
138 
139 // | ------------------------ routines ------------------------ |
140 
141 void llcp_initialize(LLCP_Receiver_t* receiver);
142 
143 bool llcp_processChar(const uint8_t char_in, LLCP_Receiver_t* receiver, LLCP_Message_t** message, bool* checksum_matched);
144 
145 uint16_t llcp_prepareMessage(uint8_t* what, uint8_t len, uint8_t* buffer);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif // LLCP_H
llcp_processChar
bool llcp_processChar(const uint8_t char_in, LLCP_Receiver_t *receiver, LLCP_Message_t **message, bool *checksum_matched)
Definition: llcp.c:67
MAX_PAYLOAD_LEN
#define MAX_PAYLOAD_LEN
Definition: llcp.h:31
LLCP_Message_t
LLCP_Message_t
Definition: llcp.h:129
EXPECTING_PAYLOAD
@ EXPECTING_PAYLOAD
Definition: llcp.h:77
__attribute__
struct __attribute__((packed))
Structure for interpreting the ToA and ToT mode can be used only after derandomization of the data.
Definition: llcp.h:100
EXPECTING_SIZE
@ EXPECTING_SIZE
Definition: llcp.h:76
llcp_hex2bin
uint8_t llcp_hex2bin(const uint8_t *ptr)
Definition: llcp.c:7
EXPECTING_CHECKSUM
@ EXPECTING_CHECKSUM
Definition: llcp.h:78
llcp_initialize
void llcp_initialize(LLCP_Receiver_t *receiver)
Definition: llcp.c:56
llcp_prepareMessage
uint16_t llcp_prepareMessage(uint8_t *what, uint8_t len, uint8_t *buffer)
Definition: llcp.c:251
llcp_bin2hex
void llcp_bin2hex(const uint8_t byte, uint8_t *buffer)
convert a single byte to hexadecimal
Definition: llcp.c:39
WAITING_FOR_MESSSAGE
@ WAITING_FOR_MESSSAGE
Definition: llcp.h:75
LLCP_RX_TX_BUFFER_SIZE
#define LLCP_RX_TX_BUFFER_SIZE
Definition: llcp.h:54
LLCP_ReceiverState_t
LLCP_ReceiverState_t
state machine states for the LLCP receiver (binary transfer)
Definition: llcp.h:73
LLCP_Receiver_t
LLCP_Receiver_t
Definition: llcp.h:107