espilon-source/tools/C3PO/tui/bridge.py
Eun0us 8b6c1cd53d ε - ChaCha20-Poly1305 AEAD + HKDF crypto upgrade + C3PO rewrite + docs
Crypto:
- Replace broken ChaCha20 (static nonce) with ChaCha20-Poly1305 AEAD
- HKDF-SHA256 key derivation from per-device factory NVS master keys
- Random 12-byte nonce per message (ESP32 hardware RNG)
- crypto_init/encrypt/decrypt API with mbedtls legacy (ESP-IDF v5.3.2)
- Custom partition table with factory NVS (fctry at 0x10000)

Firmware:
- crypto.c full rewrite, messages.c device_id prefix + AEAD encrypt
- crypto_init() at boot with esp_restart() on failure
- Fix command_t initializations across all modules (sub/help fields)
- Clean CMakeLists dependencies for ESP-IDF v5.3.2

C3PO (C2):
- Rename tools/c2 + tools/c3po -> tools/C3PO
- Per-device CryptoContext with HKDF key derivation
- KeyStore (keys.json) for master key management
- Transport parses device_id:base64(...) wire format

Tools:
- New tools/provisioning/provision.py for factory NVS key generation
- Updated flasher with mbedtls config for v5.3.2

Docs:
- Update all READMEs for new crypto, C3PO paths, provisioning
- Update roadmap, architecture diagrams, security sections
- Update CONTRIBUTING.md project structure
2026-02-10 21:28:45 +01:00

66 lines
1.7 KiB
Python

"""
Thread-safe bridge between sync threads and async Textual TUI.
"""
import queue
import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, Any
class MessageType(Enum):
DEVICE_CONNECTED = "device_connected"
DEVICE_DISCONNECTED = "device_disconnected"
DEVICE_RECONNECTED = "device_reconnected"
DEVICE_INFO_UPDATED = "device_info_updated"
DEVICE_EVENT = "device_event"
COMMAND_SENT = "command_sent"
COMMAND_RESPONSE = "command_response"
SYSTEM_MESSAGE = "system_message"
ERROR = "error"
@dataclass
class TUIMessage:
"""Message from sync thread to async TUI."""
msg_type: MessageType
payload: str
timestamp: float = field(default_factory=time.time)
device_id: Optional[str] = None
request_id: Optional[str] = None
class TUIBridge:
"""Thread-safe bridge between sync threads and async Textual app."""
def __init__(self):
self._queue: queue.Queue[TUIMessage] = queue.Queue()
self._app: Any = None
def set_app(self, app):
"""Called by TUI app on startup."""
self._app = app
def post_message(self, msg: TUIMessage):
"""Called by sync threads (Display class)."""
self._queue.put(msg)
if self._app:
try:
self._app.call_from_thread(self._app.process_bridge_queue)
except Exception:
pass
def get_pending_messages(self) -> list[TUIMessage]:
"""Called by async TUI to drain the queue."""
messages = []
while True:
try:
messages.append(self._queue.get_nowait())
except queue.Empty:
break
return messages
# Global bridge instance
tui_bridge = TUIBridge()