Merge pull request #2189 from meshtastic/xmodem-fix-2

Xmodem fix 2
This commit is contained in:
Ben Meadors 2023-01-22 09:12:51 -06:00 committed by GitHub
commit 2ecf273cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 41 deletions

View File

@ -297,11 +297,10 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf)
fromRadioScratch.which_payload_variant = meshtastic_FromRadio_queueStatus_tag; fromRadioScratch.which_payload_variant = meshtastic_FromRadio_queueStatus_tag;
fromRadioScratch.queueStatus = *queueStatusPacketForPhone; fromRadioScratch.queueStatus = *queueStatusPacketForPhone;
releaseQueueStatusPhonePacket(); releaseQueueStatusPhonePacket();
} else if (xmodemPacketForPhone) { } else if (xmodemPacketForPhone.control != meshtastic_XModem_Control_NUL) {
fromRadioScratch.which_payload_variant = meshtastic_FromRadio_xmodemPacket_tag; fromRadioScratch.which_payload_variant = meshtastic_FromRadio_xmodemPacket_tag;
fromRadioScratch.xmodemPacket = *xmodemPacketForPhone; fromRadioScratch.xmodemPacket = xmodemPacketForPhone;
free(xmodemPacketForPhone); xmodemPacketForPhone = meshtastic_XModem_init_zero;
xmodemPacketForPhone = NULL;
} else if (packetForPhone) { } else if (packetForPhone) {
printPacket("phone downloaded packet", packetForPhone); printPacket("phone downloaded packet", packetForPhone);
@ -377,7 +376,7 @@ bool PhoneAPI::available()
if (hasPacket) if (hasPacket)
return true; return true;
if (!xmodemPacketForPhone) if (xmodemPacketForPhone.control != meshtastic_XModem_Control_NUL)
xmodemPacketForPhone = xModem.getForPhone(); xmodemPacketForPhone = xModem.getForPhone();
hasPacket = !!packetForPhone; hasPacket = !!packetForPhone;
if (hasPacket) if (hasPacket)

View File

@ -44,7 +44,7 @@ class PhoneAPI
meshtastic_MeshPacket *packetForPhone = NULL; meshtastic_MeshPacket *packetForPhone = NULL;
// file transfer packets destined for phone. Push it to the queue then free it. // file transfer packets destined for phone. Push it to the queue then free it.
meshtastic_XModem *xmodemPacketForPhone = NULL; meshtastic_XModem xmodemPacketForPhone = meshtastic_XModem_init_zero;
// Keep QueueStatus packet just as packetForPhone // Keep QueueStatus packet just as packetForPhone
meshtastic_QueueStatus *queueStatusPacketForPhone = NULL; meshtastic_QueueStatus *queueStatusPacketForPhone = NULL;

View File

@ -34,10 +34,7 @@
XModemAdapter xModem; XModemAdapter xModem;
XModemAdapter::XModemAdapter() XModemAdapter::XModemAdapter() {}
{
xmodemStore = (meshtastic_XModem *)malloc(meshtastic_XModem_size);
}
unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length) unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length)
{ {
@ -66,19 +63,15 @@ int XModemAdapter::check(const pb_byte_t *buf, int sz, unsigned short tcrc)
void XModemAdapter::sendControl(meshtastic_XModem_Control c) void XModemAdapter::sendControl(meshtastic_XModem_Control c)
{ {
memset(xmodemStore, 0, meshtastic_XModem_size); xmodemStore = meshtastic_XModem_init_zero;
xmodemStore->control = c; xmodemStore.control = c;
LOG_DEBUG("XModem: Notify Sending control %d.\n", c); LOG_DEBUG("XModem: Notify Sending control %d.\n", c);
packetReady.notifyObservers(packetno); packetReady.notifyObservers(packetno);
} }
meshtastic_XModem *XModemAdapter::getForPhone() meshtastic_XModem XModemAdapter::getForPhone()
{ {
if (xmodemStore) { return xmodemStore;
return xmodemStore;
} else {
return NULL;
}
} }
void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
@ -106,13 +99,13 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
if (file) { if (file) {
packetno = 1; packetno = 1;
isTransmitting = true; isTransmitting = true;
memset(xmodemStore, 0, meshtastic_XModem_size); xmodemStore = meshtastic_XModem_init_zero;
xmodemStore->control = meshtastic_XModem_Control_SOH; xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno; xmodemStore.seq = packetno;
xmodemStore->buffer.size = file.read(xmodemStore->buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes)); xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
xmodemStore->crc16 = crc16_ccitt(xmodemStore->buffer.bytes, xmodemStore->buffer.size); xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: STX Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore->buffer.size); LOG_DEBUG("XModem: STX Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore.buffer.size);
if (xmodemStore->buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) { if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true; isEOT = true;
// send EOT on next Ack // send EOT on next Ack
} }
@ -171,13 +164,13 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
} }
retrans = MAXRETRANS; // reset retransmit counter retrans = MAXRETRANS; // reset retransmit counter
packetno++; packetno++;
memset(xmodemStore, 0, meshtastic_XModem_size); xmodemStore = meshtastic_XModem_init_zero;
xmodemStore->control = meshtastic_XModem_Control_SOH; xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno; xmodemStore.seq = packetno;
xmodemStore->buffer.size = file.read(xmodemStore->buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes)); xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
xmodemStore->crc16 = crc16_ccitt(xmodemStore->buffer.bytes, xmodemStore->buffer.size); xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: ACK Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore->buffer.size); LOG_DEBUG("XModem: ACK Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore.buffer.size);
if (xmodemStore->buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) { if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true; isEOT = true;
// send EOT on next Ack // send EOT on next Ack
} }
@ -197,14 +190,14 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
isTransmitting = false; isTransmitting = false;
break; break;
} }
memset(xmodemStore, 0, meshtastic_XModem_size); xmodemStore = meshtastic_XModem_init_zero;
xmodemStore->control = meshtastic_XModem_Control_SOH; xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno; xmodemStore.seq = packetno;
file.seek((packetno - 1) * sizeof(meshtastic_XModem_buffer_t::bytes)); file.seek((packetno - 1) * sizeof(meshtastic_XModem_buffer_t::bytes));
xmodemStore->buffer.size = file.read(xmodemStore->buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes)); xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
xmodemStore->crc16 = crc16_ccitt(xmodemStore->buffer.bytes, xmodemStore->buffer.size); xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: NAK Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore->buffer.size); LOG_DEBUG("XModem: NAK Notify Sending packet %d, %d Bytes.\n", packetno, xmodemStore.buffer.size);
if (xmodemStore->buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) { if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true; isEOT = true;
// send EOT on next Ack // send EOT on next Ack
} }

View File

@ -47,7 +47,7 @@ class XModemAdapter
XModemAdapter(); XModemAdapter();
void handlePacket(meshtastic_XModem xmodemPacket); void handlePacket(meshtastic_XModem xmodemPacket);
meshtastic_XModem *getForPhone(); meshtastic_XModem getForPhone();
private: private:
bool isReceiving = false; bool isReceiving = false;
@ -67,7 +67,7 @@ class XModemAdapter
char filename[sizeof(meshtastic_XModem_buffer_t::bytes)] = {0}; char filename[sizeof(meshtastic_XModem_buffer_t::bytes)] = {0};
protected: protected:
meshtastic_XModem *xmodemStore = NULL; meshtastic_XModem xmodemStore = meshtastic_XModem_init_zero;
unsigned short crc16_ccitt(const pb_byte_t *buffer, int length); unsigned short crc16_ccitt(const pb_byte_t *buffer, int length);
int check(const pb_byte_t *buf, int sz, unsigned short tcrc); int check(const pb_byte_t *buf, int sz, unsigned short tcrc);
void sendControl(meshtastic_XModem_Control c); void sendControl(meshtastic_XModem_Control c);