Эх сурвалжийг харах

2019-11-06 / Jerry Wang
Actions:
1. Module_Wifi add AP Mode function.
Files:
1. EVSE/Modularization/Module_Wifi.c

8054 5 жил өмнө
parent
commit
8b246fc116

BIN
EVSE/Modularization/Module_Wifi


+ 359 - 308
EVSE/Modularization/Module_Wifi.c

@@ -26,6 +26,7 @@
 #include	<time.h>
 #include	<ctype.h>
 #include 	<ifaddrs.h>
+//#include	"define.h"
 #include	"../Projects/define.h"
 
 #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
@@ -50,10 +51,15 @@
 #define WIFI_MODE_AP		2
 #define WIFI_MODE_ADHOC		3
 
-#define Initializing          0
-#define Wifi_device_detected  1
-#define Wifi_AP_connected     2
-#define Internet_checking     3
+#define STA_STATE_INIT        		0
+#define STA_STATE_DEVICE_DETECT		1
+#define STA_STATE_AP_CONNECTED	    2
+#define STA_STATE_INTERNET_CHECKING 3
+
+#define AP_STATE_INIT				0
+#define AP_STATE_DEVICE_DETECT		1
+#define AP_STATE_AP_START			2
+#define AP_STATE_SERVER_LISTEN		3
 
 #define UBLOX
 //#define MT7601U
@@ -74,13 +80,6 @@ struct interface_info
 	char staticIPAddr[24];
 }Wifi;
 
-/*struct auth_info
-{
-	int auth_type;
-	char *ssid;
-	char *passwd;
-};*/
-
 struct auth_info
 {
 	int auth_type;
@@ -97,12 +96,12 @@ int getInterfaceInfo();
 int isReachableInternet();
 int getLinkQuality();
 char* getTimeString(void);
-int setWPApara();
+int setWPAconf();
 int restartWPA();
 void refreshUSB();
 void getParameters();
 
-int Wifi_module_sts = 0;
+int Wifi_module_sts = STA_STATE_INIT;
 int isSetWPA_OK = false;
 
 int cnt_pingDNS_Fail;
@@ -241,8 +240,6 @@ void refreshUSB(void)
 	pclose(fp);
 }
 
-
-
 //==========================================
 // Check wifi interface status
 //==========================================
@@ -255,51 +252,51 @@ int isFindInterface()
 	//refreshUSB();
 
 	if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
