Browse Source

[Add][Modularization][Module_4g]

2022.04.01 / Folus Wen

Actions:
1. Module_4g connect log output improve.
2. Add Tx/Rx data counter info to log.

Files:
1. As follow commit history

Image version: D0.00.XX.XXXX.XX
Image checksum: XXXXXXXX

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 3 years ago
parent
commit
3a8edd5940
1 changed files with 568 additions and 529 deletions
  1. 568 529
      EVSE/Modularization/Module_4g.c

+ 568 - 529
EVSE/Modularization/Module_4g.c

@@ -17,12 +17,12 @@
 
 #include 	<unistd.h>
 #include 	<stdarg.h>
-#include    <stdio.h>		/*標準輸入輸出定義*/
-#include    <stdlib.h>		/*標準函數庫定義*/
-#include    <unistd.h>		/*Unix 標準函數定義*/
-#include    <fcntl.h>		/*檔控制定義*/
-#include    <termios.h>		/*PPSIX 終端控制定義*/
-#include    <errno.h>		/*錯誤號定義*/
+#include    <stdio.h>
+#include    <stdlib.h>
+#include    <unistd.h>
+#include    <fcntl.h>
+#include    <termios.h>
+#include    <errno.h>
 #include 	<errno.h>
 #include 	<string.h>
 #include	<time.h>
@@ -44,6 +44,16 @@ enum DONGLE_MODEL
 	DONGLE_UBLOX
 };
 
+enum CONNECT_STEP
+{
+	CONNECT_STEP_CHECK_MODULE=0,
+	CONNECT_STEP_READ_MODULE_INFO,
+	CONNECT_STEP_READ_SIM_INFO,
+	CONNECT_STEP_CONNECTING,
+	CONNECT_STEP_CONNECTED,
+	CONNECT_STEP_DISCONNECTED
+};
+
 // Define Module network mode
 #define NET_MODE_NO_SERVICE		0
 #define NET_MODE_CDMA			1
@@ -60,7 +70,7 @@ enum DONGLE_MODEL
 #define CheckModemInterval			30	// Seconds
 #define CheckSimInterval			30 	// Seconds
 #define CheckModemInfoInterval		30	// Seconds
-#define CheckConnectionInterval		30 	// Seconds
+#define CheckConnectionInterval		60 	// Seconds
 #define CheckInternetInterval		30	// Seconds
 #define DisconnInterval				60 	// Seconds
 
@@ -70,7 +80,7 @@ enum DONGLE_MODEL
 
 int Check4GModem(void);
 int isPppUp(void);
-int isReadInfo(void);
+int isReadInfo(int connectStep);
 int isReadSimInfo(void);
 int isReachableInternet(void);
 int isModuleUnbind(void);
@@ -110,6 +120,8 @@ struct dongle_info
 	int cnt_pppFail;
 	int cnt_SearchModuleFail;
 	int cnt_ReadSimInfoFail;
+	int bytesTx;
+	int bytesRx;
 }Dongle;
 
 struct SysConfigAndInfo			*ShmSysConfigAndInfo;
@@ -132,7 +144,7 @@ int StoreLogMsg(const char *fmt, ...)
 	memset(Buf,0,sizeof(Buf));
 	CurrentTime = time(NULL);
 	tm=localtime(&CurrentTime);
-	sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]4g_SystemLog",
+	sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d]%s\" >> /Storage/SystemLog/[%04d.%02d]4g_SystemLog",
 			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
 			buffer,
 			tm->tm_year+1900,tm->tm_mon+1);
@@ -294,7 +306,7 @@ int isPppUp(void)
 //==========================================
 // Check dongle info read status
 //==========================================
-int isReadInfo(void)
+int isReadInfo(int connectStep)
 {
 	int result = PASS;
 	int uart;
@@ -307,7 +319,7 @@ int isReadInfo(void)
 		switch(Dongle.Model)
 		{
 			case DONGLE_QUECTEL:
-				default:
+			default:
 				//==============================
 				// Set don't echo command
 				//==============================
@@ -325,144 +337,164 @@ int isReadInfo(void)
 				else
 					result = FAIL;
 
-				//==============================
-				// Read Manufacturer
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+gmi\r", rx);
-				if(Length > 0)
+				if(connectStep == CONNECT_STEP_READ_MODULE_INFO)
 				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx,"OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					trim_s(tmp,Length);
-
-					if(strstr(tmp, "atgmi") != NULL)
+					//==============================
+					// Read Manufacturer
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+gmi\r", rx);
+					if(Length > 0)
 					{
-						substr(tmp, tmp, 5, strlen(tmp)-5);
-					}
-					memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
-					strncpy(Dongle.MANUFACTURER, tmp, strlen(tmp));
-				}
-				else
-					result = FAIL;
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx,"OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						trim_s(tmp,Length);
 
-				//==============================
-				// Read Model
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+gmm\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx,"OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					trim_s(tmp,Length);
+						if(strstr(tmp, "atgmi") != NULL)
+						{
+							substr(tmp, tmp, 5, strlen(tmp)-5);
+						}
+						memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
+						strncpy(Dongle.MANUFACTURER, tmp, strlen(tmp));
+					}
+					else
+						result = FAIL;
 
-					if(strstr(tmp, "atgmm") != NULL)
+					//==============================
+					// Read Model
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+gmm\r", rx);
+					if(Length > 0)
 					{
-						substr(tmp, tmp, 5, strlen(tmp)-5);
-					}
-					memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
-					strncpy(Dongle.MODELNAME, tmp, strlen(tmp));
-				}
-				else
-					result = FAIL;
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx,"OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						trim_s(tmp,Length);
 
-				//==============================
-				// Read Revision
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+gmr\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx, "OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					trim_s(tmp,Length);
+						if(strstr(tmp, "atgmm") != NULL)
+						{
+							substr(tmp, tmp, 5, strlen(tmp)-5);
+						}
+						memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
+						strncpy(Dongle.MODELNAME, tmp, strlen(tmp));
+					}
+					else
+						result = FAIL;
 
-					if(strstr(tmp, "atgmr") != NULL)
+					//==============================
+					// Read Revision
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+gmr\r", rx);
+					if(Length > 0)
 					{
-						substr(tmp, tmp, 5, strlen(tmp)-5);
-					}
-					memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
-					strncpy(Dongle.REVISION, tmp, strlen(tmp));
-				}
-				else
-					result = FAIL;
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx, "OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						trim_s(tmp,Length);
 
-				//==============================
-				// Read IMEI
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+gsn\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx, "OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					trim_s(tmp,Length);
+						if(strstr(tmp, "atgmr") != NULL)
+						{
+							substr(tmp, tmp, 5, strlen(tmp)-5);
+						}
+						memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
+						strncpy(Dongle.REVISION, tmp, strlen(tmp));
+					}
+					else
+						result = FAIL;
 
-					if(strstr(tmp, "atgsn") != NULL)
+					//==============================
+					// Read IMEI
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+gsn\r", rx);
+					if(Length > 0)
 					{
-						substr(tmp, tmp, 5, strlen(tmp)-5);
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx, "OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						trim_s(tmp,Length);
+
+						if(strstr(tmp, "atgsn") != NULL)
+						{
+							substr(tmp, tmp, 5, strlen(tmp)-5);
+						}
+						memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
+						strncpy(Dongle.IMEI, tmp, strlen(tmp));
 					}
-					memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
-					strncpy(Dongle.IMEI, tmp, strlen(tmp));
+					else
+						result = FAIL;
 				}
-				else
-					result = FAIL;
-
-				//==============================
-				// Read CSQ
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+csq\r", rx);
-				if(Length > 0)
+				else if(connectStep == CONNECT_STEP_CONNECTED)
 				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx,","));
-					strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
-					Dongle.CSQ = atoi(rx);
-				}
-				else
-					result = FAIL;
+					//==============================
+					// Read CSQ
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+csq\r", rx);
+					if(Length > 0)
+					{
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx,","));
+						strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
+						Dongle.CSQ = atoi(rx);
+					}
+					else
+						result = FAIL;
 
-				//==============================
-				// Read Mode
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+qnwinfo\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, sizeof tmp);
-					if(sscanf(rx, "%*[^:]:%[^,],%[^,],%[^,],%d", Dongle.act, Dongle.operator, Dongle.band, &Dongle.channel) == 4)
+					//==============================
+					// Read Mode
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+qnwinfo\r", rx);
+					if(Length > 0)
 					{
-						//strncpy(tmp, &rx[strcspn(rx,":")+2], strcspn(rx,",")-(strcspn(rx,":")+2));
-						if((strstr(Dongle.act, "GPRS") != NULL) || (strstr(Dongle.act, "EDGE") != NULL))
-							Dongle.MODE = NET_MODE_GMS_GPRS;
-						else if(strstr(Dongle.act, "WCDMA") != NULL)
-							Dongle.MODE = NET_MODE_WCDMA;
-						else if(strstr(Dongle.act, "GSM") != NULL)
-							Dongle.MODE = NET_MODE_GSM_WCDMA;
-						else if(strstr(Dongle.act, "TDSCDMA") != NULL)
-							Dongle.MODE = NET_MODE_TD_SCDMA;
-						else if(strstr(Dongle.act, "CDMA") != NULL)
-							Dongle.MODE = NET_MODE_CDMA;
-						else if(strstr(Dongle.act, "LTE") != NULL)
-							Dongle.MODE = NET_MODE_LTE;
-						else if((strstr(Dongle.act, "HSDPA") != NULL) || (strstr(Dongle.act, "HSUPA") != NULL) || (strstr(Dongle.act, "HSPA+") != NULL))
-							Dongle.MODE = NET_MODE_HSPA;
+						memset(tmp, 0, sizeof tmp);
+						if(sscanf(rx, "%*[^:]:%[^,],%[^,],%[^,],%d", Dongle.act, Dongle.operator, Dongle.band, &Dongle.channel) == 4)
+						{
+							//strncpy(tmp, &rx[strcspn(rx,":")+2], strcspn(rx,",")-(strcspn(rx,":")+2));
+							if((strstr(Dongle.act, "GPRS") != NULL) || (strstr(Dongle.act, "EDGE") != NULL))
+								Dongle.MODE = NET_MODE_GMS_GPRS;
+							else if(strstr(Dongle.act, "WCDMA") != NULL)
+								Dongle.MODE = NET_MODE_WCDMA;
+							else if(strstr(Dongle.act, "GSM") != NULL)
+								Dongle.MODE = NET_MODE_GSM_WCDMA;
+							else if(strstr(Dongle.act, "TDSCDMA") != NULL)
+								Dongle.MODE = NET_MODE_TD_SCDMA;
+							else if(strstr(Dongle.act, "CDMA") != NULL)
+								Dongle.MODE = NET_MODE_CDMA;
+							else if(strstr(Dongle.act, "LTE") != NULL)
+								Dongle.MODE = NET_MODE_LTE;
+							else if((strstr(Dongle.act, "HSDPA") != NULL) || (strstr(Dongle.act, "HSUPA") != NULL) || (strstr(Dongle.act, "HSPA+") != NULL))
+								Dongle.MODE = NET_MODE_HSPA;
+							else
+								Dongle.MODE = NET_MODE_UNKNOWN;
+						}
 						else
-							Dongle.MODE = NET_MODE_UNKNOWN;
+						{
+							DEBUG_WARN("at+qnwinfo response can not parse.\n");
+						}
 					}
 					else
+						result = FAIL;
+
+					//==============================
+					// Read data counter
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+qgdcnt?\r", rx);
+					if(Length > 0)
 					{
-						DEBUG_WARN("at+qnwinfo response can not parse.\n");
+						if(sscanf(rx, "%*[^:]:%d,%d", &Dongle.bytesTx, &Dongle.bytesRx) != 2)
+						{
+							DEBUG_WARN("at+qgdcnt response can not parse.\n");
+						}
 					}
+					else
+						result = FAIL;
 				}
-				else
-					result = FAIL;
 
 				break;
 			case DONGLE_THALES:
@@ -483,133 +515,142 @@ int isReadInfo(void)
 				else
 					result = FAIL;
 
-				//==============================
-				// Read Manufacturer
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+cgmi\r", rx);
-				if(Length > 0)
+				if(connectStep == CONNECT_STEP_READ_MODULE_INFO)
 				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx,"OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
-					strncpy(Dongle.MANUFACTURER, tmp+2, 9);
-				}
-				else
-					result = FAIL;
-
-				//==============================
-				// Read Model
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+cgmm\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, &rx[strcspn(rx,"PLS")], strcspn(rx,"OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
-					strncpy(Dongle.MODELNAME , tmp, 8);
-				}
-				else
-					result = FAIL;
+					//==============================
+					// Read Manufacturer
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+cgmi\r", rx);
+					if(Length > 0)
+					{
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx,"OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
+						strncpy(Dongle.MANUFACTURER, tmp+2, 9);
+					}
+					else
+						result = FAIL;
 
