diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 6cd17dac8..3fc4717a0 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -9,6 +9,7 @@ * */ #include "FSCommon.h" +#include "SPILock.h" #include "configuration.h" #ifdef HAS_SDCARD @@ -93,6 +94,8 @@ bool copyFile(const char *from, const char *to) return true; #elif defined(FSCom) + // take SPI Lock + spiLock->lock(); unsigned char cbuffer[16]; File f1 = FSCom.open(from, FILE_O_READ); @@ -115,6 +118,7 @@ bool copyFile(const char *from, const char *to) f2.flush(); f2.close(); f1.close(); + spiLock->unlock(); return true; #endif } @@ -136,16 +140,22 @@ bool renameFile(const char *pathFrom, const char *pathTo) return false; } #elif defined(FSCom) + #ifdef ARCH_ESP32 + // take SPI Lock + spiLock->lock(); // rename was fixed for ESP32 IDF LittleFS in April return FSCom.rename(pathFrom, pathTo); + spiLock->unlock(); #else + // copyFile does its own locking. if (copyFile(pathFrom, pathTo) && FSCom.remove(pathFrom)) { return true; } else { return false; } #endif + #endif } @@ -155,6 +165,7 @@ bool renameFile(const char *pathFrom, const char *pathTo) * @brief Get the list of files in a directory. * * This function returns a list of files in a directory. The list includes the full path of each file. + * We can't use SPILOCK here because of recursion. Callers of this function should use SPILOCK. * * @param dirname The name of the directory. * @param levels The number of levels of subdirectories to list. @@ -203,6 +214,7 @@ std::vector getFiles(const char *dirname, uint8_t levels) /** * Lists the contents of a directory. + * We can't use SPILOCK here because of recursion. Callers of this function should use SPILOCK. * * @param dirname The name of the directory to list. * @param levels The number of levels of subdirectories to list. @@ -316,18 +328,21 @@ void listDir(const char *dirname, uint8_t levels, bool del) void rmDir(const char *dirname) { #ifdef FSCom + spiLock->lock(); #if (defined(ARCH_ESP32) || defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) listDir(dirname, 10, true); #elif defined(ARCH_NRF52) // nRF52 implementation of LittleFS has a recursive delete function FSCom.rmdir_r(dirname); #endif + spiLock->unlock(); #endif } void fsInit() { #ifdef FSCom + spiLock->lock(); if (!FSBegin()) { LOG_ERROR("Filesystem mount failed"); // assert(0); This auto-formats the partition, so no need to fail here. @@ -338,6 +353,7 @@ void fsInit() LOG_DEBUG("Filesystem files:"); #endif listDir("/", 10); + spiLock->unlock(); #endif } @@ -347,6 +363,7 @@ void fsInit() void setupSDCard() { #ifdef HAS_SDCARD + spiLock->lock(); SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); if (!SD.begin(SDCARD_CS, SDHandler)) { @@ -373,5 +390,6 @@ void setupSDCard() LOG_DEBUG("SD Card Size: %lu MB", (uint32_t)cardSize); LOG_DEBUG("Total space: %lu MB", (uint32_t)(SD.totalBytes() / (1024 * 1024))); LOG_DEBUG("Used space: %lu MB", (uint32_t)(SD.usedBytes() / (1024 * 1024))); + spiLock->unlock(); #endif } \ No newline at end of file diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 57f2fb39a..187fe3ab0 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -13,6 +13,7 @@ #include "PowerFSM.h" #include "RTC.h" #include "Router.h" +#include "SPILock.h" #include "SafeFile.h" #include "TypeConversions.h" #include "error.h" @@ -351,12 +352,14 @@ bool NodeDB::factoryReset(bool eraseBleBonds) { LOG_INFO("Perform factory reset!"); // first, remove the "/prefs" (this removes most prefs) - rmDir("/prefs"); + rmDir("/prefs"); // this uses spilock internally... + spiLock->lock(); #ifdef FSCom if (FSCom.exists("/static/rangetest.csv") && !FSCom.remove("/static/rangetest.csv")) { LOG_ERROR("Could not remove rangetest.csv file"); } #endif + spiLock->unlock(); // second, install default state (this will deal with the duplicate mac address issue) installDefaultDeviceState(); installDefaultConfig(!eraseBleBonds); // Also preserve the private key if we're not erasing BLE bonds @@ -835,6 +838,7 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t { LoadFileResult state = LoadFileResult::OTHER_FAILURE; #ifdef FSCom + spiLock->lock(); auto f = FSCom.open(filename, FILE_O_READ); @@ -854,6 +858,7 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t } else { LOG_ERROR("Could not open / read %s", filename); } + spiLock->unlock(); #else LOG_ERROR("ERROR: Filesystem not implemented"); state = LoadFileResult::NO_FILESYSTEM; @@ -868,8 +873,10 @@ void NodeDB::loadFromDisk() // disk we will still factoryReset to restore things. #ifdef ARCH_ESP32 + spiLock->lock(); if (FSCom.exists("/static/static")) rmDir("/static/static"); // Remove bad static web files bundle from initial 2.5.13 release + spiLock->unlock(); #endif // static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM @@ -1016,11 +1023,9 @@ void NodeDB::loadFromDisk() bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_t *fields, const void *dest_struct, bool fullAtomic) { -#ifdef ARCH_ESP32 - concurrency::LockGuard g(spiLock); -#endif bool okay = false; #ifdef FSCom + spiLock->lock(); auto f = SafeFile(filename, fullAtomic); LOG_INFO("Save %s", filename); @@ -1033,6 +1038,7 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ } bool writeSucceeded = f.close(); + spiLock->unlock(); if (!okay || !writeSucceeded) { LOG_ERROR("Can't write prefs!"); @@ -1046,7 +1052,9 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ bool NodeDB::saveChannelsToDisk() { #ifdef FSCom + spiLock->lock(); FSCom.mkdir("/prefs"); + spiLock->unlock(); #endif return saveProto(channelFileName, meshtastic_ChannelFile_size, &meshtastic_ChannelFile_msg, &channelFile); } @@ -1054,7 +1062,9 @@ bool NodeDB::saveChannelsToDisk() bool NodeDB::saveDeviceStateToDisk() { #ifdef FSCom + spiLock->lock(); FSCom.mkdir("/prefs"); + spiLock->unlock(); #endif // Note: if MAX_NUM_NODES=100 and meshtastic_NodeInfoLite_size=166, so will be approximately 17KB // Because so huge we _must_ not use fullAtomic, because the filesystem is probably too small to hold two copies of this @@ -1067,7 +1077,9 @@ bool NodeDB::saveToDiskNoRetry(int saveWhat) bool success = true; #ifdef FSCom + spiLock->lock(); FSCom.mkdir("/prefs"); + spiLock->unlock(); #endif if (saveWhat & SEGMENT_CONFIG) { config.has_device = true; @@ -1118,7 +1130,9 @@ bool NodeDB::saveToDisk(int saveWhat) if (!success) { LOG_ERROR("Failed to save to disk, retrying"); #ifdef ARCH_NRF52 // @geeksville is not ready yet to say we should do this on other platforms. See bug #4184 discussion + spiLock->lock(); FSCom.format(); + spiLock->unlock(); #endif success = saveToDiskNoRetry(saveWhat); diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index b6e31d66b..e8aa0c61f 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -12,6 +12,7 @@ #include "PhoneAPI.h" #include "PowerFSM.h" #include "RadioInterface.h" +#include "SPILock.h" #include "TypeConversions.h" #include "main.h" #include "xmodem.h" @@ -54,7 +55,9 @@ void PhoneAPI::handleStartConfig() // even if we were already connected - restart our state machine state = STATE_SEND_MY_INFO; pauseBluetoothLogging = true; + spiLock->lock(); filesManifest = getFiles("/", 10); + spiLock->unlock(); LOG_DEBUG("Got %d files in manifest", filesManifest.size()); LOG_INFO("Start API client config"); diff --git a/src/mesh/http/ContentHandler.cpp b/src/mesh/http/ContentHandler.cpp index 64f7164c9..1bc988781 100644 --- a/src/mesh/http/ContentHandler.cpp +++ b/src/mesh/http/ContentHandler.cpp @@ -10,6 +10,7 @@ #include "mesh/wifi/WiFiAPClient.h" #endif #include "Led.h" +#include "SPILock.h" #include "power.h" #include "serialization/JSON.h" #include @@ -234,6 +235,7 @@ void handleAPIv1ToRadio(HTTPRequest *req, HTTPResponse *res) void htmlDeleteDir(const char *dirname) { + File root = FSCom.open(dirname); if (!root) { return; @@ -316,6 +318,7 @@ void handleFsBrowseStatic(HTTPRequest *req, HTTPResponse *res) res->setHeader("Access-Control-Allow-Origin", "*"); res->setHeader("Access-Control-Allow-Methods", "GET"); + spiLock->lock(); auto fileList = htmlListDir("/static", 10); // create json output structure @@ -323,6 +326,7 @@ void handleFsBrowseStatic(HTTPRequest *req, HTTPResponse *res) filesystemObj["total"] = new JSONValue((int)FSCom.totalBytes()); filesystemObj["used"] = new JSONValue((int)FSCom.usedBytes()); filesystemObj["free"] = new JSONValue(int(FSCom.totalBytes() - FSCom.usedBytes())); + spiLock->unlock(); JSONObject jsonObjInner; jsonObjInner["files"] = new JSONValue(fileList); @@ -349,7 +353,9 @@ void handleFsDeleteStatic(HTTPRequest *req, HTTPResponse *res) res->setHeader("Access-Control-Allow-Methods", "DELETE"); if (params->getQueryParameter("delete", paramValDelete)) { std::string pathDelete = "/" + paramValDelete; + spiLock->lock(); if (FSCom.remove(pathDelete.c_str())) { + spiLock->unlock(); LOG_INFO("%s", pathDelete.c_str()); JSONObject jsonObjOuter; jsonObjOuter["status"] = new JSONValue("ok"); @@ -358,6 +364,7 @@ void handleFsDeleteStatic(HTTPRequest *req, HTTPResponse *res) delete value; return; } else { + spiLock->unlock(); LOG_INFO("%s", pathDelete.c_str()); JSONObject jsonObjOuter; jsonObjOuter["status"] = new JSONValue("Error"); @@ -391,6 +398,8 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res) filenameGzip = "/static/index.html.gz"; } + spiLock->lock(); + if (FSCom.exists(filename.c_str())) { file = FSCom.open(filename.c_str()); if (!file.available()) { @@ -408,6 +417,7 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res) file = FSCom.open(filenameGzip.c_str()); res->setHeader("Content-Type", "text/html"); if (!file.available()) { + spiLock->unlock(); LOG_WARN("File not available - %s", filenameGzip.c_str()); res->println("Web server is running.

