forked from gurcei/m65dbg
-
Notifications
You must be signed in to change notification settings - Fork 16
/
serial.c
367 lines (313 loc) · 9.33 KB
/
serial.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* vim: set expandtab shiftwidth=2 tabstop=2: */
// serial code routine borrowed from:
// http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
// Note1: enable unix domain socket support is untested on Windows/Cygwin so
// it's better to leave commented out by default ...
// -------------------------------------------------
// Note2 (GI): I'm leaving this always enabled now, as I've gotten
// unix-sockets to work in winxp+cygwin
#define SUPPORT_UNIX_DOMAIN_SOCKET
#define _BSD_SOURCE _BSD_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#ifdef SUPPORT_UNIX_DOMAIN_SOCKET
#include <sys/un.h>
#include <sys/socket.h>
#endif
#include <netdb.h>
#include <arpa/inet.h>
#include "serial.h"
#ifdef __APPLE__
#include <sys/ioctl.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/serial/ioss.h>
#include <IOKit/IOBSD.h>
static const int B1000000 = 1000000;
static const int B1500000 = 1500000;
static const int B2000000 = 2000000;
static const int B4000000 = 4000000;
#endif
// borrow this from commands.c
extern int peek(unsigned int address);
int xemu_flag = 0;
#define error_message printf
#ifdef WINDOWS
PORT_TYPE fd=INVALID_HANDLE_VALUE;
#else
PORT_TYPE fd=-1;
#endif
bool unix_socket_flag = false;
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tcgetattr\n", errno);
return -1;
}
#ifdef __APPLE__
speed_t speed_apple = speed;
fprintf(stderr,"Setting serial speed to %d bps using OSX method.\n",speed);
if (ioctl(fd, IOSSIOSPEED, &speed_apple) == -1) {
perror("Failed to set output baud rate using IOSSIOSPEED");
}
if (tcgetattr(fd, &tty)) perror("Failed to get terminal parameters");
cfmakeraw(&tty);
if (tcsetattr(fd, TCSANOW, &tty)) perror("Failed to set OSX terminal parameters");
#else
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
error_message ("error %d from tcsetattr\n", errno);
return -1;
}
#endif
return 0;
}
void set_blocking_serial (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tggetattr\n", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 2 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
error_message ("error %d setting term attributes\n", errno);
}
/*
borrowed from: https://www.binarytides.com/hostname-to-ip-address-c-sockets-linux/
Get ip from domain name
*/
int hostname_to_ip(char * hostname , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
return 1;
}
/**
* opens the desired serial port at the required 2000000 bps, or to a unix-domain socket
*
* portname = the desired "/dev/ttyS*" device portname to use
* "unix#..path.." defines a unix-domain named stream socket to connect to (emulator)
*/
bool serialOpen(char* portname)
{
if (!strncasecmp(portname, "tcp", 3))
{
char hostname[128] = "localhost";
int port = 4510; // assume a default port of 4510
if (portname[3] == '#') // did user provide a hostname and port number?
{
sscanf(&portname[4], "%[^:]:%d", hostname, &port);
}
else if (portname[3] == '\\' && portname[4] == '#')
{
sscanf(&portname[5], "%[^:]:%d", hostname, &port);
}
struct sockaddr_in sock_st;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
error_message("error %d creating tcp/ip socket: %s\n", errno, strerror (errno));
return false;
}
char ip[100];
hostname_to_ip(hostname , ip);
printf("%s resolved to %s\n" , hostname , ip);
sock_st.sin_addr.s_addr = inet_addr(ip);
sock_st.sin_family = AF_INET;
sock_st.sin_port = htons(port);
if (connect(fd, (struct sockaddr*)&sock_st, sizeof(sock_st)) < 0)
{
error_message("error %d connecting to tcp/ip socket %s:%d: %s\n", errno, hostname, port, strerror (errno));
close(fd);
return false;
}
}
else if (!strncasecmp(portname, "unix#", 5) ||
!strncasecmp(portname, "unix\\#", 6)) {
#ifdef SUPPORT_UNIX_DOMAIN_SOCKET
struct sockaddr_un sock_st;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
error_message("error %d creating UNIX-domain socket: %s\n", errno, strerror (errno));
return false;
}
unix_socket_flag = true;
sock_st.sun_family = AF_UNIX;
int hashloc = strchr(portname, '#') - portname;
strcpy(sock_st.sun_path, portname + hashloc + 1);
if (connect(fd, (struct sockaddr*)&sock_st, sizeof(struct sockaddr_un))) {
error_message("error %d connecting to UNIX-domain socket %s: %s\n", errno, portname + 5, strerror (errno));
close(fd);
return false;
}
//set_blocking_std (fd, 0); // set no blocking
#else
error_message("unix domain socket is not compiled in this time!\n");
return false;
#endif
} else {
fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
error_message ("error %d opening %s: %s\n", errno, portname, strerror (errno));
return false;
}
set_interface_attribs (fd, B2000000, 0); // set speed to 2,000,000 bps, 8n1 (no parity)
set_blocking_serial (fd, 0); // set no blocking
// borrowed this line from m65.c - set_serial_speed(), as it seems to set the
// non-blocking behaviour properly
//fcntl(fd,F_SETFL,fcntl(fd, F_GETFL, NULL)|O_NONBLOCK);
}
xemu_flag = peek(0xffd360f) & 0x20 ? 0 : 1;
if (xemu_flag)
printf("Xemu detected!\n");
else
printf("Native hardware detected!\n");
return true;
}
void serialBaud(bool fastmode)
{
#ifndef __CYGWIN__
if (fastmode)
set_interface_attribs(fd, B4000000, 0);
else
set_interface_attribs(fd, B2000000, 0);
#endif
}
/**
* closes the opened serial port
*/
bool serialClose(void)
{
if (fd >= 0)
{
close(fd);
fd = 0;
return true;
}
return false;
}
void serialFlush(void)
{
// http://stackoverflow.com/questions/13013387/clearing-the-serial-ports-buffer
// sleep(2); //required to make flush work, for some reason (for USB serial ports?)
// tcflush(fd,TCIOFLUSH);
// I'll now try a 'manual' flush, to see if that works for Ralph's mac and my mac...
int bytes_available = 0;
static char tmp[16384];
#ifdef FIONREAD
ioctl(fd, FIONREAD, &bytes_available);
#else
ioctl(fd, TIOCINQ, &bytes_available);
#endif
if (bytes_available > 0)
read(fd, tmp, bytes_available);
}
/**
* writes a string to the serial port
*/
void serialWrite(char* string)
{
serialFlush();
int i = strlen(string);
// do we need to add a carriage return to the end?
if (string[i-1] != '\n')
{
strcat(string, "\n");
i++;
}
write (fd, string, i); // send string
// add a pause for xemu
if (xemu_flag)
usleep(10000);
}
/**
* reads serial data and feeds it into the provided buffer. The routine will read up
* until the next '.' prompt. It should also crop out the first line, which is just
* and echo of the command.
*
* returns:
* true = read till the next '.' prompt.
* false = could not read till next '.' prompt (eg, buffer was filled)
*/
bool serialRead(char* buf, int bufsize)
{
char* ptr = buf;
char* secondline = NULL;
bool foundLF = false;
// wait a millisecond first, to assure all of buffer has arrived
usleep(1000);
while (ptr - buf < bufsize)
{
int n = read (fd, ptr, bufsize); // read up to 'bufsize' characters if ready to read
if (n == -1)
return false;
// check for "." prompt
for (int k = 0; k < n; k++)
{
if ( *(ptr+k) == '\n' )
{
foundLF = true;
if (!secondline)
secondline = ptr+k+1;
}
else if (foundLF && *(ptr+k) == '.')
{
*(ptr+k) = '\0';
int len = strlen(secondline) + 1;
for (int z = 0; z < len; z++)
*(buf+z) = *(secondline+z);
return true;
}
else
foundLF = false;
}
ptr += n;
}
return false;
}