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
204 lines
5.2 KiB
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
//
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
//
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
|
|
#include "uart.hpp"
|
|
using namespace std;
|
|
#define BACK_LOG 10
|
|
#define MAX_RECV_SIZE 235
|
|
|
|
typedef struct {
|
|
int fd;
|
|
struct sockaddr_in client;
|
|
} tcpclient_t;
|
|
map<string, uint32_t> g_baundmap = {
|
|
{"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
|
|
{"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
|
|
{"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
|
|
{"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
|
|
{"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
|
|
{"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
|
|
{"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
|
|
{"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
|
|
};
|
|
UartDevice g_uart_device = {0};
|
|
set<int> g_remote_client_fds;
|
|
|
|
int safe_write(int fd, char *buff, size_t len) {
|
|
int hassend = 0;
|
|
while (true) {
|
|
int send_this_time = write(fd, buff + hassend, len - hassend);
|
|
if (send_this_time == 0) {
|
|
continue;
|
|
}
|
|
if (send_this_time < 0) {
|
|
return send_this_time;
|
|
}
|
|
|
|
hassend += send_this_time;
|
|
if (hassend == len) {
|
|
break;
|
|
}
|
|
}
|
|
return len;
|
|
}
|
|
|
|
void printf_buf(char *rx, ssize_t size) {
|
|
for (int i = 0; i < size; i++) {
|
|
printf("0x%02x,", rx[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void *netclient_thread(void *arg) {
|
|
tcpclient_t *client = (tcpclient_t *)arg;
|
|
// while循环监听数据,接收到数据转发给串口
|
|
char buff[4096];
|
|
int n = 0;
|
|
while ((n = read(client->fd, buff, 4096)) >= 0) {
|
|
printf("net->uart: %d\n", n);
|
|
printf_buf(buff, n);
|
|
uartSend(&g_uart_device, buff, n);
|
|
}
|
|
printf("socket %d is closed by client-end\n", client->fd);
|
|
close(client->fd);
|
|
g_remote_client_fds.erase(client->fd);
|
|
|
|
free(client);
|
|
pthread_detach(pthread_self());
|
|
return NULL;
|
|
}
|
|
|
|
void *uart_rx_thread(void *arg) {
|
|
char buff[4096];
|
|
while (true) {
|
|
int ret = uartReceive(&g_uart_device, buff, 1024); // send the received text over UART
|
|
if (ret < 0) {
|
|
printf("uartReceive fail\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
if (ret > 0) {
|
|
printf("net<-uart: %d\n", ret);
|
|
printf_buf(buff, ret);
|
|
for (auto &clientfd : g_remote_client_fds) {
|
|
safe_write(clientfd, buff, ret);
|
|
}
|
|
}
|
|
//
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int start_tcp_server(int port) {
|
|
int listenfd, connectfd;
|
|
struct sockaddr_in server;
|
|
struct sockaddr_in client;
|
|
pid_t childpid;
|
|
socklen_t addrlen;
|
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (listenfd == -1) {
|
|
perror("socker created failed");
|
|
exit(0);
|
|
}
|
|
int option;
|
|
option = SO_REUSEADDR;
|
|
setsockopt(listenfd, SOL_SOCKET, option, &option, sizeof(option));
|
|
bzero(&server, sizeof(server));
|
|
server.sin_family = AF_INET;
|
|
server.sin_port = htons(port);
|
|
server.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
printf("bind......\n");
|
|
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1) {
|
|
perror("Bind error!");
|
|
exit(1);
|
|
}
|
|
printf("listen......\n");
|
|
if (listen(listenfd, BACK_LOG) == -1) {
|
|
perror("listend error");
|
|
exit(1);
|
|
}
|
|
printf("waiting for clinet's request.....\n");
|
|
while (1) {
|
|
int n;
|
|
addrlen = sizeof(client);
|
|
connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen);
|
|
if (connectfd == -1) {
|
|
perror("accept error");
|
|
sleep(1);
|
|
continue;
|
|
}
|
|
|
|
printf("%s connect to me,connectfd == %d\n", inet_ntoa(client.sin_addr), connectfd);
|
|
g_remote_client_fds.insert(connectfd);
|
|
|
|
pthread_t pthread;
|
|
tcpclient_t *client = (tcpclient_t *)malloc(sizeof(tcpclient_t));
|
|
if (client == NULL) {
|
|
perror("malloc fail");
|
|
exit(-1);
|
|
}
|
|
client->fd = connectfd;
|
|
memcpy(&client->client, &client, sizeof(client->client));
|
|
pthread_create(&pthread, NULL, netclient_thread, (void *)client);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int openuart(struct UartDevice *device, const char *devname, int rate) {
|
|
device->name = (char *)devname;
|
|
device->rate = rate;
|
|
|
|
printf("UART open %s\n", device->name);
|
|
return uartStart(device, 0);
|
|
}
|
|
int main(int argc, char const *argv[]) {
|
|
/* code */
|
|
//"net_uart /dev/ttyUSB0 115200 port"
|
|
|
|
if (argc != 4) {
|
|
printf("Usage: %s /dev/ttyUSB0 115200 port\n", argv[0]);
|
|
return -1;
|
|
}
|
|
printf("device name:%s\n", argv[1]);
|
|
printf("baundrate :%s\n", argv[2]);
|
|
printf("port :%s\n", argv[3]);
|
|
|
|
auto baundrate_find_result = g_baundmap.find(argv[2]);
|
|
if (baundrate_find_result == g_baundmap.end()) {
|
|
printf("unsupport baundrate\n");
|
|
return -1;
|
|
// baundrate_find_result.
|
|
};
|
|
|
|
/**
|
|
* 打开串口
|
|
*/
|
|
int rc = openuart(&g_uart_device, argv[1], baundrate_find_result->second);
|
|
if (rc) {
|
|
perror("open uart fail");
|
|
exit(-1);
|
|
}
|
|
|
|
pthread_t uart_rx_thread;
|
|
pthread_create(&uart_rx_thread, NULL, netclient_thread, NULL);
|
|
|
|
start_tcp_server(atoi(argv[3]));
|
|
|
|
return 0;
|
|
}
|