[Feature][T8TSK-202][wifi] wifi6.so add support for with/without connman

Change-Id: I81365bbf2f36eeeeb263a9f8d2d524339f184591
diff --git a/src/lynq/lib/liblynq-wifi6/libwifi6.c b/src/lynq/lib/liblynq-wifi6/libwifi6.c
index 5ee8afc..2ac57a6 100755
--- a/src/lynq/lib/liblynq-wifi6/libwifi6.c
+++ b/src/lynq/lib/liblynq-wifi6/libwifi6.c
@@ -70,6 +70,14 @@
 const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
 const int MAX_IDLE_COUNT = 600; // 60s
 
+const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
+const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
+const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
+const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
+const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
+
+static char s_ap_iterface_name[64] = {0};
+
 struct local_wpa_ctrl{
     struct wpa_ctrl *ctrl;
     pthread_mutex_t mutex;
@@ -150,10 +158,21 @@
     pthread_mutex_unlock(&s_global_check_mutex);
 }
 
+static int system_call_v(const char * fmt, ...)
+{
+    char str_cmd[256] = {0};
+    va_list args;
+    va_start(args, fmt);
+    vsprintf(str_cmd, fmt, args);
+    va_end(args);
+    printf("system call----------%s\n", str_cmd);
+    return system(str_cmd);
+}
+
 static void check_tether_and_notify()
 {
     RLOGD("check_tether_and_notify called");
-    if (0 == system("ifconfig | grep tether"))
+    if (s_ap_iterface_name[0] != '\0' && 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
     {
         return;
     }
@@ -588,7 +607,7 @@
     size_t len = MAX_RET;
     char msg_notify[MAX_RET];
     error_number_s error;
-    lynq_wifi_sta_status_s state;
+    lynq_wifi_sta_status_s state, last_state = -1;
     int idle_count = 0;
 
     struct wpa_ctrl *lynq_wpa_ctrl = NULL;
@@ -623,6 +642,19 @@
             if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
             {
                 inner_check_connect_error(msg_notify, state, error);
+                if (last_state != state)
+                {
+                    if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
+                    {
+                        system_call_v("%s %s", sta_status_change_script, "connect");
+                    }
+                    else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
+                    {
+                        system_call_v("%s %s", sta_status_change_script, "disconnect");
+                    }
+                }
+
+                last_state = state;
             }
         }
     }
@@ -767,15 +799,7 @@
         goto out_enable;
     }
 
-    const char * cmd_check_service =
-            "state=`systemctl is-active wg870_drv_insmod.service`\n"
-            "[ \"\"$state == \"active\" ] && exit 0\n"
-            "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
-//    if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
-//        return 0;
-//    }
-
-    ret = system(cmd_check_service);
+    ret = system(start_wg870_service_script);
     if (ret != 0)
     {
         //printf("service state %d\n", ret);
@@ -784,50 +808,6 @@
         goto out_enable;
     }
 
