Beispiel #1
0
    def _connect_inner(self, link):
        try:
            if self.address is '':
                ports = link.scan()
                if not link.scanned.is_set():
                    link.scanned.wait(link.timeout)
                if not ports:
                    raise Exception('No devices found')
                if isinstance(ports[0], tuple):
                    self.address = ports[0][1]
                else:
                    self.address = ports[0]
                link.open(self.address)
            elif self.address is not '':
                link.open(self.address)

            transport = None
            if self.transport == 'ninebot':
                from py9b.transport.ninebot import NinebotTransport
                transport = NinebotTransport(link)
            elif self.transport == 'xiaomi':
                from py9b.transport.xiaomi import XiaomiTransport
                transport = XiaomiTransport(link)

                if transport.execute(
                        ReadRegs(BT.ESC, 0x68,
                                 "<H"))[0] > 0x081 and self.link is ('ble'):
                    transport.keys = link.fetch_keys()
                    transport.recover_keys()
                    tprint('Keys recovered')
                    tprint('functionality may be limited for now')

            self._tran = transport
            self._link = link

            if not self._link.connected.is_set():
                self._link.connected.wait(self._link.timeout * 1.5)

            if self._link.connected.is_set():
                self.update_state('connected')
            else:
                self.disconnect()

            return transport

        except Exception as exc:
            self.dispatch('on_error', repr(exc))
            raise exc
Beispiel #2
0
    def __enter__(self):
        link = None
        if self.link == 'bleak':
            from py9b.link.bleak import BleakLink
            link = BleakLink()
        elif self.link == 'tcp':
            from py9b.link.tcp import TCPLink
            link = TCPLink()
        elif self.link == 'serial':
            from py9b.link.serial import SerialLink
            link = SerialLink(timeout=1.0)

        link.__enter__()

        if not self.address:
            ports = link.scan()
            if not ports:
                raise Exception('No devices found')
            self.address = ports[0]

        link.open(self.address)

        transport = None
        if self.transport == 'ninebot':
            from py9b.transport.ninebot import NinebotTransport
            transport = NinebotTransport(link)

        elif self.transport == 'xiaomi':
            from py9b.transport.xiaomi import XiaomiTransport
            transport = XiaomiTransport(link)

            if transport.execute(
                    ReadRegs(BT.ESC, 0x68,
                             "<H"))[0] > 0x081 and self.link.startswith('ble'):
                transport.keys = link.fetch_keys_pro()
                transport.recover_keys()
                print('Keys recovered')

        self._transport = transport
        self._link = link

        return transport
Beispiel #3
0
    def _connect_inner(self, link):
        try:
            if not self.address:
                if platform != 'android':
                    ports = link.scan()
                    if not ports:
                        raise Exception('No devices found')
                    if isinstance(ports[0], tuple):
                        self.address = ports[0][1]
                    else:
                        self.address = ports[0]
                link.open(self.address)
            elif self.address:
                link.open(self.address)

            transport = None
            if self.transport == 'ninebot':
                from py9b.transport.ninebot import NinebotTransport
                transport = NinebotTransport(link)
            elif self.transport == 'xiaomi':
                from py9b.transport.xiaomi import XiaomiTransport
                transport = XiaomiTransport(link)

                if transport.execute(
                        ReadRegs(BT.ESC, 0x68,
                                 "<H"))[0] > 0x081 and self.link is ('ble'):
                    transport.keys = link.fetch_keys_pro()
                    transport.recover_keys()
                    tprint('Keys recovered')

            self._tran = transport
            self._link = link

            return transport

        except Exception as exc:
            self.update_state('disconnected')
            self.dispatch('on_error', repr(exc))
            raise exc
Beispiel #4
0
from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT
from py9b.transport.xiaomi import XiaomiTransport

READ_CHUNK_SIZE = 0x10

link = SerialLink(dump=True)
# link = TCPLink()
# link = BLELink()

with link:
    print("Scanning...")
    ports = link.scan()
    print(ports)

    tran = XiaomiTransport(link)

    # link.open(("127.0.0.1", 6000))
    link.open(ports[0][1])
    print("Connected")

    req = PKT(src=BT.HOST,
              dst=BT.BMS,
              cmd=0x01,
              arg=0,
              data=chr(READ_CHUNK_SIZE))

    hfo = open("BmsRegs.bin", "wb")
    for i in xrange(0x0, 0x100, READ_CHUNK_SIZE):
        print(".")
        req.arg = i >> 1
Beispiel #5
0
from py9b.command.custom import ReadMem

ADDR = 0x1000
SIZE = 0x800
READ_CHUNK_SIZE = 0x10

link = SerialLink(dump=True)
# link = TCPLink()
# link = BLELink()

with link:
    print("Scanning...")
    ports = link.scan()
    print(ports)

    tran = XiaomiTransport(link)

    # link.open(("127.0.0.1", 6000))
    link.open(ports[0][1])
    print("Connected")

    hfo = open("BmsEep.bin", "wb")
    for i in xrange(ADDR, ADDR + SIZE, READ_CHUNK_SIZE):
        print(".")
        for retry in xrange(5):
            try:
                data = tran.execute(ReadMem(BT.BMS, i, "16s"))[0]
            except LinkTimeoutException:
                continue
            break
        else:
Beispiel #6
0
from py9b.link.bleak import BleakLink

from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT
from py9b.transport.xiaomi import XiaomiTransport

from py9b.command.regio import ReadRegs

import time

link = BleakLink()
with link:
    devs = link.scan()
    print(devs)

    tran = XiaomiTransport(link)

    link.open(devs[0])

    data = tran.execute(ReadRegs(BT.ESC, 0x68, "<H"))[0]
    print("BLE version: %04x" % data)

    if data >= 0x81:
        print("Connected, fetching keys...")
        keys = link.fetch_keys()
        tran.keys = keys
        print("keys:", keys)

        # Recover longer keystream
        req = PKT(src=BT.HOST,
                  dst=BT.BMS,