c8d0587c5ff32535b58c28e4761ea782257ace2a
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / helpers / setup_wifi.sh
1 #!/bin/sh
2 # sets up a wifi interface for meshing
3 # Arguments: $1 = network interface
4
5 net="$1"
6 . /lib/functions.sh
7 . $dir/functions.sh
8
9 ##### wifi-device #####
10
11 # Get the type before we delete the wifi-device
12 config_load wireless
13 config_get type $net type
14
15 # Rename wifi-device for $net
16
17 handle_wifidevice() {
18         if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
19                 section_rename wireless $1 $net
20         fi
21 }
22 config_foreach handle_wifidevice wifi-device
23
24 # create new wifi-device for $net
25 uci set wireless.${net}=wifi-device
26
27 # get and set wifi-device defaults
28 set_defaults "wifi_device_" wireless.${net}
29
30 channel="$(uci -q get meshwizard.netconfig.$net\_channel)"
31 vap="$(uci -q get meshwizard.netconfig.$net\_vap)"
32
33 if [ -z "$channel" -o "$channel" == "default" ]; then
34         channel=$wifi_device_channel
35 fi
36
37 uci batch << EOF
38         set wireless.${net}.type="$type"
39         set wireless.${net}.channel="$channel"
40 EOF
41
42 uci_commitverbose "Setup wifi device for $netrenamed" wireless
43
44 ##### wifi iface
45
46 # Rename wifi-iface for $net
47 handle_interface() {
48         config_get device "$1" device
49         if [ "$device" == "$net" ]; then
50                 if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
51                         section_rename wireless $1 ${net}_iface
52                 fi
53         fi
54
55 config_foreach handle_interface wifi-iface
56
57 # create new wifi-device for $net
58 uci set wireless.$net\_iface=wifi-iface
59
60 # create new wifi-iface for $net from defaults
61 set_defaults "wifi_iface_" wireless.$net\_iface
62
63 # overwrite defaults
64 bssid="$($dir/helpers/gen_bssid.sh $channel $community)"
65
66 ssid="$profile_ssid"
67 if [ "$profile_ssid_scheme" == "addchannel" ]; then
68         ssid="$ssid - ch$channel"
69 fi
70
71 uci batch << EOF
72         set wireless.$net\_iface.device="${net}"
73         set wireless.$net\_iface.network="$netrenamed"
74         set wireless.$net\_iface.ssid="$ssid"
75         set wireless.$net\_iface.bssid="$bssid"
76 EOF
77
78 uci_commitverbose "Setup wifi interface for $netrenamed" wireless
79
80 ## VAP
81 ip4addr="$(uci get meshwizard.netconfig.$net\_ip4addr)"
82
83 # check if this hardware supports VAPs
84 # the interface needs to be up before the check can happen
85
86 /sbin/wifi
87
88 supports_vap="0"
89 $dir/helpers/supports_vap.sh $net $type && supports_vap=1
90
91 if [ "$supports_vap" == "1" -a "$vap" == 1 ]; then
92         uci batch <<- EOF
93                 set wireless.$net\_iface_dhcp="wifi-iface"
94                 set wireless.$net\_iface_dhcp.device="$net"
95                 set wireless.$net\_iface_dhcp.mode="ap"
96                 set wireless.$net\_iface_dhcp.encryption="none"
97                 set wireless.$net\_iface_dhcp.network="${netrenamed}dhcp"
98                 set wireless.$net\_iface_dhcp.ssid="Freifunk-$ip4addr"
99         EOF
100         uci_commitverbose "Setup VAP interface for $netrenamed" wireless
101 fi
102