#!/bin/sh
#
# Script to see if an IP adress is already used or not
#
# Getting SSID from config.ini file.

#initializing random generator
cat /data/random_mac.txt > /dev/urandom
/bin/random_mac > /data/random_mac.txt

#echo 2 > /proc/cpu/alignment

export NETIF=ath0
export WORKAREA="/lib/firmware"
export ATH_PLATFORM="parrot-omap-sdio"
export ATH_MODULE_ARGS="ifname=$NETIF"

# Switch Wifi mode depending on value into /data/config.ini
#

WIFI_MODE=`grep wifi_mode /data/config.ini | awk -F "=" '{ gsub(/ */,"",$2); print $2}'` 

case $WIFI_MODE in
0)
    WIFI_MODE=master
    ;;
1)
    WIFI_MODE=ad-hoc
    ;;
2)
    WIFI_MODE=managed
    ;;
*)
    WIFI_MODE=master
    ;;
esac


if [ -s /factory/mac_address.txt ]
then
MAC_ADDR=`cat /factory/mac_address.txt`
else
MAC_ADDR=`cat /data/random_mac.txt`
fi

loadAR6000.sh -i $NETIF --setmac $MAC_ADDR

# Waiting 2s for the wifi chip to be ready
sleep 2

AR6K_PID=`ps_procps -A -T -c | grep AR6K | awk '{print $1}'`
SDIO_PID=`ps_procps -A -T -c | grep ksdioirqd | awk '{print $1}'`

#Changing wifi priority
chrt -p -r 25 $SDIO_PID
chrt -p -r 24 $AR6K_PID


# Disabling powersaving
wmiconfig -i $NETIF --power maxperf
# Disabling 802.11n aggregation
wmiconfig -i $NETIF --allow_aggr 0 0
# enabling WMM
wmiconfig -i $NETIF --setwmm 1

i=0

while [ ! -n "$SSID" ] && [ $i -lt 10 ]
do
i=`expr $i + 1`
SSID=`grep ssid_single_player /data/config.ini | awk -F "=" '{print $2}'`

# Removing leading and trailing spaces
SSID=`echo $SSID`
sleep 1
done

if [ -n "$SSID" ]
then
echo "SSID=$SSID"
else
#default SSID.
SSID=ardrone2_wifi
echo "SSID=\"$SSID\""
fi

RANDOM_CHAN=auto


echo "Creating $WIFI_MODE Network $SSID"

iwconfig $NETIF mode $WIFI_MODE
iwconfig $NETIF essid "$SSID"

if [ "$WIFI_MODE" != "managed" ]
then
# Allowing ACS to select only channels 1 & 6
wmiconfig -i $NETIF --acsdisablehichannels 1
iwconfig $NETIF channel $RANDOM_CHAN
iwconfig $NETIF rate auto
iwconfig $NETIF commit
fi

wmiconfig -i $NETIF --dtim 1
wmiconfig -i $NETIF --commit

OK=0
BASE_ADRESS=192.168.1.
PROBE=200

while [ $OK -eq 0 ]
do
#configuring interface.
ifconfig $NETIF $BASE_ADRESS$PROBE
arping -I $NETIF -q -f -D -w 2 $BASE_ADRESS$PROBE

if [ $? -eq 1 ]
then
	if [ -s /data/old_adress.txt ]
	then
		# Testing previously given adress.
		PROBE=`cat /data/old_adress.txt`
	else
		#generating random odd IP address
		PROBE=`/bin/random_ip`
	fi
	/bin/random_ip > /data/old_adress.txt
else
	echo $PROBE > /data/old_adress.txt
	OK=1
fi

done

#Configuring DHCP server.
echo "Using address $BASE_ADRESS$PROBE"
echo "start $BASE_ADRESS`expr $PROBE + 1`" > /tmp/udhcpd.conf
echo "end $BASE_ADRESS`expr $PROBE + 4`" >> /tmp/udhcpd.conf
echo "interface $NETIF" >> /tmp/udhcpd.conf
echo "decline_time 1" >> /tmp/udhcpd.conf
echo "conflict_time 1" >> /tmp/udhcpd.conf
echo "opt router  $BASE_ADRESS$PROBE" >> /tmp/udhcpd.conf
echo "opt subnet 255.255.255.0" >> /tmp/udhcpd.conf
echo "opt lease  1200" >> /tmp/udhcpd.conf

/bin/pairing_setup.sh

# Saving random info for initialization at next reboot
echo $MAC_ADDR `date` `/bin/random_mac` > /dev/urandom
/bin/random_mac > /data/random_mac.txt



telnetd -l /bin/sh
udhcpd /tmp/udhcpd.conf

# Adding route for multicast-packet
route add -net 224.0.0.0 netmask 240.0.0.0 dev $NETIF

# Starting the daemon which responds to the AUTH messages from FreeFlight
/bin/parrotauthdaemon

