diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index cd59ad080..2d0b10c68 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -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) diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 44af11ced..5a73f5874 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -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; diff --git a/src/xmodem.cpp b/src/xmodem.cpp index dbc7bf4a7..5f30de253 100644 --- a/src/xmodem.cpp +++ b/src/xmodem.cpp @@ -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 } diff --git a/src/xmodem.h b/src/xmodem.h index 7745b6012..894488c02 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -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);