Repository navigation
Expand file tree
/
Copy pathslipmux.c
More file actions
199 lines (178 loc) · 4.92 KB
/
Copy pathslipmux.c
File metadata and controls
199 lines (178 loc) · 4.92 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include "github.com/lobaro/util-ringbuf/drv_ringbuf.h"
#include "github.com/lobaro/c-utils/lobaroAssert.h"
#include "github.com/lobaro/c-utils/logging.h"
#include "slip.h"
#include "FreeRTOS.h"
#include "task.h"
#include "fcs16.h"
static SemaphoreHandle_t txSemaphore = NULL;
static SemaphoreHandle_t rxSemaphore = NULL;
static void takeSemaphore(SemaphoreHandle_t sem) {
if (sem != NULL) {
if (!xSemaphoreTake(sem, pdMS_TO_TICKS(1000))) {
lobaroASSERT(false);
}
}
}
static void giveSemaphore(SemaphoreHandle_t sem) {
if (sem != NULL) {
if (!xSemaphoreGive(sem)) {
lobaroASSERT(false);
}
}
}
void slipmux_setSemaphores(SemaphoreHandle_t rxSem, SemaphoreHandle_t txSem) {
rxSemaphore = rxSem;
txSemaphore = txSem;
}
/* SEND_PACKET: sends a packet of length "len", starting at
* location "p".
*/
void slipmux_send_packet(const uint8_t* p, int len, uint8_t type, void (* send_char)(char c)) {
takeSemaphore(txSemaphore);
/* send an initial END character to flush out any data that may
* have accumulated in the receiver due to line noise
*/
send_char(SLIP_END);
// TODO: Skip type for IP packets
send_char(type);
uint16_t fcs = 0;
if (type == SLIPMUX_COAP) {
uint8_t frameType = SLIPMUX_COAP;
fcs = CalcFcs16(&frameType, 1); // Include the frameType into the FCS
fcs = CalcFcs16WithInit(fcs, p, len);
}
slip_encode(p, len, send_char);
// Append checksum
if (type == SLIPMUX_COAP) {
fcs ^= 0xffff; // Complement
uint8_t checkLsb = (uint8_t) fcs;
uint8_t checkMsb = (uint8_t) (fcs >> 8);
slip_encode(&checkLsb, 1, send_char); // least significant byte first
slip_encode(&checkMsb, 1, send_char);
}
/* tell the receiver that we're done sending the packet
*/
send_char(SLIP_END);
giveSemaphore(txSemaphore);
}
/* RECV_PACKET: reads a packet from buf into the buffer located at "p".
* If more than len bytes are received, the packet will
* be truncated.
* type must be 0 for new packets. If a partial packet was received, set type to that type != 0
* type will be set to the type of the packet, which is the first byte in SLIPMUX
* Returns the number of bytes stored in the buffer.
*/
int slipmux_read_packet(volatile slipBuffer_t* buf, uint8_t *p, int len, uint8_t* pType) {
char c;
int received = 0;
bool first = (*pType == 0);
takeSemaphore(rxSemaphore);
if (buf->packetCnt == 0) {
giveSemaphore(rxSemaphore);
return 0;
}
/* sit in a loop reading bytes until we put together
* a whole packet.
* Make sure not to copy them into the packet if we
* run out of room.
*/
while (1) {
/* get a character to process
*/
if (isBufferEmpty(&(buf->ringBuf))) {
if (buf->packetCnt != 0) {
Log("Buffer is empty: start: %d, end: %d - but packetCnt = %d", buf->ringBuf.start,buf->ringBuf.end, buf->packetCnt);
}
configASSERT(buf->packetCnt == 0);
giveSemaphore(rxSemaphore);
return received;
}
//taskENTER_CRITICAL();
configASSERT(!isBufferFull(&(buf->ringBuf)));
drv_rbuf_read(&(buf->ringBuf), &c);
//taskEXIT_CRITICAL();
/* handle bytestuffing if necessary
*/
switch (c) {
/* if it's an END character then we're done with
* the packet
*/
case SLIP_END:
/* a minor optimization: if there is no
* data in the packet, ignore it. This is
* meant to avoid bothering IP with all
* the empty packets generated by the
* duplicate END characters which are in
* turn sent to try to detect line noise.
*/
if (received) {
taskENTER_CRITICAL();
buf->packetCnt--;
taskEXIT_CRITICAL();
if (*pType == SLIPMUX_COAP && received >= 2) {
received -= 2; // Remove crc
}
giveSemaphore(rxSemaphore);
return received;
}
else {
// Ignore empty packets
first = true;
break;
}
/* if it's the same code as an ESC character, wait
* and get another character and then figure out
* what to store in the packet based on that.
*/
case SLIP_ESC:
if (isBufferEmpty(&(buf->ringBuf))) {
configASSERT(buf->packetCnt == 0);
giveSemaphore(rxSemaphore);
return received;
}
//taskENTER_CRITICAL();
configASSERT(!isBufferFull(&(buf->ringBuf)));
drv_rbuf_read(&(buf->ringBuf), &c);
//taskEXIT_CRITICAL();
/* if "c" is not one of these two, then we
* have a protocol violation. The best bet
* seems to be to leave the byte alone and
* just stuff it into the packet
*/
switch (c) {
case SLIP_ESC_END:
c = SLIP_END;
break;
case SLIP_ESC_ESC:
c = SLIP_ESC;
break;
}
// Store the character
if (received < len) {
if (first) {
// TODO: Do not skip IPv4 and IPv6 first bytes
*pType = c;
first = false;
} else {
p[received++] = c;
}
}
break;
default:
// Store the character
if (received < len) {
if (first) {
*pType = c;
first = false;
} else {
p[received++] = c;
}
}
}
}
giveSemaphore(rxSemaphore);
}