Skip to content

Commit 55c5970

Browse files
committed
Update and test light_ll connection_management.rs
1 parent 68da865 commit 55c5970

File tree

12 files changed

+4217
-272
lines changed

12 files changed

+4217
-272
lines changed

rust/src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn save_pair_info(adr: u32, p: &[u8]) {
7878
interval_min + 20 ms <= interval_max <= 2second
7979
timeout <= 6second
8080
*/
81+
#[cfg_attr(test, mry::mry)]
8182
pub fn update_ble_parameter_cb() {
8283
if !CONN_UPDATE_SUCCESSED.get() {
8384
setup_ble_parameter_start(

rust/src/sdk/ble_app/ble_ll_pair.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ pub fn pair_save_key()
388388
/// This function resets the pairing state and prepares initialization vectors
389389
/// for a new BLE connection. It uses the device's MAC address as part of the
390390
/// initialization vectors for encryption.
391+
#[cfg_attr(test, mry::mry)]
391392
pub fn pair_init()
392393
{
393394
// Reset pairing state to initial value

rust/src/sdk/ble_app/light_ll/connection_management.rs

Lines changed: 1728 additions & 39 deletions
Large diffs are not rendered by default.

rust/src/sdk/ble_app/light_ll/mesh_management.rs

Lines changed: 813 additions & 71 deletions
Large diffs are not rendered by default.

rust/src/sdk/ble_app/light_ll/mod.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,75 @@
1+
//! # Light Link Layer (light_ll) Module
2+
//!
3+
//! This module implements the core link layer functionality for a BLE mesh lighting system
4+
//! on the TLSR8266 chip. The light_ll module serves as the bridge between the low-level
5+
//! BLE radio hardware and the higher-level mesh networking protocol stack.
6+
//!
7+
//! ## Architecture Overview
8+
//!
9+
//! The module is organized into six main subsystems that work together to provide a complete
10+
//! mesh networking solution:
11+
//!
12+
//! 1. **Connection Management**: Handles BLE peripheral connections, timing synchronization,
13+
//! and parameter updates
14+
//! 2. **Mesh Management**: Manages mesh network topology, node discovery, and status propagation
15+
//! 3. **Packet Processing**: Parses and routes incoming packets, handles retransmission and acknowledgments
16+
//! 4. **Status Management**: Coordinates device status reporting and bridging between BLE and mesh
17+
//! 5. **OTA Management**: Provides over-the-air firmware update capabilities
18+
//! 6. **Pairing Management**: Handles device authentication and security key management
19+
//!
20+
//! ## Key Design Principles
21+
//!
22+
//! ### Multi-Protocol Support
23+
//! The system operates simultaneously as:
24+
//! - A BLE peripheral (for smartphone/gateway connections)
25+
//! - A mesh node (for device-to-device communication)
26+
//! - A bridge between these two networks
27+
//!
28+
//! ### Timing-Critical Operation
29+
//! All mesh communication is precisely timed to ensure:
30+
//! - Collision avoidance in the shared radio medium
31+
//! - Reliable message delivery with minimal latency
32+
//! - Power-efficient operation through synchronized sleep cycles
33+
//!
34+
//! ### Scalable Mesh Architecture
35+
//! The mesh protocol supports:
36+
//! - Up to 254 nodes per network (addresses 1-254, with 0 reserved for broadcast)
37+
//! - Automatic relay and routing of messages
38+
//! - Self-healing network topology
39+
//! - Group addressing for synchronized control
40+
141
/// Mesh network management functionality
42+
///
43+
/// Handles mesh node discovery, status tracking, and network topology maintenance.
44+
/// Implements algorithms for reliable status propagation and automatic node timeout detection.
245
pub mod mesh_management;
346

4-
/// BLE connection management functionality
47+
/// BLE connection management functionality
48+
///
49+
/// Manages BLE peripheral connections, including connection parameter negotiation,
50+
/// timing synchronization, and adaptive parameter updates for optimal performance.
551
pub mod connection_management;
652

753
/// Packet processing and parsing functionality
54+
///
55+
/// Implements the core packet routing logic, including mesh relay algorithms,
56+
/// duplicate detection, acknowledgment handling, and protocol parsing.
857
pub mod packet_processing;
958

1059
/// Over-The-Air (OTA) update management
60+
///
61+
/// Provides secure firmware update capabilities over the mesh network,
62+
/// with flash memory management and verification algorithms.
1163
pub mod ota_management;
1264

1365
/// Device status and reporting management
66+
///
67+
/// Coordinates status reporting between BLE and mesh networks, implementing
68+
/// bridge algorithms and response buffering for reliable data transfer.
1469
pub mod status_management;
1570

1671
/// Pairing and security management
72+
///
73+
/// Handles device authentication, key management, and secure pairing protocols
74+
/// for mesh network access control.
1775
pub mod pairing_management;
Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,141 @@
1+
//! # Over-The-Air (OTA) Update Management
2+
//!
3+
//! This module implements secure firmware update capabilities for the TLSR8266 mesh lighting
4+
//! system. It provides the core functionality for receiving, validating, and storing firmware
5+
//! updates transmitted over the mesh network.
6+
//!
7+
//! ## OTA Update Architecture
8+
//!
9+
//! The OTA system is designed with the following principles:
10+
//!
11+
//! ### Security and Reliability
12+
//! - **Write-Verify Cycle**: Every flash write operation is immediately verified by reading back
13+
//! the data to ensure integrity
14+
//! - **Atomic Operations**: Flash writes are performed in discrete pages to prevent partial updates
15+
//! - **Error Recovery**: Failed writes are detected immediately and can trigger retransmission
16+
//!
17+
//! ### Flash Memory Management
18+
//! - **Dedicated Flash Region**: Firmware updates are written to a separate flash area to avoid
19+
//! corrupting the running firmware
20+
//! - **Sequential Writing**: Firmware data is written sequentially to prevent fragmentation
21+
//! - **Address Tracking**: Current write position is maintained to resume interrupted updates
22+
//!
23+
//! ### Integration with Mesh Network
24+
//! - **Packet-Based Transfer**: Firmware is transmitted in small packets suitable for mesh networking
25+
//! - **Progress Tracking**: Update progress is tracked and can be reported across the network
26+
//! - **Error Propagation**: Write failures are communicated back to the update initiator
27+
//!
28+
//! ## Flash Memory Layout
29+
//!
30+
//! ```
31+
//! Flash Memory:
32+
//! ┌─────────────────┬─────────────────┬─────────────────┐
33+
//! │ Running FW │ New FW Area │ Config/Data │
34+
//! │ (Active) │ (OTA Target) │ (Persistent) │
35+
//! └─────────────────┴─────────────────┴─────────────────┘
36+
//! ↑
37+
//! FLASH_ADR_LIGHT_NEW_FW
38+
//! ```
39+
//!
40+
//! ## Update State Machine
41+
//!
42+
//! The OTA process follows this state progression:
43+
//! 1. **Idle**: No update in progress
44+
//! 2. **Receiving**: Accepting firmware data packets
45+
//! 3. **Continue**: Previous packet written successfully, ready for next
46+
//! 4. **Error**: Write/verify failure detected, update aborted
47+
//! 5. **Complete**: All firmware data received and verified
48+
149
use crate::config::FLASH_ADR_LIGHT_NEW_FW;
250
use crate::sdk::drivers::flash::{flash_read_page, flash_write_page};
351
use crate::sdk::light::OtaState;
452
use crate::state::{OTA_UPDATE_CURRENT_FLASH_ADDRESS, SimplifyLS};
553

6-
/// Saves OTA data to flash memory
54+
/// Saves OTA firmware data to flash memory with verification.
55+
///
56+
/// This function implements the core OTA flash write algorithm with built-in verification
57+
/// to ensure data integrity. It performs atomic write operations followed by immediate
58+
/// read-back verification to detect any flash memory errors.
59+
///
60+
/// # Flash Write Algorithm
61+
///
62+
/// The function follows a strict write-verify-advance sequence:
63+
///
64+
/// 1. **Address Calculation**: Computes the target flash address by adding the current
65+
/// write offset to the base OTA flash region address
66+
///
67+
/// 2. **Flash Write Operation**: Writes the incoming data to flash memory using the
68+
/// hardware flash driver
69+
///
70+
/// 3. **Verification Read**: Immediately reads back the written data from flash to
71+
/// verify the write operation succeeded
72+
///
73+
/// 4. **Data Comparison**: Performs byte-by-byte comparison between the original data
74+
/// and the read-back data to detect any corruption
75+
///
76+
/// 5. **State Update**: On successful verification, advances the write pointer for
77+
/// the next packet; on failure, returns error state
78+
///
79+
/// # Error Detection
80+
///
81+
/// The verification process detects several types of flash errors:
82+
/// - **Write Failures**: Flash cells that fail to program correctly
83+
/// - **Read Disturbance**: Previously written data corrupted by nearby writes
84+
/// - **Wear-Out**: Flash cells that have exceeded their write/erase cycles
85+
/// - **Power Glitches**: Incomplete writes due to power supply instability
86+
///
87+
/// # Flash Memory Safety
88+
///
89+
/// The function ensures safe flash operations by:
90+
/// - Using only the designated OTA flash region (prevents overwriting active firmware)
91+
/// - Performing sequential writes (prevents address conflicts)
92+
/// - Validating write operations before advancing (prevents partial corruption)
93+
/// - Maintaining atomic write semantics (each call either fully succeeds or fails)
94+
///
95+
/// # Performance Considerations
96+
///
97+
/// - **Flash Write Time**: Each write operation may take several milliseconds
98+
/// - **Verification Overhead**: Read-back verification doubles the flash access time
99+
/// - **Sequential Access**: Sequential writes are optimized by flash hardware
100+
/// - **Page Alignment**: Performance is optimal when data aligns with flash page boundaries
101+
///
102+
/// # Parameters
103+
/// * `data` - Firmware data packet to write to flash (1-16 bytes typical)
104+
///
105+
/// # Returns
106+
/// * `OtaState::Continue` - Data written and verified successfully, ready for next packet
107+
/// * `OtaState::Error` - Write or verification failed, update should be aborted
108+
///
109+
/// # Side Effects
110+
/// * Modifies flash memory content in the OTA region
111+
/// * Updates `OTA_UPDATE_CURRENT_FLASH_ADDRESS` on successful writes
112+
/// * May trigger flash wear leveling operations (hardware-dependent)
113+
///
114+
/// # Flash Endurance
115+
/// Flash memory has limited write/erase cycles (typically 10,000-100,000 cycles).
116+
/// This function contributes to flash wear and should only be used for legitimate
117+
/// firmware updates to preserve flash lifetime.
7118
pub fn rf_ota_save_data(data: &[u8]) -> OtaState
8119
{
120+
// Calculate target flash address: base OTA region + current write offset
9121
let addr = OTA_UPDATE_CURRENT_FLASH_ADDRESS.get() + FLASH_ADR_LIGHT_NEW_FW;
122+
123+
// Write the firmware data packet to flash memory
10124
flash_write_page(addr, data.len() as u32, data.as_ptr());
11125

126+
// Allocate temporary buffer for verification read (16 bytes max packet size)
12127
let mut tmp = [0u8; 0x10];
128+
129+
// Read back the written data for verification
13130
flash_read_page(addr, data.len() as u32, tmp.as_mut_ptr());
14131

132+
// Verify data integrity by comparing original data with read-back data
15133
if data == &tmp[..data.len()] {
134+
// Write verification successful - advance write pointer for next packet
16135
OTA_UPDATE_CURRENT_FLASH_ADDRESS.set(OTA_UPDATE_CURRENT_FLASH_ADDRESS.get() + data.len() as u32);
17136
return OtaState::Continue;
18137
} else {
138+
// Write verification failed - return error to abort update
19139
return OtaState::Error;
20140
}
21141
}

0 commit comments

Comments
 (0)