Example #1
0
def _dump(ctx, channel, outfile):
    s = yield from util.setup(ctx.obj['device'])
    ctx.obj['ezsp'] = s

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

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

    DLT_IEEE802_15_4 = 195
    pcap = pure_pcapy.Dumper(outfile, 128, DLT_IEEE802_15_4)

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

    def cb(frame_name, response):
        if frame_name == 'mfglibRxHandler':
            data = response[2]
            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))
            pcap.dump(hdr, data)
            ctx.obj['captured'] += 1

    s.add_callback(cb)

    while True:
        yield from asyncio.sleep(1)
Example #2
0
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")
    ctx.obj["start_time"] = time.time()
    ctx.obj["captured"] = 0

    def cb(frame_name, response):
        if frame_name == "mfglibRxHandler":
            data = response[2]
            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))
            pcap.dump(hdr, data)
            ctx.obj["captured"] += 1

    s.add_callback(cb)

    while True:
        await asyncio.sleep(1)
Example #3
0
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 and fcs == b"\x0F\x00":
                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, data)
            except BrokenPipeError:
                done_event.set()

            ctx.obj["captured"] += 1

    s.add_callback(cb)

    await done_event.wait()
Example #4
0
async def steal(device):
    s = await util.setup(device, baudrate=57600)
    eui64 = await getattr(s, 'getEui64')()
    eui64 = bellows.types.named.EmberEUI64(*eui64)

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

    DLT_IEEE802_15_4 = 195
    pcap = pure_pcapy.Dumper("log.pcap", 128, DLT_IEEE802_15_4)
    prompt = Prompt()

    def dumpPcap(frame):
        ts = time.time()
        ts_sec = int(ts)
        ts_usec = int((ts - ts_sec) * 1000000)
        hdr = pure_pcapy.Pkthdr(ts_sec, ts_usec, len(frame), len(frame))
        pcap.dump(hdr, frame)

    def cb(frame_name, response):
        if frame_name != "mfglibRxHandler":
            return

        data = response[2]
        dumpPcap(data)

        if len(data) < 10:  # Not sure what this is, but not a proper response
            return

        resp = interpanZll.ScanResp.deserialize(data)[0]
        if resp.transactionId != transaction_id:  # Not for us
            return

        targets.add(resp.extSrc)
        frame = interpanZll.AckFrame(seq=resp.seq).serialize()
        dumpPcap(frame)
        s.mfglibSendPacket(frame)

    cbid = s.add_callback(cb)

    for channel in range(11, 27):
        print("Scanning on channel", channel)
        v = await s.mfglibSetChannel(channel)
        util.check(v[0], "Unable to set channel")

        transaction_id = randint(0, 0xFFFFFFFF)
        targets = set()

        # https://www.nxp.com/docs/en/user-guide/JN-UG-3091.pdf section 6.8.5
        frame = interpanZll.ScanReq(
            seq=1,
            srcPan=0,
            extSrc=eui64,
            transactionId=transaction_id,
        ).serialize()
        dumpPcap(frame)
        r = await s.mfglibSendPacket(frame)
        util.check(v[0], "Unable to send packet")

        await asyncio.sleep(1)

        while len(targets) > 0:
            target = targets.pop()
            frame = interpanZll.IdentifyReq(
                seq=2,
                srcPan=0,
                extSrc=eui64,
                transactionId=transaction_id,
                extDst=target,
                frameControl=0xCC21,
            ).serialize()
            dumpPcap(frame)
            await s.mfglibSendPacket(frame)
            answer = await prompt(
                "Do you want to factory reset the light that just blinked? [y|n] "
            )

            if answer.strip().lower() == "y":
                print("Factory resetting " + str(target))
                frame = interpanZll.FactoryResetReq(
                    seq=3,
                    srcPan=0,
                    extSrc=eui64,
                    transactionId=transaction_id,
                    extDst=target,
                    frameControl=0xCC21,
                ).serialize()
                dumpPcap(frame)
                await s.mfglibSendPacket(frame)
                await asyncio.sleep(1)

    s.remove_callback(cbid)

    v = await s.mfglibEnd()

    s.close()