From ccf8e573d5d51a173355972ca3a0827284048e8d Mon Sep 17 00:00:00 2001 From: Jelmer Date: Sun, 15 Jan 2023 11:45:09 +0100 Subject: [PATCH] removed 'synchronized' part of the protocol :( --- esp32_fw/include/commstructs.h | 18 ++-- esp32_fw/include/newproto.h | 2 +- esp32_fw/include/settings.h | 1 + esp32_fw/src/newproto.cpp | 15 +--- esp32_fw/src/serial.cpp | 149 ++------------------------------- 5 files changed, 19 insertions(+), 166 deletions(-) diff --git a/esp32_fw/include/commstructs.h b/esp32_fw/include/commstructs.h index 147a12af..3cac0a35 100644 --- a/esp32_fw/include/commstructs.h +++ b/esp32_fw/include/commstructs.h @@ -12,17 +12,6 @@ struct espXferComplete { uint8_t src[8]; } __packed; -struct espJoinNetwork { - uint8_t checksum; - uint8_t src[8]; -} __packed; - -struct espSaveUpdateBlock { - uint8_t checksum; - uint8_t blockId; - uint16_t blockChecksum; -} __packed; - struct blockData { uint16_t size; uint16_t checksum; @@ -40,6 +29,12 @@ struct AvailDataReq { uint8_t protoVer; } __packed; +struct espAvailDataReq { + uint8_t checksum; + uint8_t src[8]; + struct AvailDataReq adr; +} __packed; + #define DATATYPE_NOUPDATE 0 #define DATATYPE_IMG 1 #define DATATYPE_IMGRAW 2 @@ -56,7 +51,6 @@ struct pendingData { struct AvailDataInfo availdatainfo; uint8_t attemptsLeft; uint8_t targetMac[8]; - uint8_t includedThisBurst : 1; } __packed; diff --git a/esp32_fw/include/newproto.h b/esp32_fw/include/newproto.h index 6990f345..84922d25 100644 --- a/esp32_fw/include/newproto.h +++ b/esp32_fw/include/newproto.h @@ -7,4 +7,4 @@ extern void processBlockRequest(struct espBlockRequest* br); extern bool prepareDataAvail(String* filename, uint8_t dataType, uint8_t* dst); extern void processJoinNetwork(struct espJoinNetwork* xjn); extern void processXferComplete(struct espXferComplete* xfc); -extern void processDataReq(struct AvailDataReq* adr); \ No newline at end of file +extern void processDataReq(struct espAvailDataReq* adr); \ No newline at end of file diff --git a/esp32_fw/include/settings.h b/esp32_fw/include/settings.h index e939cd8d..45b37172 100644 --- a/esp32_fw/include/settings.h +++ b/esp32_fw/include/settings.h @@ -34,6 +34,7 @@ #define RXD1 13 // 1st #define TXD1 12 // 2nd + #define ZBS_SS 21 #define ZBS_CLK 18 #define ZBS_MoSi 22 diff --git a/esp32_fw/src/newproto.cpp b/esp32_fw/src/newproto.cpp index 9cd88b80..1b1585ce 100644 --- a/esp32_fw/src/newproto.cpp +++ b/esp32_fw/src/newproto.cpp @@ -125,15 +125,6 @@ void processBlockRequest(struct espBlockRequest* br) { Serial.printf("blockId); } -void processJoinNetwork(struct espJoinNetwork* xjn) { - char buffer[64]; - uint8_t src[8]; - *((uint64_t*)src) = swap64(*((uint64_t*)xjn->src)); - sprintf(buffer, "< %02X%02X%02X%02X%02X%02X joined the network\n\0", src[2], src[3], src[4], src[5], src[6], src[7]); - wsString((String)buffer); - Serial.print(buffer); -} - void processXferComplete(struct espXferComplete* xfc) { char buffer[64]; uint8_t src[8]; @@ -143,12 +134,12 @@ void processXferComplete(struct espXferComplete* xfc) { Serial.print(buffer); } -void processDataReq(struct AvailDataReq* adr) { +void processDataReq(struct espAvailDataReq* eadr) { char buffer[64]; uint8_t src[8]; // *((uint64_t*)src) = swap64(*((uint64_t*)adr->)); - // sprintf(buffer, "< %02X%02X%02X%02X%02X%02X reports xfer complete\n\0", src[2], src[3], src[4], src[5], src[6], src[7]); - sprintf(buffer, "softVer, adr->batteryMv); + sprintf(buffer, "src[7], eadr->src[6], eadr->src[5], eadr->src[4], eadr->src[3], eadr->src[2], eadr->src[1], eadr->src[0]); + //sprintf(buffer, "softVer, adr->batteryMv); wsString((String)buffer); Serial.print(buffer); } diff --git a/esp32_fw/src/serial.cpp b/esp32_fw/src/serial.cpp index cceeedb9..c3df2624 100644 --- a/esp32_fw/src/serial.cpp +++ b/esp32_fw/src/serial.cpp @@ -118,30 +118,11 @@ void SerialRXLoop() { memset(cmdbuffer, 0x00, 4); restartBlockRequest = 0; } - if (strncmp(cmdbuffer, "RQQ>", 4) == 0) { - /* - Serial.print("BYTES REQUESTED!\n"); - for (uint8_t c = 0; c < 50; c++) { - Serial1.write(0x00); - } - restartBlockRequest++; - if (restartBlockRequest > 10) { - restartBlockRequest = 0; - for (uint8_t c = 0; c < 200; c++) { - Serial1.write(0x00); - Serial1.write(0x00); - Serial1.write(0x00); - Serial1.write(0x00); - } - } - RXState = ZBS_RX_WAIT_HEADER; - */ - } if (strncmp(cmdbuffer, "ADR>", 4) == 0) { RXState = ZBS_RX_WAIT_DATA_REQ; charindex = 0; pktindex = 0; - packetp = (uint8_t*)calloc(sizeof(struct espBlockRequest) + 8, 1); + packetp = (uint8_t*)calloc(sizeof(struct espAvailDataReq) + 8, 1); memset(cmdbuffer, 0x00, 4); } if (strncmp(cmdbuffer, "BST>", 4) == 0) { @@ -155,13 +136,6 @@ void SerialRXLoop() { packetp = (uint8_t*)calloc(sizeof(struct espXferComplete) + 8, 1); memset(cmdbuffer, 0x00, 4); } - if (strncmp(cmdbuffer, "TJN>", 4) == 0) { - RXState = ZBS_RX_WAIT_JOINNETWORK; - pktindex = 0; - packetp = (uint8_t*)calloc(sizeof(struct espJoinNetwork) + 8, 1); - memset(cmdbuffer, 0x00, 4); - } - break; case ZBS_RX_BLOCK_REQUEST: packetp[pktindex] = lastchar; @@ -182,21 +156,11 @@ void SerialRXLoop() { RXState = ZBS_RX_WAIT_HEADER; } break; - case ZBS_RX_WAIT_JOINNETWORK: - packetp[pktindex] = lastchar; - pktindex++; - if (pktindex == sizeof(struct espJoinNetwork)) { - struct espJoinNetwork* ejn = (struct espJoinNetwork*)packetp; - processJoinNetwork(ejn); - free(packetp); - RXState = ZBS_RX_WAIT_HEADER; - } - break; case ZBS_RX_WAIT_DATA_REQ: packetp[pktindex] = lastchar; pktindex++; - if (pktindex == sizeof(struct AvailDataReq)) { - struct AvailDataReq* adr = (struct AvailDataReq*)packetp; + if (pktindex == sizeof(struct espAvailDataReq)) { + struct espAvailDataReq* adr = (struct espAvailDataReq*)packetp; processDataReq(adr); free(packetp); RXState = ZBS_RX_WAIT_HEADER; @@ -227,111 +191,13 @@ void SerialRXLoop() { } } -void sendSaveUpdateBlock(struct espSaveUpdateBlock* esub) { - addCRC(esub, sizeof(struct espSaveUpdateBlock)); - Serial1.print("SUBL"); - for (uint8_t c = 0; c < sizeof(struct espSaveUpdateBlock); c++) { - Serial1.write(((uint8_t*)esub)[c]); - } - Serial1.write(0x00); - Serial1.write(0x00); - Serial1.write(0x00); - Serial1.write(0x00); -} - extern uint8_t* getDataForFile(File* file); -#define STATE_TIMEOUT 0 -#define STATE_PASS 1 -#define STATE_FAIL 2 -void doAPUpdate() { - uint8_t curBlockId = 0; - uint8_t maxBlocks = 16; - uint16_t len = 0; - uint16_t lenRemaining = 65535; - uint8_t state = true; - uint8_t* updateData = nullptr; - // open firmware file - // do AP update block erase - // 1 - - // TODO: some other source for the updates - File file = LittleFS.open("/main.bin"); - lenRemaining = file.size(); - updateData = getDataForFile(&file); - - // determine how many blocks we're gonna use for this update - maxBlocks = lenRemaining / BLOCK_DATA_SIZE; - if (lenRemaining % BLOCK_DATA_SIZE) maxBlocks++; - - uint32_t masterTimeout = millis(); - uint32_t timeout = millis(); - - // begin with the process by erasing the update block on the AP's EEPROM - state = STATE_TIMEOUT; - Serial1.print("ERAS\n"); - while (millis() - timeout < 5000UL) { - ShortRXWaitLoop(); - // wait for 'erase okay' - if (strncmp(cmdbuffer, "EROK", 4) == 0) { - state = STATE_PASS; - break; - } - } - - if (state == STATE_TIMEOUT) { - // Tag didn't respond properly to the 'erase' command, bail out - Serial.print("Failed to erase AP EEPROM data in preparation of FW update, bailing\n"); - return; - } - - // Begin transmitting AP update block data - state = STATE_TIMEOUT; - while (curBlockId < maxBlocks && (millis() - masterTimeout < 30000UL)) { - Serial.printf("Now doing block %d\n", curBlockId); - len = lenRemaining; - if (len > BLOCK_DATA_SIZE) len = BLOCK_DATA_SIZE; - - struct espSaveUpdateBlock esub; - esub.blockId = curBlockId; - // send the block data (to the raw buffer) - esub.blockChecksum = sendBlock(updateData + (curBlockId * BLOCK_DATA_SIZE), len); - - // send the update block-struct; This tells the AP which block it is and double-checks the checksum. - sendSaveUpdateBlock(&esub); - - uint32_t timeout = millis(); - state = STATE_TIMEOUT; - // we told the AP to save the block in a specific spot, now wait for the reply - while (millis() - timeout < 5000UL) { - ShortRXWaitLoop(); - if (strncmp(cmdbuffer, "BLOK", 4) == 0) { - // all good, next block please - state = STATE_PASS; - curBlockId++; - lenRemaining -= BLOCK_DATA_SIZE; - break; - } - if (strncmp(cmdbuffer, "BLFL", 4) == 0) { - state = STATE_FAIL; - break; - } - } - } - if (curBlockId == maxBlocks) { - // update complete! (in theory) - Kick the tag to start the update process - Serial1.print("UPDA\n"); - Serial1.print("UPDA\n"); - Serial1.print("UPDA\n"); - } - // parts left? -} - void zbsRxTask(void* parameter) { Serial1.begin(230400, SERIAL_8N1, RXD1, TXD1); simplePowerOn(); - + Serial1.print("VER?"); while (1) { SerialRXLoop(); @@ -342,10 +208,11 @@ void zbsRxTask(void* parameter) { if (waitingForVersion) { if (millis() > 30000) { waitingForVersion = false; - Serial.printf("We've been waiting for communication from the tag, but got nothing. This is expected if this tag hasn't been flashed yet. We'll try to flash it.\n"); + performDeviceFlash(); + // Serial.printf("We've been waiting for communication from the tag, but got nothing. This is expected if this tag hasn't been flashed yet. We'll try to flash it.\n"); // doAPUpdate(); - // performDeviceFlash(); - // SDAtest(); + //performDeviceFlash(); + // SDAtest(); } } }