luacode.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. """This module contains all the LUA code that needs to be on the device
  3. to perform whats needed. They will be uploaded if they doesn't exist"""
  4. # Copyright (C) 2015-2016 Peter Magnusson <peter@birchroad.net>
  5. # pylint: disable=C0301
  6. LUA_FUNCTIONS = ['recv_block', 'recv_name', 'recv', 'shafile', 'send_block', 'send_file', 'send']
  7. DOWNLOAD_FILE = "file.open('{filename}') print(file.seek('end', 0)) file.seek('set', {bytes_read}) uart.write(0, file.read({chunk_size}))file.close()"
  8. PRINT_FILE = "file.open('{filename}') print('---{filename}---') print(file.read()) file.close() print('---')"
  9. LIST_FILES = 'for key,value in pairs(file.list()) do print(key,value) end'
  10. #NUL = \000, ACK = \006
  11. RECV_LUA = \
  12. r"""
  13. function recv_block(d)
  14. if string.byte(d, 1) == 1 then
  15. size = string.byte(d, 2)
  16. uart.write(0,'\006')
  17. if size > 0 then
  18. file.write(string.sub(d, 3, 3+size-1))
  19. else
  20. file.close()
  21. uart.on('data')
  22. end
  23. else
  24. uart.write(0, '\021' .. d)
  25. uart.on('data')
  26. end
  27. end
  28. function recv_name(d) d = string.gsub(d, '\000', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
  29. function recv() uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
  30. function shafile(f) crypto.toHex(crypto.fhash('sha1', f)) end
  31. """
  32. SEND_LUA = \
  33. r"""
  34. function send_block(d) l = string.len(d) uart.write(0, '\001' .. string.char(l) .. d .. string.rep('#', 128 - l)) return l end
  35. function send_file(f) file.open(f) s=file.seek('end', 0) p=0 uart.on('data', 1, function(data)
  36. if data == '\006' and p<s then file.seek('set',p) p=p+send_block(file.read(128)) else
  37. send_block('') file.close() uart.on('data') print('interrupted') end end, 0) uart.write(0, f .. '\000')
  38. end
  39. function send(f) uart.on('data', 1, function (data)
  40. uart.on('data') if data == 'C' then send_file(f) else print('transfer interrupted') end end, 0)
  41. end
  42. """
  43. UART_SETUP = 'uart.setup(0,{baud},8,0,1,1)'