Msg.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**********************************************************************************
  2. * File Name: Msg.c
  3. * Function Describe:
  4. * Relate Module:
  5. * Explain:
  6. * Writer: ShiLiangWen
  7. * Date: 2015.2.10
  8. **********************************************************************************/
  9. #include "includes.h"
  10. SUT_MSG sutMsg;
  11. unsigned char g_ucTask;
  12. unsigned char g_ucLastTask;
  13. unsigned long g_ulTickCt=0;
  14. unsigned long g_ulTick500Ct=0;
  15. int g_iCSQ=0;
  16. unsigned char g_ucLoginServer=0;
  17. unsigned char g_ucPPPStatus=0;
  18. unsigned char g_ucIPStatus=0;
  19. int g_iFotaCt=0;
  20. int g_iFoTaFlag=0;
  21. #define SPK_MUTE_PIN GPIO_Pin_5
  22. #define SPK_MUTE_PORT GPIOA
  23. #define SpeakerEnable() SPK_MUTE_PORT->BRR=SPK_MUTE_PIN//Speaker Enable
  24. #define SpeakerDisable() SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN//Speaker Disable
  25. void ModemProcess(void);
  26. void SpeakerInit(void);
  27. /***********************************************************************************
  28. *NewTask
  29. ************************************************************************************/
  30. void NewTask(unsigned char Task)
  31. {
  32. g_ucLastTask=g_ucTask;
  33. g_ucTask=Task;
  34. sutMsg.TaskStart=1;
  35. }
  36. /***********************************************************************************
  37. *StartMsg
  38. ************************************************************************************/
  39. void StartMsg(void)
  40. {
  41. g_ulTick500Ct=0;
  42. g_ulTickCt=0;
  43. switch(g_ucTask){
  44. case TASK_WAIT:
  45. IapTrace("WAIT!\r\n");
  46. DelayTick(20);
  47. IapGetUpdateVersion(PCTA_MAGIC);
  48. break;
  49. case TASK_PC_UPDATE:
  50. IapTrace("PC_UPDATE!\r\n");
  51. break;
  52. case TASK_MODEM_UPDATE:
  53. #ifndef FOTA_ENABLE
  54. NewTask(TASK_GOTO_APP);
  55. return;
  56. #endif
  57. if(0==sutProductPara.FoTaServerSel && 0==sutProductPara.PIP[0]){
  58. NewTask(TASK_GOTO_APP);
  59. return;
  60. }
  61. if(1==sutProductPara.FoTaServerSel && 0==sutProductPara.GIP[0]){
  62. NewTask(TASK_GOTO_APP);
  63. return;
  64. }
  65. IapTrace("MODEM_UPDATE!\r\n");
  66. IapTrace("Modem Init...");
  67. if(0==ModemInit()){
  68. IapTrace("OK!\r\n");
  69. }else{
  70. IapTrace("Fail!\r\n");
  71. NewTask(TASK_GOTO_APP);
  72. }
  73. SpeakerInit();
  74. break;
  75. case TASK_GOTO_APP:
  76. IapTrace("GOTO_APP!\r\n");
  77. ReadFlashData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  78. if(0x55==sutProductPara.IapFlag){
  79. sutProductPara.IapFlag=0x88;
  80. WritePageData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  81. }
  82. if(0x88==sutProductPara.IapFlag)iap_jump_app_s();
  83. break;
  84. }
  85. }
  86. /*********************************************************************
  87. *Tick500Msg
  88. **********************************************************************/
  89. void Tick500Msg(void)
  90. {
  91. if(g_ulTick500Ct<0xffffffff)g_ulTick500Ct++;
  92. if(TASK_WAIT==g_ucTask){
  93. if(g_ulTick500Ct<2)return;
  94. ReadFlashData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  95. if(0x55==sutProductPara.IapFlag)NewTask(TASK_MODEM_UPDATE);
  96. else NewTask(TASK_GOTO_APP);
  97. }else if(TASK_MODEM_UPDATE==g_ucTask){
  98. ModemProcess();
  99. }else if(TASK_GOTO_APP==g_ucTask){
  100. if(g_ulTick500Ct<4)return;
  101. if(g_ulTick500Ct&0x00000001)IapTrace("No App!\r\n");
  102. }
  103. }
  104. /*******************************************************************
  105. *GetCSQ
  106. ********************************************************************/
  107. void GetCSQ(char *msg)
  108. {
  109. if(ModemStrCmp(msg,"+CSQ:"))return;
  110. g_iCSQ=atoi(&msg[5]);
  111. }
  112. /*******************************************************************
  113. *GetPPPStatus
  114. ********************************************************************/
  115. void GetPPPStatus(char *msg)
  116. {
  117. if(ModemStrCmp(msg,"+ZPPPSTATUS:"))return;
  118. if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
  119. g_ucPPPStatus=1;
  120. }else{
  121. g_ucPPPStatus=0;
  122. g_ucLoginServer=0;
  123. }
  124. }
  125. /*******************************************************************
  126. *GetIPStatusu
  127. ********************************************************************/
  128. void GetIPStatusu(char *msg)
  129. {
  130. if(ModemStrCmp(msg,"+ZIPSTATUSU:"))return;
  131. if(msg[13]=='E' && msg[15]=='T' && msg[23]=='D'){
  132. g_ucIPStatus=1;
  133. }else{
  134. g_ucIPStatus=0;
  135. g_ucLoginServer=0;
  136. }
  137. }
  138. void ModemRecvHandle(char *msg,unsigned short MsgLen)
  139. { //+ZIPRECVU:0,1,xxxx [14]
  140. //+ZIPRECVU:0,10,xxxx [15]
  141. //+ZIPRECVU:0,100,xxxx [16]
  142. //+ZIPRECVU:0,1024,xxxx [17]
  143. int flag;
  144. char buf[230];
  145. char tmp[5];
  146. int i,j,k,len;
  147. char *pData;
  148. if(ModemStrCmp(msg,"+ZIPRECVU:"))return;
  149. if(MsgLen<0){
  150. //printf("MsgLen Over!\r\n");
  151. return;
  152. }
  153. len=StrToNum(&msg[12]);
  154. if(len<10)k=16;
  155. else if(len<100)k=17;
  156. else if(len<1000)k=18;
  157. else k=19;
  158. if(MsgLen!=(len+k)){
  159. //printf("MsgLen Error!\r\n");
  160. return;
  161. }
  162. pData=&msg[k-2];
  163. #if 1 //DEBUG
  164. sprintf(buf,"[Recv=%d]",len);
  165. //if(len>70)j=70;
  166. //else j=len;
  167. //for(i=0;i<j;i++){
  168. // sprintf(tmp,"%02x,",pData[i]);
  169. // strcat(buf,tmp);
  170. //}
  171. strcat(buf,"\r\n");
  172. IapTrace(buf);
  173. #endif
  174. flag=IapRecvMsgHandle((unsigned char *)pData,len);
  175. if(-1==flag || -2==flag)return;
  176. if(0==flag){
  177. IapTrace("Completed!");
  178. ModemSendAT("AT+ZTTS=1,\"F466B06510629F52\"\r\n");//应用更新成功
  179. DelayTick(300);
  180. NewTask(TASK_GOTO_APP);
  181. return;
  182. }
  183. // if(flag<-2 && flag>-6){
  184. // sprintf(buf,"IAP Stop[%d]!\r\n",(int)flag);
  185. // IapTrace(buf);
  186. // NewTask(TASK_GOTO_APP);
  187. // }
  188. g_iFotaCt=0;
  189. g_iFoTaFlag=flag;
  190. }
  191. /***********************************************************************************
  192. *ModemProcess
  193. 500ms执行一次
  194. ************************************************************************************/
  195. unsigned char g_ucFoTaTTS=0;//
  196. void ModemProcess(void)
  197. {
  198. char buf[200];
  199. static unsigned char sucLoginTimout=60;
  200. static unsigned char sucNetStatusCt=0;
  201. if(++sucNetStatusCt>4)sucNetStatusCt=0;
  202. //30秒没登陆服务器,自动退出FOTA
  203. if(0==g_ucLoginServer || g_iFoTaFlag<0){
  204. if(sucLoginTimout)sucLoginTimout--;
  205. else{
  206. ModemSendAT("AT+ZTTS=1,\"F466B0653159258D\"\r\n");//更新失败
  207. DelayTick(300);
  208. NewTask(TASK_GOTO_APP);
  209. }
  210. }else sucLoginTimout=60;
  211. //TTS
  212. if(g_iFoTaFlag==2 && g_ucFoTaTTS==0){
  213. g_ucFoTaTTS=1;
  214. ModemSendAT("AT+ZTTS=1,\"636B2857F466B065\"\r\n");//正在更新
  215. }
  216. switch(sucNetStatusCt)
  217. {
  218. case 0:
  219. ModemSendAT("AT+CSQ?\r");
  220. 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);
  221. SlwTrace(buf);
  222. break;
  223. case 1:
  224. ModemSendAT("AT+ZPPPSTATUS\r");
  225. break;
  226. case 2:
  227. if(1==g_ucPPPStatus){
  228. //printf("AT+ZIPSTATUSU=0\r\n");
  229. ModemSendAT("AT+ZIPSTATUSU=0\r");
  230. }else{
  231. g_ucLoginServer=0;
  232. g_ucIPStatus=0;
  233. ModemSendAT("AT+ZPPPOPEN\r");
  234. }
  235. break;
  236. case 3:
  237. if(1!=g_ucIPStatus && 1==g_ucPPPStatus){
  238. g_ucLoginServer=0;
  239. if(0==sutProductPara.FoTaServerSel){
  240. sprintf(buf,"AT+ZIPSETUPU=0,%s,%d\r\n",sutProductPara.PIP,sutProductPara.FoTaProt);
  241. }else{
  242. sprintf(buf,"AT+ZIPSETUPU=0,%s,%d\r\n",sutProductPara.GIP,sutProductPara.FoTaProt);
  243. }
  244. //IapTrace(buf);//正式版本不应打印IP信息
  245. ModemSendAT(buf);
  246. }
  247. break;
  248. case 4:
  249. if(1==g_ucIPStatus && 0==g_ucLoginServer){
  250. IapGetUpdateVersion(FOTA_MAGIC);
  251. }else if(1==g_ucIPStatus && 1==g_ucLoginServer){
  252. if(++g_iFotaCt>2){
  253. g_iFotaCt=0;
  254. if(1==g_iFoTaFlag){
  255. IapGetUpdateData(FOTA_MAGIC,g_ulOffset);
  256. }else if(2==g_iFoTaFlag){
  257. IapGetUpdateData(FOTA_MAGIC,g_ulOffset);
  258. }
  259. }
  260. }
  261. break;
  262. }
  263. }
  264. /***********************************************************************************
  265. *Use for PC
  266. ************************************************************************************/
  267. void Uart1Msg(void)
  268. {
  269. int flag;
  270. flag=IapRecvMsgHandle(IapRxBuf,IapRxLen);
  271. if(0==flag){
  272. IapTrace("Completed!");
  273. NewTask(TASK_GOTO_APP);
  274. return;
  275. }
  276. if(flag<-2 && flag>-6){
  277. IapTrace("IAP Stop!\r\n");
  278. NewTask(TASK_GOTO_APP);
  279. }
  280. }
  281. char MsgBuf[2000];
  282. /***********************************************************************************
  283. *TickMsg
  284. ************************************************************************************/
  285. void TickMsg(void)
  286. {
  287. int r;
  288. if(g_ulTickCt<0xffffffff)g_ulTickCt++;
  289. if(TASK_MODEM_UPDATE==g_ucTask){
  290. r=ModemWaitMsg(MsgBuf,2000,0);
  291. if(r){
  292. GetCSQ(MsgBuf);
  293. GetPPPStatus(MsgBuf);
  294. GetIPStatusu(MsgBuf);
  295. ModemRecvHandle(MsgBuf,r);
  296. }
  297. }
  298. }
  299. /********************************************************************************
  300. SpeakerInit
  301. *********************************************************************************/
  302. void SpeakerInit(void)
  303. {
  304. GPIO_InitTypeDef GPIO_InitStructure;
  305. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
  306. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  307. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  308. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  309. SpeakerEnable();
  310. }
  311. /***********************************************************************************/