#include "includes.h" #include "socket.h" int g_iCSQ=-1; signed char Chargeing; YUNYINGSHANG_DEF thisYunYingShang=CHINA_NONE; unsigned char g_ucModemTaskEn=0; unsigned char modemInitStatus=0; unsigned char g_ucCardStatus=0; unsigned char modemInitReady=0;//0模块没开始初始化 1模块初始化成功 2模块初始化失败 static char ChekCardCnt=0; char CardStatue=0x11; unsigned char SIMSelect; unsigned int TTSPlayTick=0; char SendAT[50]; char WaitAT1[30]; char WaitAT2[30]; typedef enum{ MINI_WAT_START, MINI_GET_MODULE, MINI_GET_POC_VER, MINI_GET_POC_CODE, MINI_GET_CARD, MINI_GET_IMEI, MINI_PLY_ICCID, MINI_POC_PARA, MINI_SET_TONES, MINI_READ_SPKLV, MINI_GET_SIMSELECT, MINI_SET_MICLV, MINI_SET_TTSPARM, MINI_SET_GPSPARM, MINI_CHEAK_HEADSET, MINI_POC_AUTHSET, }M_INIT_STATUS; static unsigned char initStatus; static unsigned short timeOut; static unsigned char PocGetTypeCnt=0; char *FirmSperkr="AT+CAIET=0,0,9600,400,\"8080808002003400d3ff000002003400d6ff0000020\ 03400d9ff000002003400dcff000002003400dfff000002003400e2ff000002003400e5ff000002003400e8ff000002003400ebff000002003400eeff000002003400f1ff000002003400f4ff000002003400f7ff000002003400faff000002003400fdff0000020034000000000002003400d3ff000002003400d6ff000002003400d9ff000002003400dcff000002003400dfff000002003400e2ff000002003400e5ff000002003400e8ff000002003400ebff000002003400eeff000002003400f1ff000002003400f4ff000002003400f7ff000002003400faff000002003400fdff0000020034000000000004003400d3ff000004003400d6ff000004003400d9ff000004003400dcff000004003400dfff000004003400e2ff000004003400e5ff000004003400e8ff000004003400ebff000004003400eeff000004003400f1ff000004003400f4ff000004003400f7ff000004003400faff000004003400fdff0000040034000000000004003400d3ff000004003400\"\r\n"; char *FirmSperkr2="AT+CAIET=0,0,10000,372,\"d6ff000004003400d9ff000004003400dcff000004\ 003400dfff000004003400e2ff000004003400e5ff000004003400e8ff000004003400ebff000004003400eeff000004003400f1ff000004003400f4ff000004003400f7ff000004003400faff000004003400fdff0000040034000000000004003400d3ff000004003400d6ff000004003400d9ff000004003400dcff000004003400dfff000004003400e2ff000004003400e5ff000004003400e8ff000004003400ebff000004003400eeff000004003400f1ff000004003400f4ff000004003400f7ff000004003400faff000004003400fdff0000040034000000000004003400d3ff000004003400d6ff000004003400d9ff000004003400dcff000004003400dfff000004003400e2ff000004003400e5ff000004003400e8ff000004003400ebff000004003400eeff000004003400f1ff000004003400f4ff000004003400f7ff000004003400faff000004003400fdff00000400340000000000\"\r\n"; void SpeakTTSVoice(void) { int len,timeout,i; char ttsMessage[20*4]; //SUT_MODEM_AT_MSG *pMsg=&sutAtmPro; unsigned char codeType=ENCODE_UNICODE; if(newPara.ttsMessage[0]==0) return; len=strlen(newPara.ttsMessage); len = len+(8-len%8);//8倍数 if(newPara.ttsCodeType==TTS_UNICODE) { codeType=ENCODE_UNICODEBigend; for(i=0;iMsgData; short ret=0; static char FirstInitTTS=0; switch(initStatus){ case MINI_WAT_START: if(result==1){ SlwTrace(INF, "ModemStart...",1); ModemSendAt("ATE0\r\n"); mInitSwitchStatus(MINI_POC_AUTHSET); }else ret=-1; break; case MINI_POC_AUTHSET: if(result==1){ SlwTrace(INF, "POC-AuthOk--",1); PocAuthProcess(&msg[7]); } mInitSwitchStatus(MINI_GET_POC_VER); break; case MINI_GET_POC_VER: if(result!=1) { PocGetTypeCnt++; if(PocGetTypeCnt==1){ mInitSwitchStatus(MINI_GET_POC_VER);// break; } else if(PocGetTypeCnt==2){ hgs_poc_type=POC_NULL; SlwTrace(INF, "GetPocVer fail",1); } } else{ if(PocGetTypeCnt==0){ hgs_poc_type=POC_OWN; ModemSendAt("AT+POC=050000\r\n"); } else if(PocGetTypeCnt==1){ hgs_poc_type=POC_BND; ModemSendAt("AT+LSPA=1\r\n"); } getPocVersion(msg,hgs_poc_type); } PocGetTypeCnt=0; mInitSwitchStatus(MINI_SET_TTSPARM); break; case MINI_SET_TTSPARM: if(result != 1) SlwTrace(INF, "SetTTSPARAM fail",1); else SlwTrace(INF, "SetTTSPARAM OK",1); // if(hgs_poc_type==POC_OWN)mInitSwitchStatus(MINI_CHEAK_HEADSET);// // else { // mInitSwitchStatus(MINI_GET_MODULE); // } mInitSwitchStatus(MINI_GET_MODULE); break; case MINI_SET_MICLV://目前未使用 if(result != 1) SlwTrace(INF, "SET_MICLV fail",1); else SlwTrace(INF, "SET_MICLV OK",1); mInitSwitchStatus(MINI_CHEAK_HEADSET); break; case MINI_CHEAK_HEADSET: if(result != 1) SlwTrace(INF, "CHEAK_HEADSET fail",1); else{ SlwTrace(INF, "CHEAK_HEADSET OK",1); if(msg[9]=='0'){ sutPocStatus.HeadSetPtt=1; // ModemSendAt("AT+AUDCH=1,3\r\n"); printf("耳机接入-----\r\n"); } else if(msg[9]=='1'){ ModemSendAt("AT+AUDCH=0,0\r\n"); printf("耳机未接入----\r\n"); } } mInitSwitchStatus(MINI_GET_MODULE); break; case MINI_GET_MODULE: if(result != 1) SlwTrace(INF, "GetModemModule fail",1); else getModemModule(msg); uTimerHardDly(100); HeadsetProcess(1); /***********************************/ uTimerHardDly(50); ModemSendAt(FirmSperkr); uTimerHardDly(50); ModemSendAt(FirmSperkr2); mInitSwitchStatus(MINI_PLY_ICCID); break; case MINI_PLY_ICCID: if(result != 1) SlwTrace(INF, "GetICCID fail",1); else ModemGetICCID(msg); mInitSwitchStatus(MINI_GET_SIMSELECT); break; case MINI_GET_SIMSELECT: if(result != 1) SlwTrace(INF, "GetSIMSelect fail",1); else SIMSelect=atoi(&msg[10]); if(hgs_poc_type==POC_OWN)mInitSwitchStatus(MINI_SET_TONES); else mInitSwitchStatus(MINI_READ_SPKLV); break; case MINI_SET_TONES: if(result != 1) SlwTrace(INF, "SetTones fail",1); else SlwTrace(INF, "SetTones_Ok",1); mInitSwitchStatus(MINI_READ_SPKLV); break; case MINI_READ_SPKLV: //+CLVL:60 if(result!=1) SlwTrace(INF, "ReadSpkLv fail",1); else{ SetSpkLvProcess(2); if(newPara.VolumLv!=atoi(&msg[6])/10){ snprintf(buf,sizeof(buf),"AT+CLVL=%d\r\n",newPara.VolumLv*10);//num printf("VolumLv=%d\r\n",newPara.VolumLv); ModemSendAt(buf); } SlwTrace(INF, "SetSPKlv OK",1); } TTSPlayTick=uSysTickGet()+500;//5S后 才播报TONE音 if(hgs_poc_type==POC_BND){ g_uiGpsStat=0; newPara.gpsTimeIndex=0; snprintf(buf,sizeof(buf),"AT+POC=ttsvol=%d\r\n",newPara.VolumLv); ModemSendAt(buf); SpeakerCtrl(1); if(newPara.KeySound==0)ModemSendAt("AT+POC=tonevol=0\r\n"); else { //ModemSendAt("AT+POC=tonevol=%d\r\n",newPara.VolumLv); snprintf(buf,sizeof(buf),"AT+POC=tonevol=%d\r\n",newPara.VolumLv); ModemSendAt(buf); } mInitSwitchStatus(MINI_GET_CARD); }else { mInitSwitchStatus(MINI_SET_GPSPARM); } if(FirstInitTTS==0){ SpeakTTSVoice(); FirstInitTTS=1; } break; case MINI_SET_GPSPARM: if(result!=1) SlwTrace(INF, "GetGPSstatue fail",1); else { if(msg[6]=='N'){ if(!g_uiGpsStat){ ModemSendAt("AT+GPS=\"OFF\"\r\n"); //AT+VIBCTRL ModemSendAt("AT+VIBCTRL=\"POWER\",\"OFF\"\r\n"); printf("close GPS--%d\r\n",g_uiGpsStat); } }else{ if(g_uiGpsStat){ ModemSendAt("AT+GPS=\"ON\"\r\n"); ModemSendAt("AT+VIBCTRL=\"POWER\",\"ON\"\r\n"); printf("open GPS--%d\r\n",g_uiGpsStat); } } SlwTrace(INF, "Seting GPS",1); } if(g_uiGpsStat)ModemSendAt("AT+GPSANT=1\r\n"); if(hgs_poc_type==POC_OWN)mInitSwitchStatus(MINI_GET_POC_CODE); else mInitSwitchStatus(MINI_GET_CARD); CardStatue=0x11; break; case MINI_GET_POC_CODE: if(result!=1) SlwTrace(INF, "GetPocCode fail",1); else getPocCode(msg); mInitSwitchStatus(MINI_GET_CARD); break; case MINI_GET_CARD: switch(ChekCardCnt){ case 0: if(result != 1)CardStatue&=0x10; else { goto CARDREDY; } SIMSelect=!SIMSelect; snprintf(buf,sizeof(buf),"AT+SIMCROSS=%d\r\n",SIMSelect); ModemSendAt(buf); uTimerHardDly(100); ret=-3; ChekCardCnt=1; sutPocStatus.modemRstFlag=1; break; case 1: ChekCardCnt=0; if(result != 1){ CardStatue&=0x01; ret=-2; break; } CARDREDY: SlwTrace(INF, "Card Ready",1); sutPocStatus.CardStatus=1;//卡准备好 if(hgs_poc_type==POC_OWN){ ModemSendAt("AT+POC=10000020\r\n"); mInitSwitchStatus(MINI_POC_PARA); } else { ModemSendAt("AT+POC=open,1,1\r\n"); ret=1; } break; } break; case MINI_POC_PARA: if(result != 1) SlwTrace(INF, "GetPocPara fail",1); else ModemSetPocPara(msg,pMsg->MsgLen); ModemApnConfig(); //ModemSendAt("AT+ROUTE=0,1\r\n");//打开POClog ModemSendAt("AT+POC=030000\r\n"); ret=1; break; } return ret; } PT_THREAD (ptModemInitTask(struct pt *pt)) { static timer_t ptTimer; static unsigned char intCmd; static unsigned char status; static short i,t; short ret; SUT_MODEM_AT_MSG *pMsg=&sutAtmPro; PT_BEGIN(pt); while(1){ M_RESTART: SlwTrace(INF,"ModemInit...",1); memset((unsigned char *)&sutPocStatus, 0, sizeof(SUT_POC_STATUS)); SetLedIndicator(IndNoNet); sutPocStatus.modemRstFlag=0; modemInitReady=0; modemInitStatus=0; MsgQueueSet(); MODEM_PWREN_HIGH; PTTimerStart(&pt_timerPool, &ptTimer, 200); PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); MODEM_PWREN_LOW; PTTimerStart(&pt_timerPool, &ptTimer, 200); PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); mInitSwitchStatus(MINI_WAT_START); modemInitReady=1; sutPocStatus.ModemPowerOn=1; SEND_WAIT_PRO: status=0; for(i=0;i1) goto INIT_DONE;//使得GT可以直接跳出初始化过程 //或者升级模块模式直接跳转 if((pMsg->MsgLen = MsgQueueAccept(pMsg->MsgData,sizeof(pMsg->MsgData)))){ //printf("Ack:%s",pMsg->MsgData);调试 if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:8B0002")) SpeakerEnable();//wjl else if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:8B0000")) SpeakerDisable();//wjl if(0==ModemStrCmp((char *)pMsg->MsgData,WaitAT1) || 0==ModemStrCmp((char *)pMsg->MsgData,WaitAT2)){ status=1; goto SEND_GET_RET; } }else{ PTTimerStart(&pt_timerPool, &ptTimer, 2); PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); } } } SEND_GET_RET: ret=ModemInitAckHandler(status); //ret=-1; if(ret ==0) goto SEND_WAIT_PRO; else if(ret<0){//模块启动失败/卡不在 if(ret==-1){ SlwTrace(INF, "ModemInitTimeout GT Mode",1); EnterGTMode(); }else if(ret==-2){ SlwTrace(INF, "insert card error----",1); sutPocStatus.loginResult=4; } } INIT_DONE: //初始化完成 modemInitStatus=1;//可以消耗模块消息队列了 g_ucModemTaskEn=1; while(1){ if(sutPocStatus.modemRstFlag!=0){ printf("reset modem----\r\n"); MODEM_RESET_HIGH; PTTimerStart(&pt_timerPool, &ptTimer, 300); PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); MODEM_RESET_LOW; goto M_RESTART; } //sutPocStatus.ModemInactiveCt=0; /****************V113守护进程BUG********/ PTTimerStart(&pt_timerPool, &ptTimer, 100);//100 PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); } } PT_END(pt); } void InfoTick(void){ static unsigned int tick; if(g_ucEnterGTNow) return; if(uTimerExpired(&tick)){ uTimerStart(&tick, 500); printf("Info:Auth[%d],GT[%d],MI[%d],UI[%d] \r\n",sutGpsInfo.isGpsAuthOk,g_ucEnterGTNow,modemInitStatus,sutUIstatus.Status); } } static void AtHandle(char *msg); void ModemATProcess(void){ SUT_MODEM_AT_MSG *pMsg=&sutAtmPro; if(modemInitStatus==0) return; while(1){ pMsg->MsgLen = MsgQueueAccept(pMsg->MsgData,sizeof(pMsg->MsgData)); if(pMsg->MsgLen>0){ if(g_ucEnterGTNow!=0||USBSeletCt>1) continue;//如果进入GT模式,不需要处理模块消息了 SlwTrace(INF, (char *)pMsg->MsgData,0); sutPocStatus.ModemInactiveCt=0; if(pMsg->MsgData[0]=='+' || pMsg->MsgData[0] == '^'){ if(1 == PocHandle((char *)pMsg->MsgData)) continue; AtHandle((char *)pMsg->MsgData); }else if(pMsg->MsgData[0]=='$'&&pMsg->MsgData[3]=='R'&&pMsg->MsgData[5]=='C'){ process_gps_data((char*)&pMsg->MsgData[7],pMsg->MsgLen); sutGpsInfo.isGpsWork=1; } continue; }else if(pMsg->MsgLen<0){ SlwTrace(INF, "QueueF1",1); continue; } break; } } void ModemLedCtl(void){ if(g_ucModemTaskEn==0||g_ucEnterGTNow!=0) return; if(0==sutPocStatus.Logined)SetLedIndicator(IndNoNet); else if(sutPocStatus.Speaker>0)SetLedIndicator(IndRX); else if(sutPocStatus.Mic>0 )SetLedIndicator(IndTX); else SetLedIndicator(IndStandby); } static void AtHandle(char *pMsg) { int csq; short targetIndex; unsigned char tempSocket,i,temp; unsigned short tempLen; char buf[80]="\r\n"; char bufTemp[3]; char buf3[40]; static char Timerequest=0; STATUE ipStatus; if(0==ModemStrCmp(pMsg,"^SYSINFO:")){ if(pMsg[15]!=','){ netshowvalue=atoi(&pMsg[15]); }else{ netshowvalue=atoi(&pMsg[16]); } }else if(0==ModemStrCmp(pMsg,"+CSQ:")) { g_iCSQ=atoi(&pMsg[6]); }else if(0==ModemStrCmp(pMsg,"+POC_PPP:")){ if(pMsg[9]=='1') netPPP=OPEN; else netPPP=CLOSE; if(0==Timerequest&&netPPP==OPEN){ Timerequest=1; ModemSendAt("AT+CCLK?\r\n"); } }else if(0==ModemStrCmp(pMsg,"+POC:npong,")) { if(pMsg[11]=='1'){ netPPP=OPEN; } else netPPP=CLOSE; if(0==Timerequest&&netPPP==OPEN){ Timerequest=1; ModemSendAt("AT+CCLK?\r\n"); } } else if(0==ModemStrCmp(pMsg,"+TCPSTATUS:")){ tcpSocketStatusUpdate(pMsg[11],pMsg[13],0); }else if(0==ModemStrCmp(pMsg,"+TCPRECV:")){//+TCPRECV:0,2,31 if(-1 != (targetIndex=FindTargetIndex(pMsg, '\r', ":", 1,1))){ tempSocket = atoi(&pMsg[targetIndex]);//找到socket if(-1 != (targetIndex=FindTargetIndex(pMsg, '\r', ",",1,1))){ tempLen = atoi(&pMsg[targetIndex]); if(-1 != (targetIndex=FindTargetIndex(pMsg, '\r', ",",1,2))){ tcpSocketRecv(tempSocket, &pMsg[targetIndex], tempLen); } } } }else if(0==ModemStrCmp(pMsg,"+TCPOPEN:")){ tcpSocketStatusUpdate(pMsg[9],pMsg[11],0); }else if(0==ModemStrCmp(pMsg, "+TCPCLOSE:")){//主动关掉或者服务器关掉都是这样 tcpSocketStatusUpdate(pMsg[10],NULL,1); }else if(0==ModemStrCmp(pMsg, "+CCLK:")) { ModemGetTime(pMsg); }else if(0==ModemStrCmp(pMsg, "^MODE:")) {//^MODE: 9 与下面位置不一,模块问题 //^MODE:2 与上面位置不一,模块问题 if(pMsg[6] == ' ') bufTemp[0]=pMsg[7]; else bufTemp[0]=pMsg[6]; snprintf(buf, sizeof(buf), "[Mode %c]", bufTemp[0]); switch(bufTemp[0]) { case 0x30:strcat(buf, "No server");break; case 0x32:strcat(buf, "CDMA Mode");break; case 0x33:strcat(buf, "GSM");break; case 0x34:strcat(buf, "TD-SCDMA/EVDO");break; case 0x35:strcat(buf, "WCDMA");break; case 0x39:strcat(buf, "LTE");break; default:strcat(buf, "Unknow");break; } SlwTrace(INF, buf, 1); }else if(0==ModemStrCmp(pMsg, "+POC:START")) { sutPocStatus.modemRstFlag=1; sutPocStatus.ModemInactiveCt=0;//当模块重启后重新计数串口通讯异常计数器 } else if(0==ModemStrCmp(pMsg, "+GGIP:")) { //GSPIP返回 //+GPIP:183.131.13.10 if(newPara.serList.defaultuse==SER_JIQUN||newPara.serList.defaultuse==SER_IP)chekPipGip(pMsg,1); //strcpy(gSocket.socketList[0].Ip,showGIP);// }else if(0==ModemStrCmp(pMsg, "+GPIP:")) { if(newPara.serList.defaultuse==SER_JIQUN||newPara.serList.defaultuse==SER_IP)chekPipGip(pMsg,0); }else if(0==ModemStrCmp(pMsg, "+FOTA:")){ FotaStatusProcess(pMsg+6); uTimerStart(&fotaTick,10); }else if(0==ModemStrCmp(pMsg, "+POC:WAKEUP")){ enableDataSend(); }else if(0==ModemStrCmp(pMsg, "+UDP:")){ GetPwrMode(pMsg+5); }else if(0==ModemStrCmp(pMsg, "+AUDCH"))// { }else if(0==ModemStrCmp(pMsg,"+CTEC:")) { tempnetshowvalue=atoi(&pMsg[7]); if(newPara.netMode!=tempnetshowvalue){ snprintf(buf,sizeof(buf),"AT+CTEC=%d,%d\r\n",tempnetshowvalue,newPara.netMode); ModemSendAt(buf); } }else if(0==ModemStrCmp(pMsg,"+COPS")){ //netshowvalue=atoi(&pMsg[FindContentByChar( pMsg,',',3)]); netshowvalue=atoi(FindContentByChar(pMsg,',',3,buf)); }else if(0==ModemStrCmp(pMsg,"+HEADSET:")) { //InternalHeadSet(atoi(&pMsg[9])); } } void InternalHeadSet(char type) { switch(type) { case 0: sutPocStatus.HeadSetPtt=1; //ModemSendAt("AT+AUDCH=1,3\r\n"); break; case 1: sutPocStatus.HeadSetPtt=0; ModemSendAt("AT+AUDCH=0,0\r\n"); break; case 2: ModemSendAt("AT+POC=0B0000\r\n"); break; case 3: ModemSendAt("AT+POC=0C0000\r\n"); break; } } char *FindContentByChar( char *src,char c, char index,char *dest) { char i; char j=0; char *p=dest; for(i=0;i400)PreRead=400; else PreRead=len; tempLen=ReadFileData(CodecFileIndex,StartAddr,PreRead,(u8*)tempData); len-=PreRead; StartAddr+=PreRead; snprintf(tempbuf,sizeof(tempbuf),"AT+CAIET=0,0,%d,%d,\"",StartAddr,PreRead); ModemSendAt(tempbuf); ModemSendData(tempData,PreRead); ModemSendAt("\"\r\n"); } return 0; }