Loading...
Searching...
No Matches
BindAsyncUDP.h
1/*
2 * BindAsyncUDP.h - Async UDP library only for ESP8266!
3 * Based on ESPAsyncUDP.h - Async UDP library for ESP8266
4 * By me-no-dev <https://github.com/me-no-dev/ESPAsyncUDP>
5 * Modified by H1Jam <https://github.com/H1jam/Bind>
6*/
7#ifndef ESPASYNCUDP_H
8#define ESPASYNCUDP_H
9
10#if __has_include("BindUserConfig.h")
11#include "BindUserConfig.h"
12#endif
13
14#if defined(ARDUINO_ARCH_ESP8266) && !defined(BIND_DISABLE_WIFI)
15#include "IPAddress.h"
16#include "Print.h"
17#include <functional>
18#include "lwip/init.h"
19
20 class AsyncUDP;
21 class AsyncUDPPacket;
22 class AsyncUDPMessage;
23 struct udp_pcb;
24 struct pbuf;
25#if LWIP_VERSION_MAJOR == 1
26 struct ip_addr;
27 typedef struct ip_addr ip_addr_t;
28#else
29 struct ip4_addr;
30 typedef struct ip4_addr ip_addr_t;
31#endif
32
33 class AsyncUDPMessage : public Print
34{
35 protected:
36 uint8_t *_buffer;
37 size_t _index;
38 size_t _size;
39 public:
40 AsyncUDPMessage(size_t size=1460);
41 virtual ~AsyncUDPMessage();
42 size_t write(const uint8_t *data, size_t len);
43 size_t write(uint8_t data);
44 size_t space();
45 uint8_t * data();
46 size_t length();
47 void flush();
48 operator bool()
49 {
50 return _buffer != NULL;
51 }
52};
53
54 class AsyncUDPPacket : public Print
55{
56 protected:
57 AsyncUDP *_udp;
58 ip_addr_t *_localIp;
59 uint16_t _localPort;
60 ip_addr_t *_remoteIp;
61 uint16_t _remotePort;
62 uint8_t *_data;
63 size_t _len;
64 public:
65 AsyncUDPPacket(AsyncUDP *udp, ip_addr_t *localIp, uint16_t localPort, ip_addr_t *remoteIp, uint16_t remotePort, uint8_t *data, size_t len);
66 virtual ~AsyncUDPPacket();
67
68 uint8_t * data();
69 size_t length();
70 bool isBroadcast();
71 bool isMulticast();
72
73 IPAddress localIP();
74 uint16_t localPort();
75 IPAddress remoteIP();
76 uint16_t remotePort();
77
78 size_t send(AsyncUDPMessage &message);
79
80 size_t write(const uint8_t *data, size_t len);
81 size_t write(uint8_t data);
82};
83
84 typedef std::function<void(AsyncUDPPacket& packet)> AuPacketHandlerFunction;
85 typedef std::function<void(void * arg, AsyncUDPPacket& packet)> AuPacketHandlerFunctionWithArg;
86
87 class AsyncUDP : public Print
88{
89 protected:
90 udp_pcb *_pcb;
91 bool _connected;
92 AuPacketHandlerFunction _handler;
93
94 void _recv(udp_pcb *upcb, pbuf *pb, ip_addr_t *addr, uint16_t port);
95#if LWIP_VERSION_MAJOR == 1
96 static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, ip_addr_t *addr, uint16_t port);
97#else
98 static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port);
99#endif
100
101 public:
102 AsyncUDP();
103 virtual ~AsyncUDP();
104
105 void onPacket(AuPacketHandlerFunctionWithArg cb, void * arg=NULL);
106 void onPacket(AuPacketHandlerFunction cb);
107
108 bool listen(ip_addr_t *addr, uint16_t port);
109 bool listen(const IPAddress addr, uint16_t port);
110 bool listen(uint16_t port);
111
112 bool listenMulticast(ip_addr_t *addr, uint16_t port, uint8_t ttl=1);
113 bool listenMulticast(const IPAddress addr, uint16_t port, uint8_t ttl=1);
114
115 bool connect(ip_addr_t *addr, uint16_t port);
116 bool connect(const IPAddress addr, uint16_t port);
117
118 void close();
119
120 size_t writeTo(const uint8_t *data, size_t len, ip_addr_t *addr, uint16_t port);
121 size_t writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port);
122 size_t write(const uint8_t *data, size_t len);
123 size_t write(uint8_t data);
124
125 size_t broadcastTo(uint8_t *data, size_t len, uint16_t port);
126 size_t broadcastTo(const char * data, uint16_t port);
127 size_t broadcast(uint8_t *data, size_t len);
128 size_t broadcast(const char * data);
129
130 size_t sendTo(AsyncUDPMessage &message, ip_addr_t *addr, uint16_t port);
131 size_t sendTo(AsyncUDPMessage &message, const IPAddress addr, uint16_t port);
132 size_t send(AsyncUDPMessage &message);
133
134 size_t broadcastTo(AsyncUDPMessage &message, uint16_t port);
135 size_t broadcast(AsyncUDPMessage &message);
136
137 bool connected();
138 operator bool();
139};
140
141#endif // defined(ARDUINO_ARCH_ESP8266)
142#endif /* ESPASYNCUDP_H */