diff --git a/ESP32_AP-Flasher/include/udp.h b/ESP32_AP-Flasher/include/udp.h new file mode 100644 index 00000000..53d562c1 --- /dev/null +++ b/ESP32_AP-Flasher/include/udp.h @@ -0,0 +1,18 @@ +#include + +#include "AsyncUDP.h" + +class UDPcomm { + public: + UDPcomm(); + ~UDPcomm(); + void init(); + void send(uint8_t* output); + void processDataReq(struct espAvailDataReq* eadr); + + private: + AsyncUDP udp; + void processPacket(AsyncUDPPacket packet); +}; + +void init_udp(); \ No newline at end of file diff --git a/ESP32_AP-Flasher/src/main.cpp b/ESP32_AP-Flasher/src/main.cpp index 75fa3a4b..d4292dcb 100644 --- a/ESP32_AP-Flasher/src/main.cpp +++ b/ESP32_AP-Flasher/src/main.cpp @@ -17,7 +17,7 @@ #endif #include "web.h" - +#include "udp.h" #include "leds.h" void timeTask(void* parameter) { @@ -66,6 +66,8 @@ void setup() { // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv init_web(); + init_udp(); + loadDB("/current/tagDB.json"); xTaskCreate(zbsRxTask, "zbsRX Process", 10000, NULL, 2, NULL); diff --git a/ESP32_AP-Flasher/src/serial.cpp b/ESP32_AP-Flasher/src/serial.cpp index 149f5cff..bd70ddaa 100644 --- a/ESP32_AP-Flasher/src/serial.cpp +++ b/ESP32_AP-Flasher/src/serial.cpp @@ -5,10 +5,11 @@ #include "commstructs.h" #include "flasher.h" #include "newproto.h" +#include "powermgt.h" #include "settings.h" +#include "udp.h" #include "web.h" #include "zbs_interface.h" -#include "powermgt.h" #define ZBS_RX_WAIT_HEADER 0 #define ZBS_RX_WAIT_PKT_LEN 1 @@ -22,6 +23,8 @@ #define ZBS_RX_WAIT_JOINNETWORK 10 #define ZBS_RX_WAIT_XFERTIMEOUT 11 +extern UDPcomm udpsync; + uint8_t restartBlockRequest = 0; uint16_t sendBlock(const void* data, const uint16_t len) { @@ -196,6 +199,7 @@ void SerialRXLoop() { if (pktindex == sizeof(struct espAvailDataReq)) { struct espAvailDataReq* adr = (struct espAvailDataReq*)packetp; processDataReq(adr); + udpsync.processDataReq(adr); free(packetp); RXState = ZBS_RX_WAIT_HEADER; } diff --git a/ESP32_AP-Flasher/src/udp.cpp b/ESP32_AP-Flasher/src/udp.cpp new file mode 100644 index 00000000..37ca198f --- /dev/null +++ b/ESP32_AP-Flasher/src/udp.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include "AsyncUDP.h" +#include "commstructs.h" +#include "newproto.h" + +#define PKT_AVAIL_DATA_SHORTREQ 0xE3 +#define PKT_AVAIL_DATA_REQ 0xE5 +#define PKT_AVAIL_DATA_INFO 0xE6 +#define PKT_BLOCK_PARTIAL_REQUEST 0xE7 +#define PKT_BLOCK_REQUEST_ACK 0xE9 +#define PKT_BLOCK_REQUEST 0xE4 +#define PKT_BLOCK_PART 0xE8 +#define PKT_XFER_COMPLETE 0xEA +#define PKT_XFER_COMPLETE_ACK 0xEB +#define PKT_CANCEL_XFER 0xEC +#define PKT_PING 0xED +#define PKT_PONG 0xEE + +UDPcomm udpsync; + +void init_udp() { + udpsync.init(); +} + +UDPcomm::UDPcomm() { + // Constructor +} + +UDPcomm::~UDPcomm() { + // Destructor +} + +void UDPcomm::init() { + if (udp.listenMulticast(IPAddress(239, 10, 0, 1), 16033)) { + Serial.print("UDP Listening on IP: "); + Serial.println(WiFi.localIP()); + udp.onPacket([this](AsyncUDPPacket packet) { + this->processPacket(packet); + }); + } +} + +void UDPcomm::send(uint8_t* output) { + udp.writeTo(output, strlen((char*)output), IPAddress(239, 10, 0, 1), 16572); +} + +void UDPcomm::processPacket(AsyncUDPPacket packet) { + if (packet.data()[0] == 0xFD) { + espAvailDataReq* adr = (espAvailDataReq*)&packet.data()[1]; + processDataReq(adr); + } +} + +void UDPcomm::processDataReq(struct espAvailDataReq* eadr) { + uint8_t buffer[sizeof(struct espAvailDataReq) + 1]; + buffer[0] = PKT_AVAIL_DATA_INFO; + memcpy(buffer + 1, eadr, sizeof(struct espAvailDataReq)); + udp.writeTo(buffer, sizeof(buffer), IPAddress(239, 10, 0, 1), 16572); +} +