#! /usr/bin/lua
LUA_PATH = "?;?.lua;/usr/bin/?.lua"
ver=tonumber(string.sub(_VERSION, 5))
if(ver == nil) then ver = 0 end
if(ver >= 5.1)then
    package.path=LUA_PATH
end

require "putil"
require "util"

DEF_CACHE='80'
function get_led_lvl()
    local cmd  = "/usr/bin/shared_printenv"
    local m = execr(cmd,1)
    local sf = nfstr(m,'led=(%d+)')
    return sf[3]
end

function set_led_current(v)
    local current = tonumber(get_led_lvl())
    v = tonumber (v)
    if (current ~= v) then
        local cmd  = "/usr/bin/shared_setenv led "..v
        m = execr(cmd,1)
    end
end

function get_led_current()
    current = get_led_lvl()
    if(current == nil) then
        print('DEF')
        current = DEF_CACHE
    end
    return current
end
DEF_BOARD = 'barbados'
AP_BOARD_FN = '/MERAKI_BOARD'
ap_board = read_line(AP_BOARD_FN, DEF_BOARD)

i2c_addr_ant = "32"
DEF_I2C_DART_CONNECT = "1"
I2C_DART_CONNECT = "/meraki_gpio/dart_connector"
i2c_dart_status = read_line(I2C_DART_CONNECT, DEF_I2C_DART_CONNECT)

I2C_ANT_LEDS = "/sys/bus/i2c/devices/0-00"..i2c_addr_ant.."/leds"
i2c_ant_led_exists = false
if (file_exists(I2C_ANT_LEDS) == true) then
    i2c_ant_led_exists = true
end
-- NEW LP5562 chip using old LP5521 driver
DEF_I2C = "30"
I2C_FN = "/storage/i2c_addr"
i2c_addr = read_var(I2C_FN, DEF_I2C)
if i2c_addr == "30" then
-- OLD 5521
-- blue is amber
led_tbl = {
    ["LED_OFF"] =            "4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_BLINKING_BLUE"] =  "40FF600040006000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_BLINKING_GREEN"] = "4000C00000000000000000000000000000000000000000000000000000000000+40FF600040006000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_BLINKING_RED"] =   "4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+40FF600040006000000000000000000000000000000000000000000000000000",
    ["LED_CHIRPING_BLUE"] =  "40FF4D0040007300000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_CHIRPING_GREEN"] = "4000C00000000000000000000000000000000000000000000000000000000000+40FF4D0040007300000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_CHIRPING_RED"] =   "4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+40FF4D0040007300000000000000000000000000000000000000000000000000",
    ["LED_CYCLIC_RGO_LED"] = "E1004000600040006000E0080000000000000000000000000000000000000000+E20040FF600040006000E0020000000000000000000000000000000000000000+40FF600040006000E004E0800000000000000000000000000000000000000000",
    ["LED_CYCLIC_GRO_LED"] = "E2004000600040006000E0040000000000000000000000000000000000000000+40FF600040006000E008E0800000000000000000000000000000000000000000+E10040FF600040006000E0020000000000000000000000000000000000000000",
    ["LED1_CYCLIC_RGB_LED"] ="E10040FF600040006000E0080000000000000000000000000000000000000000+E20040FF600040006000E0020000000000000000000000000000000000000000+40FF600040006000E004E0800000000000000000000000000000000000000000",
    ["LED2_CYCLIC_RGB_FAST_LED"] = "E10040FF600040005000E0080000000000000000000000000000000000000000+E20040FF600040005000E0020000000000000000000000000000000000000000+40FF600040005000E004E0800000000000000000000000000000000000000000",
    ["LED_CYCLIC_GRB_LED"] = "E20040FF600040006000E0040000000000000000000000000000000000000000+40FF600040006000E008E0800000000000000000000000000000000000000000+E10040FF600040006000E0020000000000000000000000000000000000000000",
    ["LED_RED"] =            "4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+40FFC00000000000000000000000000000000000000000000000000000000000",
    ["LED_GREEN"] =          "4000C00000000000000000000000000000000000000000000000000000000000+40FFC00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_BLUE"] =           "40FFC00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000+4000C00000000000000000000000000000000000000000000000000000000000",
    ["LED_1"] =             "e1 1",
    ["LED_2"] =             "e1 2",
    ["LED_3"] =             "e1 3",
    ["LED_4"] =             "e1 4",
    ["LED_5"] =             "e1 5",
    ["LED_6"] =             "e1 6",
    ["LED_7"] =             "e1 7",
    ["LED_8"] =             "e1 8",
    ["LED_HI"] =            "e1 8",
    ["LED_MED"] =           "e1 3",
    ["LED_LOW"] =           "e1 1",
    ["LED_CACHE"] =         "cache"
}
end
local e1_lvl = {
    ["1"] = "2",
    ["2"] = "13",
    ["3"] = "24",
    ["4"] = "35",
    ["5"] = "46",
    ["6"] = "57",
    ["7"] = "68",
    ["8"] = "80",
}

if ap_board == "barbados" then
e1_cmds = {
        "echo $inte > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00"..i2c_addr.."/leds/d1-green/led_current",
        "echo $inte > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00"..i2c_addr.."/leds/d2-blue/led_current",
        "echo $inte > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00"..i2c_addr.."/leds/d0-red/led_current"
}
end

