Statistics
| Branch: | Revision:

immonitor / src / config / etc / init.d / S40network @ 42eb16a4

History | View | Annotate | Download (4.87 KB)

1
#!/bin/sh
2
#
3
# Start/stop the network....
4
#
5
IMCFG=/etc/ironman.cfg
6
WWWNETCFG=/etc/imnetconfig
7
WPACONF=/etc/wpa_supplicant.conf
8

    
9
NETWORK_DIR=/etc/network
10
INTERFACES=$NETWORK_DIR/interfaces
11
INTERFACES_BACKUP=$NETWORK_DIR/interfaces.bu
12
INTERFACES_APONLY=$NETWORK_DIR/interfaces.aponly
13
HOSTAPD=/etc/hostapd.conf
14
HOSTAPD_APONLY=$NETWORK_DIR/hostapd.aponly
15
DHCP=/etc/dhcpd.conf
16
DHCP_APONLY=/etc/network/dhcpd.aponly
17
IMGPIO=/usr/bin/imgpio
18

    
19
# Disable network configuration utility in web services
20
disableNetworkConfig()
21
{
22
    rm -f $WWWNETCFG
23
}
24

    
25
# Enable network configuration utility in web services
26
enableNetworkConfig()
27
{
28
    touch $WWWNETCFG
29
}
30

    
31
# Shutdown wpa_supplicant
32
killSup()
33
{ 
34
    doWlan=`ps | grep wpa_supplicant | grep -v grep`
35
    if [ "$doWlan" != "" ]
36
    then
37
        killall wpa_supplicant                                             
38
    fi
39
}
40

    
41
# Shutdown hostapd
42
killAP()
43
{
44
    doWlan=`ps | grep hostapd | grep -v grep`
45
    if [ "$doWlan" != "" ]
46
    then
47
        killall hostapd                                             
48
    fi
49
    doWlan=`ps | grep dhcpd | grep -v grep`
50
    if [ "$doWlan" != "" ]
51
    then
52
        killall dhcpd                                             
53
    fi
54
    rm -rf /var/lib/dhcp
55
}
56

    
57
# Setup or teardown wireless or wired client.
58
# We assume that /etc/wpa_supplicant.conf has been properly configured.
59
client()
60
{
61
    case "$1" in
62
        start) 
63
            wpa_supplicant -B -c $WPACONF -Dnl80211 -iwlan0 &
64
            sleep 2
65
            ;;
66
    
67
        stop)
68
            killSup
69
            ;;
70
    esac
71
}
72

    
73
# Setup or teardown wireless access point
74
# We assume that /etc/hostapd.conf has been properly configured.
75
wap()
76
{
77
    case "$1" in
78
        start)
79
            # Get channel that wifi is on and update the hostapd configuration.
80
            # We have to share the channel.
81
            # ### TBD
82

    
83
            # Create uap0 as a virtual interface on wlan0
84
            # We don't check if this is supported by the hardware.
85
            # For IronMan we assume it is.
86
            iw dev wlan0 interface add uap0 type __ap
87

    
88
            mkdir -p /var/lib/dhcp
89
            touch /var/lib/dhcp/dhcpd.leases
90
            hostapd -B $HOSTAPD uap0
91
            dhcpd uap0
92
            ;;
93
    
94
        stop)
95
            killAP
96
            ;;
97
    esac
98
}
99

    
100
# Start a default AP.  We assume that the
101
# network device is available.
102
startDefaultAP()
103
{
104
    if [ ! -f $INTERFACES_BACKUP ]
105
    then
106
        cp $INTERFACES $INTERFACES_BACKUP
107
    fi
108
    cp $INTERFACES_APONLY $INTERFACES
109

    
110
    mkdir -p /var/lib/dhcp
111
    touch /var/lib/dhcp/dhcpd.leases
112
    /sbin/ifup -a
113
    hostapd -B $HOSTAPD_APONLY
114
    dhcpd -cf $DHCP_APONLY wlan0
115
}
116

    
117
# Now change the run state of the network.
118
case "$1" in
119
    start) 
120
        echo "Starting network..."
121
        /sbin/ifdown -a
122

    
123
        # Source the IronMan configuration 
124
        source $IMCFG
125

    
126
        # Check if we're in PAIR mode
127
        # This reads the GPIO pin attached to the PAIR button.
128
        PAIR_MODE=`$IMGPIO -p $GPIO_PAIR_BUTTON -r`
129

    
130
        # The pair button drives the pin low if it's pressed.
131
        if [ $PAIR_MODE -eq 0 ]
132
        then
133
            # We're in PAIR mode.
134
            # The user is trying to configure the AP and 
135
            # internet connection via a mobile device.
136

    
137
            # Light up the PAIR MODE LED.
138
            $IMGPIO -p $GPIO_PAIR_LED -w 1
139

    
140
            # Enable network configuration in web services
141
            enableNetworkConfig
142

    
143
            # Start AP in default configuration.
144
            startDefaultAP
145

    
146
            # Resetting from this will happen when the web
147
            # server reboots the device.
148
        else
149
            # Not in PAIR mode.
150

    
151
            # Restore old interfaces file, if any.
152
            if [ -f $INTERFACES_BACKUP ]
153
            then
154
                cp $INTERFACES_BACKUP $INTERFACES
155
                rm -f $INTERFACES_BACKUP
156
            fi
157

    
158
            # Disable the PAIR MODE LED.
159
            $IMGPIO -p $GPIO_PAIR_LED -w 0
160
            $IMGPIO -d $GPIO_PAIR_LED
161

    
162
            # Disable network configuration in web services
163
            disableNetworkConfig
164

    
165
            # Start internet connection via WiFi
166
            client $1
167

    
168
            # Bring up the interfaces.  Must be done before we
169
            # bringing up the AP.
170
            /sbin/ifup -a
171

    
172
            # Start AP for IoT devices
173
            wap $1
174

    
175
            # Use these to push the wlan throughput.
176
            # The txpower update can only be done if you change the region.
177
            # /sbin/iwconfig wlan0 down
178
            # /usr/sbin/iw reg set BO
179
            # /sbin/iwconfig wlan0 up
180
            # /sbin/iwconfig wlan0 txpower 30
181
            /sbin/ifconfig wlan0 txqueuelen 10000
182
        fi
183
        ;;
184

    
185
    stop)
186
        echo "Stopping network..."
187
        /sbin/ifdown -a
188
        wap $1
189
        client $1
190
        ;;
191

    
192
    restart|reload)
193
        "$0" stop
194
        "$0" start
195
        ;;
196

    
197
    *)
198
        echo $"Usage: $0 {start|stop|restart}"
199
        exit 1
200
esac
201
exit $?
202