From d76fbd440c4c711f3d7ccfd687abfe9e6a96af77 Mon Sep 17 00:00:00 2001 From: dxl <64101226@qq.com> Date: Mon, 28 Aug 2023 01:35:57 +0800 Subject: [PATCH] Added commands related to BLE pairing binding. --- software/script/chameleon_cli_unit.py | 51 +++++++++++++++++++++++++-- software/script/chameleon_cmd.py | 33 +++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 6f35221..28059ba 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -157,11 +157,15 @@ hw_address = hw.subgroup('address', 'Device address get') hw_mode = hw.subgroup('mode', 'Device mode get/set') hw_slot = hw.subgroup('slot', 'Emulation tag slot.') hw_slot_nick = hw_slot.subgroup('nick', 'Get/Set tag nick name for slot') +hw_ble = hw.subgroup('ble', 'Bluetooth low energy') +hw_ble_bonds = hw_ble.subgroup('bonds', 'All devices bound by chameleons.') hw_settings = hw.subgroup('settings', 'Chameleon settings management') hw_settings_animation = hw_settings.subgroup( 'animation', 'Manage wake-up and sleep animation modes') hw_settings_button_press = hw_settings.subgroup( 'btnpress', 'Manage button press function') +hw_settings_ble_key = hw_settings.subgroup( + 'blekey', 'Manage ble connect key') hf = CLITree('hf', 'high frequency tag/reader') hf_14a = hf.subgroup('14a', 'ISO14443-a tag read/write/info...') @@ -1393,10 +1397,10 @@ class HWButtonSettingsGet(DeviceRequiredUnit): button_long_fn = chameleon_cmd.ButtonPressFunction.from_int( resp_long.data[0]) print(f" - {colorama.Fore.GREEN}{button} {colorama.Fore.YELLOW}short{colorama.Style.RESET_ALL}:" - " {button_fn}") + f" {button_fn}") print(f" usage: {button_fn.usage()}") print(f" - {colorama.Fore.GREEN}{button} {colorama.Fore.YELLOW}long {colorama.Style.RESET_ALL}:" - " {button_long_fn}") + f" {button_long_fn}") print(f" usage: {button_long_fn.usage()}") print("") print(" - Successfully get button function from settings") @@ -1428,3 +1432,46 @@ class HWButtonSettingsSet(DeviceRequiredUnit): else: self.cmd.set_button_press_fun(button, function) print(" - Successfully set button function to settings") + + +@hw_settings_ble_key.command('set', 'Set the ble connect key') +class HWSettingsBLEKeySet(DeviceRequiredUnit): + + def args_parser(self) -> ArgumentParserNoExit: + parser = ArgumentParserNoExit() + parser.add_argument('-k', '--key', required=True, + help="Ble connect key for your device") + return parser + + def on_exec(self, args: argparse.Namespace): + if len(args.key) != 6: + print(f" - {colorama.Fore.RED}The ble connect key length must be 6{colorama.Style.RESET_ALL}") + return + if re.match(r'[0-9]{6}', args.key): + self.cmd.set_ble_connect_key(args.key) + print(" - Successfully set ble connect key to settings") + else: + print(f" - {colorama.Fore.RED}Only 6 ASCII characters from 0 to 9 are supported.{colorama.Style.RESET_ALL}") + + +@hw_settings_ble_key.command('get', 'Get the ble connect key') +class HWSettingsBLEKeyGet(DeviceRequiredUnit): + + def args_parser(self) -> ArgumentParserNoExit: + return None + + def on_exec(self, args: argparse.Namespace): + resp = self.cmd.get_ble_connect_key() + print(" - Key(ascii): " + f"{colorama.Fore.GREEN}{resp.data.decode(encoding='ascii')}{colorama.Style.RESET_ALL}") + + +@hw_ble_bonds.command('clear', 'Clear all bindings') +class HWBLEBondsClear(DeviceRequiredUnit): + + def args_parser(self) -> ArgumentParserNoExit: + return None + + def on_exec(self, args: argparse.Namespace): + self.cmd.delete_ble_all_bonds() + print(" - Successfully clear all bonds") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 14953c2..02bcb2c 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -45,6 +45,11 @@ DATA_CMD_SET_BUTTON_PRESS_CONFIG = 1027 DATA_CMD_GET_LONG_BUTTON_PRESS_CONFIG = 1028 DATA_CMD_SET_LONG_BUTTON_PRESS_CONFIG = 1029 +DATA_CMD_SET_BLE_CONNECT_KEY_CONFIG = 1030 +DATA_CMD_GET_BLE_CONNECT_KEY_CONFIG = 1031 + +DATA_CMD_DELETE_ALL_BLE_BONDS = 1032 + DATA_CMD_SCAN_14A_TAG = 2000 DATA_CMD_MF1_SUPPORT_DETECT = 2001 DATA_CMD_MF1_NT_LEVEL_DETECT = 2002 @@ -794,7 +799,35 @@ class ChameleonCMD: 0x00, bytearray([button, function]) ) + + @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + def set_ble_connect_key(self, key: str): + """ + Set config of ble connect key + """ + data_bytes = key.encode(encoding='ascii') + # check key length + if (len(data_bytes) != 6): + raise ValueError("The ble connect key length must be 6") + + return self.device.send_cmd_sync( + DATA_CMD_SET_BLE_CONNECT_KEY_CONFIG, + 0x00, + data_bytes + ) + + def get_ble_connect_key(self): + """ + Get config of ble connect key + """ + return self.device.send_cmd_sync(DATA_CMD_GET_BLE_CONNECT_KEY_CONFIG, 0x00, None) + + def delete_ble_all_bonds(self): + """ + From peer manager delete all bonds. + """ + return self.device.send_cmd_sync(DATA_CMD_DELETE_ALL_BLE_BONDS, 0x00, None) if __name__ == '__main__': # connect to chameleon