The content you are looking for can't be found. Please see: FAQ.

0); file.close(); + spiLock->unlock(); return; } else { @@ -533,6 +544,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) // concepts of the body parser functionality easier to understand. std::string pathname = "/static/" + filename; + spiLock->lock(); // Create a new file to stream the data into File file = FSCom.open(pathname.c_str(), FILE_O_WRITE); size_t fileLength = 0; @@ -551,6 +563,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) if (FSCom.totalBytes() - FSCom.usedBytes() < 51200) { file.flush(); file.close(); + spiLock->unlock(); res->println("

Write aborted! Reserving 50k on filesystem.

"); // enableLoopWDT(); @@ -569,6 +582,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) file.flush(); file.close(); + spiLock->unlock(); res->printf("

Saved %d bytes to %s

", (int)fileLength, pathname.c_str()); } if (!didwrite) { @@ -640,9 +654,11 @@ void handleReport(HTTPRequest *req, HTTPResponse *res) jsonObjMemory["heap_free"] = new JSONValue((int)memGet.getFreeHeap()); jsonObjMemory["psram_total"] = new JSONValue((int)memGet.getPsramSize()); jsonObjMemory["psram_free"] = new JSONValue((int)memGet.getFreePsram()); + spiLock->lock(); jsonObjMemory["fs_total"] = new JSONValue((int)FSCom.totalBytes()); jsonObjMemory["fs_used"] = new JSONValue((int)FSCom.usedBytes()); jsonObjMemory["fs_free"] = new JSONValue(int(FSCom.totalBytes() - FSCom.usedBytes())); + spiLock->unlock(); // data->power JSONObject jsonObjPower; @@ -713,7 +729,9 @@ void handleDeleteFsContent(HTTPRequest *req, HTTPResponse *res) LOG_INFO("Delete files from /static/* : "); + spiLock->lock(); htmlDeleteDir("/static"); + spiLock->unlock(); res->println("


Back to admin"); } diff --git a/src/mesh/mesh-pb-constants.cpp b/src/mesh/mesh-pb-constants.cpp index deb93d0a6..6588370e0 100644 --- a/src/mesh/mesh-pb-constants.cpp +++ b/src/mesh/mesh-pb-constants.cpp @@ -1,6 +1,7 @@ #include "configuration.h" #include "FSCommon.h" +#include "SPILock.h" #include "mesh-pb-constants.h" #include #include @@ -36,6 +37,7 @@ bool pb_decode_from_bytes(const uint8_t *srcbuf, size_t srcbufsize, const pb_msg bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count) { bool status = false; + spiLock->lock(); File *file = (File *)stream->state; if (buf == NULL) { @@ -49,15 +51,20 @@ bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count) if (file->available() == 0) stream->bytes_left = 0; + spiLock->unlock(); + return status; } /// Write to an arduino file bool writecb(pb_ostream_t *stream, const uint8_t *buf, size_t count) { + spiLock->lock(); auto file = (Print *)stream->state; // LOG_DEBUG("writing %d bytes to protobuf file", count); - return file->write(buf, count) == count; + bool status = file->write(buf, count) == count; + spiLock->unlock(); + return status; } #endif diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 1bd9e8b77..859bab67a 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -4,6 +4,7 @@ #include "NodeDB.h" #include "PowerFSM.h" #include "RTC.h" +#include "SPILock.h" #include "meshUtils.h" #include #if defined(ARCH_ESP32) && !MESHTASTIC_EXCLUDE_BLUETOOTH @@ -358,12 +359,15 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta } case meshtastic_AdminMessage_delete_file_request_tag: { LOG_DEBUG("Client requesting to delete file: %s", r->delete_file_request); + #ifdef FSCom + spiLock->lock(); if (FSCom.remove(r->delete_file_request)) { LOG_DEBUG("Successfully deleted file"); } else { LOG_DEBUG("Failed to delete file"); } + spiLock->unlock(); #endif break; } diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index 31886ab1b..4fe236b6c 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -9,6 +9,7 @@ #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" // needed for button bypass +#include "SPILock.h" #include "detect/ScanI2C.h" #include "input/ScanAndSelect.h" #include "mesh/generated/meshtastic/cannedmessages.pb.h" @@ -1184,8 +1185,10 @@ bool CannedMessageModule::saveProtoForModule() { bool okay = true; -#ifdef FS - FS.mkdir("/prefs"); +#ifdef FSCom + spiLock->lock(); + FSCom.mkdir("/prefs"); + spiLock->unlock(); #endif okay &= nodeDB->saveProto(cannedMessagesConfigFile, meshtastic_CannedMessageModuleConfig_size, diff --git a/src/modules/RangeTestModule.cpp b/src/modules/RangeTestModule.cpp index bf842ce55..677cf30f7 100644 --- a/src/modules/RangeTestModule.cpp +++ b/src/modules/RangeTestModule.cpp @@ -15,6 +15,7 @@ #include "PowerFSM.h" #include "RTC.h" #include "Router.h" +#include "SPILock.h" #include "airtime.h" #include "configuration.h" #include "gps/GeoCoord.h" @@ -209,6 +210,7 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp) LOG_DEBUG("gpsStatus->getDOP() %d", gpsStatus->getDOP()); LOG_DEBUG("-----------------------------------------"); */ + spiLock->lock(); if (!FSBegin()) { LOG_DEBUG("An Error has occurred while mounting the filesystem"); return 0; @@ -289,6 +291,7 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp) fileToAppend.printf("\"%s\"\n", p.payload.bytes); fileToAppend.flush(); fileToAppend.close(); + spiLock->unlock(); #endif return 1; diff --git a/src/modules/Telemetry/Sensor/BME680Sensor.cpp b/src/modules/Telemetry/Sensor/BME680Sensor.cpp index 18515d0a8..9237cf0c9 100644 --- a/src/modules/Telemetry/Sensor/BME680Sensor.cpp +++ b/src/modules/Telemetry/Sensor/BME680Sensor.cpp @@ -5,6 +5,7 @@ #include "../mesh/generated/meshtastic/telemetry.pb.h" #include "BME680Sensor.h" #include "FSCommon.h" +#include "SPILock.h" #include "TelemetrySensor.h" BME680Sensor::BME680Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BME680, "BME680") {} @@ -75,6 +76,7 @@ bool BME680Sensor::getMetrics(meshtastic_Telemetry *measurement) void BME680Sensor::loadState() { #ifdef FSCom + spiLock->lock(); auto file = FSCom.open(bsecConfigFileName, FILE_O_READ); if (file) { file.read((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE); @@ -84,6 +86,7 @@ void BME680Sensor::loadState() } else { LOG_INFO("No %s state found (File: %s)", sensorName, bsecConfigFileName); } + spiLock->unlock(); #else LOG_ERROR("ERROR: Filesystem not implemented"); #endif @@ -92,6 +95,7 @@ void BME680Sensor::loadState() void BME680Sensor::updateState() { #ifdef FSCom + spiLock->lock(); bool update = false; if (stateUpdateCounter == 0) { /* First state update when IAQ accuracy is >= 3 */ @@ -127,6 +131,7 @@ void BME680Sensor::updateState() LOG_INFO("Can't write %s state (File: %s)", sensorName, bsecConfigFileName); } } + spiLock->unlock(); #else LOG_ERROR("ERROR: Filesystem not implemented"); #endif diff --git a/src/modules/Telemetry/Sensor/NAU7802Sensor.cpp b/src/modules/Telemetry/Sensor/NAU7802Sensor.cpp index 65f616686..075aedc52 100644 --- a/src/modules/Telemetry/Sensor/NAU7802Sensor.cpp +++ b/src/modules/Telemetry/Sensor/NAU7802Sensor.cpp @@ -5,6 +5,7 @@ #include "../mesh/generated/meshtastic/telemetry.pb.h" #include "FSCommon.h" #include "NAU7802Sensor.h" +#include "SPILock.h" #include "SafeFile.h" #include "TelemetrySensor.h" #include @@ -98,6 +99,7 @@ void NAU7802Sensor::tare() bool NAU7802Sensor::saveCalibrationData() { + spiLock->lock(); auto file = SafeFile(nau7802ConfigFileName); nau7802config.zeroOffset = nau7802.getZeroOffset(); nau7802config.calibrationFactor = nau7802.getCalibrationFactor(); @@ -112,12 +114,14 @@ bool NAU7802Sensor::saveCalibrationData() okay = true; } okay &= file.close(); + spiLock->unlock(); return okay; } bool NAU7802Sensor::loadCalibrationData() { + spiLock->lock(); auto file = FSCom.open(nau7802ConfigFileName, FILE_O_READ); bool okay = false; if (file) { @@ -134,6 +138,7 @@ bool NAU7802Sensor::loadCalibrationData() } else { LOG_INFO("No %s state found (File: %s)", sensorName, nau7802ConfigFileName); } + spiLock->unlock(); return okay; } diff --git a/src/xmodem.cpp b/src/xmodem.cpp index bf25e2da7..1d8c77760 100644 --- a/src/xmodem.cpp +++ b/src/xmodem.cpp @@ -49,6 +49,7 @@ **********************************************************************************************************************/ #include "xmodem.h" +#include "SPILock.h" #ifdef FSCom @@ -119,8 +120,11 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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; @@ -132,14 +136,18 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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)) { @@ -159,7 +167,9 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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; @@ -178,16 +188,21 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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: @@ -195,7 +210,9 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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; @@ -206,7 +223,9 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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)) { @@ -224,7 +243,9 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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; @@ -232,8 +253,11 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) 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)) {