Exemplo n.º 1
0
static int ClientSendCommandByIp(EmCmdCode emCmdCode, void *pDataBuf,
                             unsigned int u32DataSize, char *pDevIP,
                             unsigned short u16DevPort,
                             bool boIsRespond,  bool boIsEcho,
                             bool boIsReturnSock)
{
    int sock;
    StMsgPkg stMsg, stEchoMsg;

    //给u8DataBuffer字段复制,并计算校验值
    BuildMsg(&stMsg, emCmdCode, pDataBuf, u32DataSize);

    //发送数据到服务器,并接收服务器返回的数据
    sock = TcpClientCommServer(pDevIP, u16DevPort, (char *)&stMsg,
                               sizeof(StMsgPkg), 3000000, boIsRespond,
                               (char *)&stEchoMsg, sizeof(StMsgPkg));
    if (sock < 0)
    {
        LOGERR("TcpClientCommServer error\n");
        return -1;
    }


    if (boIsRespond == TRUE)
    {
        //判断echo是否正确
        if(CheckEcho(&stEchoMsg, emCmdCode))
        {
            LOGERR("CheckEcho error\n");
            return -1;
        }

        if (boIsEcho == TRUE)
        {
            if(u32DataSize == stEchoMsg.u32DataLength)
            {
                memcpy(pDataBuf, stEchoMsg.uoData.u8DataBuffer, u32DataSize);
            }
            else
            {
                LOGERR("receive message data length error\n");
                return -1;
            }
        }
    }

    if (boIsReturnSock == TRUE)
    {
        return sock;
    }
    else
    {
        close(sock);
        return 0;
    }
}
Exemplo n.º 2
0
/// @cond debug
//****************************************************************************
//	Only internal use:
//	cAPNConfig callback function
//****************************************************************************
int cAPNConfig()
{
    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
        // ----------	APN Configuration	----------
        sprintf(msg2send, 	"AT+KCNXCFG=0,\"GPRS\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\r",
                xApn, xLogin, xPassw, xIp, xDns1, xDns2);

        GSMWrite(msg2send);
        // Start timeout count
        tick = TickGetDiv64K(); // 1 tick every seconds
        maxtimeout = 2 + 3;
        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\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;
        }
        else
        {
            vTaskDelay(200); // To prevent too fast TCP connection attempt
        }

    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;
}
Exemplo n.º 3
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;
}	
Exemplo n.º 4
0
/// @cond debug
int  cSMTPParamsClear()
{
    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+KSMTPCLEAR\r");

        GSMWrite(msg2send);
        // 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;
        }

    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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
