Пример #1
0
/// @cond debug
int  cSMTPEmailSend()
{
    char cmdReply[200];
    char msg2send[200];
    int resCheck = 0;
    DWORD tick;
    int countData;
    int chars2read;

    switch(smInternal)
    {
    case 0:
        // Check if Buffer is free
        if(GSMBufferSize() > 0)
        {
            // Parse Unsol Message
            mainGSMStateMachine = SM_GSM_CMD_PENDING;
            return -1;
        }
        else
            smInternal++;

    case 1:
        // AT+KSMTPSUBJECT
        sprintf(msg2send, "AT+KSMTPSUBJECT=\"");

        GSMWrite(msg2send);
        GSMWrite(smtpsubject);
        GSMWrite("\"\r");

        // Start timeout count
        tick = TickGetDiv64K(); // 1 tick every seconds
        maxtimeout = 2;
        smInternal++;

    case 2:
        vTaskDelay(20);
        // Check ECHO
        countData = 0;

        resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }
        else
        {
            int flushNum = strlen(smtpsubject) + 2;
            while(flushNum > 0)
            {
                GSMRead(cmdReply, 1);
                flushNum--;
            }
            cmdReply[0] = '\0';
        }

    case 3:
        // Get reply (\r\nOK\r\n)
        vTaskDelay(1);
        // Get OK
        sprintf(msg2send, "OK");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
        resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 4:
        maxtimeout = 60; //just to prevent hardware lock... it should be less since depends on data size
        // AT+KSMTPUL=1,<size> -> with "1,<size>" it is specified to use normal mode
        int smtpmsglen = strlen(smtpmsg);
        sprintf(msg2send, "AT+KSMTPUL=1,%d\r",smtpmsglen);

        GSMWrite(msg2send);
        // Start timeout count
        tick = TickGetDiv64K(); // 1 tick every seconds

        smInternal++;

    case 5:
        vTaskDelay(20);
        // Check ECHO
        countData = 0;

        resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 6:
        // Get reply "+KSMTPUL: <session_id>"
        vTaskDelay(20);
        sprintf(msg2send, "+KSMTPUL");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>+KSMTPUL: <session_id><CR><LF>

        resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }
        else
        {
            // Get SMPT last session_id:
            char temp[25];
            int res = getfield(':', '\r', 5, 1, cmdReply, temp, 500);
            if(res != 1)
            {
                // Execute Error Handler
                gsmDebugPrint("Error in getfield for +KSMTPUL socket\r\n");
                break;
            }
            else
            {
                smtpLastSessionId = atoi(temp);
            }
        }

    case 7:
        // Get reply "\r\nCONNECT\r\n"
        vTaskDelay(2);
        sprintf(msg2send, "CONNECT\r\n");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>CONNECT<CR><LF>

        resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }
        else
        {
            GSMWrite(smtpmsg);
        }

    case 8:
        // Get reply (\r\nOK\r\n)
        vTaskDelay(20);
        // Get OK
        sprintf(msg2send, "\r\nOK");
        chars2read = 2;
        countData = 0; // GSM buffer should be: <CR><LF>OK<CR><LF>
        resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }


    default:
        break;
    }

    smInternal = 0;
    // Cmd = 0 only if the last command successfully executed
    mainOpStatus.ExecStat = OP_SUCCESS;
    mainOpStatus.Function = 0;
    mainOpStatus.ErrorCode = 0;
    mainGSMStateMachine = SM_GSM_IDLE;
    return -1;
}
/// @cond debug
int cHTTPRequest()
{
	char cmdReply[200];
	char msg2send[200];
	int resCheck = 0;
	DWORD tick;
	int countData;
	int chars2read;
	int msgLength = 0;
	int httpCount = 0;
	char tmp[25];
	const char termString[6] = "\r\n\r\n";
	
	do
	{
		switch(smInternal)
		{
			case 0:
				// Check if Buffer is free
				if(GSMBufferSize() > 0)
				{
					// Parse Unsol Message
					mainGSMStateMachine = SM_GSM_CMD_PENDING;
					return -1;
				}
				else
					smInternal++;
			case 1:
				// Send first AT command
				
				// Calculate tcp write length...
				// Header length:
				if(httpReqType == HTTP_GET)
				{
					msgLength = 131;
				}
				else if(httpReqType == HTTP_POST)
				{
					msgLength = 132;
				}
				
				msgLength += strlen(httpReqUrl);
				
				msgLength += strlen(httpWriteBuffer);
				
				sprintf(msg2send, "%d", strlen(httpWriteBuffer));
				
				msgLength += strlen(msg2send);
				
				// ----------	TCP Write Command ----------
				sprintf(msg2send, "AT+KTCPSND=%d,%d\r",xSocket->number, msgLength);
				
				GSMWrite(msg2send);
				// Start timeout count
				tick = TickGetDiv64K(); // 1 tick every seconds
				maxtimeout = 60;
				smInternal++;
				
			case 2:
				vTaskDelay(20);
				// Check ECHO 
				countData = 0;
				
				resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
							
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 3:
				// Get reply "\r\nCONNECT\r\n"
				vTaskDelay(20);
				sprintf(msg2send, "\r\nCONNECT");
				chars2read = 2;
				countData = 4; // GSM buffer should be: <CR><LF>CONNECT<CR><LF>
				
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				else
				{
					if(httpReqType == HTTP_GET)
					{
						GSMWrite("GET ");
					}	
					else
					{
						GSMWrite("POST ");
					}	
					
					char* req = strstr(httpReqUrl, "/");
					GSMWrite(req);
					GSMWrite(" HTTP/1.1\r\nHost: ");
					req = strtok(httpReqUrl, "/");
					
					GSMWrite(req);
					GSMWrite("\r\nUser-Agent: Flyport-GPRS\r\n");
					if(httpParams != HTTP_NO_PARAM)
					{
						GSMWrite(httpParams);
					}
					else
					{
						GSMWrite("Content-Length: ");
						char contentlength[5];
						sprintf(contentlength, "%d", strlen(httpWriteBuffer));
						GSMWrite(contentlength);
						GSMWrite("\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: */*");
						GSMWrite("\r\n\r\n");
						GSMWrite(httpWriteBuffer);
					}
					
					// and write --EOF--Pattern-- (without \r)
					GSMWrite("--EOF--Pattern--");
				}	
		
			case 4:
				// Get reply (\r\nOK\r\n)
				vTaskDelay(20);
				// Get OK
				sprintf(msg2send, "OK");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 5:
				// Read 1 char at time on Hilo TCP Buffer to get 
				// HTTP Header reply, using GSMpSeek/GSMpRead, etc
				
				// ----------	TCP Status Update ----------
				sprintf(msg2send, "AT+KTCPSTAT=%d\r",xSocket->number);
				
				GSMWrite(msg2send);
				// Start timeout count
				tick = TickGetDiv64K(); // 1 tick every seconds
				maxtimeout = 60;
				smInternal++;
				
			case 6:
				vTaskDelay(20);
				// Check ECHO 
				countData = 0;
				
				resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
							
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 7:
				// Get reply "\r\n+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data>\r\n"
				vTaskDelay(20);
				sprintf(msg2send, "+KTCPSTAT");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data><CR><LF>
				
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				else
				{
					// Get status
					char temp[25];
					int res = getfield(':', ',', 5, 1, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->status = atoi(temp);
					}
					
					// Get tcp_notif
					res = getfield(',', ',', 5, 1, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->notif = atoi(temp);
					}
					
					// Get rcv_data
					res = getfield(',', '\r', 6, 3, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->rxLen = atoi(temp);
					}	
				}	
				
			case 8:
				// Get reply (\r\nOK\r\n)
				vTaskDelay(1);
				// Get OK
				sprintf(msg2send, "OK");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
				// Simone, debug HTTP Request...
				// in this way we only send the request:
				maxHTTPattempt = -2;
				smInternal = 20;
				break;
				
			case 9:
				// check if there are enough BYTEs on TCP Buffer for HTTP Response parsing
				if(xSocket->rxLen < 12)
				{
					// Break operation
					smInternal = 5;
					maxHTTPattempt--;
					break;
				}
				maxHTTPattempt = 10;
				// Send first AT command
				// ----------	TCP Read Command ----------
				httpReadBufferCount = 12;				
				
				sprintf(msg2send, "AT+KTCPRCV=%d,%d\r",xSocket->number, httpReadBufferCount);
				
				GSMWrite(msg2send);
				// Start timeout count
				tick = TickGetDiv64K(); // 1 tick every seconds
				maxtimeout = 60;
				smInternal++;
				
			case 10:
				vTaskDelay(20);
				// Check ECHO 
				countData = 0;
				
				resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
							
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 11:
				// Get reply "\r\nCONNECT\r\n"
				vTaskDelay(2);
				sprintf(msg2send, "CONNECT");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>CONNECT<CR><LF>
				
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 12:
				// Get "HTTP/1.1 "reply:
				// Read data from TCP Socket
				httpReadBufferCount = GSMRead(tmp, 12);
				tmp[12] = '\0';
				// Decrease xSocket->rxLen:
				xSocket->rxLen -= httpReadBufferCount;
				
				char* httpReplyVal = strstr(tmp, "HTTP/1.1 ");
				if(httpReplyVal != NULL)
				{
					httpCount = atoi(&httpReplyVal[9]);
				}
				xHTTPCode = httpCount;
				httpCount = 0;
				smInternal++;
				
			case 13:
				// Now flush all the TCP Data until the first \r\n\r\n
						
				// ----------	TCP Status Update ----------
				sprintf(msg2send, "AT+KTCPSTAT=%d\r",xSocket->number);
				
				GSMWrite(msg2send);
				// Start timeout count
				tick = TickGetDiv64K(); // 1 tick every seconds
				maxtimeout = 60;
				smInternal++;
				
			case 14:
				vTaskDelay(2);
				// Check ECHO 
				countData = 0;
				
				resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
							
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 15:
				// Get reply "\r\n+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data>\r\n"
				vTaskDelay(2);
				sprintf(msg2send, "+KTCPSTAT");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data><CR><LF>
				
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				else
				{
					// Get status
					char temp[25];
					int res = getfield(':', ',', 5, 1, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->status = atoi(temp);
					}
					
					// Get tcp_notif
					res = getfield(',', ',', 5, 1, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint("Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->notif = atoi(temp);
					}
					
					// Get rcv_data
					res = getfield(',', '\r', 6, 3, cmdReply, temp, 500);
					if(res != 1)
					{
						// Execute Error Handler
						gsmDebugPrint("Error in getfield for +KTCPSTAT socket\r\n");
						break;
					}
					else
					{
						xSocket->rxLen = atoi(temp);
					}	
				}	
				
			case 16:
				// Get reply (\r\nOK\r\n)
				//vTaskDelay(1);
				// Get OK
				sprintf(msg2send, "OK");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 17:
				if(xSocket->rxLen == 0)
				{
					smInternal = 13;
					maxHTTPattempt--;
					if(maxHTTPattempt > 0)
						vTaskDelay(20);
					else
						// Break operation
						break;
				}
				else
				{
					// ----------	TCP Read Command ----------
					sprintf(msg2send, "AT+KTCPRCV=%d,1\r",xSocket->number);
					
					GSMWrite(msg2send);
					// Start timeout count
					tick = TickGetDiv64K(); // 1 tick every seconds
					maxtimeout = 60;
					smInternal++;
				}	
				
			case 18:
				//vTaskDelay(2);
				// Check ECHO 
				countData = 0;
				
				resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
							
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				
			case 19:
				// Get reply "\r\nCONNECT\r\n"
				//vTaskDelay(2);
				sprintf(msg2send, "CONNECT");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>CONNECT<CR><LF>
				
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				else
				{
					GSMRead(tmp, 1+22); //our char + "--EOF--Pattern--\r\nOK"
					//UARTWriteCh(1, tmp[0]);
					xSocket->rxLen--;
					if(tmp[0] == termString[httpCount])
					{
						httpCount++;
					}	
					if(httpCount > 4)
					{
						maxHTTPattempt = -2; // we found the termString as desired
						break;
					}	
					else
					{
						smInternal = 13;
					}
				}	
				/*
			case 20:
				// Get reply (\r\n--EOF--Pattern--\r\nOK\r\n)
				vTaskDelay(1);
				// Get OK
				sprintf(msg2send, "--EOF--Pattern--\r\nOK");
				chars2read = 2;
				countData = 2; // GSM buffer should be: <CR><LF>--EOF--Pattern--<CR><LF>OK<CR><LF>
				resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
				
				CheckErr(resCheck, &smInternal, &tick);
				
				if(resCheck)
				{
					return mainOpStatus.ErrorCode;
				}
				*/
				
			default:
				break;
		
		}// switch(smInternal)
	}while(maxHTTPattempt > 0);	
	smInternal = 0;
	// Cmd = 0 only if the last command successfully executed
	mainOpStatus.ExecStat = OP_SUCCESS;
	mainOpStatus.Function = 0;
	mainOpStatus.ErrorCode = 0;
	mainGSMStateMachine = SM_GSM_IDLE;
	return -1;
}	
Пример #3
0
/// @cond debug
int  cSMTPEmailTo()
{
    char cmdReply[350];
    char msg2send[350];
    int resCheck = 0;
    DWORD tick;
    int countData;
    int chars2read;

    switch(smInternal)
    {
    case 0:
        // Check if Buffer is free
        if(GSMBufferSize() > 0)
        {
            // Parse Unsol Message
            mainGSMStateMachine = SM_GSM_CMD_PENDING;
            return -1;
        }
        else
            smInternal++;

    case 1:
        // AT+KSMTPTO
        sprintf(msg2send, "AT+KSMTPTO=\"%s\",",smtpTo1);
        GSMWrite(msg2send);
        GSMWrite("\"");
        GSMWrite(smtpTo2);
        GSMWrite("\",\"");
        GSMWrite(smtpCc1);
        GSMWrite("\",\"");
        GSMWrite(smtpCc2);
        GSMWrite("\"");
        GSMWrite("\r");

        // Start timeout count
        tick = TickGetDiv64K(); // 1 tick every seconds
        maxtimeout = 2;
        smInternal++;

    case 2:
        vTaskDelay(1);
        // Check ECHO
        countData = 0;

        resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }
        else
        {
            int flushNum = 	strlen(smtpTo2) +
                            strlen(smtpCc1) +
                            strlen(smtpCc2) + 10;
            while(flushNum > 0)
            {
                GSMRead(cmdReply, 1);
                flushNum--;
            }
            cmdReply[0] = '\0';
        }

    case 3:
        // Get reply (\r\nOK\r\n)
        vTaskDelay(1);
        // Get OK
        sprintf(msg2send, "OK");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
        resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    default:
        break;
    }

    smInternal = 0;
    // Cmd = 0 only if the last command successfully executed
    mainOpStatus.ExecStat = OP_SUCCESS;
    mainOpStatus.Function = 0;
    mainOpStatus.ErrorCode = 0;
    mainGSMStateMachine = SM_GSM_IDLE;
    return -1;
}
Пример #4
0
/// @cond debug
//****************************************************************************
//	Only internal use:
//	cTCPRead callback function
//****************************************************************************
int cTCPRead()
{
	char cmdReply[200];
	char msg2send[200];
	int resCheck = 0;
	DWORD tick;
	int countData;
	int chars2read;
	
	switch(smInternal)
	{
		case 0:
			// Check if Buffer is free
			if(GSMBufferSize() > 0)
			{
				// Parse Unsol Message
				mainGSMStateMachine = SM_GSM_CMD_PENDING;
				return -1;
			}
			else
				smInternal++;
			
		case 1:	
			// Send first AT command
			// ----------	TCP Status Update ----------
			sprintf(msg2send, "AT+KTCPSTAT=%d\r",xSocket->number);
			
			GSMWrite(msg2send);
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 60;
			smInternal++;
			
		case 2:
			vTaskDelay(20);
			// Check ECHO 
			countData = 0;
			
			resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
						
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				#if defined (STACK_USE_UART)
				char debugStr[100];
				sprintf(debugStr, "error on function %d, smInternal %d\r\n", mainOpStatus.Function, smInternal);
				gsmDebugPrint( debugStr);
				vTaskDelay(20);
				#endif
				return mainOpStatus.ErrorCode;
			}
			
		case 3:
			// Get reply "\r\n+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data>\r\n"
			vTaskDelay(20);
			sprintf(msg2send, "+KTCPSTAT");
			chars2read = 2;
			countData = 2; // GSM buffer should be: <CR><LF>+KTCPSTAT: <status>,<tcp_notif>,<rem_data>,<rcv_data><CR><LF>
			
			resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			else
			{
				// Get status
				char temp[25];
				int res = getfield(':', ',', 5, 1, cmdReply, temp, 500);
				if(res != 1)
				{
					// Execute Error Handler
					gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
					break;
				}
				else
				{
					xSocket->status = atoi(temp);
				}
				
				// Get tcp_notif
				res = getfield(',', ',', 5, 1, cmdReply, temp, 500);
				if(res != 1)
				{
					// Execute Error Handler
					gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
					break;
				}
				else
				{
					xSocket->notif = atoi(temp);
				}
				
				// Get rcv_data
				res = getfield(',', '\r', 6, 3, cmdReply, temp, 500);
				if(res != 1)
				{
					// Execute Error Handler
					gsmDebugPrint( "Error in getfield for +KTCPSTAT socket\r\n");
					break;
				}
				else
				{
					xSocket->rxLen = atoi(temp);
				}
			}	
			
		case 4:
			// Get reply (\r\nOK\r\n)
			vTaskDelay(1);
			// Get OK
			sprintf(msg2send, "OK");
			chars2read = 2;
			countData = 2; // GSM buffer should be: <CR><LF>OK<CR><LF>
			resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			
		case 5:
			if(xSocket->rxLen == 0)
			{
				// Break operation
				break;
			}
			// Send first AT command
			// ----------	TCP Read Command ----------
			if(tcpReadBufferCount > xSocket->rxLen)
				tcpReadBufferCount = xSocket->rxLen;				
			
			if(tcpReadBufferCount > GSM_BUFFER_SIZE)
					tcpReadBufferCount = GSM_BUFFER_SIZE;
			
			sprintf(msg2send, "AT+KTCPRCV=%d,%d\r",xSocket->number, tcpReadBufferCount);
			
			GSMWrite(msg2send);
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 60;
			smInternal++;
			
		case 6:
			vTaskDelay(1);
			// Check ECHO 
			countData = 0;
			
			resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
						
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			
		case 7:
			// Get reply "\r\nCONNECT\r\n"
			vTaskDelay(1);
			sprintf(msg2send, "CONNECT");
			chars2read = 2;
			countData = 2; // GSM buffer should be: <CR><LF>CONNECT<CR><LF>
			
			resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			else
			{
				// Read data from TCP Socket
				int rxCount = 0;
				int retCount = 0;			
				while(rxCount < tcpReadBufferCount)
				{
					char tmp[1];
					retCount = GSMRead(tmp, 1);
					*(tcpReadBuffer+rxCount) = tmp[0];
					rxCount += retCount;
					if(retCount == 0)
						vTaskDelay(1);
				}
				
				// Set tcpReadBufferCount as the effective number of BYTEs read
				tcpReadBufferCount = rxCount;
			}
		
		case 8:
			// Get reply (--EOF--Pattern--\r\nOK\r\n)
			vTaskDelay(1);
			// Get OK
			sprintf(msg2send, "--EOF--Pattern--\r\nOK");
			chars2read = 2;
			countData = 0; // GSM buffer should be: --EOF--Pattern--<CR><LF>OK<CR><LF>
			resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			
		default:
			break;
	}
	
	smInternal = 0;
	// Cmd = 0 only if the last command successfully executed
	mainOpStatus.ExecStat = OP_SUCCESS;
	mainOpStatus.Function = 0;
	mainOpStatus.ErrorCode = 0;
	mainGSMStateMachine = SM_GSM_IDLE;
	return -1;
}