b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | . /lib/functions.sh |
| 2 | . /usr/share/libubox/jshn.sh |
| 3 | |
| 4 | json_select_array() { |
| 5 | local _json_no_warning=1 |
| 6 | |
| 7 | json_select "$1" |
| 8 | [ $? = 0 ] && return |
| 9 | |
| 10 | json_add_array "$1" |
| 11 | json_close_array |
| 12 | |
| 13 | json_select "$1" |
| 14 | } |
| 15 | |
| 16 | json_select_object() { |
| 17 | local _json_no_warning=1 |
| 18 | |
| 19 | json_select "$1" |
| 20 | [ $? = 0 ] && return |
| 21 | |
| 22 | json_add_object "$1" |
| 23 | json_close_object |
| 24 | |
| 25 | json_select "$1" |
| 26 | } |
| 27 | |
| 28 | ucidef_set_interface() { |
| 29 | local network=$1; shift |
| 30 | |
| 31 | [ -z "$network" ] && return |
| 32 | |
| 33 | json_select_object network |
| 34 | json_select_object "$network" |
| 35 | |
| 36 | while [ -n "$1" ]; do |
| 37 | local opt=$1; shift |
| 38 | local val=$1; shift |
| 39 | |
| 40 | [ -n "$opt" -a -n "$val" ] || break |
| 41 | |
| 42 | [ "$opt" = "device" -a "$val" != "${val/ //}" ] && { |
| 43 | json_select_array "ports" |
| 44 | for e in $val; do json_add_string "" "$e"; done |
| 45 | json_close_array |
| 46 | } || { |
| 47 | json_add_string "$opt" "$val" |
| 48 | } |
| 49 | done |
| 50 | |
| 51 | if ! json_is_a protocol string; then |
| 52 | case "$network" in |
| 53 | lan) json_add_string protocol static ;; |
| 54 | wan) json_add_string protocol dhcp ;; |
| 55 | *) json_add_string protocol none ;; |
| 56 | esac |
| 57 | fi |
| 58 | |
| 59 | json_select .. |
| 60 | json_select .. |
| 61 | } |
| 62 | |
| 63 | ucidef_set_board_id() { |
| 64 | json_select_object model |
| 65 | json_add_string id "$1" |
| 66 | json_select .. |
| 67 | } |
| 68 | |
| 69 | ucidef_set_model_name() { |
| 70 | json_select_object model |
| 71 | json_add_string name "$1" |
| 72 | json_select .. |
| 73 | } |
| 74 | |
| 75 | ucidef_set_compat_version() { |
| 76 | json_select_object system |
| 77 | json_add_string compat_version "${1:-1.0}" |
| 78 | json_select .. |
| 79 | } |
| 80 | |
| 81 | ucidef_set_interface_lan() { |
| 82 | ucidef_set_interface "lan" device "$1" protocol "${2:-static}" |
| 83 | } |
| 84 | |
| 85 | ucidef_set_interface_wan() { |
| 86 | ucidef_set_interface "wan" device "$1" protocol "${2:-dhcp}" |
| 87 | } |
| 88 | |
| 89 | ucidef_set_interfaces_lan_wan() { |
| 90 | local lan_if="$1" |
| 91 | local wan_if="$2" |
| 92 | |
| 93 | ucidef_set_interface_lan "$lan_if" |
| 94 | ucidef_set_interface_wan "$wan_if" |
| 95 | } |
| 96 | |
| 97 | ucidef_set_bridge_device() { |
| 98 | json_select_object bridge |
| 99 | json_add_string name "${1:-switch0}" |
| 100 | json_select .. |
| 101 | } |
| 102 | |
| 103 | ucidef_set_bridge_mac() { |
| 104 | json_select_object bridge |
| 105 | json_add_string macaddr "${1}" |
| 106 | json_select .. |
| 107 | } |
| 108 | |
| 109 | _ucidef_set_network_device_common() { |
| 110 | json_select_object "network_device" |
| 111 | json_select_object "${1}" |
| 112 | json_add_string "${2}" "${3}" |
| 113 | json_select .. |
| 114 | json_select .. |
| 115 | } |
| 116 | |
| 117 | ucidef_set_network_device_mac() { |
| 118 | _ucidef_set_network_device_common $1 macaddr $2 |
| 119 | } |
| 120 | |
| 121 | ucidef_set_network_device_path() { |
| 122 | _ucidef_set_network_device_common $1 path $2 |
| 123 | } |
| 124 | |
| 125 | ucidef_set_network_device_path_port() { |
| 126 | _ucidef_set_network_device_common $1 path $2 |
| 127 | _ucidef_set_network_device_common $1 port $3 |
| 128 | } |
| 129 | |
| 130 | ucidef_set_network_device_gro() { |
| 131 | _ucidef_set_network_device_common $1 gro $2 |
| 132 | } |
| 133 | |
| 134 | ucidef_set_network_device_conduit() { |
| 135 | _ucidef_set_network_device_common $1 conduit $2 |
| 136 | } |
| 137 | |
| 138 | _ucidef_add_switch_port() { |
| 139 | # inherited: $num $device $need_tag $want_untag $role $index $prev_role |
| 140 | # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 |
| 141 | |
| 142 | n_ports=$((n_ports + 1)) |
| 143 | |
| 144 | json_select_array ports |
| 145 | json_add_object |
| 146 | json_add_int num "$num" |
| 147 | [ -n "$device" ] && json_add_string device "$device" |
| 148 | [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag" |
| 149 | [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag" |
| 150 | [ -n "$role" ] && json_add_string role "$role" |
| 151 | [ -n "$index" ] && json_add_int index "$index" |
| 152 | json_close_object |
| 153 | json_select .. |
| 154 | |
| 155 | # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles() |
| 156 | [ -n "$device" ] && { |
| 157 | export "cpu$n_cpu=$n_ports" |
| 158 | n_cpu=$((n_cpu + 1)) |
| 159 | } |
| 160 | |
| 161 | # create/append object to role list |
| 162 | [ -n "$role" ] && { |
| 163 | json_select_array roles |
| 164 | |
| 165 | if [ "$role" != "$prev_role" ]; then |
| 166 | json_add_object |
| 167 | json_add_string role "$role" |
| 168 | json_add_string ports "$num" |
| 169 | json_close_object |
| 170 | |
| 171 | prev_role="$role" |
| 172 | n_vlan=$((n_vlan + 1)) |
| 173 | else |
| 174 | json_select_object "$n_vlan" |
| 175 | json_get_var port ports |
| 176 | json_add_string ports "$port $num" |
| 177 | json_select .. |
| 178 | fi |
| 179 | |
| 180 | json_select .. |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | _ucidef_finish_switch_roles() { |
| 185 | # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 |
| 186 | local index role roles num device need_tag want_untag port ports |
| 187 | |
| 188 | json_select switch |
| 189 | json_select "$name" |
| 190 | json_get_keys roles roles |
| 191 | json_select .. |
| 192 | json_select .. |
| 193 | |
| 194 | for index in $roles; do |
| 195 | eval "port=\$cpu$(((index - 1) % n_cpu))" |
| 196 | |
| 197 | json_select switch |
| 198 | json_select "$name" |
| 199 | json_select ports |
| 200 | json_select "$port" |
| 201 | json_get_vars num device need_tag want_untag |
| 202 | json_select .. |
| 203 | json_select .. |
| 204 | |
| 205 | if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then |
| 206 | num="${num}t" |
| 207 | device="${device}.${index}" |
| 208 | fi |
| 209 | |
| 210 | json_select roles |
| 211 | json_select "$index" |
| 212 | json_get_vars role ports |
| 213 | json_add_string ports "$ports $num" |
| 214 | json_add_string device "$device" |
| 215 | json_select .. |
| 216 | json_select .. |
| 217 | json_select .. |
| 218 | json_select .. |
| 219 | |
| 220 | json_select_object network |
| 221 | local devices |
| 222 | |
| 223 | json_select_object "$role" |
| 224 | # attach previous interfaces (for multi-switch devices) |
| 225 | json_get_var devices device |
| 226 | if ! list_contains devices "$device"; then |
| 227 | devices="${devices:+$devices }$device" |
| 228 | fi |
| 229 | json_select .. |
| 230 | json_select .. |
| 231 | |
| 232 | ucidef_set_interface "$role" device "$devices" |
| 233 | done |
| 234 | } |
| 235 | |
| 236 | ucidef_set_ar8xxx_switch_mib() { |
| 237 | local name="$1" |
| 238 | local type="$2" |
| 239 | local interval="$3" |
| 240 | |
| 241 | json_select_object switch |
| 242 | json_select_object "$name" |
| 243 | json_add_int ar8xxx_mib_type $type |
| 244 | json_add_int ar8xxx_mib_poll_interval $interval |
| 245 | json_select .. |
| 246 | json_select .. |
| 247 | } |
| 248 | |
| 249 | ucidef_add_switch() { |
| 250 | local name="$1"; shift |
| 251 | local port num role device index need_tag prev_role |
| 252 | local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5 |
| 253 | local n_cpu=0 n_vlan=0 n_ports=0 |
| 254 | |
| 255 | json_select_object switch |
| 256 | json_select_object "$name" |
| 257 | json_add_boolean enable 1 |
| 258 | json_add_boolean reset 1 |
| 259 | |
| 260 | for port in "$@"; do |
| 261 | case "$port" in |
| 262 | [0-9]*@*) |
| 263 | num="${port%%@*}" |
| 264 | device="${port##*@}" |
| 265 | need_tag=0 |
| 266 | want_untag=0 |
| 267 | [ "${num%t}" != "$num" ] && { |
| 268 | num="${num%t}" |
| 269 | need_tag=1 |
| 270 | } |
| 271 | [ "${num%u}" != "$num" ] && { |
| 272 | num="${num%u}" |
| 273 | want_untag=1 |
| 274 | } |
| 275 | ;; |
| 276 | [0-9]*:*:[0-9]*) |
| 277 | num="${port%%:*}" |
| 278 | index="${port##*:}" |
| 279 | role="${port#[0-9]*:}"; role="${role%:*}" |
| 280 | ;; |
| 281 | [0-9]*:*) |
| 282 | num="${port%%:*}" |
| 283 | role="${port##*:}" |
| 284 | ;; |
| 285 | esac |
| 286 | |
| 287 | if [ -n "$num" ] && [ -n "$device$role" ]; then |
| 288 | _ucidef_add_switch_port |
| 289 | fi |
| 290 | |
| 291 | unset num device role index need_tag want_untag |
| 292 | done |
| 293 | json_select .. |
| 294 | json_select .. |
| 295 | |
| 296 | _ucidef_finish_switch_roles |
| 297 | } |
| 298 | |
| 299 | ucidef_add_switch_attr() { |
| 300 | local name="$1" |
| 301 | local key="$2" |
| 302 | local val="$3" |
| 303 | |
| 304 | json_select_object switch |
| 305 | json_select_object "$name" |
| 306 | |
| 307 | case "$val" in |
| 308 | true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;; |
| 309 | [0-9]) json_add_int "$key" "$val" ;; |
| 310 | *) json_add_string "$key" "$val" ;; |
| 311 | esac |
| 312 | |
| 313 | json_select .. |
| 314 | json_select .. |
| 315 | } |
| 316 | |
| 317 | ucidef_add_switch_port_attr() { |
| 318 | local name="$1" |
| 319 | local port="$2" |
| 320 | local key="$3" |
| 321 | local val="$4" |
| 322 | local ports i num |
| 323 | |
| 324 | json_select_object switch |
| 325 | json_select_object "$name" |
| 326 | |
| 327 | json_get_keys ports ports |
| 328 | json_select_array ports |
| 329 | |
| 330 | for i in $ports; do |
| 331 | json_select "$i" |
| 332 | json_get_var num num |
| 333 | |
| 334 | if [ -n "$num" ] && [ $num -eq $port ]; then |
| 335 | json_select_object attr |
| 336 | |
| 337 | case "$val" in |
| 338 | true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;; |
| 339 | [0-9]) json_add_int "$key" "$val" ;; |
| 340 | *) json_add_string "$key" "$val" ;; |
| 341 | esac |
| 342 | |
| 343 | json_select .. |
| 344 | fi |
| 345 | |
| 346 | json_select .. |
| 347 | done |
| 348 | |
| 349 | json_select .. |
| 350 | json_select .. |
| 351 | json_select .. |
| 352 | } |
| 353 | |
| 354 | ucidef_set_interface_macaddr() { |
| 355 | local network="$1" |
| 356 | local macaddr="$2" |
| 357 | |
| 358 | ucidef_set_interface "$network" macaddr "$macaddr" |
| 359 | } |
| 360 | |
| 361 | ucidef_set_label_macaddr() { |
| 362 | local macaddr="$1" |
| 363 | |
| 364 | json_select_object system |
| 365 | json_add_string label_macaddr "$macaddr" |
| 366 | json_select .. |
| 367 | } |
| 368 | |
| 369 | ucidef_add_atm_bridge() { |
| 370 | local vpi="$1" |
| 371 | local vci="$2" |
| 372 | local encaps="$3" |
| 373 | local payload="$4" |
| 374 | local nameprefix="$5" |
| 375 | |
| 376 | json_select_object dsl |
| 377 | json_select_object atmbridge |
| 378 | json_add_int vpi "$vpi" |
| 379 | json_add_int vci "$vci" |
| 380 | json_add_string encaps "$encaps" |
| 381 | json_add_string payload "$payload" |
| 382 | json_add_string nameprefix "$nameprefix" |
| 383 | json_select .. |
| 384 | json_select .. |
| 385 | } |
| 386 | |
| 387 | ucidef_add_adsl_modem() { |
| 388 | local annex="$1" |
| 389 | local firmware="$2" |
| 390 | |
| 391 | json_select_object dsl |
| 392 | json_select_object modem |
| 393 | json_add_string type "adsl" |
| 394 | json_add_string annex "$annex" |
| 395 | json_add_string firmware "$firmware" |
| 396 | json_select .. |
| 397 | json_select .. |
| 398 | } |
| 399 | |
| 400 | ucidef_add_vdsl_modem() { |
| 401 | local annex="$1" |
| 402 | local tone="$2" |
| 403 | local xfer_mode="$3" |
| 404 | |
| 405 | json_select_object dsl |
| 406 | json_select_object modem |
| 407 | json_add_string type "vdsl" |
| 408 | json_add_string annex "$annex" |
| 409 | json_add_string tone "$tone" |
| 410 | json_add_string xfer_mode "$xfer_mode" |
| 411 | json_select .. |
| 412 | json_select .. |
| 413 | } |
| 414 | |
| 415 | ucidef_set_led_ataport() { |
| 416 | _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4" |
| 417 | } |
| 418 | |
| 419 | _ucidef_set_led_common() { |
| 420 | local cfg="led_$1" |
| 421 | local name="$2" |
| 422 | local sysfs="$3" |
| 423 | |
| 424 | json_select_object led |
| 425 | |
| 426 | json_select_object "$1" |
| 427 | json_add_string name "$name" |
| 428 | json_add_string sysfs "$sysfs" |
| 429 | } |
| 430 | |
| 431 | ucidef_set_led_default() { |
| 432 | local default="$4" |
| 433 | |
| 434 | _ucidef_set_led_common "$1" "$2" "$3" |
| 435 | |
| 436 | json_add_string default "$default" |
| 437 | json_select .. |
| 438 | |
| 439 | json_select .. |
| 440 | } |
| 441 | |
| 442 | ucidef_set_led_heartbeat() { |
| 443 | _ucidef_set_led_common "$1" "$2" "$3" |
| 444 | |
| 445 | json_add_string trigger heartbeat |
| 446 | json_select .. |
| 447 | |
| 448 | json_select .. |
| 449 | } |
| 450 | |
| 451 | ucidef_set_led_gpio() { |
| 452 | local gpio="$4" |
| 453 | local inverted="$5" |
| 454 | |
| 455 | _ucidef_set_led_common "$1" "$2" "$3" |
| 456 | |
| 457 | json_add_string trigger "$trigger" |
| 458 | json_add_string type gpio |
| 459 | json_add_int gpio "$gpio" |
| 460 | json_add_boolean inverted "$inverted" |
| 461 | json_select .. |
| 462 | |
| 463 | json_select .. |
| 464 | } |
| 465 | |
| 466 | ucidef_set_led_ide() { |
| 467 | _ucidef_set_led_trigger "$1" "$2" "$3" disk-activity |
| 468 | } |
| 469 | |
| 470 | ucidef_set_led_netdev() { |
| 471 | local dev="$4" |
| 472 | local mode="${5:-link tx rx}" |
| 473 | |
| 474 | _ucidef_set_led_common "$1" "$2" "$3" |
| 475 | |
| 476 | json_add_string type netdev |
| 477 | json_add_string device "$dev" |
| 478 | json_add_string mode "$mode" |
| 479 | json_select .. |
| 480 | |
| 481 | json_select .. |
| 482 | } |
| 483 | |
| 484 | ucidef_set_led_oneshot() { |
| 485 | _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5 |
| 486 | } |
| 487 | |
| 488 | ucidef_set_led_portstate() { |
| 489 | local port_state="$4" |
| 490 | |
| 491 | _ucidef_set_led_common "$1" "$2" "$3" |
| 492 | |
| 493 | json_add_string trigger port_state |
| 494 | json_add_string type portstate |
| 495 | json_add_string port_state "$port_state" |
| 496 | json_select .. |
| 497 | |
| 498 | json_select .. |
| 499 | } |
| 500 | |
| 501 | ucidef_set_led_rssi() { |
| 502 | local iface="$4" |
| 503 | local minq="$5" |
| 504 | local maxq="$6" |
| 505 | local offset="${7:-0}" |
| 506 | local factor="${8:-1}" |
| 507 | |
| 508 | _ucidef_set_led_common "$1" "$2" "$3" |
| 509 | |
| 510 | json_add_string type rssi |
| 511 | json_add_string name "$name" |
| 512 | json_add_string iface "$iface" |
| 513 | json_add_string minq "$minq" |
| 514 | json_add_string maxq "$maxq" |
| 515 | json_add_string offset "$offset" |
| 516 | json_add_string factor "$factor" |
| 517 | json_select .. |
| 518 | |
| 519 | json_select .. |
| 520 | } |
| 521 | |
| 522 | ucidef_set_led_switch() { |
| 523 | local trigger_name="$4" |
| 524 | local port_mask="$5" |
| 525 | local speed_mask="$6" |
| 526 | local mode="$7" |
| 527 | |
| 528 | _ucidef_set_led_common "$1" "$2" "$3" |
| 529 | |
| 530 | json_add_string trigger "$trigger_name" |
| 531 | json_add_string type switch |
| 532 | json_add_string mode "$mode" |
| 533 | json_add_string port_mask "$port_mask" |
| 534 | json_add_string speed_mask "$speed_mask" |
| 535 | json_select .. |
| 536 | |
| 537 | json_select .. |
| 538 | } |
| 539 | |
| 540 | _ucidef_set_led_timer() { |
| 541 | local trigger_name="$4" |
| 542 | local delayon="$5" |
| 543 | local delayoff="$6" |
| 544 | |
| 545 | _ucidef_set_led_common "$1" "$2" "$3" |
| 546 | |
| 547 | json_add_string type "$trigger_name" |
| 548 | json_add_string trigger "$trigger_name" |
| 549 | json_add_int delayon "$delayon" |
| 550 | json_add_int delayoff "$delayoff" |
| 551 | json_select .. |
| 552 | |
| 553 | json_select .. |
| 554 | } |
| 555 | |
| 556 | ucidef_set_led_timer() { |
| 557 | _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5 |
| 558 | } |
| 559 | |
| 560 | _ucidef_set_led_trigger() { |
| 561 | local trigger_name="$4" |
| 562 | |
| 563 | _ucidef_set_led_common "$1" "$2" "$3" |
| 564 | |
| 565 | json_add_string trigger "$trigger_name" |
| 566 | json_select .. |
| 567 | |
| 568 | json_select .. |
| 569 | } |
| 570 | |
| 571 | ucidef_set_led_usbdev() { |
| 572 | local dev="$4" |
| 573 | |
| 574 | _ucidef_set_led_common "$1" "$2" "$3" |
| 575 | |
| 576 | json_add_string type usb |
| 577 | json_add_string device "$dev" |
| 578 | json_select .. |
| 579 | |
| 580 | json_select .. |
| 581 | } |
| 582 | |
| 583 | ucidef_set_led_usbhost() { |
| 584 | _ucidef_set_led_trigger "$1" "$2" "$3" usb-host |
| 585 | } |
| 586 | |
| 587 | ucidef_set_led_usbport() { |
| 588 | local obj="$1" |
| 589 | local name="$2" |
| 590 | local sysfs="$3" |
| 591 | shift |
| 592 | shift |
| 593 | shift |
| 594 | |
| 595 | _ucidef_set_led_common "$obj" "$name" "$sysfs" |
| 596 | |
| 597 | json_add_string type usbport |
| 598 | json_select_array ports |
| 599 | for port in "$@"; do |
| 600 | json_add_string port "$port" |
| 601 | done |
| 602 | json_select .. |
| 603 | json_select .. |
| 604 | |
| 605 | json_select .. |
| 606 | } |
| 607 | |
| 608 | ucidef_set_led_wlan() { |
| 609 | _ucidef_set_led_trigger "$1" "$2" "$3" "$4" |
| 610 | } |
| 611 | |
| 612 | ucidef_set_rssimon() { |
| 613 | local dev="$1" |
| 614 | local refresh="$2" |
| 615 | local threshold="$3" |
| 616 | |
| 617 | json_select_object rssimon |
| 618 | |
| 619 | json_select_object "$dev" |
| 620 | [ -n "$refresh" ] && json_add_int refresh "$refresh" |
| 621 | [ -n "$threshold" ] && json_add_int threshold "$threshold" |
| 622 | json_select .. |
| 623 | |
| 624 | json_select .. |
| 625 | } |
| 626 | |
| 627 | ucidef_add_gpio_switch() { |
| 628 | local cfg="$1" |
| 629 | local name="$2" |
| 630 | local pin="$3" |
| 631 | local default="${4:-0}" |
| 632 | |
| 633 | json_select_object gpioswitch |
| 634 | json_select_object "$cfg" |
| 635 | json_add_string name "$name" |
| 636 | json_add_string pin "$pin" |
| 637 | json_add_int default "$default" |
| 638 | json_select .. |
| 639 | json_select .. |
| 640 | } |
| 641 | |
| 642 | ucidef_set_hostname() { |
| 643 | local hostname="$1" |
| 644 | |
| 645 | json_select_object system |
| 646 | json_add_string hostname "$hostname" |
| 647 | json_select .. |
| 648 | } |
| 649 | |
| 650 | ucidef_set_timezone() { |
| 651 | local timezone="$1" |
| 652 | json_select_object system |
| 653 | json_add_string timezone "$timezone" |
| 654 | json_select .. |
| 655 | } |
| 656 | |
| 657 | ucidef_set_wireless() { |
| 658 | local band="$1" |
| 659 | local ssid="$2" |
| 660 | local encryption="$3" |
| 661 | local key="$4" |
| 662 | |
| 663 | case "$band" in |
| 664 | all|2g|5g|6g) ;; |
| 665 | *) return;; |
| 666 | esac |
| 667 | [ -z "$ssid" ] && return |
| 668 | |
| 669 | json_select_object wlan |
| 670 | json_select_object defaults |
| 671 | json_select_object ssids |
| 672 | json_select_object "$band" |
| 673 | json_add_string ssid "$ssid" |
| 674 | [ -n "$encryption" ] && json_add_string encryption "$encryption" |
| 675 | [ -n "$key" ] && json_add_string key "$key" |
| 676 | json_select .. |
| 677 | json_select .. |
| 678 | json_select .. |
| 679 | json_select .. |
| 680 | } |
| 681 | |
| 682 | ucidef_set_country() { |
| 683 | local country="$1" |
| 684 | |
| 685 | json_select_object wlan |
| 686 | json_select_object defaults |
| 687 | json_add_string country "$country" |
| 688 | json_select .. |
| 689 | json_select .. |
| 690 | } |
| 691 | |
| 692 | ucidef_set_wireless_mac_count() { |
| 693 | local band="$1" |
| 694 | local mac_count="$2" |
| 695 | |
| 696 | case "$band" in |
| 697 | 2g|5g|6g) ;; |
| 698 | *) return;; |
| 699 | esac |
| 700 | [ -z "$mac_count" ] && return |
| 701 | |
| 702 | json_select_object wlan |
| 703 | json_select_object defaults |
| 704 | json_select_object ssids |
| 705 | json_select_object "$band" |
| 706 | json_add_string mac_count "$mac_count" |
| 707 | json_select .. |
| 708 | json_select .. |
| 709 | json_select .. |
| 710 | json_select .. |
| 711 | } |
| 712 | |
| 713 | ucidef_set_root_password_plain() { |
| 714 | local passwd="$1" |
| 715 | json_select_object credentials |
| 716 | json_add_string root_password_plain "$passwd" |
| 717 | json_select .. |
| 718 | } |
| 719 | |
| 720 | ucidef_set_root_password_hash() { |
| 721 | local passwd="$1" |
| 722 | json_select_object credentials |
| 723 | json_add_string root_password_hash "$passwd" |
| 724 | json_select .. |
| 725 | } |
| 726 | |
| 727 | ucidef_set_ssh_authorized_key() { |
| 728 | local ssh_key="$1" |
| 729 | json_select_object credentials |
| 730 | json_select_array ssh_authorized_keys |
| 731 | json_add_string "" "$ssh_key" |
| 732 | json_select .. |
| 733 | json_select .. |
| 734 | } |
| 735 | |
| 736 | ucidef_set_ntpserver() { |
| 737 | local server |
| 738 | |
| 739 | json_select_object system |
| 740 | json_select_array ntpserver |
| 741 | for server in "$@"; do |
| 742 | json_add_string "" "$server" |
| 743 | done |
| 744 | json_select .. |
| 745 | json_select .. |
| 746 | } |
| 747 | |
| 748 | ucidef_set_poe() { |
| 749 | json_select_object poe |
| 750 | json_add_string "budget" "$1" |
| 751 | json_select_array ports |
| 752 | for port in $2; do |
| 753 | json_add_string "" "$port" |
| 754 | done |
| 755 | json_select .. |
| 756 | json_select .. |
| 757 | } |
| 758 | |
| 759 | ucidef_add_wlan() { |
| 760 | local path="$1"; shift |
| 761 | |
| 762 | ucidef_wlan_idx=${ucidef_wlan_idx:-0} |
| 763 | |
| 764 | json_select_object wlan |
| 765 | json_select_object "wl$ucidef_wlan_idx" |
| 766 | json_add_string path "$path" |
| 767 | json_add_fields "$@" |
| 768 | json_select .. |
| 769 | json_select .. |
| 770 | |
| 771 | ucidef_wlan_idx="$((ucidef_wlan_idx + 1))" |
| 772 | } |
| 773 | |
| 774 | board_config_update() { |
| 775 | json_init |
| 776 | [ -f ${CFG} ] && json_load "$(cat ${CFG})" |
| 777 | |
| 778 | # auto-initialize model id and name if applicable |
| 779 | if ! json_is_a model object; then |
| 780 | json_select_object model |
| 781 | [ -f "/tmp/sysinfo/board_name" ] && \ |
| 782 | json_add_string id "$(cat /tmp/sysinfo/board_name)" |
| 783 | [ -f "/tmp/sysinfo/model" ] && \ |
| 784 | json_add_string name "$(cat /tmp/sysinfo/model)" |
| 785 | json_select .. |
| 786 | fi |
| 787 | } |
| 788 | |
| 789 | board_config_flush() { |
| 790 | json_dump -i -o ${CFG} |
| 791 | } |