[package] base-files: add ucidef_add_switch_port function
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / functions / uci-defaults.sh
1 #!/bin/sh
2 # Copyright (C) 2011 OpenWrt.org
3
4 UCIDEF_LEDS_CHANGED=0
5
6 ucidef_set_led_netdev() {
7 local cfg="led_$1"
8 local name=$2
9 local sysfs=$3
10 local dev=$4
11
12 uci -q get system.$cfg && return 0
13
14 uci batch <<EOF
15 set system.$cfg='led'
16 set system.$cfg.name='$name'
17 set system.$cfg.sysfs='$sysfs'
18 set system.$cfg.trigger='netdev'
19 set system.$cfg.dev='$dev'
20 set system.$cfg.mode='link tx rx'
21 EOF
22 UCIDEF_LEDS_CHANGED=1
23 }
24
25 ucidef_set_led_usbdev() {
26 local cfg="led_$1"
27 local name=$2
28 local sysfs=$3
29 local dev=$4
30
31 uci -q get system.$cfg && return 0
32
33 uci batch <<EOF
34 set system.$cfg='led'
35 set system.$cfg.name='$name'
36 set system.$cfg.sysfs='$sysfs'
37 set system.$cfg.trigger='usbdev'
38 set system.$cfg.dev='$dev'
39 set system.$cfg.interval='50'
40 EOF
41 UCIDEF_LEDS_CHANGED=1
42 }
43
44 ucidef_set_led_wlan() {
45 local cfg="led_$1"
46 local name=$2
47 local sysfs=$3
48 local trigger=$4
49
50 uci -q get system.$cfg && return 0
51
52 uci batch <<EOF
53 set system.$cfg='led'
54 set system.$cfg.name='$name'
55 set system.$cfg.sysfs='$sysfs'
56 set system.$cfg.trigger='$trigger'
57 EOF
58 UCIDEF_LEDS_CHANGED=1
59 }
60
61 ucidef_set_led_switch() {
62 local cfg="led_$1"
63 local name=$2
64 local sysfs=$3
65 local trigger=$4
66 local port_mask=$5
67
68 uci -q get system.$cfg && return 0
69
70 uci batch <<EOF
71 set system.$cfg='led'
72 set system.$cfg.name='$name'
73 set system.$cfg.sysfs='$sysfs'
74 set system.$cfg.trigger='$trigger'
75 set system.$cfg.port_mask='$port_mask'
76 EOF
77 UCIDEF_LEDS_CHANGED=1
78 }
79
80 ucidef_set_led_default() {
81 local cfg="led_$1"
82 local name=$2
83 local sysfs=$3
84 local default=$4
85
86 uci -q get system.$cfg && return 0
87
88 uci batch <<EOF
89 set system.$cfg='led'
90 set system.$cfg.name='$name'
91 set system.$cfg.sysfs='$sysfs'
92 set system.$cfg.default='$default'
93 EOF
94 UCIDEF_LEDS_CHANGED=1
95 }
96
97 ucidef_set_led_rssi() {
98 local cfg="led_$1"
99 local name=$2
100 local sysfs=$3
101 local iface=$4
102 local minq=$5
103 local maxq=$6
104 local offset=$7
105 local factor=$8
106
107 uci -q get system.$cfg && return 0
108
109 uci batch <<EOF
110 set system.$cfg='led'
111 set system.$cfg.name='$name'
112 set system.$cfg.sysfs='$sysfs'
113 set system.$cfg.trigger='rssi'
114 set system.$cfg.iface='rssid_$iface'
115 set system.$cfg.minq='$minq'
116 set system.$cfg.maxq='$maxq'
117 set system.$cfg.offset='$offset'
118 set system.$cfg.factor='$factor'
119 EOF
120 UCIDEF_LEDS_CHANGED=1
121 }
122
123 ucidef_set_rssimon() {
124 local dev="$1"
125 local refresh="$2"
126 local threshold="$3"
127
128 local cfg="rssid_$dev"
129
130 uci -q get system.$cfg && return 0
131
132 uci batch <<EOF
133 set system.$cfg='rssid'
134 set system.$cfg.dev='$dev'
135 set system.$cfg.refresh='$refresh'
136 set system.$cfg.threshold='$threshold'
137 EOF
138 UCIDEF_LEDS_CHANGED=1
139 }
140
141 ucidef_commit_leds()
142 {
143 [ "$UCIDEF_LEDS_CHANGED" == "1" ] && uci commit system
144 }
145
146 ucidef_set_interface_loopback() {
147 uci batch <<EOF
148 set network.loopback='interface'
149 set network.loopback.ifname='lo'
150 set network.loopback.proto='static'
151 set network.loopback.ipaddr='127.0.0.1'
152 set network.loopback.netmask='255.0.0.0'
153 set network.globals='globals'
154 set network.globals.ula_prefix='auto'
155 EOF
156 }
157
158 ucidef_set_interface_raw() {
159 local cfg=$1
160 local ifname=$2
161
162 uci batch <<EOF
163 set network.$cfg='interface'
164 set network.$cfg.ifname='$ifname'
165 set network.$cfg.proto='none'
166 EOF
167 }
168
169 ucidef_set_interface_lan() {
170 local ifname=$1
171
172 uci batch <<EOF
173 set network.lan='interface'
174 set network.lan.ifname='$ifname'
175 set network.lan.type='bridge'
176 set network.lan.proto='static'
177 set network.lan.ipaddr='192.168.1.1'
178 set network.lan.netmask='255.255.255.0'
179 set network.lan.ip6assign='60'
180 EOF
181 }
182
183 ucidef_set_interface_wan() {
184 local ifname=$1
185
186 uci batch <<EOF
187 set network.wan='interface'
188 set network.wan.ifname='$ifname'
189 set network.wan.proto='dhcp'
190 set network.wan6='interface'
191 set network.wan6.ifname='@wan'
192 set network.wan6.proto='dhcpv6'
193 EOF
194 }
195
196 ucidef_set_interfaces_lan_wan() {
197 local lan_ifname=$1
198 local wan_ifname=$2
199
200 ucidef_set_interface_lan "$lan_ifname"
201 ucidef_set_interface_wan "$wan_ifname"
202 }
203
204 ucidef_set_interface_macaddr() {
205 local ifname=$1
206 local mac=$2
207
208 uci batch <<EOF
209 set network.$ifname.macaddr='$mac'
210 EOF
211 }
212
213 ucidef_add_switch() {
214 local name=$1
215 local reset=$2
216 local enable=$3
217 uci batch <<EOF
218 add network switch
219 set network.@switch[-1].name='$name'
220 set network.@switch[-1].reset='$reset'
221 set network.@switch[-1].enable_vlan='$enable'
222 EOF
223 }
224
225 ucidef_add_switch_vlan() {
226 local device=$1
227 local vlan=$2
228 local ports=$3
229 uci batch <<EOF
230 add network switch_vlan
231 set network.@switch_vlan[-1].device='$device'
232 set network.@switch_vlan[-1].vlan='$vlan'
233 set network.@switch_vlan[-1].ports='$ports'
234 EOF
235 }
236
237 ucidef_add_switch_port() {
238 local device=$1
239 local port=$2
240 uci batch <<EOF
241 add network switch_port
242 set network.@switch_port[-1].device='$device'
243 set network.@switch_port[-1].port='$port'
244 EOF
245 }
246