def test_on_boarding_codes(device): ret = device.send(command="onboardingcodes", expected_output="Done") assert ret != None and len(ret) > 2 boarding_codes = parse_boarding_codes_response(ret[1:-1]) for param, value in boarding_codes.items(): ret = device.send(command="onboardingcodes {}".format(param), expected_output="Done") assert ret != None and len(ret) > 1 assert value == ret[-2].strip() try: device_details = dict(SetupPayload().ParseQrCode( "VP:vendorpayload%{}".format(boarding_codes['qrcode'])).attributes) except exceptions.ChipStackError as ex: log.error(ex.msg) assert False assert device_details != None and len(device_details) != 0 try: device_details = dict(SetupPayload().ParseManualPairingCode( boarding_codes['manualpairingcode']).attributes) except exceptions.ChipStackError as ex: log.error(ex.msg) assert False assert device_details != None and len(device_details) != 0
def do_setuppayload(self, line): """ setup-payload generate [options] Options: -v Vendor ID -p Product ID -cf Custom Flow [Standard = 0, UserActionRequired = 1, Custom = 2] -dc Discovery Capabilities [SoftAP = 1 | BLE = 2 | OnNetwork = 4] -dv Discriminator Value -ps Passcode setup-payload parse-manual <manual-pairing-code> setup-payload parse-qr <qr-code-payload> """ try: arglist = shlex.split(line) if arglist[0] not in ("generate", "parse-manual", "parse-qr"): self.do_help("setup-payload") return if arglist[0] == "generate": parser = argparse.ArgumentParser() parser.add_argument("-v", type=int, default=0, dest='vendorId') parser.add_argument("-p", type=int, default=0, dest='productId') parser.add_argument('-cf', type=int, default=0, dest='customFlow') parser.add_argument("-dc", type=int, default=0, dest='capabilities') parser.add_argument("-dv", type=int, default=0, dest='discriminator') parser.add_argument("-ps", type=int, dest='passcode') args = parser.parse_args(arglist[1:]) SetupPayload().PrintOnboardingCodes( args.passcode, args.vendorId, args.productId, args.discriminator, args.customFlow, args.capabilities) if arglist[0] == "parse-manual": SetupPayload().ParseManualPairingCode(arglist[1]).Print() if arglist[0] == "parse-qr": SetupPayload().ParseQrCode(arglist[1]).Print() except exceptions.ChipStackException as ex: print(str(ex)) return
def do_setuppayload(self, line): """ setup-payload parse-manual <manual-pairing-code> setup-payload parse-qr <qr-code-payload> """ try: args = shlex.split(line) if (len(args) != 2) or (args[0] not in ("parse-manual", "parse-qr")): self.do_help("setup-payload") return if args[0] == "parse-manual": SetupPayload().ParseManualPairingCode(args[1]).Print() if args[0] == "parse-qr": SetupPayload().ParseQrCode(args[1]).Print() except exceptions.ChipStackException as ex: print(str(ex)) return
def do_connect(self, line): """ connect -ip <ip address> <setup pin code> [<nodeid>] connect -ble <discriminator> <setup pin code> [<nodeid>] connect -qr <qr code> [<nodeid>] connect -code <manual pairing code> [<nodeid>] connect command is used for establishing a rendezvous session to the device. currently, only connect using setupPinCode is supported. -qr option will connect to the first device with a matching long discriminator. TODO: Add more methods to connect to device (like cert for auth, and IP for connection) """ try: args = shlex.split(line) if len(args) <= 1: print("Usage:") self.do_help("connect SetupPinCode") return nodeid = random.randint(1, 1000000) # Just a random number if len(args) == 4: nodeid = int(args[3]) print("Device is assigned with nodeid = {}".format(nodeid)) if args[0] == "-ip" and len(args) >= 3: self.devCtrl.ConnectIP(args[1].encode( "utf-8"), int(args[2]), nodeid) elif args[0] == "-ble" and len(args) >= 3: self.devCtrl.ConnectBLE(int(args[1]), int(args[2]), nodeid) elif args[0] in ['-qr', '-code'] and len(args) >= 2: if len(args) == 3: nodeid = int(args[2]) print("Parsing QR code {}".format(args[1])) setupPayload = None if args[0] == '-qr': setupPayload = SetupPayload().ParseQrCode(args[1]) elif args[0] == '-code': setupPayload = SetupPayload( ).ParseManualPairingCode(args[1]) if not int(setupPayload.attributes.get("RendezvousInformation", 0)): print("No rendezvous information provided, default to all.") setupPayload.attributes["RendezvousInformation"] = 0b111 setupPayload.Print() self.ConnectFromSetupPayload(setupPayload, nodeid) else: print("Usage:") self.do_help("connect SetupPinCode") return print( "Device temporary node id (**this does not match spec**): {}".format(nodeid)) except exceptions.ChipStackException as ex: print(str(ex)) return
def do_discover(self, line): """ discover -qr qrcode discover -all discover command is used to discover available devices. """ try: args = shlex.split(line) if len(args) < 1: print("Usage:") self.do_help("discover") return if args[0] == "-qr" and len(args) >= 2: setupPayload = SetupPayload().ParseQrCode(args[1]) longDiscriminator = ctypes.c_uint16( int(setupPayload.attributes['Discriminator'])) self.devCtrl.DiscoverCommissioningLongDiscriminator( longDiscriminator) print("Waiting for device responses...") strlen = 100 addrStrStorage = ctypes.create_string_buffer(strlen) count = 0 maxWaitTime = 2 while (not self.devCtrl.GetIPForDiscoveredDevice( 0, addrStrStorage, strlen) and count < maxWaitTime): time.sleep(0.2) count = count + 0.2 elif args[0] == "-all": self.devCtrl.DiscoverAllCommissioning() # Discovery happens through mdns, which means we need to wait for responses to come back. # TODO(cecille): I suppose we could make this a command line arg. Or Add a callback when # x number of responses are received. For now, just 2 seconds. We can all wait that long. print("Waiting for device responses...") time.sleep(2) else: print("Usage:") self.do_help("discover") return except exceptions.ChipStackException as ex: print('exception') print(str(ex)) return self.devCtrl.PrintDiscoveredDevices()
def get_device_details(device): """ Get device details from logs :param device: serial device instance :return: device details dictionary or None """ ret = device.wait_for_output("SetupQRCode") if ret == None or len(ret) < 2: return None qr_code = re.sub(r"[\[\]]", "", ret[-1].partition("SetupQRCode:")[2]).strip() try: device_details = dict(SetupPayload().ParseQrCode( "VP:vendorpayload%{}".format(qr_code)).attributes) except exceptions.ChipStackError as ex: log.error(ex.msg) return None return device_details
def do_discover(self, line): """ discover -qr qrcode discover -all discover -l long_discriminator discover -s short_discriminator discover -v vendor_id discover -t device_type discover -c commissioning_enabled discover -a discover command is used to discover available devices. """ try: arglist = shlex.split(line) if len(arglist) < 1: print("Usage:") self.do_help("discover") return parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument('-all', help='discover all commissionable nodes and commissioners', action='store_true') group.add_argument('-qr', help='discover commissionable nodes matching provided QR code', type=str) group.add_argument('-l', help='discover commissionable nodes with given long discriminator', type=int) group.add_argument('-s', help='discover commissionable nodes with given short discriminator', type=int) group.add_argument('-v', help='discover commissionable nodes wtih given vendor ID', type=int) group.add_argument('-t', help='discover commissionable nodes with given device type', type=int) group.add_argument('-c', help='discover commissionable nodes with given commissioning mode', type=int) group.add_argument('-a', help='discover commissionable nodes put in commissioning mode from command', action='store_true') args=parser.parse_args(arglist) if args.all: self.commissionableNodeCtrl.DiscoverCommissioners() self.wait_for_many_discovered_devices() self.commissionableNodeCtrl.PrintDiscoveredCommissioners() self.devCtrl.DiscoverAllCommissioning() self.wait_for_many_discovered_devices() elif args.qr is not None: setupPayload = SetupPayload().ParseQrCode(args.qr) longDiscriminator = ctypes.c_uint16(int(setupPayload.attributes['Discriminator'])) self.devCtrl.DiscoverCommissionableNodesLongDiscriminator(longDiscriminator) self.wait_for_one_discovered_device() elif args.l is not None: self.devCtrl.DiscoverCommissionableNodesLongDiscriminator(ctypes.c_uint16(args.l)) self.wait_for_one_discovered_device() elif args.s is not None: self.devCtrl.DiscoverCommissionableNodesShortDiscriminator(ctypes.c_uint16(args.s)) self.wait_for_one_discovered_device() elif args.v is not None: self.devCtrl.DiscoverCommissionableNodesVendor(ctypes.c_uint16(args.v)) self.wait_for_many_discovered_devices() elif args.t is not None: self.devCtrl.DiscoverCommissionableNodesDeviceType(ctypes.c_uint16(args.t)) self.wait_for_many_discovered_devices() elif args.c is not None: self.devCtrl.DiscoverCommissionableNodesCommissioningEnabled(ctypes.c_uint16(args.c)) self.wait_for_many_discovered_devices() elif args.a is not None: self.devCtrl.DiscoverCommissionableNodesCommissioningEnabledFromCommand() self.wait_for_many_discovered_devices() else: self.do_help("discover") return self.devCtrl.PrintDiscoveredDevices() except exceptions.ChipStackException as ex: print('exception') print(str(ex)) return except: self.do_help("discover") return