From a9d167ed2db05c5d367290b8bde77e252a59fbda Mon Sep 17 00:00:00 2001 From: shripad621git Date: Fri, 5 Jan 2024 17:52:17 +0530 Subject: [PATCH] Added the support for onboarding paylaod in factory script --- .../tools/generate_esp32_chip_factory_bin.py | 48 +++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/scripts/tools/generate_esp32_chip_factory_bin.py b/scripts/tools/generate_esp32_chip_factory_bin.py index 37ef6beac3cfb3..8f0b5329d336b7 100755 --- a/scripts/tools/generate_esp32_chip_factory_bin.py +++ b/scripts/tools/generate_esp32_chip_factory_bin.py @@ -32,6 +32,8 @@ CHIP_TOPDIR = os.path.dirname(os.path.realpath(__file__))[:-len(os.path.join('scripts', 'tools'))] sys.path.insert(0, os.path.join(CHIP_TOPDIR, 'scripts', 'tools', 'spake2p')) from spake2p import generate_verifier # noqa: E402 isort:skip +sys.path.insert(0, os.path.join(CHIP_TOPDIR, 'src', 'setup_payload', 'python')) +from generate_setup_payload import CommissioningFlow, SetupPayload # noqa: E402 isort:skip if os.getenv('IDF_PATH'): sys.path.insert(0, os.path.join(os.getenv('IDF_PATH'), @@ -48,11 +50,11 @@ TOOLS = {} - FACTORY_PARTITION_CSV = 'nvs_partition.csv' FACTORY_PARTITION_BIN = 'factory_partition.bin' NVS_KEY_PARTITION_BIN = 'nvs_key_partition.bin' ESP_SECURE_CERT_PARTITION_BIN = 'esp_secure_cert_partititon.bin' +ONBOARDING_DATA_FILE = 'onboarding_codes.csv' FACTORY_DATA = { # CommissionableDataProvider @@ -602,6 +604,12 @@ def any_base_int(s): return int(s, 0) help='Do not generate the factory partition binary') parser.add_argument('--output_dir', type=str, default='bin', help='Created image output file path') + parser.add_argument('-cf', '--commissioning-flow', type=any_base_int, default=0, + help='Device commissioning flow, 0:Standard, 1:User-Intent, 2:Custom. \ + Default is 0.', choices=[0, 1, 2]) + parser.add_argument('-dm', '--discovery-mode', type=any_base_int, default=1, + help='Commissionable device discovery networking technology. \ + 0:WiFi-SoftAP, 1:BLE, 2:On-network. Default is BLE.', choices=[0, 1, 2]) parser.set_defaults(generate_bin=True) return parser.parse_args() @@ -633,11 +641,43 @@ def set_up_out_dirs(args): os.makedirs(args.output_dir, exist_ok=True) +def generate_onboarding_data(args): + if (args.vendor_id and args.product_id): + payloads = SetupPayload(args.discriminator, args.passcode, args.discovery_mode, CommissioningFlow(args.commissioning_flow), + args.vendor_id, args.product_id) + else: + payloads = SetupPayload(args.discriminator, args.passcode, args.discovery_mode, CommissioningFlow(args.commissioning_flow)) + + logging.info('Discovery mode' + str(args.discovery_mode)) + chip_qrcode = payloads.generate_qrcode() + chip_manualcode = payloads.generate_manualcode() + # ToDo: remove this if qrcode tool can handle the standard manual code format + if args.commissioning_flow == CommissioningFlow.Standard: + chip_manualcode = chip_manualcode[:4] + '-' + chip_manualcode[4:7] + '-' + chip_manualcode[7:] + else: + chip_manualcode = '"' + chip_manualcode[:4] + '-' + chip_manualcode[4:7] + '-' + chip_manualcode[7:11] + '\n' + \ + chip_manualcode[11:15] + '-' + chip_manualcode[15:18] + '-' + \ + chip_manualcode[18:20] + '-' + chip_manualcode[20:21] + '"' + + logging.info('Generated QR code: ' + chip_qrcode) + logging.info('Generated manual code: ' + chip_manualcode) + + csv_data = 'qrcode,manualcode,discriminator,passcode\n' + csv_data += chip_qrcode + ',' + chip_manualcode + ',' + str(args.discriminator) + ',' + str(args.passcode) + '\n' + + with open(os.path.join(args.output_dir, ONBOARDING_DATA_FILE), 'w') as f: + f.write(csv_data) + + def main(): args = get_args() - set_up_out_dirs(args) - set_up_factory_data(args) - generate_factory_partiton_binary(args) + if (args.discriminator and args.passcode): + set_up_out_dirs(args) + set_up_factory_data(args) + generate_factory_partiton_binary(args) + generate_onboarding_data(args) + else: + logging.info("Please specify discriminator and passcode") if __name__ == "__main__":