|
| 1 | + |
| 2 | +# Client Service Socket API |
| 3 | + |
| 4 | +The client service accepts JSON requests on the Unix socket and returns JSON responses. |
| 5 | + |
| 6 | +## Request Format |
| 7 | + |
| 8 | +All requests follow this JSON structure: |
| 9 | + |
| 10 | +```json |
| 11 | +{ |
| 12 | + "command": "attach", |
| 13 | + "id": "1-1.4", |
| 14 | + "bus": null, |
| 15 | + "serial": null, |
| 16 | + "desc": null, |
| 17 | + "first": false, |
| 18 | + "host": null |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +**Fields:** |
| 23 | +- `command`: Either `"attach"` or `"detach"` (required) |
| 24 | +- `id`: Bus ID of the device (e.g., "1-1.4") |
| 25 | +- `bus`: Bus number filter |
| 26 | +- `serial`: Serial number filter |
| 27 | +- `desc`: Description filter (substring match) |
| 28 | +- `first`: If true, use first matching device if multiple matches |
| 29 | +- `host`: Specific server hostname/IP to query (if null, queries all configured servers) |
| 30 | + |
| 31 | +You must provide at least one of: `id`, `bus`, `serial`, or `desc` to identify the device. |
| 32 | + |
| 33 | +## Response Format |
| 34 | + |
| 35 | +### Success Response |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "status": "success", |
| 40 | + "data": { |
| 41 | + "bus_id": "1-1.4", |
| 42 | + "device_id": "vid=0x1234 pid=0x5678", |
| 43 | + "description": "USB Device Description" |
| 44 | + }, |
| 45 | + "server": "192.168.1.100", |
| 46 | + "local_devices": ["/dev/ttyUSB0", "/dev/ttyUSB1"] |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +**Fields:** |
| 51 | +- `status`: Always `"success"` for successful operations |
| 52 | +- `data`: The USB device information |
| 53 | + - `bus_id`: The USB bus ID on the remote server |
| 54 | + - `device_id`: Vendor and product IDs |
| 55 | + - `description`: Human-readable device description |
| 56 | +- `server`: The server hostname/IP where the device was found |
| 57 | +- `local_devices`: List of local device files created (for attach operations) |
| 58 | + |
| 59 | +### Error Response |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "status": "error", |
| 64 | + "message": "Error description" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +**Status values:** |
| 69 | +- `"error"`: General error (invalid request, connection error, etc.) |
| 70 | +- `"not_found"`: No device matching the criteria was found |
| 71 | +- `"multiple_matches"`: Multiple devices matched and `first` was not set to `true` |
| 72 | + |
| 73 | +## Usage Examples |
| 74 | + |
| 75 | +### Using netcat |
| 76 | + |
| 77 | +Attach a device: |
| 78 | +```bash |
| 79 | +echo '{"command":"attach","desc":"Arduino","first":true}' | nc -U /tmp/usb-remote-client.sock |
| 80 | +``` |
| 81 | + |
| 82 | +Detach a device by bus ID: |
| 83 | +```bash |
| 84 | +echo '{"command":"detach","id":"1-1.4"}' | nc -U /tmp/usb-remote-client.sock |
| 85 | +``` |
| 86 | + |
| 87 | +### Using Python |
| 88 | + |
| 89 | +```python |
| 90 | +import json |
| 91 | +import socket |
| 92 | + |
| 93 | +# Connect to the client service socket |
| 94 | +sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 95 | +sock.connect("/tmp/usb-remote-client.sock") |
| 96 | + |
| 97 | +# Send attach request |
| 98 | +request = { |
| 99 | + "command": "attach", |
| 100 | + "desc": "Arduino", |
| 101 | + "first": True |
| 102 | +} |
| 103 | +sock.sendall(json.dumps(request).encode() + b'\n') |
| 104 | + |
| 105 | +# Receive response |
| 106 | +response = sock.recv(4096).decode() |
| 107 | +result = json.loads(response) |
| 108 | + |
| 109 | +if result["status"] == "success": |
| 110 | + print(f"Attached device: {result['data']['description']}") |
| 111 | + print(f"Local devices: {result['local_devices']}") |
| 112 | +else: |
| 113 | + print(f"Error: {result['message']}") |
| 114 | + |
| 115 | +sock.close() |
| 116 | +``` |
| 117 | + |
| 118 | +### Using curl (with socat) |
| 119 | + |
| 120 | +You can use `socat` to create a bridge between HTTP and the Unix socket for testing: |
| 121 | + |
| 122 | +```bash |
| 123 | +# Terminal 1: Start socat bridge |
| 124 | +socat TCP-LISTEN:8080,reuseaddr,fork UNIX-CONNECT:/tmp/usb-remote-client.sock |
| 125 | + |
| 126 | +# Terminal 2: Send requests via HTTP |
| 127 | +curl -X POST http://localhost:8080 -d '{"command":"attach","desc":"Arduino","first":true}' |
| 128 | +``` |
| 129 | + |
| 130 | +## See Also |
| 131 | + |
| 132 | +- [Client Service Socket API](client_service_api.md) - Client service API documentation |
| 133 | +- [Server Setup](../how-to/server_setup.md) - Server installation and configuration |
| 134 | +- [Architecture](architecture.md) - System architecture overview |
0 commit comments