Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- Removed all usage of `is not None` its not a pythonic way
- Removed `required=False` as default is already False
  • Loading branch information
shubhamdp committed Jan 11, 2023
1 parent a936cf0 commit 795068a
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions scripts/tools/generate_esp32_chip_factory_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def check_int_range(value, min_value, max_value, name):

def validate_args(args):
# Validate the passcode
if args.passcode is not None:
if args.passcode:
if ((args.passcode < 0x0000001 and args.passcode > 0x5F5E0FE) or (args.passcode in INVALID_PASSCODES)):
logging.error('Invalid passcode:' + str(args.passcode))
sys.exit(1)
Expand Down Expand Up @@ -277,48 +277,48 @@ def gen_spake2p_params(passcode):


def populate_factory_data(args, spake2p_params):
if args.discriminator is not None:
if args.discriminator:
FACTORY_DATA['discriminator']['value'] = args.discriminator

if spake2p_params is not None:
if spake2p_params:
FACTORY_DATA['iteration-count']['value'] = spake2p_params['Iteration Count']
FACTORY_DATA['salt']['value'] = spake2p_params['Salt']
FACTORY_DATA['verifier']['value'] = spake2p_params['Verifier']

if args.dac_cert is not None:
if args.dac_cert:
FACTORY_DATA['dac-cert']['value'] = os.path.abspath(args.dac_cert)
if args.pai_cert is not None:
if args.pai_cert:
FACTORY_DATA['pai-cert']['value'] = os.path.abspath(args.pai_cert)
if args.cd is not None:
if args.cd:
FACTORY_DATA['cert-dclrn']['value'] = os.path.abspath(args.cd)
if args.dac_key is not None:
if args.dac_key:
FACTORY_DATA['dac-key']['value'] = os.path.abspath('dac_raw_privkey.bin')
FACTORY_DATA['dac-pub-key']['value'] = os.path.abspath('dac_raw_pubkey.bin')

if args.serial_num is not None:
if args.serial_num:
FACTORY_DATA['serial-num']['value'] = args.serial_num
if args.rd_id_uid is not None:
if args.rd_id_uid:
FACTORY_DATA['rd-id-uid']['value'] = args.rd_id_uid
if args.mfg_date is not None:
if args.mfg_date:
FACTORY_DATA['mfg-date']['value'] = args.mfg_date
if args.vendor_id is not None:
if args.vendor_id:
FACTORY_DATA['vendor-id']['value'] = args.vendor_id
if args.vendor_name is not None:
if args.vendor_name:
FACTORY_DATA['vendor-name']['value'] = args.vendor_name
if args.product_id is not None:
if args.product_id:
FACTORY_DATA['product-id']['value'] = args.product_id
if args.product_name is not None:
if args.product_name:
FACTORY_DATA['product-name']['value'] = args.product_name
if args.hw_ver is not None:
if args.hw_ver:
FACTORY_DATA['hardware-ver']['value'] = args.hw_ver
if (args.hw_ver_str is not None):
if args.hw_ver_str:
FACTORY_DATA['hw-ver-str']['value'] = args.hw_ver_str

if (args.calendar_types is not None):
if args.calendar_types:
FACTORY_DATA['cal-types']['value'] = calendar_types_to_uint32(args.calendar_types)

# Supported locale is stored as multiple entries, key format: "locale/<index>, example key: "locale/0"
if (args.locales is not None):
if args.locales:
FACTORY_DATA['locale-sz']['value'] = len(args.locales)

