Roboticks SDK Architecture Summary
Executive Overview
The Roboticks SDK is a C++17-based robotics framework for edge devices with:- Unified session management for tracking robot execution lifecycle
- AWS IoT MQTT integration for cloud communication
- Multi-tier artifact management (logs, recordings, telemetry)
- Background thread-based collectors for logs and session data
- HTTP REST client for device registration and capsule downloads
- File-based persistence with auto-recovery on restart
1. Session Management Architecture
Session Lifecycle (Session.hpp/cpp)
CREATED: Session initialized but not startedACTIVE: Running and collecting dataPAUSED: Temporarily suspendedCOMPLETED: Finished successfullyUPLOADING: Artifacts being sent to backendUPLOADED: All data transferredFAILED: Terminated with error
start()- Begin session (CREATED → ACTIVE)pause()/resume()- Pause/resume executioncomplete()- Finish session (records end time)fail(reason)- Terminate with erroraddArtifact()- Register collected filegetArtifacts()- Retrieve all collected filestoJson()- Serialize for storage/transmission
- Uses
std::atomic<SessionStatus>for thread-safe state transitions - Mutex protects collections and metadata map
- Destructor auto-completes active sessions on shutdown
- Times stored in microseconds (epoch-based)
2. DeviceManager Overview
Core Responsibilities
TheDeviceManager is a singleton facade that:
- Manages device registration with backend (one-time, cached in
/etc/roboticks/) - Maintains active sessions (create, pause, resume, complete)
- Publishes telemetry via MQTT to AWS IoT Core
- Collects logs from modules via ZeroMQ
- Handles commands from backend (via MQTT subscriptions)
- Manages storage (sessions directory with active/completed subdirs)
Configuration
Storage Directory Structure
3. Initialization & Registration Flow
Step 1: Load Configuration
- Checks multiple locations for
device.yaml - Falls back to defaults if not found
- Loads from YAML or environment variables
Step 2: Check for Existing Registration
If certificates exist in/etc/roboticks/:
- Load DSN from
device_idfile - Load IoT endpoint from
config.json - Configure HTTP client with mTLS
- Create IoT client connection to AWS
- Check for
project_secret(required for auto-registration) - Call
registerDevice()→ POST/api/v1/fleet/devices/register - Backend responds with certificate + DSN
- Save to
/etc/roboticks/(600 permissions for private key) - Proceed with IoT connection
Step 3: Initialize Subsystems
- Messaging System - ZeroMQ transport for internal comms (binds early)
- Session Storage - Create directories, cleanup leftover sessions
- MQTT Connection - Subscribe to command topic
- Log Collection - Create ZeroMQ subscriber + start upload thread
- Background Threads - Start heartbeat (disabled command polling)
Step 4: Auto-Start Session
- Creates initial session with metadata
- Publishes creation to backend via MQTT
Step 5: Write Ready File
- Creates
/tmp/roboticks-device-readyfor orchestration tools
4. MQTT Communication Patterns
Topics & QoS
Heartbeat Flow (every 30s)
Session Lifecycle Events
Command Handling
- Subscribed to topic at startup (not polling)
- Callback:
onMqttCommandReceived()→ parse JSON → find handler → execute - Command types: DEPLOY_CAPSULE, RESTART, UPDATE_CONFIG, START_SESSION, STOP_SESSION, OTA_UPDATE
Log Upload
5. Log Collection System
Architecture
Log Buffer Management
Upload Strategy
- Batch Trigger 1: Buffer reaches 25 logs → upload immediately
- Batch Trigger 2: 30 seconds timeout → upload whatever is buffered
- On Failure: Requeue failed logs back to front (bounded by buffer size)
- On Shutdown: Flush all remaining logs before exit
6. HTTP Communication
Client Setup
Key Endpoints
File Upload Method
- Uses multipart/form-data
- Supports progress callbacks
- Implemented via libcurl
7. Background Threads
Heartbeat Thread
Command Polling Thread
Log Upload Thread
Shutdown Flow
8. Teardown & Cleanup
DeviceManager::shutdown()
- Stop heartbeat thread
- Stop command polling thread
- Shutdown log collection (flush all buffered logs)
- Finalize active session (publish completion event)
- Save all sessions to disk
- Mark as uninitialized
Session Destruction
- Destructor auto-completes active sessions
- All files persisted to disk before cleanup
File Cleanup
- Old sessions removed by
cleanupOldSessions(30)(>30 days old) - Incomplete sessions recovered on startup
- Session files saved to
/var/roboticks/sessions/{active|completed}/{session_id}/
9. Signal Handling
Setup (in device_manager_main.cpp)
Main Loop
10. File Handling & Artifacts
Current Implementation
Collected File Types
runtime.log- Main runtime log{module_name}.log- Per-module logs- (Future)
recording- Video/sensor recordings - (Future)
telemetry- Raw sensor data - (Future)
map- Generated maps/point clouds
Collection Methods
Storage Layout
11. Key Design Patterns
Patterns Used
| Pattern | Location | Purpose |
|---|---|---|
| Singleton | DeviceManager | Global device state, thread-safe |
| Facade | DeviceManager | Unified interface to complex subsystems |
| Observer | Event callbacks | Decouple session events from handlers |
| Ring Buffer | Log buffer | Fixed memory, auto-drop oldest on overflow |
| Producer-Consumer | Log system | Async log collection ↔ upload threads |
| RAII | Lock guards | Exception-safe mutex protection |
| Atomic types | Flags/counters | Lock-free thread synchronization |
Thread Safety
std::atomic<bool>for flags (stop signals, ready states)std::mutex + std::lock_guard<>for collections (sessions, logs, handlers)std::condition_variablefor thread coordination (log upload)- All callbacks execute in caller’s thread (no spawning)
12. Recommended Locations for File Upload Feature
Option A: Extend Session API (Recommended)
File:/Users/mujacic/roboticks-sdk/packages/roboticks-device/include/roboticks/device/Session.hpp
Add methods to Session class:
- Sessions already track artifacts
- Fits existing lifecycle (COMPLETED → UPLOADING → UPLOADED)
- Can reuse existing HTTP client
Option B: Extend DeviceManager Upload (Better for Device-Level)
File:/Users/mujacic/roboticks-sdk/packages/roboticks-device/src/DeviceManager.cpp
Add to DeviceManager:
- Device-level control of uploads
- Can rate-limit/batch multiple sessions
- Better for offline buffering strategy
Option C: New Artifact Upload Thread (Best for Scale)
File: Create newArtifactUploader class
- Parallel uploads don’t block log system
- Independent retry logic
- Scales to multiple sessions
13. Integration Points Summary
For File Upload Implementation
Must Use:- ✅
Session::addArtifact()- Register files before upload - ✅
DeviceManager::http_client_- HTTP requests (already with mTLS) - ✅
DeviceManager::iot_client_- Publish upload status events - ✅
config_.storage_path- Know where files are stored
- ✅ Thread safety - Use
std::lock_guard+ mutexes - ✅ Shutdown sequence - Check
shutdown_requestedflag - ✅ Signal handling - Don’t block graceful SIGTERM
- ✅ MQTT QoS - Use QoS=1 for important updates
- ✅
http_client_.uploadFile()- Already supports multipart uploads - ✅ Progress callback pattern - Used in capsule download
- ✅ Status reporting - Device already publishes to backend
- ✅ Event system -
registerEventCallback()for progress tracking
14. Current TODOs in Codebase
| Location | TODO | Priority |
|---|---|---|
| Session.cpp:104 | Get actual file size on addArtifact | High |
| DeviceManager.cpp:1162 | Implement actual upload to backend | High |
| DeviceManager.cpp:1247 | Implement session file serialization | High |
| DeviceManager.cpp:1253 | Load sessions from disk on startup | Medium |
| Various | Support multipart form uploads | High |
| Various | Offline buffering (queue uploads) | Medium |
| Various | Retry logic for failed uploads | Medium |
Summary Table
| Component | Language | Files | Purpose |
|---|---|---|---|
| Session | C++ | Session.hpp/cpp | Session lifecycle & artifact tracking |
| DeviceManager | C++ | DeviceManager.hpp/cpp | Device orchestration & cloud sync |
| IoTClient | C++ | IoTClient.hpp/cpp | AWS MQTT communication |
| HttpClient | C++ | HttpClient.hpp/cpp | REST API communication |
| DeviceManagerMain | C++ | device_manager_main.cpp | Entrypoint & signal handling |
| Configuration | YAML | device.yaml | Runtime config |
| Certificates | PEM | /etc/roboticks/* | Device identity (mTLS) |
| Session Storage | JSON | /var/roboticks/sessions/* | Persistent session state |