-    RLOGD("check service state is OK");
-    for (i=0; i<10; i++)
-    {
-        if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
-            break;
-        }
-        usleep(300*1000);
-    }
-
-    if (i >= 10)
-    {
-	RLOGE("[wifi error] check connman technologies no wifi");
-        ret = -1;
-        goto out_enable;
-    }
-
-    //@todo delete add temp check for socket avilable start (20220606)
-    for (i=0; i<60; i++)
-    {
-        if (system("netstat -an | grep -q DGRAM") == 0) {
-            break;
-        }
-        sleep(1);
-    }
-
-    if (i >= 60)
-    {
-        ret  = -1;
-        goto out_enable;
-    }
-    //@todo delete add temp check for socket avilable end (20220606)
-
-    if (0 != system("ifconfig | grep -q ap0"))
-    {
-        system("connmanctl enable wifi");
-        usleep(300*1000);
-        system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
-        system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
-        usleep(300*1000);
-        system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
-        usleep(300*1000);
-        system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
-    }
-
     if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
     {
         ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
@@ -1903,11 +1883,11 @@
 
     CHECK_WPA_CTRL(CTRL_AP);
 
-//    system("connmanctl enable wifi");
-//    system("connmanctl tether wifi on cy-test 12345678");
-//    system("ifconfig wlan0 down");
-//    system("ifconfig wlan0 up");
-//    system("ifconfig wlan0 up");
+    sprintf(LYNQ_WIFI_CMD, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
+    if (0 != exec_cmd(LYNQ_WIFI_CMD, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
+    {
+        return -1;
+    }
 
     //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
     //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
@@ -1917,6 +1897,13 @@
 
     check_tether_and_notify();
 
+    ret = system_call_v("%s %s", start_stop_ap_script, "start");
+    if (ret != 0)
+    {
+        RLOGE("lynq_wifi_ap_start excute script fail");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -1938,7 +1925,14 @@
     DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
 
 //    system("connmanctl tether wifi off");
-	
+
+    ret = system_call_v("%s %s", start_stop_ap_script, "stop");
+    if (ret != 0)
+    {
+        RLOGE("lynq_wifi_ap_start excute script fail");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -2834,17 +2828,13 @@
     CHECK_IDX(idx, CTRL_STA);
     CHECK_WPA_CTRL(CTRL_STA);
 
-    system("connmanctl enable wifi");
-
-    if (system("ifconfig | grep -q wlan0") != 0)
+    ret = system_call_v("%s %s", start_stop_sta_script, "start");
+    if (ret != 0)
     {
+        RLOGE("lynq_wifi_ap_start excute script fail");
         return -1;
     }
 
-//    DO_OK_FAIL_REQUEST(cmd_remove_all);
-//    system(lynq_first_sta_cmd);
-//    system(lynq_reconfigure_cmd);
-//    DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
     system(lynq_enable_sta_cmd);
     system(lynq_reconnect_cmd);
 //    DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
@@ -2861,19 +2851,16 @@
     CHECK_IDX(idx, CTRL_STA);
     CHECK_WPA_CTRL(CTRL_STA);
 
-//    curr_state.ap = &ap_info;
-//    curr_state.state = NULL;
-
-//    if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
-//        return 0;
-//    }
-
-//    sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
-//    DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
     system(lynq_disable_sta_cmd);
     DO_OK_FAIL_REQUEST(cmd_save_config);
-//tmp add cmd fix gsw wlan0 didn't free ip after stop sta
-    system("ifconfig wlan0 0.0.0.0");
+
+    ret = system_call_v("%s %s", start_stop_sta_script, "stop");
+    if (ret != 0)
+    {
+        RLOGE("lynq_wifi_ap_start excute script fail");
+        return -1;
+    }
+
     return 0;
 //    return system("connmanctl disable wifi");
 }
@@ -3424,7 +3411,7 @@
 
     if (idx == 1)
     {
-       ifaName = "tether";
+       ifaName = s_ap_iterface_name;
     }
     else if (idx != 0)
     {
@@ -3695,7 +3682,8 @@
     int mask_len;
     char *p;
     char tmp[64] = {0};
-    if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
+    sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
+    if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
         return -1;
     p = strstr(str_cmd_ret, "Mask:");
     if (p == NULL)
@@ -3732,6 +3720,12 @@
     nmax = 6;
     ncheckcount = nidlecount = 0;
 
+    if (s_ap_iterface_name[0] == '\0')
+    {
+        RLOGE("------gbw thread run\n");
+        return;
+    }
+
     RLOGD("------gbw thread run\n");
     sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
     while (dest_ip[0] == '\0') {
@@ -3750,28 +3744,27 @@
         }
     }
 
-    system("tc qdisc del dev tether root > /dev/null 2>&1");
-    system("tc qdisc add dev tether root handle 1: htb r2q 1");
-    system("tc class add dev tether parent 1:  classid 1:1  htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
+    system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
+    system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
+    system_call_v("tc class add dev %s parent 1:  classid 1:1  htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000", s_ap_iterface_name);
     if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
     {
         RLOGD("not get tether info\n");
         return;
     }
-    sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret);
-    system(str_cmd);
-    system("tc class add dev tether parent 1:  classid 1:2  htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
-    sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
-    //printf("----cmd:%s\n", str_cmd);
-    system(str_cmd);
+    system_call_v("tc filter add dev %s parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", s_ap_iterface_name, str_cmd_ret);
+    system_call_v("tc class add dev %s parent 1:  classid 1:2  htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000", s_ap_iterface_name);
+    system_call_v("tc filter add dev %s parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", s_ap_iterface_name, dest_ip);
 
     while (1) {
         sleep(1);
         memset(str_cmd, 0, sizeof(str_cmd));
-        if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
+        memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
+        sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
+        if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
             continue;
         //printf("ap1 --- %s\n", str_cmd);
-        n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
+        n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
         if (n > 9) {
             if (strcmp(results[1], "Sent") == 0) {
                 currAP1Bytes = atoll(results[2]);
@@ -3782,10 +3775,12 @@
         }
 
         memset(str_cmd, 0, sizeof(str_cmd));
-        if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
+        memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
+        sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
+        if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
             continue;
         //printf("ap2 --- %s\n", str_cmd);
-        n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
+        n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
         if (n > 9) {
             if (strcmp(results[1], "Sent") == 0) {
                 currAP2Bytes = atoll(results[2]);
@@ -3833,9 +3828,8 @@
 
         if (currSetAP1Speed != setAP1Speed) {
             setAP1Speed = currSetAP1Speed;
-            sprintf(str_cmd, "tc class replace dev tether parent 1:  classid 1:1  htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4));
-            //printf("------***change speed: %s\n", str_cmd);
-            system(str_cmd);
+            system_call_v(str_cmd, "tc class replace dev %s parent 1:  classid 1:1  htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
+                         s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
         }
     }
 }
@@ -3894,9 +3888,15 @@
 
 static int stopGBW() {
     void* retval;
+    char cmd[64] = {0};
     pthread_cancel(g_gbw_watcher_pid);
     pthread_join(g_gbw_watcher_pid, &retval);
     g_gbw_watcher_pid = 0;
-    system("tc qdisc del dev tether root");
+    sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
+    if (s_ap_iterface_name[0] != '\0')
+    {
+        sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
+        system(cmd);
+    }
 }
 //you.chen add for tv-box end
diff --git a/src/lynq/lib/liblynq-wifi6/scripts/get_interface_name.sh b/src/lynq/lib/liblynq-wifi6/scripts/get_interface_name.sh
new file mode 100755
index 0000000..4dc8717
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts/get_interface_name.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$1" == "0" ]; then
+	echo "wlan0"
+elif [ "$1" == "1" ]; then
+	echo "ap0"
+else
+	exit 1
+fi
+
diff --git a/src/lynq/lib/liblynq-wifi6/scripts/sta_status_change.sh b/src/lynq/lib/liblynq-wifi6/scripts/sta_status_change.sh
new file mode 100755
index 0000000..829303e
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts/sta_status_change.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+
diff --git a/src/lynq/lib/liblynq-wifi6/scripts/start_stop_ap.sh b/src/lynq/lib/liblynq-wifi6/scripts/start_stop_ap.sh
new file mode 100755
index 0000000..8fe91f7
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts/start_stop_ap.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+if [ "$1" == "start" ]; then
+	ifconfig ap0 192.168.15.1 netmask 255.255.255.240
+
+	if [ $? != 0 ]; then
+		exit 1
+	fi
+	mkdir -p /run/wg870/dnsmasq.d
+	echo "interface=ap0" > /run/wg870/dnsmasq.conf
+	echo "listen-address=127.0.0.1,192.168.15.1" >> /run/wg870/dnsmasq.conf
+	echo "bind-interfaces"  >> /run/wg870/dnsmasq.conf
+	echo "localise-queries" >> /run/wg870/dnsmasq.conf
+	echo "dhcp-range=192.168.15.2,192.168.15.14,255.255.255.240,1h"  >> /run/wg870/dnsmasq.conf
+	ps -ef | grep "/usr/bin/dnsmasq" | grep "/run/wg870/dnsmasq.conf"
+	if [ $? != 0 ]; then
+		/usr/bin/dnsmasq -x /run/wg870/dnsmasq.pid -7 /run/wg870/dnsmasq.d --local-service -C /run/wg870/dnsmasq.conf -r /run/wg870
+	fi
+
+elif [ "$1" == "stop" ]; then
+	ps -ef | grep "/usr/bin/dnsmasq" | grep "/run/wg870/dnsmasq.conf" | awk '{print $2}' | xargs kill
+	ifconfig ap0 down
+	if [ $? != 0 ]; then
+		exit 1
+	fi
+else
+	exit 2
+fi
diff --git a/src/lynq/lib/liblynq-wifi6/scripts/start_stop_sta.sh b/src/lynq/lib/liblynq-wifi6/scripts/start_stop_sta.sh
new file mode 100755
index 0000000..5f33520
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts/start_stop_sta.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+if [ "$1" == "start" ]; then
+        ifconfig wlan0 up
+        if [ $? != 0 ]; then
+                exit 1
+        fi
+		ps -ef | grep "dhcpcd wlan0" | grep -v grep
+		if [ $? != 0 ];	then
+			dhcpcd wlan0 -t 0 -o domain_name_servers --noipv4ll -B -G &
+		fi
+elif [ "$1" == "stop" ]; then
+        ps -ef | grep "dhcpcd wlan0" | grep -v grep | awk '{print $2}' | xargs kill
+        ifconfig wlan0 0.0.0.0
+        if [ $? != 0 ]; then
+                exit 1
+        fi
+else
+        exit 2
+fi
+
diff --git a/src/lynq/lib/liblynq-wifi6/scripts/start_wg870_service.sh b/src/lynq/lib/liblynq-wifi6/scripts/start_wg870_service.sh
new file mode 100755
index 0000000..18e3d57
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts/start_wg870_service.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+for ((i=0; i < 10; i++)); do
+        state=`systemctl is-active wg870_drv_insmod.service`
+        if [ "$state" == "active" ]; then
+                break
+        elif [ "$state" != "activating" ]; then
+                echo "to start now"
+                systemctl start wg870_drv_insmod.service
+        fi
+        usleep 100000
+done
+
+if [ "$state" != "active" ]; then
+        exit 1
+fi
+
+state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ ping`
+if  [ "$state" != "PONG" ]; then
+    for ((i=0; i < 50; i++)); do
+        usleep 100000
+        state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ ping`
+        echo "$state"
+        if [ "$state" == "PONG" ]; then
+            service_started=1
+            break
+        fi
+    done
+else
+        service_started=1
+fi
+
+if [ "$service_started" != "1" ];then
+        exit 1
+fi
+
+state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ interface | grep -vE "Available|p2p-dev" | grep wlan0`
+if [ $? != 0 ]; then
+        state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ interface_add wlan0 /data/wifi/wg870/wpa_supplicant.conf nl80211`
+        if [ "$state" != "OK" ]; then
+                exit 2
+        fi
+fi
+
+state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ interface | grep -vE "Available|p2p-dev" | grep ap0`
+if [ $? != 0 ]; then
+		wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0
+        state=`wpa_cli -iwpa_wlan0_cmd -p/var/run/ interface_add ap0 /data/wifi/wg870/wpa_supplicant_ap.conf nl80211`
+
+        if [ "$state" != "OK" ]; then
+                exit 3
+        fi
+        ifconfig ap0 down
+fi
+
+
+exit 0
+
diff --git a/src/lynq/lib/liblynq-wifi6/scripts_connman/get_interface_name.sh b/src/lynq/lib/liblynq-wifi6/scripts_connman/get_interface_name.sh
new file mode 100755
index 0000000..61b44d9
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts_connman/get_interface_name.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$1" == "0" ]; then
+	echo "wlan0"
+elif [ "$1" == "1" ]; then
+	echo "tether"
+else
+	exit 1
+fi
+
diff --git a/src/lynq/lib/liblynq-wifi6/scripts_connman/sta_status_change.sh b/src/lynq/lib/liblynq-wifi6/scripts_connman/sta_status_change.sh
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts_connman/sta_status_change.sh
diff --git a/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_ap.sh b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_ap.sh
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_ap.sh
diff --git a/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_sta.sh b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_sta.sh
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_stop_sta.sh
diff --git a/src/lynq/lib/liblynq-wifi6/scripts_connman/start_wg870_service.sh b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_wg870_service.sh
new file mode 100755
index 0000000..303a075
--- /dev/null
+++ b/src/lynq/lib/liblynq-wifi6/scripts_connman/start_wg870_service.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+for ((i=0; i < 10; i++)); do
+	state=`systemctl is-active wg870_drv_insmod.service`
+	if [ "$state" == "active" ]; then
+		break	
+	elif [ "$state" != "activating" ]; then
+
+		systemctl start wg870_drv_insmod.service
+	fi
+	usleep 100000 
+done
+
+if [ "$state" != "active" ]; then
+	exit 1
+fi
+
+for ((i=0; i < 10; i++)); do
+	connmanctl technologies | grep -q "/net/connman/technology/wifi"
+	if [ $? == 0 ]; then
+		connman_started=1
+		break
+	fi
+done
+
+if [ "$connman_started" == ""]; then
+	exit 2
+fi
+
+ifconfig | grep -q wlan0
+
+if [ $? != 0 ]; then
+	connmanctl enable wifi
+
+	for ((i=0; i < 5; i++)); do
+		usleep 100000 
+		ifconfig | grep -q wlan0
+		if [ $? == 0 ]; then
+			wlan0_started=1
+			wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect
+			break
+		fi
+	done
+else
+	wlan0_started=1
+fi
+
+
+if [ "$wlan0_started" == ""]; then
+    exit 3
+fi
+
+ifconfig | grep -q ap0
+
+if [ $? != 0 ]; then
+	wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0
+	connmanctl tether wifi on lynq 1qaz@WSX#$%^
+
+	for ((i=0; i < 5; i++)); do
+		usleep 100000 
+		ifconfig | grep -q ap0
+		if [ $? == 0 ]; then
+			ap0_started=1
+			wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect
+			break
+		fi
+	done
+else
+	ap0_started=1
+fi
+
+
+if [ "$ap0_started" == ""]; then
+    exit 4
+fi
+