Skip to content

Commit

Permalink
Remove bellows dump and bellows scan (#665)
Browse files Browse the repository at this point in the history
* Remove `bellows dump`

* Drop `bellows scan`

* Test out zigpy/workflows#27
  • Loading branch information
puddly authored Jan 23, 2025
1 parent 7d0d18f commit be9a3ac
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
shared-ci:
uses: zigpy/workflows/.github/workflows/ci.yml@main
uses: zigpy/workflows/.github/workflows/ci.yml@puddly/no-codecov-upload-on-fork
with:
CODE_FOLDER: bellows
CACHE_VERSION: 2
Expand Down
82 changes: 5 additions & 77 deletions bellows/cli/dump.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import asyncio
import logging
import time

import click
import pure_pcapy

from . import util
from .main import main

LOGGER = logging.getLogger(__name__)
Expand All @@ -25,76 +21,8 @@
@click.pass_context
def dump(ctx, channel, outfile):
"""Capture frames on CHANNEL and write to FILE in tcpdump format"""
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(_dump(ctx, channel, outfile))
except KeyboardInterrupt:
captured = ctx.obj.get("captured", 0)
start_time = ctx.obj.get("start_time", None)
if start_time:
duration = time.time() - start_time
click.echo(f"\nCaptured {captured} frames in {duration:0.2f}s", err=True)
finally:
if "ezsp" in ctx.obj:
loop.run_until_complete(ctx.obj["ezsp"].mfglibEnd())
loop.run_until_complete(ctx.obj["ezsp"].disconnect())


def ieee_15_4_fcs(data: bytes) -> bytes:
# Modified from the implementation in `scapy.layers.dot15d4:Dot15d4FCS.compute_fcs`
crc = 0x0000

for c in data:
q = (crc ^ c) & 15 # Do low-order 4 bits
crc = (crc // 16) ^ (q * 0x1081)

q = (crc ^ (c // 16)) & 15 # And high 4 bits
crc = (crc // 16) ^ (q * 0x1081)

return crc.to_bytes(2, "little")


async def _dump(ctx, channel, outfile):
s = await util.setup(ctx.obj["device"], ctx.obj["baudrate"])
ctx.obj["ezsp"] = s

v = await s.mfglibStart(True)
util.check(v[0], "Unable to start mfglib")

v = await s.mfglibSetChannel(channel)
util.check(v[0], "Unable to set channel")

pcap = pure_pcapy.Dumper(outfile, 128, 195) # DLT_IEEE_15_4

click.echo("Capture started", err=True)
ctx.obj["start_time"] = time.time()
ctx.obj["captured"] = 0

done_event = asyncio.Event()

def cb(frame_name, response):
if frame_name == "mfglibRxHandler":
data = response[2]

# Later releases of EmberZNet incorrectly use a static FCS
fcs = data[-2:]
if s.ezsp_version >= 8:
computed_fcs = ieee_15_4_fcs(data[0:-2])
LOGGER.debug("Fixing FCS (expected %s, got %s)", computed_fcs, fcs)
data = data[0:-2] + computed_fcs

ts = time.time()
ts_sec = int(ts)
ts_usec = int((ts - ts_sec) * 1000000)
hdr = pure_pcapy.Pkthdr(ts_sec, ts_usec, len(data), len(data))

try:
pcap.dump(hdr, bytes(data))
except BrokenPipeError:
done_event.set()

ctx.obj["captured"] += 1

s.add_callback(cb)

await done_event.wait()
click.echo(
"`bellows dump` has been deprecated. Please use the `packet-capture` command from zigpy-cli.",
err=True,
)
ctx.exit(1)
27 changes: 5 additions & 22 deletions bellows/cli/network.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import functools
import logging
import math

import click
from zigpy.config import SCHEMA_NETWORK
Expand Down Expand Up @@ -137,24 +136,8 @@ async def leave(ctx):
@util.background
async def scan(ctx, channels, duration_ms, energy_scan):
"""Scan for networks or radio interference"""
s = await util.setup(ctx.obj["device"], ctx.obj["baudrate"])

channel_mask = util.channel_mask(channels)
click.echo("Scanning channels {}".format(" ".join(map(str, channels))))

# TFM says:
# Sets the exponent of the number of scan periods, where a scan period is
# 960 symbols. The scan will occur for ((2^duration) + 1) scan periods.
# 1 symbol is 16us
duration_symbols = duration_ms / (960 * 0.016)
duration_symbol_exp = max(0, math.ceil(math.log(duration_symbols - 1, 2)))

scan_type = t.EzspNetworkScanType.ACTIVE_SCAN
if energy_scan:
scan_type = t.EzspNetworkScanType.ENERGY_SCAN

v = await s.startScan(scan_type, channel_mask, duration_symbol_exp)
for network in v:
click.echo(network)

await s.disconnect()
click.echo(
"`bellows scan` has been deprecated. Please use the `energy-scan` and `network-scan` commands from zigpy-cli.",
err=True,
)
ctx.exit(1)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ requires-python = ">=3.8"
dependencies = [
"click",
"click-log>=0.2.1",
"pure_pcapy3==1.0.1",
"voluptuous",
"zigpy>=0.75.0",
'async-timeout; python_version<"3.11"',
Expand Down

0 comments on commit be9a3ac

Please sign in to comment.