ModemTask.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /********************************************************************************
  2. * File Name: ModemTask.c
  3. * Function Describe: 完成串口与Modem的通讯
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2015-8-2
  7. *******************************************************************************/
  8. #define THIS_FILE_ID 8
  9. /*******************************************************************************/
  10. #include "includes.h"
  11. OS_TID idModemTask=0;
  12. U64 stkModemTask[MODEM_TASK_STK_SIZE];
  13. OS_TID idModemUartTask=0;
  14. U64 stkModemUartTask[MODEM_UART_TASK_STK_SIZE];
  15. OS_SEM ModemUartSem;
  16. SUT_MODEM_STATUS sutModemStatus;
  17. unsigned char g_ucModemTaskSleep=0;
  18. char ModemMsgQueueDataBuffer[MODEM_MSG_DATA_BUF_LEN];
  19. SUT_MSG_QUEUE ModemMsgQueue;
  20. //----------------------------------------------------------
  21. //----------------------------------------------------------
  22. unsigned char g_ucModemTaskEn=0;
  23. unsigned char g_ucTestCodeSend = 0;//SendTest包的测量值标志码
  24. unsigned char g_ucTestCodeRecv = 0;//SendTestAck包的测量值标志码
  25. unsigned char g_ucSaveSetupAckResult = 0;//
  26. unsigned char g_ucReadTestAckResult = 0;//
  27. unsigned char g_ucSetControlAckResult = 0;//
  28. unsigned char g_ucSendTest = 0;//
  29. int SetControlRegNum=0;
  30. unsigned short atous(char *pMsg);
  31. M_RESULT SendPosition(void);
  32. /*******************************************************************
  33. *ModemUartTask
  34. ********************************************************************/
  35. __task void ModemUartTask(void)
  36. {
  37. os_sem_init (&ModemUartSem, 0);
  38. while(1){
  39. os_sem_wait (&ModemUartSem, 0xffff);
  40. tsk_lock ();
  41. Uart3RecvProcess();
  42. tsk_unlock ();
  43. }
  44. }
  45. int CheckAKey(void)
  46. {
  47. char buf[15];
  48. unsigned long akey;
  49. if(sutProductPara.PSN==0)return 1;//PSN=0 允许
  50. akey = getAKeyByPSN(sutProductPara.PSN);
  51. if(akey!=sutProductPara.AKEY || sutProductPara.AKEY==0){
  52. strcpy(buf,"ER");
  53. strcat(buf,"ROR:A");
  54. strcat(buf,"KN\r\n");
  55. SlwTrace(DEBUG,buf);//ERROR:AKN
  56. return 0;
  57. }
  58. return 1;
  59. }
  60. /*******************************************************************
  61. *ConnectAppServer
  62. ********************************************************************/
  63. M_RESULT ConnectAppServer(void)
  64. {
  65. SlwTrace(DEBUG,"ConnectAppServer>>\r\n");
  66. return ModemOpenSocket(0,"TCP",sutProductPara.AppServer,sutProductPara.AppPort);
  67. }
  68. M_RESULT ConnectAuthServer(void)
  69. {
  70. SlwTrace(DEBUG,"ConnectAuthServer>>\r\n");
  71. return ModemOpenSocket(AUTH_UDP_SOCKET,"UDP",sutProductPara.authServer,sutProductPara.authPort);
  72. }
  73. /*******************************************************************
  74. *ConnectFotaServer
  75. ********************************************************************/
  76. M_RESULT ConnectFotaServer(void)
  77. {
  78. SlwTrace(DEBUG,"ConnectFotaServer>>\r\n");
  79. return ModemOpenSocket(0,"TCP",sutProductPara.FotaServer,sutProductPara.FotaPort);
  80. }
  81. /*
  82. 发登陆包
  83. */
  84. M_RESULT SendLogin(void)
  85. {
  86. unsigned short len;
  87. SlwTrace(DEBUG,"SendLogin>>\r\n");
  88. len=PacketLogin(sutModemStatus.CmdNum);
  89. return ModemSendToSocket(0,g_ucPropacketBuf,len);
  90. }
  91. /*
  92. 发当前点位置包
  93. */
  94. M_RESULT SendCurPosition(void)
  95. {
  96. char buf[30];
  97. unsigned short i,len;
  98. SUT_POSITION sutPosition;
  99. SlwTrace(DEBUG,"SendTest>>\r\n");
  100. memset(&sutPosition,0,sizeof(SUT_POSITION));
  101. if(sutGpsInfo.isGpsValid)sutPosition.Status.GNSS=1;
  102. else sutPosition.Status.GNSS=0;
  103. sutPosition.Status.ACC=1;
  104. sutPosition.Status.BD=sutGpsInfo.isBDWork;
  105. sutPosition.Status.GPS=sutGpsInfo.isGpsWork;
  106. sutPosition.Status.West=sutGpsInfo.West;
  107. sutPosition.Status.South=sutGpsInfo.South;
  108. sutPosition.Latitude=sutGpsInfo.Latitude;//22620926;//114035268; //114.035268
  109. sutPosition.Longitude=sutGpsInfo.Longitude; //114035268;//22620926; //22.620926
  110. sutPosition.Speed=sutGpsInfo.Speed;//0;
  111. sutPosition.Aspect=sutGpsInfo.Aspect;//123;
  112. sutPosition.Altitude=sutGpsInfo.Altitude;
  113. RTC_Get(sutPosition.Time);
  114. sutPosition.ExtraMsgId=0x3A;//RT231项目固定0x3A
  115. sutPosition.NetMode=1;//4G
  116. sutPosition.CSQ =sutModemStatus.CSQ;
  117. sutPosition.Infrared=sutTestData.Infrared;
  118. sutPosition.Camera=sutTestData.Camera;
  119. for(i=0;i<sutTestData.TcLen;i++){
  120. sutPosition.TcData[i]=sutTestData.TcData[i];
  121. }
  122. sutPosition.TcLen=sutTestData.TcLen;
  123. sutPosition.ExtraMsgLen=sutPosition.TcLen+4;//
  124. len=PacketPosition(sutModemStatus.CmdNum,sutPosition);
  125. return ModemSendToSocket(0,g_ucPropacketBuf,len);
  126. }
  127. //M_RESULT SendPosition(void)
  128. //{
  129. // unsigned short len;
  130. // SUT_POSITION sutPosition;
  131. // if(sutStorageFiFo.len>1){
  132. // SlwTrace(DEBUG,"SendMorePosition>>\r\n");
  133. // len=PacketMorePosition(sutModemStatus.CmdNum);
  134. // }else{
  135. // SlwTrace(DEBUG,"SendOnePosition>>\r\n");
  136. // if(StorageFiFoPop(&sutPosition)){
  137. // len=PacketPosition(sutModemStatus.CmdNum,sutPosition);
  138. // }
  139. // }
  140. // return ModemSendToSocket(0,g_ucTcpSendBuf,len);
  141. //}
  142. /************************************************
  143. NetProcess
  144. 每10ms执行一次
  145. **************************************************/
  146. void NetProcess(unsigned char reset)
  147. {
  148. int i;
  149. unsigned char AppIp[4]={0,0,0,0};
  150. M_RESULT r;
  151. if(reset){
  152. return;
  153. }
  154. //=======
  155. //===========以下控制每秒执行一次============
  156. static U32 os_old_time=0;
  157. U32 os_cur_time=os_time_get();
  158. if((os_cur_time-os_old_time)>100)os_old_time=os_cur_time;
  159. else return;
  160. //=========================================
  161. switch(sutModemStatus.Step){
  162. case STEP_CARD:
  163. SlwTrace(DEBUG,"<CARD>\r\n");
  164. sutModemStatus.ServerStatus=CLOSED;
  165. r=ModemGetCardStatus();
  166. if(SUCCEED==r){
  167. sutModemStatus.ATTimeoutCt=0;
  168. sutModemStatus.CardStatus=OPENED;
  169. sutModemStatus.CheckCSQCt=0;
  170. sutModemStatus.RebootModemCt=0;
  171. sutModemStatus.Step=STEP_CSQ;
  172. }else if(FAIL==r){
  173. sutModemStatus.ATTimeoutCt=0;
  174. sutModemStatus.CardStatus=CLOSED;
  175. if(++sutModemStatus.CheckCardCt>20){
  176. ModemReboot(sutModemStatus.RebootModemCt++);
  177. }
  178. }else{//TIMEOUT
  179. sutModemStatus.CardStatus=CLOSED;
  180. if(++sutModemStatus.ATTimeoutCt>5){
  181. sutModemStatus.ATTimeoutCt=0;
  182. ModemReboot(sutModemStatus.RebootModemCt++);
  183. }
  184. }
  185. break;
  186. case STEP_CSQ:
  187. SlwTrace(DEBUG,"<CSQ>\r\n");
  188. sutModemStatus.ServerStatus=CLOSED;
  189. r=ModemGetCSQ(&sutModemStatus.CSQ);
  190. if(SUCCEED==r){
  191. sutModemStatus.ATTimeoutCt=0;
  192. if(sutModemStatus.CSQ>1 && sutModemStatus.CSQ<32){
  193. sutModemStatus.RebootModemCt=0;
  194. sutModemStatus.Step=STEP_CREG;
  195. }else if(++sutModemStatus.CheckCSQCt>20){
  196. sutModemStatus.CheckCSQCt=0;
  197. sutModemStatus.Step=STEP_CARD;
  198. }
  199. }else{
  200. sutModemStatus.Step=STEP_CARD;
  201. }
  202. break;
  203. case STEP_CREG:
  204. SlwTrace(DEBUG,"<CREG>\r\n");
  205. sutModemStatus.ServerStatus=CLOSED;
  206. r=WaitingForCReg(90);
  207. if(SUCCEED==r){
  208. sutModemStatus.CregStatus=OPENED;
  209. sutModemStatus.Step=STEP_PDP;
  210. sutModemStatus.RebootModemCt=0;
  211. }else{//TIMEOUT
  212. sutModemStatus.CregStatus=CLOSED;
  213. ModemReboot(sutModemStatus.RebootModemCt++);
  214. if(sutModemStatus.RebootModemCt>6){
  215. sutModemStatus.Step=STEP_CARD;
  216. }
  217. }
  218. break;
  219. case STEP_PDP:
  220. SlwTrace(DEBUG,"<PDP>\r\n");
  221. sutModemStatus.ServerStatus=CLOSED;
  222. r=ModemOpenPDP();
  223. if(SUCCEED==r){
  224. sutModemStatus.PDPStatus=OPENED;
  225. sutModemStatus.Step=STEP_QUERY_PDP;
  226. sutModemStatus.PDPCloseCt=0;
  227. sutModemStatus.RebootModemCt=0;
  228. }else if(FAIL==r){
  229. sutModemStatus.PDPStatus=CLOSED;
  230. r=ModemClosePDP();
  231. if(SUCCEED==r){
  232. sutModemStatus.PDPCloseCt=0;
  233. sutModemStatus.Step=STEP_CREG;
  234. }else if(FAIL==r){
  235. if(++sutModemStatus.PDPCloseCt>3){
  236. sutModemStatus.PDPCloseCt=0;
  237. ModemReboot(sutModemStatus.RebootModemCt++);
  238. if(sutModemStatus.RebootModemCt>6){
  239. sutModemStatus.RebootModemCt=0;
  240. sutModemStatus.Step=STEP_CARD;
  241. }
  242. }
  243. }else{//ModemClosePDP TIMEOUT
  244. sutModemStatus.PDPStatus=CLOSED;
  245. sutModemStatus.PDPCloseCt=0;
  246. ModemReboot(sutModemStatus.RebootModemCt++);
  247. if(sutModemStatus.RebootModemCt>6){
  248. sutModemStatus.RebootModemCt=0;
  249. sutModemStatus.Step=STEP_CARD;
  250. }
  251. }
  252. }else{//ModemOpenPDP TIMEOUT
  253. sutModemStatus.PDPStatus=CLOSED;
  254. ModemReboot(sutModemStatus.RebootModemCt++);
  255. if(sutModemStatus.RebootModemCt>6){
  256. sutModemStatus.RebootModemCt=0;
  257. sutModemStatus.Step=STEP_CARD;
  258. }
  259. }
  260. break;
  261. case STEP_QUERY_PDP://查询PDP状态
  262. SlwTrace(DEBUG,"<QUERY_PDP>\r\n");
  263. r=ModemQueryPDP();
  264. if(SUCCEED==r){
  265. if(authSocketStatus==0) sutModemStatus.Step=STEP_OPEN_USOCKET;
  266. }else if(FAIL==r){
  267. sutModemStatus.Step=STEP_PDP;
  268. authSocketStatus=0;
  269. }else{//query timeout
  270. authSocketStatus=0;
  271. sutModemStatus.PDPStatus=CLOSED;
  272. ModemReboot(sutModemStatus.RebootModemCt++);
  273. if(sutModemStatus.RebootModemCt>6){
  274. sutModemStatus.RebootModemCt=0;
  275. sutModemStatus.Step=STEP_CARD;
  276. }
  277. }
  278. break;
  279. case STEP_OPEN_USOCKET:
  280. SlwTrace(DEBUG,"<QUERY_USOCKET>\r\n");
  281. r=ModemGetSocket(AUTH_UDP_SOCKET,10);
  282. if(r==FAIL){
  283. r=ConnectAuthServer();
  284. if(SUCCEED==r){
  285. authSocketStatus=1;
  286. sutModemStatus.Step=STEP_QUERY_PDP;
  287. SlwTrace(DEBUG,"Connect server Ok!\r\n");
  288. }else if(FAIL==r){
  289. authSocketStatus=0;
  290. SlwTrace(DEBUG,"Connect server fail!\r\n");
  291. sutModemStatus.Step=STEP_PDP;
  292. }else{
  293. authSocketStatus=0;
  294. SlwTrace(DEBUG,"Connect server timeout!\r\n");
  295. sutModemStatus.Step=STEP_PDP;
  296. }
  297. }else{
  298. authSocketStatus=0;
  299. }
  300. break;
  301. #if 0
  302. case STEP_DNS:
  303. SlwTrace(DEBUG,"<DNS>\r\n");
  304. unsigned char isSaveIP=0;
  305. if(SUCCEED==ModemGetIPbyDNS(sutProductPara.AppServer,AppIp)){
  306. if(AppIp[0]!=sutServerIp.AppServerIP[0] || AppIp[1]!=sutServerIp.AppServerIP[1] ||
  307. AppIp[2]!=sutServerIp.AppServerIP[2] || AppIp[3]!=sutServerIp.AppServerIP[3]){
  308. for(i=0;i<4;i++)sutServerIp.AppServerIP[i]=AppIp[i];
  309. isSaveIP=1;
  310. }
  311. if(SUCCEED==ModemGetIPbyDNS(sutProductPara.FotaServer,AppIp)){
  312. if(AppIp[0]!=sutServerIp.FotaServerIP[0] || AppIp[1]!=sutServerIp.FotaServerIP[1] ||
  313. AppIp[2]!=sutServerIp.FotaServerIP[2] || AppIp[3]!=sutServerIp.FotaServerIP[3]){
  314. for(i=0;i<4;i++)sutServerIp.FotaServerIP[i]=AppIp[i];
  315. isSaveIP=1;
  316. }
  317. sutModemStatus.Step=STEP_SOCKET;
  318. sutModemStatus.GetDNSCt=0;
  319. }
  320. if(isSaveIP){
  321. SaveServerIP();
  322. SlwTrace(DEBUG,"SaveServerIP\r\n");
  323. }
  324. }else if(++sutModemStatus.GetDNSCt>5){
  325. sutModemStatus.GetDNSCt=0;
  326. sutModemStatus.Step=STEP_SOCKET;
  327. }
  328. break;
  329. case STEP_SOCKET:
  330. SlwTrace(DEBUG,"<SOCKET>\r\n");
  331. sutModemStatus.ServerStatus=CLOSED;
  332. r=ModemGetSocket(0,10);
  333. if(r==FAIL){
  334. sutModemStatus.SocketStatus=CLOSED;
  335. if(sutFotaPara.fotaStatus!=FOTA_END){
  336. if(sutModemStatus.FoTaGetFileInfoCt<5){
  337. sutModemStatus.FoTaGetFileInfoCt++;
  338. r=ConnectFotaServer();
  339. }else{
  340. sutFotaPara.fotaStatus=FOTA_END;
  341. ModemCloseSocket(0);
  342. return;
  343. }
  344. }else{
  345. r=ConnectAppServer();
  346. }
  347. if(SUCCEED==r){
  348. sutModemStatus.SocketStatus=OPENED;
  349. sutModemStatus.Step=STEP_LOGIN;
  350. sutModemStatus.SocketStatus=OPENED;
  351. sutModemStatus.CmdNum=sutModemStatus.RecvCmdNum+1;
  352. sutModemStatus.CtrlReSendCt=5;
  353. SlwTrace(DEBUG,"Connect server succeed!\r\n");
  354. }else if(FAIL==r){
  355. sutModemStatus.SocketStatus=CLOSED;
  356. SlwTrace(DEBUG,"Connect server fail!\r\n");
  357. ModemCloseSocket(0);
  358. sutModemStatus.Step=STEP_PDP;
  359. }else{
  360. sutModemStatus.SocketStatus=CLOSED;
  361. SlwTrace(DEBUG,"Connect server timeout!\r\n");
  362. ModemCloseSocket(0);
  363. sutModemStatus.Step=STEP_PDP;
  364. }
  365. }else{
  366. sutModemStatus.SocketStatus=CLOSED;
  367. ModemCloseSocket(0);
  368. }
  369. break;
  370. case STEP_LOGIN:
  371. SlwTrace(DEBUG,"<LOGIN>\r\n");
  372. sutModemStatus.ServerStatus=CLOSED;
  373. //远程升级
  374. if(sutFotaPara.fotaStatus==FOTA_GET_FILEINFO){
  375. sutModemStatus.ServerStatus=CLOSED;
  376. if(++sutFotaPara.reSendCt>FOTA_RE_SEND_CT_MAX){
  377. sutFotaPara.reSendCt=0;
  378. r=FoTaGetFileInfo(0);
  379. if(FAIL==r){
  380. sutModemStatus.Step=STEP_SOCKET;
  381. }else if(TIMEOUT==r){
  382. sutModemStatus.Step=STEP_CARD;
  383. }
  384. }
  385. if(sutModemStatus.SocketStatus==CLOSED){
  386. sutModemStatus.Step=STEP_SOCKET;
  387. }
  388. return;
  389. }else if(sutFotaPara.fotaStatus==FOTA_GET_FILEDATA){
  390. sutModemStatus.FoTaGetFileInfoCt=0;
  391. sutModemStatus.ServerStatus=OPENED;
  392. if(++sutFotaPara.reSendCt>FOTA_RE_SEND_CT_MAX){
  393. sutFotaPara.reSendCt=0;
  394. r=FoTaGetFileData(0);
  395. if(FAIL==r){
  396. sutModemStatus.Step=STEP_SOCKET;
  397. }else if(TIMEOUT==r){
  398. sutModemStatus.Step=STEP_CARD;
  399. }
  400. }
  401. if(sutModemStatus.SocketStatus==CLOSED){
  402. sutModemStatus.Step=STEP_SOCKET;
  403. }
  404. return;
  405. }
  406. //正常工作
  407. sutModemStatus.ServerStatus=CLOSED;
  408. if(sutModemStatus.CmdNum!=sutModemStatus.RecvCmdNum){
  409. if(++sutModemStatus.CtrlReSendCt>5){
  410. sutModemStatus.CtrlReSendCt=0;
  411. r=SendLogin();
  412. if(FAIL==r){
  413. sutModemStatus.Step=STEP_SOCKET;
  414. }else if(TIMEOUT==r){
  415. sutModemStatus.Step=STEP_CARD;
  416. }
  417. }
  418. }else{
  419. if(sutModemStatus.UnauthorizedCt>0){
  420. sutModemStatus.UnauthorizedCt--;
  421. if(sutModemStatus.UnauthorizedCt==0){
  422. sutModemStatus.CmdNum=sutModemStatus.RecvCmdNum+1;
  423. }
  424. }else{
  425. sutModemStatus.Step=STEP_SERVICE;
  426. sutModemStatus.CtrlIntervalSendCt=sutDeviceConfig.UploadInterval;
  427. }
  428. }
  429. if(sutModemStatus.SocketStatus==CLOSED){
  430. sutModemStatus.Step=STEP_SOCKET;
  431. }
  432. break;
  433. case STEP_SERVICE:
  434. SlwTrace(DEBUG,"<SERVICE>\r\n");
  435. sutModemStatus.ServerStatus=OPENED;
  436. if(++sutModemStatus.CtrlIntervalSendCt>=sutDeviceConfig.UploadInterval || sutTestData.TcUpdated){
  437. sutModemStatus.CtrlIntervalSendCt=0;
  438. sutModemStatus.CtrlReSendCt=5;
  439. sutModemStatus.CmdNum++;
  440. sutTestData.TcUpdated=0;
  441. }
  442. if(sutModemStatus.CmdNum!=sutModemStatus.RecvCmdNum){
  443. if(++sutModemStatus.CtrlReSendCt>5){
  444. sutModemStatus.CtrlReSendCt=0;
  445. r=SendCurPosition();
  446. if(FAIL==r){
  447. sutModemStatus.Step=STEP_SOCKET;
  448. }else if(TIMEOUT==r){
  449. sutModemStatus.Step=STEP_CARD;
  450. }
  451. }
  452. }
  453. break;
  454. #endif
  455. }
  456. }
  457. /*************************************************************
  458. *将s转为数字
  459. "123abc" --> 123
  460. "123" --> 123
  461. **************************************************************/
  462. int stou(char *s)
  463. {
  464. int i;
  465. int j;
  466. char buf[6];
  467. char d;
  468. for(i=0;i<5;i++){
  469. d=s[i];
  470. if(d>0x39 || d<0x30)break;
  471. buf[i]=d;
  472. }
  473. buf[i]=0;
  474. return atoi(buf);
  475. }
  476. //在“123,xxx" 这样的字符串里截取','前的数字转为整形出
  477. unsigned short atous(char *pMsg)
  478. {
  479. int i=0;
  480. char data[6];//0~65535
  481. char *p=pMsg;
  482. memset(data,0,6);
  483. while(*p){
  484. if(*p==',')break;
  485. data[i++]=*p;
  486. if(i>5)break;
  487. p++;
  488. }
  489. if(*p==0)return 0;
  490. return atoi(data);
  491. }
  492. void TraceData(unsigned char *pData,unsigned short DataLen)
  493. {
  494. int i;
  495. char buf[10];
  496. sprintf(buf,"[%d]",DataLen);
  497. SlwTrace(DEBUG,buf);
  498. if(DataLen>100)DataLen=100;
  499. for(i=0;i<DataLen;i++){
  500. sprintf(buf,"%02X",pData[i]);
  501. SlwTrace(DEBUG,buf);
  502. }
  503. if(DataLen>255)SlwTrace(DEBUG,"...");
  504. SlwTrace(DEBUG,"\r\n");
  505. }
  506. /*******************************************************************
  507. *AtHandle
  508. +QIURC: "recv",0,2\r\nXXXXXX
  509. +QIURC: "recv",0,20\r\nXXXXXXXXXXXXXXXXXXXX
  510. +QIURC: "recv",0,200\r\nXXXXXX
  511. +QIURC: "recv",0,2000\r\nXXXXXX
  512. ********************************************************************/
  513. void AtHandle(char *pMsg,unsigned short MsgLen)
  514. {
  515. unsigned short UploadInterval,SamplingInterval;
  516. unsigned short AckID,AckNum;
  517. unsigned char Result;
  518. unsigned short NewVer;
  519. int cmd;
  520. int i;
  521. char buf[30];
  522. unsigned char *pData;
  523. unsigned short len;
  524. unsigned char socket;
  525. if(ModemStrCmp(pMsg,"+QIURC: \"closed\",0")){
  526. sutModemStatus.SocketStatus=CLOSED;
  527. return;
  528. }
  529. //
  530. if(MsgLen<20 || 1!=ModemStrCmp(pMsg,"+QIURC: \"recv\",")){
  531. return;
  532. }
  533. socket=pMsg[15]-0x30;
  534. sutModemStatus.ServerOfflineCt=0;
  535. //+QIURC: "recv",0,25
  536. len=atoi((char *)&pMsg[17]);
  537. if(len<1 || len>1035)return;//fota 一包数据最大可达1035bytes
  538. //printf("AtHandle=%d\r\n",MsgLen);
  539. if(len<10){//+QIURC: "recv",0,5\r\nXXX
  540. pData=(unsigned char *)&pMsg[20];
  541. }else if(len<100){//+QIURC: "recv",0,25\r\nXXX
  542. pData=(unsigned char *)&pMsg[21];
  543. }else if(len<1000){//+QIURC: "recv",0,255\r\nXXX
  544. pData=(unsigned char *)&pMsg[22];
  545. }else{////+QIURC: "recv",0,1255\r\nXXX
  546. pData=(unsigned char *)&pMsg[23];
  547. }
  548. //TraceData(pData,len);
  549. //FoTa升级接收数据处理
  550. // if(sutFotaPara.fotaStatus!=FOTA_END){
  551. // FoTaRecvData(pData,len);
  552. // return;
  553. // }
  554. // static unsigned char testdata[]={
  555. // 0x7E,0x09,0x29,0x00,0x28,0x00,0x01,0x90,0x21,0x00,0x02,0x17,0x7D,0x01,0x52,0x54,0x4C,0x32,0x35,0x32,
  556. // 0x00,0x00,0x00,0x01,0x00,0x66,0xA6,0x94,0xDC,0xD8,0x38,0x39,0x38,0x36,0x30,0x37,0x42,0x33,0x31,0x35,
  557. // 0x31,0x37,0x33,0x30,0x34,0x38,0x37,0x33,0x37,0x35,0x00,0x05,0x00,0x05,0x8C,0x7E
  558. // };
  559. // len=sizeof(testdata);
  560. // memcpy(pData,testdata,len);
  561. if(socket!=AUTH_UDP_SOCKET) return;
  562. //正常应用接收数据处理
  563. cmd=CheckPacket(pData,len);
  564. if(cmd<0){
  565. sprintf(buf,"CheckPacket Err=%d\r\n",cmd);
  566. SlwTrace(DEBUG,buf);
  567. return;
  568. }
  569. switch(cmd)
  570. {
  571. case AUTH_ACK_ID:
  572. //8930 0027 000123456789 0000 37ffd6054b543335504302430000000000000000000000000000000000000000 00 1f 04 6735d710 40
  573. authBubiaoHandleRecv(pData+12);
  574. break;
  575. case 0x8200://上传应答
  576. //pData=8001 0005 000000000000 0000 01 02 03 04 86
  577. // ID 属性 设备编号 流水号 结果 红外使能 摄像头使能 复位使能 校验
  578. SlwTrace(DEBUG,"<<[8001]\r\n");
  579. AckNum=((unsigned short)pData[10]<<8) | pData[11];
  580. sutModemStatus.RecvCmdNum=AckNum;
  581. if(pData[12]!=0){//结果 0--成功
  582. return;
  583. };
  584. sutTestData.InfraredEn=pData[13];
  585. sutTestData.CameraEn=pData[14];
  586. sutTestData.ResetEn=pData[15];
  587. sprintf(buf,"IEn=%02X CEn=%02X REn=%02X\r\n",sutTestData.InfraredEn,sutTestData.CameraEn,sutTestData.ResetEn);
  588. SlwTrace(DEBUG,buf);
  589. break;
  590. case 0x8929://登陆应答
  591. //pData=8929 000E 000190210001 0018 190217142738 01 00 0000 0500 0500 01
  592. // ID Len 编码 流水号 时间 验证 升级 新版本 采样间隔 上传间隔 校验
  593. SlwTrace(DEBUG,"<<[8929]\r\n");
  594. AckNum=((unsigned short)pData[10]<<8)|pData[11];
  595. sutModemStatus.RecvCmdNum=AckNum;
  596. RTC_Set(&pData[12]);
  597. //鉴权结果 0--成功 >0 未成功需要继续等N分钟再重新登陆
  598. sutModemStatus.UnauthorizedCt=(unsigned short)pData[18]*60;
  599. if(sutModemStatus.UnauthorizedCt>0){
  600. SlwTrace(DEBUG,"Unauthorized!\r\n");
  601. sprintf(buf,"UCt=%d\r\n",sutModemStatus.UnauthorizedCt);
  602. SlwTrace(DEBUG,buf);
  603. }
  604. #if 0 //升级测试
  605. *(pData+19)=1;
  606. *(pData+20)=0;
  607. *(pData+21)=108;
  608. #endif
  609. if(pData[19]==1){//使能升级
  610. NewVer=((unsigned short)pData[20]<<8)|pData[21];
  611. if(NewVer!=0 && NewVer>sutProductPara.SoftwareVer && sutModemStatus.FoTaGetFileInfoCt<5){
  612. sprintf(buf,"CurVer=%d NewVer=%d\r\n",sutProductPara.SoftwareVer,NewVer);
  613. SlwTrace(DEBUG,buf);
  614. SlwTrace(DEBUG,"Upgrade now!\r\n");
  615. sutFotaPara.targetVersion=NewVer;
  616. sutFotaPara.fotaStatus=FOTA_GET_FILEINFO;
  617. ModemCloseSocket(0);
  618. sutModemStatus.Step=STEP_SOCKET;
  619. return;
  620. }else{
  621. sutModemStatus.FoTaGetFileInfoCt=0;
  622. }
  623. }else sutModemStatus.FoTaGetFileInfoCt=0;
  624. UploadInterval=((unsigned short)pData[22]<<8)|pData[23];
  625. SamplingInterval=((unsigned short)pData[24]<<8)|pData[25];
  626. if((sutDeviceConfig.UploadInterval!=UploadInterval&& UploadInterval>=5) ||
  627. (sutDeviceConfig.SamplingInterval!=SamplingInterval && SamplingInterval>=5)){
  628. sutDeviceConfig.UploadInterval=UploadInterval;
  629. sutDeviceConfig.SamplingInterval=SamplingInterval;
  630. SaveDeviceConfigToFlash();
  631. }
  632. break;
  633. }
  634. }
  635. /*
  636. 等多久后重启
  637. */
  638. void WaitForReboot(unsigned short time)
  639. {
  640. char buf[30];
  641. unsigned short t=time;
  642. while(t--){
  643. sprintf(buf,"WaitForReboot...%d\r\n",t);
  644. SlwTrace(DEBUG,buf);
  645. os_dly_wait(100);
  646. }
  647. SlwTrace(DEBUG,"WillBeReboot!\r\n");
  648. while(1);
  649. }
  650. /*
  651. 根据IMEI号后9位数转成unsigned long型号
  652. */
  653. unsigned long getAsnByIMEI(char *pIMEI)
  654. {
  655. char buf[10];
  656. int i;
  657. for(i=0;i<9;i++){
  658. buf[i]=pIMEI[6+i];
  659. }
  660. buf[9]=0;
  661. return atol(buf);
  662. }
  663. /*******************************************************************
  664. *ModemTask
  665. ********************************************************************/
  666. __task void ModemTask(void)
  667. {
  668. unsigned long ASN;
  669. int i,j;
  670. char *p;
  671. char temp[22];
  672. int r;
  673. RunMake(THIS_FILE_ID);
  674. unsigned char sim_card_check_ct=0;
  675. SlwTrace(DEBUG,"ModemTask Start...\r\n");
  676. memset(&sutModemStatus,0,sizeof(SUT_MODEM_STATUS));
  677. NetProcess(1);
  678. //空中升级初始化
  679. MyFoTaInit();
  680. sutModemStatus.WorkStatus=CLOSED;
  681. MODEM_TASK_BEGIN:
  682. sutModemStatus.CardStatus=CLOSED;
  683. sutModemStatus.CregStatus=CLOSED;
  684. sutModemStatus.PDPStatus=CLOSED;
  685. sutModemStatus.SocketStatus=CLOSED;
  686. sutModemStatus.ServerStatus=CLOSED;
  687. //初始化模块
  688. if(SUCCEED!=ModemInit()){
  689. SlwTrace(DEBUG,"Will be reboot!\r\n");
  690. while(1);
  691. }
  692. sutModemStatus.OnceWorked=1;
  693. //检查SIM是否在位
  694. for(i=0;i<20;i++){
  695. if(SUCCEED==ModemGetCardStatus())break;
  696. }
  697. if(i==20){
  698. SlwTrace(DEBUG,"SIM card not ready!reboot and check again!\r\n");
  699. ModemReboot(sutModemStatus.RebootModemCt);
  700. if(sutModemStatus.RebootModemCt<50)sutModemStatus.RebootModemCt++;
  701. goto MODEM_TASK_BEGIN;
  702. }
  703. sutModemStatus.RebootModemCt=0;
  704. sutModemStatus.CardStatus=OPENED;
  705. //获取CCID
  706. SlwTrace(DEBUG,"Get ICCID...\r\n");
  707. memset(temp,0,sizeof(temp));
  708. for(i=0;i<5;i++){
  709. if(SUCCEED==ModemGetCCID(temp))break;
  710. os_dly_wait(100);
  711. }
  712. if(i<5){
  713. if(0!=strcmp(sutDeviceConfig.CCID,temp)){
  714. strncpy(sutDeviceConfig.CCID,temp,sizeof(sutDeviceConfig.CCID));
  715. SaveDeviceConfigToFlash();
  716. }
  717. }
  718. while(1)
  719. {
  720. if(g_ucModemTaskSleep){
  721. os_dly_wait(0xffff);
  722. continue;
  723. }else{
  724. os_dly_wait(1);
  725. }
  726. r=MsgQueueAccept(&ModemMsgQueue,ModemMsgBuf,sizeof(ModemMsgBuf));
  727. if(r>0){
  728. sutModemStatus.UartInactiveCt=0;
  729. ModemMsgBuf[r]=0;
  730. SlwTrace(DEBUG,ModemMsgBuf);//打印MODEM信息
  731. AtHandle(ModemMsgBuf,r);
  732. //sprintf(buf,"Remaining MsgNum=%d\r\n",Uart3MsgQueue.MsgNum);
  733. //SlwTrace(DEBUG,buf);
  734. }else if(r<0){
  735. // sprintf(buf,"ModemMsgBuf Over![%d]\r\n",r);
  736. SlwTrace(DEBUG,"ModemMsgBuf Over!\r\n");
  737. // }else if(g_ucUARTSel!=COM_SEL_MODEM){
  738. }
  739. if(g_ucUARTSel!=COM_SEL_MODEM){ //
  740. ModemSendAuthReqHandle();
  741. NetProcess(0);
  742. }
  743. }
  744. }
  745. /*******************************************************************************/