diff --git a/culib.lua b/culib.lua index 3ee758b..98c4a80 100644 --- a/culib.lua +++ b/culib.lua @@ -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. diff --git a/oc/app/chat-cli.lua b/oc/app/chat-cli.lua index 28d228e..a0845e3 100644 --- a/oc/app/chat-cli.lua +++ b/oc/app/chat-cli.lua @@ -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") diff --git a/oc/app/chat-srv.lua b/oc/app/chat-srv.lua index 6c27cc0..5450ddd 100644 --- a/oc/app/chat-srv.lua +++ b/oc/app/chat-srv.lua @@ -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") diff --git a/oc/app/iot-mgr.lua b/oc/app/iot-mgr.lua new file mode 100644 index 0000000..d54d0dd --- /dev/null +++ b/oc/app/iot-mgr.lua @@ -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 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))) diff --git a/oc/app/ping.lua b/oc/app/ping.lua index 82c676f..cf03507 100644 --- a/oc/app/ping.lua +++ b/oc/app/ping.lua @@ -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") diff --git a/oc/app/protocol.iot b/oc/app/protocol.iot index f9141d8..2880719 100644 --- a/oc/app/protocol.iot +++ b/oc/app/protocol.iot @@ -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. diff --git a/protocol.2 b/protocol.2 index dea1ec4..9d9ce4a 100644 --- a/protocol.2 +++ b/protocol.2 @@ -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: diff --git a/relib.lua b/relib.lua index 9add9f9..9b53fc0 100644 --- a/relib.lua +++ b/relib.lua @@ -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