A few adjustments to culib, a fix to relib, and adding a few missing things

This commit is contained in:
20kdc 2017-03-23 01:10:09 +00:00
parent 2c13ad9469
commit 7f9623ddbd
8 changed files with 143 additions and 5 deletions

View File

@ -36,13 +36,13 @@ return function (hostname, transmit, onReceive, time)
-- How many packets need to be stored in seenBefore's keyspace
-- before 'panic' is the best response?
local tuningMaxSeenBeforeCountBeforeEmergencyFlush = 0x300
local tuningMaxSeenBeforeCountBeforeEmergencyFlush = 0x100
-- Prevents OOM by LKR cache flooding - how many entries can the LKR have, max?
-- (Though spamming packets from many sources is now a viable method for dropping LKR,
-- it used to be a viable OOM method.)
-- Note that setting this to 0 or less will effectively result in a value of 1.
local tuningMaxLKREntries = 0x400
local tuningMaxLKREntries = 0x200
-- Expect another packet after this amount of time,
-- or else clear the known receivers cache entry.

View File

@ -1,3 +1,5 @@
-- I, 20kdc, release this into the public domain.
-- No warranty is provided, implied or otherwise.
-- Chat client
local serv = ({...})[1]
local event = require("event")

View File

@ -1,3 +1,5 @@
-- I, 20kdc, release this into the public domain.
-- No warranty is provided, implied or otherwise.
-- Chat server.
local occure = require("occure")
local event = require("event")

131
oc/app/iot-mgr.lua Normal file
View File

@ -0,0 +1,131 @@
-- I, 20kdc, release this into the public domain.
-- No warranty is provided, implied or otherwise.
-- Controller for objects following the 'IoT protocol'
-- (see <soontobe> provided microcontroller source)
local occure = require("occure")
local event = require("event")
local args = {...}
local cmdinfo = {
["help"] = {"Lists the commands"},
["discover"] = {"Sends a packet on the discovery port to a given address", "target"},
["get"] = {"Gets a value", "target", "index"},
["set"] = {"Sets a value", "target", "index", "data"},
["invoke"] = {"Runs an action", "target", "index", "data"}
}
local function packet(tp, idx, data)
return string.char(idx + (tp * 64)) .. data
end
local didGet = false
local getTarg = 0
local function getHelper(tp, tfrom, tto, p, d, u)
if tp ~= "copper_packet" then return end
if p == 4 then
if tto == occure.getHostname() then
if d:sub(1, 1) == packet(3, getTarg, "") then
print(d)
didGet = true
end
end
end
end
local function displayDisc(d)
local types = {
[0] = "void",
[0x40] = "void=",
[0x80] = "action(void)",
[1] = "string",
[0x41] = "string=",
[0x81] = "action(string)",
[2] = "boolean",
[0x42] = "boolean=",
[0x82] = "action(boolean)",
[3] = "float",
[0x43] = "float=",
[0x83] = "action(float)",
[4] = "descriptor",
[0x44] = "descriptor=",
[0x84] = "action(descriptor)",
}
while #d > 7 do
local tp = d:byte()
local n = types[tp] or ("unknown " .. tp)
print(" " .. d:sub(2, 8) .. ": " .. n)
d = d:sub(9)
end
end
local function discHelper(tp, tfrom, tto, p, d, u)
if tp ~= "copper_packet" then return end
if p == 4 then
if tto == occure.getHostname() then
if d:byte() == 0xC0 then
-- Discovery response.
print("\"" .. tfrom .. "\"")
displayDisc(d:sub(2))
end
end
end
end
local commands = {
["help"] = function ()
for k, v in pairs(cmdinfo) do
print(k .. ": " .. v[1])
for i = 2, #v do
print(" " .. v[i])
end
end
end,
["discover"] = function (target)
occure.output(target, 1, "", true)
event.listen("copper_packet", discHelper)
pcall(os.sleep, 10)
event.ignore("copper_packet", discHelper)
end,
["get"] = function (target, index)
index = tonumber(index)
local ack = false
getTarg = index
didGet = false
occure.output(target, 4, packet(0, index, ""), false, function() ack = true end)
event.listen("copper_packet", getHelper)
local safety = 0
while (not didGet) and (safety < 30) do
os.sleep(1)
safety = safety + 1
end
event.ignore("copper_packet", getHelper)
if not didGet then
if ack then
print("ACK'd but no response - bad parameters likely.")
else
print("Didn't get any response, not even an ACK.")
end
end
end,
["set"] = function (target, index, data)
index = tonumber(index)
local complete = nil
occure.output(target, 4, packet(1, index, data), false, function() complete = "acknowledged!" end, function() complete = "unacknowledged :(" end)
while not complete do
os.sleep(1)
end
print(complete)
end,
["invoke"] = function (target, index, data)
index = tonumber(index)
local complete = nil
occure.output(target, 4, packet(2, index, data), false, function() complete = "acknowledged!" end, function() complete = "unacknowledged :(" end)
while not complete do
os.sleep(1)
end
print(complete)
end
}
if not commands[args[1]] then error("No such command - try 'iot-mgr help'") end
if #args ~= #cmdinfo[args[1]] then error("Parameter count mismatch.") end
commands[args[1]](select(2, table.unpack(args)))

View File

@ -1,3 +1,5 @@
-- I, 20kdc, release this into the public domain.
-- No warranty is provided, implied or otherwise.
local occure = require("occure")
local computer = require("computer")
local event = require("event")

View File

@ -45,7 +45,7 @@ They are, as follows:
Part 3. Discovery & Description
Upon the receipt of any unreliable packet on port 1 that is addressed to "*" or the IoT device name,
Upon the receipt of any packet on port 1 that is addressed to "*" or the IoT device name,
it should send a packet back on port 4 formatted as a Get response packet for variable 0.
Variable 0 is always the Descriptor, which describes the variables and actions available.

View File

@ -40,6 +40,8 @@ The final header byte is the actual indicator of what is in the packet.
0x00 indicates that this is an unreliable packet.
0x01 indicates that this is a reliable packet, expecting acknowledgement.
0x02 indicates that this is an acknowledgement for a reliable packet.
Other packets should be ignored as far as information is concerned,
but as for routing-wise, not dropped.
An example scenario will now be presented:

View File

@ -124,8 +124,7 @@ return function (hostname, transmit, onRReceive, time, culib)
local t = time()
while i <= #timers do
if timers[i][2] <= t then
timers[i][1]()
table.remove(timers, i)
table.remove(timers, i)[1]()
else
i = i + 1
end