diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a51b8f6..9f1a7c64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +### Changed + +### Hardware + + +## [26.1.10] + +### Added + ### Changed - Added feed forward, disabled PowerTable for ERG lookup. - Added tests and removal of duplicates in pt column. diff --git a/CustomCharacteristic.md b/CustomCharacteristic.md index 3943639e..f6493477 100644 --- a/CustomCharacteristic.md +++ b/CustomCharacteristic.md @@ -76,6 +76,8 @@ From BLE_common.h |BLE_targetPosition |0x19 |int36|Position (in steps) the motor is maintaining. | |BLE_externalControl |0x1A |bool |01 disables internal calculation of targetPosition.| |BLE_syncMode |0x1B |bool |01 stops motor movement for external calibration | +|BLE_UDPLogging |0x2E |bool |Enable/disable UDP log streaming | +|BLE_BLELogging |0x30 |bool/str|Write: enable/disable BLE log streaming. Read: returns last log message| *syncMode will disable the movement of the stepper motor by forcing stepperPosition = targetPosition prior to the motor control. While this mode is enabled, it allows the client to set parameters like incline and shifterPosition without moving the motor from it's current position. Once the parameters are set, this mode should be turned back off and SS2K will resume normal operation. diff --git a/dependencies.lock b/dependencies.lock index b7f40e3b..a3a69572 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -96,6 +96,6 @@ direct_dependencies: - espressif/network_provisioning - idf - joltwallet/littlefs -manifest_hash: e7ce7a886dfdb3cc5cd59acbdb4ff333c0a91c6e4de55b9fd6323d866c7cc691 +manifest_hash: df9baa925ebc2a028ee0e349be53f2169bb7ac38b2cb9c63b3ebf69123c43ef8 target: esp32 version: 2.0.0 diff --git a/include/BLE_Custom_Characteristic.h b/include/BLE_Custom_Characteristic.h index 441fe4ad..4c1eff5e 100644 --- a/include/BLE_Custom_Characteristic.h +++ b/include/BLE_Custom_Characteristic.h @@ -63,6 +63,7 @@ const uint8_t BLE_hMax = 0x2B; // Maximum homing value const uint8_t BLE_homingSensitivity = 0x2C; // Homing sensitivity value const uint8_t BLE_pTab4Pwr = 0x2D; // Use power values for power table const uint8_t BLE_UDPLogging = 0x2E; // Enable or disable UDP logging +const uint8_t BLE_BLELogging = 0x30; // Enable or disable BLE logging class BLE_ss2kCustomCharacteristic { public: diff --git a/include/BleAppender.h b/include/BleAppender.h new file mode 100644 index 00000000..5acc7e55 --- /dev/null +++ b/include/BleAppender.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2020 Anthony Doud & Joel Baranick + * All rights reserved + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#pragma once + +#include "LogAppender.h" +#include + +class BleAppender : public ILogAppender { + public: + void Log(const char *message); + void Initialize(); + const char *getLastMessage(); + + private: + static const size_t MAX_MESSAGE_SIZE = 500; // MTU-safe size + std::string lastMessage; + void trimMessage(const char *message); +}; diff --git a/include/SmartSpin_parameters.h b/include/SmartSpin_parameters.h index 1fd58f1e..85e6099c 100644 --- a/include/SmartSpin_parameters.h +++ b/include/SmartSpin_parameters.h @@ -76,6 +76,7 @@ class RuntimeParameters { int minResistance = -DEFAULT_RESISTANCE_RANGE; int maxResistance = DEFAULT_RESISTANCE_RANGE; bool simTargetWatts = false; + bool bleLogEnabled = false; public: Measurement watts; @@ -114,6 +115,9 @@ class RuntimeParameters { void setMaxResistance(int max) { maxResistance = max; } int getMaxResistance() { return maxResistance; } + void setBleLogEnabled(bool enabled) { bleLogEnabled = enabled; } + bool getBleLogEnabled() { return bleLogEnabled; } + String returnJSON(); }; diff --git a/src/BLE_Custom_Characteristic.cpp b/src/BLE_Custom_Characteristic.cpp index ecef6582..ee907021 100644 --- a/src/BLE_Custom_Characteristic.cpp +++ b/src/BLE_Custom_Characteristic.cpp @@ -87,6 +87,9 @@ This characteristic allows for reading and writing various user configuration pa #include #include #include +#include "BleAppender.h" + +extern BleAppender bleAppender; void BLE_ss2kCustomCharacteristic::setupService(NimBLEServer *pServer) { pSmartSpin2kService = spinBLEServer.pServer->createService(SMARTSPIN2K_SERVICE_UUID); @@ -828,6 +831,19 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) { } break; + case BLE_BLELogging: // 0x30 + LOG_BUF_APPEND("<-BLELogging"); + if (rxValue[0] == cc_read) { + returnValue[0] = cc_success; + returnString = bleAppender.getLastMessage(); + } + if (rxValue[0] == cc_write) { + returnValue[0] = cc_success; + rtConfig->setBleLogEnabled(rxValue[2]); + LOG_BUF_APPEND("(%s)", rtConfig->getBleLogEnabled() ? "true" : "false"); + } + break; + default: LOG_BUF_APPEND("<-Unknown Characteristic"); returnValue[0] = cc_error; diff --git a/src/BLE_Server.cpp b/src/BLE_Server.cpp index cee59e9e..7dba08c7 100644 --- a/src/BLE_Server.cpp +++ b/src/BLE_Server.cpp @@ -157,7 +157,7 @@ void MyServerCallbacks::onDisconnect(NimBLEServer* pServer) { } void MyServerCallbacks::onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo) { - SS2K_LOG(BLE_SERVER_LOG_TAG, "MTU updated: %u for connection ID: %u", MTU, connInfo.getConnHandle()); + //SS2K_LOG(BLE_SERVER_LOG_TAG, "MTU updated: %u for connection ID: %u", MTU, connInfo.getConnHandle()); } bool MyServerCallbacks::onConnParamsUpdateRequest(uint16_t handle, const ble_gap_upd_params* params) { diff --git a/src/BleAppender.cpp b/src/BleAppender.cpp new file mode 100644 index 00000000..5d815c16 --- /dev/null +++ b/src/BleAppender.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 Anthony Doud & Joel Baranick + * All rights reserved + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#include "BleAppender.h" +#include "Main.h" +#include "BLE_Custom_Characteristic.h" + +void BleAppender::Initialize() {} + +void BleAppender::Log(const char *message) { + if (!rtConfig->getBleLogEnabled()) { + return; + } + + // Cache the message + trimMessage(message); + + // Use the existing custom characteristic notification mechanism + BLE_ss2kCustomCharacteristic::notify(BLE_BLELogging); +} + +const char *BleAppender::getLastMessage() { + return lastMessage.c_str(); +} + +void BleAppender::trimMessage(const char *message) { + if (message == nullptr) { + lastMessage = ""; + return; + } + + // Copy message and remove trailing newlines + std::string msg(message); + while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r')) { + msg.pop_back(); + } + + // Trim to MTU-safe size + if (msg.length() > MAX_MESSAGE_SIZE) { + msg = msg.substr(0, MAX_MESSAGE_SIZE); + } + + lastMessage = msg; +} diff --git a/src/Main.cpp b/src/Main.cpp index 5dd92226..e55d4cac 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -17,6 +17,7 @@ #include "Power_Table.h" #include "UdpAppender.h" #include "WebsocketAppender.h" +#include "BleAppender.h" #include "BLE_Custom_Characteristic.h" #include "BLE_Definitions.h" #include @@ -52,6 +53,7 @@ RuntimeParameters* rtConfig = new RuntimeParameters; ///////////// Log Appender ///////////// UdpAppender udpAppender; WebSocketAppender webSocketAppender; +BleAppender bleAppender; ///////////// BEGIN SETUP ///////////// #ifndef UNIT_TEST @@ -141,6 +143,7 @@ extern "C" void app_main() { // Configure and Initialize Logger logHandler.addAppender(&webSocketAppender); logHandler.addAppender(&udpAppender); + logHandler.addAppender(&bleAppender); logHandler.initialize(); ss2k->startTasks(); httpServer.start(); diff --git a/src/SmartSpin_parameters.cpp b/src/SmartSpin_parameters.cpp index 25fe1c68..5d46a215 100644 --- a/src/SmartSpin_parameters.cpp +++ b/src/SmartSpin_parameters.cpp @@ -38,6 +38,7 @@ JsonDocument doc; doc["maxStep"] = this->maxStep; doc["minResistance"] = this->minResistance; doc["maxResistance"] = this->maxResistance; + doc["bleLogEnabled"] = this->bleLogEnabled; String output; serializeJson(doc, output);