123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /**********************************************************************************
- * File Name: Msg.c
- * Function Describe:
- * Relate Module:
- * Explain:
- * Writer: ShiLiangWen
- * Date: 2015.2.10
- **********************************************************************************/
- #include "includes.h"
- SUT_MSG sutMsg;
- unsigned char g_ucTask;
- unsigned char g_ucLastTask;
- unsigned long g_ulTickCt=0;
- unsigned long g_ulTick500Ct=0;
- int g_iCSQ=0;
- unsigned char g_ucLoginServer=0;
- unsigned char g_ucPPPStatus=0;
- unsigned char g_ucIPStatus=0;
- int g_iFotaCt=0;
- int g_iFoTaFlag=0;
- #define SPK_MUTE_PIN GPIO_Pin_5
- #define SPK_MUTE_PORT GPIOA
- #define SpeakerEnable() SPK_MUTE_PORT->BRR=SPK_MUTE_PIN//Speaker Enable
- #define SpeakerDisable() SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN//Speaker Disable
- void ModemProcess(void);
- void SpeakerInit(void);
- /***********************************************************************************
- *NewTask
- ************************************************************************************/
- void NewTask(unsigned char Task)
- {
- g_ucLastTask=g_ucTask;
- g_ucTask=Task;
- sutMsg.TaskStart=1;
- }
- /***********************************************************************************
- *StartMsg
- ************************************************************************************/
- void StartMsg(void)
- {
- g_ulTick500Ct=0;
- g_ulTickCt=0;
- switch(g_ucTask){
- case TASK_WAIT:
- IapTrace("WAIT!\r\n");
- DelayTick(20);
- IapGetUpdateVersion(PCTA_MAGIC);
- break;
- case TASK_PC_UPDATE:
- IapTrace("PC_UPDATE!\r\n");
- break;
- case TASK_MODEM_UPDATE:
- #ifndef FOTA_ENABLE
- NewTask(TASK_GOTO_APP);
- return;
- #endif
- if(0==sutProductPara.FoTaServerSel && 0==sutProductPara.PIP[0]){
- NewTask(TASK_GOTO_APP);
- return;
- }
- if(1==sutProductPara.FoTaServerSel && 0==sutProductPara.GIP[0]){
- NewTask(TASK_GOTO_APP);
- return;
- }
-
- IapTrace("MODEM_UPDATE!\r\n");
- IapTrace("Modem Init...");
- if(0==ModemInit()){
- IapTrace("OK!\r\n");
- }else{
- IapTrace("Fail!\r\n");
- NewTask(TASK_GOTO_APP);
- }
- SpeakerInit();
- break;
- case TASK_GOTO_APP:
- IapTrace("GOTO_APP!\r\n");
- ReadFlashData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- if(0x55==sutProductPara.IapFlag){
- sutProductPara.IapFlag=0x88;
- WritePageData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- }
- if(0x88==sutProductPara.IapFlag)iap_jump_app_s();
- break;
- }
- }
- /*********************************************************************
- *Tick500Msg
- **********************************************************************/
- void Tick500Msg(void)
- {
- if(g_ulTick500Ct<0xffffffff)g_ulTick500Ct++;
- if(TASK_WAIT==g_ucTask){
- if(g_ulTick500Ct<2)return;
- ReadFlashData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- if(0x55==sutProductPara.IapFlag)NewTask(TASK_MODEM_UPDATE);
- else NewTask(TASK_GOTO_APP);
- }else if(TASK_MODEM_UPDATE==g_ucTask){
- ModemProcess();
- }else if(TASK_GOTO_APP==g_ucTask){
- if(g_ulTick500Ct<4)return;
- if(g_ulTick500Ct&0x00000001)IapTrace("No App!\r\n");
- }
- }
- /*******************************************************************
- *GetCSQ
- ********************************************************************/
- void GetCSQ(char *msg)
- {
- if(ModemStrCmp(msg,"+CSQ:"))return;
- g_iCSQ=atoi(&msg[5]);
-
- }
- /*******************************************************************
- *GetPPPStatus
- ********************************************************************/
- void GetPPPStatus(char *msg)
- {
- if(ModemStrCmp(msg,"+ZPPPSTATUS:"))return;
- if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
- g_ucPPPStatus=1;
- }else{
- g_ucPPPStatus=0;
- g_ucLoginServer=0;
- }
- }
- /*******************************************************************
- *GetIPStatusu
- ********************************************************************/
- void GetIPStatusu(char *msg)
- {
- if(ModemStrCmp(msg,"+ZIPSTATUSU:"))return;
- if(msg[13]=='E' && msg[15]=='T' && msg[23]=='D'){
- g_ucIPStatus=1;
- }else{
- g_ucIPStatus=0;
- g_ucLoginServer=0;
- }
- }
- void ModemRecvHandle(char *msg,unsigned short MsgLen)
- { //+ZIPRECVU:0,1,xxxx [14]
- //+ZIPRECVU:0,10,xxxx [15]
- //+ZIPRECVU:0,100,xxxx [16]
- //+ZIPRECVU:0,1024,xxxx [17]
- int flag;
- char buf[230];
- char tmp[5];
- int i,j,k,len;
- char *pData;
- if(ModemStrCmp(msg,"+ZIPRECVU:"))return;
- if(MsgLen<0){
- //printf("MsgLen Over!\r\n");
- return;
- }
- len=StrToNum(&msg[12]);
- if(len<10)k=16;
- else if(len<100)k=17;
- else if(len<1000)k=18;
- else k=19;
-
- if(MsgLen!=(len+k)){
- //printf("MsgLen Error!\r\n");
- return;
- }
- pData=&msg[k-2];
- #if 1 //DEBUG
- sprintf(buf,"[Recv=%d]",len);
- //if(len>70)j=70;
- //else j=len;
- //for(i=0;i<j;i++){
- // sprintf(tmp,"%02x,",pData[i]);
- // strcat(buf,tmp);
- //}
- strcat(buf,"\r\n");
- IapTrace(buf);
- #endif
- flag=IapRecvMsgHandle((unsigned char *)pData,len);
- if(-1==flag || -2==flag)return;
-
- if(0==flag){
- IapTrace("Completed!");
- ModemSendAT("AT+ZTTS=1,\"F466B06510629F52\"\r\n");//应用更新成功
- DelayTick(300);
- NewTask(TASK_GOTO_APP);
- return;
- }
-
- // if(flag<-2 && flag>-6){
- // sprintf(buf,"IAP Stop[%d]!\r\n",(int)flag);
- // IapTrace(buf);
- // NewTask(TASK_GOTO_APP);
- // }
- g_iFotaCt=0;
- g_iFoTaFlag=flag;
- }
- /***********************************************************************************
- *ModemProcess
- 500ms执行一次
- ************************************************************************************/
- unsigned char g_ucFoTaTTS=0;//
- void ModemProcess(void)
- {
- char buf[200];
- static unsigned char sucLoginTimout=60;
- static unsigned char sucNetStatusCt=0;
- if(++sucNetStatusCt>4)sucNetStatusCt=0;
-
- //30秒没登陆服务器,自动退出FOTA
- if(0==g_ucLoginServer || g_iFoTaFlag<0){
- if(sucLoginTimout)sucLoginTimout--;
- else{
- ModemSendAT("AT+ZTTS=1,\"F466B0653159258D\"\r\n");//更新失败
- DelayTick(300);
- NewTask(TASK_GOTO_APP);
- }
- }else sucLoginTimout=60;
-
- //TTS
- if(g_iFoTaFlag==2 && g_ucFoTaTTS==0){
- g_ucFoTaTTS=1;
- ModemSendAT("AT+ZTTS=1,\"636B2857F466B065\"\r\n");//正在更新
- }
-
- switch(sucNetStatusCt)
- {
- case 0:
- ModemSendAT("AT+CSQ?\r");
- sprintf(buf,"[IAP]CSQ=%d,PPP=%d,IP=%d,S=%d,F=%d\r\n",g_iCSQ,g_ucPPPStatus,g_ucIPStatus,g_ucLoginServer,g_iFoTaFlag);
- SlwTrace(buf);
- break;
- case 1:
- ModemSendAT("AT+ZPPPSTATUS\r");
- break;
- case 2:
- if(1==g_ucPPPStatus){
- //printf("AT+ZIPSTATUSU=0\r\n");
- ModemSendAT("AT+ZIPSTATUSU=0\r");
- }else{
- g_ucLoginServer=0;
- g_ucIPStatus=0;
- ModemSendAT("AT+ZPPPOPEN\r");
- }
- break;
- case 3:
- if(1!=g_ucIPStatus && 1==g_ucPPPStatus){
- g_ucLoginServer=0;
- if(0==sutProductPara.FoTaServerSel){
- sprintf(buf,"AT+ZIPSETUPU=0,%s,%d\r\n",sutProductPara.PIP,sutProductPara.FoTaProt);
- }else{
- sprintf(buf,"AT+ZIPSETUPU=0,%s,%d\r\n",sutProductPara.GIP,sutProductPara.FoTaProt);
- }
- //IapTrace(buf);//正式版本不应打印IP信息
- ModemSendAT(buf);
- }
- break;
- case 4:
- if(1==g_ucIPStatus && 0==g_ucLoginServer){
- IapGetUpdateVersion(FOTA_MAGIC);
- }else if(1==g_ucIPStatus && 1==g_ucLoginServer){
- if(++g_iFotaCt>2){
- g_iFotaCt=0;
- if(1==g_iFoTaFlag){
- IapGetUpdateData(FOTA_MAGIC,g_ulOffset);
- }else if(2==g_iFoTaFlag){
- IapGetUpdateData(FOTA_MAGIC,g_ulOffset);
- }
- }
- }
- break;
- }
- }
- /***********************************************************************************
- *Use for PC
- ************************************************************************************/
- void Uart1Msg(void)
- {
- int flag;
- flag=IapRecvMsgHandle(IapRxBuf,IapRxLen);
- if(0==flag){
- IapTrace("Completed!");
- NewTask(TASK_GOTO_APP);
- return;
- }
-
- if(flag<-2 && flag>-6){
- IapTrace("IAP Stop!\r\n");
- NewTask(TASK_GOTO_APP);
- }
- }
- char MsgBuf[2000];
- /***********************************************************************************
- *TickMsg
- ************************************************************************************/
- void TickMsg(void)
- {
- int r;
- if(g_ulTickCt<0xffffffff)g_ulTickCt++;
- if(TASK_MODEM_UPDATE==g_ucTask){
- r=ModemWaitMsg(MsgBuf,2000,0);
- if(r){
- GetCSQ(MsgBuf);
- GetPPPStatus(MsgBuf);
- GetIPStatusu(MsgBuf);
- ModemRecvHandle(MsgBuf,r);
- }
- }
- }
- /********************************************************************************
- SpeakerInit
- *********************************************************************************/
- void SpeakerInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
- SpeakerEnable();
- }
- /***********************************************************************************/
|