diff --git a/1 software design/components/SOFTWARE_COMPONENTS_OVERVIEW.md b/1 software design/components/SOFTWARE_COMPONENTS_OVERVIEW.md new file mode 100644 index 0000000..b31d582 --- /dev/null +++ b/1 software design/components/SOFTWARE_COMPONENTS_OVERVIEW.md @@ -0,0 +1,376 @@ +# Software Components Overview + +**Document ID:** COMP-OVERVIEW-001 +**Version:** 1.0 +**Date:** 2025-02-01 +**Project:** ASF Sensor Hub (Sub-Hub) Embedded System + +## 1. Introduction + +This document provides a comprehensive overview of all software components in the ASF Sensor Hub embedded system. Each component is designed following the layered architecture principles with clear separation of concerns, well-defined interfaces, and specific responsibilities. + +## 2. Component Architecture Overview + +```mermaid +graph TB + subgraph "Application Layer" + subgraph "Business Stack" + STM[System State Manager
C-STM-001] + SensorMgr[Sensor Manager
C-SENSOR-001] + CommMgr[Communication Manager
C-COM-001] + OTAMgr[OTA Manager
C-OTA-001] + MCMgr[Machine Constants Manager
C-MC-001] + SecurityMgr[Security Manager
C-SEC-001] + DiagMgr[Diagnostics Manager
C-DIAG-001] + EventSys[Event System
C-EVENT-001] + end + + subgraph "DP Stack" + DataPool[Data Pool
C-DATA-POOL] + Persistence[Data Persistence
C-DP-001] + end + end + + subgraph "Driver Layer" + SensorDrivers[Sensor Drivers] + NetworkStack[Network Stack] + StorageDrivers[Storage Drivers] + end + + subgraph "OSAL Layer" + ESPIDFWrappers[ESP-IDF Wrappers] + end + + subgraph "HAL Layer" + ESPIDFFramework[ESP-IDF Framework] + end +``` + +## 3. Existing Components + +### 3.1 Application Layer Components + +#### 3.1.1 Business Stack Components + +| Component ID | Component Name | Primary Purpose | Key Responsibilities | +|--------------|----------------|-----------------|---------------------| +| C-STM-001 | System State Manager | System lifecycle coordination | FSM implementation, state transitions, teardown coordination | +| C-SENSOR-001 | Sensor Manager | Sensor data acquisition | Multi-sensor management, high-frequency sampling, data filtering | +| C-COM-001 | Communication Manager | External communication | MQTT/TLS, ESP-NOW, message routing, connection management | +| C-OTA-001 | OTA Manager | Firmware updates | A/B partitioning, secure updates, automatic rollback | +| C-MC-001 | Machine Constants Manager | Configuration management | Static configuration, remote updates, validation | +| C-SEC-001 | Security Manager | System security | Secure boot, flash encryption, TLS, key management | +| C-DIAG-001 | Diagnostics Manager | System health monitoring | Diagnostic codes, health monitoring, watchdog management | +| C-EVENT-001 | Event System | Inter-component communication | Publish/subscribe, event queuing, asynchronous delivery | + +#### 3.1.2 DP Stack Components + +| Component ID | Component Name | Primary Purpose | Key Responsibilities | +|--------------|----------------|-----------------|---------------------| +| C-DATA-POOL | Data Pool | Centralized data storage | Thread-safe data access, real-time data exchange | +| C-DP-001 | Data Persistence | Persistent storage | Storage abstraction, serialization, wear management | + +### 3.2 Component Descriptions + +#### 3.2.1 System State Manager (C-STM-001) +**Location:** `application_layer/business_stack/STM/` + +The System State Manager implements the central finite state machine for the Sensor Hub, managing system lifecycle states (INIT, RUNNING, WARNING, FAULT, OTA_PREP, etc.) and coordinating controlled teardown sequences. + +**Key Features:** +- System FSM with 11 defined states +- State transition validation and enforcement +- Teardown coordination for OTA and MC updates +- State change notifications via Event System +- State-aware execution enforcement + +#### 3.2.2 Sensor Manager (C-SENSOR-001) +**Location:** `application_layer/business_stack/sensor_manager/` + +The Sensor Manager coordinates all sensor-related operations including lifecycle management, data acquisition scheduling, high-frequency sampling, and local filtering. + +**Key Features:** +- Support for 7 environmental sensor types +- High-frequency sampling (10 samples per cycle) +- Configurable filtering algorithms (median, moving average, rate-limited) +- Sensor state management and fault detection +- 1-second acquisition cycles with timestamped data + +#### 3.2.3 Communication Manager (C-COM-001) +**Location:** `application_layer/business_stack/communication_manager/` + +The Communication Manager handles all external communication including MQTT-based Main Hub communication and ESP-NOW peer communication. + +**Key Features:** +- MQTT over TLS communication with Main Hub +- ESP-NOW peer-to-peer communication +- Message formatting and encoding (CBOR) +- Connection management with automatic reconnection +- Heartbeat and keepalive mechanisms + +#### 3.2.4 OTA Manager (C-OTA-001) +**Location:** `application_layer/business_stack/ota_manager/` + +The OTA Manager provides secure, reliable firmware update functionality with A/B partitioning and automatic rollback capabilities. + +**Key Features:** +- A/B partition management +- Secure firmware validation (SHA-256, RSA-3072/ECDSA-P256) +- Automatic rollback on boot failures +- Controlled teardown coordination +- Update progress tracking and reporting + +#### 3.2.5 Machine Constants Manager (C-MC-001) +**Location:** `application_layer/business_stack/machine_constants_manager/` + +The Machine Constants Manager handles static and semi-static configuration parameters including sensor configuration, calibration data, and system identity. + +**Key Features:** +- JSON-based configuration management +- Remote configuration updates from Main Hub +- Configuration validation and integrity checking +- Version control and rollback capability +- Controlled reinitialization for updates + +#### 3.2.6 Security Manager (C-SEC-001) +**Location:** `application_layer/business_stack/security_manager/` + +The Security Manager implements comprehensive security mechanisms including secure boot, flash encryption, and communication security. + +**Key Features:** +- Secure Boot V2 with RSA-3072/ECDSA-P256 +- Flash encryption with AES-256 +- TLS/mTLS communication security +- Cryptographic key management +- Security violation detection and response + +#### 3.2.7 Diagnostics Manager (C-DIAG-001) +**Location:** `application_layer/business_stack/diagnostics_manager/` + +The Diagnostics Manager provides comprehensive system health monitoring, fault detection, and diagnostic data collection. + +**Key Features:** +- Structured diagnostic code framework +- System health monitoring and performance metrics +- Layered watchdog system management +- Engineering diagnostic sessions +- Persistent diagnostic data storage + +#### 3.2.8 Event System (C-EVENT-001) +**Location:** `application_layer/business_stack/event_system/` + +The Event System provides a publish/subscribe event bus for cross-component communication, enabling loose coupling and asynchronous event delivery. + +**Key Features:** +- Non-blocking event publishing and delivery +- Priority-based subscriber management +- Event filtering and queuing +- ISR-safe event publishing +- Overflow handling with oldest-event dropping + +#### 3.2.9 Data Pool (C-DATA-POOL) +**Location:** `application_layer/DP_stack/data_pool/` + +The Data Pool provides centralized, thread-safe data storage and access for sensor readings, system parameters, and operational data. + +**Key Features:** +- Thread-safe data access and modification +- Real-time data exchange between components +- Data validation and type checking +- Event-driven data change notifications +- Memory-efficient data organization + +#### 3.2.10 Data Persistence (C-DP-001) +**Location:** `application_layer/DP_stack/persistence/` + +The Data Persistence component provides the sole interface for persistent data access, abstracting storage media and managing data serialization. + +**Key Features:** +- Storage media abstraction (SD card, NVM) +- Data serialization/deserialization +- Wear-aware storage management +- Data integrity verification +- Critical data flushing before state transitions + +## 4. Missing Components Analysis + +Based on the architecture analysis and feature requirements, the following components appear to be missing or need to be created: + +### 4.1 Missing Application Layer Components + +#### 4.1.1 Error Handler Component +**Component ID:** C-ERROR-001 +**Location:** `application_layer/error_handler/` + +**Purpose:** Centralized error handling, fault classification, and recovery coordination. + +**Key Responsibilities:** +- Fault detection and classification +- Error recovery strategy implementation +- Fault escalation to System State Manager +- Error logging and reporting +- Recovery attempt coordination + +#### 4.1.2 Time Utils Component +**Component ID:** C-TIME-001 +**Location:** `application_layer/utils/time_utils/` + +**Purpose:** Time management, timestamp generation, and time synchronization. + +**Key Responsibilities:** +- System time management +- Timestamp generation for sensor data +- Time synchronization with external sources +- Time zone handling +- Uptime tracking + +#### 4.1.3 Logger Component +**Component ID:** C-LOGGER-001 +**Location:** `application_layer/utils/logger/` + +**Purpose:** Centralized logging infrastructure for system diagnostics and debugging. + +**Key Responsibilities:** +- Multi-level logging (DEBUG, INFO, WARN, ERROR, FATAL) +- Log message formatting and timestamping +- Log output routing (UART, file, network) +- Log level configuration +- Log rotation and management + +### 4.2 Missing Driver Layer Components + +#### 4.2.1 Hardware Abstraction Layer (HAL) Components +**Component ID:** C-HAL-001 +**Location:** `drivers/hal/` + +**Purpose:** Hardware abstraction for sensors, communication interfaces, and peripherals. + +**Key Responsibilities:** +- I2C/SPI/UART interface abstraction +- GPIO management +- ADC interface abstraction +- Timer and PWM abstraction +- Interrupt handling abstraction + +#### 4.2.2 Sensor Driver Components +**Component ID:** C-SENSOR-DRV-001 to C-SENSOR-DRV-007 +**Location:** `drivers/sensors/` + +**Purpose:** Hardware-specific sensor drivers for each sensor type. + +**Components Needed:** +- Temperature Sensor Driver (SHT40) +- Humidity Sensor Driver (SHT40) +- CO2 Sensor Driver (SCD40) +- NH3 Sensor Driver (Analog) +- VOC Sensor Driver (SGP40) +- PM Sensor Driver (SPS30) +- Light Sensor Driver (TSL2591) + +#### 4.2.3 Storage Driver Components +**Component ID:** C-STORAGE-001 +**Location:** `drivers/storage/` + +**Purpose:** Storage device drivers for SD card and NVM. + +**Key Responsibilities:** +- SD card interface driver +- NVM (flash) interface driver +- File system abstraction +- Storage health monitoring +- Wear leveling support + +### 4.3 Missing Utility Components + +#### 4.3.1 Crypto Utils Component +**Component ID:** C-CRYPTO-001 +**Location:** `application_layer/utils/crypto_utils/` + +**Purpose:** Cryptographic utility functions and algorithm implementations. + +**Key Responsibilities:** +- Hash function implementations +- Encryption/decryption utilities +- Digital signature utilities +- Random number generation +- Key derivation functions + +#### 4.3.2 Data Validation Component +**Component ID:** C-VALIDATION-001 +**Location:** `application_layer/utils/validation/` + +**Purpose:** Data validation utilities for sensor data and configuration. + +**Key Responsibilities:** +- Sensor data range validation +- Configuration parameter validation +- Data format validation +- Checksum and integrity validation +- Schema validation for JSON data + +## 5. Component Dependencies + +### 5.1 Dependency Matrix + +| Component | Depends On | Provides To | +|-----------|------------|-------------| +| System State Manager | Event System, Error Handler, Persistence | All Components | +| Sensor Manager | Sensor Drivers, Event System, Time Utils, MC Manager | Data Pool, Communication Manager | +| Communication Manager | Network Stack, TLS Manager, Event System | Main Hub APIs, OTA Manager | +| OTA Manager | Communication Manager, System State Manager, Security Manager | System State Manager | +| Machine Constants Manager | Persistence, Communication Manager, System State Manager | All Components | +| Security Manager | Crypto Utils, Hardware Security, Diagnostics Manager | All Components | +| Diagnostics Manager | Persistence, Event System, Security Manager | All Components | +| Event System | Time Utils, Logger | All Components | +| Data Pool | Persistence, Event System | Sensor Manager, Communication Manager | +| Data Persistence | Storage Drivers, Error Handler | Data Pool, Machine Constants Manager | + +### 5.2 Interface Dependencies + +All components follow the dependency inversion principle, depending on interfaces rather than concrete implementations. This enables: +- Testability through mock implementations +- Flexibility in implementation changes +- Clear contract definitions +- Reduced coupling between components + +## 6. Component Implementation Status + +### 6.1 Completed Components (Specification Phase) +- ✅ System State Manager (C-STM-001) +- ✅ Sensor Manager (C-SENSOR-001) +- ✅ Communication Manager (C-COM-001) +- ✅ OTA Manager (C-OTA-001) +- ✅ Machine Constants Manager (C-MC-001) +- ✅ Security Manager (C-SEC-001) +- ✅ Diagnostics Manager (C-DIAG-001) +- ✅ Event System (C-EVENT-001) +- ✅ Data Pool (C-DATA-POOL) +- ✅ Data Persistence (C-DP-001) + +### 6.2 Missing Components (Need Specification) +- ❌ Error Handler (C-ERROR-001) +- ❌ Time Utils (C-TIME-001) +- ❌ Logger (C-LOGGER-001) +- ❌ Hardware Abstraction Layer (C-HAL-001) +- ❌ Sensor Drivers (C-SENSOR-DRV-001 to 007) +- ❌ Storage Drivers (C-STORAGE-001) +- ❌ Crypto Utils (C-CRYPTO-001) +- ❌ Data Validation (C-VALIDATION-001) + +## 7. Next Steps + +1. **Create Missing Component Specifications**: Develop detailed component specifications for all missing components following the same format as existing components. + +2. **Validate Component Interfaces**: Review and validate all component interfaces to ensure proper abstraction and minimal coupling. + +3. **Implementation Planning**: Create implementation roadmap prioritizing critical path components. + +4. **Integration Testing Strategy**: Develop comprehensive integration testing strategy for component interactions. + +5. **Performance Validation**: Validate that the component architecture meets all performance and resource constraints. + +--- + +**Document Status:** Complete - Component Analysis +**Next Review:** After missing component specifications are created +**Dependencies:** Component specifications, architecture requirements \ No newline at end of file diff --git a/1 software design/components/communication_manager/COMPONENT.md b/1 software design/components/communication_manager/COMPONENT.md new file mode 100644 index 0000000..4242fad --- /dev/null +++ b/1 software design/components/communication_manager/COMPONENT.md @@ -0,0 +1,378 @@ +# Communication Manager Component Specification + +**Component ID:** C-COM-001 +**Component Name:** Communication Manager +**Version:** 1.0 +**Date:** 2025-02-01 + +## 1. Component Overview + +### 1.1 Purpose +The Communication Manager is responsible for managing all external communication interfaces including MQTT-based Main Hub communication, ESP-NOW peer communication, and long-range fallback communication. It provides a unified interface for all communication protocols and handles connection management, message routing, and protocol-specific operations. + +### 1.2 Scope +- MQTT over TLS communication with Main Hub +- ESP-NOW peer-to-peer communication +- Message formatting and encoding (CBOR) +- Connection management and health monitoring +- Communication protocol abstraction + +### 1.3 Responsibilities +- Establish and maintain secure connections with Main Hub +- Handle MQTT message publishing and subscription +- Manage ESP-NOW peer communication +- Implement heartbeat and keepalive mechanisms +- Provide communication status monitoring +- Handle automatic reconnection with exponential backoff +- Route messages between internal components and external entities + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Communication Manager" + CM[Communication Controller] + MH[MQTT Handler] + EN[ESP-NOW Handler] + MF[Message Formatter] + CS[Connection Supervisor] + end + + subgraph "External Interfaces" + MQTT[MQTT Broker] + PEER[Peer Hubs] + end + + subgraph "Internal Components" + SM[Sensor Manager] + DP[Data Pool] + DIAG[Diagnostics Manager] + end + + CM --> MH + CM --> EN + CM --> MF + CM --> CS + + MH --> MQTT + EN --> PEER + + SM --> CM + DP --> CM + DIAG --> CM +``` + +### 2.2 Internal Components + +#### 2.2.1 Communication Controller +- Central coordination of all communication activities +- Message routing and protocol selection +- Communication policy enforcement + +#### 2.2.2 MQTT Handler +- MQTT client implementation +- TLS session management +- Topic subscription and message publishing +- QoS handling and message acknowledgment + +#### 2.2.3 ESP-NOW Handler +- ESP-NOW protocol implementation +- Peer discovery and management +- Message encryption and decryption +- Acknowledgment and retry mechanisms + +#### 2.2.4 Message Formatter +- CBOR encoding and decoding +- Message versioning and schema validation +- Payload compression and optimization + +#### 2.2.5 Connection Supervisor +- Connection health monitoring +- Automatic reconnection logic +- Network status tracking +- Communication diagnostics + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 ICommunicationManager +```cpp +class ICommunicationManager { +public: + virtual ~ICommunicationManager() = default; + + // Connection Management + virtual Result initialize(const CommunicationConfig& config) = 0; + virtual Result connect() = 0; + virtual Result disconnect() = 0; + virtual ConnectionStatus getConnectionStatus() const = 0; + + // Message Operations + virtual Result publishSensorData(const SensorDataMessage& data) = 0; + virtual Result publishDiagnostic(const DiagnosticMessage& diag) = 0; + virtual Result publishHeartbeat(const HeartbeatMessage& heartbeat) = 0; + virtual Result sendPeerMessage(const PeerMessage& message, NodeId peer) = 0; + + // Subscription Management + virtual Result subscribeToCommands(ICommandHandler* handler) = 0; + virtual Result subscribeToOTAUpdates(IOTAHandler* handler) = 0; + virtual Result subscribeToPeerMessages(IPeerMessageHandler* handler) = 0; + + // Status and Control + virtual CommunicationStats getStatistics() const = 0; + virtual Result resetConnection() = 0; +}; +``` + +#### 3.1.2 IMessageHandler +```cpp +class IMessageHandler { +public: + virtual ~IMessageHandler() = default; + virtual Result handleIncomingMessage(const Message& message) = 0; + virtual Result handleConnectionEvent(ConnectionEvent event) = 0; +}; +``` + +#### 3.1.3 IConnectionStatus +```cpp +class IConnectionStatus { +public: + virtual ~IConnectionStatus() = default; + virtual bool isConnected() const = 0; + virtual int getSignalStrength() const = 0; + virtual std::chrono::milliseconds getLatency() const = 0; + virtual uint32_t getMessagesSent() const = 0; + virtual uint32_t getMessagesReceived() const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 ITLSManager +- TLS session establishment and management +- Certificate validation and management +- Secure communication channel setup + +#### 3.2.2 INetworkStack +- Low-level network operations +- Wi-Fi connection management +- Network status monitoring + +#### 3.2.3 IDataPool +- Access to sensor data for transmission +- Retrieval of latest sensor values +- Data validity checking + +#### 3.2.4 IEventSystem +- Event publication for communication events +- Event subscription for system events +- Asynchronous notification handling + +#### 3.2.5 IDiagnosticsManager +- Diagnostic event reporting +- Communication error logging +- Performance metrics reporting + +## 4. Dynamic View + +### 4.1 MQTT Communication Sequence + +```mermaid +sequenceDiagram + participant SM as Sensor Manager + participant CM as Communication Manager + participant MH as MQTT Handler + participant TLS as TLS Manager + participant BROKER as MQTT Broker + + SM->>CM: publishSensorData(data) + CM->>CM: formatMessage(data) + CM->>MH: publish(topic, payload) + MH->>TLS: sendSecure(message) + TLS->>BROKER: encrypted_message + BROKER-->>TLS: ack + TLS-->>MH: send_result + MH-->>CM: publish_result + CM-->>SM: operation_result +``` + +### 4.2 Connection Management Sequence + +```mermaid +sequenceDiagram + participant CS as Connection Supervisor + participant MH as MQTT Handler + participant TLS as TLS Manager + participant DIAG as Diagnostics Manager + + CS->>CS: monitorConnection() + CS->>MH: checkConnectionHealth() + MH-->>CS: connection_status + + alt Connection Lost + CS->>DIAG: reportConnectionLoss() + CS->>CS: startReconnectionTimer() + CS->>MH: reconnect() + MH->>TLS: establishConnection() + TLS-->>MH: connection_result + MH-->>CS: reconnection_result + end +``` + +## 5. Configuration + +### 5.1 Communication Configuration +```cpp +struct CommunicationConfig { + // MQTT Configuration + std::string mqtt_broker_url; + uint16_t mqtt_port; + std::string mqtt_client_id; + std::string mqtt_username; + std::string mqtt_password; + uint16_t mqtt_keepalive_seconds; + uint8_t mqtt_qos_level; + + // ESP-NOW Configuration + bool espnow_enabled; + uint8_t espnow_channel; + std::vector espnow_peers; + + // TLS Configuration + std::string ca_certificate; + std::string client_certificate; + std::string client_private_key; + + // Timing Configuration + uint32_t heartbeat_interval_ms; + uint32_t reconnection_timeout_ms; + uint32_t message_timeout_ms; +}; +``` + +### 5.2 Topic Structure +```cpp +namespace Topics { + constexpr const char* SENSOR_DATA = "/farm/{site_id}/{house_id}/{node_id}/data/{sensor_id}"; + constexpr const char* HEARTBEAT = "/farm/{site_id}/{house_id}/{node_id}/status/heartbeat"; + constexpr const char* SYSTEM_STATUS = "/farm/{site_id}/{house_id}/{node_id}/status/system"; + constexpr const char* COMMANDS = "/farm/{site_id}/{house_id}/{node_id}/cmd/{command_type}"; + constexpr const char* DIAGNOSTICS = "/farm/{site_id}/{house_id}/{node_id}/diag/{severity}"; + constexpr const char* OTA = "/farm/{site_id}/{house_id}/{node_id}/ota/{action}"; +} +``` + +## 6. State Management + +### 6.1 Communication States +```cpp +enum class CommunicationState { + DISCONNECTED, + CONNECTING, + CONNECTED, + RECONNECTING, + ERROR, + DISABLED +}; +``` + +### 6.2 State Transitions +- DISCONNECTED → CONNECTING: Connection initiation +- CONNECTING → CONNECTED: Successful connection establishment +- CONNECTED → RECONNECTING: Connection loss detected +- RECONNECTING → CONNECTED: Successful reconnection +- Any State → ERROR: Critical communication error +- Any State → DISABLED: System shutdown or disable command + +## 7. Error Handling + +### 7.1 Error Categories +- **Connection Errors**: Network connectivity issues, broker unavailability +- **Authentication Errors**: Certificate validation failures, credential issues +- **Protocol Errors**: MQTT protocol violations, message format errors +- **Timeout Errors**: Message delivery timeouts, keepalive failures +- **Resource Errors**: Memory allocation failures, buffer overflows + +### 7.2 Error Recovery Strategies +- **Automatic Reconnection**: Exponential backoff with maximum retry limit +- **Message Queuing**: Store messages during disconnection periods +- **Graceful Degradation**: Continue operation with reduced functionality +- **Diagnostic Reporting**: Log all communication errors for analysis + +## 8. Performance Characteristics + +### 8.1 Timing Requirements +- **Message Latency**: < 100ms for on-demand data requests +- **Heartbeat Interval**: 10 seconds (configurable) +- **Reconnection Time**: < 30 seconds with exponential backoff +- **Message Throughput**: > 100 messages/minute + +### 8.2 Resource Usage +- **Memory**: < 32KB for communication buffers +- **CPU**: < 5% average utilization +- **Network**: Adaptive based on message frequency and size + +## 9. Security Considerations + +### 9.1 Communication Security +- All MQTT communication encrypted with TLS 1.2 +- Device authentication using X.509 certificates +- ESP-NOW messages encrypted with AES-128 +- Message integrity verification + +### 9.2 Access Control +- Topic-based access control via broker configuration +- Peer authentication for ESP-NOW communication +- Command validation and authorization + +## 10. Testing Strategy + +### 10.1 Unit Tests +- Message formatting and parsing +- Connection state management +- Error handling and recovery +- Configuration validation + +### 10.2 Integration Tests +- End-to-end message delivery +- TLS session establishment +- ESP-NOW peer communication +- Broker interaction testing + +### 10.3 Performance Tests +- Message throughput and latency +- Connection stability under load +- Memory usage profiling +- Network efficiency analysis + +## 11. Dependencies + +### 11.1 Internal Dependencies +- TLS Manager for secure communication +- Event System for asynchronous notifications +- Data Pool for sensor data access +- Diagnostics Manager for error reporting + +### 11.2 External Dependencies +- ESP-IDF MQTT client library +- ESP-IDF ESP-NOW library +- mbedTLS for cryptographic operations +- CBOR encoding/decoding library + +## 12. Constraints and Assumptions + +### 12.1 Constraints +- Maximum message size: 8KB +- Maximum concurrent connections: 1 MQTT + 20 ESP-NOW peers +- Network latency: < 1000ms for reliable operation +- Memory usage: Bounded and deterministic + +### 12.2 Assumptions +- Reliable network connectivity for MQTT communication +- Proper broker configuration and availability +- Valid certificates and credentials provisioned +- Sufficient system resources for communication operations \ No newline at end of file diff --git a/1 software design/components/data_pool/COMPONENT.md b/1 software design/components/data_pool/COMPONENT.md new file mode 100644 index 0000000..e69de29 diff --git a/1 software design/components/diagnostics_manager/COMPONENT.md b/1 software design/components/diagnostics_manager/COMPONENT.md new file mode 100644 index 0000000..34a205d --- /dev/null +++ b/1 software design/components/diagnostics_manager/COMPONENT.md @@ -0,0 +1,527 @@ +# Diagnostics Manager Component Specification + +**Component ID:** C-DIAG-001 +**Component Name:** Diagnostics Manager +**Version:** 1.0 +**Date:** 2025-02-01 + +## 1. Component Overview + +### 1.1 Purpose +The Diagnostics Manager is responsible for comprehensive system health monitoring, fault detection, diagnostic data collection, and engineering access capabilities. It provides centralized diagnostic code management, persistent diagnostic data storage, and diagnostic session support for maintenance and troubleshooting. + +### 1.2 Scope +- Diagnostic code framework and management +- System health monitoring and fault detection +- Diagnostic data collection and storage +- Engineering diagnostic sessions +- Layered watchdog system management +- Performance and resource monitoring + +### 1.3 Responsibilities +- Implement structured diagnostic code framework +- Collect and classify diagnostic events +- Persist diagnostic data across system resets +- Provide diagnostic session interface for engineers +- Monitor system health and performance metrics +- Manage watchdog systems for fault detection +- Generate diagnostic reports and summaries + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Diagnostics Manager" + DM[Diagnostic Controller] + DC[Diagnostic Collector] + DR[Diagnostic Reporter] + DS[Diagnostic Session] + HM[Health Monitor] + WM[Watchdog Manager] + end + + subgraph "Storage Layer" + DL[Diagnostic Logger] + DP[Data Pool] + end + + subgraph "Hardware Monitoring" + TWD[Task Watchdog] + IWD[Interrupt Watchdog] + RWD[RTC Watchdog] + TM[Temperature Monitor] + VM[Voltage Monitor] + end + + DM --> DC + DM --> DR + DM --> DS + DM --> HM + DM --> WM + + DC --> DL + DR --> DP + + HM --> TM + HM --> VM + WM --> TWD + WM --> IWD + WM --> RWD +``` + +### 2.2 Internal Components + +#### 2.2.1 Diagnostic Controller +- Central coordination of diagnostic activities +- Diagnostic event routing and processing +- Diagnostic policy enforcement + +#### 2.2.2 Diagnostic Collector +- Diagnostic event collection and enrichment +- Timestamp and context information addition +- Diagnostic code validation and assignment + +#### 2.2.3 Diagnostic Reporter +- Diagnostic event reporting to external systems +- Diagnostic summary generation +- Real-time diagnostic notifications + +#### 2.2.4 Diagnostic Session +- Engineering access interface +- Diagnostic data retrieval and analysis +- Diagnostic record management + +#### 2.2.5 Health Monitor +- System vital signs monitoring +- Performance metrics collection +- Resource usage tracking + +#### 2.2.6 Watchdog Manager +- Multi-layer watchdog system management +- Watchdog feeding coordination +- Watchdog timeout handling + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 IDiagnosticsManager +```cpp +class IDiagnosticsManager { +public: + virtual ~IDiagnosticsManager() = default; + + // Diagnostic Event Management + virtual Result reportDiagnostic(DiagnosticCode code, + DiagnosticSeverity severity, + const std::string& context) = 0; + virtual Result reportDiagnostic(const DiagnosticEvent& event) = 0; + virtual Result clearDiagnostic(DiagnosticCode code) = 0; + virtual Result clearAllDiagnostics() = 0; + + // Diagnostic Query + virtual std::vector getActiveDiagnostics() const = 0; + virtual std::vector getDiagnosticHistory( + std::chrono::system_clock::time_point since) const = 0; + virtual DiagnosticSummary getDiagnosticSummary() const = 0; + + // Health Monitoring + virtual SystemHealth getSystemHealth() const = 0; + virtual PerformanceMetrics getPerformanceMetrics() const = 0; + virtual ResourceUsage getResourceUsage() const = 0; + + // Session Management + virtual Result createDiagnosticSession( + const SessionCredentials& credentials) = 0; + virtual Result closeDiagnosticSession(DiagnosticSessionId session) = 0; +}; +``` + +#### 3.1.2 IDiagnosticReporter +```cpp +class IDiagnosticReporter { +public: + virtual ~IDiagnosticReporter() = default; + virtual Result reportEvent(const DiagnosticEvent& event) = 0; + virtual Result reportHealthStatus(const SystemHealth& health) = 0; + virtual Result reportPerformanceMetrics(const PerformanceMetrics& metrics) = 0; +}; +``` + +#### 3.1.3 IHealthMonitor +```cpp +class IHealthMonitor { +public: + virtual ~IHealthMonitor() = default; + virtual SystemHealth getCurrentHealth() const = 0; + virtual void startHealthMonitoring() = 0; + virtual void stopHealthMonitoring() = 0; + virtual Result registerHealthCallback(IHealthCallback* callback) = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 IPersistenceManager +- Persistent storage of diagnostic events +- Diagnostic data retrieval and querying +- Storage space management + +#### 3.2.2 IEventSystem +- Event publication for diagnostic notifications +- Event subscription for system events +- Asynchronous event handling + +#### 3.2.3 ISecurityManager +- Diagnostic session authentication +- Access control for diagnostic operations +- Security violation reporting + +#### 3.2.4 ISystemStateManager +- System state information for diagnostics +- State change notifications +- System health correlation + +## 4. Dynamic View + +### 4.1 Diagnostic Event Processing Sequence + +```mermaid +sequenceDiagram + participant COMP as System Component + participant DM as Diagnostics Manager + participant DC as Diagnostic Collector + participant DL as Diagnostic Logger + participant DR as Diagnostic Reporter + participant ES as Event System + + COMP->>DM: reportDiagnostic(code, severity, context) + DM->>DC: collectDiagnosticData(code, severity, context) + DC->>DC: enrichDiagnostic(timestamp, source, details) + DC->>DL: persistDiagnostic(diagnostic_event) + DL-->>DC: persistence_result + DC->>DR: reportDiagnosticEvent(diagnostic_event) + DR->>ES: publishDiagnosticEvent(event) + + alt Critical Diagnostic + DM->>DM: triggerEmergencyAction() + DM->>ES: publishCriticalAlert(event) + end +``` + +### 4.2 Health Monitoring Sequence + +```mermaid +sequenceDiagram + participant HM as Health Monitor + participant TM as Temperature Monitor + participant VM as Voltage Monitor + participant WM as Watchdog Manager + participant DM as Diagnostics Manager + + loop Health Check Cycle (1 second) + HM->>TM: getTemperature() + TM-->>HM: temperature_value + HM->>VM: getVoltage() + VM-->>HM: voltage_value + HM->>WM: getWatchdogStatus() + WM-->>HM: watchdog_status + + HM->>HM: analyzeHealthMetrics() + + alt Health Issue Detected + HM->>DM: reportHealthIssue(issue_type, severity) + end + + HM->>HM: updateHealthStatus() + end +``` + +### 4.3 Diagnostic Session Sequence + +```mermaid +sequenceDiagram + participant ENG as Engineer + participant DS as Diagnostic Session + participant DM as Diagnostics Manager + participant SM as Security Manager + participant DL as Diagnostic Logger + + ENG->>DS: requestDiagnosticSession(credentials) + DS->>SM: authenticateUser(credentials) + SM-->>DS: authentication_result + + alt Authentication Success + DS->>DM: createSession(user_id, permissions) + DM-->>DS: session_id + DS-->>ENG: session_established(session_id) + + ENG->>DS: getDiagnosticSummary() + DS->>DM: getDiagnosticSummary() + DM-->>DS: diagnostic_summary + DS-->>ENG: summary_data + + ENG->>DS: retrieveDiagnostics(filter) + DS->>DL: queryDiagnostics(filter) + DL-->>DS: diagnostic_records + DS-->>ENG: diagnostic_data + + ENG->>DS: clearDiagnostics(codes) + DS->>DM: clearDiagnosticCodes(codes) + DM->>DL: removeDiagnostics(codes) + DL-->>DM: clear_result + DM-->>DS: operation_result + DS-->>ENG: operation_complete + else Authentication Failed + DS-->>ENG: access_denied + end +``` + +## 5. Diagnostic Code System + +### 5.1 Diagnostic Code Structure +```cpp +struct DiagnosticCode { + uint16_t category; // System category (e.g., SENSOR, COMM, STORAGE) + uint16_t component; // Component identifier + uint16_t error; // Specific error code + + // Example: SEN-TEMP-001 = 0x0101001 + static constexpr uint16_t SENSOR_CATEGORY = 0x01; + static constexpr uint16_t TEMPERATURE_COMPONENT = 0x01; + static constexpr uint16_t SENSOR_FAILURE = 0x001; +}; +``` + +### 5.2 Diagnostic Severity Levels +```cpp +enum class DiagnosticSeverity { + INFO = 0, // Informational messages + WARNING = 1, // Non-critical issues + ERROR = 2, // Recoverable errors + CRITICAL = 3, // System degradation + FATAL = 4 // System failure +}; +``` + +### 5.3 Diagnostic Event Structure +```cpp +struct DiagnosticEvent { + DiagnosticCode code; + DiagnosticSeverity severity; + std::chrono::system_clock::time_point timestamp; + std::string source_component; + std::string context; + std::map metadata; + uint32_t occurrence_count; + bool is_active; +}; +``` + +## 6. Health Monitoring + +### 6.1 System Health Metrics +```cpp +struct SystemHealth { + // Temperature Monitoring + float cpu_temperature_celsius; + bool temperature_warning; + bool temperature_critical; + + // Memory Monitoring + size_t free_heap_bytes; + size_t min_free_heap_bytes; + float heap_usage_percentage; + + // Storage Monitoring + size_t sd_card_free_bytes; + size_t sd_card_total_bytes; + bool sd_card_healthy; + + // Communication Monitoring + bool main_hub_connected; + int wifi_signal_strength_dbm; + uint32_t communication_errors; + + // Power Monitoring + float supply_voltage_v; + bool brownout_detected; + + // Overall Health Status + HealthStatus overall_status; +}; + +enum class HealthStatus { + HEALTHY, + WARNING, + DEGRADED, + CRITICAL, + FAILED +}; +``` + +### 6.2 Performance Metrics +```cpp +struct PerformanceMetrics { + // CPU Utilization + float cpu_utilization_percentage; + float max_cpu_utilization_percentage; + + // Task Performance + std::map task_metrics; + + // Communication Performance + uint32_t messages_sent_per_minute; + uint32_t messages_received_per_minute; + std::chrono::milliseconds average_response_time; + + // Sensor Performance + uint32_t sensor_readings_per_minute; + std::chrono::milliseconds average_sensor_read_time; + + // Storage Performance + uint32_t storage_writes_per_minute; + std::chrono::milliseconds average_write_time; +}; +``` + +## 7. Watchdog System + +### 7.1 Watchdog Configuration +```cpp +struct WatchdogConfig { + // Task Watchdog + bool task_watchdog_enabled; + std::chrono::seconds task_watchdog_timeout; + std::vector monitored_tasks; + + // Interrupt Watchdog + bool interrupt_watchdog_enabled; + std::chrono::seconds interrupt_watchdog_timeout; + + // RTC Watchdog + bool rtc_watchdog_enabled; + std::chrono::seconds rtc_watchdog_timeout; +}; +``` + +### 7.2 Watchdog Management +- **Task Watchdog**: Monitors FreeRTOS tasks for deadlocks (10s timeout) +- **Interrupt Watchdog**: Detects ISR hangs (3s timeout) +- **RTC Watchdog**: Final safety net for total system freeze (30s timeout) + +## 8. Configuration + +### 8.1 Diagnostics Configuration +```cpp +struct DiagnosticsConfig { + // Storage Configuration + size_t max_diagnostic_records; + std::chrono::hours diagnostic_retention_period; + bool persistent_storage_enabled; + + // Health Monitoring Configuration + std::chrono::seconds health_check_interval; + HealthThresholds health_thresholds; + bool continuous_monitoring_enabled; + + // Reporting Configuration + bool real_time_reporting_enabled; + DiagnosticSeverity min_reporting_severity; + std::chrono::seconds reporting_interval; + + // Session Configuration + std::chrono::minutes session_timeout; + uint32_t max_concurrent_sessions; + bool remote_sessions_enabled; +}; +``` + +## 9. Error Handling + +### 9.1 Error Categories +- **Storage Errors**: Diagnostic persistence failures +- **Memory Errors**: Insufficient memory for diagnostic operations +- **Configuration Errors**: Invalid diagnostic configuration +- **Session Errors**: Authentication or authorization failures +- **Hardware Errors**: Sensor or monitoring hardware failures + +### 9.2 Error Recovery Strategies +- **Graceful Degradation**: Continue operation with reduced diagnostic capability +- **Memory Management**: Implement diagnostic record rotation and cleanup +- **Fallback Storage**: Use alternative storage when primary fails +- **Self-Diagnostics**: Monitor diagnostic system health + +## 10. Performance Characteristics + +### 10.1 Timing Requirements +- **Diagnostic Event Processing**: < 10ms per event +- **Health Check Cycle**: 1 second interval +- **Diagnostic Query Response**: < 100ms for typical queries +- **Session Operations**: < 500ms for session establishment + +### 10.2 Resource Usage +- **Memory**: < 16KB for diagnostic buffers and metadata +- **Storage**: Configurable with rotation (default 1MB) +- **CPU**: < 2% average utilization for monitoring + +## 11. Security Considerations + +### 11.1 Access Control +- Diagnostic session authentication required +- Role-based access to diagnostic operations +- Audit logging of diagnostic access and modifications + +### 11.2 Data Protection +- Sensitive diagnostic data encryption +- Secure diagnostic data transmission +- Diagnostic data integrity verification + +## 12. Testing Strategy + +### 12.1 Unit Tests +- Diagnostic event processing and storage +- Health monitoring algorithms +- Watchdog management functionality +- Session management and authentication + +### 12.2 Integration Tests +- End-to-end diagnostic reporting +- Health monitoring integration +- Diagnostic session workflows +- Cross-component diagnostic correlation + +### 12.3 Hardware Tests +- Watchdog timeout and recovery testing +- Hardware monitoring accuracy +- Performance under stress conditions + +## 13. Dependencies + +### 13.1 Internal Dependencies +- Persistence Manager for diagnostic storage +- Event System for diagnostic notifications +- Security Manager for session authentication +- System State Manager for system context + +### 13.2 External Dependencies +- ESP-IDF watchdog APIs +- FreeRTOS task monitoring +- Hardware monitoring peripherals +- File system for diagnostic storage + +## 14. Constraints and Assumptions + +### 14.1 Constraints +- Diagnostic system must remain operational during system faults +- Memory usage must be bounded and predictable +- Diagnostic operations must not interfere with real-time requirements +- Storage space for diagnostics is limited and requires rotation + +### 14.2 Assumptions +- Sufficient system resources for diagnostic operations +- Reliable storage medium for diagnostic persistence +- Proper system time for diagnostic timestamping +- Valid security credentials for diagnostic sessions \ No newline at end of file diff --git a/1 software design/components/error_handler/COMPONENT.md b/1 software design/components/error_handler/COMPONENT.md new file mode 100644 index 0000000..9599826 --- /dev/null +++ b/1 software design/components/error_handler/COMPONENT.md @@ -0,0 +1,494 @@ +# Error Handler Component Specification + +**Component ID:** C-ERROR-001 +**Component Name:** Error Handler +**Version:** 1.0 +**Date:** 2025-02-01 +**Location:** `application_layer/error_handler/` + +## 1. Component Overview + +### 1.1 Purpose +The Error Handler component provides centralized error handling, fault classification, and recovery coordination for the ASF Sensor Hub. It serves as the primary mechanism for detecting, classifying, and responding to system faults while coordinating recovery strategies across all system components. + +### 1.2 Scope +- Centralized error detection and classification +- Fault severity assessment and escalation +- Recovery strategy implementation and coordination +- Error logging and diagnostic reporting +- Integration with System State Manager for fault-driven state transitions + +### 1.3 Responsibilities +- Receive and classify errors from all system components +- Assess fault severity and determine appropriate response +- Coordinate recovery attempts with affected components +- Escalate critical faults to System State Manager +- Maintain error history and statistics +- Provide error reporting interface for diagnostics + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Error Handler" + EH[Error Controller] + FC[Fault Classifier] + RS[Recovery Strategist] + EL[Error Logger] + ES[Error Statistics] + end + + subgraph "External Components" + STM[System State Manager] + DIAG[Diagnostics Manager] + LOGGER[Logger] + COMPS[System Components] + end + + EH --> FC + EH --> RS + EH --> EL + EH --> ES + + COMPS --> EH + EH --> STM + EH --> DIAG + EL --> LOGGER +``` + +### 2.2 Internal Components + +#### 2.2.1 Error Controller +- Central coordination of error handling operations +- Error routing and processing workflow management +- Component lifecycle and state management + +#### 2.2.2 Fault Classifier +- Error categorization and severity assessment +- Fault pattern recognition and analysis +- Error correlation and root cause analysis + +#### 2.2.3 Recovery Strategist +- Recovery strategy selection and implementation +- Recovery attempt coordination with components +- Recovery success/failure tracking + +#### 2.2.4 Error Logger +- Error event logging and persistence +- Error history management +- Integration with system logger + +#### 2.2.5 Error Statistics +- Error frequency and pattern tracking +- System reliability metrics calculation +- Error trend analysis + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 IErrorHandler +```cpp +class IErrorHandler { +public: + virtual ~IErrorHandler() = default; + + // Error Reporting + virtual Result reportError(const ErrorInfo& error) = 0; + virtual Result reportFault(FaultSeverity severity, + ComponentId component, + const std::string& description) = 0; + virtual Result reportRecovery(ComponentId component, + RecoveryResult result) = 0; + + // Error Query + virtual std::vector getActiveErrors() const = 0; + virtual std::vector getErrorHistory( + std::chrono::system_clock::time_point since) const = 0; + virtual ErrorStatistics getErrorStatistics() const = 0; + + // Recovery Management + virtual Result initiateRecovery(ComponentId component) = 0; + virtual Result clearError(ErrorId error_id) = 0; + virtual Result clearAllErrors() = 0; + + // System Integration + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual SystemHealthStatus getSystemHealth() const = 0; +}; +``` + +#### 3.1.2 IFaultClassifier +```cpp +class IFaultClassifier { +public: + virtual ~IFaultClassifier() = default; + virtual FaultSeverity classifyFault(const ErrorInfo& error) const = 0; + virtual FaultCategory categorizeFault(const ErrorInfo& error) const = 0; + virtual RecoveryStrategy selectRecoveryStrategy(const ErrorInfo& error) const = 0; + virtual bool shouldEscalateToStateManager(const ErrorInfo& error) const = 0; +}; +``` + +#### 3.1.3 IRecoveryManager +```cpp +class IRecoveryManager { +public: + virtual ~IRecoveryManager() = default; + virtual Result executeRecovery(const ErrorInfo& error, + RecoveryStrategy strategy) = 0; + virtual bool isRecoveryInProgress(ComponentId component) const = 0; + virtual RecoveryStatus getRecoveryStatus(ComponentId component) const = 0; + virtual void cancelRecovery(ComponentId component) = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 ISystemStateManager +- System state transition requests for critical faults +- System state information for context-aware error handling +- Teardown coordination for recovery operations + +#### 3.2.2 IDiagnosticsManager +- Diagnostic event reporting for error conditions +- System health correlation with error patterns +- Diagnostic session support for error analysis + +#### 3.2.3 ILogger +- Error event logging and persistence +- Log level management for error messages +- Log rotation and storage management + +#### 3.2.4 IEventSystem +- Error event publication for system-wide notification +- Event subscription for component status updates +- Asynchronous error notification delivery + +## 4. Dynamic View + +### 4.1 Error Handling Sequence + +```mermaid +sequenceDiagram + participant COMP as System Component + participant EH as Error Handler + participant FC as Fault Classifier + participant RS as Recovery Strategist + participant STM as System State Manager + participant DIAG as Diagnostics Manager + + COMP->>EH: reportError(error_info) + EH->>FC: classifyFault(error_info) + FC->>FC: assessSeverity(error_info) + FC-->>EH: fault_classification + + alt Critical Fault + EH->>STM: requestStateTransition(FAULT, error_info) + EH->>DIAG: reportCriticalFault(error_info) + else Recoverable Fault + EH->>RS: selectRecoveryStrategy(error_info) + RS-->>EH: recovery_strategy + EH->>RS: executeRecovery(error_info, strategy) + RS->>COMP: initiateRecovery() + COMP-->>RS: recovery_result + + alt Recovery Successful + RS-->>EH: recovery_success + EH->>EH: clearError(error_id) + else Recovery Failed + RS-->>EH: recovery_failed + EH->>FC: escalateFault(error_info) + end + end + + EH->>EH: updateErrorStatistics() + EH-->>COMP: error_handled +``` + +### 4.2 Recovery Coordination Sequence + +```mermaid +sequenceDiagram + participant EH as Error Handler + participant RS as Recovery Strategist + participant SM as Sensor Manager + participant CM as Communication Manager + participant STM as System State Manager + + EH->>RS: executeRecovery(SENSOR_FAULT, RESTART_COMPONENT) + RS->>SM: requestComponentRestart() + SM->>SM: stopSensorAcquisition() + SM->>SM: reinitializeSensors() + SM-->>RS: restart_complete + + alt Restart Successful + RS->>SM: validateComponentHealth() + SM-->>RS: health_check_passed + RS-->>EH: recovery_successful + else Restart Failed + RS-->>EH: recovery_failed + EH->>STM: escalateToSystemLevel(SENSOR_FAULT) + end +``` + +### 4.3 Error Escalation Sequence + +```mermaid +sequenceDiagram + participant EH as Error Handler + participant FC as Fault Classifier + participant STM as System State Manager + participant DIAG as Diagnostics Manager + participant ES as Event System + + EH->>FC: evaluateEscalation(repeated_fault) + FC->>FC: analyzeErrorPattern() + FC-->>EH: escalation_required + + EH->>STM: requestStateTransition(WARNING, fault_info) + STM-->>EH: state_transition_accepted + + EH->>DIAG: reportSystemDegradation(fault_pattern) + EH->>ES: publishSystemAlert(DEGRADED_PERFORMANCE) + + EH->>EH: updateSystemHealthStatus(DEGRADED) +``` + +## 5. Error Classification System + +### 5.1 Error Categories +```cpp +enum class ErrorCategory { + SENSOR_ERROR, // Sensor communication or data errors + COMMUNICATION_ERROR, // Network or protocol errors + STORAGE_ERROR, // File system or storage device errors + MEMORY_ERROR, // Memory allocation or corruption errors + SECURITY_ERROR, // Security violation or authentication errors + CONFIGURATION_ERROR, // Configuration validation or loading errors + HARDWARE_ERROR, // Hardware failure or malfunction + SOFTWARE_ERROR, // Software logic or state errors + SYSTEM_ERROR, // System-level or integration errors + UNKNOWN_ERROR // Unclassified errors +}; +``` + +### 5.2 Fault Severity Levels +```cpp +enum class FaultSeverity { + INFO = 0, // Informational, no action required + WARNING = 1, // Warning condition, monitoring required + MINOR = 2, // Minor fault, automatic recovery possible + MAJOR = 3, // Major fault, component degradation + CRITICAL = 4, // Critical fault, system degradation + FATAL = 5 // Fatal fault, system failure imminent +}; +``` + +### 5.3 Recovery Strategies +```cpp +enum class RecoveryStrategy { + NO_ACTION, // No recovery action needed + RETRY_OPERATION, // Retry the failed operation + RESTART_COMPONENT, // Restart the affected component + RESET_CONFIGURATION, // Reset to default configuration + ISOLATE_COMPONENT, // Isolate faulty component + SYSTEM_RESTART, // Restart entire system + MANUAL_INTERVENTION // Requires manual intervention +}; +``` + +### 5.4 Error Information Structure +```cpp +struct ErrorInfo { + ErrorId error_id; + ComponentId source_component; + ErrorCategory category; + FaultSeverity severity; + std::string description; + std::string context; + std::chrono::system_clock::time_point timestamp; + std::map metadata; + uint32_t occurrence_count; + RecoveryStrategy suggested_recovery; + bool is_active; + bool requires_escalation; +}; +``` + +## 6. Recovery Management + +### 6.1 Recovery Strategies Implementation +```cpp +class RecoveryManager { +public: + Result executeRetryOperation(const ErrorInfo& error); + Result executeComponentRestart(ComponentId component); + Result executeConfigurationReset(ComponentId component); + Result executeComponentIsolation(ComponentId component); + Result executeSystemRestart(); + +private: + std::map recovery_status_; + std::chrono::seconds recovery_timeout_; + uint32_t max_recovery_attempts_; +}; +``` + +### 6.2 Recovery Coordination +- **Component Restart**: Coordinate with component to perform clean restart +- **Configuration Reset**: Load default configuration for affected component +- **Component Isolation**: Disable component while maintaining system operation +- **System Restart**: Coordinate with System State Manager for controlled restart + +## 7. Error Statistics and Monitoring + +### 7.1 Error Statistics +```cpp +struct ErrorStatistics { + // Overall Statistics + uint32_t total_errors; + uint32_t active_errors; + uint32_t resolved_errors; + + // By Category + std::map errors_by_category; + + // By Severity + std::map errors_by_severity; + + // By Component + std::map errors_by_component; + + // Recovery Statistics + uint32_t successful_recoveries; + uint32_t failed_recoveries; + float recovery_success_rate; + + // Timing Statistics + std::chrono::milliseconds avg_resolution_time; + std::chrono::milliseconds max_resolution_time; + + // System Health + SystemHealthStatus overall_health; + float system_reliability_score; +}; +``` + +### 7.2 Health Assessment +- **System Reliability Score**: Based on error frequency and recovery success +- **Component Health Tracking**: Individual component reliability metrics +- **Trend Analysis**: Error pattern recognition and prediction +- **Performance Impact**: Error impact on system performance metrics + +## 8. Configuration + +### 8.1 Error Handler Configuration +```cpp +struct ErrorHandlerConfig { + // Classification Configuration + std::map default_severities; + std::map component_criticality; + + // Recovery Configuration + uint32_t max_recovery_attempts; + std::chrono::seconds recovery_timeout; + bool automatic_recovery_enabled; + + // Escalation Configuration + uint32_t escalation_threshold; + std::chrono::minutes escalation_window; + bool automatic_escalation_enabled; + + // Logging Configuration + bool persistent_error_logging; + uint32_t max_error_history_size; + std::chrono::hours error_retention_period; + + // Statistics Configuration + bool statistics_enabled; + std::chrono::minutes statistics_update_interval; +}; +``` + +## 9. Performance Characteristics + +### 9.1 Timing Requirements +- **Error Processing**: < 10ms for error classification and initial response +- **Recovery Initiation**: < 100ms for recovery strategy execution +- **Error Logging**: < 5ms for error event logging +- **Statistics Update**: < 50ms for statistics calculation + +### 9.2 Resource Usage +- **Memory**: < 8KB for error tracking and statistics +- **Storage**: Configurable error history with rotation +- **CPU**: < 1% average utilization for error monitoring + +## 10. Security Considerations + +### 10.1 Error Information Security +- Sensitive error information sanitization +- Access control for error history and statistics +- Secure error reporting to external systems + +### 10.2 Recovery Security +- Authentication for manual recovery operations +- Validation of recovery commands and parameters +- Audit logging of recovery actions + +## 11. Testing Strategy + +### 11.1 Unit Tests +- Error classification algorithms +- Recovery strategy selection logic +- Error statistics calculation +- Configuration validation + +### 11.2 Integration Tests +- End-to-end error handling workflows +- Recovery coordination with components +- System state manager integration +- Diagnostic system integration + +### 11.3 Fault Injection Tests +- Simulated component failures +- Recovery mechanism validation +- Error escalation scenarios +- System resilience testing + +## 12. Dependencies + +### 12.1 Internal Dependencies +- System State Manager for fault escalation +- Diagnostics Manager for error reporting +- Logger for error event logging +- Event System for error notifications + +### 12.2 External Dependencies +- Component interfaces for recovery coordination +- Configuration system for error handler settings +- Time utilities for timestamp generation +- Storage system for error persistence + +## 13. Constraints and Assumptions + +### 13.1 Constraints +- Error handling must not significantly impact system performance +- Recovery operations must be bounded in time and resources +- Error information must be stored within memory constraints +- Recovery strategies must not compromise system security + +### 13.2 Assumptions +- Components provide accurate error information +- Recovery operations are implemented by components +- System State Manager is available for escalation +- Sufficient system resources for error handling operations + +--- + +**Document Status:** Final for Implementation +**Dependencies:** System State Manager, Diagnostics Manager, Logger, Event System +**Next Review:** After component implementation and integration testing \ No newline at end of file diff --git a/1 software design/components/hardware_abstraction_layer/COMPONENT.md b/1 software design/components/hardware_abstraction_layer/COMPONENT.md new file mode 100644 index 0000000..64bcd5c --- /dev/null +++ b/1 software design/components/hardware_abstraction_layer/COMPONENT.md @@ -0,0 +1,750 @@ +# Hardware Abstraction Layer (HAL) Component Specification + +**Component ID:** C-HAL-001 +**Component Name:** Hardware Abstraction Layer +**Version:** 1.0 +**Date:** 2025-02-01 +**Location:** `drivers/hal/` + +## 1. Component Overview + +### 1.1 Purpose +The Hardware Abstraction Layer (HAL) provides a unified, platform-independent interface for accessing hardware peripherals on the ESP32-S3. It abstracts the ESP-IDF hardware APIs and provides consistent interfaces for I2C, SPI, UART, GPIO, ADC, and other peripherals used by the ASF Sensor Hub. + +### 1.2 Scope +- Hardware peripheral abstraction (I2C, SPI, UART, GPIO, ADC, PWM, Timers) +- Platform-independent hardware interface definitions +- Resource management and conflict resolution +- Hardware configuration and initialization +- Error handling and hardware fault detection +- Performance optimization for embedded constraints + +### 1.3 Responsibilities +- Provide unified interfaces for hardware peripherals +- Abstract ESP-IDF specific implementations +- Manage hardware resource allocation and sharing +- Handle hardware initialization and configuration +- Implement hardware error detection and recovery +- Optimize hardware operations for performance and power + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Hardware Abstraction Layer" + HC[HAL Controller] + I2C[I2C Interface] + SPI[SPI Interface] + UART[UART Interface] + GPIO[GPIO Interface] + ADC[ADC Interface] + PWM[PWM Interface] + TIMER[Timer Interface] + RM[Resource Manager] + end + + subgraph "ESP-IDF Layer" + I2C_DRV[I2C Driver] + SPI_DRV[SPI Driver] + UART_DRV[UART Driver] + GPIO_DRV[GPIO Driver] + ADC_DRV[ADC Driver] + PWM_DRV[PWM Driver] + TIMER_DRV[Timer Driver] + end + + subgraph "Hardware" + ESP32S3[ESP32-S3 MCU] + SENSORS[Sensors] + STORAGE[Storage] + DISPLAY[Display] + end + + HC --> I2C + HC --> SPI + HC --> UART + HC --> GPIO + HC --> ADC + HC --> PWM + HC --> TIMER + HC --> RM + + I2C --> I2C_DRV + SPI --> SPI_DRV + UART --> UART_DRV + GPIO --> GPIO_DRV + ADC --> ADC_DRV + PWM --> PWM_DRV + TIMER --> TIMER_DRV + + I2C_DRV --> ESP32S3 + SPI_DRV --> ESP32S3 + UART_DRV --> ESP32S3 + GPIO_DRV --> ESP32S3 + ADC_DRV --> ESP32S3 + PWM_DRV --> ESP32S3 + TIMER_DRV --> ESP32S3 + + ESP32S3 --> SENSORS + ESP32S3 --> STORAGE + ESP32S3 --> DISPLAY +``` + +### 2.2 Internal Components + +#### 2.2.1 HAL Controller +- Central coordination of hardware abstraction operations +- Hardware subsystem initialization and management +- Cross-peripheral coordination and resource arbitration + +#### 2.2.2 I2C Interface +- I2C bus management and communication +- Multi-master and multi-slave support +- Clock stretching and error recovery + +#### 2.2.3 SPI Interface +- SPI bus management and communication +- Multiple SPI device support +- DMA integration for high-speed transfers + +#### 2.2.4 UART Interface +- Serial communication management +- Multiple UART port support +- Flow control and buffering + +#### 2.2.5 GPIO Interface +- Digital I/O pin management +- Interrupt handling and debouncing +- Pin multiplexing and configuration + +#### 2.2.6 ADC Interface +- Analog-to-digital conversion +- Multi-channel sampling +- Calibration and noise filtering + +#### 2.2.7 PWM Interface +- Pulse-width modulation generation +- Multiple channel support +- Frequency and duty cycle control + +#### 2.2.8 Timer Interface +- Hardware timer management +- Periodic and one-shot timers +- High-resolution timing + +#### 2.2.9 Resource Manager +- Hardware resource allocation tracking +- Conflict detection and resolution +- Resource sharing coordination + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 IHAL +```cpp +class IHAL { +public: + virtual ~IHAL() = default; + + // System Management + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual HardwareStatus getHardwareStatus() const = 0; + + // Interface Access + virtual II2C* getI2CInterface(I2CPort port) = 0; + virtual ISPI* getSPIInterface(SPIPort port) = 0; + virtual IUART* getUARTInterface(UARTPort port) = 0; + virtual IGPIO* getGPIOInterface() = 0; + virtual IADC* getADCInterface() = 0; + virtual IPWM* getPWMInterface() = 0; + virtual ITimer* getTimerInterface() = 0; + + // Resource Management + virtual Result reserveResource(HardwareResource resource, ComponentId component) = 0; + virtual Result releaseResource(HardwareResource resource, ComponentId component) = 0; + virtual bool isResourceAvailable(HardwareResource resource) const = 0; + virtual std::vector getAvailableResources() const = 0; +}; +``` + +#### 3.1.2 II2C +```cpp +class II2C { +public: + virtual ~II2C() = default; + + // Configuration + virtual Result configure(const I2CConfig& config) = 0; + virtual Result setClockSpeed(uint32_t frequency_hz) = 0; + virtual Result setTimeout(std::chrono::milliseconds timeout) = 0; + + // Communication + virtual Result write(uint8_t device_address, const uint8_t* data, size_t length) = 0; + virtual Result read(uint8_t device_address, uint8_t* data, size_t length) = 0; + virtual Result writeRead(uint8_t device_address, + const uint8_t* write_data, size_t write_length, + uint8_t* read_data, size_t read_length) = 0; + + // Device Management + virtual Result probeDevice(uint8_t device_address) = 0; + virtual std::vector scanBus() = 0; + + // Status and Control + virtual bool isBusy() const = 0; + virtual I2CStatus getStatus() const = 0; + virtual Result reset() = 0; +}; +``` + +#### 3.1.3 ISPI +```cpp +class ISPI { +public: + virtual ~ISPI() = default; + + // Configuration + virtual Result configure(const SPIConfig& config) = 0; + virtual Result addDevice(const SPIDeviceConfig& device_config) = 0; + virtual Result removeDevice(SPIDeviceHandle device) = 0; + + // Communication + virtual Result transmit(SPIDeviceHandle device, const uint8_t* data, size_t length) = 0; + virtual Result receive(SPIDeviceHandle device, uint8_t* data, size_t length) = 0; + virtual Result transmitReceive(SPIDeviceHandle device, + const uint8_t* tx_data, uint8_t* rx_data, size_t length) = 0; + + // DMA Support + virtual Result transmitDMA(SPIDeviceHandle device, const uint8_t* data, size_t length) = 0; + virtual Result receiveDMA(SPIDeviceHandle device, uint8_t* data, size_t length) = 0; + virtual bool isDMATransferComplete() const = 0; + + // Status and Control + virtual SPIStatus getStatus() const = 0; + virtual Result reset() = 0; +}; +``` + +#### 3.1.4 IUART +```cpp +class IUART { +public: + virtual ~IUART() = default; + + // Configuration + virtual Result configure(const UARTConfig& config) = 0; + virtual Result setBaudRate(uint32_t baud_rate) = 0; + virtual Result setFlowControl(UARTFlowControl flow_control) = 0; + + // Communication + virtual Result write(const uint8_t* data, size_t length) = 0; + virtual Result read(uint8_t* data, size_t length) = 0; + virtual Result writeString(const std::string& str) = 0; + virtual Result readString(size_t max_length) = 0; + + // Buffering + virtual size_t getAvailableBytes() const = 0; + virtual size_t getTxBufferSpace() const = 0; + virtual Result flush() = 0; + virtual Result clearBuffers() = 0; + + // Status and Control + virtual UARTStatus getStatus() const = 0; + virtual bool isConnected() const = 0; +}; +``` + +#### 3.1.5 IGPIO +```cpp +class IGPIO { +public: + virtual ~IGPIO() = default; + + // Pin Configuration + virtual Result configurePin(GPIOPin pin, GPIOMode mode, GPIOPull pull = GPIOPull::NONE) = 0; + virtual Result setDirection(GPIOPin pin, GPIODirection direction) = 0; + virtual Result setPull(GPIOPin pin, GPIOPull pull) = 0; + virtual Result setDriveStrength(GPIOPin pin, GPIODriveStrength strength) = 0; + + // Digital I/O + virtual Result writePin(GPIOPin pin, bool value) = 0; + virtual Result readPin(GPIOPin pin) = 0; + virtual Result togglePin(GPIOPin pin) = 0; + + // Interrupt Management + virtual Result enableInterrupt(GPIOPin pin, GPIOInterruptType type, + GPIOInterruptHandler handler) = 0; + virtual Result disableInterrupt(GPIOPin pin) = 0; + + // Status + virtual GPIOPinStatus getPinStatus(GPIOPin pin) const = 0; + virtual std::vector getAvailablePins() const = 0; +}; +``` + +#### 3.1.6 IADC +```cpp +class IADC { +public: + virtual ~IADC() = default; + + // Configuration + virtual Result configure(const ADCConfig& config) = 0; + virtual Result configureChannel(ADCChannel channel, const ADCChannelConfig& config) = 0; + virtual Result setAttenuation(ADCChannel channel, ADCAttenuation attenuation) = 0; + + // Single Conversion + virtual Result readRaw(ADCChannel channel) = 0; + virtual Result readVoltage(ADCChannel channel) = 0; + virtual Result readCalibrated(ADCChannel channel) = 0; + + // Continuous Conversion + virtual Result startContinuous(const std::vector& channels) = 0; + virtual Result stopContinuous() = 0; + virtual Result> readContinuous() = 0; + + // Calibration + virtual Result calibrate(ADCChannel channel) = 0; + virtual Result setCalibrationData(ADCChannel channel, const ADCCalibrationData& data) = 0; + + // Status + virtual ADCStatus getStatus() const = 0; + virtual bool isChannelAvailable(ADCChannel channel) const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 ESP-IDF Hardware Drivers +- I2C driver APIs for I2C communication +- SPI driver APIs for SPI communication +- UART driver APIs for serial communication +- GPIO driver APIs for digital I/O +- ADC driver APIs for analog input +- PWM driver APIs for pulse generation +- Timer driver APIs for timing operations + +#### 3.2.2 ILogger +- Hardware operation logging +- Error condition reporting +- Performance metrics logging + +#### 3.2.3 IErrorHandler +- Hardware error reporting and classification +- Hardware fault recovery coordination +- Error escalation for critical hardware failures + +## 4. Dynamic View + +### 4.1 Hardware Initialization Sequence + +```mermaid +sequenceDiagram + participant SYS as System Controller + participant HAL as HAL Controller + participant I2C as I2C Interface + participant SPI as SPI Interface + participant GPIO as GPIO Interface + participant RM as Resource Manager + + SYS->>HAL: initialize() + HAL->>RM: initializeResourceManager() + RM-->>HAL: resource_manager_ready + + par Parallel Initialization + HAL->>I2C: initialize(I2C_CONFIG) + I2C->>I2C: configureHardware() + I2C-->>HAL: i2c_ready + and + HAL->>SPI: initialize(SPI_CONFIG) + SPI->>SPI: configureHardware() + SPI-->>HAL: spi_ready + and + HAL->>GPIO: initialize(GPIO_CONFIG) + GPIO->>GPIO: configureHardware() + GPIO-->>HAL: gpio_ready + end + + HAL->>HAL: validateHardwareStatus() + HAL-->>SYS: initialization_complete +``` + +### 4.2 I2C Communication Sequence + +```mermaid +sequenceDiagram + participant SENSOR as Sensor Driver + participant I2C as I2C Interface + participant ESP_I2C as ESP-IDF I2C + participant HW as Hardware + + SENSOR->>I2C: writeRead(device_addr, cmd_data, response_buffer) + I2C->>I2C: validateParameters() + I2C->>ESP_I2C: i2c_master_write_read_device() + ESP_I2C->>HW: hardware_transaction + HW-->>ESP_I2C: transaction_result + ESP_I2C-->>I2C: operation_result + + alt Transaction Successful + I2C-->>SENSOR: success(response_data) + else Transaction Failed + I2C->>I2C: analyzeError() + I2C->>I2C: attemptRecovery() + I2C-->>SENSOR: error(error_code) + end +``` + +### 4.3 Resource Management Sequence + +```mermaid +sequenceDiagram + participant COMP1 as Component 1 + participant COMP2 as Component 2 + participant RM as Resource Manager + participant HAL as HAL Controller + + COMP1->>RM: reserveResource(I2C_PORT_0, COMPONENT_SENSOR) + RM->>RM: checkResourceAvailability(I2C_PORT_0) + RM->>RM: allocateResource(I2C_PORT_0, COMPONENT_SENSOR) + RM-->>COMP1: resource_reserved + + COMP2->>RM: reserveResource(I2C_PORT_0, COMPONENT_DISPLAY) + RM->>RM: checkResourceAvailability(I2C_PORT_0) + RM-->>COMP2: resource_unavailable(already_allocated) + + COMP1->>RM: releaseResource(I2C_PORT_0, COMPONENT_SENSOR) + RM->>RM: deallocateResource(I2C_PORT_0) + RM-->>COMP1: resource_released + + COMP2->>RM: reserveResource(I2C_PORT_0, COMPONENT_DISPLAY) + RM->>RM: allocateResource(I2C_PORT_0, COMPONENT_DISPLAY) + RM-->>COMP2: resource_reserved +``` + +## 5. Hardware Resource Management + +### 5.1 Resource Types +```cpp +enum class HardwareResource { + // Communication Interfaces + I2C_PORT_0, + I2C_PORT_1, + SPI_PORT_2, + SPI_PORT_3, + UART_PORT_0, + UART_PORT_1, + UART_PORT_2, + + // GPIO Pins + GPIO_PIN_0, + GPIO_PIN_1, + // ... (all available GPIO pins) + + // ADC Channels + ADC1_CHANNEL_0, + ADC1_CHANNEL_1, + // ... (all ADC channels) + + // PWM Channels + PWM_CHANNEL_0, + PWM_CHANNEL_1, + // ... (all PWM channels) + + // Timers + TIMER_GROUP_0_TIMER_0, + TIMER_GROUP_0_TIMER_1, + TIMER_GROUP_1_TIMER_0, + TIMER_GROUP_1_TIMER_1 +}; +``` + +### 5.2 Resource Allocation +```cpp +class ResourceManager { +public: + Result reserveResource(HardwareResource resource, ComponentId component); + Result releaseResource(HardwareResource resource, ComponentId component); + bool isResourceAvailable(HardwareResource resource) const; + ComponentId getResourceOwner(HardwareResource resource) const; + std::vector getResourcesByComponent(ComponentId component) const; + +private: + std::map resource_allocations_; + std::mutex allocation_mutex_; +}; +``` + +### 5.3 Conflict Resolution +- **Exclusive Access**: Most resources require exclusive access +- **Shared Access**: Some resources (like GPIO) can be shared with coordination +- **Priority-Based**: Higher priority components can preempt lower priority ones +- **Time-Based**: Resources can be allocated for specific time periods + +## 6. Hardware Configuration + +### 6.1 I2C Configuration +```cpp +struct I2CConfig { + uint32_t clock_speed_hz; // Clock frequency (100kHz, 400kHz, 1MHz) + GPIOPin sda_pin; // SDA pin number + GPIOPin scl_pin; // SCL pin number + bool pullup_enable; // Enable internal pullups + std::chrono::milliseconds timeout; // Transaction timeout + uint8_t slave_address; // Slave address (for slave mode) + I2CMode mode; // Master or slave mode +}; +``` + +### 6.2 SPI Configuration +```cpp +struct SPIConfig { + uint32_t clock_speed_hz; // Clock frequency + GPIOPin mosi_pin; // MOSI pin number + GPIOPin miso_pin; // MISO pin number + GPIOPin sclk_pin; // SCLK pin number + SPIMode mode; // SPI mode (0, 1, 2, 3) + SPIBitOrder bit_order; // MSB or LSB first + bool use_dma; // Enable DMA transfers + size_t dma_buffer_size; // DMA buffer size +}; + +struct SPIDeviceConfig { + GPIOPin cs_pin; // Chip select pin + uint32_t clock_speed_hz; // Device-specific clock speed + SPIMode mode; // Device-specific SPI mode + uint8_t command_bits; // Command phase bits + uint8_t address_bits; // Address phase bits + uint8_t dummy_bits; // Dummy phase bits +}; +``` + +### 6.3 UART Configuration +```cpp +struct UARTConfig { + uint32_t baud_rate; // Baud rate + UARTDataBits data_bits; // Data bits (5, 6, 7, 8) + UARTStopBits stop_bits; // Stop bits (1, 1.5, 2) + UARTParity parity; // Parity (none, even, odd) + UARTFlowControl flow_control; // Flow control (none, RTS/CTS) + GPIOPin tx_pin; // TX pin number + GPIOPin rx_pin; // RX pin number + GPIOPin rts_pin; // RTS pin number (if flow control enabled) + GPIOPin cts_pin; // CTS pin number (if flow control enabled) + size_t tx_buffer_size; // TX buffer size + size_t rx_buffer_size; // RX buffer size +}; +``` + +### 6.4 GPIO Configuration +```cpp +enum class GPIOMode { + INPUT, // Input mode + OUTPUT, // Output mode + INPUT_OUTPUT, // Bidirectional mode + ANALOG // Analog mode (for ADC) +}; + +enum class GPIOPull { + NONE, // No pull resistor + PULLUP, // Pull-up resistor + PULLDOWN // Pull-down resistor +}; + +enum class GPIODriveStrength { + WEAK, // 5mA drive strength + MEDIUM, // 10mA drive strength + STRONG, // 20mA drive strength + MAXIMUM // 40mA drive strength +}; +``` + +### 6.5 ADC Configuration +```cpp +struct ADCConfig { + ADCResolution resolution; // Resolution (9, 10, 11, 12 bits) + ADCAttenuation attenuation; // Attenuation (0dB, 2.5dB, 6dB, 11dB) + uint32_t sample_rate_hz; // Sample rate for continuous mode + bool enable_calibration; // Enable automatic calibration +}; + +struct ADCChannelConfig { + ADCAttenuation attenuation; // Channel-specific attenuation + uint32_t samples_per_read; // Number of samples to average + bool enable_filter; // Enable digital filter +}; +``` + +## 7. Error Handling and Recovery + +### 7.1 Hardware Error Types +```cpp +enum class HardwareError { + // Communication Errors + I2C_TIMEOUT, + I2C_NACK, + I2C_BUS_ERROR, + SPI_TIMEOUT, + SPI_DMA_ERROR, + UART_FRAME_ERROR, + UART_PARITY_ERROR, + UART_OVERRUN, + + // Configuration Errors + INVALID_PIN_CONFIG, + RESOURCE_CONFLICT, + CLOCK_CONFIG_ERROR, + + // Hardware Faults + PERIPHERAL_NOT_RESPONDING, + POWER_SUPPLY_ERROR, + CLOCK_FAILURE, + HARDWARE_FAULT +}; +``` + +### 7.2 Recovery Strategies +```cpp +class HardwareErrorHandler { +public: + Result handleError(HardwareError error, HardwareResource resource); + Result resetPeripheral(HardwareResource resource); + Result reinitializePeripheral(HardwareResource resource); + bool isRecoveryPossible(HardwareError error) const; + +private: + Result recoverI2CBus(I2CPort port); + Result recoverSPIBus(SPIPort port); + Result recoverUARTPort(UARTPort port); + Result resetGPIOPin(GPIOPin pin); +}; +``` + +### 7.3 Hardware Monitoring +- **Bus State Monitoring**: Monitor I2C/SPI bus states for errors +- **Signal Quality**: Monitor signal integrity and noise levels +- **Power Supply Monitoring**: Monitor supply voltages and current +- **Temperature Monitoring**: Monitor MCU temperature for thermal protection + +## 8. Performance Optimization + +### 8.1 DMA Integration +- **SPI DMA**: High-speed SPI transfers using DMA +- **UART DMA**: Efficient serial communication with DMA +- **ADC DMA**: Continuous ADC sampling with DMA +- **Memory Management**: Efficient DMA buffer management + +### 8.2 Interrupt Handling +- **GPIO Interrupts**: Efficient GPIO interrupt handling +- **Communication Interrupts**: I2C/SPI/UART interrupt handling +- **Timer Interrupts**: Precise timing with hardware timers +- **Interrupt Prioritization**: Proper interrupt priority configuration + +### 8.3 Power Management +- **Clock Gating**: Disable unused peripheral clocks +- **Sleep Modes**: Support for various sleep modes +- **Dynamic Frequency Scaling**: Adjust clock frequencies based on load +- **Peripheral Power Control**: Enable/disable peripherals as needed + +## 9. Configuration + +### 9.1 HAL Configuration +```cpp +struct HALConfig { + // I2C Configuration + std::map i2c_configs; + + // SPI Configuration + std::map spi_configs; + + // UART Configuration + std::map uart_configs; + + // GPIO Configuration + std::map gpio_configs; + + // ADC Configuration + ADCConfig adc_config; + std::map adc_channel_configs; + + // PWM Configuration + std::map pwm_configs; + + // Timer Configuration + std::map timer_configs; + + // Resource Management + bool strict_resource_checking; + bool allow_resource_sharing; + std::chrono::milliseconds resource_timeout; +}; +``` + +## 10. Performance Characteristics + +### 10.1 Timing Requirements +- **I2C Operations**: Standard (100kHz), Fast (400kHz), Fast+ (1MHz) +- **SPI Operations**: Up to 80MHz clock speed +- **UART Operations**: Up to 5Mbps baud rate +- **GPIO Operations**: < 1μs for digital I/O operations +- **ADC Operations**: Up to 83.3kSPS sample rate + +### 10.2 Resource Usage +- **Memory**: < 16KB for HAL data structures and buffers +- **CPU**: Hardware-accelerated operations minimize CPU usage +- **Power**: Optimized for low-power operation with sleep modes + +## 11. Testing Strategy + +### 11.1 Unit Tests +- Individual peripheral interface functionality +- Resource management correctness +- Configuration validation +- Error handling and recovery + +### 11.2 Integration Tests +- Multi-peripheral coordination +- Resource sharing scenarios +- Hardware fault injection +- Performance benchmarking + +### 11.3 Hardware-in-the-Loop Tests +- Real hardware peripheral testing +- Signal integrity validation +- Timing accuracy verification +- Power consumption measurement + +## 12. Dependencies + +### 12.1 Internal Dependencies +- Logger for hardware operation logging +- Error Handler for hardware fault reporting +- Configuration Manager for hardware settings + +### 12.2 External Dependencies +- ESP-IDF hardware driver APIs +- FreeRTOS for interrupt handling and synchronization +- Hardware peripheral specifications +- ESP32-S3 technical reference manual + +## 13. Constraints and Assumptions + +### 13.1 Constraints +- Limited to ESP32-S3 hardware capabilities +- Resource sharing limited by hardware design +- Performance limited by hardware specifications +- Power consumption constraints for battery operation + +### 13.2 Assumptions +- Proper hardware design and connections +- Adequate power supply for all peripherals +- Proper signal integrity and noise immunity +- Hardware components within operating specifications + +--- + +**Document Status:** Final for Implementation +**Dependencies:** ESP-IDF Hardware Drivers, Logger, Error Handler, Configuration Manager +**Next Review:** After component implementation and hardware validation \ No newline at end of file diff --git a/1 software design/components/logger/COMPONENT.md b/1 software design/components/logger/COMPONENT.md new file mode 100644 index 0000000..4056787 --- /dev/null +++ b/1 software design/components/logger/COMPONENT.md @@ -0,0 +1,623 @@ +# Logger Component Specification + +**Component ID:** C-LOGGER-001 +**Component Name:** Logger +**Version:** 1.0 +**Date:** 2025-02-01 +**Location:** `application_layer/utils/logger/` + +## 1. Component Overview + +### 1.1 Purpose +The Logger component provides centralized logging infrastructure for the ASF Sensor Hub, enabling comprehensive system diagnostics, debugging, and operational monitoring. It supports multiple log levels, output destinations, and efficient log management with minimal performance impact. + +### 1.2 Scope +- Multi-level logging system (DEBUG, INFO, WARN, ERROR, FATAL) +- Multiple output destinations (UART, file, network, memory buffer) +- Log formatting and timestamping +- Log rotation and storage management +- Runtime log level configuration +- Thread-safe logging operations + +### 1.3 Responsibilities +- Provide unified logging interface for all system components +- Format and timestamp log messages +- Route log messages to appropriate output destinations +- Manage log file rotation and storage limits +- Support runtime log level configuration +- Ensure thread-safe logging operations + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Logger" + LC[Log Controller] + LF[Log Formatter] + LR[Log Router] + LM[Log Manager] + LB[Log Buffer] + end + + subgraph "Output Destinations" + UART[UART Output] + FILE[File Output] + NET[Network Output] + MEM[Memory Buffer] + end + + subgraph "External Components" + COMPS[System Components] + FS[File System] + CM[Communication Manager] + TU[Time Utils] + end + + LC --> LF + LC --> LR + LC --> LM + LC --> LB + + LR --> UART + LR --> FILE + LR --> NET + LR --> MEM + + COMPS --> LC + FILE --> FS + NET --> CM + LF --> TU +``` + +### 2.2 Internal Components + +#### 2.2.1 Log Controller +- Central coordination of logging operations +- Log level filtering and management +- Component lifecycle and configuration + +#### 2.2.2 Log Formatter +- Log message formatting and structuring +- Timestamp generation and formatting +- Message metadata enrichment + +#### 2.2.3 Log Router +- Route log messages to appropriate destinations +- Output destination management +- Load balancing and failover + +#### 2.2.4 Log Manager +- Log file rotation and cleanup +- Storage space management +- Log retention policy enforcement + +#### 2.2.5 Log Buffer +- In-memory log buffering for performance +- Asynchronous log processing +- Buffer overflow handling + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 ILogger +```cpp +class ILogger { +public: + virtual ~ILogger() = default; + + // Logging Methods + virtual void debug(const std::string& message, const std::string& component = "") = 0; + virtual void info(const std::string& message, const std::string& component = "") = 0; + virtual void warn(const std::string& message, const std::string& component = "") = 0; + virtual void error(const std::string& message, const std::string& component = "") = 0; + virtual void fatal(const std::string& message, const std::string& component = "") = 0; + + // Formatted Logging + virtual void logf(LogLevel level, const std::string& component, + const char* format, ...) = 0; + virtual void log(LogLevel level, const std::string& component, + const std::string& message) = 0; + + // Structured Logging + virtual void logStructured(LogLevel level, const std::string& component, + const std::string& message, + const std::map& metadata) = 0; + + // Configuration + virtual Result setLogLevel(LogLevel level) = 0; + virtual LogLevel getLogLevel() const = 0; + virtual Result setComponentLogLevel(const std::string& component, LogLevel level) = 0; + virtual LogLevel getComponentLogLevel(const std::string& component) const = 0; + + // Output Management + virtual Result addOutput(std::unique_ptr output) = 0; + virtual Result removeOutput(const std::string& output_name) = 0; + virtual std::vector getActiveOutputs() const = 0; + + // System Management + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual Result flush() = 0; + virtual LogStatistics getStatistics() const = 0; +}; +``` + +#### 3.1.2 ILogOutput +```cpp +class ILogOutput { +public: + virtual ~ILogOutput() = default; + virtual Result writeLog(const LogEntry& entry) = 0; + virtual Result flush() = 0; + virtual std::string getName() const = 0; + virtual bool isAvailable() const = 0; + virtual LogOutputStatistics getStatistics() const = 0; +}; +``` + +#### 3.1.3 ILogFormatter +```cpp +class ILogFormatter { +public: + virtual ~ILogFormatter() = default; + virtual std::string formatLogEntry(const LogEntry& entry) const = 0; + virtual Result setFormat(const std::string& format_string) = 0; + virtual std::string getFormat() const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 ITimeUtils +- Timestamp generation for log entries +- Time formatting for log messages +- Duration tracking for performance logging + +#### 3.2.2 IFileSystem +- Log file creation and management +- File rotation and cleanup operations +- Storage space monitoring + +#### 3.2.3 ICommunicationManager +- Network log transmission +- Remote logging capabilities +- Log aggregation support + +#### 3.2.4 IConfigurationManager +- Log level configuration +- Output destination configuration +- Log format settings + +## 4. Dynamic View + +### 4.1 Log Message Processing Sequence + +```mermaid +sequenceDiagram + participant COMP as System Component + participant LC as Log Controller + participant LF as Log Formatter + participant LR as Log Router + participant OUT as Log Output + participant TU as Time Utils + + COMP->>LC: info("Sensor data acquired", "SensorManager") + LC->>LC: checkLogLevel(INFO, "SensorManager") + + alt Log Level Enabled + LC->>TU: getCurrentTimestamp() + TU-->>LC: timestamp + LC->>LF: formatLogEntry(entry) + LF->>LF: applyFormat(entry, timestamp) + LF-->>LC: formatted_message + LC->>LR: routeLog(formatted_message) + + par Parallel Output + LR->>OUT: writeLog(formatted_message) + OUT-->>LR: write_result + end + + LC->>LC: updateStatistics() + else Log Level Disabled + LC->>LC: discardMessage() + end +``` + +### 4.2 Log Rotation Sequence + +```mermaid +sequenceDiagram + participant LM as Log Manager + participant FS as File System + participant LC as Log Controller + + LM->>FS: checkLogFileSize() + FS-->>LM: current_size + + alt Size Limit Exceeded + LM->>LC: pauseLogging() + LM->>FS: rotateLogFile() + FS->>FS: renameCurrentLog() + FS->>FS: createNewLogFile() + FS->>FS: deleteOldLogs() + FS-->>LM: rotation_complete + LM->>LC: resumeLogging() + end +``` + +### 4.3 Asynchronous Logging Sequence + +```mermaid +sequenceDiagram + participant COMP as Component + participant LB as Log Buffer + participant LC as Log Controller + participant OUT as Output + + COMP->>LB: log(message) + LB->>LB: addToBuffer(message) + LB-->>COMP: buffered (non-blocking) + + Note over LB,OUT: Background Processing + + loop Buffer Processing + LB->>LC: getNextLogEntry() + LC->>OUT: writeLog(entry) + OUT-->>LC: write_complete + LB->>LB: removeFromBuffer() + end +``` + +## 5. Log Levels and Filtering + +### 5.1 Log Levels +```cpp +enum class LogLevel { + DEBUG = 0, // Detailed debugging information + INFO = 1, // General information messages + WARN = 2, // Warning conditions + ERROR = 3, // Error conditions + FATAL = 4, // Fatal error conditions + OFF = 5 // Logging disabled +}; +``` + +### 5.2 Log Level Management +```cpp +class LogLevelManager { +public: + void setGlobalLogLevel(LogLevel level); + LogLevel getGlobalLogLevel() const; + void setComponentLogLevel(const std::string& component, LogLevel level); + LogLevel getComponentLogLevel(const std::string& component) const; + bool isLogLevelEnabled(LogLevel level, const std::string& component) const; + +private: + LogLevel global_level_; + std::map component_levels_; +}; +``` + +### 5.3 Runtime Configuration +- **Global Log Level**: System-wide minimum log level +- **Component-Specific Levels**: Per-component log level overrides +- **Dynamic Adjustment**: Runtime log level changes without restart +- **Configuration Persistence**: Log level settings saved to configuration + +## 6. Log Formatting + +### 6.1 Log Entry Structure +```cpp +struct LogEntry { + LogLevel level; + std::string component; + std::string message; + uint64_t timestamp_ms; + uint32_t thread_id; + std::string file_name; + uint32_t line_number; + std::map metadata; + uint64_t sequence_number; +}; +``` + +### 6.2 Format Templates +```cpp +namespace LogFormats { + // Standard format: [TIMESTAMP] [LEVEL] [COMPONENT] MESSAGE + constexpr const char* STANDARD = "[%t] [%l] [%c] %m"; + + // Detailed format: [TIMESTAMP] [LEVEL] [COMPONENT] [FILE:LINE] MESSAGE + constexpr const char* DETAILED = "[%t] [%l] [%c] [%f:%n] %m"; + + // Compact format: %t %l %c %m + constexpr const char* COMPACT = "%t %l %c %m"; + + // JSON format for structured logging + constexpr const char* JSON = "{\"timestamp\":\"%t\",\"level\":\"%l\",\"component\":\"%c\",\"message\":\"%m\"}"; +} +``` + +### 6.3 Format Specifiers +- `%t` - Timestamp +- `%l` - Log level +- `%c` - Component name +- `%m` - Message +- `%f` - File name +- `%n` - Line number +- `%i` - Thread ID +- `%s` - Sequence number + +## 7. Output Destinations + +### 7.1 UART Output +```cpp +class UARTLogOutput : public ILogOutput { +public: + UARTLogOutput(UARTPort port, uint32_t baud_rate); + Result writeLog(const LogEntry& entry) override; + Result flush() override; + bool isAvailable() const override; + +private: + UARTPort uart_port_; + uint32_t baud_rate_; + bool is_initialized_; +}; +``` + +### 7.2 File Output +```cpp +class FileLogOutput : public ILogOutput { +public: + FileLogOutput(const std::string& file_path, size_t max_file_size); + Result writeLog(const LogEntry& entry) override; + Result flush() override; + Result rotateFile(); + +private: + std::string file_path_; + size_t max_file_size_; + size_t current_file_size_; + std::unique_ptr file_stream_; +}; +``` + +### 7.3 Network Output +```cpp +class NetworkLogOutput : public ILogOutput { +public: + NetworkLogOutput(const std::string& server_url, uint16_t port); + Result writeLog(const LogEntry& entry) override; + Result flush() override; + +private: + std::string server_url_; + uint16_t port_; + std::queue pending_logs_; +}; +``` + +### 7.4 Memory Buffer Output +```cpp +class MemoryLogOutput : public ILogOutput { +public: + MemoryLogOutput(size_t buffer_size); + Result writeLog(const LogEntry& entry) override; + std::vector getRecentLogs(size_t count) const; + void clearBuffer(); + +private: + std::deque log_buffer_; + size_t max_buffer_size_; + mutable std::mutex buffer_mutex_; +}; +``` + +## 8. Log Management + +### 8.1 Log Rotation +```cpp +struct LogRotationConfig { + size_t max_file_size; // Maximum size per log file + uint32_t max_file_count; // Maximum number of log files + std::chrono::hours max_age; // Maximum age of log files + bool compress_old_files; // Compress rotated files + std::string rotation_pattern; // File naming pattern +}; +``` + +### 8.2 Storage Management +- **Size-Based Rotation**: Rotate logs when file size exceeds limit +- **Time-Based Rotation**: Rotate logs based on time intervals +- **Count-Based Cleanup**: Maintain maximum number of log files +- **Compression**: Compress old log files to save space +- **Cleanup**: Automatic deletion of old log files + +### 8.3 Log Retention Policy +- **Retention Period**: Configurable log retention duration +- **Priority-Based Retention**: Keep ERROR/FATAL logs longer +- **Storage Limits**: Respect available storage space +- **Emergency Cleanup**: Free space when storage is full + +## 9. Performance Optimization + +### 9.1 Asynchronous Logging +```cpp +class AsyncLogger { +public: + void logAsync(const LogEntry& entry); + void flush(); + void setBufferSize(size_t size); + +private: + std::queue log_queue_; + std::thread worker_thread_; + std::condition_variable cv_; + std::mutex queue_mutex_; + bool shutdown_requested_; +}; +``` + +### 9.2 Performance Features +- **Lock-Free Queues**: Minimize contention in multi-threaded environment +- **Batch Processing**: Process multiple log entries together +- **Memory Pools**: Pre-allocated memory for log entries +- **Lazy Formatting**: Format messages only when needed +- **Compile-Time Filtering**: Remove debug logs in release builds + +### 9.3 Resource Management +- **Memory Limits**: Bounded memory usage for log buffers +- **CPU Throttling**: Limit CPU usage for log processing +- **I/O Optimization**: Batch file writes for efficiency +- **Network Buffering**: Buffer network logs to reduce overhead + +## 10. Configuration + +### 10.1 Logger Configuration +```cpp +struct LoggerConfig { + // Global Settings + LogLevel global_log_level; + std::map component_log_levels; + std::string log_format; + + // Output Configuration + std::vector outputs; + bool async_logging_enabled; + size_t async_buffer_size; + + // File Logging Configuration + std::string log_file_path; + LogRotationConfig rotation_config; + + // Network Logging Configuration + std::string log_server_url; + uint16_t log_server_port; + bool network_logging_enabled; + + // Performance Configuration + size_t max_message_length; + std::chrono::milliseconds flush_interval; + bool compile_time_filtering; +}; +``` + +## 11. Thread Safety + +### 11.1 Synchronization Strategy +- **Lock-Free Queues**: For high-performance logging +- **Mutex Protection**: For shared data structures +- **Thread-Local Storage**: For per-thread log contexts +- **Atomic Operations**: For counters and flags + +### 11.2 Concurrency Considerations +- **Multiple Writers**: Support concurrent logging from multiple threads +- **Reader-Writer Separation**: Separate read and write operations +- **Deadlock Prevention**: Careful lock ordering and timeout handling +- **Performance Impact**: Minimize synchronization overhead + +## 12. Error Handling + +### 12.1 Logging Errors +```cpp +enum class LogError { + OUTPUT_UNAVAILABLE, // Log output destination unavailable + BUFFER_FULL, // Log buffer overflow + FORMAT_ERROR, // Log format string error + FILE_WRITE_ERROR, // File write operation failed + NETWORK_ERROR, // Network transmission failed + CONFIGURATION_ERROR // Invalid configuration +}; +``` + +### 12.2 Error Recovery +- **Fallback Outputs**: Switch to alternative outputs on failure +- **Buffer Overflow**: Drop oldest messages when buffer is full +- **File Errors**: Create new files or switch to memory logging +- **Network Errors**: Queue messages for later transmission + +## 13. Performance Characteristics + +### 13.1 Timing Requirements +- **Log Entry Processing**: < 1ms for standard log entry +- **Asynchronous Logging**: < 100μs for queue insertion +- **File Write Operations**: Batched for efficiency +- **Network Transmission**: Asynchronous with buffering + +### 13.2 Resource Usage +- **Memory**: Configurable buffer sizes (default 64KB) +- **Storage**: Configurable with rotation and cleanup +- **CPU**: < 2% average utilization for logging operations +- **Network**: Minimal bandwidth for remote logging + +## 14. Security Considerations + +### 14.1 Log Security +- **Sensitive Data Filtering**: Prevent logging of sensitive information +- **Access Control**: Restrict access to log files and outputs +- **Log Integrity**: Protect logs from tampering +- **Secure Transmission**: Encrypt network log transmission + +### 14.2 Privacy Protection +- **Data Sanitization**: Remove or mask sensitive data +- **Retention Limits**: Automatic deletion of old logs +- **Access Logging**: Log access to log files +- **Audit Trail**: Maintain audit trail of log operations + +## 15. Testing Strategy + +### 15.1 Unit Tests +- Log level filtering correctness +- Message formatting accuracy +- Output destination functionality +- Configuration validation + +### 15.2 Integration Tests +- Multi-threaded logging scenarios +- Log rotation and cleanup +- Network logging functionality +- Performance under load + +### 15.3 Performance Tests +- Logging throughput measurement +- Memory usage validation +- CPU impact assessment +- Storage efficiency testing + +## 16. Dependencies + +### 16.1 Internal Dependencies +- Time Utils for timestamp generation +- File System for log file management +- Communication Manager for network logging +- Configuration Manager for settings + +### 16.2 External Dependencies +- ESP-IDF UART drivers +- File system support (SPIFFS/FAT) +- Network stack for remote logging +- Threading and synchronization primitives + +## 17. Constraints and Assumptions + +### 17.1 Constraints +- Memory usage must be bounded and configurable +- Logging must not significantly impact system performance +- Log files must respect storage space limitations +- Thread safety required for multi-threaded environment + +### 17.2 Assumptions +- Sufficient storage space for log files +- Network connectivity for remote logging (when enabled) +- Proper file system configuration +- Adequate system resources for logging operations + +--- + +**Document Status:** Final for Implementation +**Dependencies:** Time Utils, File System, Communication Manager, Configuration Manager +**Next Review:** After component implementation and performance validation \ No newline at end of file diff --git a/1 software design/components/machine_constants_manager/COMPONENT.md b/1 software design/components/machine_constants_manager/COMPONENT.md new file mode 100644 index 0000000..7072894 --- /dev/null +++ b/1 software design/components/machine_constants_manager/COMPONENT.md @@ -0,0 +1,540 @@ +# Machine Constants Manager Component Specification + +**Component ID:** C-MC-001 +**Component Name:** Machine Constants Manager +**Version:** 1.0 +**Date:** 2025-02-01 + +## 1. Component Overview + +### 1.1 Purpose +The Machine Constants Manager is responsible for managing static and semi-static configuration parameters for the Sensor Hub, including sensor configuration, calibration parameters, communication settings, and system identity information. It provides centralized configuration management with support for remote updates and controlled reinitialization. + +### 1.2 Scope +- Machine Constants dataset management +- Sensor configuration and calibration parameters +- Communication identifiers and settings +- System identity and addressing parameters +- Configuration validation and integrity checking +- Remote configuration updates with controlled application + +### 1.3 Responsibilities +- Load and validate Machine Constants during system initialization +- Provide configuration data to system components +- Support remote Machine Constants updates from Main Hub +- Implement controlled teardown and reinitialization for updates +- Maintain configuration version control and rollback capability +- Ensure configuration data integrity and consistency + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Machine Constants Manager" + MCM[MC Controller] + MCL[MC Loader] + MCV[MC Validator] + MCU[MC Updater] + MCS[MC Storage] + end + + subgraph "Configuration Data" + SC[Sensor Config] + CC[Communication Config] + CAL[Calibration Data] + SID[System Identity] + end + + subgraph "External Interfaces" + FS[File System] + CM[Communication Manager] + STM[System State Manager] + end + + MCM --> MCL + MCM --> MCV + MCM --> MCU + MCM --> MCS + + MCL --> SC + MCL --> CC + MCL --> CAL + MCL --> SID + + MCS --> FS + MCU --> CM + MCU --> STM +``` + +### 2.2 Internal Components + +#### 2.2.1 MC Controller +- Central coordination of Machine Constants operations +- Configuration access control and policy enforcement +- Component lifecycle management + +#### 2.2.2 MC Loader +- Configuration file loading and parsing +- Schema validation and compatibility checking +- Default configuration handling + +#### 2.2.3 MC Validator +- Configuration data validation and consistency checking +- Cross-reference validation between configuration sections +- Range and format validation + +#### 2.2.4 MC Updater +- Remote configuration update handling +- Update validation and staging +- Controlled application of configuration changes + +#### 2.2.5 MC Storage +- Configuration persistence and retrieval +- Backup and rollback management +- Configuration versioning + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 IMachineConstantsManager +```cpp +class IMachineConstantsManager { +public: + virtual ~IMachineConstantsManager() = default; + + // Initialization and Lifecycle + virtual Result initialize() = 0; + virtual Result reload() = 0; + virtual Result shutdown() = 0; + + // Configuration Access + virtual Result getSensorConfig(SensorId sensor_id) const = 0; + virtual Result getCommunicationConfig() const = 0; + virtual Result getCalibrationData(SensorId sensor_id) const = 0; + virtual Result getSystemIdentity() const = 0; + + // Configuration Queries + virtual std::vector getConfiguredSensors() const = 0; + virtual bool isSensorConfigured(SensorId sensor_id) const = 0; + virtual ConfigurationVersion getConfigurationVersion() const = 0; + virtual ConfigurationSummary getConfigurationSummary() const = 0; + + // Configuration Updates + virtual Result receiveConfigurationUpdate(const ConfigurationUpdate& update) = 0; + virtual Result validateConfigurationUpdate(const ConfigurationUpdate& update) const = 0; + virtual Result applyConfigurationUpdate() = 0; + virtual Result rollbackConfiguration() = 0; + + // Configuration Management + virtual Result backupConfiguration() = 0; + virtual Result restoreConfiguration(ConfigurationVersion version) = 0; + virtual std::vector getAvailableVersions() const = 0; +}; +``` + +#### 3.1.2 ICalibrationProvider +```cpp +class ICalibrationProvider { +public: + virtual ~ICalibrationProvider() = default; + virtual Result getCalibrationData(SensorId sensor_id) const = 0; + virtual Result updateCalibrationData(SensorId sensor_id, + const CalibrationData& data) = 0; + virtual bool isCalibrationValid(SensorId sensor_id) const = 0; + virtual std::chrono::system_clock::time_point getCalibrationDate(SensorId sensor_id) const = 0; +}; +``` + +#### 3.1.3 IConfigurationManager +```cpp +class IConfigurationManager { +public: + virtual ~IConfigurationManager() = default; + virtual Result getConfigValue(const std::string& key) const = 0; + virtual Result setConfigValue(const std::string& key, const ConfigValue& value) = 0; + virtual bool hasConfigValue(const std::string& key) const = 0; + virtual std::vector getConfigKeys() const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 IPersistenceManager +- Configuration file storage and retrieval +- Backup and versioning support +- Data integrity verification + +#### 3.2.2 ICommunicationManager +- Reception of configuration updates from Main Hub +- Configuration update acknowledgment +- Update status reporting + +#### 3.2.3 ISystemStateManager +- System state management for configuration updates +- Controlled teardown initiation +- System reinitialization coordination + +#### 3.2.4 IDiagnosticsManager +- Configuration error reporting +- Update status logging +- Configuration validation results + +## 4. Dynamic View + +### 4.1 Configuration Loading Sequence + +```mermaid +sequenceDiagram + participant SYS as System Controller + participant MCM as MC Manager + participant MCL as MC Loader + participant MCV as MC Validator + participant FS as File System + + SYS->>MCM: initialize() + MCM->>MCL: loadConfiguration() + MCL->>FS: readConfigurationFile() + FS-->>MCL: configuration_data + MCL->>MCL: parseConfiguration() + MCL->>MCV: validateConfiguration(config) + MCV->>MCV: performValidation() + + alt Validation Success + MCV-->>MCL: validation_success + MCL-->>MCM: configuration_loaded + MCM-->>SYS: initialization_complete + else Validation Failed + MCV-->>MCL: validation_errors + MCL->>MCL: loadDefaultConfiguration() + MCL-->>MCM: default_configuration_loaded + MCM-->>SYS: initialization_with_defaults + end +``` + +### 4.2 Configuration Update Sequence + +```mermaid +sequenceDiagram + participant MH as Main Hub + participant CM as Communication Manager + participant MCM as MC Manager + participant MCU as MC Updater + participant STM as System State Manager + participant MCS as MC Storage + + MH->>CM: sendConfigurationUpdate(update_data) + CM->>MCM: receiveConfigurationUpdate(update_data) + MCM->>MCU: processUpdate(update_data) + MCU->>MCU: validateUpdate(update_data) + + alt Update Valid + MCU->>MCS: stageUpdate(update_data) + MCS-->>MCU: staging_complete + MCU->>STM: requestTeardown(MC_UPDATE) + STM->>STM: executeTeardown() + STM-->>MCU: teardown_complete + MCU->>MCS: applyUpdate() + MCS-->>MCU: update_applied + MCU->>STM: requestReinitialization() + STM->>STM: reinitializeSystem() + STM-->>MCU: reinitialization_complete + MCU->>CM: sendUpdateAcknowledgment(SUCCESS) + else Update Invalid + MCU->>CM: sendUpdateAcknowledgment(VALIDATION_FAILED) + end +``` + +### 4.3 Configuration Access Sequence + +```mermaid +sequenceDiagram + participant SM as Sensor Manager + participant MCM as MC Manager + participant SC as Sensor Config + participant CAL as Calibration Data + + SM->>MCM: getSensorConfig(TEMP_SENSOR_01) + MCM->>SC: retrieveConfig(TEMP_SENSOR_01) + SC-->>MCM: sensor_config + MCM-->>SM: configuration_data + + SM->>MCM: getCalibrationData(TEMP_SENSOR_01) + MCM->>CAL: retrieveCalibration(TEMP_SENSOR_01) + CAL-->>MCM: calibration_data + MCM-->>SM: calibration_parameters +``` + +## 5. Configuration Data Model + +### 5.1 Machine Constants Structure +```cpp +struct MachineConstants { + ConfigurationHeader header; + SystemIdentity system_identity; + CommunicationConfig communication; + std::map sensors; + std::map calibrations; + SystemParameters system_parameters; + SecurityConfig security; +}; +``` + +### 5.2 Configuration Header +```cpp +struct ConfigurationHeader { + std::string version; + std::chrono::system_clock::time_point created_timestamp; + std::chrono::system_clock::time_point modified_timestamp; + std::string created_by; + std::string description; + uint32_t checksum; + std::string schema_version; +}; +``` + +### 5.3 System Identity +```cpp +struct SystemIdentity { + std::string site_id; + std::string house_id; + std::string node_id; + std::string hardware_version; + std::string firmware_version; + std::string serial_number; + std::string installation_date; + std::string location_description; +}; +``` + +### 5.4 Sensor Configuration +```cpp +struct SensorConfig { + SensorId sensor_id; + SensorType sensor_type; + std::string sensor_model; + HardwareSlot hardware_slot; + CommunicationInterface interface_type; + std::map interface_parameters; + SamplingConfig sampling; + FilterConfig filtering; + ValidationConfig validation; + bool enabled; + std::string description; +}; +``` + +### 5.5 Calibration Data +```cpp +struct CalibrationData { + SensorId sensor_id; + CalibrationMethod method; + std::vector calibration_points; + PolynomialCoefficients coefficients; + TemperatureCompensation temperature_compensation; + std::chrono::system_clock::time_point calibration_date; + std::chrono::system_clock::time_point expiry_date; + std::string calibrated_by; + std::string certificate_number; + CalibrationAccuracy accuracy; +}; +``` + +### 5.6 Communication Configuration +```cpp +struct CommunicationConfig { + // MQTT Configuration + MQTTConfig mqtt; + + // ESP-NOW Configuration + ESPNOWConfig espnow; + + // Network Configuration + NetworkConfig network; + + // Security Configuration + SecurityConfig security; + + // Protocol Settings + ProtocolSettings protocols; +}; +``` + +## 6. Configuration Validation + +### 6.1 Validation Rules +```cpp +class ConfigurationValidator { +public: + struct ValidationResult { + bool is_valid; + std::vector errors; + std::vector warnings; + }; + + ValidationResult validateConfiguration(const MachineConstants& config) const; + ValidationResult validateSensorConfig(const SensorConfig& config) const; + ValidationResult validateCalibrationData(const CalibrationData& data) const; + ValidationResult validateCommunicationConfig(const CommunicationConfig& config) const; + +private: + bool validateSensorSlotMapping(const std::map& sensors) const; + bool validateCalibrationConsistency(const CalibrationData& data) const; + bool validateNetworkParameters(const NetworkConfig& config) const; + bool validateSecuritySettings(const SecurityConfig& config) const; +}; +``` + +### 6.2 Validation Categories +- **Schema Validation**: JSON schema compliance +- **Range Validation**: Parameter value ranges +- **Consistency Validation**: Cross-reference validation +- **Hardware Validation**: Hardware compatibility checking +- **Security Validation**: Security parameter validation + +## 7. Configuration Storage + +### 7.1 File Format +- **Primary Format**: JSON for human readability and editability +- **Schema Version**: Versioned schema for backward compatibility +- **Encoding**: UTF-8 with optional compression +- **Integrity**: CRC32 checksum for corruption detection + +### 7.2 Storage Locations +```cpp +namespace ConfigPaths { + constexpr const char* PRIMARY_CONFIG = "/config/machine_constants.json"; + constexpr const char* BACKUP_CONFIG = "/config/machine_constants.backup.json"; + constexpr const char* STAGING_CONFIG = "/config/machine_constants.staging.json"; + constexpr const char* DEFAULT_CONFIG = "/config/machine_constants.default.json"; + constexpr const char* VERSION_HISTORY = "/config/versions/"; +} +``` + +### 7.3 Version Management +- **Version Numbering**: Semantic versioning (major.minor.patch) +- **Version History**: Configurable retention of previous versions +- **Rollback Support**: Ability to revert to previous versions +- **Backup Strategy**: Automatic backup before updates + +## 8. Configuration Updates + +### 8.1 Update Types +```cpp +enum class UpdateType { + FULL_REPLACEMENT, // Complete configuration replacement + PARTIAL_UPDATE, // Update specific sections + SENSOR_ADDITION, // Add new sensor configuration + SENSOR_REMOVAL, // Remove sensor configuration + CALIBRATION_UPDATE, // Update calibration data only + COMMUNICATION_UPDATE // Update communication settings +}; +``` + +### 8.2 Update Process +1. **Reception**: Receive update from Main Hub +2. **Validation**: Validate update against current configuration +3. **Staging**: Store update in staging area +4. **Teardown**: Execute controlled system teardown +5. **Application**: Apply staged configuration +6. **Reinitialization**: Reinitialize system with new configuration +7. **Verification**: Verify successful application +8. **Acknowledgment**: Send update result to Main Hub + +### 8.3 Rollback Mechanism +- **Automatic Rollback**: On validation or application failure +- **Manual Rollback**: Via diagnostic session or remote command +- **Rollback Verification**: Ensure system stability after rollback +- **Rollback Reporting**: Report rollback events and reasons + +## 9. Error Handling + +### 9.1 Error Categories +- **File System Errors**: Configuration file access failures +- **Parsing Errors**: JSON parsing or schema validation failures +- **Validation Errors**: Configuration validation failures +- **Update Errors**: Configuration update process failures +- **Consistency Errors**: Configuration inconsistency detection + +### 9.2 Error Recovery Strategies +- **Default Configuration**: Fall back to default configuration +- **Previous Version**: Rollback to last known good configuration +- **Partial Recovery**: Use valid portions of configuration +- **Safe Mode**: Minimal configuration for basic operation + +## 10. Performance Characteristics + +### 10.1 Timing Requirements +- **Configuration Loading**: < 2 seconds during startup +- **Configuration Access**: < 1ms for cached values +- **Update Validation**: < 5 seconds for typical updates +- **Update Application**: < 30 seconds including teardown/restart + +### 10.2 Resource Usage +- **Memory**: < 8KB for configuration cache +- **Storage**: Configurable with version retention limits +- **CPU**: Minimal impact during normal operation + +## 11. Security Considerations + +### 11.1 Configuration Security +- **Access Control**: Restricted access to configuration files +- **Integrity Protection**: Checksums and digital signatures +- **Encryption**: Optional encryption for sensitive parameters +- **Audit Trail**: Logging of configuration changes + +### 11.2 Update Security +- **Authentication**: Verify update source authenticity +- **Authorization**: Validate update permissions +- **Integrity**: Verify update data integrity +- **Rollback Protection**: Prevent unauthorized rollbacks + +## 12. Testing Strategy + +### 12.1 Unit Tests +- Configuration loading and parsing +- Validation logic and error handling +- Update processing and application +- Rollback mechanisms + +### 12.2 Integration Tests +- End-to-end configuration update process +- System reinitialization with new configuration +- Cross-component configuration consistency +- Error recovery scenarios + +### 12.3 System Tests +- Configuration update under various system states +- Performance impact of configuration operations +- Stress testing with large configurations +- Security validation of update process + +## 13. Dependencies + +### 13.1 Internal Dependencies +- Persistence Manager for file operations +- System State Manager for teardown/restart coordination +- Communication Manager for remote updates +- Diagnostics Manager for error reporting + +### 13.2 External Dependencies +- JSON parsing library (e.g., nlohmann/json) +- File system support (SPIFFS, FAT) +- Cryptographic library for integrity verification +- Schema validation library + +## 14. Constraints and Assumptions + +### 14.1 Constraints +- Configuration size limited by available storage +- Update process requires system restart +- Configuration changes require controlled teardown +- Version history limited by storage capacity + +### 14.2 Assumptions +- Reliable storage medium for configuration persistence +- Sufficient system resources for configuration operations +- Valid default configuration available as fallback +- Proper authentication for configuration updates \ No newline at end of file diff --git a/1 software design/components/ota_manager/COMPONENT.md b/1 software design/components/ota_manager/COMPONENT.md new file mode 100644 index 0000000..a7fb7da --- /dev/null +++ b/1 software design/components/ota_manager/COMPONENT.md @@ -0,0 +1,618 @@ +# OTA Manager Component Specification + +**Component ID:** C-OTA-001 +**Component Name:** OTA Manager +**Version:** 1.0 +**Date:** 2025-02-01 + +## 1. Component Overview + +### 1.1 Purpose +The OTA Manager is responsible for managing over-the-air firmware updates with A/B partitioning and automatic rollback capabilities. It provides secure, reliable firmware update functionality while ensuring system availability and data integrity throughout the update process. + +### 1.2 Scope +- OTA update negotiation and readiness validation +- Secure firmware reception and temporary storage +- Firmware integrity validation and verification +- Safe firmware activation with controlled teardown +- A/B partitioning management with automatic rollback +- Update status reporting and error handling + +### 1.3 Responsibilities +- Negotiate OTA updates with Main Hub +- Validate system readiness for firmware updates +- Receive and store firmware images securely +- Verify firmware integrity using cryptographic methods +- Manage A/B partition switching and boot control +- Implement automatic rollback on update failures +- Coordinate with system teardown and reinitialization +- Report update status and results to Main Hub + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "OTA Manager" + OM[OTA Controller] + UN[Update Negotiator] + FR[Firmware Receiver] + IV[Integrity Validator] + FA[Firmware Activator] + RM[Rollback Manager] + PM[Partition Manager] + end + + subgraph "Storage Layer" + FS[Firmware Storage] + PS[Partition Service] + DP[Data Pool] + end + + subgraph "Security Layer" + SB[Secure Boot] + FE[Flash Encryption] + CV[Certificate Validator] + CM[Crypto Manager] + end + + OM --> UN + OM --> FR + OM --> IV + OM --> FA + OM --> RM + OM --> PM + + FR --> FS + IV --> CV + IV --> CM + FA --> PS + RM --> PM + FS --> DP + PS --> SB + PS --> FE +``` + +### 2.2 Internal Components + +#### 2.2.1 OTA Controller +- Central coordination of OTA operations +- State management for update process +- Error handling and recovery coordination + +#### 2.2.2 Update Negotiator +- OTA update negotiation with Main Hub +- System readiness validation +- Update acceptance/rejection logic + +#### 2.2.3 Firmware Receiver +- Secure firmware image reception +- Chunked download management +- Progress tracking and resume capability + +#### 2.2.4 Integrity Validator +- Firmware integrity verification +- Cryptographic signature validation +- Metadata and compatibility checking + +#### 2.2.5 Firmware Activator +- Controlled firmware activation process +- System teardown coordination +- Boot partition management + +#### 2.2.6 Rollback Manager +- Automatic rollback detection and execution +- Boot failure recovery +- Rollback status reporting + +#### 2.2.7 Partition Manager +- A/B partition management +- Boot partition selection +- Partition health monitoring + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 IOTAManager +```cpp +class IOTAManager { +public: + virtual ~IOTAManager() = default; + + // OTA Lifecycle Management + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual OTAState getCurrentState() const = 0; + + // Update Negotiation + virtual Result receiveUpdateNotification(const UpdateNotification& notification) = 0; + virtual Result validateSystemReadiness() const = 0; + virtual Result acceptUpdate(const UpdateAcceptance& acceptance) = 0; + virtual Result rejectUpdate(const UpdateRejection& rejection) = 0; + + // Firmware Reception + virtual Result startFirmwareDownload(const FirmwareMetadata& metadata) = 0; + virtual Result receiveFirmwareChunk(const FirmwareChunk& chunk) = 0; + virtual Result completeFirmwareDownload() = 0; + virtual DownloadProgress getDownloadProgress() const = 0; + + // Firmware Validation and Activation + virtual Result validateFirmware() = 0; + virtual Result activateFirmware() = 0; + virtual Result confirmFirmware() = 0; + + // Rollback Management + virtual Result rollbackFirmware() = 0; + virtual bool isRollbackAvailable() const = 0; + virtual RollbackInfo getRollbackInfo() const = 0; + + // Status and Information + virtual OTAStatus getOTAStatus() const = 0; + virtual FirmwareInfo getCurrentFirmwareInfo() const = 0; + virtual FirmwareInfo getPendingFirmwareInfo() const = 0; + virtual std::vector getOTAHistory() const = 0; +}; +``` + +#### 3.1.2 IUpdateController +```cpp +class IUpdateController { +public: + virtual ~IUpdateController() = default; + virtual Result initiateUpdate(const UpdateRequest& request) = 0; + virtual Result cancelUpdate() = 0; + virtual Result pauseUpdate() = 0; + virtual Result resumeUpdate() = 0; + virtual UpdateStatus getUpdateStatus() const = 0; +}; +``` + +#### 3.1.3 IFirmwareStatus +```cpp +class IFirmwareStatus { +public: + virtual ~IFirmwareStatus() = default; + virtual FirmwareVersion getCurrentVersion() const = 0; + virtual FirmwareVersion getPendingVersion() const = 0; + virtual PartitionInfo getActivePartition() const = 0; + virtual PartitionInfo getInactivePartition() const = 0; + virtual bool isPendingFirmwareValid() const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 ICommunicationManager +- Reception of OTA notifications and firmware data +- Transmission of OTA status and acknowledgments +- Secure communication channel management + +#### 3.2.2 ISystemStateManager +- System state management for OTA operations +- Controlled teardown initiation +- System reinitialization coordination + +#### 3.2.3 IPersistenceManager +- Firmware image storage and retrieval +- OTA metadata persistence +- Backup and recovery data management + +#### 3.2.4 ISecurityManager +- Firmware signature verification +- Certificate validation +- Cryptographic operations + +#### 3.2.5 IDiagnosticsManager +- OTA event logging and reporting +- Error condition reporting +- Performance metrics collection + +## 4. Dynamic View + +### 4.1 OTA Negotiation Sequence + +```mermaid +sequenceDiagram + participant MH as Main Hub + participant CM as Communication Manager + participant OM as OTA Manager + participant UN as Update Negotiator + participant STM as System State Manager + + MH->>CM: otaUpdateAvailable(firmware_info) + CM->>OM: receiveUpdateNotification(firmware_info) + OM->>UN: negotiateUpdate(firmware_info) + UN->>STM: checkSystemReadiness() + STM-->>UN: system_status + UN->>UN: validateUpdateCompatibility() + + alt System Ready and Compatible + UN-->>OM: negotiation_success + OM->>STM: transitionToOTAState() + STM-->>OM: state_transition_complete + OM->>CM: sendOTAAcceptance() + CM->>MH: otaAccepted() + else System Not Ready or Incompatible + UN-->>OM: negotiation_failed(reason) + OM->>CM: sendOTARejection(reason) + CM->>MH: otaRejected(reason) + end +``` + +### 4.2 Firmware Download and Validation Sequence + +```mermaid +sequenceDiagram + participant MH as Main Hub + participant FR as Firmware Receiver + participant FS as Firmware Storage + participant IV as Integrity Validator + participant CV as Certificate Validator + participant CM as Crypto Manager + + MH->>FR: startFirmwareTransfer(metadata) + FR->>FS: initializeFirmwareStorage(metadata) + FS-->>FR: storage_initialized + + loop Firmware Chunks + MH->>FR: sendFirmwareChunk(chunk_data, chunk_id) + FR->>FS: storeFirmwareChunk(chunk_data, chunk_id) + FS-->>FR: chunk_stored + FR->>FR: updateProgress() + end + + FR->>IV: validateFirmwareIntegrity() + IV->>FS: calculateSHA256Hash() + FS-->>IV: calculated_hash + IV->>IV: compareWithExpectedHash() + + alt Hash Valid + IV->>CV: validateFirmwareSignature() + CV->>CM: verifySignature(firmware, signature) + CM-->>CV: signature_valid + + alt Signature Valid + CV-->>IV: signature_validation_success + IV-->>FR: firmware_validated + else Signature Invalid + CV-->>IV: signature_validation_failed + IV-->>FR: validation_failed + end + else Hash Invalid + IV-->>FR: integrity_check_failed + end +``` + +### 4.3 Firmware Activation and Rollback Sequence + +```mermaid +sequenceDiagram + participant OM as OTA Manager + participant FA as Firmware Activator + participant PM as Partition Manager + participant STM as System State Manager + participant RM as Rollback Manager + participant SB as Secure Boot + + OM->>FA: activateFirmware() + FA->>STM: initiateTeardown() + STM->>STM: executeTeardown() + STM-->>FA: teardown_complete + + FA->>PM: flashFirmwareToInactivePartition() + PM->>PM: writeFirmwareToPartition() + PM-->>FA: flash_complete + + FA->>PM: setBootPartition(inactive_partition) + PM->>PM: updatePartitionTable() + PM-->>FA: boot_partition_set + + FA->>FA: rebootSystem() + + Note over FA: System Reboots + + alt Boot Success + SB->>SB: validateNewFirmware() + SB->>FA: firmwareBootSuccessful() + FA->>FA: startConfirmationTimer() + FA->>OM: sendHealthReport() + + alt Confirmation Received + OM->>RM: confirmFirmwareStability() + RM->>PM: markPartitionValid() + else Confirmation Timeout + RM->>PM: rollbackToPreviousPartition() + PM->>PM: setBootPartition(previous_partition) + PM->>PM: rebootSystem() + end + else Boot Failure + SB->>RM: bootFailureDetected() + RM->>PM: rollbackToPreviousPartition() + PM->>PM: setBootPartition(previous_partition) + PM->>PM: rebootSystem() + end +``` + +## 5. OTA State Management + +### 5.1 OTA States +```cpp +enum class OTAState { + IDLE, // No OTA operation in progress + NEGOTIATING, // Negotiating update with Main Hub + DOWNLOADING, // Downloading firmware image + VALIDATING, // Validating firmware integrity + READY_TO_ACTIVATE, // Ready for firmware activation + ACTIVATING, // Activating new firmware + CONFIRMING, // Confirming firmware stability + ROLLING_BACK, // Rolling back to previous firmware + COMPLETED, // OTA operation completed successfully + FAILED, // OTA operation failed + CANCELLED // OTA operation cancelled +}; +``` + +### 5.2 State Transitions +```cpp +class OTAStateMachine { +public: + bool isValidTransition(OTAState from, OTAState to) const; + Result transitionTo(OTAState new_state); + OTAState getCurrentState() const; + std::vector getValidNextStates() const; + +private: + OTAState current_state_; + std::map> valid_transitions_; +}; +``` + +## 6. A/B Partitioning + +### 6.1 Partition Layout +```cpp +struct PartitionLayout { + // Boot partitions + Partition bootloader; // ~32KB - Initial boot code + Partition partition_table; // ~4KB - Partition table + Partition factory; // Optional - Factory/rescue firmware + + // Application partitions (A/B) + Partition ota_0; // 3.5MB - Primary application slot + Partition ota_1; // 3.5MB - Secondary application slot + + // Data partitions + Partition nvs; // 64KB - Non-volatile storage + Partition phy_init; // ~4KB - PHY initialization data + Partition coredump; // 64KB - Core dump storage +}; +``` + +### 6.2 Partition Management +```cpp +class PartitionManager { +public: + Result initialize(); + + // Partition Information + PartitionInfo getActivePartition() const; + PartitionInfo getInactivePartition() const; + std::vector getAllPartitions() const; + + // Partition Operations + Result setBootPartition(PartitionId partition_id); + Result markPartitionValid(PartitionId partition_id); + Result markPartitionInvalid(PartitionId partition_id); + + // Firmware Operations + Result writeFirmwareToPartition(PartitionId partition_id, + const FirmwareImage& firmware); + Result eraseFirmwarePartition(PartitionId partition_id); + Result validatePartitionFirmware(PartitionId partition_id); + + // Rollback Support + Result rollbackToPartition(PartitionId partition_id); + bool isRollbackAvailable() const; + PartitionId getPreviousValidPartition() const; +}; +``` + +## 7. Firmware Validation + +### 7.1 Validation Methods +```cpp +class FirmwareValidator { +public: + struct ValidationResult { + bool is_valid; + std::vector errors; + ValidationMetrics metrics; + }; + + // Integrity Validation + ValidationResult validateIntegrity(const FirmwareImage& firmware) const; + ValidationResult validateSHA256Hash(const FirmwareImage& firmware, + const std::string& expected_hash) const; + + // Signature Validation + ValidationResult validateSignature(const FirmwareImage& firmware, + const DigitalSignature& signature) const; + ValidationResult validateCertificateChain(const CertificateChain& chain) const; + + // Compatibility Validation + ValidationResult validateCompatibility(const FirmwareMetadata& metadata) const; + ValidationResult validateHardwareCompatibility(const HardwareRequirements& requirements) const; + ValidationResult validateVersionCompatibility(const VersionInfo& version) const; + + // Metadata Validation + ValidationResult validateMetadata(const FirmwareMetadata& metadata) const; + ValidationResult validateSize(const FirmwareImage& firmware, size_t expected_size) const; +}; +``` + +### 7.2 Security Validation +- **SHA-256 Hash**: Firmware integrity verification +- **RSA-3072/ECDSA-P256**: Digital signature verification +- **Certificate Chain**: Certificate validation and trust verification +- **Anti-Rollback**: Version downgrade protection +- **Secure Boot**: Boot-time firmware validation + +## 8. Rollback Management + +### 8.1 Rollback Triggers +```cpp +enum class RollbackTrigger { + BOOT_FAILURE, // System failed to boot with new firmware + HEALTH_CHECK_TIMEOUT, // No health report within confirmation window + APPLICATION_CRASH, // Application crash during confirmation period + MANUAL_ROLLBACK, // Manual rollback command from Main Hub + VALIDATION_FAILURE, // Post-boot validation failure + WATCHDOG_TIMEOUT // Watchdog timeout indicating system hang +}; +``` + +### 8.2 Rollback Process +1. **Failure Detection**: Detect rollback trigger condition +2. **Rollback Decision**: Determine if rollback is appropriate +3. **Partition Switch**: Switch boot partition to previous valid firmware +4. **System Reboot**: Reboot into previous firmware +5. **Rollback Verification**: Verify successful rollback +6. **Status Reporting**: Report rollback event and reason + +### 8.3 Rollback Prevention +- **Confirmation Window**: 60-120 seconds for firmware confirmation +- **Health Monitoring**: Continuous health checks during confirmation +- **Graceful Degradation**: Attempt recovery before rollback +- **Rollback Limits**: Prevent infinite rollback loops + +## 9. Configuration + +### 9.1 OTA Configuration +```cpp +struct OTAConfig { + // Download Configuration + size_t chunk_size; // Default: 4096 bytes + std::chrono::seconds download_timeout; + uint32_t max_retry_attempts; + + // Validation Configuration + bool signature_validation_required; + bool certificate_validation_required; + std::vector trusted_ca_certificates; + + // Activation Configuration + std::chrono::seconds confirmation_timeout; // Default: 90 seconds + bool automatic_rollback_enabled; + uint32_t max_rollback_attempts; + + // Storage Configuration + std::string firmware_storage_path; + size_t max_firmware_size; + bool cleanup_after_activation; + + // Security Configuration + bool secure_download_required; + EncryptionMethod storage_encryption; + bool anti_rollback_enabled; +}; +``` + +## 10. Error Handling + +### 10.1 Error Categories +- **Network Errors**: Download failures, communication timeouts +- **Storage Errors**: Insufficient space, write failures +- **Validation Errors**: Integrity check failures, signature validation failures +- **Activation Errors**: Partition switching failures, boot failures +- **Rollback Errors**: Rollback process failures + +### 10.2 Error Recovery Strategies +- **Retry Logic**: Automatic retry with exponential backoff +- **Graceful Degradation**: Continue operation with current firmware +- **Automatic Rollback**: Rollback on activation failures +- **Error Reporting**: Detailed error reporting to Main Hub +- **Diagnostic Logging**: Comprehensive logging for troubleshooting + +## 11. Performance Characteristics + +### 11.1 Timing Requirements +- **OTA Negotiation**: < 30 seconds +- **Firmware Download**: Variable based on size and network speed +- **Firmware Validation**: < 60 seconds for typical firmware +- **Firmware Activation**: < 2 minutes including teardown and reboot +- **Total OTA Process**: < 10 minutes end-to-end + +### 11.2 Resource Usage +- **Memory**: < 64KB for OTA operations and buffers +- **Storage**: Temporary storage equal to firmware size +- **CPU**: < 10% during download, higher during validation +- **Network**: Adaptive based on available bandwidth + +## 12. Security Considerations + +### 12.1 Secure Download +- All firmware downloads over encrypted channels (TLS 1.2) +- Firmware integrity verification during download +- Secure temporary storage with encryption + +### 12.2 Firmware Validation +- Mandatory cryptographic signature verification +- Certificate chain validation with trusted root CAs +- Anti-rollback protection via eFuse version tracking + +### 12.3 Activation Security +- Secure Boot V2 validation of new firmware +- Encrypted partition storage +- Rollback protection against unauthorized downgrades + +## 13. Testing Strategy + +### 13.1 Unit Tests +- OTA state machine transitions +- Firmware validation algorithms +- Partition management operations +- Error handling and recovery logic + +### 13.2 Integration Tests +- End-to-end OTA update process +- Communication with Main Hub +- System teardown and reinitialization +- Rollback scenarios and recovery + +### 13.3 System Tests +- OTA updates under various system conditions +- Network interruption and recovery +- Power failure during OTA process +- Security validation and attack resistance + +## 14. Dependencies + +### 14.1 Internal Dependencies +- Communication Manager for OTA communication +- System State Manager for teardown coordination +- Persistence Manager for firmware storage +- Security Manager for cryptographic operations +- Diagnostics Manager for event logging + +### 14.2 External Dependencies +- ESP-IDF OTA APIs and partition management +- mbedTLS for cryptographic operations +- File system support for firmware storage +- Secure Boot and Flash Encryption hardware + +## 15. Constraints and Assumptions + +### 15.1 Constraints +- OTA process requires system restart +- Firmware size limited by partition size (3.5MB) +- Network connectivity required for firmware download +- Sufficient storage space required for temporary firmware storage + +### 15.2 Assumptions +- Reliable network connectivity during download +- Valid firmware signatures and certificates +- Proper A/B partition configuration +- Sufficient system resources for OTA operations +- Main Hub provides valid firmware images and metadata \ No newline at end of file diff --git a/1 software design/components/security_manager/COMPONENT.md b/1 software design/components/security_manager/COMPONENT.md new file mode 100644 index 0000000..bf2dfbb --- /dev/null +++ b/1 software design/components/security_manager/COMPONENT.md @@ -0,0 +1,682 @@ +# Security Manager Component Specification + +**Component ID:** C-SEC-001 +**Component Name:** Security Manager +**Version:** 1.0 +**Date:** 2025-02-01 + +## 1. Component Overview + +### 1.1 Purpose +The Security Manager is responsible for implementing comprehensive security mechanisms to protect the system against unauthorized access, malicious firmware, and data breaches. It provides centralized security policy enforcement, cryptographic operations, and security violation handling across all system components. + +### 1.2 Scope +- Secure boot process management and enforcement +- Flash encryption and secure storage management +- Communication security with TLS/mTLS implementation +- Cryptographic key management and protection +- Security violation detection and response +- Access control and authentication services +- Security policy enforcement and compliance + +### 1.3 Responsibilities +- Enforce secure boot verification for all firmware +- Manage flash encryption and secure storage access +- Provide cryptographic services for communication security +- Implement key management and certificate handling +- Detect and respond to security violations +- Maintain security policies and access control +- Coordinate security operations across system components + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Security Manager" + SM[Security Controller] + SBC[Secure Boot Controller] + FEM[Flash Encryption Manager] + TM[TLS Manager] + KM[Key Manager] + SVH[Security Violation Handler] + ACM[Access Control Manager] + end + + subgraph "Cryptographic Services" + CM[Crypto Manager] + CA[Certificate Authority] + RNG[Random Number Generator] + HSM[Hardware Security Module] + end + + subgraph "Hardware Security" + EFUSE[eFuse Manager] + SBH[Secure Boot Hardware] + FEH[Flash Encryption Hardware] + HWRNG[Hardware RNG] + end + + SM --> SBC + SM --> FEM + SM --> TM + SM --> KM + SM --> SVH + SM --> ACM + + SBC --> SBH + FEM --> FEH + TM --> CM + TM --> CA + KM --> HSM + KM --> EFUSE + CM --> RNG + RNG --> HWRNG +``` + +### 2.2 Internal Components + +#### 2.2.1 Security Controller +- Central coordination of security operations +- Security policy enforcement and compliance +- Security state management and monitoring + +#### 2.2.2 Secure Boot Controller +- Secure boot process management +- Firmware signature verification +- Boot failure handling and recovery + +#### 2.2.3 Flash Encryption Manager +- Flash encryption configuration and management +- Secure storage access control +- Encryption key derivation and management + +#### 2.2.4 TLS Manager +- TLS/mTLS session management +- Certificate validation and handling +- Secure communication channel establishment + +#### 2.2.5 Key Manager +- Cryptographic key lifecycle management +- Key storage and protection +- Key derivation and rotation + +#### 2.2.6 Security Violation Handler +- Security violation detection and classification +- Incident response and recovery +- Security event logging and reporting + +#### 2.2.7 Access Control Manager +- Authentication and authorization services +- Role-based access control +- Session management and validation + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 ISecurityManager +```cpp +class ISecurityManager { +public: + virtual ~ISecurityManager() = default; + + // Security Lifecycle + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual SecurityStatus getSecurityStatus() const = 0; + + // Secure Boot Management + virtual Result validateFirmwareSignature(const FirmwareImage& firmware) = 0; + virtual Result enableSecureBoot() = 0; + virtual bool isSecureBootEnabled() const = 0; + virtual SecureBootStatus getSecureBootStatus() const = 0; + + // Flash Encryption Management + virtual Result enableFlashEncryption() = 0; + virtual bool isFlashEncryptionEnabled() const = 0; + virtual Result encryptFlashRegion(FlashRegion region) = 0; + virtual Result decryptFlashRegion(FlashRegion region) = 0; + + // Communication Security + virtual Result establishSecureConnection(const ConnectionParams& params) = 0; + virtual Result validateCertificate(const Certificate& certificate) = 0; + virtual Result terminateSecureConnection(TLSSessionId session_id) = 0; + + // Key Management + virtual Result generateKey(KeyType type, KeyUsage usage) = 0; + virtual Result storeKey(KeyHandle handle, const SecureStorage& storage) = 0; + virtual Result deleteKey(KeyHandle handle) = 0; + virtual Result rotateKey(KeyHandle handle) = 0; + + // Access Control + virtual Result authenticateUser(const Credentials& credentials) = 0; + virtual Result authorizeOperation(SessionId session, Operation operation) = 0; + virtual Result terminateSession(SessionId session) = 0; + + // Security Violations + virtual Result reportSecurityViolation(const SecurityViolation& violation) = 0; + virtual std::vector getSecurityViolations() const = 0; + virtual Result clearSecurityViolations() = 0; +}; +``` + +#### 3.1.2 ICryptoManager +```cpp +class ICryptoManager { +public: + virtual ~ICryptoManager() = default; + + // Symmetric Encryption + virtual Result> encrypt(const std::vector& data, + const SymmetricKey& key) = 0; + virtual Result> decrypt(const std::vector& data, + const SymmetricKey& key) = 0; + + // Asymmetric Encryption + virtual Result> encryptAsymmetric(const std::vector& data, + const PublicKey& key) = 0; + virtual Result> decryptAsymmetric(const std::vector& data, + const PrivateKey& key) = 0; + + // Digital Signatures + virtual Result sign(const std::vector& data, + const PrivateKey& key) = 0; + virtual Result verify(const std::vector& data, + const Signature& signature, + const PublicKey& key) = 0; + + // Hash Functions + virtual Result calculateSHA256(const std::vector& data) = 0; + virtual Result calculateHMAC(const std::vector& data, + const SymmetricKey& key) = 0; + + // Random Number Generation + virtual Result> generateRandomBytes(size_t length) = 0; + virtual Result generateRandomNumber() = 0; +}; +``` + +#### 3.1.3 IKeyManager +```cpp +class IKeyManager { +public: + virtual ~IKeyManager() = default; + + // Key Generation + virtual Result generateSymmetricKey(SymmetricKeyType type) = 0; + virtual Result generateAsymmetricKeyPair(AsymmetricKeyType type) = 0; + virtual Result deriveKey(const KeyDerivationParams& params) = 0; + + // Key Storage + virtual Result storeKey(KeyHandle handle, const SecureStorage& storage) = 0; + virtual Result loadKey(const KeyIdentifier& identifier) = 0; + virtual Result deleteKey(KeyHandle handle) = 0; + + // Key Operations + virtual Result rotateKey(KeyHandle handle) = 0; + virtual Result backupKey(KeyHandle handle, const BackupParams& params) = 0; + virtual Result restoreKey(const BackupData& backup) = 0; + + // Key Information + virtual Result getKeyInfo(KeyHandle handle) const = 0; + virtual std::vector listKeys() const = 0; + virtual bool isKeyValid(KeyHandle handle) const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 IDiagnosticsManager +- Security violation reporting and logging +- Security event documentation +- Security metrics collection + +#### 3.2.2 IPersistenceManager +- Secure storage of certificates and keys +- Security configuration persistence +- Audit log storage + +#### 3.2.3 ISystemStateManager +- Security state coordination +- Boot failure state management +- Security-related state transitions + +#### 3.2.4 IEventSystem +- Security event publication +- Security alert notifications +- Asynchronous security event handling + +## 4. Dynamic View + +### 4.1 Secure Boot Sequence + +```mermaid +sequenceDiagram + participant PWR as Power On + participant ROM as ROM Bootloader + participant SBC as Secure Boot Controller + participant BV as Boot Validator + participant ARB as Anti-Rollback Manager + participant APP as Application + participant SVH as Security Violation Handler + + PWR->>ROM: System Reset + ROM->>SBC: initializeSecureBoot() + SBC->>BV: validateFirmwareSignature() + BV->>BV: verifyRSA3072Signature() + BV->>ARB: checkAntiRollback() + ARB-->>BV: version_check_result + + alt Signature Valid and Version OK + BV-->>SBC: validation_success + SBC->>APP: jumpToApplication() + else Validation Failed + BV-->>SBC: validation_failed + SBC->>SVH: reportSecurityViolation(BOOT_FAILURE) + SBC->>SBC: enterBootFailureState() + end +``` + +### 4.2 TLS Connection Establishment Sequence + +```mermaid +sequenceDiagram + participant APP as Application + participant TM as TLS Manager + participant CA as Certificate Authority + participant KM as Key Manager + participant CM as Crypto Manager + participant MH as Main Hub + + APP->>TM: establishSecureConnection(main_hub_params) + TM->>KM: getDeviceCertificate() + KM-->>TM: device_certificate + TM->>KM: getDevicePrivateKey() + KM-->>TM: device_private_key + + TM->>MH: TLS_ClientHello + Certificate + MH->>TM: TLS_ServerHello + Certificate + + TM->>CA: validateServerCertificate(server_cert) + CA->>CA: verifyCertificateChain() + CA-->>TM: certificate_validation_result + + alt Certificate Valid + TM->>CM: generateSessionKeys() + CM-->>TM: session_keys + TM->>TM: establishSecureChannel() + TM-->>APP: secure_connection_established + else Certificate Invalid + TM->>SVH: reportSecurityViolation(CERT_VALIDATION_FAILED) + TM-->>APP: connection_failed + end +``` + +### 4.3 Security Violation Handling Sequence + +```mermaid +sequenceDiagram + participant COMP as System Component + participant SVH as Security Violation Handler + participant SM as Security Manager + participant DIAG as Diagnostics Manager + participant STM as System State Manager + participant ES as Event System + + COMP->>SVH: reportSecurityViolation(violation) + SVH->>SVH: classifyViolation(violation) + SVH->>DIAG: logSecurityEvent(violation) + + alt Critical Violation + SVH->>STM: transitionToSecureState() + SVH->>ES: publishCriticalSecurityAlert(violation) + SVH->>SM: implementSecurityMeasures() + else Non-Critical Violation + SVH->>ES: publishSecurityWarning(violation) + SVH->>SM: updateSecurityMetrics() + end + + SVH->>SVH: updateViolationHistory() + SVH-->>COMP: violation_handled +``` + +## 5. Security Policies and Configuration + +### 5.1 Security Policy Structure +```cpp +struct SecurityPolicy { + // Secure Boot Policy + SecureBootPolicy secure_boot; + + // Flash Encryption Policy + FlashEncryptionPolicy flash_encryption; + + // Communication Security Policy + CommunicationSecurityPolicy communication; + + // Key Management Policy + KeyManagementPolicy key_management; + + // Access Control Policy + AccessControlPolicy access_control; + + // Violation Response Policy + ViolationResponsePolicy violation_response; +}; +``` + +### 5.2 Secure Boot Configuration +```cpp +struct SecureBootPolicy { + bool enabled; + SignatureAlgorithm signature_algorithm; // RSA-3072 or ECDSA-P256 + bool anti_rollback_enabled; + uint32_t minimum_security_version; + std::vector trusted_keys; + BootFailureAction failure_action; +}; +``` + +### 5.3 Flash Encryption Configuration +```cpp +struct FlashEncryptionPolicy { + bool enabled; + EncryptionAlgorithm algorithm; // AES-256 + EncryptionMode mode; // Release or Development + std::vector encrypted_regions; + KeyDerivationMethod key_derivation; + bool external_storage_encryption; +}; +``` + +### 5.4 Communication Security Configuration +```cpp +struct CommunicationSecurityPolicy { + TLSVersion minimum_tls_version; // TLS 1.2 + bool mutual_authentication_required; + std::vector trusted_ca_certificates; + CertificateValidationLevel validation_level; + CipherSuite allowed_cipher_suites; + uint32_t session_timeout_seconds; +}; +``` + +## 6. Cryptographic Operations + +### 6.1 Supported Algorithms +```cpp +namespace CryptoAlgorithms { + // Symmetric Encryption + constexpr const char* AES_256_GCM = "AES-256-GCM"; + constexpr const char* AES_256_CBC = "AES-256-CBC"; + + // Asymmetric Encryption + constexpr const char* RSA_3072 = "RSA-3072"; + constexpr const char* ECDSA_P256 = "ECDSA-P256"; + + // Hash Functions + constexpr const char* SHA_256 = "SHA-256"; + constexpr const char* SHA_512 = "SHA-512"; + + // Key Derivation + constexpr const char* PBKDF2 = "PBKDF2"; + constexpr const char* HKDF = "HKDF"; +} +``` + +### 6.2 Key Management +```cpp +class SecureKeyManager { +public: + // Key Types + enum class KeyType { + SYMMETRIC_AES_256, + ASYMMETRIC_RSA_3072, + ASYMMETRIC_ECDSA_P256, + DEVICE_IDENTITY, + SESSION_KEY, + ENCRYPTION_KEY + }; + + // Key Storage Locations + enum class KeyStorage { + EFUSE, // One-time programmable secure storage + ENCRYPTED_FLASH, // Encrypted flash storage + HARDWARE_HSM, // Hardware security module + VOLATILE_MEMORY // Temporary storage in RAM + }; + + // Key Operations + Result generateKey(KeyType type, KeyStorage storage); + Result rotateKey(KeyHandle handle); + Result revokeKey(KeyHandle handle); + Result getKeyInfo(KeyHandle handle) const; +}; +``` + +## 7. Certificate Management + +### 7.1 Certificate Types +```cpp +enum class CertificateType { + DEVICE_IDENTITY, // Unique device certificate + CA_ROOT, // Root CA certificate + CA_INTERMEDIATE, // Intermediate CA certificate + SERVER_CERTIFICATE, // Server authentication certificate + CLIENT_CERTIFICATE // Client authentication certificate +}; +``` + +### 7.2 Certificate Validation +```cpp +class CertificateValidator { +public: + struct ValidationResult { + bool is_valid; + std::vector errors; + CertificateInfo certificate_info; + }; + + ValidationResult validateCertificate(const Certificate& certificate) const; + ValidationResult validateCertificateChain(const CertificateChain& chain) const; + ValidationResult checkCertificateRevocation(const Certificate& certificate) const; + bool isCertificateExpired(const Certificate& certificate) const; + bool isCertificateTrusted(const Certificate& certificate) const; +}; +``` + +## 8. Security Violation Management + +### 8.1 Violation Types +```cpp +enum class SecurityViolationType { + // Boot Security Violations + SECURE_BOOT_FAILURE, + FIRMWARE_SIGNATURE_INVALID, + ANTI_ROLLBACK_VIOLATION, + + // Communication Security Violations + TLS_HANDSHAKE_FAILURE, + CERTIFICATE_VALIDATION_FAILURE, + MESSAGE_INTEGRITY_VIOLATION, + + // Access Control Violations + AUTHENTICATION_FAILURE, + AUTHORIZATION_FAILURE, + SESSION_HIJACKING_ATTEMPT, + + // Cryptographic Violations + KEY_COMPROMISE_DETECTED, + ENCRYPTION_FAILURE, + SIGNATURE_VERIFICATION_FAILURE, + + // Physical Security Violations + TAMPER_DETECTION, + SIDE_CHANNEL_ATTACK, + FAULT_INJECTION_DETECTED +}; +``` + +### 8.2 Violation Response Actions +```cpp +enum class ViolationResponseAction { + LOG_ONLY, // Log the violation but continue operation + ALERT_AND_CONTINUE, // Send alert and continue with monitoring + DEGRADE_FUNCTIONALITY, // Reduce system functionality + ISOLATE_COMPONENT, // Isolate affected component + SYSTEM_SHUTDOWN, // Controlled system shutdown + EMERGENCY_LOCKDOWN // Immediate system lockdown +}; +``` + +### 8.3 Violation Handling +```cpp +class SecurityViolationHandler { +public: + void reportViolation(const SecurityViolation& violation); + void handleViolation(const SecurityViolation& violation); + std::vector getViolationHistory() const; + SecurityMetrics getSecurityMetrics() const; + +private: + ViolationResponseAction determineResponse(SecurityViolationType type) const; + void executeResponse(ViolationResponseAction action, const SecurityViolation& violation); + void updateSecurityState(const SecurityViolation& violation); +}; +``` + +## 9. Hardware Security Integration + +### 9.1 eFuse Management +```cpp +class eFuseManager { +public: + // eFuse Operations + Result burneFuse(eFuseField field, const std::vector& data); + Result> readeFuse(eFuseField field) const; + bool iseFuseBurned(eFuseField field) const; + + // Security Version Management + Result setSecurityVersion(uint32_t version); + uint32_t getSecurityVersion() const; + bool isVersionRollbackAllowed(uint32_t target_version) const; + + // Key Management + Result burnEncryptionKey(const EncryptionKey& key); + Result burnSigningKey(const PublicKey& key); + bool isKeyBurned(KeyType type) const; +}; +``` + +### 9.2 Hardware Security Module Integration +```cpp +class HardwareSecurityModule { +public: + // Cryptographic Operations + Result> performCryptoOperation( + CryptoOperation operation, + const std::vector& input, + const CryptoParams& params) const; + + // Key Operations + Result generateSecureKey(KeyType type); + Result storeSecureKey(KeyHandle handle, const SecureKey& key); + Result deleteSecureKey(KeyHandle handle); + + // Random Number Generation + Result> generateTrueRandomBytes(size_t length) const; + + // Attestation + Result generateAttestation() const; + Result verifyAttestation(const AttestationReport& report) const; +}; +``` + +## 10. Performance and Resource Management + +### 10.1 Performance Characteristics +- **Secure Boot Validation**: < 5 seconds during boot +- **TLS Handshake**: < 10 seconds for connection establishment +- **Cryptographic Operations**: Hardware-accelerated when available +- **Key Operations**: < 100ms for typical key operations +- **Certificate Validation**: < 2 seconds for certificate chain validation + +### 10.2 Resource Usage +- **Memory**: < 32KB for security operations and buffers +- **Storage**: Variable based on certificate and key storage requirements +- **CPU**: Hardware acceleration utilized when available +- **Power**: Optimized for low-power operation + +## 11. Error Handling and Recovery + +### 11.1 Error Categories +- **Cryptographic Errors**: Key generation failures, encryption/decryption errors +- **Certificate Errors**: Certificate validation failures, expiration errors +- **Hardware Errors**: HSM failures, eFuse access errors +- **Communication Errors**: TLS handshake failures, connection errors +- **Configuration Errors**: Invalid security policy configuration + +### 11.2 Recovery Strategies +- **Graceful Degradation**: Reduce security level while maintaining operation +- **Fallback Mechanisms**: Use alternative security methods when primary fails +- **Error Isolation**: Isolate security failures to prevent system-wide impact +- **Recovery Procedures**: Automated recovery from transient security failures + +## 12. Compliance and Standards + +### 12.1 Security Standards +- **FIPS 140-2**: Cryptographic module validation +- **Common Criteria**: Security evaluation criteria +- **IEC 62443**: Industrial communication networks security +- **ISO/IEC 27001**: Information security management + +### 12.2 Cryptographic Standards +- **NIST SP 800-series**: Cryptographic guidelines and recommendations +- **RFC 5280**: X.509 certificate and CRL profile +- **RFC 8446**: TLS 1.3 specification +- **PKCS standards**: Public key cryptography standards + +## 13. Testing Strategy + +### 13.1 Security Testing +- **Penetration Testing**: Simulated attacks and vulnerability assessment +- **Cryptographic Testing**: Algorithm implementation validation +- **Certificate Testing**: Certificate validation and chain verification +- **Protocol Testing**: TLS/mTLS implementation testing + +### 13.2 Compliance Testing +- **Standards Compliance**: Verification against security standards +- **Interoperability Testing**: Compatibility with external systems +- **Performance Testing**: Security operation performance validation +- **Stress Testing**: Security under high load and attack conditions + +## 14. Dependencies + +### 14.1 Internal Dependencies +- Diagnostics Manager for security event logging +- System State Manager for security state coordination +- Persistence Manager for secure storage operations +- Communication Manager for secure communication + +### 14.2 External Dependencies +- ESP-IDF security APIs (Secure Boot, Flash Encryption) +- mbedTLS cryptographic library +- Hardware security features (eFuse, HSM, Hardware RNG) +- Certificate authority infrastructure + +## 15. Constraints and Assumptions + +### 15.1 Constraints +- Hardware security features must be properly configured +- Cryptographic operations limited by available hardware acceleration +- Key storage limited by eFuse and secure storage capacity +- Security operations must not significantly impact system performance + +### 15.2 Assumptions +- Proper hardware security configuration during manufacturing +- Valid certificates and keys provisioned during deployment +- Secure communication channels available for key exchange +- Sufficient system resources for security operations +- Proper physical security of the device deployment environment \ No newline at end of file diff --git a/1 software design/components/time_utils/COMPONENT.md b/1 software design/components/time_utils/COMPONENT.md new file mode 100644 index 0000000..bfeac59 --- /dev/null +++ b/1 software design/components/time_utils/COMPONENT.md @@ -0,0 +1,513 @@ +# Time Utils Component Specification + +**Component ID:** C-TIME-001 +**Component Name:** Time Utils +**Version:** 1.0 +**Date:** 2025-02-01 +**Location:** `application_layer/utils/time_utils/` + +## 1. Component Overview + +### 1.1 Purpose +The Time Utils component provides centralized time management, timestamp generation, and time synchronization services for the ASF Sensor Hub. It ensures consistent time representation across all system components and provides accurate timestamps for sensor data, events, and system operations. + +### 1.2 Scope +- System time management and synchronization +- High-precision timestamp generation +- Time zone handling and conversion +- Uptime and duration tracking +- Time-based scheduling utilities +- Time format conversion and validation + +### 1.3 Responsibilities +- Maintain accurate system time +- Generate timestamps for sensor data and events +- Provide time synchronization with external sources +- Handle time zone conversions +- Track system uptime and component operation durations +- Provide time-based utility functions + +## 2. Component Architecture + +### 2.1 Static View + +```mermaid +graph TB + subgraph "Time Utils" + TC[Time Controller] + TS[Time Synchronizer] + TG[Timestamp Generator] + TZ[Timezone Handler] + UT[Uptime Tracker] + TF[Time Formatter] + end + + subgraph "Time Sources" + RTC[RTC Hardware] + NTP[NTP Server] + GPS[GPS Module] + MH[Main Hub] + end + + subgraph "External Components" + SM[Sensor Manager] + ES[Event System] + DIAG[Diagnostics Manager] + LOGGER[Logger] + end + + TC --> TS + TC --> TG + TC --> TZ + TC --> UT + TC --> TF + + TS --> RTC + TS --> NTP + TS --> GPS + TS --> MH + + SM --> TG + ES --> TG + DIAG --> TG + LOGGER --> TG +``` + +### 2.2 Internal Components + +#### 2.2.1 Time Controller +- Central coordination of time management operations +- Time source selection and prioritization +- Time accuracy monitoring and validation + +#### 2.2.2 Time Synchronizer +- External time source synchronization +- Time drift detection and correction +- Synchronization scheduling and management + +#### 2.2.3 Timestamp Generator +- High-precision timestamp generation +- Monotonic time tracking +- Timestamp format standardization + +#### 2.2.4 Timezone Handler +- Time zone conversion and management +- Daylight saving time handling +- Local time calculation + +#### 2.2.5 Uptime Tracker +- System uptime monitoring +- Component operation duration tracking +- Performance timing utilities + +#### 2.2.6 Time Formatter +- Time format conversion utilities +- String representation generation +- Time parsing and validation + +## 3. Interfaces + +### 3.1 Provided Interfaces + +#### 3.1.1 ITimeUtils +```cpp +class ITimeUtils { +public: + virtual ~ITimeUtils() = default; + + // Time Management + virtual Result initialize() = 0; + virtual Result shutdown() = 0; + virtual Result synchronizeTime() = 0; + virtual TimeStatus getTimeStatus() const = 0; + + // Timestamp Generation + virtual uint64_t getCurrentTimestampMs() const = 0; + virtual uint64_t getCurrentTimestampUs() const = 0; + virtual std::chrono::system_clock::time_point getCurrentTimePoint() const = 0; + virtual uint64_t getMonotonicTimestampMs() const = 0; + + // Time Conversion + virtual std::string formatTimestamp(uint64_t timestamp_ms, + TimeFormat format = TimeFormat::ISO8601) const = 0; + virtual Result parseTimestamp(const std::string& time_str, + TimeFormat format = TimeFormat::ISO8601) const = 0; + virtual Result convertToUtc(uint64_t local_timestamp_ms) const = 0; + virtual Result convertToLocal(uint64_t utc_timestamp_ms) const = 0; + + // Uptime and Duration + virtual uint64_t getSystemUptimeMs() const = 0; + virtual uint64_t getComponentUptimeMs(ComponentId component) const = 0; + virtual Result startDurationTracking(const std::string& operation_id) = 0; + virtual Result stopDurationTracking(const std::string& operation_id) = 0; + + // Time Zone Management + virtual Result setTimeZone(const std::string& timezone) = 0; + virtual std::string getCurrentTimeZone() const = 0; + virtual bool isDaylightSavingTime() const = 0; + + // Time Validation + virtual bool isTimestampValid(uint64_t timestamp_ms) const = 0; + virtual bool isTimeAccurate() const = 0; + virtual std::chrono::milliseconds getTimeAccuracy() const = 0; +}; +``` + +#### 3.1.2 ITimestampProvider +```cpp +class ITimestampProvider { +public: + virtual ~ITimestampProvider() = default; + virtual uint64_t getTimestamp() const = 0; + virtual uint64_t getMonotonicTimestamp() const = 0; + virtual bool isTimestampAccurate() const = 0; +}; +``` + +#### 3.1.3 ITimeFormatter +```cpp +class ITimeFormatter { +public: + virtual ~ITimeFormatter() = default; + virtual std::string formatTime(uint64_t timestamp_ms, TimeFormat format) const = 0; + virtual std::string formatDuration(uint64_t duration_ms) const = 0; + virtual Result parseTime(const std::string& time_str, TimeFormat format) const = 0; +}; +``` + +### 3.2 Required Interfaces + +#### 3.2.1 IRTCDriver +- Real-time clock hardware access +- RTC time reading and setting +- RTC alarm and interrupt management + +#### 3.2.2 INetworkTime +- NTP client functionality +- Network time synchronization +- Time server communication + +#### 3.2.3 IConfigurationManager +- Time zone configuration +- Synchronization settings +- Time source preferences + +#### 3.2.4 ILogger +- Time synchronization event logging +- Time accuracy monitoring logs +- Error condition reporting + +## 4. Dynamic View + +### 4.1 Time Synchronization Sequence + +```mermaid +sequenceDiagram + participant TC as Time Controller + participant TS as Time Synchronizer + participant NTP as NTP Client + participant RTC as RTC Driver + participant LOGGER as Logger + + TC->>TS: synchronizeTime() + TS->>NTP: requestTimeSync() + NTP->>NTP: queryNTPServer() + NTP-->>TS: ntp_time_response + + alt NTP Sync Successful + TS->>TS: validateTimeResponse(ntp_time) + TS->>RTC: setSystemTime(ntp_time) + RTC-->>TS: time_set_success + TS->>TS: updateTimeAccuracy(HIGH) + TS->>LOGGER: logTimeSyncSuccess(ntp_time) + TS-->>TC: sync_successful + else NTP Sync Failed + TS->>RTC: getCurrentTime() + RTC-->>TS: rtc_time + TS->>TS: updateTimeAccuracy(LOW) + TS->>LOGGER: logTimeSyncFailure(reason) + TS-->>TC: sync_failed + end +``` + +### 4.2 Timestamp Generation Sequence + +```mermaid +sequenceDiagram + participant SM as Sensor Manager + participant TG as Timestamp Generator + participant RTC as RTC Driver + participant TC as Time Controller + + SM->>TG: getCurrentTimestampMs() + TG->>RTC: getSystemTime() + RTC-->>TG: current_time + TG->>TC: getTimeAccuracy() + TC-->>TG: accuracy_status + TG->>TG: generateTimestamp(current_time, accuracy) + TG-->>SM: timestamp_ms + + Note over SM,TG: Timestamp includes accuracy metadata +``` + +### 4.3 Duration Tracking Sequence + +```mermaid +sequenceDiagram + participant COMP as Component + participant UT as Uptime Tracker + participant TG as Timestamp Generator + + COMP->>UT: startDurationTracking("sensor_acquisition") + UT->>TG: getMonotonicTimestamp() + TG-->>UT: start_timestamp + UT->>UT: storeStartTime("sensor_acquisition", start_timestamp) + UT-->>COMP: tracking_started + + Note over COMP,UT: Operation executes + + COMP->>UT: stopDurationTracking("sensor_acquisition") + UT->>TG: getMonotonicTimestamp() + TG-->>UT: end_timestamp + UT->>UT: calculateDuration(start_timestamp, end_timestamp) + UT-->>COMP: operation_duration_ms +``` + +## 5. Time Management + +### 5.1 Time Sources Priority +```cpp +enum class TimeSource { + NTP_SERVER = 0, // Highest priority - Network Time Protocol + GPS_MODULE = 1, // High priority - GPS time + MAIN_HUB = 2, // Medium priority - Main Hub time + RTC_HARDWARE = 3, // Low priority - Hardware RTC + SYSTEM_CLOCK = 4 // Lowest priority - System clock +}; +``` + +### 5.2 Time Accuracy Levels +```cpp +enum class TimeAccuracy { + HIGH, // ±1 second accuracy (NTP/GPS synchronized) + MEDIUM, // ±10 seconds accuracy (Recent sync) + LOW, // ±60 seconds accuracy (RTC only) + UNKNOWN // Accuracy unknown (no sync) +}; +``` + +### 5.3 Time Synchronization Strategy +- **Primary Sync**: NTP server synchronization every 24 hours +- **Backup Sync**: Main Hub time synchronization every 6 hours +- **Drift Monitoring**: Continuous monitoring of time drift +- **Accuracy Tracking**: Real-time accuracy assessment +- **Fallback**: RTC hardware as last resort time source + +## 6. Timestamp Generation + +### 6.1 Timestamp Formats +```cpp +enum class TimeFormat { + UNIX_TIMESTAMP_MS, // Milliseconds since Unix epoch + UNIX_TIMESTAMP_US, // Microseconds since Unix epoch + ISO8601, // ISO 8601 format (YYYY-MM-DDTHH:MM:SS.sssZ) + RFC3339, // RFC 3339 format + HUMAN_READABLE, // Human-readable format + COMPACT // Compact format for storage +}; +``` + +### 6.2 Timestamp Structure +```cpp +struct Timestamp { + uint64_t timestamp_ms; // Milliseconds since epoch + TimeAccuracy accuracy; // Timestamp accuracy level + TimeSource source; // Time source used + bool is_synchronized; // Whether time is synchronized + std::chrono::milliseconds drift; // Estimated drift from accurate time +}; +``` + +### 6.3 Monotonic Time Tracking +- **Boot Time Reference**: Monotonic time since system boot +- **Overflow Handling**: 64-bit counters to prevent overflow +- **High Resolution**: Microsecond resolution for precise timing +- **Drift Compensation**: Compensation for clock drift + +## 7. Time Zone Management + +### 7.1 Time Zone Support +```cpp +class TimeZoneManager { +public: + Result setTimeZone(const std::string& timezone); + std::string getCurrentTimeZone() const; + bool isDaylightSavingTime(uint64_t timestamp_ms) const; + uint64_t convertToUtc(uint64_t local_timestamp_ms) const; + uint64_t convertToLocal(uint64_t utc_timestamp_ms) const; + +private: + std::string current_timezone_; + int32_t utc_offset_minutes_; + bool dst_active_; +}; +``` + +### 7.2 Supported Time Zones +- **UTC**: Coordinated Universal Time (default) +- **Local**: Configurable local time zone +- **DST Support**: Automatic daylight saving time handling +- **Offset Calculation**: UTC offset calculation and conversion + +## 8. Performance Monitoring + +### 8.1 Uptime Tracking +```cpp +struct UptimeInfo { + uint64_t system_uptime_ms; // Total system uptime + uint64_t component_uptime_ms; // Component-specific uptime + uint32_t boot_count; // Number of system boots + uint64_t last_boot_time_ms; // Timestamp of last boot + std::map operation_durations; // Operation timing +}; +``` + +### 8.2 Duration Tracking +- **Operation Timing**: Track duration of specific operations +- **Performance Metrics**: Calculate average, min, max durations +- **Bottleneck Identification**: Identify slow operations +- **Trend Analysis**: Monitor performance trends over time + +## 9. Configuration + +### 9.1 Time Utils Configuration +```cpp +struct TimeUtilsConfig { + // Synchronization Configuration + std::string ntp_server_url; + std::chrono::hours sync_interval; + std::chrono::seconds sync_timeout; + bool automatic_sync_enabled; + + // Time Zone Configuration + std::string default_timezone; + bool dst_enabled; + int32_t utc_offset_minutes; + + // Accuracy Configuration + std::chrono::milliseconds max_acceptable_drift; + TimeAccuracy minimum_required_accuracy; + bool strict_accuracy_enforcement; + + // Logging Configuration + bool log_sync_events; + bool log_accuracy_changes; + bool log_time_jumps; + + // Performance Configuration + bool uptime_tracking_enabled; + bool duration_tracking_enabled; + uint32_t max_tracked_operations; +}; +``` + +## 10. Error Handling + +### 10.1 Time-Related Errors +```cpp +enum class TimeError { + SYNC_FAILED, // Time synchronization failed + INVALID_TIMESTAMP, // Invalid timestamp value + TIMEZONE_INVALID, // Invalid time zone specification + ACCURACY_INSUFFICIENT, // Time accuracy below requirements + DRIFT_EXCESSIVE, // Time drift exceeds limits + SOURCE_UNAVAILABLE, // Time source not available + FORMAT_INVALID // Invalid time format +}; +``` + +### 10.2 Error Recovery Strategies +- **Sync Retry**: Retry synchronization with exponential backoff +- **Source Fallback**: Fall back to alternative time sources +- **Accuracy Degradation**: Continue with reduced accuracy +- **Error Reporting**: Report time-related issues to diagnostics + +## 11. Performance Characteristics + +### 11.1 Timing Requirements +- **Timestamp Generation**: < 1ms for timestamp generation +- **Time Synchronization**: < 10 seconds for NTP synchronization +- **Format Conversion**: < 5ms for time format conversion +- **Duration Calculation**: < 1ms for duration tracking operations + +### 11.2 Resource Usage +- **Memory**: < 4KB for time management data structures +- **Storage**: Minimal persistent storage for time zone data +- **CPU**: < 0.5% average utilization for time operations +- **Network**: Periodic NTP synchronization traffic + +## 12. Security Considerations + +### 12.1 Time Security +- **NTP Security**: Secure NTP communication to prevent time attacks +- **Time Validation**: Validate time sources and detect time jumps +- **Accuracy Verification**: Verify time accuracy from multiple sources + +### 12.2 Timestamp Security +- **Monotonic Guarantees**: Ensure monotonic timestamp progression +- **Tamper Detection**: Detect attempts to manipulate system time +- **Audit Trail**: Maintain audit trail of time changes + +## 13. Testing Strategy + +### 13.1 Unit Tests +- Timestamp generation accuracy +- Time format conversion correctness +- Duration tracking precision +- Time zone conversion validation + +### 13.2 Integration Tests +- NTP synchronization functionality +- RTC integration and fallback +- Component timestamp consistency +- Time accuracy maintenance + +### 13.3 System Tests +- Long-term time accuracy validation +- Time synchronization under network failures +- Performance impact of time operations +- Time zone and DST handling + +## 14. Dependencies + +### 14.1 Internal Dependencies +- Configuration Manager for time settings +- Logger for time event logging +- Network stack for NTP synchronization +- Hardware drivers for RTC access + +### 14.2 External Dependencies +- ESP-IDF time and RTC APIs +- NTP client library +- Time zone database +- Hardware RTC support + +## 15. Constraints and Assumptions + +### 15.1 Constraints +- Time accuracy limited by available time sources +- Network dependency for high-accuracy time synchronization +- RTC hardware accuracy limitations +- Memory constraints for time zone data + +### 15.2 Assumptions +- Network connectivity available for NTP synchronization +- RTC hardware properly configured and functional +- System clock reasonably stable between synchronizations +- Time zone configuration provided during system setup + +--- + +**Document Status:** Final for Implementation +**Dependencies:** RTC Driver, Network Stack, Configuration Manager, Logger +**Next Review:** After component implementation and accuracy validation \ No newline at end of file