-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathslip.c
More file actions
108 lines (81 loc) · 1.79 KB
/
slip.c
File metadata and controls
108 lines (81 loc) · 1.79 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "slip.h"
#include "ip.h"
uint8_t *slip_rx_buffer;
uint8_t *slip_tx_buffer;
uint16_t slip_rx_length;
uint8_t slip_rx_escaped;
uint8_t slip_tx_sent;
void slip_init(void) {
slip_rx_buffer = slip_buffer_alloc();
slip_tx_buffer = slip_buffer_alloc();
slip_rx_length = 0;
slip_rx_escaped = 0;
}
void slip_reset(void) {
slip_rx_length = 0;
slip_rx_escaped = 0;
}
uint8_t slip_rx_decode(uint8_t b) {
if (b == SLIP_END) {
if (slip_rx_length > 0) {
return SLIP_DECODE_DONE;
}
return SLIP_DECODE_SKIP;
}
if (b == SLIP_ESC) {
slip_rx_escaped = 1;
return SLIP_DECODE_OK;
}
if (slip_rx_escaped) {
b = (b == SLIP_ESC_END) ? SLIP_END : SLIP_ESC;
slip_rx_escaped = 0;
}
slip_rx_buffer[slip_rx_length++] = b;
if (slip_rx_length >= SLIP_MAX) {
return SLIP_DECODE_RST;
}
return SLIP_DECODE_OK;
}
void slip_rx(void) {
uint8_t c;
uint8_t status;
while (1) {
c = bdos(CPM_RRDR, 0);
status = slip_rx_decode(c);
if (status == SLIP_DECODE_DONE) {
slip_tx_sent = 0;
ip_rx((struct ip_hdr *)slip_rx_buffer);
if (!slip_tx_sent) {
slip_tx(NULL, 0);
}
slip_reset();
return;
} else if (status == SLIP_DECODE_RST) {
slip_reset();
return;
}
}
}
void slip_tx(uint8_t *buffer, uint16_t len) {
uint16_t i;
slip_tx_sent = 1;
bdos(CPM_WPUN, SLIP_END);
for (i = 0; i < len; i++) {
switch (buffer[i]) {
case SLIP_END:
bdos(CPM_WPUN, SLIP_ESC);
bdos(CPM_WPUN, SLIP_ESC_END);
break;
case SLIP_ESC:
bdos(CPM_WPUN, SLIP_ESC);
bdos(CPM_WPUN, SLIP_ESC_ESC);
break;
default:
bdos(CPM_WPUN, buffer[i]);
}
}
bdos(CPM_WPUN, SLIP_END);
}