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.

204 lines
5.2 KiB

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. printf("net->uart: %d\n", n);
  67. printf_buf(buff, n);
  68. uartSend(&g_uart_device, buff, n);
  69. }
  70. printf("socket %d is closed by client-end\n", client->fd);
  71. close(client->fd);
  72. g_remote_client_fds.erase(client->fd);
  73. free(client);
  74. pthread_detach(pthread_self());
  75. return NULL;
  76. }
  77. void *uart_rx_thread(void *arg) {
  78. char buff[4096];
  79. while (true) {
  80. int ret = uartReceive(&g_uart_device, buff, 1024); // send the received text over UART
  81. if (ret < 0) {
  82. printf("uartReceive fail\n");
  83. exit(-1);
  84. }
  85. //
  86. if (ret > 0) {
  87. printf("net<-uart: %d\n", ret);
  88. printf_buf(buff, ret);
  89. for (auto &clientfd : g_remote_client_fds) {
  90. safe_write(clientfd, buff, ret);
  91. }
  92. }
  93. //
  94. }
  95. return NULL;
  96. }
  97. int start_tcp_server(int port) {
  98. int listenfd, connectfd;
  99. struct sockaddr_in server;
  100. struct sockaddr_in client;
  101. pid_t childpid;
  102. socklen_t addrlen;
  103. listenfd = socket(AF_INET, SOCK_STREAM, 0);
  104. if (listenfd == -1) {
  105. perror("socker created failed");
  106. exit(0);
  107. }
  108. int option;
  109. option = SO_REUSEADDR;
  110. setsockopt(listenfd, SOL_SOCKET, option, &option, sizeof(option));
  111. bzero(&server, sizeof(server));
  112. server.sin_family = AF_INET;
  113. server.sin_port = htons(port);
  114. server.sin_addr.s_addr = htonl(INADDR_ANY);
  115. printf("bind......\n");
  116. if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1) {
  117. perror("Bind error!");
  118. exit(1);
  119. }
  120. printf("listen......\n");
  121. if (listen(listenfd, BACK_LOG) == -1) {
  122. perror("listend error");
  123. exit(1);
  124. }
  125. printf("waiting for clinet's request.....\n");
  126. while (1) {
  127. int n;
  128. addrlen = sizeof(client);
  129. connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen);
  130. if (connectfd == -1) {
  131. perror("accept error");
  132. sleep(1);
  133. continue;
  134. }
  135. printf("%s connect to me,connectfd == %d\n", inet_ntoa(client.sin_addr), connectfd);
  136. g_remote_client_fds.insert(connectfd);
  137. pthread_t pthread;
  138. tcpclient_t *client = (tcpclient_t *)malloc(sizeof(tcpclient_t));
  139. if (client == NULL) {
  140. perror("malloc fail");
  141. exit(-1);
  142. }
  143. client->fd = connectfd;
  144. memcpy(&client->client, &client, sizeof(client->client));
  145. pthread_create(&pthread, NULL, netclient_thread, (void *)client);
  146. }
  147. return 0;
  148. }
  149. int openuart(struct UartDevice *device, const char *devname, int rate) {
  150. device->name = (char *)devname;
  151. device->rate = rate;
  152. printf("UART open %s\n", device->name);
  153. return uartStart(device, 0);
  154. }
  155. int main(int argc, char const *argv[]) {
  156. /* code */
  157. //"net_uart /dev/ttyUSB0 115200 port"
  158. if (argc != 4) {
  159. printf("Usage: %s /dev/ttyUSB0 115200 port\n", argv[0]);
  160. return -1;
  161. }
  162. printf("device name:%s\n", argv[1]);
  163. printf("baundrate :%s\n", argv[2]);
  164. printf("port :%s\n", argv[3]);
  165. auto baundrate_find_result = g_baundmap.find(argv[2]);
  166. if (baundrate_find_result == g_baundmap.end()) {
  167. printf("unsupport baundrate\n");
  168. return -1;
  169. // baundrate_find_result.
  170. };
  171. /**
  172. *
  173. */
  174. int rc = openuart(&g_uart_device, argv[1], baundrate_find_result->second);
  175. if (rc) {
  176. perror("open uart fail");
  177. exit(-1);
  178. }
  179. pthread_t uart_rx_thread;
  180. pthread_create(&uart_rx_thread, NULL, netclient_thread, NULL);
  181. start_tcp_server(atoi(argv[3]));
  182. return 0;
  183. }