#!/bin/sh

#############################################
# Copyright (C) 2006 OpenWrt.org
#Autor: Jimmy
#Date: 2020-11-06
#Version: v1.1
# Add the wifi key correct judge
#############################################

wpa_file=/tmp/wpa_supplicant.log

watch_log() {
    not_found_num=0
    while read line
    do
        case "$line" in
            *'CTRL-EVENT-CONNECTED'*)
                echo 1             #connected
                return
                ;;
            *'4-Way Handshake failed'*)
                echo 2             #incorrect key
                return
                ;;
            *'Could not connect to wpa_supplicant'*)
                echo 3             #Could not connect to wpa_supplicant
                return
                ;;
            *'Failed to initiate AP scan'*)
                echo 4             #failed to init ap scan
                return
                ;;
            *'No suitable network found'*)
                not_found_num=$((not_found_num+1))
                if [ $not_found_num -ge 3 ]; then
                    echo 5
                    return
                fi
                ;;
        esac
    done < $wpa_file
}

watch() {
    ##1. Make sure that wpa_supplicant has write info to the wpa file
    while true
    do
        if [ -e $wpa_file -a -s $wpa_file ]; then
            break;
        else
            #printf "\n"
            sleep 1
        fi
    done

    ##2. wpa_supplicant write info item by item
    num=0
    while [ $num -lt 15 ]
    do
        result=$(watch_log)
        if [[ "$result" != "" ]]; then
            if [ "$result" != "0" ]; then
	        echo $result
                return
            fi
        fi
        num=$(($num+1))
        sleep 1
    done
    echo $num
}

start_sta() {
    ifconfig wlan0 down

    echo 0 > /sys/module/bcmdhd/parameters/op_mode
    echo /lib/firmware/fw_bcm43438a0.bin > /sys/module/bcmdhd/parameters/firmware_path
    ifconfig wlan0 up
    if [[ -e "/var/run/wpa_supplicant/wlan0" ]]; then
         rm "/var/run/wpa_supplicant/wlan0"
    fi

    rm -rf $wpa_file

    wpa_supplicant -i wlan0 -c /etc/wifi/wpa_supplicant.conf -d -t 2>&1 > $wpa_file &
    udhcpc -i wlan0 2>&1 > /dev/null &

    #result=$(watch)
    #echo $result 2>&1
}

stop_sta() {
    killall -q wpa_supplicant
    killall -q hostapd
    killall -q udhcpc
    killall -q dnsmasq
}

restart_sta() {
    stop_sta
    sleep 1s
    start_sta
}


start_ap() {
        if [[ -z "$1" ]]; then
                mac_addr=$(ifconfig wlan0|awk '{print $5}'|cut -c 13-|tr -d ' :\n')
                ap_name="SmartLife-$mac_addr"
                softap_up $ap_name 2>&1 > /dev/null
        else
                softap_up $1 2>&1 > /dev/null
        fi
}

stop_ap() {
        softap_down 2>&1 > /dev/null
}

restart_ap() {
        stop_ap 2>&1 > /dev/null
        sleep 1s
        start_ap $1 2>&1 > /dev/null
}

case $1 in
        start_sta)
                restart_sta
                ;;
        stop_sta)
                stop_sta
                ;;
        restart_sta)
                restart_sta
                ;;

        start_ap)
                restart_ap $2
                ;;
        stop_ap)
                stop_ap
                ;;
        restart_ap)
                restart_ap $2
                ;;
		watch)
				watch
				;;
        *)
                echo "Input option: $1 was error"
                ;;
esac
