1
0

uci-defaults.sh 12 KB

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