diff --git a/arch/stm32/stm32.ini b/arch/stm32/stm32.ini new file mode 100644 index 0000000000..2cea4bbc58 --- /dev/null +++ b/arch/stm32/stm32.ini @@ -0,0 +1,36 @@ +[stm32_base] +extends = arduino_base +platform = ststm32 +platform_packages = platformio/framework-arduinoststm32@https://github.com/stm32duino/Arduino_Core_STM32.git#361a7fdb67e2a7104e99b4f42a802469eef8b129 + +build_type = release + +;board_build.flash_offset = 0x08000000 + +build_flags = + ${arduino_base.build_flags} + -flto + -Isrc/platform/stm32wl -g + -DMESHTASTIC_MINIMIZE_BUILD + -DDEBUG_MUTE +; -DVECT_TAB_OFFSET=0x08000000 + -DconfigUSE_CMSIS_RTOS_V2=1 +; -DSPI_MODE_0=SPI_MODE0 + -fmerge-all-constants + -ffunction-sections + -fdata-sections + +build_src_filter = + ${arduino_base.build_src_filter} - - - - - - - - - - - - - - + +board_upload.offset_address = 0x08000000 +upload_protocol = stlink + +lib_deps = + ${env.lib_deps} + charlesbaynham/OSFS@^1.2.3 + https://github.com/caveman99/Crypto.git#f61ae26a53f7a2d0ba5511625b8bf8eff3a35d5e + +lib_ignore = + mathertel/OneButton + Wire \ No newline at end of file diff --git a/arch/stm32/stm32wl5e.ini b/arch/stm32/stm32wl5e.ini deleted file mode 100644 index 4d74ade8fb..0000000000 --- a/arch/stm32/stm32wl5e.ini +++ /dev/null @@ -1,28 +0,0 @@ -[stm32wl5e_base] -platform_packages = platformio/framework-arduinoststm32 @ https://github.com/stm32duino/Arduino_Core_STM32.git#6e3f9910d0122e82a6c3438507dfac3d2fd80a39 -platform = ststm32 -board = generic_wl5e -framework = arduino - -build_type = debug - -build_flags = - ${arduino_base.build_flags} - -Isrc/platform/stm32wl -g - -DconfigUSE_CMSIS_RTOS_V2=1 - -DVECT_TAB_OFFSET=0x08000000 - -build_src_filter = - ${arduino_base.build_src_filter} - - - - - - - - - - - - - - - -board_upload.offset_address = 0x08000000 -upload_protocol = stlink - -lib_deps = - ${env.lib_deps} - https://github.com/kokke/tiny-AES-c.git#f06ac37fc31dfdaca2e0d9bec83f90d5663c319b - https://github.com/littlefs-project/littlefs.git#v2.5.1 - https://github.com/stm32duino/STM32FreeRTOS.git#10.3.1 - -lib_ignore = - mathertel/OneButton \ No newline at end of file diff --git a/boards/generic_wl5e.json b/boards/wiscore_rak3172.json similarity index 91% rename from boards/generic_wl5e.json rename to boards/wiscore_rak3172.json index 5c4bc24a75..714e09115e 100644 --- a/boards/generic_wl5e.json +++ b/boards/wiscore_rak3172.json @@ -1,5 +1,8 @@ { "build": { + "arduino": { + "variant_h": "variant_RAK3172_MODULE.h" + }, "core": "stm32", "cpu": "cortex-m4", "extra_flags": "-DSTM32WLxx -DSTM32WLE5xx -DARDUINO_GENERIC_WLE5CCUX", diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 7d3788c4d9..3017ec085c 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -24,6 +24,30 @@ SPIClass SPI1(HSPI); #endif // HAS_SDCARD +#if defined(ARCH_STM32WL) + +uint16_t OSFS::startOfEEPROM = 1; +uint16_t OSFS::endOfEEPROM = 2048; + +// 3) How do I read from the medium? +void OSFS::readNBytes(uint16_t address, unsigned int num, byte *output) +{ + for (uint16_t i = address; i < address + num; i++) { + *output = EEPROM.read(i); + output++; + } +} + +// 4) How to I write to the medium? +void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input) +{ + for (uint16_t i = address; i < address + num; i++) { + EEPROM.update(i, *input); + input++; + } +} +#endif + /** * @brief Copies a file from one location to another. * @@ -33,7 +57,33 @@ SPIClass SPI1(HSPI); */ bool copyFile(const char *from, const char *to) { -#ifdef FSCom +#ifdef ARCH_STM32WL + unsigned char cbuffer[2048]; + + // Var to hold the result of actions + OSFS::result r; + + r = OSFS::getFile(from, cbuffer); + + if (r == notfound) { + LOG_ERROR("Failed to open source file %s\n", from); + return false; + } else if (r == noerr) { + r = OSFS::newFile(to, cbuffer, true); + if (r == noerr) { + return true; + } else { + LOG_ERROR("OSFS Error %d\n", r); + return false; + } + + } else { + LOG_ERROR("OSFS Error %d\n", r); + return false; + } + return true; + +#elif defined(FSCom) unsigned char cbuffer[16]; File f1 = FSCom.open(from, FILE_O_READ); @@ -70,7 +120,13 @@ bool copyFile(const char *from, const char *to) */ bool renameFile(const char *pathFrom, const char *pathTo) { -#ifdef FSCom +#ifdef ARCH_STM32WL + if (copyFile(pathFrom, pathTo) && (OSFS::deleteFile(pathFrom) == OSFS::result::NO_ERROR)) { + return true; + } else { + return false; + } +#elif defined(FSCom) #ifdef ARCH_ESP32 // rename was fixed for ESP32 IDF LittleFS in April return FSCom.rename(pathFrom, pathTo); diff --git a/src/FSCommon.h b/src/FSCommon.h index 8fbabd9526..3d485d1b1d 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -15,10 +15,13 @@ #endif #if defined(ARCH_STM32WL) -#include "platform/stm32wl/InternalFileSystem.h" // STM32WL version -#define FSCom InternalFS -#define FSBegin() FSCom.begin() -using namespace LittleFS_Namespace; +// STM32WL series 2 Kbytes (8 rows of 256 bytes) +#include +#include + +// Useful consts +const OSFS::result noerr = OSFS::result::NO_ERROR; +const OSFS::result notfound = OSFS::result::FILE_NOT_FOUND; #endif #if defined(ARCH_RP2040) diff --git a/src/Power.cpp b/src/Power.cpp index 19c5c99375..138b06e719 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -200,7 +200,8 @@ class AnalogBatteryLevel : public HasBatteryLevel } #endif -#if HAS_TELEMETRY && !defined(ARCH_PORTDUINO) && !defined(HAS_PMU) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR +#if HAS_TELEMETRY && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !defined(HAS_PMU) && \ + !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR if (hasINA()) { LOG_DEBUG("Using INA on I2C addr 0x%x for device battery voltage\n", config.power.device_battery_ina_address); return getINAVoltage(); @@ -420,7 +421,7 @@ class AnalogBatteryLevel : public HasBatteryLevel } #endif -#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO) +#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) uint16_t getINAVoltage() { if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) { diff --git a/src/configuration.h b/src/configuration.h index 6351c35b18..b298d54243 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -259,6 +259,7 @@ along with this program. If not, see . #define MESHTASTIC_EXCLUDE_SCREEN 1 #define MESHTASTIC_EXCLUDE_MQTT 1 #define MESHTASTIC_EXCLUDE_POWERMON 1 +#define MESHTASTIC_EXCLUDE_I2C 1 #endif // Turn off all optional modules diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 8738e2722d..b831b0e719 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -1,7 +1,8 @@ #include "ScanI2CTwoWire.h" +#if !MESHTASTIC_EXCLUDE_I2C + #include "concurrency/LockGuard.h" -#include "configuration.h" #if defined(ARCH_PORTDUINO) #include "linux/LinuxHardwareI2C.h" #endif @@ -403,3 +404,4 @@ size_t ScanI2CTwoWire::countDevices() const { return foundDevices.size(); } +#endif \ No newline at end of file diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 82b48f6b47..c8dd96469a 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -1,5 +1,8 @@ #pragma once +#include "configuration.h" +#if !MESHTASTIC_EXCLUDE_I2C + #include #include #include @@ -55,4 +58,5 @@ class ScanI2CTwoWire : public ScanI2C uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const; DeviceType probeOLED(ScanI2C::DeviceAddress) const; -}; \ No newline at end of file +}; +#endif \ No newline at end of file diff --git a/src/freertosinc.h b/src/freertosinc.h index 166054241c..e9e6cd53a0 100644 --- a/src/freertosinc.h +++ b/src/freertosinc.h @@ -12,7 +12,7 @@ #include #endif -#if defined(ARDUINO_NRF52_ADAFRUIT) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RP2040) +#if defined(ARDUINO_NRF52_ADAFRUIT) || defined(ARDUINO_ARCH_RP2040) #define HAS_FREE_RTOS #include diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 66b0430f48..04c855ee29 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -1197,7 +1197,7 @@ int GPS::prepareDeepSleep(void *unused) GnssModel_t GPS::probe(int serialSpeed) { -#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_RP2040) +#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_RP2040) || defined(ARCH_STM32WL) _serial_gps->end(); _serial_gps->begin(serialSpeed); #else @@ -1291,7 +1291,7 @@ GnssModel_t GPS::probe(int serialSpeed) _serial_gps->write(_message_prt, sizeof(_message_prt)); delay(500); serialSpeed = 9600; -#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_RP2040) +#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_RP2040) || defined(ARCH_STM32WL) _serial_gps->end(); _serial_gps->begin(serialSpeed); #else diff --git a/src/gps/GeoCoord.cpp b/src/gps/GeoCoord.cpp index 2224bd2816..5abb25a06c 100644 --- a/src/gps/GeoCoord.cpp +++ b/src/gps/GeoCoord.cpp @@ -493,7 +493,7 @@ std::shared_ptr GeoCoord::pointAtDistance(double bearing, double range * The bearing in string format * @return Bearing in degrees */ -uint GeoCoord::bearingToDegrees(const char *bearing) +unsigned int GeoCoord::bearingToDegrees(const char *bearing) { if (strcmp(bearing, "N") == 0) return 0; @@ -537,7 +537,7 @@ uint GeoCoord::bearingToDegrees(const char *bearing) * The bearing in degrees * @return Bearing in string format */ -const char *GeoCoord::degreesToBearing(uint degrees) +const char *GeoCoord::degreesToBearing(unsigned int degrees) { if (degrees >= 348 || degrees < 11) return "N"; diff --git a/src/gps/GeoCoord.h b/src/gps/GeoCoord.h index b02d12afb8..ecdaf0ec7b 100644 --- a/src/gps/GeoCoord.h +++ b/src/gps/GeoCoord.h @@ -117,8 +117,8 @@ class GeoCoord static float bearing(double lat1, double lon1, double lat2, double lon2); static float rangeRadiansToMeters(double range_radians); static float rangeMetersToRadians(double range_meters); - static uint bearingToDegrees(const char *bearing); - static const char *degreesToBearing(uint degrees); + static unsigned int bearingToDegrees(const char *bearing); + static const char *degreesToBearing(unsigned int degrees); // Point to point conversions int32_t distanceTo(const GeoCoord &pointB); diff --git a/src/main.cpp b/src/main.cpp index 1879423449..dc5863bbc2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,11 @@ #include "concurrency/OSThread.h" #include "concurrency/Periodic.h" #include "detect/ScanI2C.h" + +#if !MESHTASTIC_EXCLUDE_I2C #include "detect/ScanI2CTwoWire.h" +#include +#endif #include "detect/axpDebug.h" #include "detect/einkScan.h" #include "graphics/RAKled.h" @@ -31,7 +35,6 @@ #include "shutdown.h" #include "sleep.h" #include "target_specific.h" -#include #include #include // #include @@ -159,8 +162,10 @@ bool pauseBluetoothLogging = false; bool pmu_found; +#if !MESHTASTIC_EXCLUDE_I2C // Array map of sensor types with i2c address and wire as we'll find in the i2c scan std::pair nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1] = {}; +#endif Router *router = NULL; // Users of router don't care what sort of subclass implements that API @@ -349,6 +354,7 @@ void setup() #endif +#if !MESHTASTIC_EXCLUDE_I2C #if defined(I2C_SDA1) && defined(ARCH_RP2040) Wire1.setSDA(I2C_SDA1); Wire1.setSCL(I2C_SCL1); @@ -373,6 +379,7 @@ void setup() #elif HAS_WIRE Wire.begin(); #endif +#endif #ifdef PIN_LCD_RESET // FIXME - move this someplace better, LCD is at address 0x3F @@ -405,6 +412,7 @@ void setup() powerStatus->observe(&power->newStatus); power->setup(); // Must be after status handler is installed, so that handler gets notified of the initial configuration +#if !MESHTASTIC_EXCLUDE_I2C // We need to scan here to decide if we have a screen for nodeDB.init() and because power has been applied to // accessories auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); @@ -560,6 +568,7 @@ void setup() SCANNER_TO_SENSORS_MAP(ScanI2C::DeviceType::DFROBOT_LARK, meshtastic_TelemetrySensorType_DFROBOT_LARK) i2cScanner.reset(); +#endif #ifdef HAS_SDCARD setupSDCard(); @@ -620,6 +629,7 @@ void setup() screen_model = meshtastic_Config_DisplayConfig_OledType_OLED_SH1107; // keep dimension of 128x64 #endif +#if !MESHTASTIC_EXCLUDE_I2C #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR if (acc_info.type != ScanI2C::DeviceType::NONE) { config.display.wake_on_tap_or_motion = true; @@ -635,6 +645,7 @@ void setup() ambientLightingThread = new AmbientLightingThread(rgb_found.type); } #endif +#endif #ifdef T_WATCH_S3 drv.begin(); @@ -721,6 +732,7 @@ void setup() RECORD_CRITICALERROR(meshtastic_CriticalErrorCode_NO_AXP192); // Record a hardware fault for missing hardware #endif +#if !MESHTASTIC_EXCLUDE_I2C // Don't call screen setup until after nodedb is setup (because we need // the current region name) #if defined(ST7735_CS) || defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ST7789_CS) || defined(HX8357_CS) || \ @@ -733,6 +745,7 @@ void setup() #else if (screen_found.port != ScanI2C::I2CPort::NO_I2C) screen->setup(); +#endif #endif screen->print("Started...\n"); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index c0bed34371..4257837b33 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -192,9 +192,11 @@ bool NodeDB::factoryReset() LOG_INFO("Performing factory reset!\n"); // first, remove the "/prefs" (this removes most prefs) rmDir("/prefs"); +#ifdef FSCom if (FSCom.exists("/static/rangetest.csv") && !FSCom.remove("/static/rangetest.csv")) { LOG_ERROR("Could not remove rangetest.csv file\n"); } +#endif // second, install default state (this will deal with the duplicate mac address issue) installDefaultDeviceState(); installDefaultConfig(); @@ -574,7 +576,7 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t state = LoadFileResult::DECODE_FAILED; } else { LOG_INFO("Loaded %s successfully\n", filename); - state = LoadFileResult::SUCCESS; + state = LoadFileResult::LOAD_SUCCESS; } f.close(); } else { @@ -582,7 +584,7 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t } #else LOG_ERROR("ERROR: Filesystem not implemented\n"); - state = LoadFileState::NO_FILESYSTEM; + state = LoadFileResult::NO_FILESYSTEM; #endif return state; } @@ -593,7 +595,7 @@ void NodeDB::loadFromDisk() auto state = loadProto(prefFileName, sizeof(meshtastic_DeviceState) + MAX_NUM_NODES * sizeof(meshtastic_NodeInfo), sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, &devicestate); - if (state != LoadFileResult::SUCCESS) { + if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultDeviceState(); // Our in RAM copy might now be corrupt } else { if (devicestate.version < DEVICESTATE_MIN_VER) { @@ -610,7 +612,7 @@ void NodeDB::loadFromDisk() state = loadProto(configFileName, meshtastic_LocalConfig_size, sizeof(meshtastic_LocalConfig), &meshtastic_LocalConfig_msg, &config); - if (state != LoadFileResult::SUCCESS) { + if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultConfig(); // Our in RAM copy might now be corrupt } else { if (config.version < DEVICESTATE_MIN_VER) { @@ -623,7 +625,7 @@ void NodeDB::loadFromDisk() state = loadProto(moduleConfigFileName, meshtastic_LocalModuleConfig_size, sizeof(meshtastic_LocalModuleConfig), &meshtastic_LocalModuleConfig_msg, &moduleConfig); - if (state != LoadFileResult::SUCCESS) { + if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultModuleConfig(); // Our in RAM copy might now be corrupt } else { if (moduleConfig.version < DEVICESTATE_MIN_VER) { @@ -636,7 +638,7 @@ void NodeDB::loadFromDisk() state = loadProto(channelFileName, meshtastic_ChannelFile_size, sizeof(meshtastic_ChannelFile), &meshtastic_ChannelFile_msg, &channelFile); - if (state != LoadFileResult::SUCCESS) { + if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultChannels(); // Our in RAM copy might now be corrupt } else { if (channelFile.version < DEVICESTATE_MIN_VER) { @@ -648,7 +650,7 @@ void NodeDB::loadFromDisk() } state = loadProto(oemConfigFile, meshtastic_OEMStore_size, sizeof(meshtastic_OEMStore), &meshtastic_OEMStore_msg, &oemStore); - if (state == LoadFileResult::SUCCESS) { + if (state == LoadFileResult::LOAD_SUCCESS) { LOG_INFO("Loaded OEMStore\n"); } diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 5207d8629b..258b7276dd 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -40,7 +40,7 @@ uint32_t sinceReceived(const meshtastic_MeshPacket *p); enum LoadFileResult { // Successfully opened the file - SUCCESS = 1, + LOAD_SUCCESS = 1, // File does not exist NOT_FOUND = 2, // Device does not have a filesystem diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 0b63b4a581..f0775741a7 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -42,7 +42,9 @@ void PhoneAPI::handleStartConfig() if (!isConnected()) { onConnectionChanged(true); observe(&service.fromNumChanged); +#ifdef FSCom observe(&xModem.packetReady); +#endif } // even if we were already connected - restart our state machine @@ -62,7 +64,9 @@ void PhoneAPI::close() state = STATE_SEND_NOTHING; unobserve(&service.fromNumChanged); +#ifdef FSCom unobserve(&xModem.packetReady); +#endif releasePhonePacket(); // Don't leak phone packets on shutdown releaseQueueStatusPhonePacket(); releaseMqttClientProxyPhonePacket(); @@ -110,7 +114,9 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) break; case meshtastic_ToRadio_xmodemPacket_tag: LOG_INFO("Got xmodem packet\n"); +#ifdef FSCom xModem.handlePacket(toRadioScratch.xmodemPacket); +#endif break; #if !MESHTASTIC_EXCLUDE_MQTT case meshtastic_ToRadio_mqttClientProxyMessage_tag: @@ -496,12 +502,14 @@ bool PhoneAPI::available() if (hasPacket) return true; +#ifdef FSCom if (xmodemPacketForPhone.control == meshtastic_XModem_Control_NUL) xmodemPacketForPhone = xModem.getForPhone(); if (xmodemPacketForPhone.control != meshtastic_XModem_Control_NUL) { xModem.resetForPhone(); return true; } +#endif #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_STOREFORWARD diff --git a/src/mesh/STM32WLE5JCInterface.h b/src/mesh/STM32WLE5JCInterface.h index 73d53d92f9..fad7933329 100644 --- a/src/mesh/STM32WLE5JCInterface.h +++ b/src/mesh/STM32WLE5JCInterface.h @@ -23,7 +23,7 @@ static const float tcxoVoltage = 1.7; * Wio-E5 module ONLY transmits through RFO_HP * Receive: PA4=1, PA5=0 * Transmit(high output power, SMPS mode): PA4=0, PA5=1 */ -static const RADIOLIB_PIN_TYPE rfswitch_pins[3] = {PA4, PA5, RADIOLIB_NC}; +static const RADIOLIB_PIN_TYPE rfswitch_pins[5] = {PA4, PA5, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC}; static const Module::RfSwitchMode_t rfswitch_table[4] = { {STM32WLx::MODE_IDLE, {LOW, LOW}}, {STM32WLx::MODE_RX, {HIGH, LOW}}, {STM32WLx::MODE_TX_HP, {LOW, HIGH}}, END_OF_MODE_TABLE}; diff --git a/src/mesh/mesh-pb-constants.cpp b/src/mesh/mesh-pb-constants.cpp index 93dbf0178b..676208e25a 100644 --- a/src/mesh/mesh-pb-constants.cpp +++ b/src/mesh/mesh-pb-constants.cpp @@ -1,6 +1,7 @@ -#include "mesh-pb-constants.h" -#include "FSCommon.h" #include "configuration.h" + +#include "FSCommon.h" +#include "mesh-pb-constants.h" #include #include #include @@ -15,6 +16,7 @@ size_t pb_encode_to_bytes(uint8_t *destbuf, size_t destbufsize, const pb_msgdesc LOG_ERROR("Panic: can't encode protobuf reason='%s'\n", PB_GET_ERROR(&stream)); assert( 0); // If this assert fails it probably means you made a field too large for the max limits specified in mesh.options + return 0; } else { return stream.bytes_written; } diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 98b789f41d..8939b972b9 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -259,11 +259,13 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta } case meshtastic_AdminMessage_delete_file_request_tag: { LOG_DEBUG("Client is requesting to delete file: %s\n", r->delete_file_request); +#ifdef FSCom if (FSCom.remove(r->delete_file_request)) { LOG_DEBUG("Successfully deleted file\n"); } else { LOG_DEBUG("Failed to delete file\n"); } +#endif break; } #ifdef ARCH_PORTDUINO diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index 84b5a3260e..524d37a3d9 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -1066,7 +1066,7 @@ void CannedMessageModule::loadProtoForModule() { if (nodeDB->loadProto(cannedMessagesConfigFile, meshtastic_CannedMessageModuleConfig_size, sizeof(meshtastic_CannedMessageModuleConfig), &meshtastic_CannedMessageModuleConfig_msg, - &cannedMessageModuleConfig) != LoadFileResult::SUCCESS) { + &cannedMessageModuleConfig) != LoadFileResult::LOAD_SUCCESS) { installDefaultCannedMessageModuleConfig(); } } diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index c025592401..652db04d3d 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -355,7 +355,7 @@ ExternalNotificationModule::ExternalNotificationModule() if (moduleConfig.external_notification.enabled) { if (nodeDB->loadProto(rtttlConfigFile, meshtastic_RTTTLConfig_size, sizeof(meshtastic_RTTTLConfig), - &meshtastic_RTTTLConfig_msg, &rtttlConfig) != LoadFileResult::SUCCESS) { + &meshtastic_RTTTLConfig_msg, &rtttlConfig) != LoadFileResult::LOAD_SUCCESS) { memset(rtttlConfig.ringtone, 0, sizeof(rtttlConfig.ringtone)); strncpy(rtttlConfig.ringtone, "24:d=32,o=5,b=565:f6,p,f6,4p,p,f6,p,f6,2p,p,b6,p,b6,p,b6,p,b6,p,b,p,b,p,b,p,b,p,b,p,b,p,b,p,b,1p.,2p.,p", diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index 300afc2460..5a0e36fea2 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -7,7 +7,9 @@ #include "input/cardKbI2cImpl.h" #include "input/kbMatrixImpl.h" #endif +#if !MESHTASTIC_EXCLUDE_ADMIN #include "modules/AdminModule.h" +#endif #if !MESHTASTIC_EXCLUDE_ATAK #include "modules/AtakPluginModule.h" #endif @@ -20,7 +22,9 @@ #if !MESHTASTIC_EXCLUDE_NEIGHBORINFO #include "modules/NeighborInfoModule.h" #endif +#if !MESHTASTIC_EXCLUDE_NODEINFO #include "modules/NodeInfoModule.h" +#endif #if !MESHTASTIC_EXCLUDE_GPS #include "modules/PositionModule.h" #endif @@ -88,8 +92,12 @@ void setupModules() #if (HAS_BUTTON || ARCH_PORTDUINO) && !MESHTASTIC_EXCLUDE_INPUTBROKER inputBroker = new InputBroker(); #endif +#if !MESHTASTIC_EXCLUDE_ADMIN adminModule = new AdminModule(); +#endif +#if !MESHTASTIC_EXCLUDE_NODEINFO nodeInfoModule = new NodeInfoModule(); +#endif #if !MESHTASTIC_EXCLUDE_GPS positionModule = new PositionModule(); #endif @@ -192,7 +200,9 @@ void setupModules() #endif #endif } else { +#if !MESHTASTIC_EXCLUDE_ADMIN adminModule = new AdminModule(); +#endif #if HAS_TELEMETRY new DeviceTelemetryModule(); #endif diff --git a/src/platform/stm32wl/InternalFileSystem.cpp b/src/platform/stm32wl/InternalFileSystem.cpp deleted file mode 100644 index d42a646a56..0000000000 --- a/src/platform/stm32wl/InternalFileSystem.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 hathach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "InternalFileSystem.h" -#include - -//--------------------------------------------------------------------+ -// LFS Disk IO -//--------------------------------------------------------------------+ - -static inline uint32_t lba2addr(uint32_t block) -{ - return ((uint32_t)LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; -} - -static int _internal_flash_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) -{ - (void)c; - - uint32_t addr = lba2addr(block) + off; - uint8_t prom; - - for (int i = 0; i < size; i++) { - prom = EEPROM.read(addr + i); - memcpy((char *)buffer + i, &prom, 1); - } - return 0; -} - -// Program a region in a block. The block must have previously -// been erased. Negative error codes are propagated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _internal_flash_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) -{ - (void)c; - - uint32_t addr = lba2addr(block) + off; - uint8_t prom; - - for (int i = 0; i < size; i++) { - memcpy(&prom, (char *)buffer + i, 1); - EEPROM.update(addr + i, prom); - } - return 0; -} - -// Erase a block. A block must be erased before being programmed. -// The state of an erased block is undefined. Negative error codes -// are propagated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _internal_flash_erase(const struct lfs_config *c, lfs_block_t block) -{ - (void)c; - - uint32_t addr = lba2addr(block); - - // implement as write 0xff to whole block address - for (int i = 0; i < LFS_BLOCK_SIZE; i++) { - EEPROM.update(addr, 0xff); - } - - return 0; -} - -// Sync the state of the underlying block device. Negative error codes -// are propagated to the user. -static int _internal_flash_sync(const struct lfs_config *c) -{ - // we don't use a ram cache, this is a noop - return 0; -} - -static struct lfs_config _InternalFSConfig = {.context = NULL, - - .read = _internal_flash_read, - .prog = _internal_flash_prog, - .erase = _internal_flash_erase, - .sync = _internal_flash_sync, - - .read_size = LFS_CACHE_SIZE, - .prog_size = LFS_CACHE_SIZE, - .block_size = LFS_BLOCK_SIZE, - .block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE, - .block_cycles = - 500, // protection against wear leveling (suggested values between 100-1000) - .cache_size = LFS_CACHE_SIZE, - .lookahead_size = LFS_CACHE_SIZE, - - .read_buffer = lfs_read_buffer, - .prog_buffer = lfs_prog_buffer, - .lookahead_buffer = lfs_lookahead_buffer}; - -InternalFileSystem InternalFS; - -//--------------------------------------------------------------------+ -// -//--------------------------------------------------------------------+ - -InternalFileSystem::InternalFileSystem(void) : LittleFS(&_InternalFSConfig) {} - -bool InternalFileSystem::begin(void) -{ - // failed to mount, erase all sector then format and mount again - if (!LittleFS::begin()) { - // Erase all sectors of internal flash region for Filesystem. - // implement as write 0xff to whole block address - for (uint32_t addr = LFS_FLASH_ADDR; addr < (LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE); addr++) { - EEPROM.update(addr, 0xff); - } - - // lfs format - this->format(); - - // mount again if still failed, give up - if (!LittleFS::begin()) - return false; - } - - return true; -} diff --git a/src/platform/stm32wl/InternalFileSystem.h b/src/platform/stm32wl/InternalFileSystem.h deleted file mode 100644 index 66344194e1..0000000000 --- a/src/platform/stm32wl/InternalFileSystem.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 hathach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef INTERNALFILESYSTEM_H_ -#define INTERNALFILESYSTEM_H_ - -#include "LittleFS.h" - -// The EEPROM Library assumes our usable flash area starts at logical 0 -#define LFS_FLASH_ADDR 0 - -// use the built in EEPROM emulation. Total Size is 2Kbyte -#define LFS_BLOCK_SIZE 128 // min. block size is 128 to fit CTZ pointers -#define LFS_CACHE_SIZE 16 - -#define LFS_FLASH_TOTAL_SIZE FLASH_PAGE_SIZE - -static uint8_t lfs_read_buffer[LFS_CACHE_SIZE] = {0}; -static uint8_t lfs_prog_buffer[LFS_CACHE_SIZE] = {0}; -static uint8_t lfs_lookahead_buffer[LFS_CACHE_SIZE] = {0}; - -class InternalFileSystem : public LittleFS -{ - public: - InternalFileSystem(void); - - // overwrite to also perform low level format (sector erase of whole flash region) - bool begin(void); -}; - -extern InternalFileSystem InternalFS; - -#endif /* INTERNALFILESYSTEM_H_ */ diff --git a/src/platform/stm32wl/LittleFS.cpp b/src/platform/stm32wl/LittleFS.cpp deleted file mode 100644 index b1267d88a6..0000000000 --- a/src/platform/stm32wl/LittleFS.cpp +++ /dev/null @@ -1,258 +0,0 @@ -#include -#include - -#include "LittleFS.h" - -using namespace LittleFS_Namespace; - -//--------------------------------------------------------------------+ -// Implementation -//--------------------------------------------------------------------+ - -LittleFS::LittleFS(void) : LittleFS(NULL) {} - -LittleFS::LittleFS(struct lfs_config *cfg) -{ - memset(&_lfs, 0, sizeof(_lfs)); - _lfs_cfg = cfg; - _mounted = false; - _mutex = xSemaphoreCreateMutexStatic(&this->_MutexStorageSpace); -} - -LittleFS::~LittleFS() {} - -// Initialize and mount the file system -// Return true if mounted successfully else probably corrupted. -// User should format the disk and try again -bool LittleFS::begin(struct lfs_config *cfg) -{ - _lockFS(); - - bool ret; - // not a loop, just an quick way to short-circuit on error - do { - if (_mounted) { - ret = true; - break; - } - if (cfg) { - _lfs_cfg = cfg; - } - if (nullptr == _lfs_cfg) { - ret = false; - break; - } - // actually attempt to mount, and log error if one occurs - int err = lfs_mount(&_lfs, _lfs_cfg); - PRINT_LFS_ERR(err); - _mounted = (err == LFS_ERR_OK); - ret = _mounted; - } while (0); - - _unlockFS(); - return ret; -} - -// Tear down and unmount file system -void LittleFS::end(void) -{ - _lockFS(); - - if (_mounted) { - _mounted = false; - int err = lfs_unmount(&_lfs); - PRINT_LFS_ERR(err); - (void)err; - } - - _unlockFS(); -} - -bool LittleFS::format(void) -{ - _lockFS(); - - int err = LFS_ERR_OK; - bool attemptMount = _mounted; - // not a loop, just an quick way to short-circuit on error - do { - // if already mounted: umount first -> format -> remount - if (_mounted) { - _mounted = false; - err = lfs_unmount(&_lfs); - if (LFS_ERR_OK != err) { - PRINT_LFS_ERR(err); - break; - } - } - err = lfs_format(&_lfs, _lfs_cfg); - if (LFS_ERR_OK != err) { - PRINT_LFS_ERR(err); - break; - } - - if (attemptMount) { - err = lfs_mount(&_lfs, _lfs_cfg); - if (LFS_ERR_OK != err) { - PRINT_LFS_ERR(err); - break; - } - _mounted = true; - } - // success! - } while (0); - - _unlockFS(); - return LFS_ERR_OK == err; -} - -// Open a file or folder -LittleFS_Namespace::File LittleFS::open(char const *filepath, uint8_t mode) -{ - // No lock is required here ... the File() object will synchronize with the mutex provided - return LittleFS_Namespace::File(filepath, mode, *this); -} - -// Check if file or folder exists -bool LittleFS::exists(char const *filepath) -{ - struct lfs_info info; - _lockFS(); - - bool ret = (0 == lfs_stat(&_lfs, filepath, &info)); - - _unlockFS(); - return ret; -} - -// Create a directory, create intermediate parent if needed -bool LittleFS::mkdir(char const *filepath) -{ - bool ret = true; - const char *slash = filepath; - if (slash[0] == '/') - slash++; // skip root '/' - - _lockFS(); - - // make intermediate parent directory(ies) - while (NULL != (slash = strchr(slash, '/'))) { - char parent[slash - filepath + 1] = {0}; - memcpy(parent, filepath, slash - filepath); - - int rc = lfs_mkdir(&_lfs, parent); - if (rc != LFS_ERR_OK && rc != LFS_ERR_EXIST) { - PRINT_LFS_ERR(rc); - ret = false; - break; - } - slash++; - } - // make the final requested directory - if (ret) { - int rc = lfs_mkdir(&_lfs, filepath); - if (rc != LFS_ERR_OK && rc != LFS_ERR_EXIST) { - PRINT_LFS_ERR(rc); - ret = false; - } - } - - _unlockFS(); - return ret; -} - -// Remove a file -bool LittleFS::remove(char const *filepath) -{ - _lockFS(); - - int err = lfs_remove(&_lfs, filepath); - PRINT_LFS_ERR(err); - - _unlockFS(); - return LFS_ERR_OK == err; -} - -// Rename a file -bool LittleFS::rename(char const *oldfilepath, char const *newfilepath) -{ - _lockFS(); - - int err = lfs_rename(&_lfs, oldfilepath, newfilepath); - PRINT_LFS_ERR(err); - - _unlockFS(); - return LFS_ERR_OK == err; -} - -// Remove a folder -bool LittleFS::rmdir(char const *filepath) -{ - _lockFS(); - - int err = lfs_remove(&_lfs, filepath); - PRINT_LFS_ERR(err); - - _unlockFS(); - return LFS_ERR_OK == err; -} - -// Remove a folder recursively -bool LittleFS::rmdir_r(char const *filepath) -{ - /* adafruit: lfs is modified to remove non-empty folder, - According to below issue, comment these 2 line won't corrupt filesystem - at least when using LFS v1. If moving to LFS v2, see tracked issue - to see if issues (such as the orphans in threaded linked list) are resolved. - https://github.com/ARMmbed/littlefs/issues/43 - */ - _lockFS(); - - int err = lfs_remove(&_lfs, filepath); - PRINT_LFS_ERR(err); - - _unlockFS(); - return LFS_ERR_OK == err; -} - -//------------- Debug -------------// -#if CFG_DEBUG - -const char *dbg_strerr_lfs(int32_t err) -{ - switch (err) { - case LFS_ERR_OK: - return "LFS_ERR_OK"; - case LFS_ERR_IO: - return "LFS_ERR_IO"; - case LFS_ERR_CORRUPT: - return "LFS_ERR_CORRUPT"; - case LFS_ERR_NOENT: - return "LFS_ERR_NOENT"; - case LFS_ERR_EXIST: - return "LFS_ERR_EXIST"; - case LFS_ERR_NOTDIR: - return "LFS_ERR_NOTDIR"; - case LFS_ERR_ISDIR: - return "LFS_ERR_ISDIR"; - case LFS_ERR_NOTEMPTY: - return "LFS_ERR_NOTEMPTY"; - case LFS_ERR_BADF: - return "LFS_ERR_BADF"; - case LFS_ERR_INVAL: - return "LFS_ERR_INVAL"; - case LFS_ERR_NOSPC: - return "LFS_ERR_NOSPC"; - case LFS_ERR_NOMEM: - return "LFS_ERR_NOMEM"; - - default: - static char errcode[10]; - sprintf(errcode, "%ld", err); - return errcode; - } - - return NULL; -} - -#endif diff --git a/src/platform/stm32wl/LittleFS.h b/src/platform/stm32wl/LittleFS.h deleted file mode 100644 index 4a0b01af2b..0000000000 --- a/src/platform/stm32wl/LittleFS.h +++ /dev/null @@ -1,85 +0,0 @@ - -#ifndef LITTLEFS_H_ -#define LITTLEFS_H_ - -#include - -#include "lfs.h" - -#include "LittleFS_File.h" - -#include "FreeRTOS.h" // tied to FreeRTOS for serialization -#include "semphr.h" - -class LittleFS -{ - public: - LittleFS(void); - explicit LittleFS(struct lfs_config *cfg); - virtual ~LittleFS(); - - bool begin(struct lfs_config *cfg = NULL); - void end(void); - - // Open the specified file/directory with the supplied mode (e.g. read or - // write, etc). Returns a File object for interacting with the file. - // Note that currently only one file can be open at a time. - LittleFS_Namespace::File open(char const *filename, uint8_t mode = LittleFS_Namespace::FILE_O_READ); - - // Methods to determine if the requested file path exists. - bool exists(char const *filepath); - - // Create the requested directory hierarchy--if intermediate directories - // do not exist they will be created. - bool mkdir(char const *filepath); - - // Delete the file. - bool remove(char const *filepath); - - // Rename the file. - bool rename(char const *oldfilepath, char const *newfilepath); - - // Delete a folder (must be empty) - bool rmdir(char const *filepath); - - // Delete a folder (recursively) - bool rmdir_r(char const *filepath); - - // format file system - bool format(void); - - /*------------------------------------------------------------------*/ - /* INTERNAL USAGE ONLY - * Although declare as public, it is meant to be invoked by internal - * code. User should not call these directly - *------------------------------------------------------------------*/ - lfs_t *_getFS(void) { return &_lfs; } - void _lockFS(void) { xSemaphoreTake(_mutex, portMAX_DELAY); } - void _unlockFS(void) { xSemaphoreGive(_mutex); } - - protected: - bool _mounted; - struct lfs_config *_lfs_cfg; - lfs_t _lfs; - SemaphoreHandle_t _mutex; - - private: - StaticSemaphore_t _MutexStorageSpace; -}; - -#if !CFG_DEBUG -#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL) -#define PRINT_LFS_ERR(_err) -#else -#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs) -#define PRINT_LFS_ERR(_err) \ - do { \ - if (_err) { \ - VERIFY_MESS((long int)_err, dbg_strerr_lfs); \ - } \ - } while (0) // LFS_ERR are of type int, VERIFY_MESS expects long_int - -const char *dbg_strerr_lfs(int32_t err); -#endif - -#endif /* LITTLEFS_H_ */ diff --git a/src/platform/stm32wl/LittleFS_File.cpp b/src/platform/stm32wl/LittleFS_File.cpp deleted file mode 100644 index 548a3d3009..0000000000 --- a/src/platform/stm32wl/LittleFS_File.cpp +++ /dev/null @@ -1,362 +0,0 @@ -#include - -#include "LittleFS.h" - -#include - -//--------------------------------------------------------------------+ -// MACRO TYPEDEF CONSTANT ENUM DECLARATION -//--------------------------------------------------------------------+ - -using namespace LittleFS_Namespace; - -File::File(LittleFS &fs) -{ - _fs = &fs; - _is_dir = false; - _name[0] = 0; - _dir_path = NULL; - - _dir = NULL; - _file = NULL; -} - -File::File(char const *filename, uint8_t mode, LittleFS &fs) : File(fs) -{ - // public constructor calls public API open(), which will obtain the mutex - this->open(filename, mode); -} - -bool File::_open_file(char const *filepath, uint8_t mode) -{ - int flags = (mode == FILE_O_READ) ? LFS_O_RDONLY : (mode == FILE_O_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : 0; - - if (flags) { - _file = (lfs_file_t *)malloc(sizeof(lfs_file_t)); - if (!_file) - return false; - - int rc = lfs_file_open(_fs->_getFS(), _file, filepath, flags); - - if (rc) { - // failed to open - PRINT_LFS_ERR(rc); - return false; - } - - // move to end of file - if (mode == FILE_O_WRITE) - lfs_file_seek(_fs->_getFS(), _file, 0, LFS_SEEK_END); - - _is_dir = false; - } - - return true; -} - -bool File::_open_dir(char const *filepath) -{ - _dir = (lfs_dir_t *)malloc(sizeof(lfs_dir_t)); - if (!_dir) - return false; - - int rc = lfs_dir_open(_fs->_getFS(), _dir, filepath); - - if (rc) { - // failed to open - PRINT_LFS_ERR(rc); - return false; - } - - _is_dir = true; - - _dir_path = (char *)malloc(strlen(filepath) + 1); - strcpy(_dir_path, filepath); - - return true; -} - -bool File::open(char const *filepath, uint8_t mode) -{ - bool ret = false; - _fs->_lockFS(); - - ret = this->_open(filepath, mode); - - _fs->_unlockFS(); - return ret; -} - -bool File::_open(char const *filepath, uint8_t mode) -{ - bool ret = false; - - // close if currently opened - if (this->isOpen()) - _close(); - - struct lfs_info info; - int rc = lfs_stat(_fs->_getFS(), filepath, &info); - - if (LFS_ERR_OK == rc) { - // file existed, open file or directory accordingly - ret = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath); - } else if (LFS_ERR_NOENT == rc) { - // file not existed, only proceed with FILE_O_WRITE mode - if (mode == FILE_O_WRITE) - ret = _open_file(filepath, mode); - } else { - PRINT_LFS_ERR(rc); - } - - // save bare file name - if (ret) { - char const *splash = strrchr(filepath, '/'); - strncpy(_name, splash ? (splash + 1) : filepath, LFS_NAME_MAX); - } - return ret; -} - -size_t File::write(uint8_t ch) -{ - return write(&ch, 1); -} - -size_t File::write(uint8_t const *buf, size_t size) -{ - lfs_ssize_t wrcount = 0; - _fs->_lockFS(); - - if (!this->_is_dir) { - wrcount = lfs_file_write(_fs->_getFS(), _file, buf, size); - if (wrcount < 0) { - wrcount = 0; - } - } - - _fs->_unlockFS(); - return wrcount; -} - -int File::read(void) -{ - // this thin wrapper relies on called function to synchronize - int ret = -1; - uint8_t ch; - if (read(&ch, 1) > 0) { - ret = static_cast(ch); - } - return ret; -} - -int File::read(void *buf, uint16_t nbyte) -{ - int ret = 0; - _fs->_lockFS(); - - if (!this->_is_dir) { - ret = lfs_file_read(_fs->_getFS(), _file, buf, nbyte); - } - - _fs->_unlockFS(); - return ret; -} - -int File::peek(void) -{ - int ret = -1; - _fs->_lockFS(); - - if (!this->_is_dir) { - uint32_t pos = lfs_file_tell(_fs->_getFS(), _file); - uint8_t ch = 0; - if (lfs_file_read(_fs->_getFS(), _file, &ch, 1) > 0) { - ret = static_cast(ch); - } - (void)lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET); - } - - _fs->_unlockFS(); - return ret; -} - -int File::available(void) -{ - int ret = 0; - _fs->_lockFS(); - - if (!this->_is_dir) { - uint32_t fsize = lfs_file_size(_fs->_getFS(), _file); - uint32_t pos = lfs_file_tell(_fs->_getFS(), _file); - ret = fsize - pos; - } - - _fs->_unlockFS(); - return ret; -} - -bool File::seek(uint32_t pos) -{ - bool ret = false; - _fs->_lockFS(); - - if (!this->_is_dir) { - ret = lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET) >= 0; - } - - _fs->_unlockFS(); - return ret; -} - -uint32_t File::position(void) -{ - uint32_t ret = 0; - _fs->_lockFS(); - - if (!this->_is_dir) { - ret = lfs_file_tell(_fs->_getFS(), _file); - } - - _fs->_unlockFS(); - return ret; -} - -uint32_t File::size(void) -{ - uint32_t ret = 0; - _fs->_lockFS(); - - if (!this->_is_dir) { - ret = lfs_file_size(_fs->_getFS(), _file); - } - - _fs->_unlockFS(); - return ret; -} - -bool File::truncate(uint32_t pos) -{ - int32_t ret = LFS_ERR_ISDIR; - _fs->_lockFS(); - if (!this->_is_dir) { - ret = lfs_file_truncate(_fs->_getFS(), _file, pos); - } - _fs->_unlockFS(); - return (ret == 0); -} - -bool File::truncate(void) -{ - int32_t ret = LFS_ERR_ISDIR; - _fs->_lockFS(); - if (!this->_is_dir) { - uint32_t pos = lfs_file_tell(_fs->_getFS(), _file); - ret = lfs_file_truncate(_fs->_getFS(), _file, pos); - } - _fs->_unlockFS(); - return (ret == 0); -} - -void File::flush(void) -{ - _fs->_lockFS(); - - if (!this->_is_dir) { - lfs_file_sync(_fs->_getFS(), _file); - } - - _fs->_unlockFS(); - return; -} - -void File::close(void) -{ - _fs->_lockFS(); - this->_close(); - _fs->_unlockFS(); -} - -void File::_close(void) -{ - if (this->isOpen()) { - if (this->_is_dir) { - lfs_dir_close(_fs->_getFS(), _dir); - free(_dir); - _dir = NULL; - - if (this->_dir_path) - free(_dir_path); - _dir_path = NULL; - } else { - lfs_file_close(this->_fs->_getFS(), _file); - free(_file); - _file = NULL; - } - } -} - -File::operator bool(void) -{ - return isOpen(); -} - -bool File::isOpen(void) -{ - return (_file != NULL) || (_dir != NULL); -} - -// WARNING -- although marked as `const`, the values pointed -// to may change. For example, if the same File -// object has `open()` called with a different -// file or directory name, this same pointer will -// suddenly (unexpectedly?) have different values. -char const *File::name(void) -{ - return this->_name; -} - -bool File::isDirectory(void) -{ - return this->_is_dir; -} - -File File::openNextFile(uint8_t mode) -{ - _fs->_lockFS(); - - File ret(*_fs); - if (this->_is_dir) { - struct lfs_info info; - int rc; - - // lfs_dir_read returns 0 when reaching end of directory, 1 if found an entry - // Skip the "." and ".." entries ... - do { - rc = lfs_dir_read(_fs->_getFS(), _dir, &info); - } while (rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name))); - - if (rc == 1) { - // string cat name with current folder - char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1]; // potential for significant stack usage - strcpy(filepath, _dir_path); - if (!(_dir_path[0] == '/' && _dir_path[1] == 0)) - strcat(filepath, "/"); // only add '/' if cwd is not root - strcat(filepath, info.name); - - (void)ret._open(filepath, mode); // return value is ignored ... caller is expected to check isOpened() - } else if (rc < 0) { - PRINT_LFS_ERR(rc); - } - } - _fs->_unlockFS(); - return ret; -} - -void File::rewindDirectory(void) -{ - _fs->_lockFS(); - if (this->_is_dir) { - lfs_dir_rewind(_fs->_getFS(), _dir); - } - _fs->_unlockFS(); -} diff --git a/src/platform/stm32wl/LittleFS_File.h b/src/platform/stm32wl/LittleFS_File.h deleted file mode 100644 index e88a2790d8..0000000000 --- a/src/platform/stm32wl/LittleFS_File.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef LITTLEFS_FILE_H_ -#define LITTLEFS_FILE_H_ - -// Forward declaration -class LittleFS; - -namespace LittleFS_Namespace -{ - -// avoid conflict with other FileSystem FILE_READ/FILE_WRITE -enum { - FILE_O_READ = 0, - FILE_O_WRITE = 1, -}; - -class File : public Stream -{ - public: - explicit File(LittleFS &fs); - File(char const *filename, uint8_t mode, LittleFS &fs); - - public: - bool open(char const *filename, uint8_t mode); - - //------------- Stream API -------------// - virtual size_t write(uint8_t ch); - virtual size_t write(uint8_t const *buf, size_t size); - size_t write(const char *str) - { - if (str == NULL) - return 0; - return write((const uint8_t *)str, strlen(str)); - } - size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); } - - virtual int read(void); - int read(void *buf, uint16_t nbyte); - - virtual int peek(void); - virtual int available(void); - virtual void flush(void); - - bool seek(uint32_t pos); - uint32_t position(void); - uint32_t size(void); - - bool truncate(uint32_t pos); - bool truncate(void); - - void close(void); - - operator bool(void); - - bool isOpen(void); - char const *name(void); - - bool isDirectory(void); - File openNextFile(uint8_t mode = FILE_O_READ); - void rewindDirectory(void); - - private: - LittleFS *_fs; - - bool _is_dir; - - union { - lfs_file_t *_file; - lfs_dir_t *_dir; - }; - - char *_dir_path; - char _name[LFS_NAME_MAX + 1]; - - bool _open(char const *filepath, uint8_t mode); - bool _open_file(char const *filepath, uint8_t mode); - bool _open_dir(char const *filepath); - void _close(void); -}; - -} // namespace LittleFS_Namespace - -#endif /* LITTLEFS_FILE_H_ */ diff --git a/src/platform/stm32wl/STM32WLCryptoEngine.cpp b/src/platform/stm32wl/STM32WLCryptoEngine.cpp index 7367a2bc03..4debdf78e1 100644 --- a/src/platform/stm32wl/STM32WLCryptoEngine.cpp +++ b/src/platform/stm32wl/STM32WLCryptoEngine.cpp @@ -1,33 +1,64 @@ +#undef RNG +#include "AES.h" +#include "CTR.h" #include "CryptoEngine.h" -#include "aes.hpp" #include "configuration.h" class STM32WLCryptoEngine : public CryptoEngine { + + CTRCommon *ctr = NULL; + public: STM32WLCryptoEngine() {} ~STM32WLCryptoEngine() {} + virtual void setKey(const CryptoKey &k) override + { + CryptoEngine::setKey(k); + LOG_DEBUG("Installing AES%d key!\n", key.length * 8); + if (ctr) { + delete ctr; + ctr = NULL; + } + if (key.length != 0) { + if (key.length == 16) + ctr = new CTR(); + else + ctr = new CTR(); + + ctr->setKey(key.bytes, key.length); + } + } /** * Encrypt a packet * * @param bytes is updated in place */ - virtual void encrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes) override + virtual void encrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override { if (key.length > 0) { - AES_ctx ctx; - initNonce(fromNode, packetNum); - AES_init_ctx_iv(&ctx, key.bytes, nonce); - AES_CTR_xcrypt_buffer(&ctx, bytes, numBytes); + initNonce(fromNode, packetId); + if (numBytes <= MAX_BLOCKSIZE) { + static uint8_t scratch[MAX_BLOCKSIZE]; + memcpy(scratch, bytes, numBytes); + memset(scratch + numBytes, 0, + sizeof(scratch) - numBytes); // Fill rest of buffer with zero (in case cypher looks at it) + + ctr->setIV(nonce, sizeof(nonce)); + ctr->setCounterSize(4); + ctr->encrypt(bytes, scratch, numBytes); + } else { + LOG_ERROR("Packet too large for crypto engine: %d. noop encryption!\n", numBytes); + } } } - virtual void decrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes) override + virtual void decrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override { // For CTR, the implementation is the same - encrypt(fromNode, packetNum, numBytes, bytes); + encrypt(fromNode, packetId, numBytes, bytes); } private: diff --git a/src/platform/stm32wl/main-stm32wl.cpp b/src/platform/stm32wl/main-stm32wl.cpp index 60c3cce107..3eddbb3cfb 100644 --- a/src/platform/stm32wl/main-stm32wl.cpp +++ b/src/platform/stm32wl/main-stm32wl.cpp @@ -26,11 +26,3 @@ void getMacAddr(uint8_t *dmac) } void cpuDeepSleep(uint32_t msecToWake) {} - -/* pacify libc_nano */ -extern "C" { -int _gettimeofday(struct timeval *tv, void *tzvp) -{ - return -1; -} -} \ No newline at end of file diff --git a/src/xmodem.cpp b/src/xmodem.cpp index 852ff34531..de73e86684 100644 --- a/src/xmodem.cpp +++ b/src/xmodem.cpp @@ -50,6 +50,8 @@ #include "xmodem.h" +#ifdef FSCom + XModemAdapter xModem; XModemAdapter::XModemAdapter() {} @@ -248,4 +250,5 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) // Unknown control character break; } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/xmodem.h b/src/xmodem.h index 2ba0bb39f1..4cfcb43e18 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -38,6 +38,8 @@ #define MAXRETRANS 25 +#ifdef FSCom + class XModemAdapter { public: @@ -75,3 +77,4 @@ class XModemAdapter }; extern XModemAdapter xModem; +#endif // FSCom \ No newline at end of file diff --git a/variants/rak3172/platformio.ini b/variants/rak3172/platformio.ini new file mode 100644 index 0000000000..63766c988c --- /dev/null +++ b/variants/rak3172/platformio.ini @@ -0,0 +1,12 @@ +[env:rak3172] +extends = stm32_base +board_level = extra +board = wiscore_rak3172 +build_flags = + ${stm32_base.build_flags} + -Ivariants/rak3172 + -DHAL_DAC_MODULE_ONLY + -DSERIAL_UART_INSTANCE=1 + -DPIN_SERIAL_RX=PB7 + -DPIN_SERIAL_TX=PB6 +upload_port = stlink \ No newline at end of file diff --git a/variants/rak3172/variant.h b/variants/rak3172/variant.h new file mode 100644 index 0000000000..21de65b2cd --- /dev/null +++ b/variants/rak3172/variant.h @@ -0,0 +1,12 @@ +/* +This variant is a work in progress. +Do not expect a working Meshtastic device with this target. +*/ + +#ifndef _VARIANT_RAK3172_ +#define _VARIANT_RAK3172_ + +#define USE_STM32WLx +#define MAX_NUM_NODES 10 + +#endif \ No newline at end of file diff --git a/variants/wio-e5/platformio.ini b/variants/wio-e5/platformio.ini index 07f6efa6df..12cd6190d2 100644 --- a/variants/wio-e5/platformio.ini +++ b/variants/wio-e5/platformio.ini @@ -1,11 +1,34 @@ [env:wio-e5] -extends = stm32wl5e_base +extends = stm32_base board_level = extra +board = lora_e5_dev_board build_flags = - ${stm32wl5e_base.build_flags} + ${stm32_base.build_flags} -Ivariants/wio-e5 - -DHAL_DAC_MODULE_ONLY -DSERIAL_UART_INSTANCE=1 -DPIN_SERIAL_RX=PB7 -DPIN_SERIAL_TX=PB6 + -DHAL_DAC_MODULE_ONLY + -DHAL_ADC_MODULE_DISABLED + -DHAL_COMP_MODULE_DISABLED + -DHAL_CRC_MODULE_DISABLED + -DHAL_CRYP_MODULE_DISABLED + -DHAL_GTZC_MODULE_DISABLED + -DHAL_HSEM_MODULE_DISABLED + -DHAL_I2C_MODULE_DISABLED + -DHAL_I2S_MODULE_DISABLED + -DHAL_IPCC_MODULE_DISABLED + -DHAL_IRDA_MODULE_DISABLED + -DHAL_IWDG_MODULE_DISABLED + -DHAL_LPTIM_MODULE_DISABLED + -DHAL_PKA_MODULE_DISABLED + -DHAL_RNG_MODULE_DISABLED + -DHAL_RTC_MODULE_DISABLED + -DHAL_SMARTCARD_MODULE_DISABLED + -DHAL_SMBUS_MODULE_DISABLED + -DHAL_TIM_MODULE_DISABLED + -DHAL_WWDG_MODULE_DISABLED + -DHAL_EXTI_MODULE_DISABLED +; -D PIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF + upload_port = stlink \ No newline at end of file diff --git a/variants/wio-e5/variant.h b/variants/wio-e5/variant.h index 86e58bcb2e..b4345a530e 100644 --- a/variants/wio-e5/variant.h +++ b/variants/wio-e5/variant.h @@ -13,5 +13,6 @@ Do not expect a working Meshtastic device with this target. #define _VARIANT_WIOE5_ #define USE_STM32WLx +#define MAX_NUM_NODES 10 #endif \ No newline at end of file