Skip to content

Commit

Permalink
Add new omci and int shell libraries to help in decoding MEs
Browse files Browse the repository at this point in the history
  • Loading branch information
djGrrr committed Oct 5, 2024
1 parent 65d8b6e commit d2c32e6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
87 changes: 87 additions & 0 deletions files/common/lib/8311-omci-lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/sh
omci="/usr/bin/omci_pipe.sh"
pcre="/usr/bin/pcre2grep"

_lib_8311_omci() {
return 0
}

_lib_int &>/dev/null || . /lib/functions/int.sh
_lib_hexbin &>/dev/null || . /lib/functions/hexbin.sh

mibs() {
if [ -n "$1" ]; then
me=$(($1))
$omci md | $pcre -o1 "^\|\s+${me}\s+\|\s+(\d+)\b"
else
$omci md | $pcre -o1 -o2 --om-separator=' ' '^\|\s+(\d+)\s+\|\s+(\d+)\s+'
fi
}

mib() {
[ -n "$2" ] || return 1
$omci meg "$(($1))" "$(($2))"
}

mibattr() {
[ -n "$3" ] || return 1
local attr=$(($3)) || return 1
local data=$(mib "$1" "$2") || return $?

echo "$data" | $pcre -o1 -M "^(?s)-{79}\n(\s*${attr}\s+.+?)\n-{79}$"
}

mibattrdata() {
local nl=true
local hexstr=false
local me=
local id=
local attr=

while [ $# -gt 0 ]; do
case "$1" in
-n)
nl=false;
;;
-x)
hexstr=true
;;
*)
if [ -z "$me" ]; then
me="$1"
elif [ -z "$id" ]; then
id="$1"
elif [ -z "$attr" ]; then
attr="$1"
else
return 1
fi
;;
esac
shift
done

local mibattr=$(mibattr "$me" "$id" "$attr") || return $?
local typesize=$(echo "$mibattr" | head -n1 | $pcre --om-separator ' ' -o1 -o2 '\b(\d+)b\s+(\S+)\s+\S+$')
local bytes=$(echo "$typesize" | cut -d' ' -f1)
local type=$(echo "$typesize" | cut -d' ' -f2)

local sint=false
[ "$type" = "SINT" ] && sint=true
if $sint || [ "$type" = "BF" ] || [ "$type" = "ENUM" ] || [ "$type" = "PTR" ] || [ "$type" = "UINT" ]; then
local int="0x$(echo "$mibattr" | head -n2 | tail -n1 | $pcre -o1 '^((?:\s+0x[0-9a-f]{2,8})+)' | sed -r -e 's/0x//g' -e 's/\s+//g')"
local inttype="uint"
$sint && inttype="int"

local size=$((bytes * 8))

"$inttype$size" "$int"
elif [ "$type" = "STR" ]; then
printf $(echo "$mibattr" | tail -n1) | { $hexstr && str2hex || cat; }
! $hexstr && $nl && echo
elif [ "$type" = "TBL" ]; then
printf $(echo "$mibattr" | tail -n1) | xxd -p -c "$bytes"
else
return 1
fi
}
39 changes: 39 additions & 0 deletions files/common/lib/functions/int.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
_lib_int() {
return 0
}

int8() {
int=$(($1 & 0xff))
[ "$int" -gt $((0x80)) ] && echo $((int - 0x100)) || echo $int
}

uint8() {
echo $(($1 & 0xff))
}

int16() {
int=$(($1 & 0xffff))
[ "$int" -ge $((0x8000)) ] && echo $((int - 0x10000)) || echo $int
}

uint16() {
echo $(($1 & 0xffff))
}

int32() {
local int=$(($1 & 0xffffffff))
[ "$int" -ge $((0x80000000)) ] && echo $((int - 0x100000000)) || echo $int
}

uint32() {
echo $(($1 & 0xffffffff))
}

int64() {
echo $(($1))
}

uint64() {
printf '%u\n' "$(int64 "$1")"
}
6 changes: 4 additions & 2 deletions files/common/usr/sbin/8311-extvlan-decode.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh
_lib_8311_omci &>/dev/null || . /lib/8311-omci-lib.sh

HEADER=true
TABLE=false

Expand Down Expand Up @@ -246,7 +248,7 @@ vlan_parse() {
}


ext_vlan_tables=$(omci_pipe.sh md | grep -E -o '^\|\s+171\s+\|\s+(\d+)\s+' | awk '{print $4}')
ext_vlan_tables=$(mibs 171)

i=0
for ext_vlan_table in $ext_vlan_tables; do
Expand All @@ -260,7 +262,7 @@ for ext_vlan_table in $ext_vlan_tables; do
fi
[ "$i" -gt 0 ] && echo

data=$(omci_pipe.sh meg 171 $ext_vlan_table | grep -A999 '6 RX frame VLAN table' | grep -B999 "7 Associated ME ptr" | grep -E '^ ( 0x[0-9a-f]{2}){16}\s*$' | sed -r -e 's/\s+0x//g')
data=$(mibattrdata 171 $ext_vlan_table 6)
for vlan_filter in $data; do
w=$(echo $vlan_filter | sed -r 's/(.{8})/0x\1 /g')
vlan_parse $w
Expand Down

0 comments on commit d2c32e6

Please sign in to comment.