Skip to content

Commit

Permalink
Discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
aBozowski committed Nov 3, 2023
1 parent 6900a19 commit 4f6644c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/tools/interop/idt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,10 @@ Collect contextually relevant networking info from the local environment and pro
### `probe`
- `probe` contains the base class for (`idt`'s) host platform specific implementation.
- Reuses the dnssd discovery implementation to build probe targets
- Calls platform + addr type specific probe methods for each target
- `linux` and `mac` contain `probe` implementations for each host platform
- Reuses the dnssd discovery implementation to build probe targets.
- Calls platform + addr type specific probe methods for each target.
- `linux` and `mac` contain `probe` implementations for each host platform.
- - The package contains a simple dataclass to represent probe targets.
### Conventions
Expand Down
37 changes: 23 additions & 14 deletions src/tools/interop/idt/discovery/ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

import asyncio
import datetime
import os
import sys
import time

import log
Expand All @@ -38,7 +39,6 @@ def __init__(self, artifact_dir: str):

def parse_vid_pid(self, loggable_data: str) -> str:
try:
self.logger.critical(loggable_data)
vid = loggable_data[8:10] + loggable_data[6:8]
pid = loggable_data[12:14] + loggable_data[10:12]
except IndexError:
Expand Down Expand Up @@ -69,38 +69,47 @@ def handle_device_states(self) -> None:
def log_ble_discovery(
self,
name: str,
bin_data: bytes,
bin_service_data: bytes,
ble_device: BLEDevice,
rssi: int) -> None:
loggable_data = bin_data.hex()
hex_service_data = bin_service_data.hex()
if self.is_matter_device(name):
device_id = f"{ble_device.name}_{ble_device.address}"
self.devices_seen_this_time.add(device_id)
if device_id not in self.devices_seen_last_time:
to_log = f"DISCOVERED\n{ble_device.name} {ble_device.address}"
to_log += f"{name}\n{loggable_data}\n"
to_log = f"DISCOVERED\n"
to_log += f"BLE DEVICE NAME: {ble_device.name}\n"
to_log += f"BLE ADDR: {ble_device.address}\n"
to_log += f"NAME: {name}\n"
to_log += f"HEX SERVICE DATA: {hex_service_data}\n"
to_log += f"RSSI {rssi}\n"
to_log += self.parse_vid_pid(loggable_data)
to_log += self.parse_vid_pid(hex_service_data)
self.write_device_log(device_id, to_log)

async def browse(self, scanner: BleakScanner) -> None:
devices: dict[str, tuple[BLEDevice, AdvertisementData]] = await scanner.discover(return_adv=True)
for device in devices.values():
ble_device = device[0]
ad_data = device[1]
for name, bin_data in ad_data.service_data.items():
for name, bin_service_data in ad_data.service_data.items():
self.log_ble_discovery(
name, bin_data, ble_device, ad_data.rssi)
name, bin_service_data, ble_device, ad_data.rssi)
self.handle_device_states()

async def browse_interactive(self) -> None:
scanner = BleakScanner()
self.logger.warning(
"Scanning BLE\nDCL Lookup: https://webui.dcl.csa-iot.org/")
async def browser_task(self, scanner) -> None:
while True:
try:
time.sleep(self.throttle_seconds)
await asyncio.sleep(self.throttle_seconds)
await self.browse(scanner)
except BleakDBusError as e:
self.logger.critical(e)
time.sleep(self.error_seconds)

async def browse_interactive(self) -> None:
scanner = BleakScanner()
self.logger.warning(
"Scanning BLE\nDCL Lookup: https://webui.dcl.csa-iot.org/\nPress enter to stop!\n")
task = asyncio.create_task(self.browser_task(scanner))
await asyncio.get_event_loop().run_in_executor(
None, sys.stdin.readline)
task.cancel()
2 changes: 1 addition & 1 deletion src/tools/interop/idt/discovery/dnssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def browse_interactive(self) -> None:
zc = Zeroconf()
ServiceBrowser(zc, list(_MDNS_TYPES.keys()), self)
try:
input("Browsing Matter DNS-SD, press enter to stop\n")
input("Browsing Matter DNS-SD, press enter to stop\nDCL Lookup: https://webui.dcl.csa-iot.org/\n")
finally:
zc.close()

Expand Down
2 changes: 2 additions & 0 deletions src/tools/interop/idt/idt.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ def command_discover(self, args: argparse.Namespace) -> None:
safe_mkdir(self.ble_artifact_dir)
scanner = MatterBleScanner(self.ble_artifact_dir)
asyncio.run(scanner.browse_interactive())
self.zip_artifacts()
else:
safe_mkdir(self.dnssd_artifact_dir)
MatterDnssdListener(self.dnssd_artifact_dir).browse_interactive()
self.zip_artifacts()

def zip_artifacts(self) -> None:
zip_basename = os.path.basename(self.artifact_dir)
Expand Down

0 comments on commit 4f6644c

Please sign in to comment.