123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "includes.h"
- #if(SOCKET_NUM_MAX>5)
- #error "SOCKET_NUM_MAX 不能大于5个"
- #endif
- static void tcpSocketRun(void);
- SOCKET_DEF gSocket;
- //150ms执行一次LED任务
- PT_THREAD (ptSocketTask(struct pt *pt)){
- static timer_t ptTimer;
- static char firstRun=1;
- if(firstRun!=0){
- tcpSocketInit();
- if(tcpSocketAdd(buBiaoTickHandler, buBiaoRecvHandler, buBiaoSocketShutDown,showGIP,newPara.gpsPort)<0) SlwTrace(INF, "Create bibiao socket fail",1);//最多建议SOCKET_NUM_MAX个socket连接
- //if(tcpSocketAdd(FotaTickHandler, FotaIap_Handle, buBiaoSocketShutDown,FOTA_IAP_IP,FOTA_IAP_PORT)<0) SlwTrace(INF, "Create FOTA socket fail",1);
- firstRun=0;
- }
- PT_BEGIN(pt);
- while(1){
- if(g_ucEnterGTNow==0) tcpSocketRun();//GT模式下不操作socket
- PTTimerStart(&pt_timerPool, &ptTimer, 50);
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- void tcpSocketInit(void){
- memset((unsigned char *)&gSocket,0,sizeof(SOCKET_DEF));
- }
- short tcpSocketAdd(void (*UserHandle)(unsigned char socketID),void (*RxCallBack)(unsigned char *data , unsigned int),void (*SockeShutDown)(unsigned char socketID),char *ipAddr, unsigned short port)
- {
- unsigned char i;
- SOCKET_INFO *socket;
- for(i=0;i<SOCKET_NUM_MAX;i++){
- socket=&gSocket.socketList[i];
- if(socket->status==0){
- strncpy(socket->Ip, ipAddr, sizeof(socket->Ip));
- socket->port=port;
- socket->UserHandle=UserHandle;
- socket->RxCallBack=RxCallBack;
- socket->SockeShutDown=SockeShutDown;
- socket->machine=i;
- socket->status=1;
- return i;
- }
- }
- return -1;
- }
- void tcpSocketDel(short socketID){
- unsigned char i;
- SOCKET_INFO *socket;
- char info[30];
-
- if(socketID>=SOCKET_NUM_MAX) return;
- socket=&gSocket.socketList[socketID];
- if(socket->status==0) return;
- if(socket->tcpStatus!=0){
- snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
- ModemSendAt(info);
- socket->tcpStatus=0;
- }
- socket->status=0;
- }
- static void tcpCloseSocket(void){
- unsigned char i;
- SOCKET_INFO *socket;
- char info[30];
-
- for(i=0;i<SOCKET_NUM_MAX;i++){
- socket=&gSocket.socketList[i];
- if(socket->status!=0){
- if(socket->tcpStatus!=0){
- snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",i+1);
- ModemSendAt(info);
- socket->tcpStatus=0;
- }
- }
- }
- }
- //网络连接检测及控制
- static char tcpLinkProcess(void){
- char ret=0;
- static char tick=0;
- static STATUE lastPPP=CLOSE;
-
- //AT+CCLK?
- if(hgs_poc_type==POC_OWN)
- {
-
- switch(QueryTick){
- case 0:
- // printf("QueryTick===%d",QueryTime);
- ModemSendAt("AT+POC_PPP?\r\n");
- ModemSendAt("AT+CSQ\r\n");
- if(g_uiGpsStat)ModemSendAt("AT+GPSRD=\"ALL\"\r\n");
- ModemSendAt("AT+CTEC?\r\n");
- ModemSendAt("AT+UDP?\r\n");
- ModemSendAt("AT+GGIP?\r\n");
- ModemSendAt("AT+GPIP?\r\n");
- if(sutPocStatus.CardStatus)ModemSendAt("AT+COPS?\r\n");
- break;
- }
-
- if(++QueryTick>=QueryTime) QueryTick=0;
- if(netPPP==CLOSE){
- if(lastPPP != netPPP) tcpCloseSocket();
- lastPPP=netPPP;
- return 1;
- }
- lastPPP=netPPP;
- return ret;
- }else if(hgs_poc_type==POC_BND) {
-
- switch(tick){
- case 0:break;
- case 2:ModemSendAt("AT+CSQ\r\n");break;//csqSendFlag=0;
- case 4:
- if(g_uiGpsStat)ModemSendAt("AT+GPSRD=\"ALL\"\r\n");
- break;
- case 6:ModemSendAt("AT+CTEC?\r\n");break;//ModemSendAt("AT^SYSINFO\r\n");break;
- case 8:break;
- case 10:break;
- case 12:break;
- case 14:ModemSendAt("AT+COPS?\r\n");
- break;
- }
- if(++tick>=15) tick=0;
- return 0;
- }
-
- }
- static char LogicHandler(unsigned char index){
- char ret=1;
- char needSendAt=1;
- char info[50];
-
- SOCKET_INFO *socket=&gSocket.socketList[index];
-
- if(*(socket->Ip)==0){
- return 0;
- }
- switch(QueryTick){
- case 0:snprintf(info, sizeof(info),"AT+TCPSTATUS=%d\r\n",index+1);
- break;
- case 5:
- if(socket->tcpStatus==0) snprintf(info, sizeof(info),"AT+TCPOPEN=%d,%s:%d\r\n", index+1,socket->Ip,socket->port);
- else needSendAt=0;
- break;
- default:ret=0;needSendAt=0;
- break;
- }
-
- // if(++socket->machine > QueryTime) socket->machine=0;//5
- if(needSendAt) ModemSendAt(info);
- return ret;
- }
- char tcpSocketSendData(unsigned char socketID,unsigned char *data, unsigned int length){
- static char runStep=0;
- unsigned char i,ret=0;
- SOCKET_INFO *socket;
- char buf[20];
- if(socketID >= SOCKET_NUM_MAX) return 1;
-
- socket=&gSocket.socketList[socketID];
- if(socket->status==0) return 2;
- if(socket->tcpStatus==0) return 3;
- snprintf(buf, sizeof(buf), "AT+TCPSEND=%d,%d,",socketID+1, length);
- ModemSendAt(buf);
- ModemSendData(data, length);
- ModemSendAt("\r\n");
- return 0;
- }
- static void tcpSocketRun(void){
- static char runStep=0;
- unsigned char i,j,w,ret=0;
- SOCKET_INFO *socket;
-
- if(tcpLinkProcess()) return;
- if(!g_uiGpsStat||hgs_poc_type!=POC_OWN) return; //不去增加服务器负担
-
- for(j=0;j<2;j++){
- if(j==0) w=0;
- else w=gSocket.index;
-
- for(i=w;i<SOCKET_NUM_MAX;i++){
- socket=&gSocket.socketList[i];
- if(socket->status!=0){//判断 socket是否定义
- if(j==0) socket->UserHandle(i);
- else if(LogicHandler(i))break;
- }
- }
- }
- if(i>=SOCKET_NUM_MAX) gSocket.index=0;
- }
- void tcpSocketRecv(unsigned char id, unsigned char *data, unsigned char length){
-
- unsigned char i,thisID;
- SOCKET_INFO *socket;
-
- thisID=id-1;
- if(thisID>= SOCKET_NUM_MAX) return;
- socket=&gSocket.socketList[thisID];
- if(socket->status==0) return;
- socket->RxCallBack(data,length);
- }
- /*
- 更新状态
- type:0 status为1表示打开
- type:1 status为1表示关闭
- */
- void tcpSocketStatusUpdate(char socketID, char status,char type){
- unsigned char i,thisID;
- SOCKET_INFO *socket;
-
- thisID=socketID-0x31;
- if(thisID>= SOCKET_NUM_MAX) return;
- socket=&gSocket.socketList[thisID];
- if(socket->status==0) return;
-
- if(type==0){
- if(status=='1') socket->tcpStatus=1;
- else if(status=='0') socket->tcpStatus=0;
- }else{
- socket->tcpStatus=0;
- if(NULL!=socket->SockeShutDown) socket->SockeShutDown(thisID);
- }
- }
- char tcpSocketGetStatus(char socketID){
- SOCKET_INFO *socket;
- if(socketID>= SOCKET_NUM_MAX) return 1;
- socket=&gSocket.socketList[socketID];
- if(socket->status==0) return 2;
- if(socket->tcpStatus==0) return 3;
- return 0;
- }
- void SocketParaUpdate(unsigned char socketID,char *ipAddr, unsigned short port){
- SOCKET_INFO *socket;
- char info[30];
-
- if(socketID>= SOCKET_NUM_MAX) return;
- socket=&gSocket.socketList[socketID];
- if(socket->status==0) return ;
- if(socket->tcpStatus!=0){
- if(strcmp(socket->Ip, ipAddr) || socket->port != port){
- strncpy(socket->Ip, ipAddr, sizeof(socket->Ip));
- socket->port=port;
- snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
- ModemSendAt(info);
- socket->tcpStatus=0;
- }
- }
- }
|