15 #include "net/uartbridge.h"
20 #define UDP_ESCAPE 0xF5
21 #define EMBED_UDP 0x01
24 static int _inChar = -1;
26 static bool _rxMode =
false;
27 static bool _txMode =
false;
29 static uint8_t _ubRxBuf[BUF_SIZE];
30 static int _ubRxPos = 0;
31 static int _ubRxLen = 0;
32 static int _ubRxTyp = 0;
34 static uint8_t _ubTxBuf[BUF_SIZE];
35 static int _ubTxPos = 0;
36 static int _ubTxLen = 0;
38 static uint8_t _check;
44 if (_inChar == -1)
return -1;
52 static void add2Check(uint8_t v)
56 _check = 0xFF & ( ( _check << 1 ) | ( _check >> 7 ) );
60 static int readChar(
bool wait,
bool a2c)
64 if (!
suartRx(CONSOLE_UART,(
char*)&c))
return -1;
72 static int writeChar(
bool wait, uint8_t c)
74 if (wait)
while (!csuartTxReady(CONSOLE_UART)) { };
75 if (!
suartTx(CONSOLE_UART,c))
return -1;
80 static void ubInterpretRx()
85 for (i = 0; i < _ubRxLen - 1; ++i)
87 add2Check(_ubRxBuf[i]);
89 if (_check != _ubRxBuf[_ubRxLen - 1]) {
98 addr.
ip = ((0xFF & _ubRxBuf[0]) << 24) | ((0xFF & _ubRxBuf[1]) << 16) |
99 ((0xFF & _ubRxBuf[2]) << 8) | (0xFF & _ubRxBuf[3]);
100 addr.
port = ((0xFF & _ubRxBuf[4]) << 8) | (0xFF & _ubRxBuf[5]);
101 _ubRx(&addr, &_ubRxBuf[6], _ubRxLen - 6);
118 if (_ubTxPos >= _ubTxLen) _txMode =
false;
128 _ubRxBuf[_ubRxPos++] = c;
129 if (_ubRxPos >= _ubRxLen ) {
135 if (c == UDP_ESCAPE) {
138 _ubRxTyp = readChar(
true,
true);
139 _ubRxLen = ( ( readChar(
true,
true) << 8 ) | readChar(
true,
true) ) + 1;
140 if (_ubRxLen > BUF_SIZE) {
141 puts(
"INVALID LENGTH");
156 bool ubTx(
SockAddr * addr, uint8_t * data,
int len)
158 if (_txMode)
return false;
160 _ubTxBuf[0] = 0xFF & ( addr->
ip >> 24 );
161 _ubTxBuf[1] = 0xFF & ( addr->
ip >> 16 );
162 _ubTxBuf[2] = 0xFF & ( addr->
ip >> 8 );
163 _ubTxBuf[3] = 0xFF & ( addr->
ip >> 0 );
164 _ubTxBuf[4] = 0xFF & ( addr->
port >> 8 );
165 _ubTxBuf[5] = 0xFF & ( addr->
port >> 0 );
166 memcpy(&_ubTxBuf[6], data, len);
170 writeChar(
true, UDP_ESCAPE);
171 writeChar(
true, EMBED_UDP);
172 writeChar(
true, _ubTxLen >> 8);
173 writeChar(
true, _ubTxLen);
uint16_t port
Port number.
static bool suartTxReady(SUART_Descriptor *desc)
Returns whether or not the TX buffer is empty.
White Rabbit Simple UART Driver.
Combination of IP address and port.
bool suartRx(SUART_Descriptor *desc, char *c)
Receives a character.
static bool suartRxReady(SUART_Descriptor *desc)
Returns whether or not the RX buffer has data.
bool suartTx(SUART_Descriptor *desc, char c)
Transmits a character.