tupApi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "tupApi.h"
  2. #include "log.h"
  3. #include <string.h>
  4. #include "main.h"
  5. #include "app.h"
  6. void tupParaSet(TUPSEND_DEF *para, T_UINT16 port, LSAPI_SOCK_Callback *callback,T_UINT16 stacksize){
  7. para->port=port;
  8. para->tupRecvCallBack=callback;
  9. para->stacksize=stacksize;
  10. }
  11. void trySendTup(T_UINT8 *data, T_UINT16 len, TUP_ENUM tup, TUPSEND_DEF *para){
  12. T_INT32 ret;
  13. if(para->fd<=0){
  14. wlog_warn("tup failed,invalid fd");
  15. return;
  16. }
  17. if(TUP_TCP==para->tupType) ret=LSAPI_SOCK_Send(para->fd,data,len,0);
  18. else ret=LSAPI_SOCK_Sendto(para->fd,data,len,0, &para->sockAddr, sizeof(LSAPI_SOCK_TcpipSocketAddr_t));
  19. if(ret<0) wlog_info("tup[%d]:%d send failed", para->fd,len);
  20. else wlog_info("tup[%d]:%d send ok",para->fd,len);
  21. }
  22. void userCloseSocket(T_INT32 *fd){
  23. if(*fd<=0) return;
  24. wlog_info("SockClose:%d",*fd);
  25. LSAPI_SOCK_Close(*fd);
  26. *fd=0;
  27. }
  28. void showTupEventInfo(const char *name, T_UINT32 id,TUPSEND_DEF *para){
  29. if(para->ptimer_t!=NULL){//有回调响应后清除定时器
  30. wlog_info("[%s]timerStop",name);
  31. LSAPI_OSI_TimerStop(para->ptimer_t);
  32. para->ptimer_t=NULL;
  33. }
  34. switch(id){
  35. case LSAPI_SOCK_TCPIP_REV_DATA_IND: wlog_info("[%d %s]:recv data ok",id,name);break;
  36. case LSAPI_SOCK_TCPIP_CLOSE_IND: para->tupStatus=CNNT_CLOSED;userCloseSocket(&para->fd);wlog_info("[%d %s]:server shut",id,name);break;
  37. case LSAPI_SOCK_TCPIP_ERR_IND: para->tupStatus=CNNT_ERROR;userCloseSocket(&para->fd);wlog_info("[%d %s]:link err",id,name);break;
  38. case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:para->tupStatus=CNNT_OK;wlog_info("[%d %s]:link ok",id,name);break;
  39. case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP: para->tupStatus=CNNT_CLOSED;userCloseSocket(&para->fd);wlog_info("[%d %s]:client shut",id,name);break;
  40. case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP: wlog_info("[%d %s]:send data ok",id,name);break;
  41. default:wlog_info("%d %s]:ignore",id,name);break;
  42. }
  43. WakeupNow(name);
  44. }
  45. void sockTimerOutCallback(void *param){
  46. TUPSEND_DEF *para=(TUPSEND_DEF *)param;
  47. wlog_info("sockTimerOutCallback enter");
  48. LSAPI_OSI_TimerStop(para->ptimer_t);
  49. userCloseSocket(&para->fd);
  50. para->failNo=3;//连接超时错误3
  51. threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT,NULL);
  52. }
  53. void sockEntry(void *param){
  54. TUPSEND_DEF *para=(TUPSEND_DEF *)param;
  55. T_INT32 sockfd;
  56. T_UINT8 type;
  57. int g_keepalive = 0;
  58. wlog_info("[NewThread:sockEntry]%x",LSAPI_OSI_ThreadCurrent());
  59. if(TUP_TCP==para->tupType) type=LSAPI_SOCK_TCPIP_SOCK_STREAM;
  60. else type=LSAPI_SOCK_TCPIP_SOCK_DGRAM;
  61. sockfd = LSAPI_SOCK_Create(LSAPI_SOCK_TCPIP_AF_INET, type, 0, para->tupRecvCallBack,(uint32_t)para);
  62. if(sockfd<0){
  63. wlog_info("sockfd create err=%d",LSAPI_SOCK_Error());
  64. para->failNo=1;//错误1
  65. wlog_warn("reset pdp!!!");
  66. app.netWork.resetPDP=1;
  67. LSAPI_OSI_ThreadExit();
  68. }
  69. para->fd=sockfd;
  70. wlog_info("tup info:%d,%s:%d", sockfd,para->saddr, para->port);
  71. LSAPI_SOCK_TcpipSocketAddr_t *serveraddr=&para->sockAddr;
  72. serveraddr->sin_family = LSAPI_SOCK_TCPIP_AF_INET;
  73. serveraddr->sin_port = LSAPI_SOCK_htons(para->port);
  74. serveraddr->sin_addr.s_addr = LSAPI_SOCK_inet_addr(para->saddr);
  75. LSAPI_SOCK_SetSockOpt(sockfd, LSAPI_SOCK_SOL_SOCKET, LSPAI_SOCK_KEEPALIVE, (void *)&g_keepalive, sizeof(g_keepalive));
  76. if(LSAPI_RET_OK != (LSAPI_SOCK_Connect(sockfd, serveraddr))){
  77. wlog_error("sockfd connect err");
  78. userCloseSocket(&sockfd);
  79. para->fd=0;
  80. para->failNo=2;//错误2
  81. userExitThread("sockEntry");
  82. return;
  83. }
  84. para->ptimer_t=LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), sockTimerOutCallback, param);
  85. if(NULL != para->ptimer_t) LSAPI_OSI_TimerStart(para->ptimer_t, 10000);//设置为超时10秒
  86. wlog_info("open fd:%d",sockfd);
  87. LSAPI_OSI_Event_t event={};
  88. for(;;){
  89. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  90. if(USER_EVENT_EXIT==event.id){
  91. userCloseSocket(&para->fd);
  92. userExitThread("sockEntry");
  93. }
  94. }
  95. }
  96. TUP_CONNECT_ENUM tryConnectTup(T_INT8 *seraddr, TUP_ENUM tup,TUPSEND_DEF *para){
  97. //必须使用线程来创建连接再waitevent
  98. para->saddr=seraddr;
  99. para->tupType=tup;
  100. para->failNo=0;
  101. para->tupStatus=CNNT_BUSY;
  102. para->fd=0;
  103. if(NULL==LSAPI_OSI_ThreadCreate("sockCreate", sockEntry,(void *)para,LSAPI_OSI_PRIORITY_NORMAL,para->stacksize,4)){
  104. wlog_error("tryConnectTup thread create error for %s",seraddr);
  105. return TUP_STATUS_FALSE;
  106. }
  107. return TUP_STATUS_WAIT;
  108. }
  109. void threadPostEvent(LSAPI_OSI_Thread_t *threadId, T_INT32 id, T_INT32 param1){
  110. LSAPI_OSI_Event_t pEventSend;
  111. pEventSend.id=id;
  112. pEventSend.param1=param1;
  113. LSAPI_OSI_EvnetSend(threadId,&pEventSend);
  114. }
  115. void userExitThread(const char *name){
  116. wlog_info("[DieThread:%x]%s", LSAPI_OSI_ThreadCurrent(),name);
  117. LSAPI_OSI_ThreadExit();
  118. }