Roboticks SDK - File Upload Implementation Guide
Executive Summary
The roboticks-sdk is a sophisticated C++17 framework with:- 2500+ lines of session/device management code
- Working HTTP client with file upload via libcurl
- MQTT integration for AWS IoT Core
- Artifact tracking system ready for extensions
- Background thread patterns for async operations
- Signal-safe shutdown with resource cleanup
Session and DeviceManager classes. No new infrastructure needed.
Current State: What Works
Already Implemented
- HTTP multipart upload -
HttpClient::uploadFile()exists and works - Session artifact tracking -
Session::addArtifact()stores file metadata - Session persistence - Basic framework for saving/loading sessions
- Status publishing - MQTT events for session lifecycle
- Thread patterns - Log upload thread shows async batching strategy
- Error handling - HTTP error codes, JSON parsing with try/catch
Already Partially Done (TODOs)
-
File size calculation - Session.cpp line 104
-
Session serialization - DeviceManager.cpp line 1247
-
Session persistence - DeviceManager.cpp line 1253
-
Upload implementation - DeviceManager.cpp line 1162
Implementation Plan
Phase 1: Quick Wins (1-2 hours)
1.1 Fix artifact size calculation File:/Users/mujacic/roboticks-sdk/packages/roboticks-device/src/Session.cpp
- Accurate artifact metadata
- Needed for upload progress reporting
- 10 lines of code
- Prerequisite for upload features
1.2 Implement Session JSON serialization File:
/Users/mujacic/roboticks-sdk/packages/roboticks-device/src/DeviceManager.cpp
Currently saveSessionToFile() is stubbed:
Session::toJson():
- Session state survives restart
- Enables recovery of incomplete sessions
- Prerequisite for upload queue persistence
- ~20 lines of code
Phase 2: Core Upload Feature (3-4 hours)
2.1 Add upload methods to Session class File:/Users/mujacic/roboticks-sdk/packages/roboticks-device/include/roboticks/device/Session.hpp
Add to Session class:
- Encapsulates upload logic in Session
- Tracks individual artifact status
- Supports progress callbacks
- ~70 lines of code
- Reusable from anywhere with Session reference
2.2 Integrate with DeviceManager upload workflow File:
/Users/mujacic/roboticks-sdk/packages/roboticks-device/src/DeviceManager.cpp
Update the existing uploadSession() method (currently stubbed at line 1153):
uploadCompletedSessions() to try uploads:
- Automatic upload on session completion
- Publish status events via MQTT
- Retry logic built-in
- Integration with existing DeviceManager cycle
Phase 3: Advanced Features (2-3 hours, optional)
3.1 Background upload thread (like log uploader) For high-volume scenarios, create a dedicated uploader:- Parallel uploading without blocking main loop
- Rate limiting per device
- Better resource utilization for large sessions
- Same pattern as log upload thread
File Change Summary
Modified Files
| File | Changes | LOC |
|---|---|---|
| Session.hpp | Add upload methods, size helper | 30 |
| Session.cpp | Implement upload logic, size calc | 80 |
| DeviceManager.hpp | Add upload thread members (optional) | 5 |
| DeviceManager.cpp | Real uploadSession(), integrate with cycle | 60 |
Total Code to Write: ~150-200 LOC
Total Time Estimate:
- Phase 1 (Quick wins): 1-2 hours
- Phase 2 (Core feature): 3-4 hours
- Phase 3 (Advanced): 2-3 hours (optional)
Testing Strategy
Unit Tests
Integration Tests
Manual Testing
- Create test device config
- Start device manager with debug logging
- Let it collect artifacts
- Verify HTTP requests logged
- Check backend received files
Deployment Checklist
- Unit tests pass (>90% coverage on upload code)
- Integration tests pass
- Manual testing on Jetson device
- Graceful shutdown with pending uploads
- MQTT status events publishing correctly
- File permissions correct (600 for private keys)
- Error handling for network failures
- Progress callbacks working
- Cleanup old uploaded sessions
- Thread safety with stress tests
Common Pitfalls to Avoid
- Forgetting to lock mutex - Always use
std::lock_guardwhen accessing shared state - Blocking main thread - Keep uploads in background or async
- Not flushing on shutdown - Ensure graceful SIGTERM flushes pending uploads
- Hardcoding URLs - Use config.storage_path, ROBOTICKS_API_URL consistently
- No error recovery - Retry failed uploads, don’t just log and exit
- Missing size validation - Check file exists before upload
- Race conditions - Session state changes must be atomic or protected
Success Criteria
Final implementation should:- Upload all session artifacts to backend on completion
- Support progress tracking
- Handle network failures gracefully
- Publish MQTT status events
- Clean shutdown without data loss
- Thread-safe concurrent uploads
- Respect device storage limits
- Pass all automated tests
- Scale to multiple large sessions