24 #ifdef HAS_ETHERNETUDP
25 #include "../../interfaces/ARDUINO/UDPHelper_ARDUINO.h"
27 #include "../../interfaces/LINUX/UDPHelper_POSIX.h"
30 #include <PJONDefines.h>
33 #ifndef GUDP_RESPONSE_TIMEOUT
34 #define GUDP_RESPONSE_TIMEOUT 100000ul
37 #ifndef GUDP_MAX_REMOTE_NODES
38 #define GUDP_MAX_REMOTE_NODES 10
42 #ifndef GUDP_RECEIVE_TIME
43 #define GUDP_RECEIVE_TIME 0
46 #define GUDP_DEFAULT_PORT 7000
47 #define GUDP_MAGIC_HEADER (uint32_t) 0x0DFAC3FF
51 bool _udp_initialized =
false;
52 uint16_t _port = GUDP_DEFAULT_PORT;
53 bool _auto_registration =
true;
56 uint8_t _remote_node_count = 0;
57 uint8_t _remote_id[GUDP_MAX_REMOTE_NODES];
58 uint8_t _remote_ip[GUDP_MAX_REMOTE_NODES][4];
59 uint16_t _remote_port[GUDP_MAX_REMOTE_NODES];
65 if(!_udp_initialized) {
66 udp.set_magic_header(htonl(GUDP_MAGIC_HEADER));
67 if (udp.begin(_port)) {
68 _udp_initialized =
true;
71 return _udp_initialized;
74 int16_t find_remote_node(uint8_t
id)
76 for(uint8_t i = 0; i < _remote_node_count; i++)
77 if(_remote_id[i] ==
id) {
83 void autoregister_sender(
const uint8_t *message, uint16_t length)
86 if (_auto_registration && length>4) {
89 PJONTools::parse_header(message, packet_info);
90 uint8_t sender_id = packet_info.tx.id;
98 udp.get_sender(sender_ip, sender_port);
101 int16_t pos = find_remote_node(sender_id);
103 add_node(sender_id, sender_ip, sender_port);
106 memcpy(_remote_ip[pos], sender_ip, 4);
107 _remote_port[pos] = sender_port;
118 const uint8_t remote_ip[],
119 uint16_t port_number = GUDP_DEFAULT_PORT
122 if (_remote_node_count == GUDP_MAX_REMOTE_NODES) {
125 _remote_id[_remote_node_count] = remote_id;
126 memcpy(_remote_ip[_remote_node_count], remote_ip, 4);
127 _remote_port[_remote_node_count] = port_number;
128 _remote_node_count++;
129 return _remote_node_count - 1;
135 void set_autoregistration(
bool enabled)
137 _auto_registration = enabled;
143 uint32_t back_off(uint8_t attempts)
146 return 10000ul * attempts + PJON_RANDOM(10000);
149 return 1000ul + PJON_RANDOM(1000);
160 bool begin(uint8_t = 0)
176 static uint8_t get_max_attempts()
184 static uint16_t get_receive_time()
186 return GUDP_RECEIVE_TIME;
192 void handle_collision() { };
197 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
199 uint16_t length = udp.receive_frame(
data, max_length);
200 if (length != PJON_FAIL) {
201 autoregister_sender(
data, length);
209 uint16_t receive_response()
214 uint32_t start = PJON_MICROS();
216 uint16_t reply_length = 0;
218 reply_length = receive_frame(result,
sizeof result);
221 if(reply_length == 1)
222 if (result[0] == PJON_ACK) {
226 }
while ((uint32_t)(PJON_MICROS() - start) < GUDP_RESPONSE_TIMEOUT);
234 void send_response(uint8_t response)
236 udp.send_response(response);
242 void send_frame(uint8_t *
data, uint16_t length)
245 uint8_t
id =
data[0];
247 for(uint8_t pos = 0; pos < _remote_node_count; pos++) {
248 udp.send_frame(
data, length, _remote_ip[pos], _remote_port[pos]);
251 int16_t pos = find_remote_node(
id);
253 udp.send_frame(
data, length, _remote_ip[pos], _remote_port[pos]);
262 void set_port(uint16_t port = GUDP_DEFAULT_PORT)