[ep93xx] add 3.2 support
[openwrt.git] / package / ltq-dsl-app / files / dsl_control
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 OpenWrt.org
3 START=99
4
5 EXTRA_COMMANDS="status lucistat"
6 EXTRA_HELP=" status Get DSL status information
7 lucistat Get status information if lua friendly format"
8
9 SERVICE_DAEMONIZE=1
10 SERVICE_WRITE_PID=1
11
12 #
13 # Basic functions to send CLI commands to the dsl_cpe_control daemon
14 #
15 dsl_cmd() {
16 pkill -0 dsl_cpe_control && (
17 echo "$@" > /tmp/pipe/dsl_cpe0_cmd
18 cat /tmp/pipe/dsl_cpe0_ack
19 )
20 }
21 dsl_val() {
22 echo $(expr "$1" : '.*'$2'=\([-\.[:alnum:]]*\).*')
23 }
24
25 #
26 # Simple divide by 10 routine to cope with one decimal place
27 #
28 dbt() {
29 local a=$(expr $1 / 10)
30 local b=$(expr $1 % 10)
31 echo "${a}.${b}"
32 }
33 #
34 # Take a number and convert to k or meg
35 #
36 scale() {
37 local val=$1
38 local a
39 local b
40
41 if [ "$val" -gt 1000000 ]; then
42 a=$(expr $val / 1000)
43 b=$(expr $a % 1000)
44 a=$(expr $a / 1000)
45 printf "%d.%03d Mb" ${a} ${b}
46 elif [ "$val" -gt 1000 ]; then
47 a=$(expr $val / 1000)
48 printf "%d Kb" ${a}
49 else
50 echo "${val} b"
51 fi
52 }
53
54 #
55 # Read the data rates for both directions
56 #
57 data_rates() {
58 local csg
59 local dru
60 local drd
61 local sdru
62 local sdrd
63
64 csg=$(dsl_cmd g997csg 0 1)
65 drd=$(dsl_val "$csg" ActualDataRate)
66
67 csg=$(dsl_cmd g997csg 0 0)
68 dru=$(dsl_val "$csg" ActualDataRate)
69
70 [ -z "$drd" ] && drd=0
71 [ -z "$dru" ] && dru=0
72
73 sdrd=$(scale $drd)
74 sdru=$(scale $dru)
75
76 if [ "$action" = "lucistat" ]; then
77 echo "dsl.data_rate_down=$drd"
78 echo "dsl.data_rate_up=$dru"
79 echo "dsl.data_rate_down_s=\"$sdrd\""
80 echo "dsl.data_rate_up_s=\"$sdru\""
81 else
82 echo "Data Rate: ${sdrd}/s / ${sdru}/s"
83 fi
84 }
85
86 #
87 # Chipset
88 #
89 chipset() {
90 local vig
91 local cs
92 local csv
93
94 vig=$(dsl_cmd vig)
95 cs=$(dsl_val "$vig" DSL_ChipSetType)
96 csv=$(dsl_val "$vig" DSL_ChipSetHWVersion)
97
98 if [ "$action" = "lucistat" ]; then
99 echo "dsl.chipset=\"${cs} ${csv}\""
100 else
101 echo "Chipset: ${cs} ${csv}"
102 fi
103 }
104
105 #
106 # Work out how long the line has been up
107 #
108 line_uptime() {
109 local ccsg
110 local et
111 local etr
112 local d
113 local h
114 local m
115 local s
116 local rc=""
117
118 ccsg=$(dsl_cmd pmccsg 0 0 0)
119 et=$(dsl_val "$ccsg" nElapsedTime)
120
121 [ -z "$et" ] && et=0
122
123 if [ "$action" = "lucistat" ]; then
124 echo "dsl.line_uptime=${et}"
125 return
126 fi
127
128 d=$(expr $et / 86400)
129 etr=$(expr $et % 86400)
130 h=$(expr $etr / 3600)
131 etr=$(expr $etr % 3600)
132 m=$(expr $etr / 60)
133 s=$(expr $etr % 60)
134
135
136 [ "${d}${h}${m}${s}" -ne 0 ] && rc="${s}s"
137 [ "${d}${h}${m}" -ne 0 ] && rc="${m}m ${rc}"
138 [ "${d}${h}" -ne 0 ] && rc="${h}h ${rc}"
139 [ "${d}" -ne 0 ] && rc="${d}d ${rc}"
140
141 [ -z "$rc" ] && rc="down"
142 echo "Line Uptime: ${rc}"
143 }
144
145 #
146 # Get noise and attenuation figures
147 #
148 line_data() {
149 local lsg
150 local latnu
151 local latnd
152 local snru
153 local snrd
154
155 lsg=$(dsl_cmd g997lsg 1 1)
156 latnd=$(dsl_val "$lsg" LATN)
157 snrd=$(dsl_val "$lsg" SNR)
158
159 lsg=$(dsl_cmd g997lsg 0 1)
160 latnu=$(dsl_val "$lsg" LATN)
161 snru=$(dsl_val "$lsg" SNR)
162
163 [ -z "$latnd" ] && latnd=0
164 [ -z "$latnu" ] && latnu=0
165 [ -z "$snrd" ] && snrd=0
166 [ -z "$snru" ] && snru=0
167
168 latnd=$(dbt $latnd)
169 latnu=$(dbt $latnu)
170 snrd=$(dbt $snrd)
171 snru=$(dbt $snru)
172
173 if [ "$action" = "lucistat" ]; then
174 echo "dsl.line_attenuation_down=$latnd"
175 echo "dsl.line_attenuation_up=$latnu"
176 echo "dsl.noise_margin_down=$snrd"
177 echo "dsl.noise_margin_up=$snru"
178 else
179 echo "Line Attenuation: ${latnd}dB / ${latnu}dB"
180 echo "Noise Margin: ${snrd}dB / ${snru}dB"
181 fi
182 }
183
184 #
185 # Is the line up? Or what state is it in?
186 #
187 line_state() {
188 local lsg=$(dsl_cmd lsg)
189 local ls=$(dsl_val "$lsg" nLineState);
190 local s;
191
192 case "$ls" in
193 "0x0") s="not initialized" ;;
194 "0x1") s="exception" ;;
195 "0x10") s="not updated" ;;
196 "0xff") s="idle request" ;;
197 "0x100") s="idle" ;;
198 "0x1ff") s="silent request" ;;
199 "0x200") s="silent" ;;
200 "0x300") s="handshake" ;;
201 "0x380") s="full_init" ;;
202 "0x400") s="discovery" ;;
203 "0x500") s="training" ;;
204 "0x600") s="analysis" ;;
205 "0x700") s="exchange" ;;
206 "0x800") s="showtime_no_sync" ;;
207 "0x801") s="showtime_tc_sync" ;;
208 "0x900") s="fastretrain" ;;
209 "0xa00") s="lowpower_l2" ;;
210 "0xb00") s="loopdiagnostic active" ;;
211 "0xb10") s="loopdiagnostic data exchange" ;;
212 "0xb20") s="loopdiagnostic data request" ;;
213 "0xc00") s="loopdiagnostic complete" ;;
214 "0x1000000") s="test" ;;
215 "0xd00") s="resync" ;;
216 "0x3c0") s="short init entry" ;;
217 "") s="not running daemon"; ls="0xfff" ;;
218 *) s="unknown" ;;
219 esac
220
221 if [ $action = "lucistat" ]; then
222 echo "dsl.line_state_num=$ls"
223 echo "dsl.line_state_detail=\"$s\""
224 if [ "$ls" = "0x801" ]; then
225 echo "dsl.line_state=\"UP\""
226 else
227 echo "dsl.line_state=\"DOWN\""
228 fi
229 else
230 if [ "$ls" = "0x801" ]; then
231 echo "Line State: UP [$ls: $s]"
232 else
233 echo "Line State: DOWN [$ls: $s]"
234 fi
235 fi
236 }
237
238 #
239 # Main status routine
240 #
241 status() {
242 chipset
243 line_state
244 data_rates
245 line_data
246 line_uptime
247 }
248
249 #
250 # Luci (lua) compatible version that's easy to parse
251 #
252 lucistat() {
253 echo "local dsl={}"
254 status
255 echo "return dsl"
256 }
257
258 #
259 # Simple start routine
260 #
261 start() {
262 # start CPE dsl daemon in the background
263 service_start /sbin/dsl_cpe_control -i \
264 -n /sbin/dsl_notify.sh \
265 -f /lib/firmware/ModemHWE.bin
266 }
267
268 #
269 # For stop we want to simulate the notification call for when
270 # the line goes down, so that we can stop the ppp link before
271 # we die.
272 #
273 stop() {
274 DSL_NOTIFICATION_TYPE="DSL_INTERFACE_STATUS" \
275 DSL_INTERFACE_STATUS="DOWN" \
276 /sbin/dsl_notify.sh
277
278 service_stop /sbin/dsl_cpe_control
279 }
280
This page took 0.064622 seconds and 5 git commands to generate.