summaryrefslogtreecommitdiff
path: root/recipes-bsp/wifiup/wifiup/wifi.init
blob: 064a63334f6de23bce9b2880fdc86a2ffe511d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/sh

LOCK=/var/lock/wifi_lock
WIFI_AP_CONFIG_FILE="/var/run/hostapd.conf"
WIFI_CLIENT_CONFIG_FILE="/var/run/wpa_supplicant.conf"
WIFI_LOG_FILE="/var/log/wpa_supplicant.log"
UDHCPC_WIFIUP_SCRIPT="/etc/udhcpc.d/wifiup"
FACTORY_NVS_PATH="/lib/firmware/ti-connectivity/wl1271-nvs.bin.factory"
NVS_PATH="/var/oem/mts/wl1271-nvs.bin"

if mkdir $LOCK; then
  trap "rm -fr $LOCK" EXIT
else
  echo "Wifi lock not acquired, resource in use" >&2
  exit 1
fi

cnt=$(ps -A | grep 'wifi$' | wc -l)

function init() {
  echo "initializing wifi chip"

  # install factory nvs file if necessary
  if [[ ! -f $NVS_PATH ]]; then
    echo "$NVS_PATH not found, using factory default"
    mount -o remount,rw /var/oem
    mkdir -p /var/oem/mts
    cp $FACTORY_NVS_PATH $NVS_PATH
    mount -o remount,ro /var/oem
  fi

  # enable wifi chip
  mts-io-sysfs store wlan-enabled 0
  usleep 50000
  mts-io-sysfs store wlan-enabled 1
  sleep 1
  # load atmel_mci module
  lsmod | grep -q atmel_mci || {
    modprobe atmel_mci && sleep 3
  }

  # led
  ## INFO: code from scripts/led_ni_setup
  echo "netdev" > /sys/class/leds/led-b/trigger
  echo "wlan0" > /sys/class/leds/led-b/device_name
  echo "link tx rx" > /sys/class/leds/led-b/mode
}

function start() {
  JSON=$(curl -m 5 -s 127.0.0.1/api?fields=wifi,lan,system/macAddress,system/mode)

  : ${WIFI_AP_ENABLED:='false'}
  : ${WIFI_AP_MAC:='0F:0A:0C:0E:05:01'}
  : ${WIFI_CLIENT_ENABLED:='true'}
  : ${WIFI_CLIENT_MODE:='WAN'}
  : ${SYSTEM_MODE:='STATION'}

  if [ "$SYSTEM_MODE" != "ROUTER" ]; then
    echo "Can NOT configure WiFi Access Point or Client. System Mode: $SYSTEM_MODE"
    exit 1
  fi

  if [ "$WIFI_AP_ENABLED"  == "true" ] && [ "$WIFI_CLIENT_ENABLED"  == "true" ]; then
    echo "Can NOT enable both Wifi Access Point and Client at the same time"
    exit 1
  fi

  if [ "$WIFI_AP_ENABLED"  == "false" ] && [ "$WIFI_CLIENT_ENABLED"  == "false" ]; then
    echo "Wifi is disabled.  Powering Down Wifi Chip"
    stop
    exit
  fi

  init

  # Check to see if Wifi AP is enabled
  # And that Wifi Client is not enabled 
  if [ "$WIFI_AP_ENABLED" == "true" ] && [ "$WIFI_CLIENT_ENABLED" == "false" ]; then
    echo "WiFi AP enabled - Bringing up hostapd on wlan0"
    # initialize hostapd config 
    wifi_ap_init
    if [ -f $WIFI_AP_CONFIG_FILE ]; then
      hostapd -B $WIFI_AP_CONFIG_FILE -P /var/run/hostapd_wlan0.pid
      sleep 5
      ip link set br0 address $WIFI_AP_MAC
    else
      echo "Error generating $WIFI_AP_CONFIG_FILE"
      exit 1
    fi
  else
    echo "WiFi AP is disabled"
  fi

  # Check to see if Wifi Client is enabled
  # And that Wifi AP is not enabled
  if [ "$WIFI_CLIENT_ENABLED" == "true" ] && [ "$WIFI_AP_ENABLED"  == "false" ]; then
    echo "WiFi Client enabled - Configuring wlan0"

    # initialize wpasupplicant config
    wifi_client_init

    ifconfig wlan0 up

    # Check to see if ap is designated
    if [ -f $WIFI_CLIENT_CONFIG_FILE ]; then
      echo "WiFi Client - Configuring wpa supplicant"
      # syslog option is '-s', file option is '-f <path>'
      wpa_supplicant -B -iwlan0 -Dnl80211 -c $WIFI_CLIENT_CONFIG_FILE -P /var/run/wpa_supplicant_wlan0.pid $ -s &

      sleep 10

      if [ "$WIFI_CLIENT_MODE" == "WAN" ]; then
        echo "WiFi Client - Starting DHCP client"
        udhcpc -i wlan0 -s $UDHCPC_WIFIUP_SCRIPT -p /var/run/udhcpc_wlan0.pid > /dev/null 2>&1 &
      elif [ "$WIFI_CLIENT_MODE" == "LAN" ]; then
        echo "WiFi Client - Adding to bridge"
        brctl addif br0 wlan0
      fi
      sleep 2
    fi
  else
    echo "WiFi client is disabled"
  fi
}

function stop() {
  echo -n "Shutting down wifi "

  if [ -f /var/run/wpa_supplicant_wlan0.pid ]; then
    PID=$(cat /var/run/wpa_supplicant_wlan0.pid)
    kill $PID > /dev/null 2>&1
  fi
  if [ -f /var/run/udhcpc_wlan0.pid ]; then
    PID=$(cat /var/run/udhcpc_wlan0.pid)
    kill $PID > /dev/null 2>&1
  fi
  if [ -f /var/run/hostapd_wlan0.pid ]; then
    PID=$(cat /var/run/hostapd_wlan0.pid)
    kill $PID > /dev/null 2>&1
  fi

  modprobe -r wl12xx > /dev/null 2>&1
  modprobe -r wlcore > /dev/null 2>&1
  modprobe -r wlcore_sdio > /dev/null 2>&1
  modprobe -r atmel_mci > /dev/null 2>&1
}

function scan() {
  init

  # Ensure WLAN is up
  ifconfig wlan0 up
  # Scan
  iw wlan0 scan
}

case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "restart")
    ## Stop the service and regardless of whether it was
    ## running or not, start it again.
    echo "Restarting wifi"
    stop
    start
    ;;
  "scan")
    scan
    ;;
  *)
    ## If no parameters are given, print which are avaiable.
    echo "Usage: $0 {start|stop|scan|restart}"
    exit 1
    ;;
esac