+	{
+		if (getifaddrs(&ifaddr) != FAIL)
 		{
-			if (getifaddrs(&ifaddr) != FAIL)
+			for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
 			{
-				for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
-				{
-					if (ifa->ifa_addr == NULL ||
-						ifa->ifa_addr->sa_family != AF_PACKET) continue;
+				if (ifa->ifa_addr == NULL ||
+					ifa->ifa_addr->sa_family != AF_PACKET) continue;
 
-					for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
+				for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
+				{
+					if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
 					{
-						if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
-						{
-							Wifi.currentInterface = ifa->ifa_name;
-							result = PASS;
-						}
+						Wifi.currentInterface = ifa->ifa_name;
+						result = PASS;
 					}
 				}
-
-				freeifaddrs(ifaddr);
-				freeifaddrs(ifa);
 			}
+
+			freeifaddrs(ifaddr);
+			freeifaddrs(ifa);
 		}
-		else
+	}
+	else
+	{
+		if (getifaddrs(&ifaddr) != FAIL)
 		{
-			if (getifaddrs(&ifaddr) != FAIL)
+			for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
 			{
-				for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
-				{
-					if (ifa->ifa_addr == NULL ||
-						ifa->ifa_addr->sa_family != AF_PACKET) continue;
+				if (ifa->ifa_addr == NULL ||
+					ifa->ifa_addr->sa_family != AF_PACKET) continue;
 
-					for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
+				for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
+				{
+					if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
 					{
-						if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
-						{
-							Wifi.currentInterface = ifa->ifa_name;
-							result = PASS;
-						}
+						Wifi.currentInterface = ifa->ifa_name;
+						result = PASS;
 					}
 				}
-
-				freeifaddrs(ifaddr);
-				freeifaddrs(ifa);
 			}
+
+			freeifaddrs(ifaddr);
+			freeifaddrs(ifa);
 		}
+	}
 
 
 
@@ -377,7 +374,7 @@ int getInterfaceInfo()
 //==========================================
 // Check if module connect to wifi AP
 //==========================================
-int checkAP()
+int isConnectAP()
 {
 	int result = PASS;
 	FILE *fp;
@@ -449,6 +446,36 @@ int isReachableInternet()
 	return result;
 }
 
+//==========================================
+// Check module soft AP is start up
+//==========================================
+int isStartUpAP()
+{
+	int result = FAIL;
+	FILE *fp;
+	char cmd[256];
+	char buf[512];
+
+	// Get IP address & net mask
+	strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
+
+	fp = popen(cmd, "r");
+	if(fp != NULL)
+	{
+		while(fgets(buf, sizeof(buf), fp) != NULL)
+		{
+			if(strstr(buf, "BSS status = started") > 0)
+			{
+				result = PASS;
+				DEBUG_INFO("AP already start up.\n");
+			}
+		}
+	}
+	pclose(fp);
+
+	return result;
+}
+
 //==========================================
 // Get wifi connection quality
 //==========================================
@@ -493,254 +520,139 @@ int getLinkQuality()
 int setWPAconf()
 {
 	int result = FAIL;
-	char cmdBuf[512];
-
-	/*system("cat /dev/null > /etc/wpa.conf");
-	system("echo ctrl_interface_group=0 >> /etc/wpa.conf");
-	system("echo ctrl_interface=/var/run/wpa_supplicant >> /etc/wpa.conf");
-	system("echo ap_scan=1 >> /etc/wpa.conf");
-	system("echo update_config=1 >> /etc/wpa.conf");
-
-	system("echo network={ >> /etc/wpa.conf");
-	sprintf(cmdBuf, "echo ssid=\\\"%s\\\" >> /etc/wpa.conf", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
-	system(cmdBuf);
-	sprintf(cmdBuf, "echo psk=\\\"%s\\\" >> /etc/wpa.conf", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
-	system(cmdBuf);
-	system("echo scan_ssid=1 >> /etc/wpa.conf");
-	system("echo pairwise=CCMP TKIP >> /etc/wpa.conf");
-	system("echo key_mgmt=WPA-PSK >> /etc/wpa.conf");
-
-	switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
-	{
-		case WIFI_MODE_STA:
-			system("echo mode=0 >> /etc/wpa.conf");
-			break;
-		case WIFI_MODE_AP:
-			system("echo mode=2 >> /etc/wpa.conf");
-			break;
-		case WIFI_MODE_ADHOC:
-			system("echo mode=1 >> /etc/wpa.conf");
-
-			break;
-		case WIFI_MODE_DISABLE:
-		default:
-			break;
-
-	}
-	system("echo } >> /etc/wpa.conf");*/
 
+	char cmdBuf[512];
 	FILE *pFile;
 	char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
 
-	if(strlen((const char*)Wifi_A.passwd)>0) {
-		if(strlen((const char*)Wifi_A.passwd)>=8) {
-			strcat(buffer, "network={\n    ssid=\"");
-			strcat(buffer, (char*)Wifi_A.ssid);
-			strcat(buffer, "\"\n    key_mgmt=WPA-EAP WPA-PSK \n");
-			strcat(buffer, "    psk=\"");
-			strcat(buffer, (char*)Wifi_A.passwd);
-			strcat(buffer, "\"\n}\n\n");
-		}
-
-		if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13) {
-			strcat(buffer, "network={\n    ssid=\"");
-			strcat(buffer, (char*)Wifi_A.ssid);
-			strcat(buffer, "\"\n    key_mgmt=NONE\n");
-			strcat(buffer, "    wep_key0=\"");
-			strcat(buffer, (char*)Wifi_A.passwd);
-			strcat(buffer, "\"\n    auth_alg=OPEN SHARED\n");
-			strcat(buffer, "\n}\n\n");
-		}
-
-		strcat(buffer, "network={\n    ssid=\"");
-		strcat(buffer, (char*)Wifi_A.ssid);
-		strcat(buffer, "\"\n    key_mgmt=NONE");
-		strcat(buffer, "\n}");
-	}
-	else
-	{
-		strcat(buffer, "network={\n    ssid=\"");
-		strcat(buffer, (char*)Wifi_A.ssid);
-		strcat(buffer, "\"\n    key_mgmt=NONE");
-		strcat(buffer, "\n}");
-	}
-
-
-	pFile = fopen("/etc/wpa.conf","w");
-
-	fwrite(buffer,strlen(buffer),1,pFile);
-	fclose(pFile);
-	/*if(restartWPA() == PASS && checkAP() == PASS) {
-		result = PASS;
-	}*/
-
-	sprintf(cmdBuf, "pgrep -f \"wpa_supplicant -i %s\" | xargs kill", Wifi.currentInterface);
+	// Stop dhcp client or server
+	sprintf(cmdBuf, "pgrep -f \"udhcpc\" | xargs kill");
 	system(cmdBuf);
 
-	sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
+	sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
 	system(cmdBuf);
 
-	sleep(5);
+	// Clear wifi configuration file
+	system("cat /dev/null > /etc/wpa.conf");
 
-	sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
-	system(cmdBuf);
-	if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
+	switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
 	{
-		sprintf(cmdBuf, "/sbin/udhcpc -i %s -s /root/simple.script > /dev/null &", Wifi.currentInterface);
-		system(cmdBuf);
-	}
-    else
-    {
-		strcpy(cmdBuf, "ifconfig ");
-		strcat(cmdBuf, Wifi.currentInterface);
-		strcat(cmdBuf, " ");
-		strcat(cmdBuf, Wifi.staticIPAddr);
-		system(cmdBuf);
-
-		DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
-	}
-
+		case WIFI_MODE_STA:
+			// Fill out configuration value depend on share memory setting
+			if(strlen((const char*)Wifi_A.passwd)>0)
+			{
+				if(strlen((const char*)Wifi_A.passwd)>=8)
+				{
+					strcat(buffer, "network={\n    ssid=\"");
+					strcat(buffer, (char*)Wifi_A.ssid);
+					strcat(buffer, "\"\n    key_mgmt=WPA-EAP WPA-PSK \n");
+					strcat(buffer, "    psk=\"");
+					strcat(buffer, (char*)Wifi_A.passwd);
+					strcat(buffer, "\"\n}\n\n");
+				}
 
-	result = PASS;
-	return result;
-}
+				if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
+				{
+					strcat(buffer, "network={\n    ssid=\"");
+					strcat(buffer, (char*)Wifi_A.ssid);
+					strcat(buffer, "\"\n    key_mgmt=NONE\n");
+					strcat(buffer, "    wep_key0=\"");
+					strcat(buffer, (char*)Wifi_A.passwd);
+					strcat(buffer, "\"\n    auth_alg=OPEN SHARED\n");
+					strcat(buffer, "\n}\n\n");
+				}
 
-//==========================================
-// WPA parameter set
-//==========================================
-int setWPApara()
-{
-	int result = FAIL;
-	char cmdBuf[512];
+				strcat(buffer, "network={\n    ssid=\"");
+				strcat(buffer, (char*)Wifi_A.ssid);
+				strcat(buffer, "\"\n    key_mgmt=NONE");
+				strcat(buffer, "\n}");
+			}
+			else
+			{
+				strcat(buffer, "network={\n    ssid=\"");
+				strcat(buffer, (char*)Wifi_A.ssid);
+				strcat(buffer, "\"\n    key_mgmt=NONE");
+				strcat(buffer, "\n}");
+			}
 
-	DEBUG_INFO("Taregt SSID: %s\n", Wifi.currentSSID);
-	DEBUG_INFO("Taregt password: %s\n", Wifi.currentPasswd);
+			// Configuration value write to /etc/wpa.conf
+			pFile = fopen("/etc/wpa.conf","w");
+			fwrite(buffer,strlen(buffer), 1, pFile);
+			fclose(pFile);
 
-	sprintf(cmdBuf, "wpa_cli -i %s remove_network 0 > /dev/null 2>&1", Wifi.currentInterface);
-	DEBUG_INFO("%s\n", cmdBuf);
-	system(cmdBuf);
+			// Restart wpa_supplicant
+			sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
+			system(cmdBuf);
+			sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
+			system(cmdBuf);
 
-	sprintf(cmdBuf, "wpa_cli -i %s add_network 0 > /dev/null 2>&1", Wifi.currentInterface);
-	DEBUG_INFO("%s\n", cmdBuf);
-	system(cmdBuf);
+			// Stop wifi AP
+			sprintf(cmdBuf, "/root/uaputl bss_stop");
+			system(cmdBuf);
 
-	sprintf(cmdBuf, "wpa_cli -i %s set_network 0 ssid '\"%s\"' > /dev/null 2>&1", Wifi.currentInterface, Wifi.currentSSID);
-	DEBUG_INFO("%s\n", cmdBuf);
-	system(cmdBuf);
+			sleep(5);
 
-	switch(Wifi.authType)
-	{
-		case AUTH_TYPE_NONE:
-			DEBUG_INFO("Wifi auth type is NONE.\n");
+			// Wifi IP set by DHCP client or static
+			if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
+			{
+				sprintf(cmdBuf, "/sbin/udhcpc -i %s -s /root/simple.script > /dev/null &", Wifi.currentInterface);
+				system(cmdBuf);
+			}
+			else
+			{
+				strcpy(cmdBuf, "ifconfig ");
+				strcat(cmdBuf, Wifi.currentInterface);
+				strcat(cmdBuf, " ");
+				strcat(cmdBuf, Wifi.staticIPAddr);
+				system(cmdBuf);
 
-			sprintf(cmdBuf, "wpa_cli -i %s set_network 0 key_mgmt NONE > /dev/null 2>&1", Wifi.currentInterface);
-			DEBUG_INFO("%s\n", cmdBuf);
+				DEBUG_INFO("Setting %s static IP to %s ...\n", Wifi.currentInterface, Wifi.staticIPAddr);
+			}
+			break;
+		case WIFI_MODE_AP:
+			// Stop wifi AP
+			sprintf(cmdBuf, "/root/uaputl bss_stop");
 			system(cmdBuf);
 
-			break;
-		case AUTH_TYPE_WEP:
-			DEBUG_INFO("Wifi auth type is WEP.\n");
+			// Set SSID to uaputl.conf
+			sprintf(cmdBuf, "sed -i 's/ SSID=.*$/ SSID=\"%s\"/' /root/uaputl_config/uaputl.conf", Wifi_A.ssid);
+			system(cmdBuf);
 
-			sprintf(cmdBuf, "wpa_cli -i %s set_network 0 key_mgmt NONE > /dev/null 2>&1", Wifi.currentInterface);
-			DEBUG_INFO("%s\n", cmdBuf);
+			// Set AP configuration by uaputl.conf
+			sprintf(cmdBuf, "/root/uaputl sys_config /root/uaputl_config/uaputl.conf");
 			system(cmdBuf);
 
-			sprintf(cmdBuf, "wpa_cli -i %s set_network 0 wep_key0 '\"%s\"' > /dev/null 2>&1", Wifi.currentInterface, Wifi.currentPasswd);
-			DEBUG_INFO("%s\n", cmdBuf);
+			// Start up wifi AP
+			sprintf(cmdBuf, "/root/uaputl bss_start");
 			system(cmdBuf);
 
-			break;
-		case AUTH_TYPE_WPAPSK:
-			DEBUG_INFO("Wifi auth type is WPAPSK.\n");
+			sleep(5);
 
-			sprintf(cmdBuf, "wpa_cli -i %s set_network 0 psk '\"%s\"' > /dev/null 2>&1", Wifi.currentInterface, Wifi.currentPasswd);
-			DEBUG_INFO("%s\n", cmdBuf);
+			// Set wifi AP ip address
+			sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
 			system(cmdBuf);
+			DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
 
+			// Start DHCP server
+			if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
+			{
+				sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
+				system(cmdBuf);
+			}
 			break;
-		case AUTH_TYPE_WPA2PSK:
-			DEBUG_INFO("Wifi auth type is WPA2PSK.\n");
+		case WIFI_MODE_ADHOC:
 			break;
+		case WIFI_MODE_DISABLE:
 		default:
-			DEBUG_INFO("Wifi auth type can not be identify and switch to default NONE.\n");
-
-			sprintf(cmdBuf, "wpa_cli -i %s set_network 0 key_mgmt NONE > /dev/null 2>&1", Wifi.currentInterface);
-			DEBUG_INFO("%s\n", cmdBuf);
-			system(cmdBuf);
-
 			break;
-	}
 
-	sprintf(cmdBuf, "wpa_cli -i %s enable_network 0 > /dev/null 2>&1", Wifi.currentInterface);
-	DEBUG_INFO("%s\n", cmdBuf);
-	system(cmdBuf);
-
-
-	if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient != 1)
-	{
-		// IP obtain method is configuration DHCP
-		sprintf(cmdBuf, "dhclient %s > /dev/null 2>&1", Wifi.currentInterface);
-		DEBUG_INFO("%s\n", cmdBuf);
-		system(cmdBuf);
-	}
-	else
-	{
-		// IP obtain method is configuration static
-		sprintf(cmdBuf, "ifconfig %s %s netmask %s > /dev/null 2>&1", Wifi.currentInterface, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
-		DEBUG_INFO("%s\n", cmdBuf);
-		system(cmdBuf);
-
-		sprintf(cmdBuf, "route add default gw %s > /dev/null 2>&1", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
-		DEBUG_INFO("%s\n", cmdBuf);
-		system(cmdBuf);
 	}
 
 
 	result = PASS;
-	return result;
-}
-
-
-/*
-int setWPApara()
-{
-	int result = FAIL;
-
-	FILE *pFile;
-	char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
-
-	if(strlen(Wifi_A.passwd)>0) {
-		strcat(buffer, "network={\n    ssid=\"");
-		strcat(buffer, Wifi_A.ssid);
-		strcat(buffer, "\"\n    key_mgmt=WPA-EAP WPA-PSK \n");
-		strcat(buffer, "    psk=\"");
-		strcat(buffer, Wifi_A.passwd);
-		strcat(buffer, "\"\n}\n\n");
-
-		strcat(buffer, "network={\n    ssid=\"");
-		strcat(buffer, Wifi_A.ssid);
-		strcat(buffer, "\"\n    key_mgmt=NONE\n");
-		strcat(buffer, "    wep_key0=\"");
-		strcat(buffer, Wifi_A.passwd);
-		strcat(buffer, "\"\n}\n\n");
-
-		strcat(buffer, "network={\n    ssid=\"");
-		strcat(buffer, Wifi_A.ssid);
-		strcat(buffer, "\"\n    key_mgmt=NONE");
-		strcat(buffer, "\n}");
-	}
-
-
-	pFile = fopen("/etc/wpa_supplicant/wpa_supplicant.conf","w");
-
-	fwrite(buffer,strlen(buffer),1,pFile);
-	fclose(pFile);
-	if(restartWPA() == PASS && checkAP() == PASS) {
-		result = PASS;
-	}
 
 	return result;
-}*/
+}
 
 //=================================
 // Common routine
@@ -942,45 +854,11 @@ int restartWPA(void)
 	return result;
 }
 