for i in range(len(args.locales)):
Expand All @@ -333,7 +333,7 @@ def populate_factory_data(args, spake2p_params):
# - fl-sz/<index> : number of fixed labels for the endpoint
# - fl-k/<ep>/<index> : fixed label key for the endpoint and index
# - fl-v/<ep>/<index> : fixed label value for the endpoint and index
if (args.fixed_labels is not None):
if args.fixed_labels:
dict = get_fixed_label_dict(args.fixed_labels)
for key in dict.keys():
_sz = {
Expand Down Expand Up @@ -431,9 +431,9 @@ def print_flashing_help(encrypt, bin_filename):


def clean_up():
if FACTORY_DATA['dac-pub-key']['value'] is not None:
if FACTORY_DATA['dac-pub-key']['value']:
os.remove(FACTORY_DATA['dac-pub-key']['value'])
if FACTORY_DATA['dac-key']['value'] is not None:
if FACTORY_DATA['dac-key']['value']:
os.remove(FACTORY_DATA['dac-key']['value'])


Expand All @@ -443,62 +443,63 @@ def any_base_int(s): return int(s, 0)
parser = argparse.ArgumentParser(description='Chip Factory NVS binary generator tool')

# These will be used by CommissionalbeDataProvider
parser.add_argument('-p', '--passcode', type=any_base_int, required=False,
parser.add_argument('-p', '--passcode', type=any_base_int,
help='The setup passcode for pairing, range: 0x01-0x5F5E0FE')
parser.add_argument('-d', '--discriminator', type=any_base_int, required=False,
parser.add_argument('-d', '--discriminator', type=any_base_int,
help='The discriminator for pairing, range: 0x00-0x0FFF')

# These will be used by DeviceAttestationCredentialsProvider
parser.add_argument('--dac-cert', type=str, required=False,
parser.add_argument('--dac-cert', type=str,
help='The path to the DAC certificate in der format')
parser.add_argument('--dac-key', type=str, required=False,
parser.add_argument('--dac-key', type=str,
help='The path to the DAC private key in der format')
parser.add_argument('--pai-cert', type=str, required=False,
parser.add_argument('--pai-cert', type=str,
help='The path to the PAI certificate in der format')
parser.add_argument('--cd', type=str, required=False,
parser.add_argument('--cd', type=str,
help='The path to the certificate declaration der format')

# These will be used by DeviceInstanceInfoProvider
parser.add_argument('--vendor-id', type=any_base_int, required=False, help='Vendor id')
parser.add_argument('--vendor-name', type=str, required=False, help='Vendor name')
parser.add_argument('--product-id', type=any_base_int, required=False, help='Product id')
parser.add_argument('--product-name', type=str, required=False, help='Product name')
parser.add_argument('--hw-ver', type=any_base_int, required=False, help='Hardware version')
parser.add_argument('--hw-ver-str', type=str, required=False, help='Hardware version string')
parser.add_argument('--mfg-date', type=str, required=False, help='Manufacturing date in format YYYY-MM-DD')
parser.add_argument('--serial-num', type=str, required=False, help='Serial number')
parser.add_argument('--rd-id-uid', type=str, required=False,
parser.add_argument('--vendor-id', type=any_base_int, help='Vendor id')
parser.add_argument('--vendor-name', type=str, help='Vendor name')
parser.add_argument('--product-id', type=any_base_int, help='Product id')
parser.add_argument('--product-name', type=str, help='Product name')
parser.add_argument('--hw-ver', type=any_base_int, help='Hardware version')
parser.add_argument('--hw-ver-str', type=str, help='Hardware version string')
parser.add_argument('--mfg-date', type=str, help='Manufacturing date in format YYYY-MM-DD')
parser.add_argument('--serial-num', type=str, help='Serial number')
parser.add_argument('--rd-id-uid', type=str,
help='128-bit unique identifier for generating rotating device identifier, provide 32-byte hex string, e.g. "1234567890abcdef1234567890abcdef"')

# These will be used by DeviceInfoProvider
parser.add_argument('--calendar-types', type=str, nargs='+', required=False,
parser.add_argument('--calendar-types', type=str, nargs='+',
help='List of supported calendar types.\nSupported Calendar Types: Buddhist, Chinese, Coptic, Ethiopian, Gregorian, Hebrew, Indian, Islamic, Japanese, Korean, Persian, Taiwanese')
parser.add_argument('--locales', type=str, nargs='+', required=False,
parser.add_argument('--locales', type=str, nargs='+',
help='List of supported locales, Language Tag as defined by BCP47, eg. en-US en-GB')
parser.add_argument('--fixed-labels', type=str, nargs='+', required=False,
parser.add_argument('--fixed-labels', type=str, nargs='+',
help='List of fixed labels, eg: "0/orientation/up" "1/orientation/down" "2/orientation/down"')

parser.add_argument('-s', '--size', type=any_base_int, required=False, default=0x6000,
parser.add_argument('-s', '--size', type=any_base_int, default=0x6000,
help='The size of the partition.bin, default: 0x6000')
parser.add_argument('-e', '--encrypt', action='store_true', required=False,
parser.add_argument('-e', '--encrypt', action='store_true',
help='Encrypt the factory parititon NVS binary')
parser.add_argument('--no-bin', action='store_true', required=False,
parser.add_argument('--no-bin', action='store_false', dest='generate_bin',
help='Do not generate the factory partition binary')
parser.set_defaults(generate_bin=True)

args = parser.parse_args()
validate_args(args)

if args.passcode is not None:
if args.passcode:
spake2p_params = gen_spake2p_params(args.passcode)

populate_factory_data(args, spake2p_params)

if args.dac_key is not None:
if args.dac_key:
gen_raw_ec_keypair_from_der(args.dac_key, FACTORY_DATA['dac-pub-key']['value'], FACTORY_DATA['dac-key']['value'])

generate_nvs_csv(FACTORY_PARTITION_CSV)

if args.no_bin is False:
if args.generate_bin:
generate_nvs_bin(args.encrypt, args.size, FACTORY_PARTITION_CSV, FACTORY_PARTITION_BIN)
print_flashing_help(args.encrypt, FACTORY_PARTITION_BIN)

Expand Down

0 comments on commit 795068a

Please sign in to comment.