Advertisement
Cremulus

More adventures of the SLOG file transfer

Jul 14th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. // The FTP transfer utility
  2. // Do NOT look to this for coding style pointers. QAD code.
  3.  
  4. //pragma(1) // pragma(1) Forces a compiler output listing be shown
  5.  
  6. int bSetupOk
  7. int u8RxD
  8. int iCRC,iRxState,iRxStuffed,iRxCMD,iRxLength,iRxCnt
  9. str sRxData
  10. int iTxCRC
  11.  
  12. const
  13. {
  14. // Comms stuff
  15. bSTUFF = $A6
  16. bSOM = $A7
  17. cmdGetDirectory = $44
  18. cmdReadFile = $52
  19. cmdWriteFile = $57
  20. cmdResetLogger = $5A
  21. cmdCRCFile = $43
  22. cmdDeleteFile = $A0
  23. cmdRenameFile = $A1
  24. cmdCopyFile = $A2
  25. // Oh, for enumerated types...
  26. sRxIdle = 0
  27. sRxExpCMD = 1
  28. sRxExpLEN0 = 2
  29. sRxExpLEN1 = 3
  30. sRxExpLEN2 = 4
  31. sRxExpDATA = 5
  32. sRxExpCRCL = 6
  33. sRxExpCRCH = 7
  34. }
  35.  
  36. // Forwards
  37. int vHandleRxChar(int u8RxD);
  38. int vHandleRxMsg();
  39. int vHandleTxByte(int u8TxD);
  40. int vHandleTxData(int iCMD, str sDATA);
  41.  
  42. // Setup the ESP port and BT port
  43. bSetupOk = devicesetup(devESP,500000,parityNONE,2)
  44. // Assume we are setup
  45. iRxState = sRxIdle
  46. iRxStuffed=0
  47. // Now, we wait for a command
  48. repeat
  49. while rxwait(devESP)<1
  50. {
  51. // We should sleep here
  52. }
  53. // We have at least one character waiting
  54. vHandleRxChar(rxread(devESP,u8))
  55. until false
  56.  
  57. // Handle sending a stuffed byte
  58. int vHandleTxByte(int u8TxD)
  59. {
  60. // CRC the damned thing
  61. crc16(iTxCRC,u8TxD)
  62. // Send it
  63. if u8TxD = bSTUFF
  64. {
  65. txwrite(devESP,u8 bSTUFF, 0)
  66. }
  67. else if u8TxD = bSOM
  68. {
  69. txwrite(devESP,u8 bSTUFF, 1)
  70. }
  71. else
  72. txwrite(devESP,u8 u8TxD)
  73. }
  74.  
  75. // Handle a received character
  76. int vHandleRxChar(int u8RxD)
  77. {
  78. if u8RxD = bSOM
  79. {
  80. iRxState = sRxExpCMD
  81. iCRC = $FFFF
  82. }
  83. else if u8RxD = bSTUFF
  84. iRxStuffed = bSTUFF
  85. else
  86. {
  87. // Unstuff data bytes
  88. u8RxD+=iRxStuffed
  89. iRxStuffed=0;
  90.  
  91. switch (iRxState)
  92. {
  93. case sRxExpCMD // Expect CMD
  94. iRxCMD = u8RxD
  95. crc16(iCRC,u8RxD)
  96. iRxState = sRxExpLEN0
  97. break
  98. case sRxExpLEN0 // Expect LEN LSB
  99. iRxLength = u8RxD
  100. crc16(iCRC,u8RxD)
  101. iRxState = sRxExpLEN1
  102. break
  103. case sRxExpLEN1 // Expect LEN MSB
  104. iRxLength += u8RxD * $100
  105. crc16(iCRC,u8RxD)
  106. iRxState = sRxExpLEN2
  107. break
  108. case sRxExpLEN2 // Expect LEN MSB
  109. iRxLength += u8RxD * $10000
  110. crc16(iCRC,u8RxD)
  111. // Do we have any data?
  112. sRxData = ""
  113. iRxCnt = 0
  114. if iRxLength>0
  115. iRxState = sRxExpDATA
  116. else
  117. iRxState = sRxExpCRCL
  118. break
  119. case sRxExpDATA
  120. sRxData = sRxData + chr$(u8RxD)
  121. crc16(iCRC,u8RxD)
  122. if ++iRxCnt >= iRxLength
  123. iRxState = sRxExpCRCL
  124. break;
  125. case sRxExpCRCL
  126. if u8RxD <> (iCRC and $FF)
  127. iRxState = sRxIdle
  128. else
  129. iRxState = sRxExpCRCH
  130. break;
  131. case sRxExpCRCH
  132. if u8RxD = (iCRC >> 8)
  133. {
  134. pragma(2)
  135. vHandleRxMsg()
  136. }
  137. iRxState = sRxIdle
  138. break;
  139. }
  140. }
  141. }
  142.  
  143. // Send a reply
  144. int vHandleTxData(int iCMD, str sDATA)
  145. {
  146. int i,iLength,iTxCRCCopy
  147.  
  148. txwrite(devESP,u8 bSOM)
  149. iTxCRC = $FFFF
  150. vHandleTxByte(iCMD)
  151. iLength = length(sDATA)
  152. vHandleTxByte(iLength and $FF)
  153. vHandleTxByte((iLength >> 8) and $FF)
  154. vHandleTxByte((iLength >> 16) and $FF)
  155. i=0
  156. while i<iLength
  157. {
  158. while txfree(devESP) < 2
  159. {
  160. // Should sleep here
  161. }
  162. // Send the next character (byte)
  163. vHandleTxByte(asc(mid$(sDATA,i++,1)))
  164. }
  165. // Now, send the CRC (Using a copy so we do not change it)
  166. iTxCRCCopy=iTxCRC
  167. vHandleTxByte(iTxCRCCopy)
  168. vHandleTxByte(iTxCRCCopy >> 8)
  169. }
  170.  
  171. // Extract a filename
  172. string sGetRxFileName(int Offset)
  173. {
  174. int len
  175.  
  176. len = asc(mid$(sRxData,Offset,1))
  177. return mid$(sRxData,Offset+1,len)
  178. }
  179.  
  180. // Handle a received message
  181. int vHandleRxMsg()
  182. {
  183. string sFN,sFN2,sFile,sReply
  184. int iFileHandle,iFileError
  185.  
  186. switch(iRxCMD)
  187. {
  188. case cmdGetDirectory
  189. vHandleTxData(cmdGetDirectory,filedir("/"))
  190. break
  191.  
  192. case cmdReadFile
  193. sFN = sGetRxFileName(0)
  194. // Read the files contents
  195. iFileError = 1 // Assume an error if we cant even open the file
  196. iFileHandle=fileopen(sFN)
  197. // If we managed to open it, read the contents
  198. if iFileHandle
  199. {
  200. iFileError = 0
  201. // This will set an error if it couldnt read the file
  202. sFile = fileread(iFileHandle,iFileError, string -1) // Read as many are available
  203. fileclose(iFileHandle)
  204. }
  205. // If it was okay, send it
  206. if iFileError = 0
  207. {
  208. sReply = chr$(length(sFN)) + sFN + sFile
  209. vHandleTxData(cmdWriteFile,sReply)
  210. }
  211. else
  212. {
  213. // Else just reply with the filename
  214. sReply = chr$(length(sFN)) + sFN
  215. vHandleTxData(cmdWriteFile,sReply)
  216. }
  217. break
  218.  
  219. case cmdWriteFile
  220. sFN = sGetRxFileName(0)
  221. println "Write a file",sFN
  222. break
  223.  
  224. case cmdCRCFile
  225. sFN = sGetRxFileName(0)
  226. println "CRC a file",sFN
  227. break
  228.  
  229. case cmdDeleteFile
  230. sFN = sGetRxFileName(0)
  231. filedelete(sFN)
  232. break
  233.  
  234. case cmdRenameFile
  235. sFN = sGetRxFileName(0)
  236. sFN2 = sGetRxFileName(length(sFN)+1)
  237. filerename(sFN,sFN2)
  238. break
  239.  
  240. case cmdCopyFile
  241. sFN = sGetRxFileName(0)
  242. sFN2 = sGetRxFileName(length(sFN)+1)
  243. filecopy(sFN,sFN2)
  244. break
  245.  
  246. case cmdResetLogger
  247. reset // We just unceremoniously reset.
  248. break
  249.  
  250. default
  251. println "Unknown command..."
  252. }
  253. }
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement