base-files: add a hardware detection layer
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / functions / uci-defaults-new.sh
1 #!/bin/ash
2
3 CFG=/etc/board.json
4
5 . /usr/share/libubox/jshn.sh
6
7 json_select_array() {
8 local _json_no_warning=1
9
10 json_select "$1"
11 [ $? = 0 ] && return
12
13 json_add_array $1
14 json_close_array
15
16 json_select "$1"
17 }
18
19 json_select_object() {
20 local _json_no_warning=1
21
22 json_select "$1"
23 [ $? = 0 ] && return
24
25 json_add_object $1
26 json_close_object
27
28 json_select "$1"
29 }
30
31 _ucidef_set_interface() {
32 local name=$1
33 local iface=$2
34
35 json_select_object $name
36 json_add_string ifname "${iface%%.*}"
37 [ "$iface" == "${iface%%.*}" ] || json_add_boolean create_vlan 1
38 json_select ..
39 }
40
41 ucidef_set_interface_loopback()
42 {
43 # stub
44 local a=$1
45 }
46
47 ucidef_set_interface_lan() {
48 local lan_if=$1
49
50 json_select_object network
51 _ucidef_set_interface lan $lan_if
52 json_select ..
53 }
54
55 ucidef_set_interfaces_lan_wan() {
56 local lan_if=$1
57 local wan_if=$2
58
59 json_select_object network
60 _ucidef_set_interface lan $lan_if
61 _ucidef_set_interface wan $wan_if
62 json_select ..
63 }
64
65 ucidef_add_switch() {
66 local name=$1
67 local reset=$2
68 local enable=$3
69
70 json_select_object switch
71
72 json_select_object $name
73 [ "$enable" -eq 1 ] && json_add_boolean enable 1
74 [ "$reset" -eq 1 ] && json_add_boolean reset 1
75 json_select ..
76
77 json_select ..
78 }
79
80 ucidef_add_switch_attr() {
81 local name=$1
82 local key=$2
83 local val=$3
84
85 json_select_object switch
86
87 json_select_object $name
88 json_add_string $key $val
89 json_select ..
90
91 json_select ..
92 }
93
94 ucidef_add_switch_vlan() {
95 local name=$1
96 local vlan=$2
97 local ports=$3
98 local cpu_port=''
99
100 case $vlan in
101 1) vlan=lan;;
102 2) vlan=wan;;
103 *) vlan=vlan$vlan;;
104 esac
105
106 json_select_object switch
107 json_select_object $name
108 json_select_object vlans
109
110 json_add_array $vlan
111 for p in $ports; do
112 if [ ${p%t} != $p ]; then
113 cpu_port=$p
114 else
115 json_add_int "" $p
116 fi
117 done
118 json_close_array
119
120 json_select ..
121 [ -n "$cpu_port" ] && json_add_int cpu_port $cpu_port
122 json_select ..
123 json_select ..
124 }
125
126 ucidef_set_interface_macaddr() {
127 local network=$1
128 local macaddr=$2
129
130 json_select_object network
131
132 json_select $network
133 [ $? -eq 0 ] || {
134 json_select ..
135 return
136 }
137
138 json_add_string macaddr $macaddr
139 json_select ..
140
141 json_select ..
142 }
143
144 ucidef_set_led_netdev() {
145 local cfg="led_$1"
146 local name=$2
147 local sysfs=$3
148 local dev=$4
149
150 json_select_object led
151
152 json_select_object $1
153 json_add_string name $name
154 json_add_string type netdev
155 json_add_string sysfs $sysfs
156 json_add_string device $dev
157 json_select ..
158
159 json_select ..
160 }
161
162 ucidef_set_led_interface() {
163 local name=$1
164 local sysfs=$2
165
166 json_select_object led
167
168 json_select_object $1
169 json_add_string name $name
170 json_add_string type interface
171 json_add_string sysfs $sysfs
172 json_add_string interface $name
173 json_select ..
174
175 json_select ..
176 }
177
178 ucidef_set_led_usbdev() {
179 local cfg="led_$1"
180 local name=$2
181 local sysfs=$3
182 local dev=$4
183
184 json_select_object led
185
186 json_select_object $1
187 json_add_string name $name
188 json_add_string type usb
189 json_add_string sysfs $sysfs
190 json_add_string device $dev
191 json_select ..
192
193 json_select ..
194 }
195
196 ucidef_set_led_wlan() {
197 local cfg="led_$1"
198 local name=$2
199 local sysfs=$3
200 local trigger=$4
201
202 json_select_object led
203
204 json_select_object $1
205 json_add_string name $name
206 json_add_string type trigger
207 json_add_string sysfs $sysfs
208 json_add_string trigger $trigger
209 json_select ..
210
211 json_select ..
212 }
213
214 ucidef_set_led_switch() {
215 local cfg="led_$1"
216 local name=$2
217 local sysfs=$3
218 local trigger=$4
219 local port_mask=$5
220
221 json_select_object led
222
223 json_select_object $1
224 json_add_string name $name
225 json_add_string type switch
226 json_add_string sysfs $sysfs
227 json_add_string trigger $trigger
228 json_add_string port_mask $port_mask
229 json_select ..
230
231 json_select ..
232 }
233
234 ucidef_set_led_default() {
235 local cfg="led_$1"
236 local name=$2
237 local sysfs=$3
238 local default=$4
239
240 json_select_object led
241
242 json_select_object $1
243 json_add_string name $name
244 json_add_string sysfs $sysfs
245 json_add_string default $default
246 json_select ..
247
248 json_select ..
249 }
250
251 ucidef_set_led_rssi() {
252 local cfg="led_$1"
253 local name=$2
254 local sysfs=$3
255 local iface=$4
256 local minq=$5
257 local maxq=$6
258 local offset=$7
259 local factor=$8
260
261 json_select_object led
262
263 json_select_object rssi
264 json_select_object $1
265 json_add_string name $name
266 json_add_string sysfs $sysfs
267 json_add_string minq $minq
268 json_add_string maxq $maxq
269 json_add_string offset $offset
270 json_add_string factor $factor
271 json_select ..
272 json_select ..
273
274 json_select ..
275 }
276
277 ucidef_set_rssimon() {
278 local dev="$1"
279 local refresh="$2"
280 local threshold="$3"
281
282 json_select_object led
283
284 json_select_object rssi
285 json_add_string type rssi
286 json_add_string dev $dev
287 json_add_string threshold $threshold
288 json_select ..
289
290 json_select ..
291
292 }
293
294 board_config_update() {
295 json_init
296 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
297 }
298
299 board_config_flush() {
300 json_dump -i > /tmp/.board.json
301 mv /tmp/.board.json ${CFG}
302 }