Skip to content

Commit cc24225

Browse files
committed
mod: parsing packet header according to arduino/arduino-router#3
1 parent 1825fee commit cc24225

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/udp_bridge.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@ This file is part of the Arduino_RouterBridge library.
1818
#define UDP_CONNECT_MULTI_METHOD "udp/connectMulticast"
1919
#define UDP_CLOSE_METHOD "udp/close"
2020
#define UDP_WRITE_METHOD "udp/write"
21+
#define UDP_AWAIT_READ_METHOD "udp/awaitRead"
2122
#define UDP_READ_METHOD "udp/read"
2223

2324
#include <api/Udp.h>
2425

2526
#define DEFAULT_UDP_BUF_SIZE 4096
2627

2728

29+
struct BridgeUdpMeta {
30+
MsgPack::str_t host;
31+
uint16_t port;
32+
uint16_t size;
33+
34+
BridgeUdpMeta() {
35+
host = "";
36+
port = 0;
37+
size = 0;
38+
}
39+
40+
MSGPACK_DEFINE(size, host, port); // -> [code, traceback]
41+
};
42+
2843
template<size_t BufferSize=DEFAULT_UDP_BUF_SIZE>
2944
class BridgeUDP final: public UDP {
3045

@@ -45,6 +60,7 @@ class BridgeUDP final: public UDP {
4560
IPAddress _remoteIP{}; // remote IP address for the incoming packet whilst it's being processed
4661
uint16_t _remotePort{}; // remote port for the incoming packet whilst it's being processed
4762
uint16_t _remaining{}; // remaining bytes of incoming packet yet to be processed
63+
BridgeUdpMeta packet_meta{};
4864

4965
public:
5066

@@ -95,8 +111,8 @@ class BridgeUDP final: public UDP {
95111
void stop() override {
96112
k_mutex_lock(&udp_mutex, K_FOREVER);
97113

98-
String msg;
99114
if (_connected) {
115+
String msg;
100116
_connected = !bridge->call(UDP_CLOSE_METHOD, connection_id).result(msg);
101117
}
102118

@@ -156,16 +172,16 @@ class BridgeUDP final: public UDP {
156172
while (_remaining) read(); // ensure previous packet is read
157173

158174
int out = 0;
159-
if (available() >= 8) {
160-
uint8_t tmpBuf[8];
161-
for (size_t i = 0; i < 8; ++i) {
162-
tmpBuf[i] = temp_buffer.read_char();
175+
176+
RpcResult async_res = bridge->call(UDP_AWAIT_READ_METHOD, connection_id, read_timeout);
177+
const bool ret = _connected && async_res.result(packet_meta);
178+
179+
if (ret) {
180+
if (!_remoteIP.fromString(packet_meta.host)) {
181+
_remoteIP.fromString("0.0.0.0");
163182
}
164-
_remoteIP = tmpBuf;
165-
_remotePort = tmpBuf[4];
166-
_remotePort = (_remotePort << 8) + tmpBuf[5];
167-
_remaining = tmpBuf[6];
168-
_remaining = (_remaining << 8) + tmpBuf[7];
183+
_remotePort = packet_meta.port;
184+
_remaining = packet_meta.size;
169185
out = _remaining;
170186
}
171187

0 commit comments

Comments
 (0)