From d233ef772f9a904060d0e86fae30dbacd6344a84 Mon Sep 17 00:00:00 2001 From: shchen-Lab <75290921+shchen-Lab@users.noreply.github.com> Date: Fri, 21 Jul 2023 23:51:00 +0800 Subject: [PATCH] [BouffaloLab]Add generate factory qr code scripts (#28146) * add factory qr code scripts * remove unused modules * Restyled by autopep8 * remove unused modules --------- Co-authored-by: Restyled.io --- scripts/setup/requirements.bouffalolab.txt | 6 ++ scripts/tools/bouffalolab/factory_qrcode.py | 84 +++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 scripts/tools/bouffalolab/factory_qrcode.py diff --git a/scripts/setup/requirements.bouffalolab.txt b/scripts/setup/requirements.bouffalolab.txt index 87fa5c49ab96e5..acd5fa726bb22b 100644 --- a/scripts/setup/requirements.bouffalolab.txt +++ b/scripts/setup/requirements.bouffalolab.txt @@ -1 +1,7 @@ bflb-iot-tool>=1.8.0 +jsonschema>=4.4.0 +cbor2>=5.4.3 +ecdsa>=0.18.0 +qrcode==7.4.2 +bitarray==2.6.0 +python_stdnum==1.18 diff --git a/scripts/tools/bouffalolab/factory_qrcode.py b/scripts/tools/bouffalolab/factory_qrcode.py new file mode 100644 index 00000000000000..1a7f6303495775 --- /dev/null +++ b/scripts/tools/bouffalolab/factory_qrcode.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2022 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import os +import sys + +try: + import qrcode + from generate_setup_payload import CommissioningFlow, SetupPayload +except ImportError: + SDK_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) + sys.path.append(os.path.join(SDK_ROOT, "src/setup_payload/python")) + try: + import qrcode + from generate_setup_payload import CommissioningFlow, SetupPayload + except ModuleNotFoundError or ImportError: + no_onboarding_modules = True + else: + no_onboarding_modules = False +else: + no_onboarding_modules = False + + +def allow_any_int(i): return int(i, 0) + + +def get_args(): + parser = argparse.ArgumentParser(description="BouffaloLab QR Code generator tool") + mandatory_arguments = parser.add_argument_group( + "Mandatory keys", "These arguments must be provided to generate QR Code JSON file") + mandatory_arguments.add_argument("--vendor_id", type=allow_any_int, required=True, + help="[int | hex int] Provide Vendor Identification Number") + mandatory_arguments.add_argument("--product_id", type=allow_any_int, required=True, + help="[int | hex int] Provide Product Identification Number") + mandatory_arguments.add_argument("--discriminator", type=allow_any_int, required=True, + help="[int] Provide BLE pairing discriminator. \ + A 12-bit value matching the field of the same name in \ + the setup code. Discriminator is used during a discovery process.") + mandatory_arguments.add_argument("--pincode", type=allow_any_int, required=True, + help=("[int | hex] Default PASE session passcode. " + "(This is mandatory to generate QR Code).")) + mandatory_arguments.add_argument("-o", "--output", type=str, required=True, + help="Output path to store .json file, e.g. my_dir/output.json") + return parser.parse_args() + + +def generate_qrcode_data(args): + if no_onboarding_modules is False: + setup_payload = SetupPayload(discriminator=args.discriminator, + pincode=args.pincode, + rendezvous=2, # fixed pairing BLE + flow=CommissioningFlow.Standard, + vid=args.vendor_id, + pid=args.product_id) + with open(args.output[:-len(".json")] + ".txt", "w") as manual_code_file: + manual_code_file.write("Manualcode : " + setup_payload.generate_manualcode() + "\n") + manual_code_file.write("QRCode : " + setup_payload.generate_qrcode()) + qr = qrcode.make(setup_payload.generate_qrcode()) + qr.save(args.output[:-len(".json")] + ".png") + else: + print("Please install it with all dependencies: pip3 install -r ./scripts/setup/requirements.bouffalolab.txt from the Matter root directory") + + +def main(): + args = get_args() + generate_qrcode_data(args) + + +if __name__ == "__main__": + main()