/// @cond debug
int  cSMTPParamsSet()
{
    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
        // AT&K3
        sprintf(msg2send, "AT&K3\r");

        GSMWrite(msg2send);
        // 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;
        }

    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:
        // AT+KCNXTIMER
        sprintf(msg2send, "AT+KCNXTIMER=0,60,2,70\r");

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

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

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

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 6:
        // 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 7:
        // AT+CGATT=1
        sprintf(msg2send, "AT+CGATT=1\r");

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

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

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

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 9:
        // 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 10:
        // ----------	SMTP Parameter Configuration	----------
        sprintf(msg2send, "AT+KSMTPPARAM=\"%s\",%d,\"%s\"\r",smtpdomain, smtpport, smtpsender);

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

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

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

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 12:
        /*
        // Get reply "+KSMTPPARAM: <socket>"
        vTaskDelay(2);
        sprintf(msg2send, "+KSMTPPARAM");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>+KSMTPPARAM: <socket><CR><LF>

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

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
        	return mainOpStatus.ErrorCode;
        }
        */
        smInternal++;

    case 13:
        // 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 14:
        // ----------	SMTP Login Configuration	----------
        sprintf(msg2send, "AT+KSMTPPWD=\"%s\",\"%s\"\r",smtplogin, smtppassw);

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

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

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

        CheckErr(resCheck, &smInternal, &tick);

        if(resCheck)
        {
            return mainOpStatus.ErrorCode;
        }

    case 16:
        /*
        // Get reply "+KSMTPPWD: <socket>"
        vTaskDelay(2);
        sprintf(msg2send, "+KSMTPPWD");
        chars2read = 2;
        countData = 2; // GSM buffer should be: <CR><LF>+KSMTPPARAM: <socket><CR><LF>

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

        CheckErr(resCheck, &smInternal, &tick);

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

        }
        */
        smInternal++;

    case 17:
        // 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;
}
Exemplo n.º 8
0
/// @cond debug
//****************************************************************************
//	Only internal use:
//	cTCPStatus callback function
//****************************************************************************
int cTCPStatus()
{
	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)
			{
				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 +KTCP_DATA 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 +KTCP_DATA 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 +KTCP_DATA 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;
			}
			
		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;
}
Exemplo n.º 9
0
static int ClientSendCommand(EmCmdCode emCmdCode, void *pDataBuf,
                             unsigned int u32DataSize, int s32LoginID,
                             bool boIsRespond,  bool boIsEcho, bool boIsReturnSock)
{
    int ret, sock;
    StMsgPkg stMsg, stEchoMsg;
    stLoginInfo Login;
    unsigned short port;

    ret = GetQueueElem(&s_UserLoginInfoQueue, s32LoginID, &Login);
    if (ret < 0)
    {
        LOGERR("please login\n");
        return -1;
    }

    memset(&stMsg, 0, sizeof(StMsgPkg));
    stMsg.stAccount = Login.stUserAccount;

    //给u8DataBuffer字段复制,并计算校验值
    BuildMsg(&stMsg, emCmdCode, pDataBuf, u32DataSize);

    if (emCmdCode == _CmdPlayCtrl)
    {
        port = STREAM_SERVER_DEFAULT_PORT;
    }
    else
    {
        port = Login.u16DevPort;
    }

    //发送数据到服务器,并接收服务器返回的数据
    sock = TcpClientCommServer(Login.pDevIP, port, (char *)&stMsg,
                               sizeof(StMsgPkg), 3000000, boIsRespond,
                               (char *)&stEchoMsg, sizeof(StMsgPkg));
    if (sock < 0)
    {
        LOGERR("TcpClientCommServer error\n");
        return -1;
    }


    if (boIsRespond == TRUE)
    {
        //判断echo是否正确
        if(CheckEcho(&stEchoMsg, emCmdCode))
        {
            LOGERR("CheckEcho error\n");
            return -1;
        }

        if (boIsEcho == TRUE)
        {
            if(u32DataSize == stEchoMsg.u32DataLength)
            {
                memcpy(pDataBuf, stEchoMsg.uoData.u8DataBuffer, u32DataSize);
            }
            else
            {
                LOGERR("receive message data length error\n");
                return -1;
            }
        }
    }

    if (boIsReturnSock == TRUE)
    {
        return sock;
    }
    else
    {
        close(sock);
        return 0;
    }
}
Exemplo n.º 10
0
/// @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;
}
Exemplo n.º 11
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;
}
Exemplo n.º 12
0
/// @cond debug
//****************************************************************************
//	Only internal use:
//	cTCPClientClose callback function
//****************************************************************************
int cTCPClientClose()
{
	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 Connection Close	----------
			sprintf(msg2send, "AT+KTCPCLOSE=%d,1\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)
			{
				return mainOpStatus.ErrorCode;
			}
			
		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:	
			// Send AT command
			// ----------	Delete TCP Socket ----------
			sprintf(msg2send, "AT+KTCPDEL=%d\r",xSocket->number);
			
			GSMWrite(msg2send);
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 2;
			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 (\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;
			}
			else
			{
				xSocket->number = INVALID_SOCKET;
			}
				
		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;
}
Exemplo n.º 13
0
/// @cond debug
//****************************************************************************
//	Only internal use:
//	cTCPClientOpen callback function
//****************************************************************************
int cTCPClientOpen()
{
	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 Connection Configuration	----------
			sprintf(msg2send, "AT+KTCPCFG=0,0,\"%s\",%d\r",xIPAddress, xTCPPort);
			
			GSMWrite(msg2send);
			// 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;
			}
			
		case 3:
			// Get reply "+KTCPCFG: <socket>"
			vTaskDelay(1);
			sprintf(msg2send, "+KTCPCFG");
			chars2read = 2;
			countData = 2; // GSM buffer should be: <CR><LF>+KTCPCFG: <socket><CR><LF>
			
			resCheck = CheckCmd(countData, chars2read, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			else
			{
				// Get socket number
				char temp[25];
				int res = getfield(':', '\r', 5, 1, cmdReply, temp, 500);
				if(res != 1)
				{
					// Execute Error Handler
					gsmDebugPrint( "Error in getfield\r\n");
					break;
				}
				else
				{
					xSocket->number = 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:	
			// ----------	Initiate TCP Connection	----------
			sprintf(msg2send, "AT+KTCPCNX=%d\r",xSocket->number);
			
			GSMWrite(msg2send);
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 180;
			smInternal++;
			
		case 6:
			vTaskDelay(1);
			// Check ECHO 
			countData = 0;
			
			resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
						
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				xSocket->number = INVALID_SOCKET;
				return mainOpStatus.ErrorCode;
			}
			
		case 7:
			// Get reply (\r\nOK\r\n)
			vTaskDelay(1);
			// Get OK
			sprintf(msg2send, "\r\nOK");
			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)
			{
				xSocket->number = INVALID_SOCKET;
				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;
}
Exemplo n.º 14
0
/// @cond debug
int cCALLVoiceStart()
{
	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
			sprintf(msg2send, "ATD=\"%s\";\r", mainCall.CallerID);
			GSMWrite(msg2send);
			
			// Start timeout count
			tick = TickGetDiv64K(); // 1 tick every seconds
			maxtimeout = 60;
			smInternal++;
		
		case 2:
			// Check ECHO 
			countData = 0;
			resCheck = CheckEcho(countData, tick, cmdReply, msg2send, maxtimeout);
			
			CheckErr(resCheck, &smInternal, &tick);
			
			if(resCheck)
			{
				return mainOpStatus.ErrorCode;
			}
			
		case 3:
			// Get reply (OK, BUSY, ERROR, etc...)
			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;		
	}
	mainCall.Status = CALL_IN_PROG;
	
	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;
}