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