123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #include "includes.h"
- #define LOCATIONTIME 30
- TUPSEND_DEF tupCtl;
- NET_DEF netWork;
- APP_STATUS app_status;
- #define LOC_PWD "000000"
- //user BH4TDV-9 pass 16808 vers APRS-51G2-20171220\r\n
- void LoginPackSend(TUPSEND_DEF *para)
- {
- char data[100]={0};
- snprintf(data,sizeof(data), "user %s pass %s vers APRS-51G2-20171220\r\n",sutApp.UserInfo.Firm_CallID,LOC_PWD);
- //snprintf(data,sizeof(data), "user %s pass %d vers APRS-51G2-20171220\r\n","MYCALL",12726);
- trySendTup(data,strlen(data), TUP_TCP,para);
- MSG_INFO(1, "LoginPackSend-->%s",data);
- }
- //BH4TDV-9>51G2:!3134.36N/12019.93EY304/002/A=00007551G2 13.8V S08\r\n
- void ApplocationSend(TUPSEND_DEF *para){
- char data[100]={0};
- char tempbuf[20];
- char len=0;
- float Vbat=0;
-
- memcpy(data,sutApp.UserInfo.Firm_CallID,strlen(sutApp.UserInfo.Firm_CallID));
- strcat(data,">");
- strcat(data,OHPOC_APP_NAME);
- strcat(data,":");
- strcat(data,"!");
- len=strlen(data);
- snprintf(data+len, sizeof(data)-len,"%.2f%c",sutGpsInfo.App_latitue,sutGpsInfo.NSFlag);
- //strcat(data,"2237.22N");//wd
- strcat(data,"/");
- len=strlen(data);
- snprintf(data+len, sizeof(data)-len,"%.2f%c",sutGpsInfo.App_longitue,sutGpsInfo.EWFlag);
- //strcat(data,"11402.12E");//jd
- strcat(data,"Y304/002/A=00007551G2");//jd
- Vbat=sutApp.g_iBAT;
- Vbat=Vbat/100;
- snprintf(tempbuf,sizeof(tempbuf)," %0.3fV S08\r\n",Vbat);
- strcat(data,tempbuf);
- trySendTup(data,strlen(data),TUP_TCP,para);
- MSG_INFO(1, "ApplocationSend-->%s",data);
- }
- void Applocation_Tick(TUPSEND_DEF *para){
- static char LocationCnt=0;
- switch(app_status.login){
- case 0://未登录
- //1min try
- if(++app_status.app_netcnt>20){
- LoginPackSend(para);
- app_status.app_netcnt=0;
- }
- LocationCnt=0;
- break;
- case 1://已登录
- if(LocationCnt==0)ApplocationSend(para);
- if(++LocationCnt>LOCATIONTIME){
- LocationCnt=0;
- }
- if(++app_status.hearttick>40){
- tupCtl.tupStatus=CNNT_READY;
- userCloseSocket(¶->fd);
- }
- break;
-
- }
- }
- #define APP_TCP_THREAD_STACK 4*1024 //
- void Tup_Process(int interva){
- static int cnt=0;
- static unsigned char reconcnt=0;
- char AppIp[40]={0};
- unsigned short AppPort=0;
-
- if(netWork.netReady!=1)return;
- if(!newPara.Xinbiao_IpEnable)return;
- if(++cnt<(1*1000/interva)) return;
- cnt=0;
-
- if(CNNT_READY==tupCtl.tupStatus){
- if(sutApp.UserInfo.IP[0]==0)return;
- tupParaSet(&tupCtl,sutApp.UserInfo.port,ApplocationCallBack,APP_TCP_THREAD_STACK);
- app_status.app_netcnt=40;
- switch(tryConnectTup(sutApp.UserInfo.IP,TUP_TCP,&tupCtl)){
- case TUP_STATUS_FALSE:
- wlog_info("App cnnt failed");
- break;
- case TUP_STATUS_WAIT:
- wlog_info("App wait result");
- tupCtl.tupStatus=CNNT_BUSY;
- break;
- }
- }else if(CNNT_OK==tupCtl.tupStatus){
- Applocation_Tick(&tupCtl);
- }else if(CNNT_ERROR==tupCtl.tupStatus){
- wlog_info("APP cnnt err");
- if(++reconcnt<40)return;
- reconcnt=0;
- tupCtl.tupStatus=CNNT_READY;
-
- }else if(CNNT_CLOSED==tupCtl.tupStatus){
- if(++reconcnt<40)return;
- reconcnt=0;
- tupCtl.tupStatus=CNNT_READY;
- }
- }
- void MS5700_Networking(unsigned int interval){
- uint32_t nRet;
- static int nAttstate,nActstate;
- static int LastnAttstate=0,LastnActstate=0;
- bool netIf;
- static bool lastNetIf=FALSE;
- //static unsigned short cnt=0;
- static unsigned short regCnt=0;
- static unsigned short tickcnt=0;
- if(++tickcnt<(1000/interval)) return;//1���һ��ADC����
- tickcnt=0;
-
- nRet = LSAPI_NET_GetGprsAttState(&nAttstate);
- if(nRet!=0){
- MSG_WARN(1,"Get REG status failed");
- return;
- }
- if(nAttstate!=1){
- if(LastnAttstate!=nAttstate){ MSG_WARN(1,"reg off");}
- netWork.reg=0;
- netWork.pdp=0;
- netWork.netReady=0;
- MSG_INFO(1,"wait reg");
- if(regCnt==60){
- MSG_WARN(1,"!!!CFUN=0");
- LSAPI_NET_CFUN(0);
- }else if(regCnt>=61) {
- regCnt=0;
- MSG_WARN(1,"!!!CFUN=1");
- LSAPI_NET_CFUN(1);
- }
- ++regCnt;
- }else{
- regCnt=0;
- if(LastnAttstate!=nAttstate){ MSG_WARN(1,"reg on");}
- netWork.reg=1;
- nRet = LSAPI_NET_GetGprsActState(&nActstate);
- if(nRet!=0){
- MSG_WARN(1,"LSAPI_NET_GetGprsActState get failed:%d",nRet);
- }else{
- if(nActstate!=1){
- if(LastnActstate != nActstate){ MSG_WARN(1,"pdp off");}
- netWork.pdp=0;
- netWork.netReady=0;
- // ls_PdpActive(); HYL
-
- }else{
- if(LastnActstate != nActstate){ MSG_WARN(1,"pdp on");}
- netWork.pdp=1;
- netIf=LSAPI_NET_GET_GprsNetIf();
- if(false==netIf){
- if(lastNetIf!=netIf){ MSG_WARN(1,"netif off");}
- netWork.netReady=0;
- MSG_INFO(1,"netif start create");
- LSAPI_NET_NetIf_Create();
- MSG_INFO(1,"netif create done");
- }else{
- if(lastNetIf!=netIf){ MSG_WARN(1,"netif on");}
- netWork.netReady=1;
- }
- lastNetIf=netIf;
- }
- LastnActstate=nActstate;
- }
- }
- LastnAttstate=nAttstate;
- }
- void ApplocationRecDataHandler(T_UINT8 *info, T_UINT32 length){
- static T_UINT8 msg[100];
- static T_UINT32 len=0;
- T_UINT32 tlen,i;
- // # logresp bh4tdv-9 verified, server T2XWT\r\n 登录成功
- // # aprsc 2.1.2-gc90ee9c 29 Mar 2017 15:42:15 GMT T2XWT 192.168.88.253:14580\r\n 心跳
- if(msgCmp(info,"# logresp")){
- app_status.login=1;
- MSG_INFO(1,"login OK");
- }
- if(msgCmp(info,"# aprsc")){
- app_status.hearttick=0;
- MSG_INFO(1,"aprs tick");
- }
-
- }
- void ApplocationCallBack(void *param){
- T_UINT8 buffer[512];//512
- LSAPI_OSI_Event_t *pEvent = (LSAPI_OSI_Event_t *)param;
- TUPSEND_DEF *para=(TUPSEND_DEF *)pEvent->param3;
- T_BOOL needExitThread=FALSE;
- T_INT32 len_ret;
-
- showTupEventInfo("ApplocationCallBack",pEvent->id,para,0);
-
- switch(pEvent->id){
- case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:
-
- break;
- case LSAPI_SOCK_TCPIP_REV_DATA_IND:
- while(1){
- len_ret=LSAPI_SOCK_Recv(para->fd,buffer, sizeof(buffer),0);
- if(len_ret>0){
- wlog_info("ApplocationCallBack Rec:%d,%d",para->fd,len_ret);
- ApplocationRecDataHandler(buffer, len_ret);
- }else if(len_ret==0) break;
- else{
- wlog_info("ApplocationCallBack failed");
- break;
- }
- }
- break;
- case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP:
- case LSAPI_SOCK_TCPIP_CLOSE_IND:
- case LSAPI_SOCK_TCPIP_ERR_IND:
- needExitThread=true;
- break;
- default: break;
- }
- if(needExitThread)threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
- }
|