-int main(void)
+void proc_sta()
 {
-	if(InitShareMemory() == FAIL)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("InitShareMemory NG\n");
-		#endif
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}
-		sleep(5);
-		return 0;
-	}
-
-	//=============================================
-	// Install WIFI module driver
-	//=============================================
-	#ifdef UBLOX
-		system("insmod /lib/modules/mlan.ko");
-		system("insmod /lib/modules/usb8801.ko");
-		sleep(5);
-		system("ifconfig mlan0");
-	#endif
-
-	#ifdef MT7601U
-		system("insmod /lib/modules/mt7601u.ko");
-		sleep(5);
-		system("ifconfig wlan0");
-	#endif
-
-
-	DEBUG_INFO("=================[State 0]===================\n");
-
-
-	for(;;)
+	switch(Wifi_module_sts)
 	{
-		switch(Wifi_module_sts) {
-		case Initializing:
+		case STA_STATE_INIT:
 			// get info from shared memory
 			getParameters();
 
@@ -988,27 +866,31 @@ int main(void)
 			ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
 			isSetWPA_OK = false;
 			// check interface
-			if(strlen((const char*)Wifi_A.ssid)>0 && isFindInterface() == PASS){
+			if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
+			{
 				DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
-				Wifi_module_sts = Wifi_device_detected;
+				Wifi_module_sts = STA_STATE_DEVICE_DETECT;
 				ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
 				DEBUG_INFO("=================[State 1]===================\n");
-			}else {
+			}
+			else
+			{
 				ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
 				DEBUG_ERROR("Wifi support interface valid result: Fail\n");
 				sleep(30);
 			}
 			break;
-		case Wifi_device_detected:
+		case STA_STATE_DEVICE_DETECT:
 
-			if((isSetWPA_OK == false) && (setWPAconf() == PASS)) {
+			if(!isSetWPA_OK && (setWPAconf() == PASS))
+			{
 				isSetWPA_OK = true;
 				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
 				sleep(5);
 			}
-			else if((isSetWPA_OK == true) && (checkAP() == PASS))
+			else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
 			{
-				Wifi_module_sts = Wifi_AP_connected;
+				Wifi_module_sts = STA_STATE_AP_CONNECTED;
 				ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
 				DEBUG_INFO("=================[State 2]===================\n");
 			}
@@ -1016,19 +898,22 @@ int main(void)
 			{
 				cnt_getAP_Fail++;
 				DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
-				if(cnt_getAP_Fail>=3) {
-					Wifi_module_sts = Initializing;
+				if(cnt_getAP_Fail>=3)
+				{
+					Wifi_module_sts = STA_STATE_INIT;
 					cnt_getAP_Fail = 0;
 
 					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
 					DEBUG_INFO("=================[State 0]===================\n");
-				}else {
+				}
+				else
+				{
 					sleep(10);
 				}
 			}
 
 			break;
-		case Wifi_AP_connected:
+		case STA_STATE_AP_CONNECTED:
 			getInterfaceInfo();
 			getLinkQuality();
 			DEBUG_INFO("Wifi quality: %d\n", Wifi.rssi);
@@ -1041,7 +926,8 @@ int main(void)
 				cnt_pingDNS_Fail = 0;
 				sleep(30);
 			}
-			else{
+			else
+			{
 				cnt_pingDNS_Fail++;
 				DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
 				sleep(5);
@@ -1057,23 +943,188 @@ int main(void)
 				memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
 				memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
 
-				if(isFindInterface() == PASS){
+				if(isFindInterface() == PASS)
+				{
 					DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
 					isSetWPA_OK = false;
 					ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
-					Wifi_module_sts = Wifi_device_detected;
+					Wifi_module_sts = STA_STATE_DEVICE_DETECT;
 					DEBUG_INFO("=================[State 1]===================\n");
-				}else {
+				}
+				else
+				{
+					DEBUG_INFO("Wifi support interface valid result: Fail\n");
+					ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
+					Wifi_module_sts = STA_STATE_INIT;
+					DEBUG_INFO("=================[State 0]===================\n");
+				}
+			}
+			break;
+		default:
+			Wifi_module_sts = STA_STATE_INIT;
+			break;
+	}
+}
+
+void proc_ap()
+{
+	switch(Wifi_module_sts)
+	{
+		case AP_STATE_INIT:
+			// get info from shared memory
+			getParameters();
+			isSetWPA_OK = false;
+			ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
+
+			// check interface
+			if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
+			{
+				DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
+				Wifi_module_sts = AP_STATE_DEVICE_DETECT;
+				ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
+				DEBUG_INFO("=================[State 1]===================\n");
+			}
+			else
+			{
+				ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
+				DEBUG_ERROR("Wifi support interface valid result: Fail\n");
+				sleep(30);
+			}
+			break;
+		case AP_STATE_DEVICE_DETECT:
+			if(!isSetWPA_OK && (setWPAconf() == PASS))
+			{
+				isSetWPA_OK = true;
+				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
+				sleep(5);
+			}
+			else if(isSetWPA_OK && (isStartUpAP() == PASS))
+			{
+				Wifi_module_sts = AP_STATE_AP_START;
+				ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
+				DEBUG_INFO("=================[State 2]===================\n");
+			}
+			else
+			{
+				cnt_getAP_Fail++;
+				DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
+				if(cnt_getAP_Fail>=3)
+				{
+					Wifi_module_sts = AP_STATE_INIT;
+					cnt_getAP_Fail = 0;
+
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
+					DEBUG_INFO("=================[State 0]===================\n");
+				}
+				else
+				{
+					sleep(10);
+				}
+			}
+			break;
+		case AP_STATE_AP_START:
+			getInterfaceInfo();
+
+			if(isStartUpAP() == PASS)
+			{
+				DEBUG_INFO("Wifi AP start up valid result: Pass\n");
+				ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
+				cnt_pingDNS_Fail = 0;
+				sleep(30);
+			}
+			else
+			{
+				cnt_pingDNS_Fail++;
+				DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
+				sleep(5);
+			}
+
+			if(cnt_pingDNS_Fail >= 3)
+			{
+				ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
+				cnt_pingDNS_Fail = 0;
+
+				DEBUG_INFO("Wifi AP start up status failed...");
+				memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
+				memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
+				memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
+
+				if(isFindInterface() == PASS)
+				{
+					DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
+					isSetWPA_OK = false;
+					ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
+					Wifi_module_sts = STA_STATE_DEVICE_DETECT;
+					DEBUG_INFO("=================[State 1]===================\n");
+				}
+				else
+				{
 					DEBUG_INFO("Wifi support interface valid result: Fail\n");
 					ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
-					Wifi_module_sts = Initializing;
+					Wifi_module_sts = STA_STATE_INIT;
 					DEBUG_INFO("=================[State 0]===================\n");
 				}
 			}
 			break;
+		default:
+			Wifi_module_sts = AP_STATE_INIT;
+			break;
+	}
+}
+
+int main(void)
+{
+	if(InitShareMemory() == FAIL)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("InitShareMemory NG\n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return 0;
+	}
+
+	//=============================================
+	// Install WIFI module driver
+	//=============================================
+	#ifdef UBLOX
+		system("insmod /lib/modules/mlan.ko");
+		system("insmod /lib/modules/usb8801.ko");
+		sleep(5);
+		system("ifconfig mlan0 up");
+		system("ifconfig uap0 up");
+	#endif
+
+	#ifdef MT7601U
+		system("insmod /lib/modules/mt7601u.ko");
+		sleep(5);
+		system("ifconfig wlan0 up");
+	#endif
+
+
+	DEBUG_INFO("=================[State 0]===================\n");
+	for(;;)
+	{
+		switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
+		{
+			case WIFI_MODE_STA:
+				proc_sta();
+				break;
+			case WIFI_MODE_AP:
+				proc_ap();
+				break;
+			case WIFI_MODE_ADHOC:
+				break;
+			case WIFI_MODE_DISABLE:
+			default:
+				break;
 		}
+
+
 	}
 
 	return 0;
 }
-

BIN
EVSE/Modularization/OcppBackend


BIN
EVSE/Modularization/WebService


BIN
EVSE/Modularization/libModule_RFID.a


BIN
EVSE/Modularization/logPackTools