Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The FTP transfer utility
- // Do NOT look to this for coding style pointers. QAD code.
- // pragma(1) // Uncomment to forces a compiler output listing be shown
- int bSetupOk
- int u8RxD
- int iCRC,iRxState,iRxStuffed,iRxCMD,iRxLength,iRxCnt
- str sRxData
- int iTxCRC
- int iRxOffset
- const
- {
- // Comms stuff
- bSTUFF = $A6
- bSOM = $A7
- cmdGetDirectory = $44
- cmdReadFile = $52
- cmdReadFileACK = $72
- cmdWriteFile = $57
- cmdWriteFileACK = $77
- cmdResetLogger = $5A
- cmdResetLoggerACK = $7A
- cmdCRCFile = $43
- cmdCRCFileACK = $63
- cmdDeleteFile = $A0
- cmdDeleteFileACK = $C0
- cmdRenameFile = $A1
- cmdRenameFileACK = $C1
- cmdCopyFile = $A2
- cmdCopyFileACK = $C2
- // Oh, for enumerated types...
- sRxIdle = 0
- sRxExpCMD = 1
- sRxExpLEN0 = 2
- sRxExpLEN1 = 3
- sRxExpLEN2 = 4
- sRxExpDATA = 5
- sRxExpCRCL = 6
- sRxExpCRCH = 7
- }
- // Forwards
- int vHandleRxChar(int u8RxD);
- int vHandleRxMsg();
- int vHandleTxByte(int u8TxD);
- int vHandleTxData(int iCMD, str sDATA);
- // Setup the ESP port and BT port
- bSetupOk = devicesetup(devESP,500000,parityNONE,2)
- // Assume we are setup
- iRxState = sRxIdle
- iRxStuffed=0
- // Now, we wait for a command
- repeat
- while rxwait(devESP)<1
- {
- // We should sleep here
- }
- // We have at least one character waiting
- vHandleRxChar(rxread(devESP,u8))
- until false
- // Handle sending a stuffed byte
- int vHandleTxByte(int u8TxD)
- {
- // CRC the damned thing
- crc16(iTxCRC,u8TxD)
- // Send it
- if u8TxD = bSTUFF
- {
- txwrite(devESP,u8 bSTUFF, 0)
- }
- else if u8TxD = bSOM
- {
- txwrite(devESP,u8 bSTUFF, 1)
- }
- else
- txwrite(devESP,u8 u8TxD)
- }
- // Handle a received character
- int vHandleRxChar(int u8RxD)
- {
- if u8RxD = bSOM
- {
- iRxState = sRxExpCMD
- iCRC = $FFFF
- }
- else if u8RxD = bSTUFF
- iRxStuffed = bSTUFF
- else
- {
- // Unstuff data bytes
- u8RxD+=iRxStuffed
- iRxStuffed=0;
- switch (iRxState)
- {
- case sRxExpCMD // Expect CMD
- iRxCMD = u8RxD
- crc16(iCRC,u8RxD)
- iRxState = sRxExpLEN0
- break
- case sRxExpLEN0 // Expect LEN LSB
- iRxLength = u8RxD
- crc16(iCRC,u8RxD)
- iRxState = sRxExpLEN1
- break
- case sRxExpLEN1 // Expect LEN MSB
- iRxLength += u8RxD * $100
- crc16(iCRC,u8RxD)
- iRxState = sRxExpLEN2
- break
- case sRxExpLEN2 // Expect LEN MSB
- iRxLength += u8RxD * $10000
- crc16(iCRC,u8RxD)
- // Do we have any data?
- sRxData = ""
- iRxCnt = 0
- if iRxLength>0
- iRxState = sRxExpDATA
- else
- iRxState = sRxExpCRCL
- break
- case sRxExpDATA
- sRxData = sRxData + chr$(u8RxD)
- crc16(iCRC,u8RxD)
- if ++iRxCnt >= iRxLength
- iRxState = sRxExpCRCL
- break;
- case sRxExpCRCL
- if u8RxD <> (iCRC and $FF)
- iRxState = sRxIdle
- else
- iRxState = sRxExpCRCH
- break;
- case sRxExpCRCH
- if u8RxD = (iCRC >> 8)
- {
- vHandleRxMsg()
- }
- iRxState = sRxIdle
- break;
- }
- }
- }
- // Send a reply
- int vHandleTxData(int iCMD, str sDATA)
- {
- int i,iLength,iTxCRCCopy
- txwrite(devESP,u8 bSOM)
- iTxCRC = $FFFF
- vHandleTxByte(iCMD)
- iLength = length(sDATA)
- vHandleTxByte(iLength and $FF)
- vHandleTxByte((iLength >> 8) and $FF)
- vHandleTxByte((iLength >> 16) and $FF)
- i=0
- while i<iLength
- {
- while txfree(devESP) < 2
- {
- // Should sleep here
- }
- // Send the next character (byte)
- vHandleTxByte(asc(mid$(sDATA,i++,1)))
- }
- // Now, send the CRC (Using a copy so we do not change it)
- iTxCRCCopy=iTxCRC
- vHandleTxByte(iTxCRCCopy)
- vHandleTxByte(iTxCRCCopy >> 8)
- }
- // Extract a filename
- string sGetRxFileName()
- {
- int len
- len = asc(mid$(sRxData,iRxOffset++,1))
- result = mid$(sRxData,iRxOffset,len)
- iRxOffset+=len
- }
- // Handle a received message
- int vHandleRxMsg()
- {
- string sFN,sFN2,sFile,sReply
- int iFileHandle,iFileError
- iRxOffset = 0 // We start looking at the first character
- switch(iRxCMD)
- {
- case cmdGetDirectory
- vHandleTxData(cmdGetDirectory,filedir("/"))
- break
- case cmdReadFile
- // Extract the filename
- sFN = sGetRxFileName()
- // Read the file contents
- iFileError = 1 // Assume an error if we cant even open the file
- iFileHandle=fileopen(sFN)
- // If we managed to open it, read the contents
- if iFileHandle
- {
- iFileError = 0
- // This will set an error if it couldnt read the file
- sFile = fileread(iFileHandle,iFileError, string -1) // Read as many are available
- fileclose(iFileHandle)
- }
- // If it was okay, send it
- if iFileError = 0
- {
- sReply = chr$(length(sFN)) + sFN + sFile
- vHandleTxData(cmdReadFileACK,sReply)
- }
- else
- {
- // Else just reply with the filename
- sReply = chr$(length(sFN)) + sFN
- vHandleTxData(cmdReadFileACK,sReply)
- }
- break
- case cmdWriteFile
- // Extract the filename
- sFN = sGetRxFileName()
- // Write the file contents
- iFileError = 1 // Assume an error if we cant even open the file
- iFileHandle=filecreate(sFN)
- // If we managed to create it, write the contents
- if iFileHandle
- {
- iFileError = 0
- // This will set an error if it couldnt write the file
- filewrite(iFileHandle,iFileError, string mid$(sRxData,iRxOffset,iRxLength-iRxOffset)) // Write as many as are available in the buffer
- fileclose(iFileHandle)
- }
- // Ack it
- if iFileError <> 0
- iFileError = 0
- else
- iFileError = 1
- // Build the reply string
- sReply = chr$(length(sFN)) + sFN + chr$(iFileError)
- vHandleTxData(cmdWriteFileACK,sReply)
- break
- case cmdCRCFile
- sFN = sGetRxFileName()
- // Ack it
- sReply = chr$(length(sFN)) + sFN + hex$(filecrc16(sFN),4)
- vHandleTxData(cmdCRCFileACK,sReply)
- break
- case cmdDeleteFile
- // Extract the filename
- sFN = sGetRxFileName()
- filedelete(sFN)
- // Ack it
- sReply = chr$(length(sFN)) + sFN
- vHandleTxData(cmdDeleteFileACK,sReply)
- break
- case cmdRenameFile
- sFN = sGetRxFileName()
- sFN2 = sGetRxFileName()
- filerename(sFN,sFN2)
- // Ack it
- sReply = chr$(length(sFN)) + sFN + chr$(length(sFN2)) + sFN2
- vHandleTxData(cmdRenameFileACK,sReply)
- break
- case cmdCopyFile
- sFN = sGetRxFileName()
- sFN2 = sGetRxFileName()
- filecopy(sFN,sFN2)
- // Ack it
- sReply = chr$(length(sFN)) + sFN + chr$(length(sFN2)) + sFN2
- vHandleTxData(cmdCopyFileACK,sReply)
- break
- case cmdResetLogger
- vHandleTxData(cmdResetLoggerACK,"")
- txwrite(devESP,u32 $FFFFFFFF)
- // Wait for the bytes to be transmitted
- while txwait(devESP) > 0
- {
- // Should sleep here
- }
- reset // Now we can just unceremoniously reset.
- break
- default
- println "Unknown command..."
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement