transport.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdbool.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <sys/socket.h>
  9. #include <sys/types.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14. #include <netinet/in.h>
  15. #include <termios.h>
  16. #include <fcntl.h>
  17. #include "transport.h"
  18. #define SA struct sockaddr
  19. unsigned char leading[2] = { 0x12, 0x34 };
  20. bool
  21. tcp_init(const char *address, uint16_t port, int *fd)
  22. {
  23. int sock;
  24. struct sockaddr_in servaddr;
  25. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  26. return false;
  27. bzero(&servaddr, sizeof(servaddr));
  28. servaddr.sin_family = AF_INET;
  29. servaddr.sin_addr.s_addr = inet_addr(address);
  30. servaddr.sin_port = htons(port);
  31. if (connect(sock, (SA *)&servaddr, sizeof(servaddr)) != 0) {
  32. close(sock);
  33. return false;
  34. }
  35. *fd = sock;
  36. return true;
  37. }
  38. int
  39. parse_baudrate(int baud)
  40. {
  41. switch (baud) {
  42. case 9600:
  43. return B9600;
  44. case 19200:
  45. return B19200;
  46. case 38400:
  47. return B38400;
  48. case 57600:
  49. return B57600;
  50. case 115200:
  51. return B115200;
  52. case 230400:
  53. return B230400;
  54. case 460800:
  55. return B460800;
  56. case 500000:
  57. return B500000;
  58. case 576000:
  59. return B576000;
  60. case 921600:
  61. return B921600;
  62. case 1000000:
  63. return B1000000;
  64. case 1152000:
  65. return B1152000;
  66. case 1500000:
  67. return B1500000;
  68. case 2000000:
  69. return B2000000;
  70. case 2500000:
  71. return B2500000;
  72. case 3000000:
  73. return B3000000;
  74. case 3500000:
  75. return B3500000;
  76. case 4000000:
  77. return B4000000;
  78. default:
  79. return -1;
  80. }
  81. }
  82. bool
  83. uart_init(const char *device, int baudrate, int *fd)
  84. {
  85. int uart_fd;
  86. struct termios uart_term;
  87. uart_fd = open(device, O_RDWR | O_NOCTTY);
  88. if (uart_fd < 0)
  89. return false;
  90. memset(&uart_term, 0, sizeof(uart_term));
  91. uart_term.c_cflag = baudrate | CS8 | CLOCAL | CREAD;
  92. uart_term.c_iflag = IGNPAR;
  93. uart_term.c_oflag = 0;
  94. /* set noncanonical mode */
  95. uart_term.c_lflag = 0;
  96. uart_term.c_cc[VTIME] = 30;
  97. uart_term.c_cc[VMIN] = 1;
  98. tcflush(uart_fd, TCIFLUSH);
  99. if (tcsetattr(uart_fd, TCSANOW, &uart_term) != 0) {
  100. close(uart_fd);
  101. return false;
  102. }
  103. *fd = uart_fd;
  104. return true;
  105. }
  106. bool
  107. udp_send(const char *address, int port, const char *buf, int len)
  108. {
  109. int sockfd;
  110. ssize_t size_sent;
  111. struct sockaddr_in servaddr;
  112. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  113. return false;
  114. memset(&servaddr, 0, sizeof(servaddr));
  115. servaddr.sin_family = AF_INET;
  116. servaddr.sin_port = htons(port);
  117. servaddr.sin_addr.s_addr = INADDR_ANY;
  118. size_sent = sendto(sockfd, buf, len, MSG_CONFIRM,
  119. (const struct sockaddr *)&servaddr, sizeof(servaddr));
  120. close(sockfd);
  121. return (size_sent != -1) ? true : false;
  122. }
  123. bool
  124. host_tool_send_data(int fd, const char *buf, unsigned int len)
  125. {
  126. int cnt = 0;
  127. ssize_t ret;
  128. if (fd == -1 || buf == NULL || len <= 0) {
  129. return false;
  130. }
  131. resend:
  132. ret = write(fd, buf, len);
  133. if (ret == -1) {
  134. if (errno == ECONNRESET) {
  135. close(fd);
  136. }
  137. // repeat sending if the outbuffer is full
  138. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  139. if (++cnt > 10) {
  140. close(fd);
  141. return false;
  142. }
  143. sleep(1);
  144. goto resend;
  145. }
  146. }
  147. return (ret == len);
  148. }
  149. #define SET_RECV_PHASE(ctx, new_phase) \
  150. do { \
  151. ctx->phase = new_phase; \
  152. ctx->size_in_phase = 0; \
  153. } while (0)
  154. /*
  155. * input: 1 byte from remote
  156. * output: parse result
  157. * return: -1 invalid sync byte
  158. * 1 byte added to buffer, waiting more for complete packet
  159. * 0 completed packet
  160. * 2 in receiving payload
  161. */
  162. int
  163. on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
  164. {
  165. if (ctx->phase == Phase_Non_Start) {
  166. if (ctx->message.payload) {
  167. free(ctx->message.payload);
  168. ctx->message.payload = NULL;
  169. ctx->message.payload_size = 0;
  170. }
  171. if (leading[0] == ch) {
  172. ctx->phase = Phase_Leading;
  173. }
  174. else {
  175. return -1;
  176. }
  177. }
  178. else if (ctx->phase == Phase_Leading) {
  179. if (leading[1] == ch) {
  180. SET_RECV_PHASE(ctx, Phase_Type);
  181. }
  182. else {
  183. ctx->phase = Phase_Non_Start;
  184. return -1;
  185. }
  186. }
  187. else if (ctx->phase == Phase_Type) {
  188. unsigned char *p = (unsigned char *)&ctx->message.message_type;
  189. p[ctx->size_in_phase++] = ch;
  190. if (ctx->size_in_phase == sizeof(ctx->message.message_type)) {
  191. ctx->message.message_type = ntohs(ctx->message.message_type);
  192. SET_RECV_PHASE(ctx, Phase_Size);
  193. }
  194. }
  195. else if (ctx->phase == Phase_Size) {
  196. unsigned char *p = (unsigned char *)&ctx->message.payload_size;
  197. p[ctx->size_in_phase++] = ch;
  198. if (ctx->size_in_phase == sizeof(ctx->message.payload_size)) {
  199. ctx->message.payload_size = ntohl(ctx->message.payload_size);
  200. SET_RECV_PHASE(ctx, Phase_Payload);
  201. if (ctx->message.payload) {
  202. free(ctx->message.payload);
  203. ctx->message.payload = NULL;
  204. }
  205. /* no payload */
  206. if (ctx->message.payload_size == 0) {
  207. SET_RECV_PHASE(ctx, Phase_Non_Start);
  208. return 0;
  209. }
  210. ctx->message.payload = (char *)malloc(ctx->message.payload_size);
  211. SET_RECV_PHASE(ctx, Phase_Payload);
  212. }
  213. }
  214. else if (ctx->phase == Phase_Payload) {
  215. ctx->message.payload[ctx->size_in_phase++] = ch;
  216. if (ctx->size_in_phase == ctx->message.payload_size) {
  217. SET_RECV_PHASE(ctx, Phase_Non_Start);
  218. return 0;
  219. }
  220. return 2;
  221. }
  222. return 1;
  223. }