OpenWrt – Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/bin/ash
2  
3 . /lib/functions.sh
4 . /usr/share/libubox/jshn.sh
5  
6 json_select_array() {
7 local _json_no_warning=1
8  
9 json_select "$1"
10 [ $? = 0 ] && return
11  
12 json_add_array "$1"
13 json_close_array
14  
15 json_select "$1"
16 }
17  
18 json_select_object() {
19 local _json_no_warning=1
20  
21 json_select "$1"
22 [ $? = 0 ] && return
23  
24 json_add_object "$1"
25 json_close_object
26  
27 json_select "$1"
28 }
29  
3 office 30 _ucidef_set_interface() {
31 local name="$1"
32 local iface="$2"
33 local proto="$3"
1 office 34  
3 office 35 json_select_object "$name"
36 json_add_string ifname "$iface"
1 office 37  
3 office 38 if ! json_is_a protocol string || [ -n "$proto" ]; then
39 case "$proto" in
40 static|dhcp|none|pppoe) : ;;
41 *)
42 case "$name" in
43 lan) proto="static" ;;
44 wan) proto="dhcp" ;;
45 *) proto="none" ;;
46 esac
47 ;;
48 esac
1 office 49  
3 office 50 json_add_string protocol "$proto"
1 office 51 fi
52  
53 json_select ..
54 }
55  
56 ucidef_set_board_id() {
57 json_select_object model
58 json_add_string id "$1"
59 json_select ..
60 }
61  
62 ucidef_set_model_name() {
63 json_select_object model
64 json_add_string name "$1"
65 json_select ..
66 }
67  
68 ucidef_set_interface_lan() {
3 office 69 json_select_object network
70 _ucidef_set_interface lan "$@"
71 json_select ..
1 office 72 }
73  
74 ucidef_set_interface_wan() {
3 office 75 json_select_object network
76 _ucidef_set_interface wan "$@"
77 json_select ..
1 office 78 }
79  
80 ucidef_set_interfaces_lan_wan() {
81 local lan_if="$1"
82 local wan_if="$2"
83  
3 office 84 json_select_object network
85 _ucidef_set_interface lan "$lan_if"
86 _ucidef_set_interface wan "$wan_if"
87 json_select ..
1 office 88 }
89  
3 office 90 ucidef_set_interface_raw() {
91 json_select_object network
92 _ucidef_set_interface "$@"
93 json_select ..
94 }
95  
1 office 96 _ucidef_add_switch_port() {
97 # inherited: $num $device $need_tag $want_untag $role $index $prev_role
98 # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
99  
100 n_ports=$((n_ports + 1))
101  
102 json_select_array ports
103 json_add_object
104 json_add_int num "$num"
105 [ -n "$device" ] && json_add_string device "$device"
106 [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
107 [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag"
108 [ -n "$role" ] && json_add_string role "$role"
109 [ -n "$index" ] && json_add_int index "$index"
110 json_close_object
111 json_select ..
112  
113 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
114 [ -n "$device" ] && {
115 export "cpu$n_cpu=$n_ports"
116 n_cpu=$((n_cpu + 1))
117 }
118  
119 # create/append object to role list
120 [ -n "$role" ] && {
121 json_select_array roles
122  
123 if [ "$role" != "$prev_role" ]; then
124 json_add_object
125 json_add_string role "$role"
126 json_add_string ports "$num"
127 json_close_object
128  
129 prev_role="$role"
130 n_vlan=$((n_vlan + 1))
131 else
132 json_select_object "$n_vlan"
133 json_get_var port ports
134 json_add_string ports "$port $num"
135 json_select ..
136 fi
137  
138 json_select ..
139 }
140 }
141  
142 _ucidef_finish_switch_roles() {
143 # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
144 local index role roles num device need_tag want_untag port ports
145  
146 json_select switch
147 json_select "$name"
148 json_get_keys roles roles
149 json_select ..
150 json_select ..
151  
152 for index in $roles; do
153 eval "port=\$cpu$(((index - 1) % n_cpu))"
154  
155 json_select switch
156 json_select "$name"
157 json_select ports
158 json_select "$port"
159 json_get_vars num device need_tag want_untag
160 json_select ..
161 json_select ..
162  
163 if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
164 num="${num}t"
165 device="${device}.${index}"
166 fi
167  
168 json_select roles
169 json_select "$index"
170 json_get_vars role ports
171 json_add_string ports "$ports $num"
172 json_add_string device "$device"
173 json_select ..
174 json_select ..
175 json_select ..
176 json_select ..
177  
178 json_select_object network
179 local devices
180  
181 json_select_object "$role"
182 # attach previous interfaces (for multi-switch devices)
183 json_get_var devices ifname
184 if ! list_contains devices "$device"; then
185 devices="${devices:+$devices }$device"
186 fi
187 json_select ..
3 office 188  
189 _ucidef_set_interface "$role" "$devices"
1 office 190 json_select ..
191 done
192 }
193  
194 ucidef_add_switch() {
195 local name="$1"; shift
196 local port num role device index need_tag prev_role
197 local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
198 local n_cpu=0 n_vlan=0 n_ports=0
199  
200 json_select_object switch
201 json_select_object "$name"
202 json_add_boolean enable 1
203 json_add_boolean reset 1
204  
205 for port in "$@"; do
206 case "$port" in
207 [0-9]*@*)
208 num="${port%%@*}"
209 device="${port##*@}"
210 need_tag=0
211 want_untag=0
212 [ "${num%t}" != "$num" ] && {
213 num="${num%t}"
214 need_tag=1
215 }
216 [ "${num%u}" != "$num" ] && {
217 num="${num%u}"
218 want_untag=1
219 }
220 ;;
221 [0-9]*:*:[0-9]*)
222 num="${port%%:*}"
223 index="${port##*:}"
224 role="${port#[0-9]*:}"; role="${role%:*}"
225 ;;
226 [0-9]*:*)
227 num="${port%%:*}"
228 role="${port##*:}"
229 ;;
230 esac
231  
232 if [ -n "$num" ] && [ -n "$device$role" ]; then
233 _ucidef_add_switch_port
234 fi
235  
236 unset num device role index need_tag want_untag
237 done
238 json_select ..
239 json_select ..
240  
241 _ucidef_finish_switch_roles
242 }
243  
244 ucidef_add_switch_attr() {
245 local name="$1"
246 local key="$2"
247 local val="$3"
248  
249 json_select_object switch
250 json_select_object "$name"
251  
252 case "$val" in
253 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
254 [0-9]) json_add_int "$key" "$val" ;;
255 *) json_add_string "$key" "$val" ;;
256 esac
257  
258 json_select ..
259 json_select ..
260 }
261  
262 ucidef_add_switch_port_attr() {
263 local name="$1"
264 local port="$2"
265 local key="$3"
266 local val="$4"
267 local ports i num
268  
269 json_select_object switch
270 json_select_object "$name"
271  
272 json_get_keys ports ports
273 json_select_array ports
274  
275 for i in $ports; do
276 json_select "$i"
277 json_get_var num num
278  
279 if [ -n "$num" ] && [ $num -eq $port ]; then
280 json_select_object attr
281  
282 case "$val" in
283 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
284 [0-9]) json_add_int "$key" "$val" ;;
285 *) json_add_string "$key" "$val" ;;
286 esac
287  
288 json_select ..
289 fi
290  
291 json_select ..
292 done
293  
294 json_select ..
295 json_select ..
296 json_select ..
297 }
298  
299 ucidef_set_interface_macaddr() {
300 local network="$1"
301 local macaddr="$2"
302  
3 office 303 json_select_object network
304  
305 json_select "$network"
306 [ $? -eq 0 ] || {
307 json_select ..
308 return
309 }
310  
311 json_add_string macaddr "$macaddr"
312 json_select ..
313  
314 json_select ..
1 office 315 }
316  
317 ucidef_add_atm_bridge() {
318 local vpi="$1"
319 local vci="$2"
320 local encaps="$3"
321 local payload="$4"
322 local nameprefix="$5"
323  
324 json_select_object dsl
325 json_select_object atmbridge
326 json_add_int vpi "$vpi"
327 json_add_int vci "$vci"
328 json_add_string encaps "$encaps"
329 json_add_string payload "$payload"
330 json_add_string nameprefix "$nameprefix"
331 json_select ..
332 json_select ..
333 }
334  
335 ucidef_add_adsl_modem() {
336 local annex="$1"
337 local firmware="$2"
338  
339 json_select_object dsl
340 json_select_object modem
341 json_add_string type "adsl"
342 json_add_string annex "$annex"
343 json_add_string firmware "$firmware"
344 json_select ..
345 json_select ..
346 }
347  
348 ucidef_add_vdsl_modem() {
349 local annex="$1"
350 local tone="$2"
351 local xfer_mode="$3"
352  
353 json_select_object dsl
354 json_select_object modem
355 json_add_string type "vdsl"
356 json_add_string annex "$annex"
357 json_add_string tone "$tone"
358 json_add_string xfer_mode "$xfer_mode"
359 json_select ..
360 json_select ..
361 }
362  
363 ucidef_set_led_ataport() {
364 _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
365 }
366  
367 _ucidef_set_led_common() {
368 local cfg="led_$1"
369 local name="$2"
370 local sysfs="$3"
371  
372 json_select_object led
373  
374 json_select_object "$1"
375 json_add_string name "$name"
376 json_add_string sysfs "$sysfs"
377 }
378  
379 ucidef_set_led_default() {
380 local default="$4"
381  
382 _ucidef_set_led_common "$1" "$2" "$3"
383  
384 json_add_string default "$default"
385 json_select ..
386  
387 json_select ..
388 }
389  
390 ucidef_set_led_gpio() {
391 local gpio="$4"
392 local inverted="$5"
393  
394 _ucidef_set_led_common "$1" "$2" "$3"
395  
396 json_add_string trigger "$trigger"
397 json_add_string type gpio
398 json_add_int gpio "$gpio"
399 json_add_boolean inverted "$inverted"
400 json_select ..
401  
402 json_select ..
403 }
404  
405 ucidef_set_led_ide() {
406 _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
407 }
408  
409 ucidef_set_led_netdev() {
410 local dev="$4"
411 local mode="${5:-link tx rx}"
412  
413 _ucidef_set_led_common "$1" "$2" "$3"
414  
415 json_add_string type netdev
416 json_add_string device "$dev"
417 json_add_string mode "$mode"
418 json_select ..
419  
420 json_select ..
421 }
422  
423 ucidef_set_led_oneshot() {
424 _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
425 }
426  
427 ucidef_set_led_portstate() {
428 local port_state="$4"
429  
430 _ucidef_set_led_common "$1" "$2" "$3"
431  
432 json_add_string trigger port_state
433 json_add_string type portstate
434 json_add_string port_state "$port_state"
435 json_select ..
436  
437 json_select ..
438 }
439  
440 ucidef_set_led_rssi() {
441 local iface="$4"
442 local minq="$5"
443 local maxq="$6"
444 local offset="${7:-0}"
445 local factor="${8:-1}"
446  
447 _ucidef_set_led_common "$1" "$2" "$3"
448  
449 json_add_string type rssi
450 json_add_string name "$name"
451 json_add_string iface "$iface"
452 json_add_string minq "$minq"
453 json_add_string maxq "$maxq"
454 json_add_string offset "$offset"
455 json_add_string factor "$factor"
456 json_select ..
457  
458 json_select ..
459 }
460  
461 ucidef_set_led_switch() {
462 local trigger_name="$4"
463 local port_mask="$5"
464 local speed_mask="$6"
465  
466 _ucidef_set_led_common "$1" "$2" "$3"
467  
468 json_add_string trigger "$trigger_name"
469 json_add_string type switch
470 json_add_string port_mask "$port_mask"
471 json_add_string speed_mask "$speed_mask"
472 json_select ..
473  
474 json_select ..
475 }
476  
477 _ucidef_set_led_timer() {
478 local trigger_name="$4"
479 local delayon="$5"
480 local delayoff="$6"
481  
482 _ucidef_set_led_common "$1" "$2" "$3"
483  
484 json_add_string trigger "$trigger_name"
485 json_add_int delayon "$delayon"
486 json_add_int delayoff "$delayoff"
487 json_select ..
488  
489 json_select ..
490 }
491  
492 ucidef_set_led_timer() {
493 _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
494 }
495  
496 _ucidef_set_led_trigger() {
497 local trigger_name="$4"
498  
499 _ucidef_set_led_common "$1" "$2" "$3"
500  
501 json_add_string trigger "$trigger_name"
502 json_select ..
503  
504 json_select ..
505 }
506  
507 ucidef_set_led_usbdev() {
508 local dev="$4"
509  
510 _ucidef_set_led_common "$1" "$2" "$3"
511  
512 json_add_string type usb
513 json_add_string device "$dev"
514 json_select ..
515  
516 json_select ..
517 }
518  
519 ucidef_set_led_usbhost() {
520 _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
521 }
522  
523 ucidef_set_led_usbport() {
524 local obj="$1"
525 local name="$2"
526 local sysfs="$3"
527 shift
528 shift
529 shift
530  
531 _ucidef_set_led_common "$obj" "$name" "$sysfs"
532  
533 json_add_string type usbport
534 json_select_array ports
535 for port in "$@"; do
536 json_add_string port "$port"
537 done
538 json_select ..
539 json_select ..
540  
541 json_select ..
542 }
543  
544 ucidef_set_led_wlan() {
545 _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
546 }
547  
548 ucidef_set_rssimon() {
549 local dev="$1"
550 local refresh="$2"
551 local threshold="$3"
552  
553 json_select_object rssimon
554  
555 json_select_object "$dev"
556 [ -n "$refresh" ] && json_add_int refresh "$refresh"
557 [ -n "$threshold" ] && json_add_int threshold "$threshold"
558 json_select ..
559  
560 json_select ..
561 }
562  
563 ucidef_add_gpio_switch() {
564 local cfg="$1"
565 local name="$2"
566 local pin="$3"
567 local default="${4:-0}"
568  
569 json_select_object gpioswitch
570 json_select_object "$cfg"
571 json_add_string name "$name"
572 json_add_int pin "$pin"
573 json_add_int default "$default"
574 json_select ..
575 json_select ..
576 }
577  
578 ucidef_set_hostname() {
579 local hostname="$1"
580  
581 json_select_object system
582 json_add_string hostname "$hostname"
583 json_select ..
584 }
585  
586 ucidef_set_ntpserver() {
587 local server
588  
589 json_select_object system
590 json_select_array ntpserver
591 for server in "$@"; do
592 json_add_string "" "$server"
593 done
594 json_select ..
595 json_select ..
596 }
597  
598 board_config_update() {
599 json_init
600 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
601  
602 # auto-initialize model id and name if applicable
603 if ! json_is_a model object; then
604 json_select_object model
605 [ -f "/tmp/sysinfo/board_name" ] && \
606 json_add_string id "$(cat /tmp/sysinfo/board_name)"
607 [ -f "/tmp/sysinfo/model" ] && \
608 json_add_string name "$(cat /tmp/sysinfo/model)"
609 json_select ..
610 fi
611 }
612  
613 board_config_flush() {
614 json_dump -i > /tmp/.board.json
615 mv /tmp/.board.json ${CFG}
616 }