123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- #include "includes.h"
- #define TUP_SOCKET_NUN 2 //had better not lagger that DOMAIN_NUM_MAX
- typedef enum{
- TUPS_IDLE,
- TUPS_START,
- TUPS_DOMAIN,
- TUPS_LINK,
- TUPS_WAIT,
- TUPS_LINE
- }TUPS_ENUM;
- typedef struct{
- IPD_ENUM ipdType;
- TUP_ENUM tup;
- T_INT8 ipDomain[IP_DOMAIN_SIZE+1];
- T_UINT16 port;
- }TCPUDP_DEF;
- typedef struct{
- TUPS_ENUM status;
- T_BOOL needClose;
- TCPUDP_DEF server;
- T_UINT8 index;
- T_UINT32 time;
- T_BOOL printFlag;
- TUPSEND_DEF sendPara;
- }TUP_DEF;
- TUP_DEF tupPara[TUP_SOCKET_NUN];
- void tupRecv_cb(void *param){
- T_UINT8 buffer[TUP_DATA_MAX+EXT_DATA];
- T_INT8 buf[32];
- LSAPI_OSI_Event_t *pEvent = (LSAPI_OSI_Event_t *)param;
- TUPSEND_DEF *para=(TUPSEND_DEF *)pEvent->param3;
- int len_ret,tlen,i;
- bool needClose=false;
- showTupEventInfo("tcpUdp",pEvent->id,para,0);
- switch(pEvent->id){
- case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:
- wlog_info("tupRecv_cb server ok:%d",para->fd);
- break;
- case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP:
- wlog_info("tupRecv_cb send:%d",para->fd);
- break;
- case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP:
- wlog_info("tupRecv_cb client shut:%d",para->fd);
- needClose=true;
- break;
- case LSAPI_SOCK_TCPIP_REV_DATA_IND:
- while(1){
- if(TUP_TCP==para->tupType)
- len_ret=LSAPI_SOCK_Recv(para->fd,buffer, TUP_DATA_MAX,0);
- else
- len_ret=LSAPI_SOCK_Recvfrom(para->fd,buffer, TUP_DATA_MAX,0,¶->sockAddr);
- if(len_ret>=0){
- if(len_ret==0) break;
- snprintf(buf,sizeof(buf), "+TCPRECV:%d,%d,",para->index+1,len_ret);
- tlen=strlen(buf);
- for(i=len_ret-1;i>=0;i--) buffer[i+tlen]=buffer[i];
- memcpy(buffer, buf,tlen);
- tlen += len_ret;
- buffer[tlen++]='\r';
- buffer[tlen++]='\n';
- msgSToOutter(buffer, tlen);
- wlog_info("tupRecv_cb recv[%d]%d",para->fd,len_ret);
- }else{
- wlog_warn("tupRecv_cb failed");
- break;
- }
- }
- break;
- case LSAPI_SOCK_TCPIP_CLOSE_IND:
- wlog_info("tupRecv_cb server shut:%d",para->fd);
- needClose=true;
- break;
- case LSAPI_SOCK_TCPIP_ERR_IND:
- wlog_info("tupRecv_cb server error:%d",para->fd);
- needClose=true;
- break;
- default: break;
- }
-
- LSAPI_OSI_Free(pEvent);
- if(needClose==true) threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
- }
- T_UINT8 tupOpenApi(T_UINT8 socket,TCPUDP_DEF *para){
- T_UINT8 thisSocket;
- if(socket>TUP_SOCKET_NUN) return 1;//get error
- thisSocket=socket-1;
- if(TUPS_LINE==tupPara[thisSocket].status) return 2;//open already
- else if(TUPS_IDLE!=tupPara[thisSocket].status) return 3;//socket is opening
- tupPara[thisSocket].server.ipdType=para->ipdType;
- tupPara[thisSocket].server.tup=para->tup;
- tupPara[thisSocket].server.port=para->port;
- strcpy(tupPara[thisSocket].server.ipDomain, para->ipDomain);
- tupPara[thisSocket].time=getSysTick()+200;//if domain busy in 2 seconds, we set it to failed
- tupPara[thisSocket].needClose=FALSE;
- tupPara[thisSocket].sendPara.index=thisSocket;
- tupParaSet(&tupPara[thisSocket].sendPara, para->port, tupRecv_cb,TUP_API_THREAD_STACK);
- if(para->ipdType==IPD_IP) tupPara[thisSocket].status=TUPS_LINK;
- else tupPara[thisSocket].status=TUPS_START;
- return 0;//try open ok
- }
- T_UINT8 tupCloseApi(T_UINT8 socket){
- T_UINT8 thisSocket;
- if(socket>TUP_SOCKET_NUN) return 1;//get error
- thisSocket=socket-1;
- tupPara[thisSocket].needClose=TRUE;
- return 0;
- }
- void tupTest(void){
- tupOpen("1,122.224.240.106:11602,0\r\n",25);
- tupOpen("2,122.224.240.106:11602,0\r\n",25);
- }
- void isTupTaskIdle(void){
- T_UINT8 i;
- for(i=0;i<TUP_SOCKET_NUN;i++){
- if(tupPara[i].status!=TUPS_IDLE && tupPara[i].status!=TUPS_LINE){
- ticketDeVote(TICKET_PT_TUP);
- return;
- }
- }
- ticketVote(TICKET_PT_TUP);
- }
- //tup socket handler start...////////////////////////////////
- PT_THREAD (ptTupTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- static T_BOOL flag=TRUE;
- static T_UINT8 index=0;
- T_INT8 info[30];
- T_UINT8 ret;
- if(TRUE==flag){
- memset((T_UINT8 *)&tupPara, 0, sizeof(TUP_DEF));
- flag=FALSE;
- //tupTest();
- }
- PT_BEGIN(pt);
- while(1){
- if(TUPS_START==tupPara[index].status){
- if(getSysTick()<tupPara[index].time){
- if(tupPara[index].printFlag==FALSE){//just printf once
- tupPara[index].printFlag=TRUE;
- wlog_info("TUPS_START[%d]:%s,%d", index+1, tupPara[index].server.ipDomain,tupPara[index].server.port);
- }
-
- if(TRUE==setDomainForIp(tupPara[index].server.ipDomain, &tupPara[index].index)){
- //done, we wait result
- tupPara[index].time=getSysTick()+500;//we wait 5 seconds for domain result
- tupPara[index].status=TUPS_DOMAIN;
- wlog_info("TUPS_DOMAIN[%d]",index+1);
- }
- }else{//setDomainForIp Timeout , we make it failed and quit
- wlog_warn("TUPS_START[%d] timeout", index);
- snprintf(info, sizeof(info),"+TCPOPEN:%d,0\r\n",index+1);
- msgToOutter(info);
- tupPara[index].status=TUPS_IDLE;
- }
- }else if(TUPS_DOMAIN==tupPara[index].status){
- ret=getDomainForIp(tupPara[index].index, tupPara[index].server.ipDomain, IP_DOMAIN_SIZE);
- if(DOMAIN_ERR==ret){
- wlog_warn("TUPS_DOMAIN[%d] failed", index+1);
- snprintf(info, sizeof(info),"+TCPOPEN:%d,0\r\n",index+1);
- msgToOutter(info);
- tupPara[index].status=TUPS_IDLE;
- }else if(DOMAIN_OK==ret){
- //done
- wlog_info("TUPS_DOMAIN[%d]:%s", index+1, tupPara[index].server.ipDomain);
- tupPara[index].status=TUPS_LINK;
- }else{
- if(getSysTick()>tupPara[index].time){//timeout
- wlog_warn("TUPS_DOMAIN[%d] timeout", index+1);
- snprintf(info, sizeof(info),"+TCPOPEN:%d,0\r\n",index+1);
- msgToOutter(info);
- tupPara[index].status=TUPS_IDLE;
- }
- }
- }else if(TUPS_LINK==tupPara[index].status){
- //trying to open tcp/udp here
- snprintf(info, sizeof(info),"+TCPOPEN:%d,",index+1);
- switch(tryConnectTup(tupPara[index].server.ipDomain, tupPara[index].server.tup,&tupPara[index].sendPara)){
- case TUP_STATUS_FALSE:
- wlog_warn("tupOpen failed:%d", index+1);
- strcat(info, "0");
- tupPara[index].status=TUPS_IDLE;
- strcat(info, "\r\n");
- msgToOutter(info);
- break;
- case TUP_STATUS_TRUE:
- wlog_warn("tupOpen ok:%d", index+1);
- strcat(info, "1");
- tupPara[index].status=TUPS_LINE;
- strcat(info, "\r\n");
- msgToOutter(info);
- break;
- case TUP_STATUS_WAIT:
- wlog_warn("tupOpen and wait result:%d", index+1);
- tupPara[index].status=TUPS_WAIT;
- break;
- }
- }else if(TUPS_WAIT==tupPara[index].status){
- if(tupPara[index].sendPara.errno!=0 ||tupPara[index].sendPara.tupStatus==CNNT_ERROR){
- snprintf(info, sizeof(info),"+TCPOPEN:%d,0\r\n",index+1);
- msgToOutter(info);
- tupPara[index].needClose=TRUE;
- }else if(tupPara[index].sendPara.tupStatus==CNNT_OK){
- snprintf(info, sizeof(info),"+TCPOPEN:%d,1\r\n",index+1);
- msgToOutter(info);
- tupPara[index].status=TUPS_LINE;
- }
- }else if(TUPS_LINE==tupPara[index].status){
- if(tupPara[index].sendPara.tupStatus==CNNT_CLOSED) tupPara[index].needClose=TRUE;
- }
- if(tupPara[index].needClose==TRUE){
- snprintf(info, sizeof(info),"+TCPCLOSE:%d\r\n",index+1);
- if(TUPS_IDLE == tupPara[index].status){
- tupPara[index].needClose=FALSE;
- msgToOutter(info);
- }else if(TUPS_DOMAIN != tupPara[index].status){
- //exec tup close cmd now
- if(tupPara[index].sendPara.fd!=0){
- //wlog_info("***skip close fd for test");
- userCloseSocket(&tupPara[index].sendPara.fd);
- }
- wlog_warn("TUPS[%d] user close done",index+1);
- if(tupPara[index].status==TUPS_LINE) msgToOutter(info);//打开过,也就是输出过+TCPOPEN:x,1才输出+TCPCLOSE:x
- tupPara[index].status=TUPS_IDLE;
- tupPara[index].needClose=FALSE;
- }
- }
- if(++index>=TUP_SOCKET_NUN) index=0;
- isTupTaskIdle();
- PTTimerStart(ptPool, &ptTimer,1);//can be be fast
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- T_BOOL findPortIndexAndLastDouHaoIndex(T_UINT8 *src, T_UINT16 srcLen,T_UINT16 *portIndex, T_UINT16 *lastIndex){
- //1,www.baidu.com:12345,0
- //1,120.77.66.129:12345,1
- T_UINT8 i;
- T_BOOL findPortAlread=FALSE;
- *lastIndex=0;
- for(i=0;i<srcLen;i++){
- if(TRUE==findPortAlread){
- if(src[i]==','){
- if(i+1<srcLen){
- *lastIndex=i+1;
- break;
- }
- }
- }
- if(src[i] == ':'){
- *portIndex=i+1;
- findPortAlread=TRUE;
- }
- }
- return findPortAlread;
- }
- T_BOOL tupParaGet(T_UINT8 *src, T_UINT16 srcLen,T_UINT8 *socket,TCPUDP_DEF *tupCtrl){
- //analysis parameter from src,src[0]='='
- //AT+TCPOPEN=1,www.baidu.com:12345
- //AT+TCPOPEN=1,120.77.66.129:12345
- T_INT8 *p=src;
- T_UINT8 i;
- T_UINT16 portIndex,douhaoIndex;
- if(srcLen < 11)
- return FALSE;
- if(p[1] != ',')
- return FALSE;
- if(FALSE==findPortIndexAndLastDouHaoIndex(src,srcLen,&portIndex,&douhaoIndex))
- return FALSE;
-
- //socket
- if(p[0] <= '0' || p[0] > (TUP_SOCKET_NUN+0x30))
- return FALSE;
- *socket = atoi(p);
-
- //IP/DOMAIN
- if(FALSE == checkIpDomain(src+2,portIndex-2,tupCtrl->ipDomain,&tupCtrl->ipdType))
- return FALSE;
- //port
- if(src[portIndex] < '0' || src[portIndex] > '9')
- return FALSE;
- tupCtrl->port=atoi(src+portIndex);
- //tup
- if(douhaoIndex==0) tupCtrl->tup=TUP_TCP;
- else{
- if(src[douhaoIndex]=='0') tupCtrl->tup=TUP_TCP;
- else if(src[douhaoIndex]=='1') tupCtrl->tup=TUP_UDP;
- else return FALSE;
- }
-
- return TRUE;
- }
- void tupOpen(T_UINT8 *data, T_UINT16 len){
- TCPUDP_DEF tempTup;
- T_UINT8 socket,ret;
- T_INT8 info[30];
- wlog_info("user need tupopen");
- if(talking.netWork.netReady==0){
- wlog_warn("no netWork yet");
- msgToOutter("+TCPOPEN:NO NET\r\n");
- return;
- }
- if(FALSE==tupParaGet(data,len,&socket,&tempTup)){
- wlog_warn("tupopen para err");
- msgToOutter("+TCPOPEN:ERROR\r\n");
- }else{
- ret=tupOpenApi(socket, &tempTup);
- if(ret==1){
- wlog_error("tupopen socket error");
- msgToOutter("+TCPOPEN:ERROR\r\n");
- }else if(ret==2){
- wlog_warn("tupopen socket:%d open already", socket);
- snprintf(info, sizeof(info),"+TCPOPEN:%d,ALREADY\r\n", socket);
- msgToOutter(info);
- }else if(ret==3){
- wlog_warn("tupopen is opening");
- snprintf(info, sizeof(info),"+TCPOPEN:%d,SYNC\r\n", socket);
- msgToOutter(info);
- }else wlog_info("tupopen try open start ok");
- }
- }
- void tupStatus(T_UINT8 *data, T_UINT16 len){
- //AT+TCPSTATUS=socket
- T_UINT8 socket,status=0;
- T_INT8 buf[30];
- if(len != 3) goto CMD_ERROR;
- socket = atoi(data);
- if(socket < 1 || socket > TUP_SOCKET_NUN) goto CMD_ERROR;
- wlog_info("tupStatus %d",socket);
- socket --;
- if(tupPara[socket].status == TUPS_LINE) status=1;
- snprintf(buf,sizeof(buf),"+TCPSTATUS:%d,%d\r\n",socket+1,status);
- msgToOutter(buf);
- return;
- CMD_ERROR:
- msgToOutter("+TCPSTATUS:ERROR\r\n");
- }
- void tupClose(T_UINT8 *data, T_UINT16 len){
- T_UINT8 socket;
- if(len != 3) goto CMD_ERROR;
- socket = atoi(data);
- if(socket < 1 || socket > TUP_SOCKET_NUN) goto CMD_ERROR;
- wlog_info("tupClose %d",socket);
- tupCloseApi(socket);
- return;
- CMD_ERROR:
- msgToOutter("+TCPCLOSE:ERROR\r\n");
- }
- void tcpUdpShutDown(void){
- T_UINT8 i;
- for(i=1;i<=TUP_SOCKET_NUN;i++){
- if(TUPS_IDLE != tupPara[i-1].status)
- tupCloseApi(i);
- }
- }
- void tupSend(T_UINT8 *data, T_UINT16 len){
- //AT+TCPSEND=socket,len,data
- T_INT32 i;
- T_UINT8 temp;
- T_UINT32 dataIndex;
- T_UINT8 socket;
- T_INT8 buf[30];
- wlog_info("tupSend req");
- if(len < 5) goto CMD_ERROR;
- socket = atoi(data);
- if(socket < 1 || socket > TUP_SOCKET_NUN) goto CMD_ERROR;
- socket --;
- if(tupPara[socket].status != TUPS_LINE){
- socket ++;
- goto CMD_ERROR;
- }
- temp=0;
- dataIndex=-1;
- for(i=0;i<len;i++)
- if(data[i]==','){
- temp++;
- if(temp==2) dataIndex=i+1;
- }
- if(dataIndex==-1){
- socket ++;
- goto CMD_ERROR;
- }
- i=atoi(data+2);
- if(i==0) return;
- trySendTup(data+dataIndex, i, tupPara[socket].server.tup, &tupPara[socket].sendPara);
- snprintf(buf, sizeof(buf), "+TCPSEND:%d,%d\r\n",socket+1,i);
- msgToOutter(buf);
- wlog_info("tupSendData:%d",i);
- return;
- CMD_ERROR:
- snprintf(buf, sizeof(buf), "+TCPSEND:%d,ERROR\r\n",socket);
- msgToOutter(buf);
- }
|