socket_client.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "socket_client.h"
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <netinet/in.h>
  22. #include <cmsis_os2.h>
  23. #include <lwip/inet.h>
  24. #include <lwip/netdb.h>
  25. #include <sys/socket.h>
  26. #include <sys/time.h>
  27. #include "ohos_types.h"
  28. #define SOCKET_TASK_STACK (1024 * 8)
  29. #define SOCKET_TASK_PERIOD 27
  30. #define UDP_DEF_PORT 9090
  31. #define TCP_DEF_PORT 8989
  32. #define MSG_DATE_BUF_LEN 24
  33. #define PROTOCOL_HEADER "HM"
  34. #define DATA_DEVICE_NAME "TD"
  35. #define DATA_SWITCH "TA"
  36. #define DATA_MSG_ON "on"
  37. #define DATA_MSG_OFF "off"
  38. #ifndef ARRAYSIZE
  39. #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a[0])))
  40. #endif
  41. #ifndef bool
  42. #define bool unsigned char
  43. #endif
  44. #ifndef true
  45. #define true 1
  46. #endif
  47. #ifndef false
  48. #define false 0
  49. #endif
  50. #define SOCKET_CLIENT_DEBUG
  51. #ifdef SOCKET_CLIENT_DEBUG
  52. #define SCK_ERR(fmt, args...) printf("[SOCKET_ERROR][%s|%d]" fmt, __func__, __LINE__, ##args)
  53. #define SCK_DBG(fmt, args...) printf("[SOCKET_DEBUG][%s|%d]" fmt, __func__, __LINE__, ##args)
  54. #define SCK_INFO(fmt, args...) printf("[SOCKET_INFO][%s|%d]" fmt, __func__, __LINE__, ##args)
  55. #else
  56. #define SCK_ERR(fmt, args...) do {} while(0)
  57. #define SCK_DBG(fmt, args...) do {} while(0)
  58. #define SCK_INFO(fmt, args...) do {} while(0)
  59. #endif
  60. static void ResolveDevName(SocketEventCallback callback, char *value);
  61. static void ResolveSwitch(SocketEventCallback callback, char *value);
  62. typedef union {
  63. char msg[MSG_DATE_BUF_LEN];
  64. struct {
  65. char head[2];
  66. char cmd[2];
  67. char buff[20];
  68. } msg_info;
  69. } MsgInfo;
  70. typedef struct {
  71. char cmd[MSG_DATE_BUF_LEN];
  72. void (*func)(SocketEventCallback callback, char *value);
  73. } MsgData;
  74. static MsgData g_msgData[] = {
  75. {DATA_DEVICE_NAME, ResolveDevName},
  76. {DATA_SWITCH, ResolveSwitch}
  77. };
  78. static bool g_threadRunning = false;
  79. // ********************************************************************************************************************************** //
  80. static bool IsEqualTo(const char *msg1, const char *msg2, int length)
  81. {
  82. if (msg1 == NULL || msg2 == NULL || length <= 0) {
  83. SCK_ERR("NULL POINT! \n");
  84. return false;
  85. }
  86. return (strncasecmp(msg1, msg2, length) == 0);
  87. }
  88. static void ResolveDevName(SocketEventCallback callback, char *value)
  89. {
  90. SCK_INFO(" ########### value : %s ################ \n", value);
  91. }
  92. static void ResolveSwitch(SocketEventCallback callback, char *value)
  93. {
  94. SCK_INFO(" ########### value : %s ################ \n", value);
  95. if (callback != NULL) {
  96. callback(SOCKET_SET_CMD, value);
  97. }
  98. }
  99. static int SocketClientResolveData(const char *data, int len, SocketEventCallback callback)
  100. {
  101. MsgInfo msgInfo = {0};
  102. if (data == NULL || len <= 0) {
  103. SCK_ERR("NULL POINT!\n");
  104. return -1;
  105. }
  106. if (len > MSG_DATE_BUF_LEN) {
  107. len = MSG_DATE_BUF_LEN;
  108. }
  109. memcpy(msgInfo.msg, data, len);
  110. SCK_DBG("head:%s\n", msgInfo.msg_info.head);
  111. for (int i = 0; i < ARRAYSIZE(g_msgData); i++) {
  112. if (IsEqualTo(msgInfo.msg_info.cmd, g_msgData[i].cmd, strlen(g_msgData[i].cmd))) {
  113. g_msgData[i].func(callback, msgInfo.msg_info.buff);
  114. SCK_INFO(" cmd %s is match! \n", g_msgData[i].cmd);
  115. break;
  116. }
  117. }
  118. return 0;
  119. }
  120. static int SocketOpen(const char *ip, int port)
  121. {
  122. int sockfd;
  123. struct sockaddr_in recvAddr;
  124. if (ip == NULL || port <= 0) {
  125. return -1;
  126. }
  127. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  128. if (sockfd < 0) {
  129. SCK_ERR("socket failed! errno=%d\n", errno);
  130. return -1;
  131. }
  132. memset(&recvAddr, 0x00, sizeof(recvAddr));
  133. recvAddr.sin_family = AF_INET;
  134. recvAddr.sin_port = htons(port);
  135. recvAddr.sin_addr.s_addr = inet_addr(ip);
  136. if (connect(sockfd, (struct sockaddr *)&recvAddr, sizeof(recvAddr)) < 0) {
  137. SCK_ERR("connect failed! errno=%d\n", errno);
  138. close(sockfd);
  139. return -1;
  140. }
  141. return sockfd;
  142. }
  143. static int GetServerIp(char *ip, int size)
  144. {
  145. char recMsg[256] = {0};
  146. char sockfd;
  147. char *tmp = NULL;
  148. struct sockaddr_in localAddr, serverAddr;
  149. int sockaddr_len = sizeof(serverAddr);
  150. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  151. if (sockfd < 0) {
  152. SCK_ERR("socket failed!\n");
  153. return -1;
  154. }
  155. memset(&localAddr, 0x00, sizeof(localAddr));
  156. localAddr.sin_family = AF_INET;
  157. localAddr.sin_port = htons(UDP_DEF_PORT);
  158. localAddr.sin_addr.s_addr = inet_addr(INADDR_ANY);
  159. if (bind(sockfd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
  160. SCK_ERR("bind failed! errno : %d(%s)\n", errno, strerror(errno));
  161. close(sockfd);
  162. return -1;
  163. }
  164. if (recvfrom(sockfd, recMsg, sizeof(recMsg), 0, (struct sockaddr *)&serverAddr, &sockaddr_len) < 0) {
  165. SCK_ERR("recvfrom failed! \n");
  166. close(sockfd);
  167. return -1;
  168. }
  169. tmp = inet_ntoa(serverAddr.sin_addr);
  170. SCK_INFO("the server ip is %s \n", tmp);
  171. if (ip == NULL || size < strlen(tmp)) {
  172. SCK_ERR("params is invalid!! \n");
  173. close(sockfd);
  174. return -1;
  175. }
  176. strncpy(ip, tmp, strlen(tmp));
  177. close(sockfd);
  178. return 0;
  179. }
  180. static void SocketClientProcess(void *arg)
  181. {
  182. int sockfd = -1;
  183. char ipBuf[256] = {0};
  184. char sendBuf[256] = {0};
  185. char mDeviceName[256] = {0};
  186. SocketCallback *mCallback = (SocketCallback *)arg;
  187. if (mCallback == NULL) {
  188. SCK_ERR("socket callback is NULL! \n");
  189. return;
  190. }
  191. if (GetServerIp(ipBuf, sizeof(ipBuf)) < 0) {
  192. SCK_ERR("get server ip failed! \n");
  193. g_threadRunning = false;
  194. return;
  195. }
  196. sockfd = SocketOpen((const char *)ipBuf, TCP_DEF_PORT);
  197. if (sockfd < 0) {
  198. SCK_ERR("socket open failed! \n");
  199. g_threadRunning = false;
  200. return;
  201. }
  202. if (mCallback->socketEvent != NULL) {
  203. mCallback->socketEvent(SOCKET_CONNECTTED, NULL);
  204. }
  205. if (mCallback->socketGetDeviceName != NULL) {
  206. mCallback->socketGetDeviceName(mDeviceName, sizeof(mDeviceName));
  207. sprintf(sendBuf, "%s%s%s", PROTOCOL_HEADER, DATA_DEVICE_NAME, mDeviceName);
  208. if (send(sockfd, sendBuf, strlen(sendBuf), 0) < 0) {
  209. SCK_ERR("send %s failed! \n", sendBuf);
  210. goto EXIT;
  211. }
  212. }
  213. while (g_threadRunning) {
  214. char recvBuf[1024] = {0};
  215. int recvBytes = recv(sockfd, recvBuf, sizeof(recvBuf), 0);
  216. if (recvBytes <= 0) {
  217. break;
  218. }
  219. SCK_INFO("recvMsg[%d] : %s \n", recvBytes, recvBuf);
  220. if (SocketClientResolveData((const char *)recvBuf, recvBytes, mCallback->socketEvent) < 0) {
  221. break;
  222. }
  223. }
  224. EXIT:
  225. close(sockfd);
  226. sockfd = -1;
  227. if (mCallback->socketEvent != NULL) {
  228. mCallback->socketEvent(SOCKET_DISCONNECT, NULL);
  229. }
  230. g_threadRunning = false;
  231. }
  232. int SocketClientStart(SocketCallback *gCallback)
  233. {
  234. osThreadAttr_t attr;
  235. attr.name = "SocketClientTask";
  236. attr.attr_bits = 0U;
  237. attr.cb_mem = NULL;
  238. attr.cb_size = 0U;
  239. attr.stack_mem = NULL;
  240. attr.stack_size = SOCKET_TASK_STACK;
  241. attr.priority = SOCKET_TASK_PERIOD;
  242. g_threadRunning = true;
  243. if (osThreadNew(SocketClientProcess, (void *)gCallback, &attr) == NULL) {
  244. SCK_ERR("Falied to create ClientTask!\n");
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. void SocketClientStop(void)
  250. {
  251. if (g_threadRunning) {
  252. g_threadRunning = false;
  253. }
  254. }