Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 10, 2026

BLE Log Streaming Implementation - COMPLETE ✅

This PR successfully implements BLE-based log streaming capability in SmartSpin2k firmware, mirroring existing UDP logging behavior.

Implementation Summary

Runtime Flag

  • Added bleLogEnabled to RuntimeParameters (defaults to false, runtime-only)
  • Included in JSON runtime output
  • Getter/setter methods implemented

BLE Custom Characteristic (0x30 - BLE_BLELogging)

  • Write operation: 0x02, 0x30, <0|1> to enable/disable logging
  • Read operation: 0x01, 0x30 returns last cached log message
  • Notifications: Automatic push when enabled and subscribed
  • Changed from 0x2F to 0x30 to avoid conflict with PR Configurable inactivity timeout #710
  • Uses existing BLE_ss2kCustomCharacteristic::notify() function

BleAppender Class

  • Implements ILogAppender interface
  • Message caching with MTU-safe trimming (~180 bytes)
  • Newline trimming
  • Uses existing BLE notification mechanism (simplified implementation)

Integration & Documentation

  • Wired into logHandler in Main.cpp
  • Updated CustomCharacteristic.md documentation
  • Code review completed with fixes applied

Usage Examples

Enable BLE logging: 0x02, 0x30, 0x01
Disable BLE logging: 0x02, 0x30, 0x00
Read last log: 0x01, 0x300x80, 0x30, [message]
Real-time logs: Subscribe to characteristic, then enable logging

Testing Status

  • ✅ Native tests compile successfully (19 tests, 2 pre-existing failures)
  • ✅ Code review completed with all issues addressed
  • ⚠️ Full ESP32 build not verified due to environment setup issues (unrelated to changes)

Key Features

  • Runtime-only flag (not persisted across reboots)
  • Existing UDP logging remains unchanged
  • Safe message handling (MTU-safe size limits)
  • Uses existing BLE notification infrastructure

Files Changed (8 files, +100 lines, -35 lines removed)

  • include/SmartSpin_parameters.h
  • src/SmartSpin_parameters.cpp
  • include/BLE_Custom_Characteristic.h
  • src/BLE_Custom_Characteristic.cpp
  • include/BleAppender.h (new)
  • src/BleAppender.cpp (new, simplified)
  • src/Main.cpp
  • CustomCharacteristic.md
Original prompt

Implement BLE-based log streaming toggle and last-message read capability in the SmartSpin2k BLE custom characteristic, mirroring existing UDP logging behavior.

Scope and requirements:

  • Add a runtime-only boolean flag in RuntimeParameters (e.g., bleLogEnabled) with getters/setters and include it in runtime JSON output. This should not persist across reboots (no storage in userConfig defaults).
  • Introduce a new custom characteristic code (e.g., BLE_BLELogging at 0x2F) alongside existing BLE_UDPLogging (0x2E).
  • Custom characteristic behavior:
    • Write (0x02, <BLE_BLELogging>, <0|1>): enable/disable BLE log streaming (sets the runtime flag).
    • Read (0x01, <BLE_BLELogging>): return the last log message (string response like other string-return cases) with status byte and code prefix.
    • While enabled, when new log messages are produced, update the cached “last log message” and notify subscribers with the latest value. Do not notify when disabled or if no subscribers. Trim newlines and keep message length within MTU-safe bounds (e.g., ~180 bytes).
  • Add a BLE log appender (similar to UdpAppender) that forwards log messages into the BLE characteristic when the runtime toggle is on.
  • Wire the new appender into logHandler initialization in Main.cpp.
  • Keep existing UDP logging behavior unchanged.
  • Documentation: update CustomCharacteristic.md to note the new characteristic code and its read/write semantics.

Reference files (current commit e6ce99d):

  • BLE custom characteristic: include/BLE_Custom_Characteristic.h, src/BLE_Custom_Characteristic.cpp
  • Logging infra: include/SS2KLog.h, src/SS2KLog.cpp, include/LogAppender.h, src/UdpAppender.cpp
  • Runtime/user config: include/SmartSpin_parameters.h, src/SmartSpin_parameters.cpp
  • Logger setup: src/Main.cpp

Acceptance criteria:

  • Builds successfully.
  • Enabling BLE logging via custom characteristic write causes subsequent logs to be notified over BLE; disabling stops notifications.
  • Reading the characteristic returns the latest log string.
  • No persistent setting stored for BLE logging; defaults to off after reboot.
  • Existing UDP logging remains functional.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Jan 10, 2026
Copilot AI changed the title [WIP] Add BLE-based log streaming toggle and last-message read capability Add BLE log streaming with runtime toggle and read capability Jan 10, 2026
Copilot AI requested a review from doudar January 10, 2026 20:19
@doudar
Copy link
Owner

doudar commented Jan 10, 2026

@copilot Looking good, the only thing I would change is that due to another pull request (#710) that already adds 0x2F, lets make this operation 0x30. You can also update the .md with the information from #710.

Copy link
Contributor Author

Copilot AI commented Jan 10, 2026

@copilot Looking good, the only thing I would change is that due to another pull request (#710) that already adds 0x2F, lets make this operation 0x30. You can also update the .md with the information ...

Changed BLE_BLELogging characteristic code from 0x2F to 0x30 in commit 38a474f. Updated all references in the header file, implementation, and documentation.

@doudar
Copy link
Owner

doudar commented Jan 10, 2026

@copilot instead of creating your own BLE notification, use the function already in the custom characteristic.

Copy link
Contributor Author

Copilot AI commented Jan 10, 2026

@copilot instead of creating your own BLE notification, use the function already in the custom characteristic.

Refactored BleAppender to use BLE_ss2kCustomCharacteristic::notify() instead of creating custom BLE notifications in commit 249431d. This simplifies the implementation and removes 35 lines of duplicate code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants