From fa1eeaa50f7c0596d3b11315996e11d38d9cb4e1 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sun, 12 Aug 2018 02:09:08 +0100 Subject: [PATCH] Rework for LFS --- ctfws-lfs-strings.lua | 46 +++++++++++++++++++++++++++++++++++++++++++ ctfws.lua | 16 +++++++-------- init2.lua | 22 ++++++++++++++++++--- init3.lua | 16 +++++++-------- mklfs.sh | 35 ++++++++++++++++++++++++++++++++ pushall.sh | 20 ++++++++++++++----- 6 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 ctfws-lfs-strings.lua create mode 100755 mklfs.sh diff --git a/ctfws-lfs-strings.lua b/ctfws-lfs-strings.lua new file mode 100644 index 0000000..847f4e7 --- /dev/null +++ b/ctfws-lfs-strings.lua @@ -0,0 +1,46 @@ +local modload = "cron", "cron.entry", "schedule", "unschedule", + "gpio", "HIGH", "LOW", "ALARM_AUTO", + "mqtt.socket", + "math", "floor", "unregister", + "error", "self" + +local ctfws = "ctfws", "ctfws_lcd", "ctfws_tmr", + "setupD", "roundD", "rounds", "startT", "endT", "flagsN", "flagsR", "flagsY", + "times", "config", "deconfig", "setFlags", "setEndTime", + "GAME NOT CONFIGURED!", + "GAME OVER @ %02d:%02d", + "START TIME IN FUTURE", + "TIME IS UP" + +local lcdpreload = + "define_char", + "lcd", "mtmr", "ftmr", "dl_elapsed", "dl_elapsed", "dl_remain", "dl_round", + "attnState", "reset", "drawTimes", "drawFlags", "drawMessage", "drawFlagsMessage", + "%02d:%02d.%d", "%02d.%d", "%d", "%-20s", "%d\000: R=%s Y=%s", + " CMUKGB CTFWS TIMER ", " ", + "GAME :", + "SETUP :", + "START IN :", + "GAME END :", + "JAILBREAK :", + "JB# %d/%d :", + "JB# %2d/%2d :" + +local init3load = + "flg_tmr", "lastMsgTime", "mqttUser", "msg_tmr", "dprint", "mqc", + "nwfmqtt.conf", + "* * * * *", + "ctfws/game/config", + "ctfws/game/endtime", + "ctfws/game/flags", + "ctfws/game/message", + "ctfws/game/message/jail", + "none", + "^%s*(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+).*$", + "^%s*(%d+)%s+(%d+).*$", + "^%s*(%d+)%s*(.*)$", + "^%s*%?.*$", + "^ctfws/game/message", + "CONNECTING...", "MQTT", "MQTT CONNECTED", "MQTT Disconnected", "DHCP %s", + "WIFI %s", "NET", "Trying reconn...", + "alive", "beat %d %s" diff --git a/ctfws.lua b/ctfws.lua index f939377..3b76625 100644 --- a/ctfws.lua +++ b/ctfws.lua @@ -77,12 +77,10 @@ local function setEndTime(self,t) self.endT = t end -return function() - local self = {} - self.times = times - self.config = config - self.deconfig = deconfig - self.setFlags = setFlags - self.setEndTime = setEndTime - return self -end +local self = {} +self.times = times +self.config = config +self.deconfig = deconfig +self.setFlags = setFlags +self.setEndTime = setEndTime +return self diff --git a/init2.lua b/init2.lua index ed70cac..5c5687b 100644 --- a/init2.lua +++ b/init2.lua @@ -1,6 +1,22 @@ +if node.flashindex() == nil then + -- no LFS image. Perhaps it failed to flash? + if file.exists("luac.out.stage") and not file.exists("luac.out") then + -- Looks like we tried once before, indeed. Try again. + node.flashreload("luac.out.stage") + end +end +file.remove("luac.out.stage") -- remove stage file from last attempt +-- Do we have a new LFS blob? If so, install it. +if file.exists("luac.out") then + print("Updating LFS image. Will reboot if things go well.") + file.rename("luac.out", "luac.out.stage") + node.flashreload("luac.out.stage") + error("Failed to update LFS!") +end + -- It's early in boot, so we have plenty of RAM. Compile -- the rest of the firmware from source if it's there. -dofile("compileall.lc") +OVL.compileall() -- Grab some configuration parameters we might need, -- notably, the LCD address @@ -18,7 +34,7 @@ print("init2 hw") wifi.sta.sleeptype(wifi.NONE_SLEEP) -- don't power down radio gpio.mode(5,gpio.OUTPUT) -- beeper on GPIO14 i2c.setup(0,2,1,i2c.SLOW) -- init i2c on GPIO4 and GPIO5 -lcd = dofile("lcd1602.lc")(ctfwshw.lcd or 0x27) +lcd = OVL.lcd1602()(ctfwshw.lcd or 0x27) -- give the LCD time to initialize properly -tmr.create():alarm(125, tmr.ALARM_SINGLE, function() print("init2 go3") dofile("init3.lc") end) +tmr.create():alarm(125, tmr.ALARM_SINGLE, function() print("init2 go3") OVL.init3() end) diff --git a/init3.lua b/init3.lua index 8902a94..f6bdd81 100644 --- a/init3.lua +++ b/init3.lua @@ -3,16 +3,16 @@ dprint = function(...) print(...) end -- ON -- common module initialization -cron.schedule("*/5 * * * *", function(e) dofile("nwfnet-sntp.lc").dosntp(nil) end) +cron.schedule("*/5 * * * *", function(e) OVL["nwfnet-sntp"]().dosntp(nil) end) nwfnet = require "nwfnet" -- Game logic modules -ctfws = dofile("ctfws.lc")() +ctfws = OVL.ctfws() ctfws:setFlags(0,0) msg_tmr = tmr.create() flg_tmr = tmr.create() -ctfws_lcd = dofile("ctfws-lcd.lc")(ctfws, lcd, msg_tmr, flg_tmr) +ctfws_lcd = OVL["ctfws-lcd"]()(ctfws, lcd, msg_tmr, flg_tmr) ctfws_tmr = tmr.create() -- Draw the default display @@ -20,7 +20,7 @@ ctfws_lcd:drawTimes() ctfws_lcd:drawFlagsMessage("BOOT...") -- MQTT plumbing -mqc, mqttUser = dofile("nwfmqtt.lc").mkclient("nwfmqtt.conf") +mqc, mqttUser = OVL.nwfmqtt().mkclient("nwfmqtt.conf") local mqttBootTopic = string.format("ctfws/dev/%s/beat",mqttUser) mqc:lwt(mqttBootTopic,"dead",1,1) @@ -36,9 +36,9 @@ local mqtt_reconn_cronentry local function mqtt_reconn() dprint("Trying reconn...") mqtt_reconn_cronentry = cron.schedule("* * * * *", function(e) - mqc:close(); dofile("nwfmqtt.lc").connect(mqc,"nwfmqtt.conf") + mqc:close(); OVL.nwfmqtt().connect(mqc,"nwfmqtt.conf") end) - dofile("nwfmqtt.lc").connect(mqc,"nwfmqtt.conf") + OVL.nwfmqtt().connect(mqc,"nwfmqtt.conf") end local mqtt_beat_cronentry @@ -163,5 +163,5 @@ end -- hook us up to the network! ctfws_lcd:drawFlagsMessage("CONNECTING...") --- dofile("nwfnet-diag.lc")(true) -dofile("nwfnet-go.lc") +-- OVL["nwfnet-diag"]()(true) +OVL["nwfnet-go"]() diff --git a/mklfs.sh b/mklfs.sh new file mode 100755 index 0000000..6ca1faf --- /dev/null +++ b/mklfs.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +set -e -u + +SOURCES=( + ctfws-lfs-strings.lua + core/_external/lcd1602.lua + core/fifo/fifo.lua + core/net/{fifosock,nwfmqtt,nwfnet*}.lua + core/telnetd/telnetd{,-{diag,file}}.lua + core/util/compileall.lua + core/util/diag.lua + core/util/lfs-strings.lua +) + +rm -rf _lfs_build +mkdir _lfs_build + +# for i in ${SOURCES[@]}; do +# lua5.1 -e "package.path=package.path..';core/_external/luasrcdiet/?.lua'" \ +# core/_external/luasrcdiet/bin/luasrcdiet $i -o _lfs_build/`basename $i` --quiet +# done +cp ${SOURCES[@]} _lfs_build/ + +if [ -z "${LUACROSS:-}" ]; then + LUACROSS=$(readlink -f $(dirname $0)/luac.cross) +fi + +if ! [ -x "${LUACROSS}" ]; then + echo "Need cross compiler! Tried non-existant ${LUACROSS}." + exit 1 +fi + +(cd _lfs_build; $LUACROSS -f *.lua) +ls -l _lfs_build/luac.out diff --git a/pushall.sh b/pushall.sh index e123a2d..5720b14 100755 --- a/pushall.sh +++ b/pushall.sh @@ -5,9 +5,6 @@ set -e -u . ./core/host/pushcommon.sh pushsrc() { - dopushcompile core/util/compileall.lua - dopushlua core/net/nwfmqtt.lua - dopushlua core/_external/lcd1602.lua dopushlua ctfws.lua dopushlua ctfws-lcd.lua dopushlua init3.lua @@ -31,11 +28,20 @@ pushconf() { done } +pushlfs() { + if [ -z ${LUACROSS:-} ]; then + ./core/host/pushinit.sh + else + ./mklfs.sh + dopushtext _lfs_build/luac.out + fi +} + case "${1:-}" in all) pushconf pushsrc - ./core/host/pushinit.sh + pushlfs ;; both) pushconf @@ -44,11 +50,15 @@ case "${1:-}" in src) pushsrc ;; + srcmore) + pushsrc + pushlfs + ;; conf) pushconf ;; *) - echo "Please specify push mode: {conf,src,both,all}" + echo "Please specify push mode: {conf,src,srcmore,both,all}" exit 1 ;; esac -- 2.50.1