uci-defaults-new.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #!/bin/ash
  2. CFG=/etc/board.json
  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. json_select_object $name
  24. json_add_string ifname "${iface%%.*}"
  25. [ "$iface" = "${iface%%.*}" ] || json_add_boolean create_vlan 1
  26. json_select ..
  27. }
  28. ucidef_set_interface_loopback()
  29. {
  30. # stub
  31. local a=$1
  32. }
  33. ucidef_set_interface_lan() {
  34. local lan_if=$1
  35. json_select_object network
  36. _ucidef_set_interface lan $lan_if
  37. json_select ..
  38. }
  39. ucidef_set_interfaces_lan_wan() {
  40. local lan_if=$1
  41. local wan_if=$2
  42. json_select_object network
  43. _ucidef_set_interface lan $lan_if
  44. _ucidef_set_interface wan $wan_if
  45. json_select ..
  46. }
  47. ucidef_add_switch() {
  48. local name=$1
  49. local reset=$2
  50. local enable=$3
  51. json_select_object switch
  52. json_select_object $name
  53. json_add_boolean enable $enable
  54. json_add_boolean reset $reset
  55. json_select ..
  56. json_select ..
  57. }
  58. ucidef_add_switch_attr() {
  59. local name=$1
  60. local key=$2
  61. local val=$3
  62. json_select_object switch
  63. json_select_object $name
  64. json_add_string $key $val
  65. json_select ..
  66. json_select ..
  67. }
  68. ucidef_add_switch_vlan() {
  69. local name=$1
  70. local vlan=$2
  71. local ports=$3
  72. local cpu_port=''
  73. case $vlan in
  74. 1) vlan=lan;;
  75. 2) vlan=wan;;
  76. *) vlan=vlan$vlan;;
  77. esac
  78. json_select_object switch
  79. json_select_object $name
  80. json_select_object vlans
  81. json_add_array $vlan
  82. for p in $ports; do
  83. if [ ${p%t} != $p ]; then
  84. cpu_port=$p
  85. else
  86. json_add_int "" $p
  87. fi
  88. done
  89. json_close_array
  90. json_select ..
  91. [ -n "$cpu_port" ] && json_add_int cpu_port $cpu_port
  92. json_select ..
  93. json_select ..
  94. }
  95. ucidef_set_interface_macaddr() {
  96. local network=$1
  97. local macaddr=$2
  98. json_select_object network
  99. json_select $network
  100. [ $? -eq 0 ] || {
  101. json_select ..
  102. return
  103. }
  104. json_add_string macaddr $macaddr
  105. json_select ..
  106. json_select ..
  107. }
  108. ucidef_set_led_netdev() {
  109. local cfg="led_$1"
  110. local name=$2
  111. local sysfs=$3
  112. local dev=$4
  113. json_select_object led
  114. json_select_object $1
  115. json_add_string name $name
  116. json_add_string type netdev
  117. json_add_string sysfs $sysfs
  118. json_add_string device $dev
  119. json_select ..
  120. json_select ..
  121. }
  122. ucidef_set_led_interface() {
  123. local name=$1
  124. local sysfs=$2
  125. json_select_object led
  126. json_select_object $1
  127. json_add_string name $name
  128. json_add_string type interface
  129. json_add_string sysfs $sysfs
  130. json_add_string interface $name
  131. json_select ..
  132. json_select ..
  133. }
  134. ucidef_set_led_usbdev() {
  135. local cfg="led_$1"
  136. local name=$2
  137. local sysfs=$3
  138. local dev=$4
  139. json_select_object led
  140. json_select_object $1
  141. json_add_string name $name
  142. json_add_string type usb
  143. json_add_string sysfs $sysfs
  144. json_add_string device $dev
  145. json_select ..
  146. json_select ..
  147. }
  148. ucidef_set_led_wlan() {
  149. local cfg="led_$1"
  150. local name=$2
  151. local sysfs=$3
  152. local trigger=$4
  153. json_select_object led
  154. json_select_object $1
  155. json_add_string name $name
  156. json_add_string type trigger
  157. json_add_string sysfs $sysfs
  158. json_add_string trigger $trigger
  159. json_select ..
  160. json_select ..
  161. }
  162. ucidef_set_led_switch() {
  163. local cfg="led_$1"
  164. local name=$2
  165. local sysfs=$3
  166. local trigger=$4
  167. local port_mask=$5
  168. json_select_object led
  169. json_select_object $1
  170. json_add_string name $name
  171. json_add_string type switch
  172. json_add_string sysfs $sysfs
  173. json_add_string trigger $trigger
  174. json_add_string port_mask $port_mask
  175. json_select ..
  176. json_select ..
  177. }
  178. ucidef_set_led_default() {
  179. local cfg="led_$1"
  180. local name=$2
  181. local sysfs=$3
  182. local default=$4
  183. json_select_object led
  184. json_select_object $1
  185. json_add_string name $name
  186. json_add_string sysfs $sysfs
  187. json_add_string default $default
  188. json_select ..
  189. json_select ..
  190. }
  191. ucidef_set_led_rssi() {
  192. local cfg="led_$1"
  193. local name=$2
  194. local sysfs=$3
  195. local iface=$4
  196. local minq=$5
  197. local maxq=$6
  198. local offset=$7
  199. local factor=$8
  200. json_select_object led
  201. json_select_object rssi
  202. json_select_object $1
  203. json_add_string name $name
  204. json_add_string sysfs $sysfs
  205. json_add_string minq $minq
  206. json_add_string maxq $maxq
  207. json_add_string offset $offset
  208. json_add_string factor $factor
  209. json_select ..
  210. json_select ..
  211. json_select ..
  212. }
  213. ucidef_set_rssimon() {
  214. local dev="$1"
  215. local refresh="$2"
  216. local threshold="$3"
  217. json_select_object led
  218. json_select_object rssi
  219. json_add_string type rssi
  220. json_add_string dev $dev
  221. json_add_string threshold $threshold
  222. json_select ..
  223. json_select ..
  224. }
  225. board_config_update() {
  226. json_init
  227. [ -f ${CFG} ] && json_load "$(cat ${CFG})"
  228. }
  229. board_config_flush() {
  230. json_dump -i > /tmp/.board.json
  231. mv /tmp/.board.json ${CFG}
  232. }