-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitIoTUdp.cpp
57 lines (50 loc) · 1.28 KB
/
CircuitIoTUdp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "CircuitIoTUdp.h"
CircuitIoTUdp::CircuitIoTUdp() {};
void CircuitIoTUdp::setupUdp() {
_udp.begin(UDP_PORT);
Serial.print("Listening on UDP port ");
Serial.println(UDP_PORT);
}
void CircuitIoTUdp::runUdp() {
int packetSize = _udp.parsePacket();
if (packetSize) {
int len = _udp.read(_packet, 255);
if (len > 0)
{
_packet[len] = '\0';
}
processPacket(_packet);
char reply[] = "ok";
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
#if defined(ESP8266)
_udp.write(reply);
#endif
#if defined(ESP32)
int i = 0;
while (reply[i] != 0) _udp.write((uint8_t)reply[i++]);
#endif
_udp.endPacket();
}
}
void CircuitIoTUdp::processPacket(char packet[]){
String payload = String(packet);
if(payload.indexOf("|") > 0){
char *strings[2];
char *ptr = NULL;
byte index = 0;
char* np = (char *) payload.c_str();
ptr = strtok(np, "|");
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, "|");
}
if (udpCallback != NULL){
udpCallback(strings[0], (uint8_t*)strings[1], sizeof(strings[1]));
}
}
}
void CircuitIoTUdp::setUdpCallback(UDP_CALLBACK_SIGNATURE) {
this->udpCallback = udpCallback;
}