-				//==============================
-				// Read Revision
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "at+cgmr\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, &rx[strcspn(rx, "REVISION")+9], strcspn(rx, "OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
-					strncpy(Dongle.REVISION, tmp, 6);
-				}
-				else
-					result = FAIL;
+					//==============================
+					// Read Model
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+cgmm\r", rx);
+					if(Length > 0)
+					{
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, &rx[strcspn(rx,"PLS")], strcspn(rx,"OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
+						strncpy(Dongle.MODELNAME , tmp, 8);
+					}
+					else
+						result = FAIL;
 
-				//==============================
-				// Read IMEI
-				//==============================
-				Length = at_command(uart, "at+cgsn\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx, "OK"));
-					memset(rx, 0, ARRAY_SIZE(rx));
-					trim_s(tmp,Length);
-					memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
-					strncpy(Dongle.IMEI, tmp, strlen(tmp));
-				}
-				else
-					result = FAIL;
+					//==============================
+					// Read Revision
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "at+cgmr\r", rx);
+					if(Length > 0)
+					{
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, &rx[strcspn(rx, "REVISION")+9], strcspn(rx, "OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
+						strncpy(Dongle.REVISION, tmp, 6);
+					}
+					else
+						result = FAIL;
 
-				//==============================
-				// Read CSQ
-				//==============================
-				Length = at_command(uart, "at+csq\r", rx);
-				if(Length > 0)
-				{
-					memset(tmp, 0, ARRAY_SIZE(tmp));
-					memcpy(tmp, rx, strcspn(rx,","));
-					strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
-					Dongle.CSQ = atoi(rx);
+					//==============================
+					// Read IMEI
+					//==============================
+					Length = at_command(uart, "at+cgsn\r", rx);
+					if(Length > 0)
+					{
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx, "OK"));
+						memset(rx, 0, ARRAY_SIZE(rx));
+						trim_s(tmp,Length);
+						memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
+						strncpy(Dongle.IMEI, tmp, strlen(tmp));
+					}
+					else
+						result = FAIL;
 				}
-				else
-					result = FAIL;
-
-				//==============================
-				// Read Mode
-				//==============================
-				memset(rx, 0x00, ARRAY_SIZE(rx));
-				Length = at_command(uart, "AT^SMONI\r", rx);
-				if(Length > 0)
+				else if(connectStep == CONNECT_STEP_CONNECTED)
 				{
-					memset(tmp, 0, sizeof tmp);
-					if(strstr(rx, "2G,") != NULL)
+					//==============================
+					// Read CSQ
+					//==============================
+					Length = at_command(uart, "at+csq\r", rx);
+					if(Length > 0)
 					{
-						DEBUG_INFO("2G network\n");
-
-						if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.operator, Dongle.act) == 2)
-						{
-							if(strstr(Dongle.act, "G") != NULL)
-								Dongle.MODE = NET_MODE_GMS_GPRS;
-							else if(strstr(Dongle.act, "E") != NULL)
-								Dongle.MODE = NET_MODE_GMS_GPRS;
-							else
-								Dongle.MODE = NET_MODE_CDMA;
-						}
-						else
-						{
-							DEBUG_WARN("AT^SMONI response can not parse.\n");
-						}
+						memset(tmp, 0, ARRAY_SIZE(tmp));
+						memcpy(tmp, rx, strcspn(rx,","));
+						strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
+						Dongle.CSQ = atoi(rx);
 					}
-					else if(strstr(rx, "3G,") != NULL)
+					else
+						result = FAIL;
+
+					//==============================
+					// Read Mode
+					//==============================
+					memset(rx, 0x00, ARRAY_SIZE(rx));
+					Length = at_command(uart, "AT^SMONI\r", rx);
+					if(Length > 0)
 					{
-						if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%*[^,],%*[^,],%[^,]", &Dongle.channel, Dongle.operator) == 2)
+						memset(tmp, 0, sizeof tmp);
+						if(strstr(rx, "2G,") != NULL)
 						{
-							Dongle.MODE = NET_MODE_GSM_WCDMA;
-							memset(Dongle.band, 0x00, ARRAY_SIZE(Dongle.band));
-							memset(Dongle.act, 0x00, ARRAY_SIZE(Dongle.act));
+							DEBUG_INFO("2G network\n");
+
+							if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.operator, Dongle.act) == 2)
+							{
+								if(strstr(Dongle.act, "G") != NULL)
+									Dongle.MODE = NET_MODE_GMS_GPRS;
+								else if(strstr(Dongle.act, "E") != NULL)
+									Dongle.MODE = NET_MODE_GMS_GPRS;
+								else
+									Dongle.MODE = NET_MODE_CDMA;
+							}
+							else
+							{
+								DEBUG_WARN("AT^SMONI response can not parse.\n");
+							}
 						}
-						else
+						else if(strstr(rx, "3G,") != NULL)
 						{
-							DEBUG_WARN("AT^SMONI response can not parse.\n");
+							if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%*[^,],%*[^,],%[^,]", &Dongle.channel, Dongle.operator) == 2)
+							{
+								Dongle.MODE = NET_MODE_GSM_WCDMA;
+								memset(Dongle.band, 0x00, ARRAY_SIZE(Dongle.band));
+								memset(Dongle.act, 0x00, ARRAY_SIZE(Dongle.act));
+							}
+							else
+							{
+								DEBUG_WARN("AT^SMONI response can not parse.\n");
+							}
 						}
