From 3456569af87fa662aed6a0cd6eb8e1664f466af6 Mon Sep 17 00:00:00 2001 From: Song Guo Date: Wed, 2 Jun 2021 13:15:21 +0800 Subject: [PATCH] [python] Add flag to disable BLE for controller (#7179) --- src/controller/python/chip-device-ctrl.py | 32 ++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index f85732a1628a29..5b0b58d9131460 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -39,6 +39,7 @@ import time import string import re +import traceback from cmd import Cmd from chip.ChipBleUtility import FAKE_CONN_OBJ_VALUE from chip.setup_payload import SetupPayload @@ -151,8 +152,13 @@ def __init__(self, rendezvousAddr=None, controllerNodeId=0, bluetoothAdapter=Non # If we are on Linux and user selects non-default bluetooth adapter. if sys.platform.startswith("linux") and (bluetoothAdapter is not None): - self.bleMgr = BleManager(self.devCtrl) - self.bleMgr.ble_adapter_select("hci{}".format(bluetoothAdapter)) + 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") @@ -743,9 +749,15 @@ def main(): dest="bluetoothAdapter", default="hci0", type="str", - help="Controller bluetooth adapter ID", + help="Controller bluetooth adapter ID, use --no-ble to disable bluetooth functions.", metavar="", ) + optParser.add_option( + "--no-ble", + action="store_true", + dest="disableBluetooth", + help="Disable bluetooth, calling BLE related feature with this flag results in undefined behavior.", + ) (options, remainingArgs) = optParser.parse_args(sys.argv[1:]) if len(remainingArgs) != 0: @@ -754,7 +766,9 @@ def main(): adapterId = None if sys.platform.startswith("linux"): - if not options.bluetoothAdapter.startswith("hci"): + if options.disableBluetooth: + adapterId = None + elif not options.bluetoothAdapter.startswith("hci"): print( "Invalid bluetooth adapter: {}, adapter name looks like hci0, hci1 etc.") sys.exit(-1) @@ -766,8 +780,14 @@ def main(): "Invalid bluetooth adapter: {}, adapter name looks like hci0, hci1 etc.") sys.exit(-1) - devMgrCmd = DeviceMgrCmd(rendezvousAddr=options.rendezvousAddr, - controllerNodeId=options.controllerNodeId, bluetoothAdapter=adapterId) + try: + devMgrCmd = DeviceMgrCmd(rendezvousAddr=options.rendezvousAddr, + controllerNodeId=options.controllerNodeId, bluetoothAdapter=adapterId) + except Exception as ex: + print(ex) + print("Failed to bringup CHIPDeviceController CLI") + sys.exit(1) + print("Chip Device Controller Shell") if options.rendezvousAddr: print("Rendezvous address set to %s" % options.rendezvousAddr)