base-files: make ucidef_set_led_rssi offset and factor optional
[openwrt/openwrt.git] / package / base-files / files / lib / functions / uci-defaults.sh
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
30 _ucidef_set_interface() {
31 local name="$1"
32 local iface="$2"
33 local proto="$3"
34
35 json_select_object "$name"
36 json_add_string ifname "$iface"
37
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
49
50 json_add_string protocol "$proto"
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() {
69 json_select_object network
70 _ucidef_set_interface lan "$@"
71 json_select ..
72 }
73
74 ucidef_set_interface_wan() {
75 json_select_object network
76 _ucidef_set_interface wan "$@"
77 json_select ..
78 }
79
80 ucidef_set_interfaces_lan_wan() {
81 local lan_if="$1"
82 local wan_if="$2"
83
84 json_select_object network
85 _ucidef_set_interface lan "$lan_if"
86 _ucidef_set_interface wan "$wan_if"
87 json_select ..
88 }
89
90 ucidef_set_interface_raw() {
91 json_select_object network
92 _ucidef_set_interface "$@"
93 json_select ..
94 }
95
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 ..
188
189 _ucidef_set_interface "$role" "$devices"
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
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 ..
315 }
316
317 ucidef_add_atm_bridge() {
318 local vpi="$1"
319 local vci="$2"
320 local encaps="$3"
321 local payload="$4"
322
323 json_select_object dsl
324 json_select_object atmbridge
325 json_add_int vpi "$vpi"
326 json_add_int vci "$vci"
327 json_add_string encaps "$encaps"
328 json_add_string payload "$payload"
329 json_select ..
330 json_select ..
331 }
332
333 ucidef_add_adsl_modem() {
334 local annex="$1"
335 local firmware="$2"
336
337 json_select_object dsl
338 json_select_object modem
339 json_add_string type "adsl"
340 json_add_string annex "$annex"
341 json_add_string firmware "$firmware"
342 json_select ..
343 json_select ..
344 }
345
346 ucidef_add_vdsl_modem() {
347 local annex="$1"
348 local tone="$2"
349 local xfer_mode="$3"
350
351 json_select_object dsl
352 json_select_object modem
353 json_add_string type "vdsl"
354 json_add_string annex "$annex"
355 json_add_string tone "$tone"
356 json_add_string xfer_mode "$xfer_mode"
357 json_select ..
358 json_select ..
359 }
360
361 ucidef_set_led_ataport() {
362 _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
363 }
364
365 _ucidef_set_led_common() {
366 local cfg="led_$1"
367 local name="$2"
368 local sysfs="$3"
369
370 json_select_object led
371
372 json_select_object "$1"
373 json_add_string name "$name"
374 json_add_string sysfs "$sysfs"
375 }
376
377 ucidef_set_led_default() {
378 local default="$4"
379
380 _ucidef_set_led_common "$1" "$2" "$3"
381
382 json_add_string default "$default"
383 json_select ..
384
385 json_select ..
386 }
387
388 ucidef_set_led_gpio() {
389 local gpio="$4"
390 local inverted="$5"
391
392 _ucidef_set_led_common "$1" "$2" "$3"
393
394 json_add_string trigger "$trigger"
395 json_add_string type gpio
396 json_add_int gpio "$gpio"
397 json_add_boolean inverted "$inverted"
398 json_select ..
399
400 json_select ..
401 }
402
403 ucidef_set_led_ide() {
404 _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
405 }
406
407 ucidef_set_led_netdev() {
408 local dev="$4"
409 local mode="${5:-link tx rx}"
410
411 _ucidef_set_led_common "$1" "$2" "$3"
412
413 json_add_string type netdev
414 json_add_string device "$dev"
415 json_add_string mode "$mode"
416 json_select ..
417
418 json_select ..
419 }
420
421 ucidef_set_led_oneshot() {
422 _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
423 }
424
425 ucidef_set_led_portstate() {
426 local port_state="$4"
427
428 _ucidef_set_led_common "$1" "$2" "$3"
429
430 json_add_string trigger port_state
431 json_add_string type portstate
432 json_add_string port_state "$port_state"
433 json_select ..
434
435 json_select ..
436 }
437
438 ucidef_set_led_rssi() {
439 local iface="$4"
440 local minq="$5"
441 local maxq="$6"
442 local offset="${7:-0}"
443 local factor="${8:-1}"
444
445 _ucidef_set_led_common "$1" "$2" "$3"
446
447 json_add_string type rssi
448 json_add_string name "$name"
449 json_add_string iface "$iface"
450 json_add_string minq "$minq"
451 json_add_string maxq "$maxq"
452 json_add_string offset "$offset"
453 json_add_string factor "$factor"
454 json_select ..
455
456 json_select ..
457 }
458
459 ucidef_set_led_switch() {
460 local trigger_name="$4"
461 local port_mask="$5"
462 local speed_mask="$6"
463
464 _ucidef_set_led_common "$1" "$2" "$3"
465
466 json_add_string trigger "$trigger_name"
467 json_add_string type switch
468 json_add_string port_mask "$port_mask"
469 json_add_string speed_mask "$speed_mask"
470 json_select ..
471
472 json_select ..
473 }
474
475 _ucidef_set_led_timer() {
476 local trigger_name="$4"
477 local delayon="$5"
478 local delayoff="$6"
479
480 _ucidef_set_led_common "$1" "$2" "$3"
481
482 json_add_string trigger "$trigger_name"
483 json_add_int delayon "$delayon"
484 json_add_int delayoff "$delayoff"
485 json_select ..
486
487 json_select ..
488 }
489
490 ucidef_set_led_timer() {
491 _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
492 }
493
494 _ucidef_set_led_trigger() {
495 local trigger_name="$4"
496
497 _ucidef_set_led_common "$1" "$2" "$3"
498
499 json_add_string trigger "$trigger_name"
500 json_select ..
501
502 json_select ..
503 }
504
505 ucidef_set_led_usbdev() {
506 local dev="$4"
507
508 _ucidef_set_led_common "$1" "$2" "$3"
509
510 json_add_string type usb
511 json_add_string device "$dev"
512 json_select ..
513
514 json_select ..
515 }
516
517 ucidef_set_led_usbhost() {
518 _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
519 }
520
521 ucidef_set_led_usbport() {
522 local obj="$1"
523 local name="$2"
524 local sysfs="$3"
525 shift
526 shift
527 shift
528
529 _ucidef_set_led_common "$obj" "$name" "$sysfs"
530
531 json_add_string type usbport
532 json_select_array ports
533 for port in "$@"; do
534 json_add_string port "$port"
535 done
536 json_select ..
537 json_select ..
538
539 json_select ..
540 }
541
542 ucidef_set_led_wlan() {
543 _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
544 }
545
546 ucidef_set_rssimon() {
547 local dev="$1"
548 local refresh="$2"
549 local threshold="$3"
550
551 json_select_object rssimon
552
553 json_select_object "$dev"
554 [ -n "$refresh" ] && json_add_int refresh "$refresh"
555 [ -n "$threshold" ] && json_add_int threshold "$threshold"
556 json_select ..
557
558 json_select ..
559 }
560
561 ucidef_add_gpio_switch() {
562 local cfg="$1"
563 local name="$2"
564 local pin="$3"
565 local default="${4:-0}"
566
567 json_select_object gpioswitch
568 json_select_object "$cfg"
569 json_add_string name "$name"
570 json_add_int pin "$pin"
571 json_add_int default "$default"
572 json_select ..
573 json_select ..
574 }
575
576 ucidef_set_hostname() {
577 local hostname="$1"
578
579 json_select_object system
580 json_add_string hostname "$hostname"
581 json_select ..
582 }
583
584 ucidef_set_ntpserver() {
585 local server
586
587 json_select_object system
588 json_select_array ntpserver
589 for server in "$@"; do
590 json_add_string "" "$server"
591 done
592 json_select ..
593 json_select ..
594 }
595
596 board_config_update() {
597 json_init
598 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
599
600 # auto-initialize model id and name if applicable
601 if ! json_is_a model object; then
602 json_select_object model
603 [ -f "/tmp/sysinfo/board_name" ] && \
604 json_add_string id "$(cat /tmp/sysinfo/board_name)"
605 [ -f "/tmp/sysinfo/model" ] && \
606 json_add_string name "$(cat /tmp/sysinfo/model)"
607 json_select ..
608 fi
609 }
610
611 board_config_flush() {
612 json_dump -i > /tmp/.board.json
613 mv /tmp/.board.json ${CFG}
614 }