Skip to content

Commit

Permalink
autocore: fix sfp unplugged speed display
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsnowwolf committed Feb 2, 2023
1 parent f6d0acb commit 27a331a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package/lean/autocore/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2010-2022 OpenWrt.org
# Copyright (C) 2010-2023 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
Expand Down
79 changes: 40 additions & 39 deletions package/lean/autocore/files/arm/sbin/ethinfo
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#!/bin/sh

a=$(ls /sys/class/net/*/device/uevent | grep -v wlan | awk -F '/' '{print $5}')
b=$(echo "$a" | wc -l)
rm -f /tmp/state/ethinfo

echo -n "[" > /tmp/state/ethinfo

for i in $(seq 1 $b)
do
h=$(echo '{"name":' )
c=$(echo "$a" | sed -n ${i}p)
d=$(ethtool $c)

e=$(echo "$d" | grep "Link detected" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g')
if [ $e = yes ]; then
l=1
else
l=0
fi

f=$(echo "$d" | grep "Speed" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g' | tr -d "Unknown!")
[ -z "$f" ] && f=" - "

g=$(echo "$d" | grep "Duplex" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g')
if [ "$g" == "Full" ]; then
x=1
else
x=0
fi

echo -n "$h \"$c\", \"status\": $l, \"speed\": \"$f\", \"duplex\": $x}," >> /tmp/state/ethinfo
done

sed -i 's/.$//' /tmp/state/ethinfo

echo -n "]" >> /tmp/state/ethinfo

cat /tmp/state/ethinfo
#!/usr/bin/lua
-- Copyright (C) 2022-2023 Tianling Shen <cnsztl@immortalwrt.org>
-- Copyright (C) 2022-2023 Lean

local util = require "luci.util"
local jsonc = require "luci.jsonc"

local eth_info = {}
local ifname, stat
for ifname, stat in pairs(util.ubus("network.device", "status")) do
if ifname:match("^(eth%d+)$") == ifname then
local status, speed, duplex

status = stat.carrier and 1 or 0

if not stat.carrier or not stat.speed or stat.speed:sub(1, 1) == "-" or stat.speed:find("65535") then
speed = " - "
else
speed = stat.speed:sub(1, -2) .. "Mb/s"
end

if speed == " - " then
duplex = 0
elseif stat.speed:sub(-1) == "F" then
duplex = 1
else
duplex = 0
end

eth_info[#eth_info+1] = { name = ifname, status = status,
speed = speed, duplex = duplex }
end
end

table.sort(eth_info,
function(a, b)
return a.name < b.name
end)

print(jsonc.stringify(eth_info))

0 comments on commit 27a331a

Please sign in to comment.