1
0

uci-defaults.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 network=$1; shift
  22. [ -z "$network" ] && return
  23. json_select_object network
  24. json_select_object "$network"
  25. while [ -n "$1" ]; do
  26. local opt=$1; shift
  27. local val=$1; shift
  28. [ -n "$opt" -a -n "$val" ] || break
  29. json_add_string "$opt" "$val"
  30. done
  31. if ! json_is_a protocol string; then
  32. case "$network" in
  33. lan) json_add_string protocol static ;;
  34. wan) json_add_string protocol dhcp ;;
  35. *) json_add_string protocol none ;;
  36. esac
  37. fi
  38. json_select ..
  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. ucidef_set_interface "lan" ifname "$1" protocol "${2:-static}"
  53. }
  54. ucidef_set_interface_wan() {
  55. ucidef_set_interface "wan" ifname "$1" protocol "${2:-dhcp}"
  56. }
  57. ucidef_set_interfaces_lan_wan() {
  58. local lan_if="$1"
  59. local wan_if="$2"
  60. ucidef_set_interface_lan "$lan_if"
  61. ucidef_set_interface_wan "$wan_if"
  62. }
  63. _ucidef_add_switch_port() {
  64. # inherited: $num $device $need_tag $want_untag $role $index $prev_role
  65. # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
  66. n_ports=$((n_ports + 1))
  67. json_select_array ports
  68. json_add_object
  69. json_add_int num "$num"
  70. [ -n "$device" ] && json_add_string device "$device"
  71. [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
  72. [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag"
  73. [ -n "$role" ] && json_add_string role "$role"
  74. [ -n "$index" ] && json_add_int index "$index"
  75. json_close_object
  76. json_select ..
  77. # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
  78. [ -n "$device" ] && {
  79. export "cpu$n_cpu=$n_ports"
  80. n_cpu=$((n_cpu + 1))
  81. }
  82. # create/append object to role list
  83. [ -n "$role" ] && {
  84. json_select_array roles
  85. if [ "$role" != "$prev_role" ]; then
  86. json_add_object
  87. json_add_string role "$role"
  88. json_add_string ports "$num"
  89. json_close_object
  90. prev_role="$role"
  91. n_vlan=$((n_vlan + 1))
  92. else
  93. json_select_object "$n_vlan"
  94. json_get_var port ports
  95. json_add_string ports "$port $num"
  96. json_select ..
  97. fi
  98. json_select ..
  99. }
  100. }
  101. _ucidef_finish_switch_roles() {
  102. # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
  103. local index role roles num device need_tag want_untag port ports
  104. json_select switch
  105. json_select "$name"
  106. json_get_keys roles roles
  107. json_select ..
  108. json_select ..
  109. for index in $roles; do
  110. eval "port=\$cpu$(((index - 1) % n_cpu))"
  111. json_select switch
  112. json_select "$name"
  113. json_select ports
  114. json_select "$port"
  115. json_get_vars num device need_tag want_untag
  116. json_select ..
  117. json_select ..
  118. if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
  119. num="${num}t"
  120. device="${device}.${index}"
  121. fi
  122. json_select roles
  123. json_select "$index"
  124. json_get_vars role ports
  125. json_add_string ports "$ports $num"
  126. json_add_string device "$device"
  127. json_select ..
  128. json_select ..
  129. json_select ..
  130. json_select ..
  131. json_select_object network
  132. local devices
  133. json_select_object "$role"
  134. # attach previous interfaces (for multi-switch devices)
  135. json_get_var devices ifname
  136. if ! list_contains devices "$device"; then
  137. devices="${devices:+$devices }$device"
  138. fi
  139. json_select ..
  140. json_select ..
  141. ucidef_set_interface "$role" ifname "$devices"
  142. done
  143. }
  144. ucidef_set_ar8xxx_switch_mib() {
  145. local name="$1"
  146. local type="$2"
  147. local interval="$3"
  148. json_select_object switch
  149. json_select_object "$name"
  150. json_add_int ar8xxx_mib_type $type
  151. json_add_int ar8xxx_mib_poll_interval $interval
  152. json_select ..
  153. json_select ..
  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. ucidef_set_interface "$network" macaddr "$macaddr"
  245. }
  246. ucidef_add_atm_bridge() {
  247. local vpi="$1"
  248. local vci="$2"
  249. local encaps="$3"
  250. local payload="$4"
  251. local nameprefix="$5"
  252. json_select_object dsl
  253. json_select_object atmbridge
  254. json_add_int vpi "$vpi"
  255. json_add_int vci "$vci"
  256. json_add_string encaps "$encaps"
  257. json_add_string payload "$payload"
  258. json_add_string nameprefix "$nameprefix"
  259. json_select ..
  260. json_select ..
  261. }
  262. ucidef_add_adsl_modem() {
  263. local annex="$1"
  264. local firmware="$2"
  265. json_select_object dsl
  266. json_select_object modem
  267. json_add_string type "adsl"
  268. json_add_string annex "$annex"
  269. json_add_string firmware "$firmware"
  270. json_select ..
  271. json_select ..
  272. }
  273. ucidef_add_vdsl_modem() {
  274. local annex="$1"
  275. local tone="$2"
  276. local xfer_mode="$3"
  277. json_select_object dsl
  278. json_select_object modem
  279. json_add_string type "vdsl"
  280. json_add_string annex "$annex"
  281. json_add_string tone "$tone"
  282. json_add_string xfer_mode "$xfer_mode"
  283. json_select ..
  284. json_select ..
  285. }
  286. ucidef_set_led_ataport() {
  287. _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
  288. }
  289. _ucidef_set_led_common() {
  290. local cfg="led_$1"
  291. local name="$2"
  292. local sysfs="$3"
  293. json_select_object led
  294. json_select_object "$1"
  295. json_add_string name "$name"
  296. json_add_string sysfs "$sysfs"
  297. }
  298. ucidef_set_led_default() {
  299. local default="$4"
  300. _ucidef_set_led_common "$1" "$2" "$3"
  301. json_add_string default "$default"
  302. json_select ..
  303. json_select ..
  304. }
  305. ucidef_set_led_gpio() {
  306. local gpio="$4"
  307. local inverted="$5"
  308. _ucidef_set_led_common "$1" "$2" "$3"
  309. json_add_string trigger "$trigger"
  310. json_add_string type gpio
  311. json_add_int gpio "$gpio"
  312. json_add_boolean inverted "$inverted"
  313. json_select ..
  314. json_select ..
  315. }
  316. ucidef_set_led_ide() {
  317. _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
  318. }
  319. ucidef_set_led_netdev() {
  320. local dev="$4"
  321. local mode="${5:-link tx rx}"
  322. _ucidef_set_led_common "$1" "$2" "$3"
  323. json_add_string type netdev
  324. json_add_string device "$dev"
  325. json_add_string mode "$mode"
  326. json_select ..
  327. json_select ..
  328. }
  329. ucidef_set_led_oneshot() {
  330. _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
  331. }
  332. ucidef_set_led_portstate() {
  333. local port_state="$4"
  334. _ucidef_set_led_common "$1" "$2" "$3"
  335. json_add_string trigger port_state
  336. json_add_string type portstate
  337. json_add_string port_state "$port_state"
  338. json_select ..
  339. json_select ..
  340. }
  341. ucidef_set_led_rssi() {
  342. local iface="$4"
  343. local minq="$5"
  344. local maxq="$6"
  345. local offset="${7:-0}"
  346. local factor="${8:-1}"
  347. _ucidef_set_led_common "$1" "$2" "$3"
  348. json_add_string type rssi
  349. json_add_string name "$name"
  350. json_add_string iface "$iface"
  351. json_add_string minq "$minq"
  352. json_add_string maxq "$maxq"
  353. json_add_string offset "$offset"
  354. json_add_string factor "$factor"
  355. json_select ..
  356. json_select ..
  357. }
  358. ucidef_set_led_switch() {
  359. local trigger_name="$4"
  360. local port_mask="$5"
  361. local speed_mask="$6"
  362. local mode="$7"
  363. _ucidef_set_led_common "$1" "$2" "$3"
  364. json_add_string trigger "$trigger_name"
  365. json_add_string type switch
  366. json_add_string mode "$mode"
  367. json_add_string port_mask "$port_mask"
  368. json_add_string speed_mask "$speed_mask"
  369. json_select ..
  370. json_select ..
  371. }
  372. _ucidef_set_led_timer() {
  373. local trigger_name="$4"
  374. local delayon="$5"
  375. local delayoff="$6"
  376. _ucidef_set_led_common "$1" "$2" "$3"
  377. json_add_string type "$trigger_name"
  378. json_add_string trigger "$trigger_name"
  379. json_add_int delayon "$delayon"
  380. json_add_int delayoff "$delayoff"
  381. json_select ..
  382. json_select ..
  383. }
  384. ucidef_set_led_timer() {
  385. _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
  386. }
  387. _ucidef_set_led_trigger() {
  388. local trigger_name="$4"
  389. _ucidef_set_led_common "$1" "$2" "$3"
  390. json_add_string trigger "$trigger_name"
  391. json_select ..
  392. json_select ..
  393. }
  394. ucidef_set_led_usbdev() {
  395. local dev="$4"
  396. _ucidef_set_led_common "$1" "$2" "$3"
  397. json_add_string type usb
  398. json_add_string device "$dev"
  399. json_select ..
  400. json_select ..
  401. }
  402. ucidef_set_led_usbhost() {
  403. _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
  404. }
  405. ucidef_set_led_usbport() {
  406. local obj="$1"
  407. local name="$2"
  408. local sysfs="$3"
  409. shift
  410. shift
  411. shift
  412. _ucidef_set_led_common "$obj" "$name" "$sysfs"
  413. json_add_string type usbport
  414. json_select_array ports
  415. for port in "$@"; do
  416. json_add_string port "$port"
  417. done
  418. json_select ..
  419. json_select ..
  420. json_select ..
  421. }
  422. ucidef_set_led_wlan() {
  423. _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
  424. }
  425. ucidef_set_rssimon() {
  426. local dev="$1"
  427. local refresh="$2"
  428. local threshold="$3"
  429. json_select_object rssimon
  430. json_select_object "$dev"
  431. [ -n "$refresh" ] && json_add_int refresh "$refresh"
  432. [ -n "$threshold" ] && json_add_int threshold "$threshold"
  433. json_select ..
  434. json_select ..
  435. }
  436. ucidef_add_gpio_switch() {
  437. local cfg="$1"
  438. local name="$2"
  439. local pin="$3"
  440. local default="${4:-0}"
  441. json_select_object gpioswitch
  442. json_select_object "$cfg"
  443. json_add_string name "$name"
  444. json_add_int pin "$pin"
  445. json_add_int default "$default"
  446. json_select ..
  447. json_select ..
  448. }
  449. ucidef_set_hostname() {
  450. local hostname="$1"
  451. json_select_object system
  452. json_add_string hostname "$hostname"
  453. json_select ..
  454. }
  455. ucidef_set_ntpserver() {
  456. local server
  457. json_select_object system
  458. json_select_array ntpserver
  459. for server in "$@"; do
  460. json_add_string "" "$server"
  461. done
  462. json_select ..
  463. json_select ..
  464. }
  465. board_config_update() {
  466. json_init
  467. [ -f ${CFG} ] && json_load "$(cat ${CFG})"
  468. # auto-initialize model id and name if applicable
  469. if ! json_is_a model object; then
  470. json_select_object model
  471. [ -f "/tmp/sysinfo/board_name" ] && \
  472. json_add_string id "$(cat /tmp/sysinfo/board_name)"
  473. [ -f "/tmp/sysinfo/model" ] && \
  474. json_add_string name "$(cat /tmp/sysinfo/model)"
  475. json_select ..
  476. fi
  477. }
  478. board_config_flush() {
  479. json_dump -i > /tmp/.board.json
  480. mv /tmp/.board.json ${CFG}
  481. }