You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include <fcntl.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. //
  9. #include <arpa/inet.h>
  10. #include <netinet/in.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. //
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include "uart.hpp"
  18. using namespace std;
  19. #define BACK_LOG 10
  20. #define MAX_RECV_SIZE 235
  21. typedef struct {
  22. int fd;
  23. struct sockaddr_in client;
  24. } tcpclient_t;
  25. map<string, uint32_t> g_baundmap = {
  26. {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
  27. {"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
  28. {"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
  29. {"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
  30. {"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
  31. {"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
  32. {"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
  33. {"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
  34. };
  35. UartDevice g_uart_device = {0};
  36. set<int> g_remote_client_fds;
  37. int safe_write(int fd, char *buff, size_t len) {
  38. int hassend = 0;
  39. while (true) {
  40. int send_this_time = write(fd, buff + hassend, len - hassend);
  41. if (send_this_time == 0) {
  42. continue;
  43. }
  44. if (send_this_time < 0) {
  45. return send_this_time;
  46. }
  47. hassend += send_this_time;
  48. if (hassend == len) {
  49. break;
  50. }
  51. }
  52. return len;
  53. }
  54. void printf_buf(char *rx, ssize_t size) {
  55. for (int i = 0; i < size; i++) {
  56. printf("0x%02x,", rx[i]);
  57. }
  58. printf("\n");
  59. }
  60. void *netclient_thread(void *arg) {
  61. tcpclient_t *client = (tcpclient_t *)arg;
  62. // while循环监听数据,接收到数据转发给串口
  63. char buff[4096];
  64. int n = 0;
  65. while ((n = read(client->fd, buff, 4096)) >= 0) {
  66. if (n == 0) {
  67. //返回零意味着对方关闭socket,超时返回的是负数
  68. break;
  69. }
  70. printf("net->uart: %d\n", n);
  71. printf_buf(buff, n);
  72. uartSend(&g_uart_device, buff, n);
  73. }
  74. printf("socket %d is closed by client-end\n", client->fd);
  75. close(client->fd);
  76. g_remote_client_fds.erase(client->fd);
  77. free(client);
  78. pthread_detach(pthread_self());
  79. return NULL;
  80. }
  81. void *uart_rx_thread_func(void *arg) {
  82. char buff[4096];
  83. while (true) {
  84. int ret = uartReceive(&g_uart_device, buff, 1024); // send the received text over UART
  85. if (ret < 0) {
  86. printf("uartReceive fail\n");
  87. exit(-1);
  88. }
  89. //
  90. if (ret > 0) {
  91. printf("net<-uart: %d\n", ret);
  92. printf_buf(buff, ret);
  93. for (auto &clientfd : g_remote_client_fds) {
  94. safe_write(clientfd, buff, ret);
  95. }
  96. }
  97. //
  98. }
  99. return NULL;
  100. }
  101. int start_tcp_server(int port) {
  102. int listenfd, connectfd;
  103. struct sockaddr_in server;
  104. struct sockaddr_in client;
  105. pid_t childpid;
  106. socklen_t addrlen;
  107. listenfd = socket(AF_INET, SOCK_STREAM, 0);
  108. if (listenfd == -1) {
  109. perror("socker created failed");
  110. exit(0);
  111. }
  112. int option;
  113. option = SO_REUSEADDR;
  114. setsockopt(listenfd, SOL_SOCKET, option, &option, sizeof(option));
  115. bzero(&server, sizeof(server));
  116. server.sin_family = AF_INET;
  117. server.sin_port = htons(port);
  118. server.sin_addr.s_addr = htonl(INADDR_ANY);
  119. printf("bind......\n");
  120. if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1) {
  121. perror("Bind error!");
  122. exit(1);
  123. }
  124. printf("listen......\n");
  125. if (listen(listenfd, BACK_LOG) == -1) {
  126. perror("listend error");
  127. exit(1);
  128. }
  129. printf("waiting for clinet's request.....\n");
  130. while (1) {
  131. int n;
  132. addrlen = sizeof(client);
  133. connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen);
  134. if (connectfd == -1) {
  135. perror("accept error");
  136. sleep(1);
  137. continue;
  138. }
  139. printf("%s connect to me,connectfd == %d\n", inet_ntoa(client.sin_addr), connectfd);
  140. g_remote_client_fds.insert(connectfd);
  141. pthread_t pthread;
  142. tcpclient_t *client = (tcpclient_t *)malloc(sizeof(tcpclient_t));
  143. if (client == NULL) {
  144. perror("malloc fail");
  145. exit(-1);
  146. }
  147. client->fd = connectfd;
  148. memcpy(&client->client, &client, sizeof(client->client));
  149. pthread_create(&pthread, NULL, netclient_thread, (void *)client);
  150. }
  151. return 0;
  152. }
  153. int openuart(struct UartDevice *device, const char *devname, int rate) {
  154. device->name = (char *)devname;
  155. device->rate = rate;
  156. printf("UART open %s\n", device->name);
  157. return uartStart(device, 0);
  158. }
  159. int main(int argc, char const *argv[]) {
  160. /* code */
  161. //"net_uart /dev/ttyUSB0 115200 port"
  162. if (argc != 4) {
  163. printf("Usage: %s /dev/ttyUSB0 115200 port\n", argv[0]);
  164. return -1;
  165. }
  166. printf("device name:%s\n", argv[1]);
  167. printf("baundrate :%s\n", argv[2]);
  168. printf("port :%s\n", argv[3]);
  169. auto baundrate_find_result = g_baundmap.find(argv[2]);
  170. if (baundrate_find_result == g_baundmap.end()) {
  171. printf("unsupport baundrate\n");
  172. return -1;
  173. // baundrate_find_result.
  174. };
  175. /**
  176. *
  177. */
  178. int rc = openuart(&g_uart_device, argv[1], baundrate_find_result->second);
  179. if (rc) {
  180. perror("open uart fail");
  181. exit(-1);
  182. }
  183. pthread_t uart_rx_thread;
  184. pthread_create(&uart_rx_thread, NULL, uart_rx_thread_func, NULL);
  185. start_tcp_server(atoi(argv[3]));
  186. while (true) {
  187. sleep(1);
  188. }
  189. return 0;
  190. }