uci-defaults.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #!/bin/ash
  2. CFG=/etc/board.json
  3. . /lib/functions.sh
  4. . /usr/share/libubox/jshn.sh
  5. json_select_array() {
  6. local _json_no_warning=1
  7. json_select "$1"
  8. [ $? = 0 ] && return
  9. json_add_array "$1"
  10. json_close_array
  11. json_select "$1"
  12. }
  13. json_select_object() {
  14. local _json_no_warning=1
  15. json_select "$1"
  16. [ $? = 0 ] && return
  17. json_add_object "$1"
  18. json_close_object
  19. json_select "$1"
  20. }
  21. _ucidef_set_interface() {
  22. local name="$1"
  23. local iface="$2"
  24. local proto="$3"
  25. json_select_object "$name"
  26. json_add_string ifname "$iface"
  27. if ! json_is_a protocol string; then
  28. case "$proto" in
  29. static|dhcp|none|pppoe) : ;;
  30. *)
  31. case "$name" in
  32. lan) proto="static" ;;
  33. wan) proto="dhcp" ;;
  34. *) proto="none" ;;
  35. esac
  36. ;;
  37. esac
  38. json_add_string protocol "$proto"
  39. fi
  40. json_select ..
  41. }
  42. ucidef_set_board_id() {
  43. json_select_object model
  44. json_add_string id "$1"
  45. json_select ..
  46. }
  47. ucidef_set_model_name() {
  48. json_select_object model
  49. json_add_string name "$1"
  50. json_select ..
  51. }
  52. ucidef_set_interface_lan() {
  53. json_select_object network
  54. _ucidef_set_interface lan "$@"
  55. json_select ..
  56. }
  57. ucidef_set_interface_wan() {
  58. json_select_object network
  59. _ucidef_set_interface wan "$@"
  60. json_select ..
  61. }
  62. ucidef_set_interfaces_lan_wan() {
  63. local lan_if="$1"
  64. local wan_if="$2"
  65. json_select_object network
  66. _ucidef_set_interface lan "$lan_if"
  67. _ucidef_set_interface wan "$wan_if"
  68. json_select ..
  69. }
  70. ucidef_set_interface_raw() {
  71. json_select_object network
  72. _ucidef_set_interface "$@"
  73. json_select ..
  74. }
  75. _ucidef_add_switch_port() {
  76. # inherited: $num $device $need_tag $role $index $prev_role
  77. # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
  78. n_ports=$((n_ports + 1))
  79. json_select_array ports
  80. json_add_object
  81. json_add_int num "$num"
  82. [ -n "$device" ] && json_add_string device "$device"
  83. [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
  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 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
  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. [ "${num%t}" != "$num" ] && {
  171. num="${num%t}"
  172. need_tag=1
  173. }
  174. ;;
  175. [0-9]*:*:[0-9]*)
  176. num="${port%%:*}"
  177. index="${port##*:}"
  178. role="${port#[0-9]*:}"; role="${role%:*}"
  179. ;;
  180. [0-9]*:*)
  181. num="${port%%:*}"
  182. role="${port##*:}"
  183. ;;
  184. esac
  185. if [ -n "$num" ] && [ -n "$device$role" ]; then
  186. _ucidef_add_switch_port
  187. fi
  188. unset num device role index need_tag
  189. done
  190. json_select ..
  191. json_select ..
  192. _ucidef_finish_switch_roles
  193. }
  194. ucidef_add_switch_attr() {
  195. local name="$1"
  196. local key="$2"
  197. local val="$3"
  198. json_select_object switch
  199. json_select_object "$name"
  200. case "$val" in
  201. true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
  202. [0-9]) json_add_int "$key" "$val" ;;
  203. *) json_add_string "$key" "$val" ;;
  204. esac
  205. json_select ..
  206. json_select ..
  207. }
  208. ucidef_add_switch_port_attr() {
  209. local name="$1"
  210. local port="$2"
  211. local key="$3"
  212. local val="$4"
  213. local ports i num
  214. json_select_object switch
  215. json_select_object "$name"
  216. json_get_keys ports ports
  217. json_select_array ports
  218. for i in $ports; do
  219. json_select "$i"
  220. json_get_var num num
  221. if [ -n "$num" ] && [ $num -eq $port ]; then
  222. json_select_object attr
  223. case "$val" in
  224. true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
  225. [0-9]) json_add_int "$key" "$val" ;;
  226. *) json_add_string "$key" "$val" ;;
  227. esac
  228. json_select ..
  229. fi
  230. json_select ..
  231. done
  232. json_select ..
  233. json_select ..
  234. json_select ..
  235. }
  236. ucidef_set_interface_macaddr() {
  237. local network="$1"
  238. local macaddr="$2"
  239. json_select_object network
  240. json_select "$network"
  241. [ $? -eq 0 ] || {
  242. json_select ..
  243. return
  244. }
  245. json_add_string macaddr "$macaddr"
  246. json_select ..
  247. json_select ..
  248. }
  249. ucidef_add_atm_bridge() {
  250. local vpi="$1"
  251. local vci="$2"
  252. local encaps="$3"
  253. local payload="$4"
  254. json_select_object dsl
  255. json_select_object atmbridge
  256. json_add_int vpi "$vpi"
  257. json_add_int vci "$vci"
  258. json_add_string encaps "$encaps"
  259. json_add_string payload "$payload"
  260. json_select ..
  261. json_select ..
  262. }
  263. ucidef_add_adsl_modem() {
  264. local annex="$1"
  265. local firmware="$2"
  266. json_select_object dsl
  267. json_select_object modem
  268. json_add_string type "adsl"
  269. json_add_string annex "$annex"
  270. json_add_string firmware "$firmware"
  271. json_select ..
  272. json_select ..
  273. }
  274. ucidef_add_vdsl_modem() {
  275. local annex="$1"
  276. local tone="$2"
  277. local xfer_mode="$3"
  278. json_select_object dsl
  279. json_select_object modem
  280. json_add_string type "vdsl"
  281. json_add_string annex "$annex"
  282. json_add_string tone "$tone"
  283. json_add_string xfer_mode "$xfer_mode"
  284. json_select ..
  285. json_select ..
  286. }
  287. ucidef_set_led_netdev() {
  288. local cfg="led_$1"
  289. local name="$2"
  290. local sysfs="$3"
  291. local dev="$4"
  292. local mode="${5:-link tx rx}"
  293. json_select_object led
  294. json_select_object "$1"
  295. json_add_string name "$name"
  296. json_add_string type netdev
  297. json_add_string sysfs "$sysfs"
  298. json_add_string device "$dev"
  299. json_add_string mode "$mode"
  300. json_select ..
  301. json_select ..
  302. }
  303. ucidef_set_led_usbdev() {
  304. local cfg="led_$1"
  305. local name="$2"
  306. local sysfs="$3"
  307. local dev="$4"
  308. json_select_object led
  309. json_select_object "$1"
  310. json_add_string name "$name"
  311. json_add_string type usb
  312. json_add_string sysfs "$sysfs"
  313. json_add_string device "$dev"
  314. json_select ..
  315. json_select ..
  316. }
  317. ucidef_set_led_wlan() {
  318. local cfg="led_$1"
  319. local name="$2"
  320. local sysfs="$3"
  321. local trigger="$4"
  322. json_select_object led
  323. json_select_object "$1"
  324. json_add_string name "$name"
  325. json_add_string type trigger
  326. json_add_string sysfs "$sysfs"
  327. json_add_string trigger "$trigger"
  328. json_select ..
  329. json_select ..
  330. }
  331. ucidef_set_led_switch() {
  332. local cfg="led_$1"
  333. local name="$2"
  334. local sysfs="$3"
  335. local trigger="$4"
  336. local port_mask="$5"
  337. local speed_mask="$6"
  338. json_select_object led
  339. json_select_object "$1"
  340. json_add_string name "$name"
  341. json_add_string type switch
  342. json_add_string sysfs "$sysfs"
  343. json_add_string trigger "$trigger"
  344. json_add_string port_mask "$port_mask"
  345. json_add_string speed_mask "$speed_mask"
  346. json_select ..
  347. json_select ..
  348. }
  349. ucidef_set_led_default() {
  350. local cfg="led_$1"
  351. local name="$2"
  352. local sysfs="$3"
  353. local default="$4"
  354. json_select_object led
  355. json_select_object "$1"
  356. json_add_string name "$name"
  357. json_add_string sysfs "$sysfs"
  358. json_add_string default "$default"
  359. json_select ..
  360. json_select ..
  361. }
  362. ucidef_set_led_gpio() {
  363. local cfg="led_$1"
  364. local name="$2"
  365. local sysfs="$3"
  366. local gpio="$4"
  367. local inverted="$5"
  368. json_select_object led
  369. json_select_object "$1"
  370. json_add_string type gpio
  371. json_add_string name "$name"
  372. json_add_string sysfs "$sysfs"
  373. json_add_string trigger "$trigger"
  374. json_add_int gpio "$gpio"
  375. json_add_boolean inverted "$inverted"
  376. json_select ..
  377. json_select ..
  378. }
  379. ucidef_set_led_ide() {
  380. local cfg="led_$1"
  381. local name="$2"
  382. local sysfs="$3"
  383. json_select_object led
  384. json_select_object "$1"
  385. json_add_string name "$name"
  386. json_add_string sysfs "$sysfs"
  387. json_add_string trigger ide-disk
  388. json_select ..
  389. json_select ..
  390. }
  391. __ucidef_set_led_timer() {
  392. local cfg="led_$1"
  393. local name="$2"
  394. local sysfs="$3"
  395. local trigger="$4"
  396. local delayon="$5"
  397. local delayoff="$6"
  398. json_select_object led
  399. json_select_object "$1"
  400. json_add_string type "$trigger"
  401. json_add_string name "$name"
  402. json_add_string sysfs "$sysfs"
  403. json_add_int delayon "$delayon"
  404. json_add_int delayoff "$delayoff"
  405. json_select ..
  406. json_select ..
  407. }
  408. ucidef_set_led_oneshot() {
  409. __ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
  410. }
  411. ucidef_set_led_timer() {
  412. __ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
  413. }
  414. ucidef_set_led_rssi() {
  415. local cfg="led_$1"
  416. local name="$2"
  417. local sysfs="$3"
  418. local iface="$4"
  419. local minq="$5"
  420. local maxq="$6"
  421. local offset="$7"
  422. local factor="$8"
  423. json_select_object led
  424. json_select_object "$1"
  425. json_add_string type rssi
  426. json_add_string name "$name"
  427. json_add_string iface "$iface"
  428. json_add_string sysfs "$sysfs"
  429. json_add_string minq "$minq"
  430. json_add_string maxq "$maxq"
  431. json_add_string offset "$offset"
  432. json_add_string factor "$factor"
  433. json_select ..
  434. json_select ..
  435. }
  436. ucidef_set_rssimon() {
  437. local dev="$1"
  438. local refresh="$2"
  439. local threshold="$3"
  440. json_select_object rssimon
  441. json_select_object "$dev"
  442. [ -n "$refresh" ] && json_add_int refresh "$refresh"
  443. [ -n "$threshold" ] && json_add_int threshold "$threshold"
  444. json_select ..
  445. json_select ..
  446. }
  447. ucidef_add_gpio_switch() {
  448. local cfg="$1"
  449. local name="$2"
  450. local pin="$3"
  451. local default="${4:-0}"
  452. json_select_object gpioswitch
  453. json_select_object "$cfg"
  454. json_add_string name "$name"
  455. json_add_int pin "$pin"
  456. json_add_int default "$default"
  457. json_select ..
  458. json_select ..
  459. }
  460. board_config_update() {
  461. json_init
  462. [ -f ${CFG} ] && json_load "$(cat ${CFG})"
  463. # auto-initialize model id and name if applicable
  464. if ! json_is_a model object; then
  465. json_select_object model
  466. [ -f "/tmp/sysinfo/board_name" ] && \
  467. json_add_string id "$(cat /tmp/sysinfo/board_name)"
  468. [ -f "/tmp/sysinfo/model" ] && \
  469. json_add_string name "$(cat /tmp/sysinfo/model)"
  470. json_select ..
  471. fi
  472. }
  473. board_config_flush() {
  474. json_dump -i > /tmp/.board.json
  475. mv /tmp/.board.json ${CFG}
  476. }