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.queueStatus = *queueStatusPacketForPhone;
releaseQueueStatusPhonePacket();
} else if (xmodemPacketForPhone) {
} else if (xmodemPacketForPhone.control != meshtastic_XModem_Control_NUL) {
fromRadioScratch.which_payload_variant = meshtastic_FromRadio_xmodemPacket_tag;
fromRadioScratch.xmodemPacket = *xmodemPacketForPhone;
free(xmodemPacketForPhone);
xmodemPacketForPhone = NULL;
fromRadioScratch.xmodemPacket = xmodemPacketForPhone;
xmodemPacketForPhone = meshtastic_XModem_init_zero;
} else if (packetForPhone) {
printPacket("phone downloaded packet", packetForPhone);
@ -377,7 +376,7 @@ bool PhoneAPI::available()
if (hasPacket)
return true;
if (!xmodemPacketForPhone)
if (xmodemPacketForPhone.control != meshtastic_XModem_Control_NUL)
xmodemPacketForPhone = xModem.getForPhone();
hasPacket = !!packetForPhone;
if (hasPacket)

View File

@ -44,7 +44,7 @@ class PhoneAPI
meshtastic_MeshPacket *packetForPhone = NULL;
// 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
meshtastic_QueueStatus *queueStatusPacketForPhone = NULL;

View File

@ -34,10 +34,7 @@
XModemAdapter xModem;
XModemAdapter::XModemAdapter()
{
xmodemStore = (meshtastic_XModem *)malloc(meshtastic_XModem_size);
}
XModemAdapter::XModemAdapter() {}
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)
{
memset(xmodemStore, 0, meshtastic_XModem_size);
xmodemStore->control = c;
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = c;
LOG_DEBUG("XModem: Notify Sending control %d.\n", c);
packetReady.notifyObservers(packetno);
}
meshtastic_XModem *XModemAdapter::getForPhone()
meshtastic_XModem XModemAdapter::getForPhone()
{
if (xmodemStore) {
return xmodemStore;
} else {
return NULL;
}
return xmodemStore;
}
void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
@ -106,13 +99,13 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
if (file) {
packetno = 1;
isTransmitting = true;
memset(xmodemStore, 0, meshtastic_XModem_size);
xmodemStore->control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno;
xmodemStore->buffer.size = file.read(xmodemStore->buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
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);
if (xmodemStore->buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
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);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}
@ -171,13 +164,13 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
}
retrans = MAXRETRANS; // reset retransmit counter
packetno++;
memset(xmodemStore, 0, meshtastic_XModem_size);
xmodemStore->control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno;
xmodemStore->buffer.size = file.read(xmodemStore->buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
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);
if (xmodemStore->buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
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);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}
@ -197,14 +190,14 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
isTransmitting = false;
break;
}
memset(xmodemStore, 0, meshtastic_XModem_size);
xmodemStore->control = meshtastic_XModem_Control_SOH;
xmodemStore->seq = packetno;
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
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->crc16 = crc16_ccitt(xmodemStore->buffer.bytes, 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)) {
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
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);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}

View File

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