Beispiel #1
0
/// @cond debug
int cHTTPRequest()
{
	char cmdReply[200];
	char msg2send[200];
	int resCheck = 0;
	DWORD tick;
	int countData;
	int chars2read;
	int msgLength = 0;
        
	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);

            if(httpParams != HTTP_NO_PARAM)
            {
                msgLength += strlen(httpParams);
            }

            // ----------	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 = 75;// 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: ");
                
				int tokenPos;
				for(tokenPos = 0; tokenPos < strlen(httpReqUrl); tokenPos++)
				{
					if(httpReqUrl[tokenPos] == '/')
					{
						break;
					}
					GSMWriteCh(httpReqUrl[tokenPos]);
				}
				
                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\r\n");
            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(GSMBufferSize() > 0)
            {
                // Parse Unsol Message
                mainGSMStateMachine = SM_GSM_CMD_PENDING;
                return -1;
            }
            // 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 = 75;// 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\r\n");
            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;
            }
            smInternal++; // exit from the switch!
            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:
            // Check if Buffer is free
            if(GSMBufferSize() > 0)
            {
                // Parse Unsol Message
                mainGSMStateMachine = SM_GSM_CMD_PENDING;
                return -1;
            }
            else
                smInternal++;
            break;

    }// switch(smInternal)
    
	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
//****************************************************************************
//	Only internal use:
//	cTCPWrite callback function
//****************************************************************************
int cTCPWrite()
{
	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 Write Command ----------
			sprintf(msg2send, "AT+KTCPSND=%d,%d\r",xSocket->number, tcpWriteBufferCount);
			
			GSMWrite(msg2send);
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 120;
			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
			{
				int counterLen = 0;
				
				while(counterLen < tcpWriteBufferCount) 
				{
					GSMWriteCh(tcpWriteBuffer[counterLen]);
					counterLen++;
				}		
				
				// 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;
			}
			
		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;
}