diff --git a/ap_fw/main.c b/ap_fw/main.c index 06fcbf3a..30380800 100644 --- a/ap_fw/main.c +++ b/ap_fw/main.c @@ -133,7 +133,6 @@ struct blockRequest { struct blockRequestAck { uint8_t checksum; - uint16_t blockSizeMs; uint16_t pleaseWaitMs; uint8_t cancelXfer; } __packed; @@ -167,7 +166,7 @@ struct espSaveUpdateBlock { } __packed; #define TIMER_TICKS_PER_MS 1333UL -uint16_t __xdata version = 0x001D; +uint16_t __xdata version = 0x0006; #define RAW_PKT_PADDING 2 static uint8_t __xdata mRxBuf[COMMS_MAX_PACKET_SZ]; @@ -243,22 +242,6 @@ void dump(uint8_t *__xdata a, uint16_t __xdata l) { } pr("\n"); } -uint8_t getBlockDataLength() { - uint8_t partNo = 0; - uint8_t rounds = 0; - while (partNo < BLOCK_MAX_PARTS) { - for (uint8_t c = 0; (c < BLOCK_MAX_PARTS) && (partNo < BLOCK_MAX_PARTS); c++) { - if (requestedData.requestedParts[c / 8] & (1 << (c % 8))) { - partNo++; - } - } - rounds++; - if (rounds == 4) { - // return partNo; // REMOVE ME - } - } - return partNo; -} uint8_t __xdata getPacketType(void *__xdata buffer) { struct MacFcs *__xdata fcs = buffer; if ((fcs->frameType == 1) && (fcs->destAddrType == 2) && (fcs->srcAddrType == 3) && (fcs->panIdCompressed == 0)) { @@ -411,22 +394,20 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) { struct blockRequest *__xdata blockReq = (struct blockRequest *)(buffer + sizeof(struct MacFrameNormal) + 1); if (!checkCRC(blockReq, sizeof(struct blockRequest))) return; // todo: actually do something with the block request - // uint32_t __xdata curTimerValue = *t; bool __xdata requestDataDownload = false; - // if ((blockReq->blockId != requestedData.blockId) || (blockReq->ver != requestedData.ver)) { if ((blockReq->blockId != requestedData.blockId) || (!u64_isEq((const uint64_t __xdata *)&blockReq->ver, (const uint64_t __xdata *)&requestedData.ver))) { // requested block isn't already in the buffer requestDataDownload = true; } else { // requested block is already in the buffer - //if (forceBlockDownload) { - if ((timerGet() - nextBlockAttempt) > (200 * TIMER_TICKS_PER_MS)) { + if (forceBlockDownload) { + if ((timerGet() - nextBlockAttempt) > (380 * TIMER_TICKS_PER_MS)) { requestDataDownload = true; - serialBypassActive = false; + pr("FORCED\n"); } else { - pr("NOT DOWNLOADING THIS BLOCK AGAIN\n"); + pr("IGNORED\n"); } - //} + } } // copy blockrequest into requested data @@ -437,37 +418,23 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) { radiotxbuffer[0] = sizeof(struct MacFrameNormal) + 1 + sizeof(struct blockRequestAck) + RAW_PKT_PADDING; radiotxbuffer[sizeof(struct MacFrameNormal) + 1] = PKT_BLOCK_REQUEST_ACK; - /* // REMOVE ME! - if (blockStartTimer == 0) { - if (requestDataDownload) { - // check if we need to download the first block; we need to give the ESP32 some additional time to cache the file - if (blockReq->blockId == 0) { - blockRequestAck->pleaseWaitMs = 200; - } else { - blockRequestAck->pleaseWaitMs = 100; - } - blockStartTimer = timerGet() + blockRequestAck->pleaseWaitMs * TIMER_TICKS_PER_MS; + if (blockStartTimer == 0) { + if (requestDataDownload) { + // check if we need to download the first block; we need to give the ESP32 some additional time to cache the file + if (blockReq->blockId == 0) { + blockRequestAck->pleaseWaitMs = 200; } else { - // block is already in buffer - blockRequestAck->pleaseWaitMs = 50; - blockStartTimer = timerGet() + blockRequestAck->pleaseWaitMs * TIMER_TICKS_PER_MS; + blockRequestAck->pleaseWaitMs = 100; } } else { - blockRequestAck->pleaseWaitMs = (blockStartTimer - timerGet()) / TIMER_TICKS_PER_MS; - if (blockRequestAck->pleaseWaitMs < 50) { - blockRequestAck->pleaseWaitMs = 50; - blockStartTimer = timerGet() + blockRequestAck->pleaseWaitMs * TIMER_TICKS_PER_MS; - } + // block is already in buffer + blockRequestAck->pleaseWaitMs = 50; } - */ - - // REMOVE THIS BLOCK - blockRequestAck->pleaseWaitMs = 100; + } else { + blockRequestAck->pleaseWaitMs = 50; + } blockStartTimer = timerGet() + blockRequestAck->pleaseWaitMs * TIMER_TICKS_PER_MS; - blockRequestAck->blockSizeMs = 500; - // REMOVE ME! - // blockRequestAck->blockSizeMs = 15 + 15 + (getBlockDataLength() * 245) / BLOCK_MAX_PARTS; // uncomment me! blockRequestAck->cancelXfer = 0; memcpy(txHeader->src, mSelfMac, 8); @@ -486,11 +453,11 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) { radioTx(radiotxbuffer); // radioTx(radiotxbuffer); - // radioTx(radiotxbuffer); // pr("req blockreq: %02X%02X%02X%02X%02X%02X%02X%02X\n", ((uint8_t *)&(blockReq->ver))[0], ((uint8_t *)&(blockReq->ver))[1], ((uint8_t *)&(blockReq->ver))[2], ((uint8_t *)&(blockReq->ver))[3], ((uint8_t *)&(blockReq->ver))[4], ((uint8_t *)&(blockReq->ver))[5], ((uint8_t *)&(blockReq->ver))[6], ((uint8_t *)&(blockReq->ver))[7]); if (requestDataDownload) { + serialBypassActive = false; espBlockRequest(&requestedData); nextBlockAttempt = timerGet(); } @@ -547,6 +514,7 @@ void processAvailDataReq(uint8_t *buffer) { txHeader->seq = seq++; addCRC(availDataInfo, sizeof(struct AvailDataInfo)); radioTx(radiotxbuffer); + espNotifyAvailDataReq(availDataReq); } void processXferComplete(uint8_t *buffer) { @@ -656,8 +624,6 @@ void sendTimingReply(void *__xdata buf) { frameHeader->pan = rxframe->srcPan; addCRC(response, sizeof(struct timingResponse)); radioTx(radiotxbuffer); - radioTx(radiotxbuffer); - radioTx(radiotxbuffer); espNotifyJoinNetwork(rxframe->src); } void sendPart(uint8_t partNo) { @@ -682,29 +648,15 @@ void sendPart(uint8_t partNo) { } void sendBlockData() { uint8_t partNo = 0; - uint8_t rounds = 0; while (partNo < BLOCK_MAX_PARTS) { for (uint8_t c = 0; (c < BLOCK_MAX_PARTS) && (partNo < BLOCK_MAX_PARTS); c++) { if (requestedData.requestedParts[c / 8] & (1 << (c % 8))) { sendPart(c); - timerDelay(TIMER_TICKS_PER_MS); + // timerDelay(TIMER_TICKS_PER_MS); partNo++; } } - rounds++; - if (rounds == 4) { - // return; - } } - // TODO: not sure if we need this, probably not. Not sure why I added it in the first place - /* - commsRxUnencrypted(radiorxbuffer); - commsRxUnencrypted(radiorxbuffer); - commsRxUnencrypted(radiorxbuffer); - commsRxUnencrypted(radiorxbuffer); - commsRxUnencrypted(radiorxbuffer); - commsRxUnencrypted(radiorxbuffer); - */ } void sendXferCompleteAck(uint8_t *dst) { struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1); @@ -813,6 +765,7 @@ void main(void) { if (timerGet() > blockStartTimer) { sendBlockData(); blockStartTimer = 0; + radioRxFlush(); } } } diff --git a/tag_fw/syncedproto.c b/tag_fw/syncedproto.c index 43995664..bb020f7a 100644 --- a/tag_fw/syncedproto.c +++ b/tag_fw/syncedproto.c @@ -128,7 +128,6 @@ struct blockRequest { struct blockRequestAck { uint8_t checksum; - uint16_t blockSizeMs; uint16_t pleaseWaitMs; uint8_t cancelXfer; } __packed; @@ -288,9 +287,9 @@ const struct timingResponse *__xdata getTimingInfo() { radioRxFlush(); uint32_t __xdata mTimerWaitStart; radioRxEnable(true, true); - for (uint8_t c = 0; c < 3; c++) { + for (uint8_t c = 0; c < 5; c++) { sendTimingInfoReq(); - mTimerWaitStart = timerGet() + TIMER_TICKS_PER_MS * 3; + mTimerWaitStart = timerGet() + TIMER_TICKS_PER_MS * 4; while (timerGet() < mTimerWaitStart) { int8_t ret = commsRxUnencrypted(inBuffer); if (ret > 1) { @@ -425,9 +424,7 @@ void processBlockPart(struct blockPart *bp) { if ((start + size) > sizeof(blockXferBuffer)) { size = sizeof(blockXferBuffer) - start; } - if (!checkCRC(bp, sizeof(struct blockPart) + BLOCK_PART_DATA_SIZE)) { - pr("CHKSUM FAILED!\n"); - } else { + if (checkCRC(bp, sizeof(struct blockPart) + BLOCK_PART_DATA_SIZE)) { // copy block data to buffer xMemCopy((void *)(blockXferBuffer + start), (const void *)bp->data, size); // we don't need this block anymore, set bit to 0 so we don't request it again @@ -455,20 +452,16 @@ bool blockRxLoop(uint32_t timeout) { } struct blockRequestAck *__xdata continueToRX() { struct blockRequestAck *ack = (struct blockRequestAck *)(inBuffer + sizeof(struct MacFrameNormal) + 1); - ack->pleaseWaitMs = 1; - ack->blockSizeMs = 250; + ack->pleaseWaitMs = 0; + ack->cancelXfer = 0; return ack; } void sendBlockRequest() { - /* commsRxUnencrypted(mRxBuf); - commsRxUnencrypted(mRxBuf); - commsRxUnencrypted(mRxBuf); - commsRxUnencrypted(mRxBuf);*/ memset(outBuffer, 0, sizeof(struct MacFrameNormal) + sizeof(struct blockRequest) + 2 + 2); struct MacFrameNormal *__xdata f = (struct MacFrameNormal *)(outBuffer + 1); struct blockRequest *__xdata blockreq = (struct blockRequest *)(outBuffer + 2 + sizeof(struct MacFrameNormal)); outBuffer[0] = sizeof(struct MacFrameNormal) + sizeof(struct blockRequest) + 2 + 2; - if (requestPartialBlock) { + if (requestPartialBlock) {; outBuffer[sizeof(struct MacFrameNormal) + 1] = PKT_BLOCK_PARTIAL_REQUEST; } else { outBuffer[sizeof(struct MacFrameNormal) + 1] = PKT_BLOCK_REQUEST; @@ -493,9 +486,10 @@ void sendBlockRequest() { struct blockRequestAck *__xdata performBlockRequest() { uint32_t __xdata t; radioRxEnable(true, true); - for (uint8_t c = 0; c < 10; c++) { + radioRxFlush(); + for (uint8_t c = 0; c < 30; c++) { sendBlockRequest(); - t = timerGet() + (TIMER_TICKS_PER_MS * 5UL); + t = timerGet() + (TIMER_TICKS_PER_MS * (7UL + c / 10)); do { int8_t __xdata ret = commsRxUnencrypted(inBuffer); if (ret > 1) { @@ -504,19 +498,22 @@ struct blockRequestAck *__xdata performBlockRequest() { if (checkCRC((inBuffer + sizeof(struct MacFrameNormal) + 1), sizeof(struct blockRequestAck))) return (struct blockRequestAck *)(inBuffer + sizeof(struct MacFrameNormal) + 1); break; - /* case PKT_BLOCK_PART: - // pr("packet instead of ack"); - processBlockPart((struct blockPart *)(inBuffer + sizeof(struct MacFrameNormal) + 1)); + case PKT_BLOCK_PART: + // block already started while we were waiting for a get block reply + //pr("!"); + //processBlockPart((struct blockPart *)(inBuffer + sizeof(struct MacFrameNormal) + 1)); return continueToRX(); - */ + break; default: pr("pkt w/type %02X\n", getPacketType(inBuffer)); + break; } } } while (timerGet() < t); } - return NULL; + return continueToRX(); + //return NULL; } void sendXferCompletePacket() { memset(outBuffer, 0, sizeof(struct MacFrameNormal) + 2 + 4); @@ -787,6 +784,9 @@ void doDataDownload() { } pr("]\n"); #endif + + //timerDelay(TIMER_TICKS_PER_MS*100); + // DO BLOCK REQUEST - request a block, get an ack with timing info (hopefully) struct blockRequestAck *__xdata ack = performBlockRequest(); if (ack == NULL) { @@ -802,26 +802,27 @@ void doDataDownload() { } // SLEEP - until the AP is ready with the data - uint32_t temp1 = ack->pleaseWaitMs * 1UL; if (ack->pleaseWaitMs) { + ack->pleaseWaitMs -= 10; if (ack->pleaseWaitMs < 35) { timerDelay(ack->pleaseWaitMs * TIMER_TICKS_PER_MS); } else { doSleep(ack->pleaseWaitMs - 30); radioRxEnable(true, true); } + } else { + // immediately start with the reception of the block data } // BLOCK RX LOOP - receive a block, until the timeout has passed - if (!blockRxLoop(ack->blockSizeMs)) { + if (!blockRxLoop(440)) { // was 340 // didn't receive packets blockRequestAttempt++; - if (blockRequestAttempt > 4) { + if (blockRequestAttempt > 5) { pr("bailing on download, 0 blockparts rx'd\n"); return; } else { - pr("0 packets...\n"); - goto startdownload; + //goto startdownload; } } else { // successfull block RX loop @@ -918,8 +919,8 @@ void doDataDownload() { sendXferComplete(); killRadio(); eepromReadStart(EEPROM_UPDATA_AREA_START); - wdtDeviceReset(); - // selfUpdate(); + //wdtDeviceReset(); + selfUpdate(); break; }