Various things

Add persistence code to the eeprom, so it will actually work when
booting OCEmu
convert a print -> cprint
Implement the HTTP side of the internet card
Add clipboard support to the keyboard
Edit the readme to mention luasocket and luasec
This commit is contained in:
gamax92 2015-06-19 12:17:55 -06:00
parent 53f8bcbd13
commit ca66a9e20c
4 changed files with 91 additions and 8 deletions

View File

@ -4,12 +4,16 @@ OCEmu - OpenComputers Emulator
Installation
------------
Needs luafilesystem, utf8, and luaffi
Needs luafilesystem, utf8, and luaffi.
luasocket is required for the Internet Component.
luasec is optional but is required for HTTPS.
```
luarocks-5.2 install luafilesystem
luarocks-5.2 install utf8
git clone https://github.com/colesbury/luaffi.git
git clone https://github.com/gamax92/luaffi.git
cd luaffi
make
sudo cp ffi.so /appropriate/path/for/lua/libraries/

View File

@ -2,10 +2,43 @@ local address, slot, filename = ...
local crc32 = require("support.crc32")
local directory = elsa.filesystem.getSaveDirectory() .. "/" .. address
if not elsa.filesystem.exists(directory) then
elsa.filesystem.createDirectory(directory)
end
local code = elsa.filesystem.read(filename)
local data = ""
local label = "EEPROM"
local readonly = false
if elsa.filesystem.exists(directory .. "/data.lua") then
local fn, err = elsa.filesystem.load(directory .. "/data.lua","t",{})
if not fn then
cprint("Failed to unpersist eeprom @" .. address .. ": " .. err)
else
local ncode,ndata,nlabel,nread = fn()
if type(ncode) ~= "string" or type(ndata) ~= "string" or type(nlabel) ~= "string" or type(nread) ~= "boolean" then
cprint("Failed to unpersist eeprom @" .. address .. ": Invalid persist data")
cprint("code) " .. type(ncode))
cprint("data) " .. type(ndata))
cprint("labl) " .. type(nlabel))
cprint("read) " .. type(nread))
else
code,data,label,readonly = ncode,ndata,nlabel,nread
end
end
end
local function persist()
local file, err = io.open(directory .. "/data.lua", "wb")
if not file then
cprint("Failed to persist eeprom @" .. address .. ": " .. err)
return false
end
file:write(string.format("return %q,%q,%q,%s",code,data,label,tostring(readonly)):gsub("\\\n","\\n") .. "")
file:close()
return true
end
-- eeprom component
local obj = {}
@ -22,6 +55,7 @@ function obj.setData(newdata) -- Overwrite the currently stored byte array.
error("not enough space",3)
end
data = newdata
persist()
end
function obj.getDataSize() -- Get the storage capacity of this EEPROM.
cprint("eeprom.getDataSize")
@ -43,6 +77,7 @@ function obj.setLabel(newlabel) -- Set the label of the EEPROM.
compCheckArg(1,newlabel,"string","nil")
if newlabel == nil then newlabel = "EEPROM" end
label = newlabel:sub(1,16)
persist()
return label
end
function obj.getChecksum() -- Get the checksum of the data on this EEPROM.
@ -64,14 +99,16 @@ function obj.set(newcode) -- Overwrite the currently stored byte array.
error("not enough space",3)
end
code = newcode
persist()
end
function obj.makeReadonly(checksum) -- Make this EEPROM readonly if it isn't already. This process cannot be reversed!
print("eeprom.makeReadonly", checksum)
cprint("eeprom.makeReadonly", checksum)
compCheckArg(1,checksum,"string")
if checksum ~= obj.getChecksum() then
return nil, "incorrect checksum"
end
readonly = true
persist()
return true
end

View File

@ -6,6 +6,11 @@ if not okay then
return
end
local url = require("socket.url")
local okay, http = pcall(require, "ssl.https")
if not okay then
cprint("Cannot use HTTPS: " .. http)
http = require("socket.http")
end
component.connect("filesystem",gen_uuid(),nil,"lua/component/internet",true)
@ -94,17 +99,48 @@ function obj.connect(address, port) -- Opens a new TCP connection. Returns the h
return fakesocket
end
function obj.request(url, postData) -- Starts an HTTP request. If this returns true, further results will be pushed using `http_response` signals.
--STUB
cprint("internet.request",url, postData)
compCheckArg(1,url,"string")
if not config.get("internet.enableHttp",true) then
return nil, "http requests are unavailable"
end
local post
if type(postData) == "string" then
post = postData
-- TODO: Check for too many connections
if type(postData) ~= "string" then
postData = nil
end
-- TODO: This works ... but is slow.
local page, _, headers, status = http.request(url, postData)
local protocol, code, message = status:match("(.-) (.-) (.*)")
code = tonumber(code)
local fakesocket = {
read = function(n)
cprint("(socket) read",n)
-- OC doesn't actually return n bytes when requested.
if page == nil then
return nil, "connection lost"
elseif page == "" then
return nil
else
-- Return up to 8192 bytes
local data = page:sub(1,8192)
page = page:sub(8193)
return data
end
end,
response = function()
cprint("(socket) response")
return code, message, headers
end,
close = function()
cprint("(request) close")
page = nil
end,
finishConnect = function()
cprint("(socket) finishConnect")
return true
end
}
return fakesocket
end
local cec = {}

View File

@ -2,6 +2,7 @@ local address = ...
local ffi = require("ffi")
local lua_utf8 = require("utf8")
local SDL = elsa.SDL
-- Conversion table for SDL2 keys to LWJGL key codes
local keys = require("support.sdl_to_lwjgl")
@ -21,6 +22,11 @@ function elsa.keydown(event)
local key = keyevent.keysym.scancode
cprint("keydown",keys[key])
table.insert(kbdcodes,{type="key_down",addr=address,code=keys[key]})
if keys[key] == 210 then
if SDL.hasClipboardText() > 0 then
table.insert(machine.signals,{"clipboard",address,ffi.string(SDL.getClipboardText())})
end
end
end
function elsa.keyup(event)