1
0

uci-defaults.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_add_switch() {
  145. local name="$1"; shift
  146. local port num role device index need_tag prev_role
  147. local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
  148. local n_cpu=0 n_vlan=0 n_ports=0
  149. json_select_object switch
  150. json_select_object "$name"
  151. json_add_boolean enable 1
  152. json_add_boolean reset 1
  153. for port in "$@"; do
  154. case "$port" in
  155. [0-9]*@*)
  156. num="${port%%@*}"
  157. device="${port##*@}"
  158. need_tag=0
  159. want_untag=0
  160. [ "${num%t}" != "$num" ] && {
  161. num="${num%t}"
  162. need_tag=1
  163. }
  164. [ "${num%u}" != "$num" ] && {
  165. num="${num%u}"
  166. want_untag=1
  167. }
  168. ;;
  169. [0-9]*:*:[0-9]*)
  170. num="${port%%:*}"
  171. index="${port##*:}"
  172. role="${port#[0-9]*:}"; role="${role%:*}"
  173. ;;
  174. [0-9]*:*)
  175. num="${port%%:*}"
  176. role="${port##*:}"
  177. ;;
  178. esac
  179. if [ -n "$num" ] && [ -n "$device$role" ]; then
  180. _ucidef_add_switch_port
  181. fi
  182. unset num device role index need_tag want_untag
  183. done
  184. json_select ..
  185. json_select ..
  186. _ucidef_finish_switch_roles
  187. }
  188. ucidef_add_switch_attr() {
  189. local name="$1"
  190. local key="$2"
  191. local val="$3"
  192. json_select_object switch
  193. json_select_object "$name"
  194. case "$val" in
  195. true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
  196. [0-9]) json_add_int "$key" "$val" ;;
  197. *) json_add_string "$key" "$val" ;;
  198. esac
  199. json_select ..
  200. json_select ..
  201. }
  202. ucidef_add_switch_port_attr() {
  203. local name="$1"
  204. local port="$2"
  205. local key="$3"
  206. local val="$4"
  207. local ports i num
  208. json_select_object switch
  209. json_select_object "$name"
  210. json_get_keys ports ports
  211. json_select_array ports
  212. for i in $ports; do
  213. json_select "$i"
  214. json_get_var num num
  215. if [ -n "$num" ] && [ $num -eq $port ]; then
  216. json_select_object attr
  217. case "$val" in
  218. true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
  219. [0-9]) json_add_int "$key" "$val" ;;
  220. *) json_add_string "$key" "$val" ;;
  221. esac
  222. json_select ..
  223. fi
  224. json_select ..
  225. done
  226. json_select ..
  227. json_select ..
  228. json_select ..
  229. }
  230. ucidef_set_interface_macaddr() {
  231. local network="$1"
  232. local macaddr="$2"
  233. ucidef_set_interface "$network" macaddr "$macaddr"
  234. }
  235. ucidef_add_atm_bridge() {
  236. local vpi="$1"
  237. local vci="$2"
  238. local encaps="$3"
  239. local payload="$4"
  240. local nameprefix="$5"
  241. json_select_object dsl
  242. json_select_object atmbridge
  243. json_add_int vpi "$vpi"
  244. json_add_int vci "$vci"
  245. json_add_string encaps "$encaps"
  246. json_add_string payload "$payload"
  247. json_add_string nameprefix "$nameprefix"
  248. json_select ..
  249. json_select ..
  250. }
  251. ucidef_add_adsl_modem() {
  252. local annex="$1"
  253. local firmware="$2"
  254. json_select_object dsl
  255. json_select_object modem
  256. json_add_string type "adsl"
  257. json_add_string annex "$annex"
  258. json_add_string firmware "$firmware"
  259. json_select ..
  260. json_select ..
  261. }
  262. ucidef_add_vdsl_modem() {
  263. local annex="$1"
  264. local tone="$2"
  265. local xfer_mode="$3"
  266. json_select_object dsl
  267. json_select_object modem
  268. json_add_string type "vdsl"
  269. json_add_string annex "$annex"
  270. json_add_string tone "$tone"
  271. json_add_string xfer_mode "$xfer_mode"
  272. json_select ..
  273. json_select ..
  274. }
  275. ucidef_set_led_ataport() {
  276. _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
  277. }
  278. _ucidef_set_led_common() {
  279. local cfg="led_$1"
  280. local name="$2"
  281. local sysfs="$3"
  282. json_select_object led
  283. json_select_object "$1"
  284. json_add_string name "$name"
  285. json_add_string sysfs "$sysfs"
  286. }
  287. ucidef_set_led_default() {
  288. local default="$4"
  289. _ucidef_set_led_common "$1" "$2" "$3"
  290. json_add_string default "$default"
  291. json_select ..
  292. json_select ..
  293. }
  294. ucidef_set_led_gpio() {
  295. local gpio="$4"
  296. local inverted="$5"
  297. _ucidef_set_led_common "$1" "$2" "$3"
  298. json_add_string trigger "$trigger"
  299. json_add_string type gpio
  300. json_add_int gpio "$gpio"
  301. json_add_boolean inverted "$inverted"
  302. json_select ..
  303. json_select ..
  304. }
  305. ucidef_set_led_ide() {
  306. _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
  307. }
  308. ucidef_set_led_netdev() {
  309. local dev="$4"
  310. local mode="${5:-link tx rx}"
  311. _ucidef_set_led_common "$1" "$2" "$3"
  312. json_add_string type netdev
  313. json_add_string device "$dev"
  314. json_add_string mode "$mode"
  315. json_select ..
  316. json_select ..
  317. }
  318. ucidef_set_led_oneshot() {
  319. _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
  320. }
  321. ucidef_set_led_portstate() {
  322. local port_state="$4"
  323. _ucidef_set_led_common "$1" "$2" "$3"
  324. json_add_string trigger port_state
  325. json_add_string type portstate
  326. json_add_string port_state "$port_state"
  327. json_select ..
  328. json_select ..
  329. }
  330. ucidef_set_led_rssi() {
  331. local iface="$4"
  332. local minq="$5"
  333. local maxq="$6"
  334. local offset="${7:-0}"
  335. local factor="${8:-1}"
  336. _ucidef_set_led_common "$1" "$2" "$3"
  337. json_add_string type rssi
  338. json_add_string name "$name"
  339. json_add_string iface "$iface"
  340. json_add_string minq "$minq"
  341. json_add_string maxq "$maxq"
  342. json_add_string offset "$offset"
  343. json_add_string factor "$factor"
  344. json_select ..
  345. json_select ..
  346. }
  347. ucidef_set_led_switch() {
  348. local trigger_name="$4"
  349. local port_mask="$5"
  350. local speed_mask="$6"
  351. local mode="$7"
  352. _ucidef_set_led_common "$1" "$2" "$3"
  353. json_add_string trigger "$trigger_name"
  354. json_add_string type switch
  355. json_add_string mode "$mode"
  356. json_add_string port_mask "$port_mask"
  357. json_add_string speed_mask "$speed_mask"
  358. json_select ..
  359. json_select ..
  360. }
  361. _ucidef_set_led_timer() {
  362. local trigger_name="$4"
  363. local delayon="$5"
  364. local delayoff="$6"
  365. _ucidef_set_led_common "$1" "$2" "$3"
  366. json_add_string trigger "$trigger_name"
  367. json_add_int delayon "$delayon"
  368. json_add_int delayoff "$delayoff"
  369. json_select ..
  370. json_select ..
  371. }
  372. ucidef_set_led_timer() {
  373. _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
  374. }
  375. _ucidef_set_led_trigger() {
  376. local trigger_name="$4"
  377. _ucidef_set_led_common "$1" "$2" "$3"
  378. json_add_string trigger "$trigger_name"
  379. json_select ..
  380. json_select ..
  381. }
  382. ucidef_set_led_usbdev() {
  383. local dev="$4"
  384. _ucidef_set_led_common "$1" "$2" "$3"
  385. json_add_string type usb
  386. json_add_string device "$dev"
  387. json_select ..
  388. json_select ..
  389. }
  390. ucidef_set_led_usbhost() {
  391. _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
  392. }
  393. ucidef_set_led_usbport() {
  394. local obj="$1"
  395. local name="$2"
  396. local sysfs="$3"
  397. shift
  398. shift
  399. shift
  400. _ucidef_set_led_common "$obj" "$name" "$sysfs"
  401. json_add_string type usbport
  402. json_select_array ports
  403. for port in "$@"; do
  404. json_add_string port "$port"
  405. done
  406. json_select ..
  407. json_select ..
  408. json_select ..
  409. }
  410. ucidef_set_led_wlan() {
  411. _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
  412. }
  413. ucidef_set_rssimon() {
  414. local dev="$1"
  415. local refresh="$2"
  416. local threshold="$3"
  417. json_select_object rssimon
  418. json_select_object "$dev"
  419. [ -n "$refresh" ] && json_add_int refresh "$refresh"
  420. [ -n "$threshold" ] && json_add_int threshold "$threshold"
  421. json_select ..
  422. json_select ..
  423. }
  424. ucidef_add_gpio_switch() {
  425. local cfg="$1"
  426. local name="$2"
  427. local pin="$3"
  428. local default="${4:-0}"
  429. json_select_object gpioswitch
  430. json_select_object "$cfg"
  431. json_add_string name "$name"
  432. json_add_int pin "$pin"
  433. json_add_int default "$default"
  434. json_select ..
  435. json_select ..
  436. }
  437. ucidef_set_hostname() {
  438. local hostname="$1"
  439. json_select_object system
  440. json_add_string hostname "$hostname"
  441. json_select ..
  442. }
  443. ucidef_set_ntpserver() {
  444. local server
  445. json_select_object system
  446. json_select_array ntpserver
  447. for server in "$@"; do
  448. json_add_string "" "$server"
  449. done
  450. json_select ..
  451. json_select ..
  452. }
  453. board_config_update() {
  454. json_init
  455. [ -f ${CFG} ] && json_load "$(cat ${CFG})"
  456. # auto-initialize model id and name if applicable
  457. if ! json_is_a model object; then
  458. json_select_object model
  459. [ -f "/tmp/sysinfo/board_name" ] && \
  460. json_add_string id "$(cat /tmp/sysinfo/board_name)"
  461. [ -f "/tmp/sysinfo/model" ] && \
  462. json_add_string name "$(cat /tmp/sysinfo/model)"
  463. json_select ..
  464. fi
  465. }
  466. board_config_flush() {
  467. json_dump -i > /tmp/.board.json
  468. mv /tmp/.board.json ${CFG}
  469. }