This commit is contained in:
ABelliqueux 2021-05-16 16:13:35 +02:00
parent 7bf316c9f9
commit 362bdb244b
1 changed files with 136 additions and 103 deletions

View File

@ -44,9 +44,9 @@ LOAD = '06'
OPEN = '00'
loadParams = {'memAddr':-1,'flagAddr':-1, 'loadFile':-1}
loadParams = {'memAddr':-1,'bufferAddr':-1, 'loadFile':-1}
openParams = {'fileName':-1,'mode':-1}
openParams = {'fileName':-1,'bufferAddr':-1,'mode':-1}
paramBuffer = {}
@ -85,15 +85,13 @@ loadFile = -1
# One byte
#uno = int(1).to_bytes(1, byteorder='little', signed=False)
data = 0
# checkSum is the checkSum for the full data
checkSum = 0
checkSumLen = 9 # Corresponding value in pcdrv.h, l.54
checkSumLen = 10 # Corresponding value in pcdrv.h, l.54
# If set, it means the data transfer has been initiated
@ -245,11 +243,7 @@ def getData(dataInBuffer):
return [dataInBuffer, parsedData] # does it break ?
dataLen = dataInBuffer[:2]
# We're receiving 8 chars to describe a 4 bytes pointer, hence the * 2
dataLen = int(dataLen) * 2
dataLen = int( dataInBuffer[:2] )
dataInBuffer = dataInBuffer[2:]
@ -555,133 +549,140 @@ def main(args):
print( "Incoming data : " + inputBuffer )
# We're expecting the command with format : 00 01 06 04 xx xx xx xx 04 xx xx xx xx 01 xx xx xx xx xx (38 Bytes)
# LOAD command format : 00 01 06 04 xx xx xx xx 04 xx xx xx xx 01 xx xx xx xx (37 Bytes)
if len(inputBuffer) == 37:
# ~ if len(inputBuffer) == 37:
if DEBUG:
if DEBUG:
print( "Incoming data : " + inputBuffer )
print( "Incoming data : " + inputBuffer )
# Get the checksum and remove it from the buffer
# Get the checksum and remove it from the buffer
CmdCheckSum = inputBuffer[-checkSumLen:]
CmdCheckSum = inputBuffer[-checkSumLen:]
if DEBUG:
if CmdCheckSum.islower() or CmdCheckSum.isupper() :
print( "Received ChkSm: " + CmdCheckSum )
print("Checksum sould not contain letters ! Aborting.")
# Remove checksum from data
break
inputBuffer = inputBuffer[:28]
if DEBUG:
dataCheckSum = hash_djb2(bytes(inputBuffer, 'ascii'))
print( "Received ChkSm: " + CmdCheckSum)
if DEBUG:
# Remove checksum from data
print( "Computed ChkSm: " + str(dataCheckSum) )
# ~ inputBuffer = inputBuffer[:28]
inputBuffer = inputBuffer[:-checkSumLen]
# Check
dataCheckSum = hash_djb2(bytes(inputBuffer, 'ascii'))
if int(dataCheckSum) == int(CmdCheckSum):
if DEBUG:
# Parse command
print( "Computed ChkSm: " + str(dataCheckSum) )
if inputBuffer[:2] == pcdrvEscape:
# Check
if int(dataCheckSum) == int(CmdCheckSum):
# Parse command
if inputBuffer[:2] == pcdrvEscape:
if DEBUG:
print( "Received Escape : " + inputBuffer[:2] )
# Escape byte received, remove it from buffer and continue
inputBuffer = inputBuffer[2:]
if inputBuffer[:2] == pcdrvProtocol:
if DEBUG:
print( "Received Escape : " + inputBuffer[:2] )
print( "Received Proto: " + inputBuffer[:2] )
# Escape byte received, remove it from buffer and continue
# Protocol byte is pcdrv (01), remove it from buffer and continue
inputBuffer = inputBuffer[2:]
if inputBuffer[:2] == pcdrvProtocol:
if DEBUG:
if DEBUG:
print( "Received Cmd: " + inputBuffer[:2] )
print( "Received Proto: " + inputBuffer[:2] )
# Command bytes are LOAD == 06
# Protocol byte is pcdrv (01), remove it from buffer and continue
if inputBuffer[:2] == LOAD:
inputBuffer = inputBuffer[2:]
# Set corresponding parameters and mode
if DEBUG:
paramTmp = loadParams
print( "Received Cmd: " + inputBuffer[:2] )
Command = LOAD
# Command bytes are LOAD == 06
# Command butes are OPEN == 00
if inputBuffer[:2] == LOAD:
elif inputBuffer[:2] == OPEN:
# Set corresponding parameters and mode
paramTmp = openParams
paramTmp = loadParams
Command = OPEN
Command = LOAD
else:
# Command butes are OPEN == 00
print("Command not recognized : got " + inputBuffer[:2] )
elif inputBuffer[:2] == OPEN:
break
paramTmp = openParams
# Command byte is valid , remove it from buffer and continue
Command = OPEN
inputBuffer = inputBuffer[2:]
else:
dataInBuffer = inputBuffer
print("Command not recognized : got " + inputBuffer[:2] )
# For each parameter, populate corresponding data
break
for param in paramTmp:
# Command byte is valid , remove it from buffer and continue
dataInBuffer, paramTmp[param] = getData(dataInBuffer)
inputBuffer = inputBuffer[2:]
dataInBuffer = inputBuffer
# For each parameter, populate corresponding data
if DEBUG:
for param in paramTmp:
dataInBuffer, paramTmp[param] = getData(dataInBuffer)
print(param + ":" + paramTmp[param] + " - ")
if DEBUG:
# Commit parsed data to param buffer
for param in paramTmp:
paramBuffer = paramTmp
print(param + ":" + paramTmp[param] + " - ")
ser.reset_input_buffer()
# Commit parsed data to param buffer
inputBuffer = ""
paramBuffer = paramTmp
ser.reset_input_buffer()
inputBuffer = ""
Listen = 0
break
else:
print("Command checksum not matching ! Aborting...")
ser.reset_input_buffer()
inputBuffer = ""
break
Listen = 0
break
else:
print("Command checksum not matching ! Aborting...")
ser.reset_input_buffer()
inputBuffer = ""
break
# ~ else:
# ~ ser.reset_input_buffer()
# ~ inputBuffer = ""
# ~ break
if len(paramBuffer):
# Check that no param is undefined ( != -1 )
@ -698,7 +699,7 @@ def main(args):
if DEBUG > 1:
print("Received addresses and file ID : " + paramBuffer['memAddr'] + " - " + paramBuffer['flagAddr'] + " - " + paramBuffer['loadFile'] )
print("Received addresses and file ID : " + paramBuffer['memAddr'] + " - " + paramBuffer['bufferAddr'] + " - " + paramBuffer['loadFile'] )
binFileName = overlayFiles[ paramBuffer['loadFile'] ]
@ -708,7 +709,7 @@ def main(args):
"Load Data to : " + paramBuffer['memAddr'] + "\n" +
"Reset flag at: " + paramBuffer['flagAddr'] + "\n" +
"Reset flag at: " + paramBuffer['bufferAddr'] + "\n" +
"LoadFile : " + paramBuffer['loadFile'] + "\n" +
@ -740,7 +741,7 @@ def main(args):
# Send ACK
SendBin( b'OKYA' , paramBuffer['flagAddr'])
SendBin( b'OKYA' , paramBuffer['bufferAddr'])
# Reset everything
@ -756,7 +757,39 @@ def main(args):
if Command == OPEN:
print("Received OPEN. Not yet implemented !")
print("Received OPEN.")
# paramBuffer['mode'] can be 0 (RO), 1(WO), 2 (RW)
osMode = os.O_RDONLY
if int(paramBuffer['mode']) == 1:
osMode = os.O_WRONLY
else:
osMode = os.O_RDWR
if os.path.isfile( 'work/' + paramBuffer['fileName']):
localFile = os.open( 'work/' + paramBuffer['fileName'], osMode )
SendBin( b'DT' + bytes( str(localFile), 'ascii' ), paramBuffer['bufferAddr'])
# ~ time.sleep( sleepTime )
# ~ SendBin( bytes( str(localFile), 'ascii' ), paramBuffer['bufferAddr'])
else:
SendBin( b'-1', paramBuffer['bufferAddr'])
resetListener()
print("DONE!")
# ~ break
return 0