From 5c997a53813a36057b4484dd10a0f36ad7cb85bb Mon Sep 17 00:00:00 2001 From: Qige Zhao Date: Fri, 8 Dec 2017 16:43:16 +0800 Subject: [PATCH] Release "alpha" (#10) * Change directory structure (not verified yet) - arn/ - arn/device/ - arn/device/mngr.lua - arn/device/uhf.lua - arn/device/hal/ - arn/device/hal/hal_raw.lua - arn/utils/ - arn/utils/cache.lua - arn/utils/ccff.lua - arn/utils/debug.lua - arn/utils/rarp.lua - arn/utils/serialize.lua * Update ARN-Mngr * Upgrade for "ARN OMC Agent", add function ccff.file.exists() * Try Makefile * 2017.10.19 Continue ARN-Scripts (v2.0) - Add "radio query lock"; - Handle "dead-lock"; - Add "banner"; - No error on GWS5Kv2, need more tests. * Co-work with ARN-Proto (+arn-agent-omc) * Import "JSON.Encode()" * Assign default region value, and check its type before compare * "rarp": return ip only; let cache valid in next hour --- Makefile | 7 + bin/arn | 5 +- bin/arn-hal-raw | 2 +- bin/arn-mngr | 29 +++- files/banner.failsafe.txt | 16 ++ files/banner.txt | 13 ++ lib/arn/{gws_4k.lua => device/hal/gws_3k.lua} | 45 ++++-- lib/arn/device/hal/gws_4k.lua | 147 ++++++++++++++++++ lib/arn/device/hal/gws_5kv1.lua | 14 ++ lib/arn/{ => device/hal}/gws_5kv2.lua | 33 +++- lib/arn/{ => device/hal}/gws_abb.lua | 2 +- lib/arn/{ => device/hal}/hal_raw.lua | 12 +- lib/arn/{ => device}/mngr.lua | 38 +++-- lib/arn/{ => device}/uhf.lua | 6 +- lib/{qutil => arn/utils}/cache.lua | 4 +- lib/{qutil => arn/utils}/ccff.lua | 19 ++- lib/arn/utils/debug.lua | 26 ++++ lib/arn/utils/json.lua | 62 ++++++++ lib/{qutil => arn/utils}/rarp.lua | 16 +- lib/{qutil => arn/utils}/serialize.lua | 0 20 files changed, 432 insertions(+), 64 deletions(-) create mode 100644 Makefile create mode 100755 files/banner.failsafe.txt create mode 100755 files/banner.txt rename lib/arn/{gws_4k.lua => device/hal/gws_3k.lua} (70%) mode change 100644 => 100755 create mode 100755 lib/arn/device/hal/gws_4k.lua create mode 100755 lib/arn/device/hal/gws_5kv1.lua rename lib/arn/{ => device/hal}/gws_5kv2.lua (80%) mode change 100644 => 100755 rename lib/arn/{ => device/hal}/gws_abb.lua (99%) rename lib/arn/{ => device/hal}/hal_raw.lua (94%) rename lib/arn/{ => device}/mngr.lua (93%) mode change 100644 => 100755 rename lib/arn/{ => device}/uhf.lua (88%) rename lib/{qutil => arn/utils}/cache.lua (96%) rename lib/{qutil => arn/utils}/ccff.lua (88%) mode change 100644 => 100755 create mode 100644 lib/arn/utils/debug.lua create mode 100644 lib/arn/utils/json.lua rename lib/{qutil => arn/utils}/rarp.lua (84%) rename lib/{qutil => arn/utils}/serialize.lua (100%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c68f654 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +# 6Harmonics Inc +# maintainer: Qige Zhao +# updated on: 2017.10.19 + +all: + +clean: diff --git a/bin/arn b/bin/arn index 1e62e21..217cbf7 100755 --- a/bin/arn +++ b/bin/arn @@ -1,8 +1,9 @@ #!/bin/sh +# 2017.10.19 continue ARN-Scripts +# TODO: JSON not supported yet -VERSION="ARN-Scripts v1.0-170821Q" +VERSION="ARN-Scripts v2.0-191017Q" ARN_MNGR=/usr/sbin/arn-mngr -#ARN_MNGR_JSON=/usr/sbin/arn-mngr-json version() { echo "$VERSION" diff --git a/bin/arn-hal-raw b/bin/arn-hal-raw index ea97e62..de0a6a1 100755 --- a/bin/arn-hal-raw +++ b/bin/arn-hal-raw @@ -4,7 +4,7 @@ -- Provide unified commands -- by Qige -local gws_hal = require 'arn.hal_raw' +local gws_hal = require 'arn.device.hal.hal_raw' local sfmt = string.format local arg_key = tostring(arg[1]) diff --git a/bin/arn-mngr b/bin/arn-mngr index 7a0c335..020b788 100755 --- a/bin/arn-mngr +++ b/bin/arn-mngr @@ -8,26 +8,28 @@ Tasks: 2. read limits from "/etc/config/arn-spec" 3. finally, set via "arn.hal_raw" 4. "arn.mngr" provide CACHE - 5. "qutil.rarp" provide RARP + 5. "qutil.rarp" provide RARP (+rarp-client +rarp-server) Maintainer: by Qige - 2017.08.18|2017.08.22 + 2017.08.18|2017.08.22 v1.0 + 2017.08.29 v1.1 + 2017.10.19 v2.0 continue ARN-Scripts; plan to add ARN-iOMC3-Agent ]]-- --local DBG = print local function DBG(msg) end -local mngr = require 'arn.mngr' -local rarp = require 'qutil.rarp' -local ccff = require 'qutil.ccff' +local mngr = require 'arn.device.mngr' +local rarp = require 'arn.utils.rarp' +local ccff = require 'arn.utils.ccff' local is_array = ccff.val.is_array local sfmt = string.format local ssub = string.sub local slen = string.len local exec = ccff.execute -local VERSION = 'ARN-Scripts (+HAL +Cache +5K/+4K) v2017.08.18 ' +local VERSION = 'ARN-Scripts (+HAL +Cache +5Kv2) v2.0-191017Q' local arg_key = tostring(arg[1]) -- nil > 'nil' local arg_val = tostring(arg[2]) -- nil > 'nil' @@ -121,9 +123,22 @@ local function format_radio(key, obj) return result .. '\n' end +local function format_thrpt(val) + local thrpt = '0.00 Kbps' + if (val) then + local bps = tonumber(val) + if (bps > 1024 * 1024) then + thrpt = sfmt("%.2f Mbps", (bps / 1024 / 1024)) + else + thrpt = sfmt("%.2f Kbps", (bps / 1024)) + end + end + return thrpt +end + local function format_nw(key, obj) local result = sfmt(' thrpt: Rx %s, Tx %s ("arn -m" ONLY, not accurate)', - obj.rx or '0.00 Kbps', obj.tx or '0.00 Kbps') + format_thrpt(obj.rx), format_thrpt(obj.tx)) return result end diff --git a/files/banner.failsafe.txt b/files/banner.failsafe.txt new file mode 100755 index 0000000..a49013f --- /dev/null +++ b/files/banner.failsafe.txt @@ -0,0 +1,16 @@ +================= FAILSAFE MODE active =================== + Thank you for choosing + __ _ _ _ + / /_ | | | | __ _ _ __ _ __ ___ ___ _ __ (_) ___ ___ + | '_ \| |_| |/ _` | '__| '_ ` _ \ / _ \| '_ \| |/ __/ __| + | (_) | _ | (_| | | | | | | | | (_) | | | | | (__\__ \ + \___/|_| |_|\__,_|_| |_| |_| |_|\___/|_| |_|_|\___|___/ + + --------------------------------------------------------- + ARN Beijing Firmware + --------------------------------------------------------- + * Kernel Dawen Zhang, Yaoyang Wang + * Application Yu Zhou, Qige Zhao + --------------------------------------------------------- +========================================================== + diff --git a/files/banner.txt b/files/banner.txt new file mode 100755 index 0000000..889e785 --- /dev/null +++ b/files/banner.txt @@ -0,0 +1,13 @@ + Thank you for choosing + __ _ _ _ + / /_ | | | | __ _ _ __ _ __ ___ ___ _ __ (_) ___ ___ + | '_ \| |_| |/ _` | '__| '_ ` _ \ / _ \| '_ \| |/ __/ __| + | (_) | _ | (_| | | | | | | | | (_) | | | | | (__\__ \ + \___/|_| |_|\__,_|_| |_| |_| |_|\___/|_| |_|_|\___|___/ + + --------------------------------------------------------- + ARN Beijing Firmware + --------------------------------------------------------- + * Kernel Dawen Zhang, Yaoyang Wang + * Application Yu Zhou, Qige Zhao + --------------------------------------------------------- diff --git a/lib/arn/gws_4k.lua b/lib/arn/device/hal/gws_3k.lua old mode 100644 new mode 100755 similarity index 70% rename from lib/arn/gws_4k.lua rename to lib/arn/device/hal/gws_3k.lua index 2957ba9..6ea4fc4 --- a/lib/arn/gws_4k.lua +++ b/lib/arn/device/hal/gws_3k.lua @@ -1,17 +1,18 @@ --[[ Note: - Although GWS4K share the same methods and functions, + Although GWS3K share the same methods and functions, this copy will let maintainer handle 2 types of hardware. by Qige - 2017.08.16 update_rt + 2017.08.16 update_rt, copied from gws_4k.lua + 2017.10.19 dismiss error, function and data not verified ]]-- local DBG = print --local function DBG(msg) end -local ccff = require 'qutil.ccff' -local uhf = require 'arn.uhf' +local ccff = require 'arn.utils.ccff' +local uhf = require 'arn.device.uhf' local exec = ccff.execute local vint = ccff.val.n @@ -25,11 +26,11 @@ gws_radio.conf = {} gws_radio.conf.val_length_max = 8 gws_radio.cmd = {} -gws_radio.cmd.rfinfo_clean = 'echo > /tmp/.GWS4K.tmp' -gws_radio.cmd.rfinfo = 'rfinfo 2>/dev/null > /tmp/.GWS4K.tmp' -gws_radio.cmd.region = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep Region -A1 | grep [01]* -o' -gws_radio.cmd.channel = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep Channel -A1 | grep [0-9]* -o' -gws_radio.cmd.txpower = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep ^Tx | grep Power | grep [0-9\.]* -o' +gws_radio.cmd.rfinfo_clean = 'echo > /tmp/.GWS3K.tmp' +gws_radio.cmd.rfinfo = 'rfinfo 2>/dev/null > /tmp/.GWS3K.tmp' +gws_radio.cmd.region = 'cat /tmp/.GWS3K.tmp 2> /dev/null | grep Region -A1 | grep [01]* -o' +gws_radio.cmd.channel = 'cat /tmp/.GWS3K.tmp 2> /dev/null | grep Channel -A1 | grep [0-9]* -o' +gws_radio.cmd.txpower = 'cat /tmp/.GWS3K.tmp 2> /dev/null | grep ^Tx | grep Power | grep [0-9\.]* -o' gws_radio.cmd.chanbw = 'uci get wireless.radio0.chanbw' gws_radio.cmd.region_set = 'setregion %s 2> /dev/null ' @@ -39,10 +40,14 @@ gws_radio.cmd.chanbw_set = 'setchanbw %s 2> /dev/null ' gws_radio.cmd.rxgain_set = 'setrxgain %s 2> /dev/null ' function gws_radio.update_init() - DBG(sfmt("GWS4K----> update_init()")) + DBG(sfmt("GWS3K----> update_init()")) exec(gws_radio.cmd.rfinfo) end +function gws_radio.rfinfo_clean() + exec(gws_radio.cmd.rfinfo_clean) +end + --[[ Tasks: 1. Return each value by key; @@ -73,22 +78,22 @@ Tasks: 2. Fetch each parameters from tmp file. ]]-- function gws_radio.UPDATE_RT() - DBG(sfmt("GWS4K> update_rt (@%d)", os.time())) + DBG(sfmt("GWS3K> update_rt (@%d)", os.time())) local result = {} gws_radio.update_init() - DBG(sfmt("GWS4K----> update_item() region")) + DBG(sfmt("GWS3K----> update_item() region")) result.region = gws_radio.update_item('region') - DBG(sfmt("GWS4K----> update_item() channel")) + DBG(sfmt("GWS3K----> update_item() channel")) result.channo = gws_radio.update_item('channel') result.freq = uhf.channel_to_freq(result.region, result.channo) - DBG(sfmt("GWS4K----> update_item() txpower")) + DBG(sfmt("GWS3K----> update_item() txpower")) result.txpwr = gws_radio.update_item('txpower') - DBG(sfmt("GWS4K----> update_item() chanbw")) + DBG(sfmt("GWS3K----> update_item() chanbw")) result.chanbw = gws_radio.update_item('chanbw') --result.ts = os.time() @@ -96,17 +101,23 @@ function gws_radio.UPDATE_RT() end function gws_radio.SET_RT(key, value) - DBG(sfmt("GWS4K> set_rt k=%s,value=%s (@%d)", key or '-', value or '-', os.time())) + local result = true + DBG(sfmt("GWS3K> set_rt k=%s,value=%s (@%d)", key or '-', value or '-', os.time())) if (key == 'region') then exec(sfmt(gws_radio.cmd.region_set, value)) + result = false elseif (key == 'channel' or key == 'channo') then exec(sfmt(gws_radio.cmd.channel_set, value)) + result = false elseif (key == 'txpower' or key == 'txpwr') then exec(sfmt(gws_radio.cmd.txpower_set, value)) + result = false elseif (key == 'rxgain') then exec(sfmt(gws_radio.cmd.rxgain_set, value)) + result = false end - exec(gws_radio.cmd.rfinfo_clean) + gws_radio.rfinfo_clean() + return result end return gws_radio \ No newline at end of file diff --git a/lib/arn/device/hal/gws_4k.lua b/lib/arn/device/hal/gws_4k.lua new file mode 100755 index 0000000..f8ee56a --- /dev/null +++ b/lib/arn/device/hal/gws_4k.lua @@ -0,0 +1,147 @@ +--[[ +Note: + Although GWS4K share the same methods and functions, + this copy will let maintainer handle 2 types of hardware. + + by Qige + 2017.08.16 update_rt +]]-- + +local DBG = print +--local function DBG(msg) end + +local ccff = require 'arn.utils.ccff' +local uhf = require 'arn.device.uhf' + +local exec = ccff.execute +local vint = ccff.val.n +local sfmt = string.format +local ssub = string.sub +local slen = string.len + +local gws_radio = {} + +gws_radio.conf = {} +gws_radio.conf.val_length_max = 8 + +gws_radio.cmd = {} +gws_radio.cmd.rfinfo_clean = 'echo > /tmp/.GWS4Kv2.tmp' +gws_radio.cmd.rfinfo_lock = '/tmp/.GWS4Kv2.lock' +gws_radio.cmd.rfinfo_wait = 'sleep 2' +gws_radio.cmd.rfinfo = 'rfinfo 2>/dev/null > /tmp/.GWS4K.tmp' +gws_radio.cmd.region = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep Region -A1 | grep [01]* -o' +gws_radio.cmd.channel = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep Channel -A1 | grep [0-9]* -o' +gws_radio.cmd.txpower = 'cat /tmp/.GWS4K.tmp 2> /dev/null | grep ^Tx | grep Power | grep [0-9\.]* -o' +gws_radio.cmd.chanbw = 'uci get wireless.radio0.chanbw' + +gws_radio.cmd.region_set = 'setregion %s 2> /dev/null ' +gws_radio.cmd.channel_set = 'setchan %s 2> /dev/null ' +gws_radio.cmd.txpower_set = 'settxpwr %s 2> /dev/null ' +gws_radio.cmd.chanbw_set = 'setchanbw %s 2> /dev/null ' +gws_radio.cmd.rxgain_set = 'setrxgain %s 2> /dev/null ' + +function gws_radio.update_init() + DBG(sfmt("GWS4K----> update_init()")) + -- v2.0 2017.10.19 enable read lock + rfinfo_lock = fread(gws_radio.cmd.rfinfo_lock) + if (rfinfo_lock ~= 'lock' and rfinfo_lock ~= 'lock\n') then + print(sfmt('%80s', '> updating radio <')) + DBG('note> updating device < lock:', rfinfo_lock) + fwrite(gws_radio.cmd.rfinfo_lock, 'lock') + exec(gws_radio.cmd.rfinfo) + fwrite(gws_radio.cmd.rfinfo_lock, 'unlock') + DBG('note> updated') + else + print(sfmt('%80s', '> device busy <')) + lock_counts = 3 + while(rfinfo_lock == 'lock' or rfinfo_lock == 'lock\n') do + exec(gws_radio.cmd.rfinfo_wait) + rfinfo_lock = fread(gws_radio.cmd.rfinfo_lock) + lock_counts = lock_counts - 1 + if (lock_counts < 0) then + print(sfmt('%80s', 'solving dead-lock')) + break + end + end + fwrite(gws_radio.cmd.rfinfo_lock, 'unlock') -- FIXME + end + --print(exec(gws_radio.cmd.rfinfo_all)) +end + +function gws_radio.rfinfo_clean() + exec(gws_radio.cmd.rfinfo_clean) +end + +--[[ +Tasks: + 1. Return each value by key; + 2. If result is too long, return first 8 chars. +]]-- +function gws_radio.update_item(key) + local result + if (key == 'region') then + result = exec(gws_radio.cmd.region) + elseif (key == 'channel' or key == 'channo') then + result = exec(gws_radio.cmd.channel) + elseif (key == 'txpower' or key == 'txpwr') then + result = exec(gws_radio.cmd.txpower) + elseif (key == 'chanbw') then + result = exec(gws_radio.cmd.chanbw) + end + -- limit return length + local lmax = gws_radio.conf.val_length_max + if (result and slen(result) > lmax) then + result = ssub(result, 1, lmax) + end + return vint(result) +end + +--[[ +Tasks: + 1. Do cli call; + 2. Fetch each parameters from tmp file. +]]-- +function gws_radio.UPDATE_RT() + DBG(sfmt("GWS4K> update_rt (@%d)", os.time())) + local result = {} + + gws_radio.update_init() + + DBG(sfmt("GWS4K----> update_item() region")) + result.region = gws_radio.update_item('region') + + DBG(sfmt("GWS4K----> update_item() channel")) + result.channo = gws_radio.update_item('channel') + result.freq = uhf.channel_to_freq(result.region, result.channo) + + DBG(sfmt("GWS4K----> update_item() txpower")) + result.txpwr = gws_radio.update_item('txpower') + + DBG(sfmt("GWS4K----> update_item() chanbw")) + result.chanbw = gws_radio.update_item('chanbw') + + --result.ts = os.time() + return result +end + +function gws_radio.SET_RT(key, value) + local result = true + DBG(sfmt("GWS4K> set_rt k=%s,value=%s (@%d)", key or '-', value or '-', os.time())) + if (key == 'region') then + exec(sfmt(gws_radio.cmd.region_set, value)) + result = false + elseif (key == 'channel' or key == 'channo') then + exec(sfmt(gws_radio.cmd.channel_set, value)) + result = false + elseif (key == 'txpower' or key == 'txpwr') then + exec(sfmt(gws_radio.cmd.txpower_set, value)) + result = false + elseif (key == 'rxgain') then + exec(sfmt(gws_radio.cmd.rxgain_set, value)) + result = false + end + gws_radio.rfinfo_clean() + return result +end + +return gws_radio \ No newline at end of file diff --git a/lib/arn/device/hal/gws_5kv1.lua b/lib/arn/device/hal/gws_5kv1.lua new file mode 100755 index 0000000..8c5a54e --- /dev/null +++ b/lib/arn/device/hal/gws_5kv1.lua @@ -0,0 +1,14 @@ +--[[ +Note: + Although GWS5Kv1 & GWS5Kv2 share the same methods and functions, + this copy will let maintainer handle 2 types of hardware. + + by Qige + almost same as GWS5Kv2, it's a wrapper +]]-- + +--local DBG = print +local function DBG(msg) end + +local gws_radio = require 'arn.device.hal.gws_5kv2' +return gws_radio \ No newline at end of file diff --git a/lib/arn/gws_5kv2.lua b/lib/arn/device/hal/gws_5kv2.lua old mode 100644 new mode 100755 similarity index 80% rename from lib/arn/gws_5kv2.lua rename to lib/arn/device/hal/gws_5kv2.lua index a01fc4b..1be2871 --- a/lib/arn/gws_5kv2.lua +++ b/lib/arn/device/hal/gws_5kv2.lua @@ -14,8 +14,8 @@ BugList: --local DBG = print local function DBG(msg) end -local ccff = require 'qutil.ccff' -local uhf = require 'arn.uhf' +local ccff = require 'arn.utils.ccff' +local uhf = require 'arn.device.uhf' local exec = ccff.execute local vint = ccff.val.n @@ -23,6 +23,9 @@ local sfmt = string.format local ssub = string.sub local slen = string.len +local fread = ccff.file.read +local fwrite = ccff.file.write + local gws_radio = {} gws_radio.conf = {} @@ -30,6 +33,8 @@ gws_radio.conf.val_length_max = 8 gws_radio.cmd = {} gws_radio.cmd.rfinfo_clean = 'echo > /tmp/.GWS5Kv2.tmp' +gws_radio.cmd.rfinfo_lock = '/tmp/.GWS5Kv2.lock' +gws_radio.cmd.rfinfo_wait = 'sleep 1' gws_radio.cmd.rfinfo = 'echo > /tmp/.GWS5Kv2.tmp; `which gws5001app` rfinfo >/dev/null 2>&1; `which gws5001app` rfinfo 2>/dev/null > /tmp/.GWS5Kv2.tmp' gws_radio.cmd.rfinfo_all = 'cat /tmp/.GWS5Kv2.tmp 2>/dev/null' gws_radio.cmd.region = 'cat /tmp/.GWS5Kv2.tmp 2> /dev/null | grep Region | grep [01]* -o' @@ -47,7 +52,29 @@ gws_radio.cmd.txchain_set = '`which gws5001app` rf%s 2> /dev/null ' function gws_radio.rfinfo_init() DBG(sfmt("GWS5Kv2----> update_init()")) - exec(gws_radio.cmd.rfinfo) + -- v2.0 2017.10.19 enable read lock + rfinfo_lock = fread(gws_radio.cmd.rfinfo_lock) + if (rfinfo_lock ~= 'lock' and rfinfo_lock ~= 'lock\n') then + print(sfmt('%80s', '> updating radio <')) + DBG('note> updating device < lock:', rfinfo_lock) + fwrite(gws_radio.cmd.rfinfo_lock, 'lock') + exec(gws_radio.cmd.rfinfo) + fwrite(gws_radio.cmd.rfinfo_lock, 'unlock') + DBG('note> updated') + else + print(sfmt('%80s', '> device busy <')) + lock_counts = 3 + while(rfinfo_lock == 'lock' or rfinfo_lock == 'lock\n') do + exec(gws_radio.cmd.rfinfo_wait) + rfinfo_lock = fread(gws_radio.cmd.rfinfo_lock) + lock_counts = lock_counts - 1 + if (lock_counts < 0) then + print(sfmt('%80s', 'solving dead-lock')) + break + end + end + fwrite(gws_radio.cmd.rfinfo_lock, 'unlock') -- FIXME + end --print(exec(gws_radio.cmd.rfinfo_all)) end diff --git a/lib/arn/gws_abb.lua b/lib/arn/device/hal/gws_abb.lua similarity index 99% rename from lib/arn/gws_abb.lua rename to lib/arn/device/hal/gws_abb.lua index 4b7fa16..23fa2a9 100644 --- a/lib/arn/gws_abb.lua +++ b/lib/arn/device/hal/gws_abb.lua @@ -16,7 +16,7 @@ local function DBG(msg) end local iwinfo = require "iwinfo" -local ccff = require "qutil.ccff" +local ccff = require "arn.utils.ccff" local fget = ccff.conf.get local fset = ccff.conf.set local s = ccff.val.s diff --git a/lib/arn/hal_raw.lua b/lib/arn/device/hal/hal_raw.lua similarity index 94% rename from lib/arn/hal_raw.lua rename to lib/arn/device/hal/hal_raw.lua index fa876cd..aa5edfa 100644 --- a/lib/arn/hal_raw.lua +++ b/lib/arn/device/hal/hal_raw.lua @@ -2,7 +2,7 @@ -- 2017.08.01 hw_platform|init|HAL_SET_RT|HAL_GET_RT -- 2017.08.16 GWS4K|GWS5K|cmd_interval -local ccff = require 'qutil.ccff' +local ccff = require 'arn.utils.ccff' local exec = ccff.execute local file_read = ccff.file.read local file_write = ccff.file.write @@ -17,11 +17,11 @@ local function DBG(msg) end local gws_raw = {} -gws_raw.abb = require 'arn.gws_abb' -gws_raw.Util_GWS3K = require 'arn.gws_3k' -gws_raw.Util_GWS4K = require 'arn.gws_4k' -gws_raw.Util_GWS5Kv1 = require 'arn.gws_5kv1' -gws_raw.Util_GWS5Kv2 = require 'arn.gws_5kv2' +gws_raw.abb = require 'arn.device.hal.gws_abb' +gws_raw.Util_GWS3K = require 'arn.device.hal.gws_3k' +gws_raw.Util_GWS4K = require 'arn.device.hal.gws_4k' +gws_raw.Util_GWS5Kv1 = require 'arn.device.hal.gws_5kv1' +gws_raw.Util_GWS5Kv2 = require 'arn.device.hal.gws_5kv2' gws_raw.conf = {} gws_raw.conf.cmd_cpu = 'cat /proc/cpuinfo | grep system' diff --git a/lib/arn/mngr.lua b/lib/arn/device/mngr.lua old mode 100644 new mode 100755 similarity index 93% rename from lib/arn/mngr.lua rename to lib/arn/device/mngr.lua index 3dd8cc9..68e6036 --- a/lib/arn/mngr.lua +++ b/lib/arn/device/mngr.lua @@ -23,9 +23,9 @@ LOG local function DBG(msg) end -- load Utilities -local Ccff = require 'qutil.ccff' -local Cache = require 'qutil.cache' -local Uhf = require 'arn.uhf' +local Uhf = require 'arn.device.uhf' +local Ccff = require 'arn.utils.ccff' +local Cache = require 'arn.utils.cache' local exec = Ccff.execute local conf_get = Ccff.conf.get @@ -42,7 +42,7 @@ local slen = string.len local ssub = string.sub -- load ARN HAL Module -local DEV_HAL = require 'arn.hal_raw' +local DEV_HAL = require 'arn.device.hal.hal_raw' --[[ Module: Device Manager @@ -57,8 +57,9 @@ dev_mngr.conf.config = 'arn' dev_mngr.conf.fcache_set_expiry = 3 --dev_mngr.conf.nw_ifname = 'br-lan' -dev_mngr.conf.nw_ifname = 'eth0' -dev_mngr.conf.nw_cmd_fmt = "cat /proc/net/dev | grep %s | awk '{print $2,$10}' | tr -d '\n'" +dev_mngr.conf.nw_eth0_cmd = "cat /proc/net/dev | grep eth0 | awk '{print $2,$10}'" +dev_mngr.conf.nw_brlan_cmd = "cat /proc/net/dev | grep br-lan | awk '{print $2,$10}'" +dev_mngr.conf.nw_wlan0_cmd = "cat /proc/net/dev | grep wlan0 | awk '{print $2,$10}'" dev_mngr.conf.nw_cache_intl = 5 dev_mngr.conf.fcache_radio = '/tmp/.arn-cache.radio' @@ -146,7 +147,7 @@ function dev_mngr.kpi_cached_raw() DBG("----+ cache ARN Radio timeout, require update right away") radio_hal = dev_mngr.kpi_radio_rt_raw() - DBG(sfmt("--------# region=%s", radio_hal.region)) + DBG(sfmt("--------# region=%s", (radio_hal and radio_hal.region) or '-')) Cache.SAVE(cache_file, radio_hal) end if (not is_array(radio_hal)) then radio_hal = {} end @@ -167,15 +168,18 @@ end function dev_mngr.kpi_nw_counters_rt() local result = {} - local cmd = sfmt(dev_mngr.conf.nw_cmd_fmt, dev_mngr.conf.nw_ifname) + local cmd = sfmt("%s; %s; %s", dev_mngr.conf.nw_eth0_cmd, dev_mngr.conf.nw_brlan_cmd, dev_mngr.conf.nw_wlan0_cmd) local counters = exec(cmd) or '0 0' --print(counters) - rxtx_bytes = ssplit(counters, ' ') + rxtx_bytes = ssplit(counters, ' \\\n') result.rx = 0 result.tx = 0 if (is_array(rxtx_bytes)) then - if (#rxtx_bytes >= 2) then + if (#rxtx_bytes >= 6) then + result.rx = rxtx_bytes[1] + rxtx_bytes[3] + rxtx_bytes[5] + result.tx = rxtx_bytes[2] + rxtx_bytes[4] + rxtx_bytes[6] + elseif (#rxtx_bytes >= 2) then result.rx = rxtx_bytes[1] result.tx = rxtx_bytes[2] end @@ -194,8 +198,9 @@ function dev_mngr.calc_thrpt(bytes1, bytes2, elapsed) local bytes = bytes2 - bytes1 if (bytes < 0) then bytes = 0 - bytes end - local bps = bytes * 8 / elapsed + local thrpt = tonumber(sfmt("%.0f", bytes * 8 / elapsed)) + --[[ if (bps > 1024 * 1024) then thrpt = sfmt("%.2f Mbps", (bps / 1024 / 1024)) elseif (bps > 1024) then @@ -203,6 +208,7 @@ function dev_mngr.calc_thrpt(bytes1, bytes2, elapsed) else thrpt = sfmt("%.2f bps", bps) end + ]]-- return thrpt end @@ -231,8 +237,8 @@ function dev_mngr.kpi_nw_thrpt_calc() end else DBG("--------+ Bad NW Thrpt cache") - result.rx = '0.00 bps' - result.tx = '0.00 bps' + result.rx = 0 + result.tx = 0 DBG(sfmt("--------+ Save NW Thrpt cache (rx=%s,tx=%s)", nw_rxtx_rt.rx, nw_rxtx_rt.tx)) Cache.SAVE(cache_file, nw_rxtx_rt) @@ -414,7 +420,7 @@ function dev_mngr.filter_item_append_unit(item, value) else result = value end - return result + return tostring(result) end --[[ @@ -576,9 +582,9 @@ function dev_mngr.SAFE_GET(with_unit) result.nw_thrpt.tx = nw_thrpt.tx or 0.01 DBG("+ result is safe to use < noise=" .. result.abb_safe.noise) - DBG("+ return result < freq=" .. result.radio_safe.freq) + DBG("+ return result < freq=" .. (result.radio_safe.freq or '-')) return result end -return dev_mngr \ No newline at end of file +return dev_mngr diff --git a/lib/arn/uhf.lua b/lib/arn/device/uhf.lua similarity index 88% rename from lib/arn/uhf.lua rename to lib/arn/device/uhf.lua index 4118f4e..700ccda 100644 --- a/lib/arn/uhf.lua +++ b/lib/arn/device/uhf.lua @@ -51,7 +51,11 @@ function uhf.channel_to_freq(region, channel) DBG(sfmt("--------> (FIXME) channel_to_freq(UHF r=%s, c=%s)", region or '-', channel or '-')) local freq = 470 if (region and channel) then - if (region > 0) then + local rgn = 1 + if (type(region) == 'number') then + rgn = tonumber(region) + end + if (rgn > 0) then freq = freq + (0.5 + channel - 21) * 8 else freq = freq + (0.5 + channel - 14) * 6 diff --git a/lib/qutil/cache.lua b/lib/arn/utils/cache.lua similarity index 96% rename from lib/qutil/cache.lua rename to lib/arn/utils/cache.lua index 2c84fb8..be4497e 100644 --- a/lib/qutil/cache.lua +++ b/lib/arn/utils/cache.lua @@ -5,8 +5,8 @@ --local DBG = print local function DBG(msg) end -local Serializer = require 'qutil.serialize' -local Ccff = require 'qutil.ccff' +local Serializer = require 'arn.utils.serialize' +local Ccff = require 'arn.utils.ccff' local file_read = Ccff.file.read local file_write = Ccff.file.write local vint = Ccff.val.n diff --git a/lib/qutil/ccff.lua b/lib/arn/utils/ccff.lua old mode 100644 new mode 100755 similarity index 88% rename from lib/qutil/ccff.lua rename to lib/arn/utils/ccff.lua index a02c62d..259cafb --- a/lib/qutil/ccff.lua +++ b/lib/arn/utils/ccff.lua @@ -1,6 +1,7 @@ -- cmd/conf/file/filter -- by Qige -- 2017.06.30/2017.08.11 +-- 2017.09.06 ccff.file.exists local uci = require 'uci' @@ -53,12 +54,24 @@ end function ccff.file.write(path, data) if (path and data) then - local fd = io.open(path, "w") - fd:write(data) - fd:close() + local fd = io.open(path, "w+") + if (fd) then + fd:write(data) + fd:close() + else + print('error> write file', path, 'failed') + end end end +function ccff.file.exists(path) + local result = ccff.file.read(path) + if (result) then + return true + end + return false +end + function ccff.file.cp(src, des) local content = ccff.read(src) ccff.write(des, content) diff --git a/lib/arn/utils/debug.lua b/lib/arn/utils/debug.lua new file mode 100644 index 0000000..c840f3e --- /dev/null +++ b/lib/arn/utils/debug.lua @@ -0,0 +1,26 @@ +-- by Qige +-- 2017.08.14 + +local dbg = {} + +function dbg.dump_dec(str) + if (str) then + local index + for index = 1, string.len(str) do + io.write(string.format("B%d-%d ", index, string.byte(str, index))) + end + print() + end +end + +function dbg.dump_hex(str) + if (str) then + local index + for index = 1, string.len(str) do + io.write(string.format("B%d-%02X ", index, string.byte(str, index))) + end + print() + end +end + +return dbg \ No newline at end of file diff --git a/lib/arn/utils/json.lua b/lib/arn/utils/json.lua new file mode 100644 index 0000000..5837567 --- /dev/null +++ b/lib/arn/utils/json.lua @@ -0,0 +1,62 @@ +-- by Qige +-- 2017.09.04 v2.1 + +local sfmt = string.format + +local JSON = {} + +function JSON.Encode(data) + local json + local data_type = type(data) + if (data_type == 'table') then + json = JSON.encode_all(data) + elseif (data_type == 'string') then + json = sfmt('["%s"]', data) + elseif (data_type == 'number') then + json = sfmt('[%d]', data) + end + return json +end + +function JSON.encode_all(data) + local json_str = '' + if (data) then + local str_pair + local data_type = type(data) + if (data_type == 'table') then + str_pair = '{' + local str_re = '' + local idx, val + for idx,val in pairs(data) do + -- check idx is nubmer? + -- check val is table? + if (idx and type(idx) == 'number') then + str_re = sfmt('"%s":[%s]', idx, JSON.encode_all(val)) + else + str_re = sfmt('"%s":%s', idx, JSON.encode_all(val)) + end + if (str_pair ~= '{') then + str_pair = str_pair .. ',' + end + str_pair = str_pair .. str_re + end + str_pair = str_pair .. '}' + elseif (data_type == 'number') then + str_pair = data + elseif (data_type == 'string') then + if (data == 'nil' or data == 'null') then + str_pair = 'null' + else + str_pair = '"' .. data .. '"' + end + else + str_pair = 'null' -- data_type == nil + end + + json_str = str_pair + str_pair = '' + end + return json_str +end + +return JSON \ No newline at end of file diff --git a/lib/qutil/rarp.lua b/lib/arn/utils/rarp.lua similarity index 84% rename from lib/qutil/rarp.lua rename to lib/arn/utils/rarp.lua index 366a9c1..08d7f19 100644 --- a/lib/qutil/rarp.lua +++ b/lib/arn/utils/rarp.lua @@ -5,8 +5,8 @@ --local DBG = print local function DBG(msg) end -local Cache = require 'qutil.cache' -local Ccff = require 'qutil.ccff' +local Cache = require 'arn.utils.cache' +local Ccff = require 'arn.utils.ccff' local exec = Ccff.execute local sfmt = string.format local slen = string.len @@ -17,25 +17,31 @@ local rarp = {} rarp.conf = {} rarp.conf.fcache_rarp = '/tmp/.arn-cache.dev-' rarp.conf.rarp_cmd_fmt = "rarp-client %s 211 | awk '{print $2}' | tr -d '\n'" -rarp.conf.rarp_timeout = 300 +rarp.conf.rarp_timeout = 3600 --[[ Tasks: 1. Read IP from cache; 2. If cache missed, use RARP, & save to cache. ]]-- -function rarp.FETCH_IP(mac) +function rarp.FETCH_IP(mac, flagIPOnly) DBG(sfmt('rarp> FETCH_IP(%s)', mac)) if (mac and slen(mac) >= 17) then local ip_raw = rarp.load_ip_from_cache(mac) if (ip_raw and ip_raw ~= '') then DBG(sfmt('rarp> ----+ cache convert: %s=%s)', mac, ip_raw)) + if (flagIPOnly) then + return ip_raw + end return ip_raw .. ' ' .. ssub(mac, 10, -1) end ip_raw = rarp.rarp_request(mac) if (ip_raw and ip_raw ~= '') then DBG(sfmt('rarp> ----+ rarp convert: %s=%s', mac, ip_raw or '-')) rarp.save_ip_to_cache(mac, ip_raw) + if (flagIPOnly) then + return ip_raw + end return ip_raw .. '+' .. ssub(mac, 10, -1) end return mac @@ -80,4 +86,4 @@ function rarp.save_ip_to_cache(mac, ip) Cache.SAVE(cache_file, cache) end -return rarp \ No newline at end of file +return rarp diff --git a/lib/qutil/serialize.lua b/lib/arn/utils/serialize.lua similarity index 100% rename from lib/qutil/serialize.lua rename to lib/arn/utils/serialize.lua