Overview
The device monitoring service:- Monitors USB hotplug events (Linux)
- Detects device connections and disconnections
- Sends notifications to active channels
- Extensible architecture for future event sources
- USB device hotplug (add/remove)
- Future: Bluetooth, PCI, network interfaces
Architecture
Service Structure
Event Source Interface
Device Event
Configuration
Creating the Service
Configuration Options
USB Monitoring (Linux)
Setup
USB monitoring uses netlink sockets to listen for kernel uevent messages:Event Detection
Device Added:Message Format
Events are formatted for user notification:Service Lifecycle
Starting the Service
- Check if service is enabled
- Initialize event sources
- Start source monitors
- Launch event handlers
- Log startup status
Stopping the Service
- Cancel context (stops event loops)
- Stop all event sources
- Close event channels
- Log shutdown status
Event Routing
Channel Resolution
Events are sent to the last active user channel:Internal Channel Filtering
Internal channels are filtered out:- telegram
- discord
- slack
Message Delivery
Events are delivered via the message bus:Event Handling
Event Loop
- Receive event from source channel
- Validate event (non-nil)
- Format notification message
- Resolve target channel
- Send via message bus
- Log delivery status
Linux Implementation Details
Netlink Socket
USB monitoring uses netlink KOBJECT_UEVENT:Uevent Parsing
Kernel uevent messages are parsed:ACTION→ DeviceEvent.ActionDEVPATH→ DeviceEvent.DeviceIDPRODUCT→ Vendor/Product IDsDEVNAME→ Device path
Device Identification
USB devices are identified by reading sysfs:Use Cases
Camera Hotplug Detection
Scenario: Notify when MaixCAM is connected/disconnected- Initialize camera
- Start video stream
- Configure settings
Storage Device Monitoring
Scenario: Auto-backup when USB drive is inserted Implementation:- Device monitoring detects USB storage
- Notification sent to agent
- Agent identifies device as backup drive
- Agent spawns backup subagent
Hardware Development
Scenario: Debug USB device during development Workflow:- Connect development board
- Receive connection notification
- Agent checks device is recognized
- Agent runs initialization script
Advanced Configuration
Custom Event Sources
Implement custom event sources:Multi-Source Monitoring
Event Filtering
Ignore Specific Devices
Filter out unwanted devices:Device Whitelisting
Only notify for specific devices:Logging
Device events are logged:Error Handling
Source Start Failures
Event Processing Errors
Channel Resolution Failures
Performance Considerations
Resource Usage
- Memory: One goroutine per event source
- CPU: Idle when no events, active during processing
- Network: Netlink socket (kernel events only)
Event Rate
USB events are typically infrequent:- Device insertion: < 1 per minute
- Device removal: < 1 per minute
- No performance impact on system
Best Practices
-
Enable only needed sources:
-
Filter irrelevant events:
- Ignore common devices (mouse, keyboard)
- Whitelist important devices
-
Handle rapid reconnections:
- Debounce events
- Aggregate multiple events
-
Test event handling:
- Simulate device insertion/removal
- Verify notifications are sent
- Check log output
-
Monitor service health:
- Check logs for source failures
- Verify event delivery
- Test with real devices
Platform Support
Linux
- USB Monitoring: Full support via netlink
- Bluetooth: Planned (BlueZ D-Bus)
- PCI: Planned (uevent)
macOS
- USB Monitoring: Planned (IOKit)
- Bluetooth: Planned (CoreBluetooth)
Windows
- USB Monitoring: Planned (WMI)
- Bluetooth: Planned (Windows.Devices.Bluetooth)
Future Enhancements
Planned Event Sources
-
Bluetooth Monitoring
- Device pairing/unpairing
- Connection state changes
- Signal strength monitoring
-
Network Interface Monitoring
- Interface up/down
- IP address changes
- Connection state
-
PCI Device Monitoring
- GPU hotplug
- Thunderbolt devices
- PCIe device changes