-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcell-signald
executable file
·162 lines (135 loc) · 3.11 KB
/
cell-signald
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
INTERVAL=60
CELL_MGMT=cell_mgmt
MAX_SLOT_NUM=0
USED_SLOT_NUM=0
SIGNAL_FROM_SLOTS=()
# profile
CONF_DIR=/etc/moxa-cellular-utils
PRODUCT_DIR=${CONF_DIR}/product.d
KVERSION=$(which kversion)
[ -z "${KVERSION}" ] && >&2 echo "Error: \`kversion\` not found." && exit 1
PRODUCT="$(${KVERSION} 2> /dev/null | awk '{print $1}')"
_calc_max_slot_num() {
local conf=""
for conf in $(ls ${PRODUCT_DIR}); do
if [ "${conf: -5}" = ".conf" ]; then
. "${PRODUCT_DIR}/$conf"
fi
done
eval "_product_${PRODUCT}_profile" 2>/dev/null
if [ "$?" != "0" ]; then
>&2 echo "Error: \`kversion\` or profile not found."
_exit 1
fi
MAX_SLOT_NUM=${#MODULE_PATH[@]}
}
_init_signal_from_slots() {
local i=""
for (( i=0; i<${MAX_SLOT_NUM}; i++ )); do
# set to 999 for slot unused
SIGNAL_FROM_SLOTS[i]=999
# turn off all slots leds
_signal_level_to_led "$((i+1))"
done
USED_SLOT_NUM=0
}
_calc_used_slot_num() {
local count=0
local i=""
for (( i=0; i<${MAX_SLOT_NUM}; i++ )); do
if [ "${SIGNAL_FROM_SLOTS[i]}" != "0" ] && [ "${SIGNAL_FROM_SLOTS[i]}" != "999" ]; then
count=$((count+1))
fi
done
USED_SLOT_NUM=${count}
}
_signal_level_to_led() {
local slot=$1
local signal=${2:-"0"}
local o_ifs=""
local i=""
local signal_func_name=""
# Compare the signal with the threshold level and then invoke
# the product specific led representaion function.
# Do nothing if no function defined.
case ${signal} in
1)
signal_func_name="SIGNAL_LED_POOR"
;;
2|3)
signal_func_name="SIGNAL_LED_FAIR"
;;
4|5)
signal_func_name="SIGNAL_LED_EXCELLENT"
;;
*)
signal_func_name="SIGNAL_LED_FLUSH"
;;
esac
[ -z "${!signal_func_name}" ] && return 0
signal_led_funcs="$(echo "${!signal_func_name}" | \
sed -e "s/\$SLOT/$slot/g")"
o_ifs="${IFS}"
IFS=$'\n'
for i in $signal_led_funcs; do
eval "$i"
done
IFS="${o_ifs}"
}
_loop() {
local modules=""
local slot=""
local signal=""
while [ 1 ]; do
modules=$(${CELL_MGMT} modules) || modules=0
# when module uninstall from slot, turn off all leds,
# then turn on still installed modules' leds
_calc_used_slot_num
if [ ${modules} -lt ${USED_SLOT_NUM} ]; then
_init_signal_from_slots
fi
for i in $(seq 0 $((modules-1))); do
slot=$(${CELL_MGMT} -i $i slot)
# Format:
# <RAT> Level: <level> (description)
signal=$(${CELL_MGMT} -i $i signal | cut -d ' ' -f 3)
_signal_level_to_led "${slot}" "${signal}"
# record signal in $SIGNAL_FROM_SLOTS
SIGNAL_FROM_SLOTS[$((slot-1))]=${signal}
done
sleep ${INTERVAL}
done
}
usage() {
echo "Usage:"
echo " $1 [OPTION]"
echo ""
echo "Options:"
echo " -i <polling interval>"
echo " Polling interval in seconds to update the signal LED"
echo " -h"
echo " Show help"
echo " -v"
echo " Print version"
}
while getopts "i:vh" flag; do
if [ "${flag}" = "i" ]; then
re='^[0-9]+$'
if ! [[ $OPTARG =~ $re ]] ; then
echo "error: Interval should be a number" >&2
usage $0
exit 1
fi
INTERVAL=$OPTARG
elif [ "${flag}" = "h" ]; then
usage $0
exit 0
elif [ "${flag}" = "v" ]; then
${CELL_MGMT} version
exit 0
fi
done
_calc_max_slot_num
_init_signal_from_slots
_loop