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