def do_blescan(self, line): """ ble-scan Start BLE scanning operations. """ if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.scan(line) return
def do_bledisconnect(self, line): """ ble-disconnect Disconnect from a BLE peripheral. """ if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.disconnect() return
def do_blescanconnect(self, line): """ ble-scan-connect Scan and connect to a BLE peripheral identified by line. """ if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.scan_connect(line) return
def __init__(self, rendezvousAddr=None, controllerNodeId=0, bluetoothAdapter=None): self.lastNetworkId = None Cmd.__init__(self) Cmd.identchars = string.ascii_letters + string.digits + "-" if sys.stdin.isatty(): self.prompt = "chip-device-ctrl > " else: self.use_rawinput = 0 self.prompt = "" DeviceMgrCmd.command_names.sort() self.bleMgr = None self.devCtrl = ChipDeviceCtrl.ChipDeviceController( controllerNodeId=controllerNodeId, bluetoothAdapter=bluetoothAdapter) self.commissionableNodeCtrl = ChipCommissionableNodeCtrl.ChipCommissionableNodeController() # If we are on Linux and user selects non-default bluetooth adapter. if sys.platform.startswith("linux") and (bluetoothAdapter is not None): try: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.ble_adapter_select( "hci{}".format(bluetoothAdapter)) except Exception as ex: traceback.print_exc() print( "Failed to initialize BLE, if you don't have BLE, run chip-device-ctrl with --no-ble") raise ex self.historyFileName = os.path.expanduser( "~/.chip-device-ctrl-history") try: import readline if "libedit" in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") readline.set_completer_delims(" ") try: readline.read_history_file(self.historyFileName) except IOError: pass except ImportError: pass
def do_bleadapterprint(self, line): """ ble-adapter-print Print attached BLE adapter. """ if sys.platform.startswith("linux"): if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.ble_adapter_print() else: print("ble-adapter-print only works in Linux") return
def do_bleconnect(self, line): """ ble-connect <device-name> ble-connect <mac-address (linux only)> ble-connect <device-uuid> ble-connect <discriminator> Connect to a BLE peripheral identified by line. """ if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.connect(line) return
def do_bleadapterselect(self, line): """ ble-adapter-select Start BLE adapter select. """ if sys.platform.startswith("linux"): if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.ble_adapter_select(line) else: print( "ble-adapter-select only works in Linux, ble-adapter-select mac_address" ) return
def scan_chip_ble_devices(devCtrl): """ BLE scan CHIP device BLE scanning for 10 seconds and collect the results :param devCtrl: device controller instance :return: List of visible BLE devices """ devices = [] bleMgr = BleManager(devCtrl) bleMgr.scan("-t 10") for device in bleMgr.peripheral_list: devIdInfo = bleMgr.get_peripheral_devIdInfo(device) if devIdInfo: devInfo = devIdInfo.__dict__ devInfo["name"] = device.Name devices.append(devInfo) return devices
def do_bleadapterselect(self, line): """ ble-adapter-select Start BLE adapter select, deprecated, you can select adapter by command line arguments. """ if sys.platform.startswith("linux"): if not self.bleMgr: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.ble_adapter_select(line) print( "This change only applies to ble-scan\n" "Please run device controller with --bluetooth-adapter=<adapter-name> to select adapter\n" + "e.g. chip-device-ctrl --bluetooth-adapter hci0") else: print( "ble-adapter-select only works in Linux, ble-adapter-select mac_address" ) return
def __init__(self, rendezvousAddr=None, controllerNodeId=1, bluetoothAdapter=None): self.lastNetworkId = None self.replHint = None pretty.install(indent_guides=True, expand_all=True) coloredlogs.install(level='DEBUG') chip.logging.RedirectToPythonLogging() logging.getLogger().setLevel(logging.DEBUG) warnings.showwarning = ShowColoredWarnings Cmd.__init__(self) Cmd.identchars = string.ascii_letters + string.digits + "-" if sys.stdin.isatty(): self.prompt = "chip-device-ctrl > " else: self.use_rawinput = 0 self.prompt = "" DeviceMgrCmd.command_names.sort() self.bleMgr = None self.chipStack = ChipStack.ChipStack( bluetoothAdapter=bluetoothAdapter, persistentStoragePath='/tmp/chip-device-ctrl-storage.json') self.fabricAdmin = FabricAdmin.FabricAdmin(0xFFF1) self.devCtrl = self.fabricAdmin.NewController(nodeId=controllerNodeId, useTestCommissioner=True) self.commissionableNodeCtrl = ChipCommissionableNodeCtrl.ChipCommissionableNodeController( self.chipStack) # If we are on Linux and user selects non-default bluetooth adapter. if sys.platform.startswith("linux") and (bluetoothAdapter is not None): try: self.bleMgr = BleManager(self.devCtrl) self.bleMgr.ble_adapter_select( "hci{}".format(bluetoothAdapter)) except Exception as ex: traceback.print_exc() print( "Failed to initialize BLE, if you don't have BLE, run chip-device-ctrl with --no-ble" ) raise ex self.historyFileName = os.path.expanduser( "~/.chip-device-ctrl-history") try: import readline if "libedit" in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") readline.set_completer_delims(" ") try: readline.read_history_file(self.historyFileName) except IOError: pass except ImportError: pass
def bleManager(self): if self._bleManager is None: self._bleManager = BleManager(self.deviceController) return self._bleManager