123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- /********************************************************************************
- * File Name: NetTask.c
- * Function Describe:The NetTask for the system
- * Relate Module:
- * Explain: Hardware version is HS110C
- * Writer: ShiLiangWen
- * Date: 2015.4.12
- *******************************************************************************/
- #define THIS_FILE_ID 13
- //--------------------------------------------------------------------------------
- #include "includes.h"
- OS_TID idNetTask;
- int g_iCSQ=-1;
- int g_iModemVer=0;
- typedef enum {INVALID=0,OPEN=1,CLOSE=2} STATUE;
- //======================================
- static STATUE GpsServerStatus=CLOSE;
- /*******************************************************************************
- 校验
- *******************************************************************************/
- unsigned char GetCheck(unsigned char *data, unsigned short datalen)
- {
- unsigned char ck=0;
- int i;
- for(i=0;i<datalen;i++){
- ck^=data[i];
- }
- return ck;
- }
- /*****************************************************************************
- *发送数据封包
- format: 0x29 0x29 CMD Len PSN[4] Data Check 0x0D
- Len=DataLen+6
- ******************************************************************************/
- int SendBufPack(char *pBuf,unsigned char cmd,unsigned short DataLen,unsigned char *data)
- {
- SUTDS ds;
- SUTDL dl;
- int i,j;
- if(DataLen>UART3_TX_BUFFER_SIZE)return 0;
- //包头
- pBuf[0]=0x29;//Head
- pBuf[1]=0x29;//Head
- pBuf[2]=cmd;//CMD
- //DataLen
- ds.Data.usData=DataLen+6;
- pBuf[3]=ds.Data.ucData.b2;
- pBuf[4]=ds.Data.ucData.b1;
- //PSN
- dl.Data.ulData=sutProductPara.PSN;
- pBuf[5]=dl.Data.ucData.b4;
- pBuf[6]=dl.Data.ucData.b3;
- pBuf[7]=dl.Data.ucData.b2;
- pBuf[8]=dl.Data.ucData.b1;
- //Data
- j=9;
- for(i=0;i<DataLen;i++){
- pBuf[j++]=data[i];
- }
- //check
- pBuf[j++]= GetCheck(TxBuffer3,(DataLen+6));
- pBuf[j++]=0x0D;
- return j;
- }
- /********************************************************************************************************
- *SendTick 发送心跳包
- *********************************************************************************************************/
- void SendTick(void)
- {
- int len;
- char AT[100];
- len=SendBufPack((char *)TxBuffer3,0,0,NULL);
- sprintf(AT,"AT+ZIPSEND=1,%d\r",len);
- ModemSendAT(AT);
- ModemSendData(TxBuffer3,len);
- }
- /*******************************************************************
- *GetCSQ
- ********************************************************************/
- int GetCSQ(char* msg)
- {
- int CSQ;
- if( msg[1]!='C' || msg[2]!='S' || msg[3]!='Q' || msg[4]!=':')return -1;
- CSQ=atoi(&msg[5]);
- return CSQ;
- }
- /*******************************************************************
- *GetPPPStatus
- ********************************************************************/
- STATUE GetPPPStatus(char *msg)
- {
- //if(ModemStrCmp(msg,"+ZPPPSTATUS:")){ //+ZPPPSTATUS: OPENED
- if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
- return OPEN;
- }else{
- return CLOSE;
- }
- //} else return INVALID;
- }
- STATUE GetGpsServerStatus(char *msg){
- if(ModemStrCmp(msg,"+RECV:")){
- if(msg[13]=='O' && msg[15]=='E' && msg[18]=='D'){ //OPENED
- return OPEN;
- }else{
- return CLOSE;
- }
- } else return INVALID;
- }
- /*******************************************************************
- *NetRecvMsgHandle
- ********************************************************************/
- int NetRecvMsgHandle(char *msg)
- {
- if(!ModemStrCmp(msg,"+ZIPRECV:0,"))//+ZIPRECV:0,10,XXXX
- return 0;
-
- }
- /*******************************************************************
- *GetIPStatus
- ********************************************************************/
- STATUE GetIPStatus(char *msg)
- {
- //if(ModemStrCmp(msg,"+ZIPSTATUS:")){ //+ZIPSTATUS: ESTABLISHED
- if(msg[12]=='E' && msg[14]=='T' && msg[22]=='D'){
- return OPEN;
- }else{
- return CLOSE;
- }
- //}else return INVALID;
- }
- /*******************************************************************
- *GetIPStatus
- ********************************************************************/
- STATUE GetIPStatusu(char *msg)
- {
- //if(ModemStrCmp(msg,"+ZIPSTATUSU:")){ //+ZIPSTATUSU: ESTABLISHED
- if(msg[13]=='E' && msg[15]=='T' && msg[23]=='D'){
- return OPEN;
- }else{
- return CLOSE;
- }
- //}else return INVALID;
- }
- /*******************************************************************
- *GetIPStatus
- 返回:0--未获取 1--获取成功
- ********************************************************************/
- int GetCNUM(void)
- {
- /*
- int timerout=300;
- int i,len;
- char *msg;
- INT8U err;
- int ct=100;
- while(timerout){
- timerout--;
- if(++ct>100){
- ct=0;
- ModemSendAT("AT+CNUM?\r\n");
- OSQFlush(ModemQ);
- }
-
- msg = (char *)OSQPend(ModemQ, 1, &err);
- if(err==OS_ERR_NONE){
- if(ModemStrCmp(msg,"+CNUM:")){ //+CNUM:1064910139657
- len=strlen(&msg[6]);
- if(15==len){
- for(i=0;i<13;i++)sutProductPara.CNUM[i]=msg[6+i];
- sutNetPara.CNUM[13]=0;
- return 1;
- }
- }
- }
- }*/
- return 0;
- }
- /*******************************************************************
- *GetIPStatus
- 返回:0--未获取 1--获取成功
- ********************************************************************/
- int GetUIMID(void)
- {
- /*
- int timerout=300;
- int i,len;
- char *msg;
- INT8U err;
- int ct=100;
- while(timerout){
- timerout--;
- if(++ct>100){
- ct=0;
- ModemSendAT("AT+CIMI\r\n");
- OSQFlush(ModemQ);
- }
- msg = (char *)OSQPend(ModemQ, 1, &err);
- if(err==OS_ERR_NONE){
- if(ModemStrCmp(msg,"+CIMI:")){ //+CIMI: 460037590079119
- len=strlen(&msg[7]);
- if(17==len){
- for(i=0;i<15;i++)sutNetPara.UIMID[i]=msg[7+i];
- sutNetPara.UIMID[15]=0;
- return 1;
- }
- }
- }
- }
- */
- return 0;
- }
- /*************************************************************
- *
- *************************************************************/
- void RecvModemData(char *msg)
- {
- int i;
- char *pData;
- unsigned short DataLen;
- unsigned char cmd;
- char tmp[5];
- char buf[100];
- //if(!ModemStrCmp(msg,"+ZIPRECV:"))return;
- SlwTrace(DEBUG,"\r\nRECV");
-
- DataLen=atoi((char *)&msg[11]);
- //printf("\r\nl=%d\r\n",DataLen);
- if(DataLen<10){ //+ZIPRECV:1,8,xxx
- pData=msg+13;
- }else if(DataLen<100){
- pData=msg+14;
- }else if(DataLen<(UART3_RX_BUFFER_SIZE-17)){
- pData=msg+15;
- }else{//非法
- return;
- }
- #ifdef SLW_DEBUG
- buf[0]=0;
- sprintf(buf,"[%d]",DataLen);
- for(i=0;i<DataLen;i++){
- sprintf(tmp,"%02x,",pData[i]);
- strcat(buf,tmp);
- }
- strcat(buf,"\r\n");
- SlwTrace(DEBUG,buf);
- #endif
- //处理
- cmd=GpsServerProcess((unsigned char *)pData,DataLen);
- }
- /*******************************************************************
- *MainTask
- *主任务
- ********************************************************************/
- __task void NetTask(void)
- {
- unsigned char SecFlag=0;
- static STATUE PPPStatus=CLOSE;
- static STATUE IPStatus=CLOSE;
- static unsigned char sucNetStatusCt=0;
- static unsigned short susGpsTimingSendCt=0;
- SUT_MODEM_RECV_MSG *pMsg;
-
- STATUE status;
- int csq;
- //char msg[200];
- int len;
- INT8U err;
- char buf[20];
- //Obtain own system task identification number
- idNetTask = os_tsk_self();
- SlwTrace(INF,"Net Task start!\r\n");
- memset((unsigned char *)&sutPocStatus,0,sizeof(SUT_POC_STATUS));
- SetLedIndicator(IndNoNet);
- SlwTrace(INF,"ModemInit...");
- if(ModemInit()){
- SlwTrace(INF," OK!\r\n");
- }else{
- SlwTrace(INF," Fail\r\n");
- SlwTrace(INF,"The System Stop!\r\n");
- SetLedIndicator(IndModemErr);
- while(1);
- }
- ModemSendAT("ATE0\r\n");//关闭回显
- //Get ModemVer
- g_iModemVer=ModemGetVer();
- sprintf(buf,"ModemVer=%d\r\n",g_iModemVer);
- SlwTrace(INF,buf);
- //语音提示 "欢迎使用公网集群对讲机"
- SpeakerEnable();
- ModemSendAT("AT+ZTTS=1,\"226BCE8F7F4F28756C51517FC696A47FF95BB28B3A67\"\r");
- //Gps Init
- GpsDataInit();
- #ifdef GPS_ENABLE
- GPSInit();
- #endif
-
- PPPStatus=INVALID;
- IPStatus=INVALID;
- GpsServerStatus=INVALID;
- while(1)
- {
- //Modem Uart消息处理 每次大约等待10ms
- if(OS_R_TMO!=os_mbx_wait(MsgBox,(void **)&pMsg,1)){
- SlwTrace(INF,(char *)pMsg->MsgData);
- if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:") || 0==ModemStrCmp((char *)pMsg->MsgData,"+ZTTS:0")){
- PocHandle((char *)pMsg->MsgData);
- }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZIPRECV:")){
- RecvModemData((char *)pMsg->MsgData);
- }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+CSQ:")){
- g_iCSQ=GetCSQ((char *)pMsg->MsgData);
- if(g_iCSQ==99 ){
- sutPocStatus.Logined=0;//POC 离线
- SetLedIndicator(IndNoNet);
- }
- }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZPPPSTATUS:")){
- PPPStatus=GetPPPStatus((char *)pMsg->MsgData);
- }else if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZIPSTATUS:")){
- IPStatus=GetIPStatus((char *)pMsg->MsgData);
- }
- _free_box(mpool,pMsg);
- }
- #ifdef GPS_ENABLE
- //GPS Uart消息处理
- if(g_usUart2RecvLen){
- process_nema((char *)RxBuffer2);
- g_usUart2RecvLen=0;
- }
- #endif
- //=======控制大概每1秒往下执行=========
- if(++SecFlag>=100)SecFlag=0;
- else continue;
-
- if(0==sutPocStatus.Logined)SetLedIndicator(IndNoNet);
- //---控制定时发送GPS数据 ---
- if(IPStatus==OPEN && sutGpsInfo.isServerLogin){
- if(++susGpsTimingSendCt>sutGpsInfo.SendTime){
- susGpsTimingSendCt=0;
- GpsTimingSendPos();
- }
- }else{
- susGpsTimingSendCt=0;
- }
- //--------------------
- if(++sucNetStatusCt>4)sucNetStatusCt=0;
- switch(sucNetStatusCt)
- {
- case 0:
- ModemSendAT("AT+CSQ?\r");
- break;
- #ifdef GPS_ENABLE
- case 1:
- ModemSendAT("AT+ZPPPSTATUS\r");
- break;
- case 2:
- if(PPPStatus==OPEN){
- //SlwTrace(DEBUG,"AT+ZIPSTATUS=0\r\n");
- ModemSendAT("AT+ZIPSTATUS=0\r");
- }else{
- //SlwTrace(DEBUG,"AT+ZPPPOPEN\r\n");
- ModemSendAT("AT+ZPPPOPEN\r");
- }
- break;
- case 3:
- if(IPStatus!=OPEN && PPPStatus==OPEN){
- sutGpsInfo.isServerLogin=0;
- sprintf(buf,"AT+ZIPSETUP=0,%s,%d\r\n",sutProductPara.GIP,sutProductPara.GpsPort);
- //SlwTrace(DEBUG,buf);
- ModemSendAT(buf);
- }
- break;
- case 4:
- if(IPStatus==OPEN && 0==sutGpsInfo.isServerLogin){
- if(sutGpsInfo.isGpsWork)GpsServerConect();
- }
- break;
- #endif
- }
- }
- }
|