Advertisement
Cremulus

SLOG File Transfer more or less complete.

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