HAL_TCP_rtthread.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <sys/time.h>
  22. #include <sys/socket.h>
  23. #include <sys/select.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <netinet/tcp.h>
  28. #include <netdb.h>
  29. #include <rtthread.h>
  30. #include "iot_import.h"
  31. #undef perror
  32. #define perror rt_kprintf
  33. #define PLATFORM_RTTHREADSOCK_LOG(format, ...) \
  34. do { \
  35. HAL_Printf("LINUXSOCK %u %s() | "format"\n", __LINE__, __FUNCTION__, ##__VA_ARGS__);\
  36. } while(0);
  37. static uint64_t _rtthread_get_time_ms(void)
  38. {
  39. #if (RT_TICK_PER_SECOND == 1000)
  40. /* #define RT_TICK_PER_SECOND 1000 */
  41. return (uint64_t)rt_tick_get();
  42. #else
  43. uint64_t tick = 0;
  44. uint64_t clock_ms = 0;
  45. tick = rt_tick_get();
  46. tick = tick * 1000;
  47. return (uint64_t)((tick + RT_TICK_PER_SECOND - 1)/RT_TICK_PER_SECOND);
  48. #endif
  49. }
  50. static uint64_t _rtthread_time_left(uint64_t t_end, uint64_t t_now)
  51. {
  52. uint64_t t_left;
  53. if (t_end > t_now) {
  54. t_left = t_end - t_now;
  55. } else {
  56. t_left = 0;
  57. }
  58. return t_left;
  59. }
  60. uintptr_t HAL_TCP_Establish(const char *host, uint16_t port)
  61. {
  62. struct addrinfo hints;
  63. struct addrinfo *addrInfoList = NULL;
  64. struct addrinfo *cur = NULL;
  65. int fd = 0;
  66. int rc = 0;
  67. char service[6];
  68. memset(&hints, 0, sizeof(hints));
  69. PLATFORM_RTTHREADSOCK_LOG("establish tcp connection with server(host=%s port=%u)", host, port);
  70. hints.ai_family = AF_INET; /* only IPv4 */
  71. hints.ai_socktype = SOCK_STREAM;
  72. hints.ai_protocol = IPPROTO_TCP;
  73. sprintf(service, "%u", port);
  74. if ((rc = getaddrinfo(host, service, &hints, &addrInfoList)) != 0) {
  75. perror("getaddrinfo error");
  76. return 0;
  77. }
  78. for (cur = addrInfoList; cur != NULL; cur = cur->ai_next) {
  79. if (cur->ai_family != AF_INET) {
  80. perror("socket type error");
  81. rc = 0;
  82. continue;
  83. }
  84. fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
  85. if (fd < 0) {
  86. perror("create socket error");
  87. rc = 0;
  88. continue;
  89. }
  90. if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0) {
  91. rc = fd;
  92. break;
  93. }
  94. close(fd);
  95. perror("connect error");
  96. rc = 0;
  97. }
  98. if (0 == rc) {
  99. PLATFORM_RTTHREADSOCK_LOG("fail to establish tcp");
  100. } else {
  101. PLATFORM_RTTHREADSOCK_LOG("success to establish tcp, fd=%d", rc);
  102. }
  103. freeaddrinfo(addrInfoList);
  104. return (uintptr_t)rc;
  105. }
  106. int32_t HAL_TCP_Destroy(uintptr_t fd)
  107. {
  108. int rc;
  109. rc = close((int) fd);
  110. if (0 != rc) {
  111. perror("closesocket error");
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. int32_t HAL_TCP_Write(uintptr_t fd, const char *buf, uint32_t len, uint32_t timeout_ms)
  117. {
  118. int ret;
  119. uint32_t len_sent;
  120. uint64_t t_end, t_left;
  121. fd_set sets;
  122. t_end = _rtthread_get_time_ms() + timeout_ms;
  123. len_sent = 0;
  124. ret = 1; /* send one time if timeout_ms is value 0 */
  125. do {
  126. t_left = _rtthread_time_left(t_end, _rtthread_get_time_ms());
  127. if (0 != t_left) {
  128. struct timeval timeout;
  129. FD_ZERO(&sets);
  130. FD_SET(fd, &sets);
  131. timeout.tv_sec = t_left / 1000;
  132. timeout.tv_usec = (t_left % 1000) * 1000;
  133. ret = select(fd + 1, NULL, &sets, NULL, &timeout);
  134. if (ret > 0) {
  135. if (0 == FD_ISSET(fd, &sets)) {
  136. PLATFORM_RTTHREADSOCK_LOG("Should NOT arrive");
  137. /* If timeout in next loop, it will not sent any data */
  138. ret = 0;
  139. continue;
  140. }
  141. } else if (0 == ret) {
  142. PLATFORM_RTTHREADSOCK_LOG("select-write timeout %d", (int)fd);
  143. break;
  144. } else {
  145. if (EINTR == errno) {
  146. PLATFORM_RTTHREADSOCK_LOG("EINTR be caught");
  147. continue;
  148. }
  149. perror("select-write fail");
  150. break;
  151. }
  152. }
  153. if (ret > 0) {
  154. ret = send(fd, buf + len_sent, len - len_sent, 0);
  155. if (ret > 0) {
  156. len_sent += ret;
  157. } else if (0 == ret) {
  158. PLATFORM_RTTHREADSOCK_LOG("No data be sent");
  159. } else {
  160. if (EINTR == errno) {
  161. PLATFORM_RTTHREADSOCK_LOG("EINTR be caught");
  162. continue;
  163. }
  164. perror("send fail");
  165. break;
  166. }
  167. }
  168. } while ((len_sent < len) && (_rtthread_time_left(t_end, _rtthread_get_time_ms()) > 0));
  169. return len_sent;
  170. }
  171. int32_t HAL_TCP_Read(uintptr_t fd, char *buf, uint32_t len, uint32_t timeout_ms)
  172. {
  173. int ret, err_code;
  174. uint32_t len_recv;
  175. uint64_t t_end, t_left;
  176. fd_set sets;
  177. struct timeval timeout;
  178. t_end = _rtthread_get_time_ms() + timeout_ms;
  179. len_recv = 0;
  180. err_code = 0;
  181. do {
  182. t_left = _rtthread_time_left(t_end, _rtthread_get_time_ms());
  183. if (0 == t_left) {
  184. break;
  185. }
  186. FD_ZERO(&sets);
  187. FD_SET(fd, &sets);
  188. timeout.tv_sec = t_left / 1000;
  189. timeout.tv_usec = (t_left % 1000) * 1000;
  190. ret = select(fd + 1, &sets, NULL, NULL, &timeout);
  191. if (ret > 0) {
  192. ret = recv(fd, buf + len_recv, len - len_recv, 0);
  193. if (ret > 0) {
  194. len_recv += ret;
  195. } else if (0 == ret) {
  196. perror("connection is closed");
  197. err_code = -1;
  198. break;
  199. } else {
  200. if (EINTR == errno) {
  201. PLATFORM_RTTHREADSOCK_LOG("EINTR be caught");
  202. continue;
  203. }
  204. perror("recv fail");
  205. err_code = -2;
  206. break;
  207. }
  208. } else if (0 == ret) {
  209. break;
  210. } else {
  211. perror("select-recv fail");
  212. err_code = -2;
  213. break;
  214. }
  215. } while ((len_recv < len));
  216. /* priority to return data bytes if any data be received from TCP connection. */
  217. /* It will get error code on next calling */
  218. return (0 != len_recv) ? len_recv : err_code;
  219. }