mirror of
https://github.com/meshtastic/firmware.git
synced 2026-01-16 23:07:34 +00:00
I thought git would be smart enough to understand all the whitespace changes but even with all the flags I know to make it ignore theses it still blows up if there are identical changes on both sides.
I have a solution but it require creating a new commit at the merge base for each conflicting PR and merging it into develop.
I don't think blowing up all PRs is worth for now, maybe if we can coordinate this for V3 let's say.
This reverts commit 0d11331d18.
This commit is contained in:
371
src/xmodem.cpp
371
src/xmodem.cpp
@@ -2,14 +2,14 @@
|
||||
* @file xmodem.cpp
|
||||
* @brief Implementation of XMODEM protocol for Meshtastic devices.
|
||||
*
|
||||
* This file contains the implementation of the XMODEM protocol for Meshtastic devices. It is based on the XMODEM
|
||||
* implementation by Georges Menie (www.menie.org) and has been adapted for protobuf encapsulation.
|
||||
* This file contains the implementation of the XMODEM protocol for Meshtastic devices. It is based on the XMODEM implementation
|
||||
* by Georges Menie (www.menie.org) and has been adapted for protobuf encapsulation.
|
||||
*
|
||||
* The XMODEM protocol is used for reliable transmission of binary data over a serial connection. This implementation
|
||||
* supports both sending and receiving of data.
|
||||
* The XMODEM protocol is used for reliable transmission of binary data over a serial connection. This implementation supports
|
||||
* both sending and receiving of data.
|
||||
*
|
||||
* The XModemAdapter class provides the main functionality for the protocol, including CRC calculation, packet handling,
|
||||
* and control signal sending.
|
||||
* The XModemAdapter class provides the main functionality for the protocol, including CRC calculation, packet handling, and
|
||||
* control signal sending.
|
||||
*
|
||||
* @copyright Copyright (c) 2001-2019 Georges Menie
|
||||
* @author
|
||||
@@ -64,19 +64,20 @@ XModemAdapter::XModemAdapter() {}
|
||||
* @param length The length of the buffer.
|
||||
* @return The calculated checksum.
|
||||
*/
|
||||
unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length) {
|
||||
unsigned short crc16 = 0;
|
||||
while (length != 0) {
|
||||
crc16 = (unsigned char)(crc16 >> 8) | (crc16 << 8);
|
||||
crc16 ^= *buffer;
|
||||
crc16 ^= (unsigned char)(crc16 & 0xff) >> 4;
|
||||
crc16 ^= (crc16 << 8) << 4;
|
||||
crc16 ^= ((crc16 & 0xff) << 4) << 1;
|
||||
buffer++;
|
||||
length--;
|
||||
}
|
||||
unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length)
|
||||
{
|
||||
unsigned short crc16 = 0;
|
||||
while (length != 0) {
|
||||
crc16 = (unsigned char)(crc16 >> 8) | (crc16 << 8);
|
||||
crc16 ^= *buffer;
|
||||
crc16 ^= (unsigned char)(crc16 & 0xff) >> 4;
|
||||
crc16 ^= (crc16 << 8) << 4;
|
||||
crc16 ^= ((crc16 & 0xff) << 4) << 1;
|
||||
buffer++;
|
||||
length--;
|
||||
}
|
||||
|
||||
return crc16;
|
||||
return crc16;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,178 +89,190 @@ unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length) {
|
||||
* @param tcrc The expected checksum.
|
||||
* @return 1 if the checksums match, 0 otherwise.
|
||||
*/
|
||||
int XModemAdapter::check(const pb_byte_t *buf, int sz, unsigned short tcrc) { return crc16_ccitt(buf, sz) == tcrc; }
|
||||
|
||||
void XModemAdapter::sendControl(meshtastic_XModem_Control c) {
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = c;
|
||||
LOG_DEBUG("XModem: Notify Send control %d", c);
|
||||
packetReady.notifyObservers(packetno);
|
||||
int XModemAdapter::check(const pb_byte_t *buf, int sz, unsigned short tcrc)
|
||||
{
|
||||
return crc16_ccitt(buf, sz) == tcrc;
|
||||
}
|
||||
|
||||
meshtastic_XModem XModemAdapter::getForPhone() { return xmodemStore; }
|
||||
void XModemAdapter::sendControl(meshtastic_XModem_Control c)
|
||||
{
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = c;
|
||||
LOG_DEBUG("XModem: Notify Send control %d", c);
|
||||
packetReady.notifyObservers(packetno);
|
||||
}
|
||||
|
||||
void XModemAdapter::resetForPhone() { xmodemStore = meshtastic_XModem_init_zero; }
|
||||
meshtastic_XModem XModemAdapter::getForPhone()
|
||||
{
|
||||
return xmodemStore;
|
||||
}
|
||||
|
||||
void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) {
|
||||
switch (xmodemPacket.control) {
|
||||
case meshtastic_XModem_Control_SOH:
|
||||
case meshtastic_XModem_Control_STX:
|
||||
if ((xmodemPacket.seq == 0) && !isReceiving && !isTransmitting) {
|
||||
// NULL packet has the destination filename
|
||||
memcpy(filename, &xmodemPacket.buffer.bytes, xmodemPacket.buffer.size);
|
||||
void XModemAdapter::resetForPhone()
|
||||
{
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
}
|
||||
|
||||
if (xmodemPacket.control == meshtastic_XModem_Control_SOH) { // Receive this file and put to Flash
|
||||
spiLock->lock();
|
||||
file = FSCom.open(filename, FILE_O_WRITE);
|
||||
spiLock->unlock();
|
||||
if (file) {
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
isReceiving = true;
|
||||
packetno = 1;
|
||||
break;
|
||||
void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
|
||||
{
|
||||
switch (xmodemPacket.control) {
|
||||
case meshtastic_XModem_Control_SOH:
|
||||
case meshtastic_XModem_Control_STX:
|
||||
if ((xmodemPacket.seq == 0) && !isReceiving && !isTransmitting) {
|
||||
// NULL packet has the destination filename
|
||||
memcpy(filename, &xmodemPacket.buffer.bytes, xmodemPacket.buffer.size);
|
||||
|
||||
if (xmodemPacket.control == meshtastic_XModem_Control_SOH) { // Receive this file and put to Flash
|
||||
spiLock->lock();
|
||||
file = FSCom.open(filename, FILE_O_WRITE);
|
||||
spiLock->unlock();
|
||||
if (file) {
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
isReceiving = true;
|
||||
packetno = 1;
|
||||
break;
|
||||
}
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
isReceiving = false;
|
||||
break;
|
||||
} else { // Transmit this file from Flash
|
||||
LOG_INFO("XModem: Transmit file %s", filename);
|
||||
spiLock->lock();
|
||||
file = FSCom.open(filename, FILE_O_READ);
|
||||
spiLock->unlock();
|
||||
if (file) {
|
||||
packetno = 1;
|
||||
isTransmitting = true;
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: STX Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
break;
|
||||
}
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (isReceiving) {
|
||||
// normal file data packet
|
||||
if ((xmodemPacket.seq == packetno) &&
|
||||
check(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size, xmodemPacket.crc16)) {
|
||||
// valid packet
|
||||
spiLock->lock();
|
||||
file.write(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size);
|
||||
spiLock->unlock();
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
packetno++;
|
||||
break;
|
||||
}
|
||||
// invalid packet
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
break;
|
||||
} else if (isTransmitting) {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
break;
|
||||
case meshtastic_XModem_Control_EOT:
|
||||
// End of transmission
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
spiLock->lock();
|
||||
file.flush();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
isReceiving = false;
|
||||
break;
|
||||
} else { // Transmit this file from Flash
|
||||
LOG_INFO("XModem: Transmit file %s", filename);
|
||||
spiLock->lock();
|
||||
file = FSCom.open(filename, FILE_O_READ);
|
||||
spiLock->unlock();
|
||||
if (file) {
|
||||
packetno = 1;
|
||||
isTransmitting = true;
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: STX Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
break;
|
||||
}
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (isReceiving) {
|
||||
// normal file data packet
|
||||
if ((xmodemPacket.seq == packetno) && check(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size, xmodemPacket.crc16)) {
|
||||
// valid packet
|
||||
spiLock->lock();
|
||||
file.write(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size);
|
||||
spiLock->unlock();
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
packetno++;
|
||||
break;
|
||||
}
|
||||
// invalid packet
|
||||
sendControl(meshtastic_XModem_Control_NAK);
|
||||
break;
|
||||
} else if (isTransmitting) {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case meshtastic_XModem_Control_EOT:
|
||||
// End of transmission
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
spiLock->lock();
|
||||
file.flush();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
isReceiving = false;
|
||||
break;
|
||||
case meshtastic_XModem_Control_CAN:
|
||||
// Cancel transmission and remove file
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
spiLock->lock();
|
||||
file.flush();
|
||||
file.close();
|
||||
|
||||
FSCom.remove(filename);
|
||||
spiLock->unlock();
|
||||
isReceiving = false;
|
||||
break;
|
||||
case meshtastic_XModem_Control_ACK:
|
||||
// Acknowledge Send the next packet
|
||||
if (isTransmitting) {
|
||||
if (isEOT) {
|
||||
sendControl(meshtastic_XModem_Control_EOT);
|
||||
case meshtastic_XModem_Control_CAN:
|
||||
// Cancel transmission and remove file
|
||||
sendControl(meshtastic_XModem_Control_ACK);
|
||||
spiLock->lock();
|
||||
file.flush();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
LOG_INFO("XModem: Finished send file %s", filename);
|
||||
isTransmitting = false;
|
||||
isEOT = false;
|
||||
break;
|
||||
}
|
||||
retrans = MAXRETRANS; // reset retransmit counter
|
||||
packetno++;
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: ACK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
} else {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
}
|
||||
break;
|
||||
case meshtastic_XModem_Control_NAK:
|
||||
// Negative acknowledge. Send the same buffer again
|
||||
if (isTransmitting) {
|
||||
if (--retrans <= 0) {
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
spiLock->lock();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
LOG_INFO("XModem: Retransmit timeout, cancel file %s", filename);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
file.seek((packetno - 1) * sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: NAK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
} else {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
FSCom.remove(filename);
|
||||
spiLock->unlock();
|
||||
isReceiving = false;
|
||||
break;
|
||||
case meshtastic_XModem_Control_ACK:
|
||||
// Acknowledge Send the next packet
|
||||
if (isTransmitting) {
|
||||
if (isEOT) {
|
||||
sendControl(meshtastic_XModem_Control_EOT);
|
||||
spiLock->lock();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
LOG_INFO("XModem: Finished send file %s", filename);
|
||||
isTransmitting = false;
|
||||
isEOT = false;
|
||||
break;
|
||||
}
|
||||
retrans = MAXRETRANS; // reset retransmit counter
|
||||
packetno++;
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: ACK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
} else {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
}
|
||||
break;
|
||||
case meshtastic_XModem_Control_NAK:
|
||||
// Negative acknowledge. Send the same buffer again
|
||||
if (isTransmitting) {
|
||||
if (--retrans <= 0) {
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
spiLock->lock();
|
||||
file.close();
|
||||
spiLock->unlock();
|
||||
LOG_INFO("XModem: Retransmit timeout, cancel file %s", filename);
|
||||
isTransmitting = false;
|
||||
break;
|
||||
}
|
||||
xmodemStore = meshtastic_XModem_init_zero;
|
||||
xmodemStore.control = meshtastic_XModem_Control_SOH;
|
||||
xmodemStore.seq = packetno;
|
||||
spiLock->lock();
|
||||
file.seek((packetno - 1) * sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
|
||||
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
|
||||
spiLock->unlock();
|
||||
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
|
||||
LOG_DEBUG("XModem: NAK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
|
||||
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
|
||||
isEOT = true;
|
||||
// send EOT on next Ack
|
||||
}
|
||||
packetReady.notifyObservers(packetno);
|
||||
} else {
|
||||
// just received something weird.
|
||||
sendControl(meshtastic_XModem_Control_CAN);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Unknown control character
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Unknown control character
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user