-
Notifications
You must be signed in to change notification settings - Fork 2
/
ttyaccess.c
273 lines (234 loc) · 5.81 KB
/
ttyaccess.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* File: ttyaccess.c
Author: Mats Engstrom, April 2020
Language: C (UNIX)
Purpose: Handle character-by-character input from stdin and udp using pthreads
*/
#define _XOPEN_SOURCE 500 // Enable timestruct definitions in C99
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sgtty.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/errno.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include "ttyaccess.h"
struct threaddata_struct {
int termpipe;
int udp_socket;
};
static pthread_t thread;
static int pipes[2];
static struct threaddata_struct td;
#define RBLEN 1024
static volatile int rbhead = 0; // head pointer for ring buffer queue
static volatile int rbtail = 0; // tail pointer for ring buffer queue
static volatile char rbqueue[RBLEN];
static pthread_mutex_t rblock; // Mutex for locking the ring buffer
static char breakcnt;
//
// Store character into ringbuffer
//
static void queue_to_rb(char ch) {
int newtail = (rbtail + 1) % RBLEN;
if (newtail != rbhead) {
rbqueue[rbtail] = ch;
rbtail = newtail;
}
if (ch==3) breakcnt++;
else breakcnt=0;
}
//
// Get a character from ringbuffer
// returns -1 if empty, else the character
//
int get_from_rb(void) {
int ch=-1;
pthread_mutex_lock(&rblock);
if (rbhead != rbtail) {
ch = rbqueue[rbhead];
rbhead = (rbhead + 1) % RBLEN;
}
pthread_mutex_unlock(&rblock);
return ch;
}
//
// Return number of characters available in the ring buffer
//
int count_rb(void) {
int cnt;
pthread_mutex_lock(&rblock);
cnt=abs(rbhead-rbtail);
pthread_mutex_unlock(&rblock);
return cnt;
}
//
// Set stdin to either "raw" or normal mode
// mode=true for raw, false=normal
//
static void set_stdin_raw(int mode) {
static struct termios oldtio;
static int lastMode=0;
struct termios newtio;
if (mode) {
tcgetattr(STDIN_FILENO, &oldtio); // save current port settings
bzero(&newtio, sizeof(newtio));
newtio.c_lflag=0; // set input mode (non-canonical, no echo
newtio.c_cc[VTIME]=0; // inter-character timer unused
newtio.c_cc[VMIN]=1; // blocking read
tcflush(STDIN_FILENO, TCIFLUSH);
tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
} else {
if (lastMode) tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
}
lastMode=mode;
}
//
// Create and bind to a listening UDP socket
//
static int create_udp_socket(int port) {
struct sockaddr_in server_address;
int sock;
int r;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
sock = socket(PF_INET, SOCK_DGRAM, 0);
for (int i=0; i<5; i++) {
r=bind(sock, (struct sockaddr *)&server_address, sizeof(server_address));
if (r==0) break;
sleep(1);
}
if (r < 0) {
printf("Could not bind socket to UDP port %d\n",port);
exit(1);
}
return sock;
}
//
// Thread that waits for keypresses or udp messages and puts them into the ring buffer
//
void *keyin_thread(void *td) {
char buffer[256];
int len;
fd_set rfds;
int termpipe = ((struct threaddata_struct *)td)->termpipe;
int udp_socket = ((struct threaddata_struct *)td)->udp_socket;
for (;;) {
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(termpipe, &rfds);
FD_SET(udp_socket, &rfds);
while (select(udp_socket+1, &rfds, NULL, NULL, NULL) == 0);
if (FD_ISSET(termpipe, &rfds)) {
close(termpipe);
break;
}
if (FD_ISSET(STDIN_FILENO, &rfds)) {
len=read(STDIN_FILENO, buffer, sizeof(buffer));
pthread_mutex_lock(&rblock);
for (int i=0; i<len; i++) queue_to_rb(buffer[i]);
pthread_mutex_unlock(&rblock);
}
if (FD_ISSET(udp_socket, &rfds)) {
len=recv(udp_socket, buffer, sizeof(buffer),0);
pthread_mutex_lock(&rblock);
for (int i=0; i<len; i++) queue_to_rb(buffer[i]);
pthread_mutex_unlock(&rblock);
}
}
pthread_exit(NULL);
}
//
// Traps ctrl-c and send it to the ring buffer for normal processing
//
static void mykill(int arg) {
queue_to_rb(3);
}
//
//
//
void comms_init(void) {
pthread_mutex_init(&rblock, NULL);
pipe(pipes);
td.termpipe=pipes[0];
td.udp_socket=create_udp_socket(2288);
set_stdin_raw(1);
breakcnt=0;
signal(SIGINT, mykill);
pthread_create(&thread, NULL, keyin_thread, &td);
}
//
//
//
void comms_cleanup(void) {
set_stdin_raw(0);
close(pipes[1]);
printf("Closing socket %d\r\n",td.udp_socket);
close(td.udp_socket);
pthread_mutex_destroy(&rblock);
// pthread_exit(NULL);
}
//
// blocking 7 bit read from console
//
int ttygetc(void) {
int ch;
struct timespec ts100us = { .tv_sec = 0, .tv_nsec = 1000000 };
do {
ch=get_from_rb();
if (ch<0) {
if (breakcnt>4) ttybreak();
nanosleep(&ts100us,NULL);
}
} while (ch<0);
return ch;
}
//
// poll for a character from the console
//
int ttypoll(void) {
if (breakcnt>4) ttybreak();
return get_from_rb();
}
//
//
//
void ttygets(char * buf, int len) {
int i = 0;
int ch;
char c;
do {
ch = ttygetc();
if (ch == '\b' || ch==127) {
if (i > 0) {
write(STDOUT_FILENO, "\b \b", 3);
i--;
}
} else if (ch >= ' ') {
if (i < (len - 1)) {
c=ch;
write(STDOUT_FILENO, &c, 1);
buf[i] = ch;
i++;
}
}
} while ((ch != '\r') && (ch != '\n'));
write(STDOUT_FILENO, "\r\n", 2);
if (i < len) {
buf[i] = '\0';
} else {
buf[len - 1] = '\0';
}
}