23 #ifdef HAS_ETHERNETUDP
24 #include "../../interfaces/ARDUINO/UDPHelper_ARDUINO.h"
26 #include "../../interfaces/LINUX/UDPHelper_POSIX.h"
29 #include <PJONDefines.h>
31 #define LUDP_DEFAULT_PORT 7100
32 #ifndef LUDP_RESPONSE_TIMEOUT
33 #define LUDP_RESPONSE_TIMEOUT (uint32_t) 100000
35 #define LUDP_MAGIC_HEADER (uint32_t) 0x0DFAC3D0
38 #ifndef LUDP_RECEIVE_TIME
39 #define LUDP_RECEIVE_TIME 0
44 bool _udp_initialized =
false;
45 uint16_t _port = LUDP_DEFAULT_PORT;
50 if(!_udp_initialized) {
51 udp.set_magic_header(htonl(LUDP_MAGIC_HEADER));
52 if (udp.begin(_port)) {
53 _udp_initialized =
true;
56 return _udp_initialized;
62 uint32_t back_off(uint8_t attempts)
65 return 10000ul * attempts + PJON_RANDOM(10000);
67 return 1000ul * attempts + PJON_RANDOM(1000);
78 bool begin(uint8_t = 0)
94 static uint8_t get_max_attempts()
102 static uint16_t get_receive_time()
104 return LUDP_RECEIVE_TIME;
110 void handle_collision() { };
115 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
117 return udp.receive_frame(
data, max_length);
123 uint16_t receive_response()
128 uint32_t start = PJON_MICROS();
130 uint16_t reply_length = 0;
132 reply_length = receive_frame(result,
sizeof result);
136 if(reply_length == 1)
137 if(result[0] == PJON_ACK) {
140 }
while ((uint32_t)(PJON_MICROS() - start) < LUDP_RESPONSE_TIMEOUT);
148 void send_response(uint8_t response)
150 udp.send_response(response);
156 void send_frame(uint8_t *
data, uint16_t length)
158 udp.send_frame(
data, length);
164 void set_port(uint16_t port = LUDP_DEFAULT_PORT)