if ap_board == "axel-mvl" then
local e1_cmds = {
        "echo $inte > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00"..i2c_addr.."/leds/d1-green/led_current",
        "echo $inte > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00"..i2c_addr.."/leds/d2-blue/led_current",
        "echo $inte > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00"..i2c_addr.."/leds/d0-red/led_current"
}
end

if ap_board == "axel-qca" or ap_board == "axel-bcm" then
local e1_cmds = {
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr.."/leds/d1-green/led_current",
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr.."/leds/d2-blue/led_current",
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr.."/leds/d0-red/led_current"
}
end

if ((ap_board == "axel-qca") and (i2c_dart_status == "0") and (i2c_ant_led_exists == true)) then
local e1_cmds_ant = {
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr_ant.."/leds/d1-green_ant/led_current",
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr_ant.."/leds/d2-blue_ant/led_current",
        "echo $inte > /sys/bus/i2c/devices/0-00"..i2c_addr_ant.."/leds/d0-red_ant/led_current"
}
end

-- parse and execute
-- led pattern
name = "NONE"
for key, value in pairs( led_tbl ) do
    start_pos,end_pos,dfs_arg = string.find( arg[1] , key )
    if(start_pos ~= nil) then
--        # found pattern
        name = key
        patt = value
    end
end
if(name == "NONE") then
    print ("not found!")
    return
end
led_lvl = nil
sf= nfstr(patt, '^cache')
if (s(sf) > 1) then
   led_lvl = get_led_current()
   patt = 'e1 '..led_lvl
end
sf= nfstr(patt, '^e1 (.+)')
if (s(sf) > 1) then
    local cnt = table.getn(e1_cmds)
    if(led_lvl == nil) then
        led_lvl= e1_lvl[sf[3]]
    end
    set_led_current(led_lvl)
    for i = 1, cnt do
        cmd = e1_cmds[i]
        cmd = string.gsub(cmd, "$inte", led_lvl)
        os.execute(cmd)
    end
    if ((ap_board == "axel-qca") and (i2c_dart_status == "0") and (i2c_ant_led_exists == true)) then
        for i = 1, cnt do
            cmd = e1_cmds_ant[i]
            cmd = string.gsub(cmd, "$inte", led_lvl)
            os.execute(cmd)        
        end
    end
    return
end
start_pos, end_pos,lineRex,lineGex,lineBex=string.find( patt, '(%w+)+(%w+)+(%w+)' )

if ap_board == "barbados" then
line1 = 'echo 0 > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00'..i2c_addr..'/run_engine'
lineR = 'echo "'..lineRex..'" > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00'..i2c_addr..'/engine1_load'
lineG = 'echo "'..lineGex..'" > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00'..i2c_addr..'/engine2_load'
lineB = 'echo "'..lineBex..'" > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00'..i2c_addr..'/engine3_load'
line5 = 'echo 1 > /sys/devices/soc.0/internal-regs.1/f1011000.i2c/i2c-0/0-00'..i2c_addr..'/run_engine'
end

if ap_board == "axel-mvl" then
line1 = 'echo 0 > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00'..i2c_addr..'/run_engine'
lineR = 'echo "'..lineRex..'" > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00'..i2c_addr..'/engine1_load'
lineG = 'echo "'..lineGex..'" > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00'..i2c_addr..'/engine2_load'
lineB = 'echo "'..lineBex..'" > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00'..i2c_addr..'/engine3_load'
line5 = 'echo 1 > /sys/devices/platform/cp0/cp0:config-space/f2701100.i2c/i2c-1/1-00'..i2c_addr..'/run_engine'
end

if ap_board == "axel-qca" or ap_board == "axel-bcm" then
line1 = 'echo 0 > /sys/bus/i2c/devices/0-00'..i2c_addr..'/run_engine'
lineR = 'echo "'..lineRex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr..'/engine1_load'
lineG = 'echo "'..lineGex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr..'/engine2_load'
lineB = 'echo "'..lineBex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr..'/engine3_load'
line5 = 'echo 1 > /sys/bus/i2c/devices/0-00'..i2c_addr..'/run_engine'
end

os.execute(line1)
os.execute(lineR)
os.execute(lineG)
os.execute(lineB)
os.execute(line5)

-- marlin antenna led
if ((ap_board == "axel-qca") and (i2c_dart_status == "0") and (i2c_ant_led_exists == true)) then
line1_ant = 'echo 0 > /sys/bus/i2c/devices/0-00'..i2c_addr_ant..'/run_engine'
lineR_ant = 'echo "'..lineRex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr_ant..'/engine1_load'
lineG_ant = 'echo "'..lineGex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr_ant..'/engine2_load'
lineB_ant = 'echo "'..lineBex..'" > /sys/bus/i2c/devices/0-00'..i2c_addr_ant..'/engine3_load'
line5_ant = 'echo 1 > /sys/bus/i2c/devices/0-00'..i2c_addr_ant..'/run_engine'
os.execute(line1_ant)
os.execute(lineR_ant)
os.execute(lineG_ant)
os.execute(lineB_ant)
os.execute(line5_ant)
end
return
-- verson 1.0
