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