-
Notifications
You must be signed in to change notification settings - Fork 3
/
sconsole.h
176 lines (157 loc) · 4.73 KB
/
sconsole.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* sconsole.h - portable TCP socket serial port emulator
*
* Copyright (c) Michal Tomek 2018-2019 <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define TCPIP_error WSAGetLastError()
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define SOCKET int
#define closesocket close
#define TCPIP_error errno
#define SD_BOTH SHUT_RDWR
#define ioctlsocket ioctl
#endif
// MAX_SOCKET_PORTS and BASE_PORT needs to be defined
SOCKET listen_sockets[MAX_SOCKET_PORTS];
SOCKET client_sockets[MAX_SOCKET_PORTS];
int init_TCPIP() {
int i;
for (i=0;i<MAX_SOCKET_PORTS;i++) {
client_sockets[i] = INVALID_SOCKET;
listen_sockets[i] = INVALID_SOCKET;
}
#ifdef _WIN32
int e;
static WSADATA wsaData;
if ((e = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) {
printf("Serial: WSAStartup err %d\n", e);
return -1;
}
#endif
return 0;
}
void shutdown_TCPIP() {
#ifdef _WIN32
WSACleanup();
#endif
}
int init_socket_port(int port) {
struct addrinfo *res = NULL;
struct addrinfo h;
char port_str[6];
int e;
memset(&h, 0, sizeof(h));
h.ai_family = AF_INET;
h.ai_socktype = SOCK_STREAM;
h.ai_protocol = IPPROTO_TCP;
h.ai_flags = AI_PASSIVE;
sprintf(port_str,"%d",BASE_PORT+port);
if ( (e = getaddrinfo(NULL, port_str, &h, &res)) != 0 ) {
printf("Serial: getaddrinfo err %d\n", e);
return -1;
}
listen_sockets[port] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (listen_sockets[port] == INVALID_SOCKET) {
printf("Serial: socket err %d\n", TCPIP_error);
freeaddrinfo(res);
return -1;
}
#ifndef _WIN32
e = 1; // enable socket reuse
setsockopt(listen_sockets[port], SOL_SOCKET, SO_REUSEADDR, &e, sizeof(int));
#endif
if (bind( listen_sockets[port], res->ai_addr, (int)res->ai_addrlen) == SOCKET_ERROR) {
printf("Serial: bind err %d\n", TCPIP_error);
freeaddrinfo(res);
closesocket(listen_sockets[port]);
return -1;
}
freeaddrinfo(res);
if (listen(listen_sockets[port], SOMAXCONN) == SOCKET_ERROR) {
printf("Serial: listen err %d\n", TCPIP_error);
closesocket(listen_sockets[port]);
return -1;
}
printf("Serial port %d listening on %s\n", port, port_str);
return 0;
}
int open_socket_port(int port) {
if (client_sockets[port] != INVALID_SOCKET) {
printf("Serial port %d connection lost\n", port);
closesocket(client_sockets[port]);
}
client_sockets[port] = INVALID_SOCKET;
client_sockets[port] = accept(listen_sockets[port], NULL, NULL);
if (listen_sockets[port]!= INVALID_SOCKET ) { // don't complain when shutting down
if (client_sockets[port] == INVALID_SOCKET) {
printf("Serial: accept err %d\n", TCPIP_error);
//closesocket(listen_sockets[port]);
return -1;
}
unsigned long mode = 1;
ioctlsocket(client_sockets[port], FIONBIO, &mode); // nonblocking
printf("Serial port %d connected\n",port);
}
return 0;
}
void shutdown_socket_ports() {
int i;
for (i=0;i<MAX_SOCKET_PORTS;i++)
{
if (client_sockets[i] != INVALID_SOCKET) {
shutdown(client_sockets[i], SD_BOTH);
closesocket(client_sockets[i]);
client_sockets[i] = INVALID_SOCKET;
}
if (listen_sockets[i] != INVALID_SOCKET) {
shutdown(listen_sockets[i], SD_BOTH);
closesocket(listen_sockets[i]);
listen_sockets[i] = INVALID_SOCKET;
}
}
shutdown_TCPIP();
}
int char_available_socket_port(int port) {
unsigned long iMode = 0;
ioctlsocket(client_sockets[port], FIONREAD, &iMode);
return iMode!=0;
}
int is_connected_socket_port(int port) {
char buf;
return client_sockets[port] != INVALID_SOCKET && recv(client_sockets[port], &buf, 1, MSG_PEEK)!=0;
}
void tx_socket_port(int port, uint8_t data) {
send( client_sockets[port], (char*)&data, 1, 0 );
}
int rx_socket_port(int port) {
int data = 0;
recv( client_sockets[port], (char*)&data, 1, 0);
return data;
}