Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def atcab_write_bytes_zone(self, zone, slot=0, offset=0, data=None):
if not isinstance(data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
zone_size = self.atcab_get_zone_size(zone, slot=slot)
length = len(data)
if offset + length > zone_size:
raise ATCA_EXCEPTIONS.BadArgumentError()
packets = []
BS = ATCA_CONSTANTS.ATCA_BLOCK_SIZE
WS = ATCA_CONSTANTS.ATCA_WORD_SIZE
ZC = ATCA_CONSTANTS.ATCA_ZONE_CONFIG
d_idx = 0
c_blk = offset // BS
c_wrd = (offset % BS) // WS
d_mv = memoryview(data)
while d_idx < length:
# The last item makes sure we handle the selector, user extra, and lock bytes in the config properly
if c_wrd == 0 and length - d_idx >= BS and not (zone == ZC and c_blk == 2):
packet = self.atcab_write_zone(
zone,
slot=slot,
block=c_blk,
offset=0,
data=d_mv[d_idx:d_idx+BS]
)
packets.append(packet)
def atcab_get_zone_size(self, zone, slot=0):
if zone not in (
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
ATCA_CONSTANTS.ATCA_ZONE_DATA,
ATCA_CONSTANTS.ATCA_ZONE_OTP
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if slot < 0 or slot > 15:
raise ATCA_EXCEPTIONS.BadArgumentError()
if zone == ATCA_CONSTANTS.ATCA_ZONE_CONFIG:
return 128
elif zone == ATCA_CONSTANTS.ATCA_ZONE_OTP:
return 64
elif zone == ATCA_CONSTANTS.ATCA_ZONE_DATA:
if slot < 8:
return 36
elif slot == 8:
def atcab_read_serial_number(self):
return self.atcab_read_zone(
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
length=ATCA_CONSTANTS.ATCA_BLOCK_SIZE
)
def atcab_write_config_zone(self, config_data):
if not isinstance(config_data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
config_size = self.atcab_get_zone_size(ATCA_CONSTANTS.ATCA_ZONE_CONFIG)
# Write config zone excluding UserExtra and Selector
packets = self.atcab_write_bytes_zone(
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
slot=0,
offset=16,
data=config_data[16:config_size - 16]
)
# Write the UserExtra and Selector. This may fail if either value is already non-zero.
packet = self.atcab_updateextra(
ATCA_CONSTANTS.UPDATE_MODE_USER_EXTRA,
config_data[84]
)
packets.append(packet)
packet = self.atcab_updateextra(
ATCA_CONSTANTS.UPDATE_MODE_SELECTOR,
config_data[85]
)
def atcab_get_zone_size(self, zone, slot=0):
if zone not in (
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
ATCA_CONSTANTS.ATCA_ZONE_DATA,
ATCA_CONSTANTS.ATCA_ZONE_OTP
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if slot < 0 or slot > 15:
raise ATCA_EXCEPTIONS.BadArgumentError()
if zone == ATCA_CONSTANTS.ATCA_ZONE_CONFIG:
return 128
elif zone == ATCA_CONSTANTS.ATCA_ZONE_OTP:
return 64
elif zone == ATCA_CONSTANTS.ATCA_ZONE_DATA:
if slot < 8:
return 36
elif slot == 8:
return 412
elif slot < 16:
return 72