pyhubblenetwork 0.7.0__tar.gz → 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/PKG-INFO +1 -1
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/pyproject.toml +1 -1
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/__init__.py +6 -2
- pyhubblenetwork-0.9.0/src/hubblenetwork/ble.py +260 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/cli.py +242 -64
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/cloud.py +7 -1
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/crypto.py +83 -1
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/org.py +26 -10
- pyhubblenetwork-0.9.0/src/hubblenetwork/packets.py +101 -0
- pyhubblenetwork-0.9.0/tests/test_aes_eax.py +473 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_cloud_integration.py +28 -4
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_org_delete_device.py +98 -1
- pyhubblenetwork-0.9.0/tests/test_register_device_cloud.py +91 -0
- pyhubblenetwork-0.9.0/tests/test_register_device_validation.py +87 -0
- pyhubblenetwork-0.9.0/tests/test_unencrypted.py +194 -0
- pyhubblenetwork-0.7.0/src/hubblenetwork/ble.py +0 -195
- pyhubblenetwork-0.7.0/src/hubblenetwork/packets.py +0 -53
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/.gitignore +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/LICENSE +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/README.md +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/plugins/pyhubblenetwork/skills/hubble-ready-test/README.md +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/device.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/errors.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/ready.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/src/hubblenetwork/sat.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/__init__.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/conftest.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_ble_validate.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_cli_payload.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_counter_eid.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_metrics.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_sat.py +0 -0
- {pyhubblenetwork-0.7.0 → pyhubblenetwork-0.9.0}/tests/test_sat_integration.py +0 -0
|
@@ -9,10 +9,10 @@ from . import cloud
|
|
|
9
9
|
from . import ready
|
|
10
10
|
from . import sat
|
|
11
11
|
|
|
12
|
-
from .packets import Location, EncryptedPacket, DecryptedPacket, SatellitePacket
|
|
12
|
+
from .packets import Location, EncryptedPacket, UnencryptedPacket, DecryptedPacket, SatellitePacket, AesEaxPacket, UnknownPacket
|
|
13
13
|
from .device import Device
|
|
14
14
|
from .org import Organization
|
|
15
|
-
from .crypto import decrypt, UNIX_TIME, DEVICE_UPTIME
|
|
15
|
+
from .crypto import decrypt, decrypt_eax, UNIX_TIME, DEVICE_UPTIME
|
|
16
16
|
from .errors import InvalidCredentialsError
|
|
17
17
|
from .cloud import Credentials, Environment
|
|
18
18
|
|
|
@@ -22,11 +22,15 @@ __all__ = [
|
|
|
22
22
|
"ready",
|
|
23
23
|
"sat",
|
|
24
24
|
"decrypt",
|
|
25
|
+
"decrypt_eax",
|
|
25
26
|
"UNIX_TIME",
|
|
26
27
|
"DEVICE_UPTIME",
|
|
27
28
|
"SatellitePacket",
|
|
28
29
|
"Location",
|
|
29
30
|
"EncryptedPacket",
|
|
31
|
+
"UnencryptedPacket",
|
|
32
|
+
"AesEaxPacket",
|
|
33
|
+
"UnknownPacket",
|
|
30
34
|
"DecryptedPacket",
|
|
31
35
|
"Device",
|
|
32
36
|
"Organization",
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# hubblenetwork/ble.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
import asyncio
|
|
4
|
+
import struct
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from typing import Optional, List, Union
|
|
7
|
+
|
|
8
|
+
from bleak import BleakScanner
|
|
9
|
+
|
|
10
|
+
# Import your dataclass
|
|
11
|
+
from .packets import (
|
|
12
|
+
Location,
|
|
13
|
+
EncryptedPacket,
|
|
14
|
+
UnencryptedPacket,
|
|
15
|
+
AesEaxPacket,
|
|
16
|
+
UnknownPacket,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
"""
|
|
20
|
+
16-bit UUID 0xFCA6 in 128-bit Bluetooth Base UUID form
|
|
21
|
+
|
|
22
|
+
Bluetooth spec defines a base UUID 0000xxxx-0000-1000-8000-00805F9B34FB.
|
|
23
|
+
Any 16-bit (or 32-bit) UUID is expanded into that base by substituting xxxx.
|
|
24
|
+
|
|
25
|
+
Libraries normalize to consistent 128-bit strings so you don’t have to guess
|
|
26
|
+
whether a platform will report 16- vs 128-bit in scan results.
|
|
27
|
+
|
|
28
|
+
In bleak, AdvertisementData.service_uuids and the keys in AdvertisementData.service_data
|
|
29
|
+
are 128-bit strings. So matching against the normalized 128-bit form is the most portable.
|
|
30
|
+
"""
|
|
31
|
+
_TARGET_UUID = "0000fca6-0000-1000-8000-00805f9b34fb"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
_FAKE_LOCATION = Location(lat=90, lon=0, fake=True)
|
|
35
|
+
|
|
36
|
+
# ---------------------------------------------------------------------------
|
|
37
|
+
# Unencrypted protocol parsing (version 1)
|
|
38
|
+
# ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
_NETWORK_ID_MASK = (1 << 34) - 1
|
|
41
|
+
|
|
42
|
+
HubblePacket = Union[EncryptedPacket, UnencryptedPacket, AesEaxPacket, UnknownPacket]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def parse_unencrypted(data: bytes) -> Optional[tuple]:
|
|
46
|
+
"""Parse unencrypted protocol service data bytes.
|
|
47
|
+
|
|
48
|
+
Returns (protocol_version, network_id, payload) or None if *data* does
|
|
49
|
+
not look like an unencrypted-protocol advertisement (e.g. version == 0
|
|
50
|
+
means it is an encrypted packet).
|
|
51
|
+
"""
|
|
52
|
+
if len(data) < 5:
|
|
53
|
+
return None
|
|
54
|
+
header = int.from_bytes(data[0:5], "big")
|
|
55
|
+
version = header >> 34
|
|
56
|
+
if version == 0:
|
|
57
|
+
return None
|
|
58
|
+
network_id = header & _NETWORK_ID_MASK
|
|
59
|
+
return (version, network_id, data[5:])
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
_AES_EAX_MIN_SIZE = 15 # version(1) + salt(2) + EID(8) + tag(4)
|
|
63
|
+
_AES_EAX_TAG_SIZE = 4
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _make_packet(raw: bytes, rssi: int) -> HubblePacket:
|
|
67
|
+
"""Build the right packet type from raw service data bytes."""
|
|
68
|
+
ts = int(datetime.now(timezone.utc).timestamp())
|
|
69
|
+
|
|
70
|
+
if len(raw) < 1:
|
|
71
|
+
return EncryptedPacket(
|
|
72
|
+
timestamp=ts, location=_FAKE_LOCATION, payload=raw, rssi=rssi
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
version = raw[0] >> 2
|
|
76
|
+
|
|
77
|
+
if version == 0:
|
|
78
|
+
# AES-CTR advertisement layout: seq_no (2) | EID (4) | auth_tag (4) | ciphertext.
|
|
79
|
+
eid = int.from_bytes(raw[2:6], "big") if len(raw) >= 6 else None
|
|
80
|
+
auth_tag = bytes(raw[6:10]) if len(raw) >= 10 else None
|
|
81
|
+
return EncryptedPacket(
|
|
82
|
+
timestamp=ts,
|
|
83
|
+
location=_FAKE_LOCATION,
|
|
84
|
+
payload=raw,
|
|
85
|
+
rssi=rssi,
|
|
86
|
+
protocol_version=version,
|
|
87
|
+
eid=eid,
|
|
88
|
+
auth_tag=auth_tag,
|
|
89
|
+
)
|
|
90
|
+
elif version == 1:
|
|
91
|
+
parsed = parse_unencrypted(raw)
|
|
92
|
+
if parsed is not None:
|
|
93
|
+
ver, network_id, customer_payload = parsed
|
|
94
|
+
return UnencryptedPacket(
|
|
95
|
+
timestamp=ts,
|
|
96
|
+
location=_FAKE_LOCATION,
|
|
97
|
+
network_id=network_id,
|
|
98
|
+
protocol_version=ver,
|
|
99
|
+
payload=customer_payload,
|
|
100
|
+
rssi=rssi,
|
|
101
|
+
)
|
|
102
|
+
elif version == 2 and len(raw) >= _AES_EAX_MIN_SIZE:
|
|
103
|
+
nonce_salt = raw[1:3]
|
|
104
|
+
eid = struct.unpack("<Q", raw[3:11])[0]
|
|
105
|
+
auth_tag = raw[-_AES_EAX_TAG_SIZE:]
|
|
106
|
+
payload = raw[11:-_AES_EAX_TAG_SIZE] if len(raw) > _AES_EAX_MIN_SIZE else b""
|
|
107
|
+
return AesEaxPacket(
|
|
108
|
+
timestamp=ts,
|
|
109
|
+
location=_FAKE_LOCATION,
|
|
110
|
+
protocol_version=version,
|
|
111
|
+
nonce_salt=nonce_salt,
|
|
112
|
+
eid=eid,
|
|
113
|
+
payload=payload,
|
|
114
|
+
auth_tag=auth_tag,
|
|
115
|
+
rssi=rssi,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
return UnknownPacket(
|
|
119
|
+
timestamp=ts,
|
|
120
|
+
location=_FAKE_LOCATION,
|
|
121
|
+
protocol_version=version,
|
|
122
|
+
payload=raw,
|
|
123
|
+
rssi=rssi,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _extract_hubble_service_data(adv_data) -> Optional[tuple]:
|
|
128
|
+
"""Extract Hubble service data payload and RSSI from a BLE advertisement.
|
|
129
|
+
|
|
130
|
+
Returns (payload_bytes, rssi) or None if UUID 0xFCA6 not present.
|
|
131
|
+
"""
|
|
132
|
+
service_data = getattr(adv_data, "service_data", None) or {}
|
|
133
|
+
for uuid_str, data in service_data.items():
|
|
134
|
+
if (uuid_str or "").lower() == _TARGET_UUID:
|
|
135
|
+
return (bytes(data), int(getattr(adv_data, "rssi", 0) or 0))
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# ---------------------------------------------------------------------------
|
|
140
|
+
# Scanning (auto-detects encrypted vs unencrypted)
|
|
141
|
+
# ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def _scan_async(ttl: float) -> List[HubblePacket]:
|
|
145
|
+
"""Async implementation of BLE scan."""
|
|
146
|
+
done = asyncio.Event()
|
|
147
|
+
packets: List[HubblePacket] = []
|
|
148
|
+
|
|
149
|
+
def on_detect(device, adv_data) -> None:
|
|
150
|
+
nonlocal packets
|
|
151
|
+
extracted = _extract_hubble_service_data(adv_data)
|
|
152
|
+
if extracted is not None:
|
|
153
|
+
payload, rssi = extracted
|
|
154
|
+
packets.append(_make_packet(payload, rssi))
|
|
155
|
+
|
|
156
|
+
async with BleakScanner(detection_callback=on_detect):
|
|
157
|
+
try:
|
|
158
|
+
await asyncio.wait_for(done.wait(), timeout=ttl)
|
|
159
|
+
except asyncio.TimeoutError:
|
|
160
|
+
pass
|
|
161
|
+
|
|
162
|
+
return packets
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def scan(timeout: float) -> List[HubblePacket]:
|
|
166
|
+
"""
|
|
167
|
+
Scan for BLE advertisements that include service data for UUID 0xFCA6.
|
|
168
|
+
Automatically detects encrypted vs unencrypted protocol packets.
|
|
169
|
+
|
|
170
|
+
For async environments (e.g., Jupyter), use scan_async() instead.
|
|
171
|
+
"""
|
|
172
|
+
try:
|
|
173
|
+
return asyncio.run(_scan_async(timeout))
|
|
174
|
+
except RuntimeError:
|
|
175
|
+
try:
|
|
176
|
+
loop = asyncio.get_running_loop()
|
|
177
|
+
except RuntimeError:
|
|
178
|
+
loop = asyncio.new_event_loop()
|
|
179
|
+
asyncio.set_event_loop(loop)
|
|
180
|
+
try:
|
|
181
|
+
return loop.run_until_complete(_scan_async(timeout))
|
|
182
|
+
finally:
|
|
183
|
+
loop.close()
|
|
184
|
+
raise RuntimeError(
|
|
185
|
+
"Cannot run synchronous BLE scan inside an existing async event loop. "
|
|
186
|
+
"Use 'await ble.scan_async()' or install 'nest_asyncio' for Jupyter support."
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
async def scan_async(timeout: float) -> List[HubblePacket]:
|
|
191
|
+
"""
|
|
192
|
+
Async version of scan() for use in async environments like Jupyter notebooks.
|
|
193
|
+
|
|
194
|
+
Usage:
|
|
195
|
+
packets = await ble.scan_async(timeout=5.0)
|
|
196
|
+
"""
|
|
197
|
+
return await _scan_async(timeout)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
async def _scan_single_async(ttl: float) -> Optional[HubblePacket]:
|
|
201
|
+
"""Async implementation for scanning a single BLE packet."""
|
|
202
|
+
done = asyncio.Event()
|
|
203
|
+
packet: Optional[HubblePacket] = None
|
|
204
|
+
|
|
205
|
+
def on_detect(device, adv_data) -> None:
|
|
206
|
+
nonlocal packet
|
|
207
|
+
|
|
208
|
+
if packet is not None:
|
|
209
|
+
return
|
|
210
|
+
|
|
211
|
+
extracted = _extract_hubble_service_data(adv_data)
|
|
212
|
+
if extracted is None:
|
|
213
|
+
return
|
|
214
|
+
|
|
215
|
+
payload, rssi = extracted
|
|
216
|
+
packet = _make_packet(payload, rssi)
|
|
217
|
+
done.set()
|
|
218
|
+
|
|
219
|
+
async with BleakScanner(detection_callback=on_detect):
|
|
220
|
+
try:
|
|
221
|
+
await asyncio.wait_for(done.wait(), timeout=ttl)
|
|
222
|
+
except asyncio.TimeoutError:
|
|
223
|
+
pass
|
|
224
|
+
|
|
225
|
+
return packet
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def scan_single(timeout: float) -> Optional[HubblePacket]:
|
|
229
|
+
"""
|
|
230
|
+
Scan for a BLE advertisement that includes service data for UUID 0xFCA6
|
|
231
|
+
and return it. Automatically detects encrypted vs unencrypted protocol.
|
|
232
|
+
|
|
233
|
+
For async environments (e.g., Jupyter), use scan_single_async() instead.
|
|
234
|
+
"""
|
|
235
|
+
try:
|
|
236
|
+
return asyncio.run(_scan_single_async(timeout))
|
|
237
|
+
except RuntimeError:
|
|
238
|
+
try:
|
|
239
|
+
loop = asyncio.get_running_loop()
|
|
240
|
+
except RuntimeError:
|
|
241
|
+
loop = asyncio.new_event_loop()
|
|
242
|
+
asyncio.set_event_loop(loop)
|
|
243
|
+
try:
|
|
244
|
+
return loop.run_until_complete(_scan_single_async(timeout))
|
|
245
|
+
finally:
|
|
246
|
+
loop.close()
|
|
247
|
+
raise RuntimeError(
|
|
248
|
+
"Cannot run synchronous BLE scan inside an existing async event loop. "
|
|
249
|
+
"Use 'await ble.scan_single_async()' or install 'nest_asyncio' for Jupyter support."
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
async def scan_single_async(timeout: float) -> Optional[HubblePacket]:
|
|
254
|
+
"""
|
|
255
|
+
Async version of scan_single() for use in async environments like Jupyter notebooks.
|
|
256
|
+
|
|
257
|
+
Usage:
|
|
258
|
+
packet = await ble.scan_single_async(timeout=5.0)
|
|
259
|
+
"""
|
|
260
|
+
return await _scan_single_async(timeout)
|