-					}
-					else if(strstr(rx, "4G,") != NULL)
-					{
-						if(sscanf(rx, "%*[^:]:%*[^,],%d,%[^,],%*[^,],%*[^,],%[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.band, Dongle.act, Dongle.operator) == 4)
+						else if(strstr(rx, "4G,") != NULL)
 						{
-							if(strstr(Dongle.act, "FDD") != NULL)
-								Dongle.MODE = NET_MODE_LTE;
-							else if(strstr(Dongle.act, "TDD") != NULL)
-								Dongle.MODE = NET_MODE_TD_SCDMA;
+							if(sscanf(rx, "%*[^:]:%*[^,],%d,%[^,],%*[^,],%*[^,],%[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.band, Dongle.act, Dongle.operator) == 4)
+							{
+								if(strstr(Dongle.act, "FDD") != NULL)
+									Dongle.MODE = NET_MODE_LTE;
+								else if(strstr(Dongle.act, "TDD") != NULL)
+									Dongle.MODE = NET_MODE_TD_SCDMA;
+								else
+									Dongle.MODE = NET_MODE_UNKNOWN;
+							}
 							else
-								Dongle.MODE = NET_MODE_UNKNOWN;
+							{
+								DEBUG_WARN("AT^SMONI response can not parse.\n");
+							}
 						}
 						else
 						{
@@ -617,12 +658,8 @@ int isReadInfo(void)
 						}
 					}
 					else
-					{
-						DEBUG_WARN("AT^SMONI response can not parse.\n");
-					}
+						result = FAIL;
 				}
-				else
-					result = FAIL;
 
 				break;
 		}
@@ -1292,17 +1329,17 @@ int csq2rssi(int csq)
 //==========================================
 int main(void)
 {
-	unsigned char Present_Module_Mode = 0xff;
 	unsigned char Previous_Module_Mode = 0xff;
+	int connectStep=0;
+	int preConnectStep=-1;
+	int delayInterval = 5;
 
 	//==========================================
 	//Initialization share memory
 	//==========================================
 	if(InitShareMemory() == FAIL)
 	{
-		#ifdef SystemLogMessage
 		DEBUG_ERROR("InitShareMemory NG\n");
-		#endif
 
 		if(ShmStatusCodeData!=NULL)
 		{
@@ -1316,33 +1353,30 @@ int main(void)
 	DEBUG_INFO("========================================\n");
 	DEBUG_INFO("Latest Firmware Version : [ %s ].\n", Version_And_Date[0]);
 	DEBUG_INFO("Latest Upgrade Date : [ %s ].\n", Version_And_Date[1]);
-	DEBUG_INFO("========================================\n");
 	
 	for(;;)
 	{
-TOP:
-
-		if(Present_Module_Mode != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
+		if(Previous_Module_Mode != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
 		{
-			Present_Module_Mode = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled;
+			Previous_Module_Mode = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled;
+			DEBUG_INFO("========================================\n");
+			DEBUG_INFO("4G module : [ %s ]\n", (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled?"Enable":"Disable"));
 		}
 
-		switch(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
+		if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
 		{
-			case ENABLE_4G:
-
-				if(Previous_Module_Mode != Present_Module_Mode)
-				{
-					Previous_Module_Mode = Present_Module_Mode;
-					DEBUG_INFO("4G module : [ Enable ].\n");
-				}
-
-				if(((Dongle.Model = Check4GModem()) != FAIL))
-				{
-					ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
+			// Enable 4G
+			switch(connectStep)
+			{
+				case CONNECT_STEP_READ_MODULE_INFO:
+					if(preConnectStep != connectStep)
+					{
+						preConnectStep = connectStep;
+						DEBUG_INFO("----------------------------------------\n");
+						DEBUG_INFO("Into CONNECT_STEP_READ_MODULE_INFO.\n");
+					}
 
-					Dongle.cnt_SearchModuleFail = 0;
-					if(isReadInfo() == PASS)
+					if(isReadInfo(connectStep) == PASS)
 					{
 						if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME) != 0)
 							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName));
@@ -1360,318 +1394,323 @@ TOP:
 							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
 						memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
 
-						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = csq2rssi(Dongle.CSQ);
-						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
-
-						DEBUG_INFO("========================================\n");
-						DEBUG_INFO("Status: Device info readable...\n");
-						DEBUG_INFO("========================================\n");
 						DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
 						DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
 						DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
 						DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
-						DEBUG_INFO("Device RSSI: %d dBm (CSQ:%d)\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi, Dongle.CSQ);
-						DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
-						DEBUG_INFO("Device Act: %s\n", Dongle.act);
-						DEBUG_INFO("Device Band: %s\n", Dongle.band);
-						DEBUG_INFO("Device Channel: %d\n", Dongle.channel);
-						DEBUG_INFO("========================================\n");
-						
-						Dongle.cnt_ReadInfoFail = 0;
-						if(isReadSimInfo() == PASS)
+
+						Dongle.cnt_ReadSimInfoFail = 0;
+						connectStep = CONNECT_STEP_READ_SIM_INFO;
+					}
+					else
+					{
+						DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
+
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
+
+						Dongle.MODE = NET_MODE_NO_SERVICE;
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
+						memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
+
+						Dongle.cnt_ReadInfoFail++;
+						if(Dongle.cnt_ReadInfoFail > 3)
 						{
-							ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
+							system("killall 4GDetection");
+							system("killall pppd");
+							sleep(2);
+
+							Dongle.cnt_ReadInfoFail = 0;
+							rstModule();
+							connectStep = CONNECT_STEP_CHECK_MODULE;
+						}
 
-							if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID) != 0)
-								memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid));
-							memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
+						delayInterval = CheckModemInfoInterval;
+					}
+					break;
 
-							if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI) != 0)
-								memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
-							memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
+				case CONNECT_STEP_READ_SIM_INFO:
+					if(preConnectStep != connectStep)
+					{
+						preConnectStep = connectStep;
+						DEBUG_INFO("----------------------------------------\n");
+						DEBUG_INFO("Into CONNECT_STEP_READ_SIM_INFO.\n");
+					}
 
-							DEBUG_INFO("========================================\n");
-							DEBUG_INFO("Status: SIM card info readable...\n");
-							DEBUG_INFO("========================================\n");
-							DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
-							DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
-							DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
-							DEBUG_INFO("========================================\n");
+					if(isReadSimInfo() == PASS)
+					{
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
+
+						if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID) != 0)
+							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid));
+						memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
+
+						if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI) != 0)
+							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
+						memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
+
+						DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
+						DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
+						DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
+
+						Dongle.cnt_ReadSimInfoFail = 0;
+						connectStep = CONNECT_STEP_CONNECTING;
+					}
+					else
+					{
+						DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
+
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
+
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
+
+						Dongle.cnt_ReadSimInfoFail ++;
+						if(Dongle.cnt_ReadSimInfoFail > 3)
+						{
+							system("killall 4GDetection");
+							system("killall pppd");
+							sleep(2);
 
 							Dongle.cnt_ReadSimInfoFail = 0;
-							if(isPppUp() == PASS)
-							{
-								ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
-								ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
-								
-								DEBUG_INFO("========================================\n");
-								DEBUG_INFO("Status: PPP interface found...\n");
-								DEBUG_INFO("========================================\n");
-								DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
-								DEBUG_INFO("Telecom network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
-								DEBUG_INFO("Apn connnection: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
-								DEBUG_INFO("========================================\n");
+							rstModule();
 
-								Dongle.cnt_pppFail = 0;
-								if(isReachableInternet() == PASS)
-								{
-									ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
-									ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
-									Dongle.cnt_InternetFail = 0;
-									
-									// Update Rssi Value
-									if(CheckSignalRssi() != PASS)
-										DEBUG_INFO("No RSSI\n");
-									else
-										ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = csq2rssi(Dongle.CSQ);
-
-									DEBUG_INFO("========================================\n");
-									DEBUG_INFO("Status: 4G Device connecting...\n");
-									DEBUG_INFO("========================================\n");
-									DEBUG_INFO("Dongle internet valid result: Pass\n");
-									DEBUG_INFO("========================================\n");
-								
-									// Escape to the top
-									if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == DISABLE_4G_MODULE)
-									{
-										goto TOP;
-									}
-
-									sleep(CheckInternetInterval);
-								}
-								else
-								{
-									DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
-
-									ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
-
-									Dongle.cnt_InternetFail++;
-									if((Dongle.cnt_InternetFail > 3) && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
-									{
-										system("killall 4GDetection");
-										system("killall pppd");
-										sleep(2);
-										
-										Dongle.cnt_InternetFail = 0;
-										rstModule();
-									}
-									
-									sleep(DisconnInterval);
-								}
-							}
-							else
+							connectStep = CONNECT_STEP_CHECK_MODULE;
+						}
+
+						delayInterval = CheckSimInterval;
+					}
+					break;
+
+				case CONNECT_STEP_CONNECTING:
+					if(preConnectStep != connectStep)
+					{
+						preConnectStep = connectStep;
+						DEBUG_INFO("----------------------------------------\n");
+						DEBUG_INFO("Into CONNECT_STEP_CONNECTING.\n");
+					}
+
+					if(isPppUp() == PASS)
+					{
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
+
+						DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
+						DEBUG_INFO("Telecom network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
+						DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
+
+						Dongle.cnt_pppFail = 0;
+						connectStep = CONNECT_STEP_CONNECTED;
+					}
+					else
+					{
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
+
+						if(Load4gConfiguration() == FAIL)
+						{
+							DEBUG_ERROR("4G configuration value NG.\n");
+							if(ShmStatusCodeData!=NULL)
 							{
-								ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
-								ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
-
-								if(Load4gConfiguration() == FAIL)
-								{
-									DEBUG_ERROR("4G configuration value NG.\n");
-									if(ShmStatusCodeData!=NULL)
-									{
-										ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
-									}
-								}
-								else
-								{	
-									memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
-									
-									DEBUG_INFO("========================================\n");
-									DEBUG_WARN("PPP interface not found.\n");
-									DEBUG_INFO("========================================\n");
-									DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
-									DEBUG_INFO("Telecom network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
-									DEBUG_INFO("Apn connnection: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
-									DEBUG_INFO("========================================\n");
-
-									Dongle.cnt_pppFail++;
-									if(Dongle.cnt_pppFail > 3)
-									{
-										system("killall 4GDetection");
-										system("killall pppd");
-										sleep(2);
-										
-										Dongle.cnt_pppFail = 0;
-										rstModule();
-									}
-
-									system("killall 4GDetection");
-									system("killall pppd");
-									sleep(2);
-
-									if(Dongle.Model == DONGLE_QUECTEL)
-									{
-										system("/root/ppp/4GDetection /dev/ttyUSB3 &");
-										printf("4GDetection for primary device.\n");
-									}
-									else if(Dongle.Model == DONGLE_THALES)
-									{
-										system("/root/ppp/4GDetection /dev/ttyACM0 &");
-										printf("4GDetection for second device.\n");
-									}
-									else
-									{}
-								}
-
-								sleep(CheckConnectionInterval);
+								ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
 							}
 						}
 						else
 						{
-							DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
-							
-							ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
-							ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
-
-							ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
-							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
-							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
 							memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
 
-							DEBUG_INFO("========================================\n");
-							DEBUG_INFO("Status: Read Sim card info fail...\n");
-							DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
-							DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
-							DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
-							DEBUG_INFO("========================================\n");
+							DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
 
-							Dongle.cnt_ReadSimInfoFail ++;
-							if(Dongle.cnt_ReadSimInfoFail > 3)
+							Dongle.cnt_pppFail++;
+							if(Dongle.cnt_pppFail > 3)
 							{
 								system("killall 4GDetection");
 								system("killall pppd");
 								sleep(2);
-								
-								Dongle.cnt_ReadSimInfoFail = 0;
+
+								Dongle.cnt_pppFail = 0;
 								rstModule();
+
+								connectStep = CONNECT_STEP_CHECK_MODULE;
 							}
 
-							sleep(CheckSimInterval);
+							system("killall 4GDetection");
+							system("killall pppd");
+							sleep(2);
+
+							if(Dongle.Model == DONGLE_QUECTEL)
+							{
+								system("/root/ppp/4GDetection /dev/ttyUSB3 &");
+								printf("4GDetection for primary device.\n");
+							}
+							else if(Dongle.Model == DONGLE_THALES)
+							{
+								system("/root/ppp/4GDetection /dev/ttyACM0 &");
+								printf("4GDetection for second device.\n");
+							}
+							else
+							{}
 						}
+
+						delayInterval = CheckConnectionInterval;
+					}
+					break;
+
+				case CONNECT_STEP_CONNECTED:
+					if(preConnectStep != connectStep)
+					{
+						preConnectStep = connectStep;
+						DEBUG_INFO("----------------------------------------\n");
+						DEBUG_INFO("Into CONNECT_STEP_CONNECTED.\n");
+					}
+
+					if((isReachableInternet() == PASS) && (isReadInfo(connectStep) == PASS))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
+						Dongle.cnt_InternetFail = 0;
+
+						DEBUG_INFO("Dongle internet valid result: Pass\n");
+
+						delayInterval = CheckInternetInterval;
 					}
 					else
 					{
-						DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
-						
-						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
-						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
-						
-						Dongle.MODE = NET_MODE_NO_SERVICE;
-						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
-						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
-						memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
-						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
-						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
-						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
+						DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
 
-						DEBUG_INFO("========================================\n");
-						DEBUG_INFO("Status: Read device info fail...\n");
-						DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
-						DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
-						DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
-						DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
-						DEBUG_INFO("Device RSSI: %d dBm (CSQ:%d)\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi, Dongle.CSQ);
-						DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
-						DEBUG_INFO("========================================\n");
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
 
-						Dongle.cnt_ReadInfoFail++;
-						if(Dongle.cnt_ReadInfoFail > 3)
+						Dongle.cnt_InternetFail++;
+						if((Dongle.cnt_InternetFail > 3) && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
 						{
 							system("killall 4GDetection");
 							system("killall pppd");
 							sleep(2);
-							
-							Dongle.cnt_ReadInfoFail = 0;
+
+							Dongle.cnt_InternetFail = 0;
 							rstModule();
+
+							connectStep = CONNECT_STEP_CHECK_MODULE;
 						}
-						
-						sleep(CheckModemInfoInterval);
+
+						delayInterval = DisconnInterval;
 					}
-				}
-				else
-				{
-					DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
-					
-					ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
-					ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
-					memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
-					memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
-					
-					Dongle.cnt_SearchModuleFail++;
-					if(Dongle.cnt_SearchModuleFail > 3)
+
+					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = csq2rssi(Dongle.CSQ);
+					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
+					DEBUG_INFO("Device RSSI: %d dBm (CSQ:%d)\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi, Dongle.CSQ);
+					DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
+					DEBUG_INFO("Device Act: %s\n", Dongle.act);
+					DEBUG_INFO("Device Band: %s\n", Dongle.band);
+					DEBUG_INFO("Device Channel: %d\n", Dongle.channel);
+					DEBUG_INFO("Device TX counter: %d bytes\n", Dongle.bytesTx);
+					DEBUG_INFO("Device RX counter: %d bytes\n", Dongle.bytesRx);
+					DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++\n");
+
+					break;
+
+				default:
+				case CONNECT_STEP_CHECK_MODULE:
+					if(preConnectStep != connectStep)
 					{
-						DEBUG_ERROR("4G Module was broken.\n");
-						
-						if(isModuleUnbind() == PASS)
-						{
-							isModuleBind();
-						}
+						preConnectStep = connectStep;
+						DEBUG_INFO("----------------------------------------\n");
+						DEBUG_INFO("Into CONNECT_STEP_CHECK_MODULE.\n");
+					}
 
-						ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
+					if(((Dongle.Model = Check4GModem()) != FAIL))
+					{
+						ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
 						Dongle.cnt_SearchModuleFail = 0;
+						connectStep = CONNECT_STEP_READ_MODULE_INFO;
 					}
+					else
+					{
+						DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
 
-					sleep(CheckModemInterval);
-				}
-				break;
-				
-			case DISABLE_4G_MODULE:
-			default:
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
+						ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
+						memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
+						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
 
-				if(Previous_Module_Mode != Present_Module_Mode)
-				{
-					DEBUG_INFO("4G module : [ Disable ].\n");
-					Previous_Module_Mode = Present_Module_Mode;
-				}
+						Dongle.cnt_SearchModuleFail++;
+						if(Dongle.cnt_SearchModuleFail > 3)
+						{
+							DEBUG_ERROR("4G Module was broken.\n");
 
-				ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
-				ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
-				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
-				ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
-				memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
-				memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
-				
-				if(system("pidof -s 4GDetection > /dev/null") == 0)
-				{
-					DEBUG_INFO("4GDetection running, stop it.\n");
-					system("killall 4GDetection");
-				}
+							if(isModuleUnbind() == PASS)
+							{
+								isModuleBind();
+							}
 
-				if(system("pidof -s pppd > /dev/null") == 0)
-				{
-					DEBUG_INFO("pppd running, stop it.\n");
-					system("killall pppd");
-				}
+							ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
+							Dongle.cnt_SearchModuleFail = 0;
+						}
 
-				if(isPppUp() == PASS)
-				{
-					system("ifconfig ppp0 down");
-				}
-				
-				sleep(SystemInterval);
-				
-				break;
-			
+						delayInterval = CheckModemInterval;
+					}
+
+					break;
+			}
+		}
+		else
+		{
+			// Disable 4G
+			ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
+			ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
+			ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
+			ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
+			ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
+			ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
+			ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
+			memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
+			memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
+
+			if(system("pidof -s 4GDetection > /dev/null") == 0)
+			{
+				DEBUG_INFO("4GDetection running, stop it.\n");
+				system("killall 4GDetection");
+			}
+
+			if(system("pidof -s pppd > /dev/null") == 0)
+			{
+				DEBUG_INFO("pppd running, stop it.\n");
+				system("killall pppd");
+			}
+
+			if(isPppUp() == PASS)
+			{
+				system("ifconfig ppp0 down");
+			}
+
+			connectStep = CONNECT_STEP_CHECK_MODULE;
+			delayInterval = SystemInterval;
 		}
+
+		sleep(delayInterval);
 	}
 
 	return 0;