NetTask.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /********************************************************************************
  2. * File Name: NetTask.c
  3. * Function Describe:The NetTask for the system
  4. * Relate Module:
  5. * Explain: Hardware version is HS110C
  6. * Writer: ShiLiangWen
  7. * Date: 2015.4.12
  8. *******************************************************************************/
  9. #define THIS_FILE_ID 13
  10. //--------------------------------------------------------------------------------
  11. #include "includes.h"
  12. OS_TID idNetTask;
  13. int g_iCSQ=-1;
  14. int g_iModemVer=0;
  15. typedef enum {INVALID=0,OPEN=1,CLOSE=2} STATUE;
  16. //======================================
  17. static STATUE GpsServerStatus=CLOSE;
  18. /*******************************************************************************
  19. 校验
  20. *******************************************************************************/
  21. unsigned char GetCheck(unsigned char *data, unsigned short datalen)
  22. {
  23. unsigned char ck=0;
  24. int i;
  25. for(i=0;i<datalen;i++){
  26. ck^=data[i];
  27. }
  28. return ck;
  29. }
  30. /*****************************************************************************
  31. *发送数据封包
  32. format: 0x29 0x29 CMD Len PSN[4] Data Check 0x0D
  33. Len=DataLen+6
  34. ******************************************************************************/
  35. int SendBufPack(char *pBuf,unsigned char cmd,unsigned short DataLen,unsigned char *data)
  36. {
  37. SUTDS ds;
  38. SUTDL dl;
  39. int i,j;
  40. if(DataLen>UART3_TX_BUFFER_SIZE)return 0;
  41. //包头
  42. pBuf[0]=0x29;//Head
  43. pBuf[1]=0x29;//Head
  44. pBuf[2]=cmd;//CMD
  45. //DataLen
  46. ds.Data.usData=DataLen+6;
  47. pBuf[3]=ds.Data.ucData.b2;
  48. pBuf[4]=ds.Data.ucData.b1;
  49. //PSN
  50. dl.Data.ulData=sutProductPara.PSN;
  51. pBuf[5]=dl.Data.ucData.b4;
  52. pBuf[6]=dl.Data.ucData.b3;
  53. pBuf[7]=dl.Data.ucData.b2;
  54. pBuf[8]=dl.Data.ucData.b1;
  55. //Data
  56. j=9;
  57. for(i=0;i<DataLen;i++){
  58. pBuf[j++]=data[i];
  59. }
  60. //check
  61. pBuf[j++]= GetCheck(TxBuffer3,(DataLen+6));
  62. pBuf[j++]=0x0D;
  63. return j;
  64. }
  65. /********************************************************************************************************
  66. *SendTick 发送心跳包
  67. *********************************************************************************************************/
  68. void SendTick(void)
  69. {
  70. int len;
  71. char AT[100];
  72. len=SendBufPack((char *)TxBuffer3,0,0,NULL);
  73. sprintf(AT,"AT+ZIPSEND=1,%d\r",len);
  74. ModemSendAT(AT);
  75. ModemSendData(TxBuffer3,len);
  76. }
  77. /*******************************************************************
  78. *GetCSQ
  79. ********************************************************************/
  80. int GetCSQ(char* msg)
  81. {
  82. int CSQ;
  83. if( msg[1]!='C' || msg[2]!='S' || msg[3]!='Q' || msg[4]!=':')return -1;
  84. CSQ=atoi(&msg[5]);
  85. return CSQ;
  86. }
  87. /*******************************************************************
  88. *GetPPPStatus
  89. ********************************************************************/
  90. STATUE GetPPPStatus(char *msg)
  91. {
  92. //if(ModemStrCmp(msg,"+ZPPPSTATUS:")){ //+ZPPPSTATUS: OPENED
  93. if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
  94. return OPEN;
  95. }else{
  96. return CLOSE;
  97. }
  98. //} else return INVALID;
  99. }
  100. STATUE GetGpsServerStatus(char *msg){
  101. if(ModemStrCmp(msg,"+RECV:")){
  102. if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
  103. return OPEN;
  104. }else{
  105. return CLOSE;
  106. }
  107. } else return INVALID;
  108. }
  109. /*******************************************************************
  110. *NetRecvMsgHandle
  111. ********************************************************************/
  112. int NetRecvMsgHandle(char *msg)
  113. {
  114. if(!ModemStrCmp(msg,"+ZIPRECV:0,"))//+ZIPRECV:0,10,XXXX
  115. return 0;
  116. }
  117. /*******************************************************************
  118. *GetIPStatus
  119. ********************************************************************/
  120. STATUE GetIPStatus(char *msg)
  121. {
  122. //if(ModemStrCmp(msg,"+ZIPSTATUS:")){ //+ZIPSTATUS: ESTABLISHED
  123. if(msg[12]=='E' && msg[14]=='T' && msg[22]=='D'){
  124. return OPEN;
  125. }else{
  126. return CLOSE;
  127. }
  128. //}else return INVALID;
  129. }
  130. /*******************************************************************
  131. *GetIPStatus
  132. ********************************************************************/
  133. STATUE GetIPStatusu(char *msg)
  134. {
  135. //if(ModemStrCmp(msg,"+ZIPSTATUSU:")){ //+ZIPSTATUSU: ESTABLISHED
  136. if(msg[13]=='E' && msg[15]=='T' && msg[23]=='D'){
  137. return OPEN;
  138. }else{
  139. return CLOSE;
  140. }
  141. //}else return INVALID;
  142. }
  143. /*******************************************************************
  144. *GetIPStatus
  145. 返回:0--未获取 1--获取成功
  146. ********************************************************************/
  147. int GetCNUM(void)
  148. {
  149. /*
  150. int timerout=300;
  151. int i,len;
  152. char *msg;
  153. INT8U err;
  154. int ct=100;
  155. while(timerout){
  156. timerout--;
  157. if(++ct>100){
  158. ct=0;
  159. ModemSendAT("AT+CNUM?\r\n");
  160. OSQFlush(ModemQ);
  161. }
  162. msg = (char *)OSQPend(ModemQ, 1, &err);
  163. if(err==OS_ERR_NONE){
  164. if(ModemStrCmp(msg,"+CNUM:")){ //+CNUM:1064910139657
  165. len=strlen(&msg[6]);
  166. if(15==len){
  167. for(i=0;i<13;i++)sutProductPara.CNUM[i]=msg[6+i];
  168. sutNetPara.CNUM[13]=0;
  169. return 1;
  170. }
  171. }
  172. }
  173. }*/
  174. return 0;
  175. }
  176. /*******************************************************************
  177. *GetIPStatus
  178. 返回:0--未获取 1--获取成功
  179. ********************************************************************/
  180. int GetUIMID(void)
  181. {
  182. /*
  183. int timerout=300;
  184. int i,len;
  185. char *msg;
  186. INT8U err;
  187. int ct=100;
  188. while(timerout){
  189. timerout--;
  190. if(++ct>100){
  191. ct=0;
  192. ModemSendAT("AT+CIMI\r\n");
  193. OSQFlush(ModemQ);
  194. }
  195. msg = (char *)OSQPend(ModemQ, 1, &err);
  196. if(err==OS_ERR_NONE){
  197. if(ModemStrCmp(msg,"+CIMI:")){ //+CIMI: 460037590079119
  198. len=strlen(&msg[7]);
  199. if(17==len){
  200. for(i=0;i<15;i++)sutNetPara.UIMID[i]=msg[7+i];
  201. sutNetPara.UIMID[15]=0;
  202. return 1;
  203. }
  204. }
  205. }
  206. }
  207. */
  208. return 0;
  209. }
  210. /*************************************************************
  211. *
  212. *************************************************************/
  213. void RecvModemData(char *msg)
  214. {
  215. int i;
  216. char *pData;
  217. unsigned short DataLen;
  218. unsigned char cmd;
  219. char tmp[5];
  220. char buf[100];
  221. //if(!ModemStrCmp(msg,"+ZIPRECV:"))return;
  222. SlwTrace(DEBUG,"\r\nRECV");
  223. DataLen=atoi((char *)&msg[11]);
  224. //printf("\r\nl=%d\r\n",DataLen);
  225. if(DataLen<10){ //+ZIPRECV:1,8,xxx
  226. pData=msg+13;
  227. }else if(DataLen<100){
  228. pData=msg+14;
  229. }else if(DataLen<(UART3_RX_BUFFER_SIZE-17)){
  230. pData=msg+15;
  231. }else{//非法
  232. return;
  233. }
  234. #ifdef SLW_DEBUG
  235. buf[0]=0;
  236. sprintf(buf,"[%d]",DataLen);
  237. for(i=0;i<DataLen;i++){
  238. sprintf(tmp,"%02x,",pData[i]);
  239. strcat(buf,tmp);
  240. }
  241. strcat(buf,"\r\n");
  242. SlwTrace(DEBUG,buf);
  243. #endif
  244. //处理
  245. cmd=GpsServerProcess((unsigned char *)pData,DataLen);
  246. }
  247. /*******************************************************************
  248. *MainTask
  249. *主任务
  250. ********************************************************************/
  251. __task void NetTask(void)
  252. {
  253. unsigned char SecFlag=0;
  254. static STATUE PPPStatus=CLOSE;
  255. static STATUE IPStatus=CLOSE;
  256. static unsigned char sucNetStatusCt=0;
  257. static unsigned short susGpsTimingSendCt=0;
  258. SUT_MODEM_RECV_MSG *pMsg;
  259. STATUE status;
  260. int csq;
  261. //char msg[200];
  262. int len;
  263. INT8U err;
  264. char buf[20];
  265. //Obtain own system task identification number
  266. idNetTask = os_tsk_self();
  267. SlwTrace(INF,"Net Task start!\r\n");
  268. memset((unsigned char *)&sutPocStatus,0,sizeof(SUT_POC_STATUS));
  269. SetLedIndicator(IndNoNet);
  270. SlwTrace(INF,"ModemInit...");
  271. if(ModemInit()){
  272. SlwTrace(INF," OK!\r\n");
  273. }else{
  274. SlwTrace(INF," Fail\r\n");
  275. SlwTrace(INF,"The System Stop!\r\n");
  276. SetLedIndicator(IndModemErr);
  277. while(1);
  278. }
  279. ModemSendAT("ATE0\r\n");//关闭回显
  280. //Get ModemVer
  281. g_iModemVer=ModemGetVer();
  282. sprintf(buf,"ModemVer=%d\r\n",g_iModemVer);
  283. SlwTrace(INF,buf);
  284. //语音提示 "欢迎使用公网集群对讲机"
  285. SpeakerEnable();
  286. ModemSendAT("AT+ZTTS=1,\"226BCE8F7F4F28756C51517FC696A47FF95BB28B3A67\"\r");
  287. //Gps Init
  288. GpsDataInit();
  289. #ifdef GPS_ENABLE
  290. GPSInit();
  291. #endif
  292. PPPStatus=INVALID;
  293. IPStatus=INVALID;
  294. GpsServerStatus=INVALID;
  295. while(1)
  296. {
  297. //Modem Uart消息处理 每次大约等待10ms
  298. if(OS_R_TMO!=os_mbx_wait(MsgBox,(void **)&pMsg,1)){
  299. SlwTrace(INF,(char *)pMsg->MsgData);
  300. if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:") || 0==ModemStrCmp((char *)pMsg->MsgData,"+ZTTS:0")){
  301. PocHandle((char *)pMsg->MsgData);
  302. }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZIPRECV:")){
  303. RecvModemData((char *)pMsg->MsgData);
  304. }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+CSQ:")){
  305. g_iCSQ=GetCSQ((char *)pMsg->MsgData);
  306. if(g_iCSQ==99 ){
  307. sutPocStatus.Logined=0;//POC 离线
  308. SetLedIndicator(IndNoNet);
  309. }
  310. }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZPPPSTATUS:")){
  311. PPPStatus=GetPPPStatus((char *)pMsg->MsgData);
  312. }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZIPSTATUS:")){
  313. IPStatus=GetIPStatus((char *)pMsg->MsgData);
  314. }
  315. _free_box(mpool,pMsg);
  316. }
  317. #ifdef GPS_ENABLE
  318. //GPS Uart消息处理
  319. if(g_usUart2RecvLen){
  320. process_nema((char *)RxBuffer2);
  321. g_usUart2RecvLen=0;
  322. }
  323. #endif
  324. //=======控制大概每1秒往下执行=========
  325. if(++SecFlag>=100)SecFlag=0;
  326. else continue;
  327. if(0==sutPocStatus.Logined)SetLedIndicator(IndNoNet);
  328. //---控制定时发送GPS数据 ---
  329. if(IPStatus==OPEN && sutGpsInfo.isServerLogin){
  330. if(++susGpsTimingSendCt>sutGpsInfo.SendTime){
  331. susGpsTimingSendCt=0;
  332. GpsTimingSendPos();
  333. }
  334. }else{
  335. susGpsTimingSendCt=0;
  336. }
  337. //--------------------
  338. if(++sucNetStatusCt>4)sucNetStatusCt=0;
  339. switch(sucNetStatusCt)
  340. {
  341. case 0:
  342. ModemSendAT("AT+CSQ?\r");
  343. break;
  344. #ifdef GPS_ENABLE
  345. case 1:
  346. ModemSendAT("AT+ZPPPSTATUS\r");
  347. break;
  348. case 2:
  349. if(PPPStatus==OPEN){
  350. //SlwTrace(DEBUG,"AT+ZIPSTATUS=0\r\n");
  351. ModemSendAT("AT+ZIPSTATUS=0\r");
  352. }else{
  353. //SlwTrace(DEBUG,"AT+ZPPPOPEN\r\n");
  354. ModemSendAT("AT+ZPPPOPEN\r");
  355. }
  356. break;
  357. case 3:
  358. if(IPStatus!=OPEN && PPPStatus==OPEN){
  359. sutGpsInfo.isServerLogin=0;
  360. sprintf(buf,"AT+ZIPSETUP=0,%s,%d\r\n",sutProductPara.GIP,sutProductPara.GpsPort);
  361. //SlwTrace(DEBUG,buf);
  362. ModemSendAT(buf);
  363. }
  364. break;
  365. case 4:
  366. if(IPStatus==OPEN && 0==sutGpsInfo.isServerLogin){
  367. if(sutGpsInfo.isGpsWork)GpsServerConect();
  368. }
  369. break;
  370. #endif
  371. }
  372. }
  373. }