HAL_TCP_rtthread.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/socket.h>
  18. #include <sys/time.h>
  19. #include <netinet/tcp.h>
  20. #include <netdb.h>
  21. #include <rtthread.h>
  22. #include "uiot_import.h"
  23. static uint64_t rtthread_get_time_ms(void)
  24. {
  25. #if (RT_TICK_PER_SECOND == 1000)
  26. /* #define RT_TICK_PER_SECOND 1000 */
  27. return (unsigned long)rt_tick_get();
  28. #else
  29. unsigned long tick = 0;
  30. tick = rt_tick_get();
  31. tick = tick * 1000;
  32. return (unsigned long)((tick + RT_TICK_PER_SECOND - 1)/RT_TICK_PER_SECOND);
  33. #endif
  34. }
  35. static uint64_t rtthread_time_left(uint64_t t_end, uint64_t t_now)
  36. {
  37. uint64_t t_left;
  38. if (t_end > t_now) {
  39. t_left = t_end - t_now;
  40. } else {
  41. t_left = 0;
  42. }
  43. return t_left;
  44. }
  45. uintptr_t HAL_TCP_Connect(_IN_ const char *host, _IN_ uint16_t port) {
  46. struct addrinfo hints;
  47. struct addrinfo *addrInfoList = NULL;
  48. struct addrinfo *cur = NULL;
  49. int fd = 0;
  50. int rc = 0;
  51. char service[6];
  52. memset(&hints, 0, sizeof(hints));
  53. printf("establish tcp connection with server(host='%s', port=[%u])\n", host, port);
  54. hints.ai_family = AF_INET; /* only IPv4 */
  55. hints.ai_socktype = SOCK_STREAM;
  56. hints.ai_protocol = IPPROTO_TCP;
  57. sprintf(service, "%u", port);
  58. if ((rc = getaddrinfo(host, service, &hints, &addrInfoList)) != 0) {
  59. printf("getaddrinfo error(%d), host = '%s', port = [%d]\n", rc, host, port);
  60. return (uintptr_t) (-1);
  61. }
  62. for (cur = addrInfoList; cur != NULL; cur = cur->ai_next) {
  63. if (cur->ai_family != AF_INET) {
  64. printf("socket type error\n");
  65. rc = -1;
  66. continue;
  67. }
  68. fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
  69. if (fd < 0) {
  70. printf("create socket error\n");
  71. rc = -1;
  72. continue;
  73. }
  74. if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0) {
  75. rc = fd;
  76. break;
  77. }
  78. closesocket(fd);
  79. printf("connect error\n");
  80. rc = -1;
  81. }
  82. if (-1 == rc) {
  83. printf("fail to establish tcp\n");
  84. } else {
  85. printf("success to establish tcp, fd=%d\n", rc);
  86. }
  87. freeaddrinfo(addrInfoList);
  88. return (uintptr_t) rc;
  89. }
  90. int32_t HAL_TCP_Disconnect(_IN_ uintptr_t fd) {
  91. int rc;
  92. rc = closesocket((int) fd);
  93. if (0 != rc) {
  94. printf("close socket error\n");
  95. return FAILURE_RET;
  96. }
  97. return SUCCESS_RET;
  98. }
  99. int32_t HAL_TCP_Write(_IN_ uintptr_t fd, _IN_ unsigned char *buf, _IN_ size_t len, _IN_ uint32_t timeout_ms) {
  100. int ret,tcp_fd;
  101. size_t len_sent;
  102. uint64_t t_end;
  103. t_end = rtthread_get_time_ms() + timeout_ms;
  104. len_sent = 0;
  105. ret = 1; /* send one time if timeout_ms is value 0 */
  106. tcp_fd = (int)fd;
  107. do {
  108. uint64_t t_left = rtthread_time_left(t_end, rtthread_get_time_ms());
  109. ret = send(tcp_fd, buf + len_sent, len - len_sent, 0);
  110. if (ret > 0) {
  111. len_sent += ret;
  112. }
  113. else if (errno == EINTR || errno == EAGAIN){
  114. printf("send fail,try again\n");
  115. continue;
  116. }
  117. else{
  118. break;
  119. }
  120. } while ((len_sent < len)&& (rtthread_time_left(t_end, rtthread_get_time_ms()) > 0));
  121. return len_sent > 0 ? len_sent : ERR_TCP_WRITE_FAILED;
  122. }
  123. int32_t HAL_TCP_Read(_IN_ uintptr_t fd, _OU_ unsigned char *buf, _IN_ size_t len, _IN_ uint32_t timeout_ms) {
  124. int ret,tcp_fd;
  125. IoT_Error_t err_code;
  126. size_t len_recv;
  127. uint64_t t_end;
  128. t_end = rtthread_get_time_ms() + timeout_ms;
  129. len_recv = 0;
  130. err_code = SUCCESS_RET;
  131. tcp_fd = (int)fd;
  132. do {
  133. uint64_t t_left = rtthread_time_left(t_end, rtthread_get_time_ms());
  134. if (0 == t_left) {
  135. break;
  136. }
  137. ret = recv(tcp_fd, buf + len_recv, len - len_recv, MSG_DONTWAIT);
  138. if (ret > 0) {
  139. len_recv += ret;
  140. }
  141. else if (0 == ret)
  142. {
  143. continue;
  144. }
  145. else
  146. {
  147. if (errno == EINTR || errno == EAGAIN)
  148. {
  149. continue;
  150. }
  151. printf("read fail,try again\n");
  152. err_code = ERR_TCP_READ_FAILED;
  153. break;
  154. }
  155. } while (len_recv < len);
  156. return (0 != len_recv) ? len_recv : err_code;
  157. }