Skip to content

Commit

Permalink
Add some error checking for command len in python lib
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro committed Jan 13, 2017
1 parent 0a90dad commit ec9ecfa
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion sw/silta/stm32f407.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class bridge(Silta):

DEBUG = False

# Hardcoding until we can read values back from device
__CMD_MAX_STR_LEN = 4095
__SPI_MAX_BYTES = 1024
__I2C_MAX_BYTES = 1024

def __init__(self, serial_device, baud_rate=None):
''' Initialize Silta STM32F407 Bridge
Expand Down Expand Up @@ -143,6 +148,10 @@ def close(self):

# Send terminal command and wait for response
def __send_cmd(self, cmd):

if (len(cmd) + 1) > self.__CMD_MAX_STR_LEN:
raise RuntimeException('Command string too long')

self.stream.write(cmd + '\n')
if self.DEBUG is True:
print 'CMD : ' + cmd
Expand Down Expand Up @@ -183,6 +192,12 @@ def i2c(self, addr, rlen, wbytes = []):
List with read bytes (or empty list if write-only command)
'''

if len(wbytes) > self.__I2C_MAX_BYTES:
raise ValueError('wbytes too long. Max:', self.__I2C_MAX_BYTES)

if rlen > self.__I2C_MAX_BYTES:
raise ValueError('rlen too long. Max:', self.__I2C_MAX_BYTES)

rbytes = []
cmd = 'i2c ' + format(addr, '02X') + ' ' + str(rlen)

Expand Down Expand Up @@ -242,6 +257,9 @@ def spi(self, cspin, wbytes = []):
or
List of read bytes
'''
if len(wbytes) > self.__SPI_MAX_BYTES:
raise ValueError('wbytes too long. Max:', self.__SPI_MAX_BYTES)

rbytes = []

# Make sure the CS pin is selected
Expand All @@ -254,7 +272,6 @@ def spi(self, cspin, wbytes = []):

line = self.__send_cmd(cmd)


result = line.strip().split(' ')

if result[0] == 'OK':
Expand Down

0 comments on commit ec9ecfa

Please sign in to comment.