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]
)
def atcab_read_bytes_zone(self, zone, slot=0, block=0, offset=0, length=0):
zone_size = self.atcab_get_zone_size(zone, slot=slot)
if offset + length > zone_size:
raise ATCA_EXCEPTIONS.BadArgumentError()
packets = []
BS = ATCA_CONSTANTS.ATCA_BLOCK_SIZE
WS = ATCA_CONSTANTS.ATCA_WORD_SIZE
r_sz = BS
d_idx = r_idx = r_of = c_blk = c_of = 0
c_blk = offset // BS
while d_idx < length:
if r_sz == BS and zone_size - c_blk * BS < BS:
r_sz = WS
c_of = ((d_idx + offset) // WS) % (BS // WS)
packet = self.atcab_read_zone(
zone,
slot=slot,
block=c_blk,
offset=c_of,
length=r_sz
)
def atcab_is_locked(self, zone):
if zone not in (
ATCA_CONSTANTS.LOCK_ZONE_CONFIG,
ATCA_CONSTANTS.LOCK_ZONE_DATA
):
raise ATCA_EXCEPTIONS.BadArgumentError()
# Read the word with the lock bytes
# (UserExtra, Selector, LockValue, LockConfig) (config block = 2, word offset = 5)
packet = self.atcab_read_zone(
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
slot=0,
block=2,
offset=5,
length=ATCA_CONSTANTS.ATCA_WORD_SIZE
)
if zone == ATCA_CONSTANTS.LOCK_ZONE_CONFIG:
return bool(packet[3+1] != 0x55)
elif zone == ATCA_CONSTANTS.LOCK_ZONE_DATA:
return bool(packet[2+1] != 0x55)
def atcab_write_zone(self, zone, slot=0, block=0, offset=0, data=None):
if not isinstance(data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
length = len(data)
if length not in (
ATCA_CONSTANTS.ATCA_WORD_SIZE,
ATCA_CONSTANTS.ATCA_BLOCK_SIZE
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if length == ATCA_CONSTANTS.ATCA_BLOCK_SIZE:
zone = zone | ATCA_CONSTANTS.ATCA_ZONE_READWRITE_32
addr = self.atcab_get_addr(zone, slot=slot, block=block, offset=offset)
return self.atcab_write(zone, addr, data)