-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsdp.py
More file actions
257 lines (202 loc) · 7.88 KB
/
Copy pathlsdp.py
File metadata and controls
257 lines (202 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
"""
LSDP (Lenbrook Service Discovery Protocol) implementation.
Uses UDP broadcast on port 11430 for more reliable device discovery.
Copyright 2025 tbaur
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import socket
import struct
import time
import random
import logging
from typing import Dict, List, Set, Optional, Tuple
from dataclasses import dataclass
from constants import BLUOS_PORT
from validators import validate_ip, sanitize_ip
logger = logging.getLogger("BluOS")
# LSDP Constants
LSDP_PORT = 11430
LSDP_MAGIC = b"LSDP"
LSDP_VERSION = 1
# Class IDs (from BluOS API v1.7)
CLASS_BLUOS_PLAYER = 0x0001
CLASS_BLUOS_SERVER = 0x0002
CLASS_BLUOS_PLAYER_SECONDARY = 0x0003
CLASS_BLUOS_PLAYER_PAIR_SLAVE = 0x0006
CLASS_BLUOS_HUB = 0x0008
CLASS_ALL = 0xFFFF
# Message Types
MSG_QUERY = 0x51 # 'Q'
MSG_QUERY_UNICAST = 0x52 # 'R'
MSG_ANNOUNCE = 0x41 # 'A'
MSG_DELETE = 0x44 # 'D'
@dataclass
class LSDPDevice:
"""Represents a device discovered via LSDP."""
node_id: str
ip: str
class_id: int
txt_records: Dict[str, str]
class LSDPDiscovery:
"""LSDP discovery implementation."""
def __init__(self, timeout: int = 5):
self.timeout = timeout
self.discovered_devices: Dict[str, LSDPDevice] = {}
def discover(self, class_ids: Optional[List[int]] = None) -> List[str]:
"""
Discover devices using LSDP.
Args:
class_ids: List of class IDs to query (None = all BluOS players)
Returns:
List of discovered IP addresses
"""
if class_ids is None:
class_ids = [CLASS_BLUOS_PLAYER, CLASS_BLUOS_PLAYER_SECONDARY, CLASS_BLUOS_HUB]
self.discovered_devices.clear()
try:
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.settimeout(self.timeout)
# Send initial query packets (7 packets as per spec)
query_packet = self._build_query_packet(class_ids)
for i in [0, 1, 2, 3, 5, 7, 10]:
delay = i + random.uniform(0, 0.25)
time.sleep(delay)
try:
sock.sendto(query_packet, ('<broadcast>', LSDP_PORT))
except OSError as e:
logger.debug(f"LSDP query send error: {e}")
# Listen for responses
end_time = time.time() + self.timeout
while time.time() < end_time:
try:
sock.settimeout(end_time - time.time())
data, addr = sock.recvfrom(4096)
self._parse_packet(data, addr[0])
except socket.timeout:
break
except OSError as e:
logger.debug(f"LSDP receive error: {e}")
break
sock.close()
except Exception as e:
logger.error(f"LSDP discovery error: {e}")
# Extract and validate IPs
valid_ips = []
for device in self.discovered_devices.values():
sanitized_ip = sanitize_ip(device.ip)
if sanitized_ip:
valid_ips.append(sanitized_ip)
return sorted(list(set(valid_ips)))
def _build_query_packet(self, class_ids: List[int]) -> bytes:
"""Build LSDP query packet."""
# Packet header
header = struct.pack('!B', 6) # Header length
header += LSDP_MAGIC
header += struct.pack('!B', LSDP_VERSION)
# Query message
msg_length = 3 + (len(class_ids) * 2) # Length + Type + Count + classes
query = struct.pack('!B', msg_length)
query += struct.pack('!B', MSG_QUERY)
query += struct.pack('!B', len(class_ids))
for class_id in class_ids:
query += struct.pack('!H', class_id)
return header + query
def _parse_packet(self, data: bytes, source_ip: str) -> None:
"""Parse LSDP packet and extract device information."""
if len(data) < 6:
return
# Check magic word
if data[1:5] != LSDP_MAGIC:
return
# Parse version
version = data[5]
if version != LSDP_VERSION:
logger.debug(f"Unsupported LSDP version: {version}")
return
# Parse messages (skip header)
offset = data[0] # Header length
while offset < len(data):
if offset + 1 > len(data):
break
msg_length = data[offset]
if offset + msg_length > len(data):
break
msg_type = data[offset + 1]
if msg_type == MSG_ANNOUNCE:
self._parse_announce(data[offset:offset + msg_length], source_ip)
offset += msg_length
def _parse_announce(self, data: bytes, source_ip: str) -> None:
"""Parse LSDP announce message."""
if len(data) < 3:
return
offset = 2 # Skip length and type
node_id_len = data[offset]
offset += 1
if offset + node_id_len > len(data):
return
node_id = data[offset:offset + node_id_len].decode('utf-8', errors='ignore')
offset += node_id_len
if offset >= len(data):
return
addr_len = data[offset]
offset += 1
if addr_len == 4 and offset + 4 <= len(data):
# IPv4 address
ip_bytes = data[offset:offset + 4]
ip = '.'.join(str(b) for b in ip_bytes)
offset += 4
else:
ip = source_ip # Fallback to source IP
offset += addr_len
if offset >= len(data):
return
count = data[offset]
offset += 1
# Parse announce records
for _ in range(count):
if offset + 2 > len(data):
break
class_id = struct.unpack('!H', data[offset:offset + 2])[0]
offset += 2
if offset >= len(data):
break
txt_count = data[offset]
offset += 1
txt_records = {}
for _ in range(txt_count):
if offset >= len(data):
break
key_len = data[offset]
offset += 1
if offset + key_len > len(data):
break
key = data[offset:offset + key_len].decode('utf-8', errors='ignore')
offset += key_len
if offset >= len(data):
break
val_len = data[offset]
offset += 1
if offset + val_len > len(data):
break
value = data[offset:offset + val_len].decode('utf-8', errors='ignore')
offset += val_len
txt_records[key] = value
# Store device
device_key = f"{node_id}:{class_id}"
self.discovered_devices[device_key] = LSDPDevice(
node_id=node_id,
ip=ip,
class_id=class_id,
txt_records=txt_records
)