#include "4G_EC200_Handle.h" #include "stdint.h" #include "stdio.h" #include "usart.h" quectel_handle_t quectel_handle; void Quectel_SendData(char *data, uint16_t datalen) { printf("QUECTEL Send(%dB)->%s\n\r", datalen, data); RS232_Uart2_Write(data, datalen); RS232_Uart2_Write("\r\n", 2); } int Quectel_Send_Compare(char *SendData, char *RecData, int timeout) { // uint32_t start_time; int ret = -1; // // Wait_Timeout_Reload(&start_time); // RS232_Uart4_Write((uint8_t *)SendData, strlen(SendData)); // while (1) // { // if (QUECTEL_RecvData_Flag) // { // if (strstr((char *)QUECTEL_RecvBuff, RecData)) // ret = 0; // break; // } // if (Check_Wait_Timeout(&start_time, 0, timeout) == 0) // break; // } // // QUECTEL_Clear_RecvFlag(); // QUECTEL_Clear_RecvData(); return ret; } //int Quectel_AT_Resp(char *pDataBuff, uint16_t pLen, char *except) //{ //// uint16_t len; //// ////// pLen = USART2_RX_STA; ////// //// //// if(pDataBuff[pLen-1] != 0) pDataBuff[pLen] = 0; //// DEPRINT("QUECTEL Recv(%dB)->%s",len, *pDataBuff); //// if(strstr((const char*)(*pDataBuff), except) != NULL) //// { //// printf("%s ·µ»Ø³É¹¦!",except); //// return 0; //// } // return -1; //} void Quectel_Reboot(quectel_handle_t *phandle) { // static uint8_t state = 0; // // if (state == 0) // { // state = 1; //// WIFI_4G_PWR_OFF; // Wait_Timeout_Reload(&phandle->timeout); // } // else if (state == 1) // { // if (Check_Wait_Timeout(&phandle->timeout, 0, 5000+phandle->reboot_cnt%400*5000) == 0) // { // state = 2; //// WIFI_4G_PWR_ON; // phandle->reboot_cnt++; // Wait_Timeout_Reload(&phandle->timeout); // } // } // else if (state == 2) // { // if (Check_Wait_Timeout(&phandle->timeout, 0, 10000) == 0) // { // state = 0; // phandle->at_cmd = QUECTEL_CMD_AT; // phandle->module_state = ATCMD_STATE_INIT_SEND; // } // } } static QUECTEL_ATCMD last_atcmd = QUECTEL_CMD_AT; void Quectel_Init_Send(quectel_handle_t *phandle, char *apn) { // uint8_t repeat_max_cnt = 3; // char *pdata, atbuf[60]; // // switch (phandle->at_cmd) { // case QUECTEL_CMD_AT: // Quectel_SendData("AT", 2); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // break; // case QUECTEL_CMD_ATE: // Quectel_SendData("ATE0", 4); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // break; // case QUECTEL_CMD_CMEE: // Quectel_SendData("AT+CMEE=2", 9); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // break; // case QUECTEL_CMD_CPIN: //// repeat_max_cnt = 60; // if (Check_Wait_Timeout(&phandle->period_timeout, 0, 5000) == 0) // { // pdata = "AT+CPIN?"; // Quectel_SendData(pdata, strlen(pdata)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // } // break; // case QUECTEL_CMD_CREG: //// repeat_max_cnt = 60; // if (Check_Wait_Timeout(&phandle->period_timeout, 0, 5000) == 0) // { // pdata = "AT+CREG?"; // Quectel_SendData(pdata, strlen(pdata)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // } // break; // case QUECTEL_CMD_CGREG: //// repeat_max_cnt = 60; // if (Check_Wait_Timeout(&phandle->period_timeout, 0, 5000) == 0) // { // pdata = "AT+CGREG?"; // Quectel_SendData(pdata, strlen(pdata)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // } // break; // case QUECTEL_CMD_CGDCONT: // if (!strcmp(apn, "default") || !strcmp(apn, "Default")) // sprintf(atbuf, "%s", "AT+QICSGP=1,1,\"\",\"\",\"\",1"); // else // sprintf(atbuf, "AT+QICSGP=1,1,\"%s\",\"\",\"\",1", apn); // Quectel_SendData(atbuf, strlen(atbuf)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 3000); // break; // case QUECTEL_CMD_CGATT: // pdata = "AT+QIACT=1"; // Quectel_SendData(pdata, strlen(pdata)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_INIT_WAIT, 20000); // break; // default: // phandle->at_cmd = QUECTEL_CMD_AT; // break; // } // // if (last_atcmd!= phandle->at_cmd) // { // phandle->repeat_cnt = 0; // last_atcmd = phandle->at_cmd; // } //// if (phandle->repeat_cnt >= repeat_max_cnt) // if (phandle->repeat_cnt >= 60) // phandle->module_state = ATCMD_STATE_REBOOT; } void Quectel_Init_Resp(quectel_handle_t *phandle) { char *pdata, *pRxBuff; uint16_t cnt; // phandle->module_state = ATCMD_STATE_INIT_SEND; // switch (phandle->at_cmd) // { // case QUECTEL_CMD_AT: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // phandle->at_cmd = QUECTEL_CMD_ATE; // break; // case QUECTEL_CMD_ATE: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // phandle->at_cmd = QUECTEL_CMD_CMEE; // break; // case QUECTEL_CMD_CMEE: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // phandle->at_cmd = QUECTEL_CMD_CPIN; // break; // case QUECTEL_CMD_CPIN: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) { // if (strstr((const char*)pRxBuff, "READY")) // phandle->at_cmd = QUECTEL_CMD_CREG; // } else { // if (strstr((const char*)pRxBuff, "SIM not inserted")) // phandle->repeat_cnt = 0; // } // break; // case QUECTEL_CMD_CREG: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // pdata = strstr((const char*)pRxBuff, "+CREG:"); //// if(pdata) phandle->NetStatus = (QUECTEL_NETSTATUS)GSM_StringToDec(&pdata[9], 1); // if (phandle->NetStatus == QUECTEL_NET_YES || phandle->NetStatus == QUECTEL_NET_ROA) // phandle->at_cmd = QUECTEL_CMD_CGREG; // } // break; // case QUECTEL_CMD_CGREG: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // pdata = strstr((const char*)pRxBuff, "+CGREG:"); // if(pdata) phandle->GPRSStatus = (QUECTEL_NETSTATUS)GSM_StringToDec(&pdata[10], 1); // if (phandle->GPRSStatus == QUECTEL_NET_YES || phandle->GPRSStatus == QUECTEL_NET_ROA) // phandle->at_cmd = QUECTEL_CMD_CGDCONT; // } // break; // case QUECTEL_CMD_CGDCONT: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // phandle->at_cmd = QUECTEL_CMD_CGATT; // break; // case QUECTEL_CMD_CGATT: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // phandle->at_cmd = QUECTEL_CMD_AT; // } // else if (strstr((char*)pRxBuff, "ERROR")) // { // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // phandle->at_cmd = QUECTEL_CMD_AT; // } // break; // default:break; // } // QUECTEL_Clear_RecvFlag(); // QUECTEL_Clear_RecvData(); } void Quectel_TCP_Connect(quectel_handle_t *phandle, char *remote_addr, int port) { // char *pdata; // char atbuf[100]; // uint8_t repeat_max_cnt = 3; // // switch (phandle->at_cmd) // { // case QUECTEL_CMD_AT: // Quectel_SendData("AT", 2); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 3000); // break; // case QUECTEL_CMD_ENTER_AT: // DEPRINT("QUECTEL Send(%dB)->%s", 3, "+++"); // QUECTEL_Send("+++", 3); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 3000); // break; // case QUECTEL_CMD_QIOPEN: // if (OCPPCmdObjs.OCPP_Update_Accepted == 1) // sprintf(atbuf, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,2", OCPPCmdObjs.update_file_address, OCPPCmdObjs.update_file_port); // else // sprintf(atbuf, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,2", remote_addr, port); // Quectel_SendData(atbuf, strlen(atbuf)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 30000); // break; // case QUECTEL_CMD_QICLOSE: // Quectel_SendData("AT+QICLOSE=0", 12); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 3000); // break; // case QUECTEL_CMD_QISTATE: // repeat_max_cnt = 60; // if (Check_Wait_Timeout(&phandle->period_timeout, 0, 5000) == 0) // { // if (phandle->repeat_cnt <= repeat_max_cnt) // { // phandle->repeat_cnt++; // pdata = "AT+QISTATE=1,0"; // Quectel_SendData(pdata, strlen(pdata)); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 3000); // } // else // { // phandle->repeat_cnt = 0; // phandle->module_state = ATCMD_STATE_REBOOT; // } // } // break; // case QUECTEL_CMD_ATO: // Quectel_SendData("ATO", 3); // QUECTEL_SET(phandle, phandle->at_cmd, ATCMD_STATE_TCP_WAIT, 3000); // break; // default: // phandle->at_cmd = QUECTEL_CMD_AT; // break; // } } void Quectel_TCP_Connect_Resp(quectel_handle_t *phandle) { // char *pRxBuff; // uint16_t cnt; // // SIM_NetStateFlag = 0; // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // // switch (phandle->at_cmd) // { // case QUECTEL_CMD_AT: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // if (OCPPCmdObjs.OCPP_Update_Accepted == 1) // phandle->at_cmd = QUECTEL_CMD_QICLOSE; // else // phandle->at_cmd = QUECTEL_CMD_QIOPEN; // } // break; // case QUECTEL_CMD_ENTER_AT: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // if (OCPPCmdObjs.OCPP_Update_Accepted == 1) // phandle->at_cmd = QUECTEL_CMD_QICLOSE; // else // phandle->at_cmd = QUECTEL_CMD_QISTATE; // } // break; // case QUECTEL_CMD_QIOPEN: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "CONNECT") == 0) // { // SIM_NetStateFlag = 1; // phandle->module_state = ATCMD_STATE_TCP_LOOP; // } // else // phandle->at_cmd = QUECTEL_CMD_QISTATE; // break; // case QUECTEL_CMD_QICLOSE: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // phandle->at_cmd = QUECTEL_CMD_QIOPEN; // else // phandle->module_state = ATCMD_STATE_REBOOT; // break; // case QUECTEL_CMD_QISTATE: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "OK") == 0) // { // if (strstr(pRxBuff, "TCP")) // phandle->at_cmd = QUECTEL_CMD_ATO; // } // break; // case QUECTEL_CMD_ATO: // if (Quectel_AT_Resp(&pRxBuff, &cnt, "CONNECT") == 0) // { // SIM_NetStateFlag = 1; // phandle->module_state = ATCMD_STATE_TCP_LOOP; // } // break; // default:break; // } // // if (strstr(pRxBuff, "NO CARRIER")) // { // SIM_NetStateFlag = 0; // phandle->at_cmd = QUECTEL_CMD_QICLOSE; // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // } // QUECTEL_Clear_RecvFlag(); // QUECTEL_Clear_RecvData(); } void Quectel_Reset_Connect(void) { // SIM_NetStateFlag = 0; // quectel_handle.reboot_cnt = 0; // quectel_handle.module_state = ATCMD_STATE_REBOOT; } void Quectel_Process(quectel_handle_t *phandle, char *apn, char *addr, int port) { // switch (phandle->module_state) // { // case ATCMD_STATE_INIT_SEND: // Quectel_Init_Send(phandle, apn); // break; // case ATCMD_STATE_INIT_RECV: // Quectel_Init_Resp(phandle); // break; // case ATCMD_STATE_INIT_WAIT: // if (QUECTEL_RecvData_Flag == 1) { // QUECTEL_RecvData_Flag = 0; // phandle->timeout_cnt = 0; // phandle->module_state = ATCMD_STATE_INIT_RECV; // break; // } // if (Check_Wait_Timeout(&phandle->timeout, 0, 3000) == 0) { // phandle->timeout_cnt++; // phandle->module_state = ATCMD_STATE_INIT_SEND; // } // if (phandle->timeout_cnt >= 3) { // phandle->timeout_cnt = 0; // phandle->module_state = ATCMD_STATE_REBOOT; // } // break; // case ATCMD_STATE_TCP_CONNECT: // Quectel_TCP_Connect(phandle, addr, port); // break; // case ATCMD_STATE_TCP_CONNECT_RECV: // Quectel_TCP_Connect_Resp(phandle); // break; // case ATCMD_STATE_TCP_WAIT: // if (QUECTEL_RecvData_Flag == 1) { // QUECTEL_RecvData_Flag = 0; // phandle->timeout_cnt = 0; // phandle->module_state = ATCMD_STATE_TCP_CONNECT_RECV; // break; // } // if (Check_Wait_Timeout(&phandle->timeout, 0, phandle->wait_time) == 0) { // phandle->timeout_cnt++; // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // } // if (phandle->timeout_cnt >= 3) { // phandle->timeout_cnt = 0; // phandle->module_state = ATCMD_STATE_REBOOT; // if (phandle->at_cmd == QUECTEL_CMD_ENTER_AT) // { // phandle->at_cmd = QUECTEL_CMD_QISTATE; // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // } // } // break; // case ATCMD_STATE_TCP_LOOP: // if (QUECTEL_RecvData_Flag == 1) // { // QUECTEL_Clear_RecvFlag(); // if (strstr((char*)QUECTEL_RecvBuff, "NO CARRIER")) // SendDataTimeOutCount = 3; // else if (QUECTEL_Get_RecvBuff_Len() >= 2) // IRQFlags.s1_RecvFlag_Irq = 1; // } // // if (SIM_NetStateFlag == 0) // { // phandle->reboot_cnt = 0; // phandle->at_cmd = QUECTEL_CMD_ENTER_AT; // phandle->module_state = ATCMD_STATE_TCP_CONNECT; // } // break; // case ATCMD_STATE_REBOOT: // Quectel_Reboot(phandle); // break; // default: // phandle->module_state = ATCMD_STATE_INIT_SEND; // break; // } } static QUECTEL_ATCMD EC200_atcmd = QUECTEL_CMD_AT; void Quectel_Init(void) { uint8_t *pdata = NULL; switch (EC200_atcmd) { case QUECTEL_CMD_AT: Quectel_SendData("AT", 2); break; case QUECTEL_CMD_ATE: Quectel_SendData("ATE0", 4); break; case QUECTEL_CMD_CMEE: Quectel_SendData("AT+CMEE=2", 9); break; case QUECTEL_CMD_CPIN: // repeat_max_cnt = 60; pdata = "AT+CPIN?"; Quectel_SendData(pdata, strlen(pdata)); break; case QUECTEL_CMD_CREG: pdata = "AT+CREG?"; Quectel_SendData(pdata, strlen(pdata)); break; case QUECTEL_CMD_CGREG: pdata = "AT+CGREG?"; Quectel_SendData(pdata, strlen(pdata)); break; case QUECTEL_CMD_CGDCONT: // if (!strcmp(apn, "default") || !strcmp(apn, "Default")) // sprintf(atbuf, "%s", "AT+QICSGP=1,1,\"\",\"\",\"\",1"); // else // sprintf(atbuf, "AT+QICSGP=1,1,\"%s\",\"\",\"\",1", apn); // Quectel_SendData(atbuf, strlen(atbuf)); break; case QUECTEL_CMD_CGATT: pdata = "AT+QIACT=1"; Quectel_SendData(pdata, strlen(pdata)); break; default: break; } } void Quectel_InitAtcmd_Response(void); void Quectel_Test_AtCMD(void) { static uint32_t timer = 0; Quectel_InitAtcmd_Response(); if(TickOut(&timer, 2000)==TRUE) { Quectel_Init(); TickOut(&timer, 0); } } int Quectel_AT_Resp(char *pDataBuff, uint16_t pLen, char *except) { uint16_t len; // pLen = USART2_RX_STA; if(pDataBuff[pLen-1] != 0) pDataBuff[pLen] = 0; printf("QUECTEL Recv(%dB)->%s",len, pDataBuff); if(strstr((const char*)(pDataBuff), except) != NULL) { printf("%s Return Success!",except); return 0; } return -1; } void Quectel_InitAtcmd_Response(void) { char *pdata; uint16_t cnt,i; char * pRxBuff = USART2_RX_BUF; if(USART2_RX_FLAG ==1) { cnt = USART2_RX_STA; if(USART2_RX_STA > USART2_REC_LEN )USART2_RX_STA = USART2_REC_LEN; // for(i =0;iNetStatus = (QUECTEL_NETSTATUS)GSM_StringToDec(&pdata[9], 1); // if (phandle->NetStatus == QUECTEL_NET_YES || phandle->NetStatus == QUECTEL_NET_ROA) // EC200_atcmd = QUECTEL_CMD_CGREG; } break; case QUECTEL_CMD_CGREG: if (Quectel_AT_Resp(pRxBuff, cnt, "OK") == 0) { // pdata = strstr((const char*)pRxBuff, "+CGREG:"); // if(pdata) phandle->GPRSStatus = (QUECTEL_NETSTATUS)GSM_StringToDec(&pdata[10], 1); // if (phandle->GPRSStatus == QUECTEL_NET_YES || phandle->GPRSStatus == QUECTEL_NET_ROA) // EC200_atcmd = QUECTEL_CMD_CGDCONT; } break; case QUECTEL_CMD_CGDCONT: if (Quectel_AT_Resp(pRxBuff, cnt, "OK") == 0) EC200_atcmd = QUECTEL_CMD_CGATT; break; case QUECTEL_CMD_CGATT: if (Quectel_AT_Resp(pRxBuff, cnt, "OK") == 0) { // phandle->module_state = ATCMD_STATE_TCP_CONNECT; EC200_atcmd = QUECTEL_CMD_AT; } else if (strstr((char*)pRxBuff, "ERROR")) { // phandle->module_state = ATCMD_STATE_TCP_CONNECT; EC200_atcmd = QUECTEL_CMD_AT; } break; default:break; } memset(USART2_RX_BUF,0,sizeof(USART2_RX_BUF)); // QUECTEL_Clear_RecvFlag(); // QUECTEL_Clear_RecvData(); }