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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* serial port functions
*
* Author: Chris Toshok <toshok@ximian.com>
*/
#include "map.h"
#include "mph.h"
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#if defined(HAVE_POLL_H)
#include <poll.h>
#elif defined(HAVE_SYS_POLL_H)
#include <sys/poll.h>
#endif
#include <sys/ioctl.h>
/* This is for ASYNC_*, serial_struct on linux */
#if defined(HAVE_LINUX_SERIAL_H)
#include <linux/serial.h>
#endif
#include <glib.h>
/* This is for FIONREAD on solaris */
#if defined(sun)
#include <sys/filio.h>
#endif
/* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
/* IOKit is a private framework in iOS, so exclude there */
#if defined(__APPLE__) && !defined(HOST_IOS) && !defined(HOST_WATCHOS) && !defined(HOST_APPLETVOS)
#define HAVE_IOKIT 1
#endif
#if defined(HAVE_IOKIT)
#include <sys/time.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/serial/ioss.h>
#include <IOKit/IOBSD.h>
#endif
/* This is a copy of System.IO.Ports.Handshake */
typedef enum {
NoneHandshake = 0,
XOnXOff = 1,
RequestToSend = 2,
RequestToSendXOnXOff = 3
} MonoHandshake;
/* This is a copy of System.IO.Ports.Parity */
typedef enum {
NoneParity = 0,
Odd = 1,
Even = 2,
Mark = 3,
Space = 4
} MonoParity;
/* This is a copy of System.IO.Ports.StopBits */
typedef enum {
NoneStopBits = 0,
One = 1,
Two = 2,
OnePointFive = 3
} MonoStopBits;
/* This is a copy of System.IO.Ports.SerialSignal */
typedef enum {
NoneSignal,
Cd = 1, /* Carrier detect */
Cts = 2, /* Clear to send */
Dsr = 4, /* Data set ready */
Dtr = 8, /* Data terminal ready */
Rts = 16 /* Request to send */
} MonoSerialSignal;
/*
* Silence the compiler, we do not need these prototypes to be public, since these are only
* used by P/Invoke
*/
int open_serial (char *devfile);
int close_serial (int unix_fd);
guint32 read_serial (int fd, guchar *buffer, int offset, int count);
int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
int discard_buffer (int fd, gboolean input);
gint32 get_bytes_in_buffer (int fd, gboolean input);
gboolean is_baud_rate_legal (int baud_rate);
int setup_baud_rate (int baud_rate, gboolean *custom_baud_rate);
gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
MonoSerialSignal get_signals (int fd, gint32 *error);
gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
int breakprop (int fd);
gboolean poll_serial (int fd, gint32 *error, int timeout);
void *list_serial_devices (void);
int
open_serial (char *devfile)
{
int fd;
fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
return fd;
}
int
close_serial (int unix_fd)
{
// Linus writes: do not retry close after EINTR
return close (unix_fd);
}
guint32
read_serial (int fd, guchar *buffer, int offset, int count)
{
guint32 n;
n = read (fd, buffer + offset, count);
return (guint32) n;
}
int
write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
{
struct pollfd pinfo;
guint32 n;
pinfo.fd = fd;
pinfo.events = POLLOUT;
pinfo.revents = POLLOUT;
n = count;
while (n > 0)
{
ssize_t t;
if (timeout != 0) {
int c;
while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
;
if (c == -1)
return -1;
}
do {
t = write (fd, buffer + offset, n);
} while (t == -1 && errno == EINTR);
if (t < 0)
return -1;
offset += t;
n -= t;
}
return 0;
}
int
discard_buffer (int fd, gboolean input)
{
return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
}
gint32
get_bytes_in_buffer (int fd, gboolean input)
{
#if defined(__HAIKU__)
/* FIXME: Haiku doesn't support TIOCOUTQ nor FIONREAD on fds */
return -1;
#define TIOCOUTQ 0
#endif
gint32 retval;
if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
return -1;
}
return retval;
}
gboolean
is_baud_rate_legal (int baud_rate)
{
gboolean ignore = FALSE;
return setup_baud_rate (baud_rate, &ignore) != -1;
}
int
setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
{
switch (baud_rate)
{
/*Some values are not defined on OSX and *BSD */
#if defined(B921600)
case 921600:
baud_rate = B921600;
break;
#endif
#if defined(B460800)
case 460800:
baud_rate = B460800;
break;
#endif
case 230400:
baud_rate = B230400;
break;
case 115200:
baud_rate = B115200;
break;
case 57600:
baud_rate = B57600;
break;
case 38400:
baud_rate = B38400;
break;
case 19200:
baud_rate = B19200;
break;
case 9600:
baud_rate = B9600;
break;
case 4800:
baud_rate = B4800;
break;
case 2400:
baud_rate = B2400;
break;
case 1800:
baud_rate = B1800;
break;
case 1200:
baud_rate = B1200;
break;
case 600:
baud_rate = B600;
break;
case 300:
baud_rate = B300;
break;
case 200:
baud_rate = B200;
break;
case 150:
baud_rate = B150;
break;
case 134:
baud_rate = B134;
break;
case 110:
baud_rate = B110;
break;
case 75:
baud_rate = B75;
break;
case 50:
#ifdef B50
baud_rate = B50;
#else
baud_rate = -1;
break;
#endif
case 0:
#ifdef B0
baud_rate = B0;
#else
baud_rate = -1;
#endif
break;
default:
*custom_baud_rate = TRUE;
break;
}
return baud_rate;
}
gboolean
set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
{
struct termios newtio;
gboolean custom_baud_rate = FALSE;
if (tcgetattr (fd, &newtio) == -1)
return FALSE;
newtio.c_cflag |= (CLOCAL | CREAD);
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
newtio.c_oflag &= ~(OPOST);
newtio.c_iflag = IGNBRK;
/* setup baudrate */
baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
/* char lenght */
newtio.c_cflag &= ~CSIZE;
switch (dataBits)
{
case 5:
newtio.c_cflag |= CS5;
break;
case 6:
newtio.c_cflag |= CS6;
break;
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
default:
newtio.c_cflag |= CS8;
break;
}
/* stopbits */
switch (stopBits)
{
case NoneStopBits:
/* Unhandled */
break;
case One: /* One */
/* do nothing, the default is one stop bit */
newtio.c_cflag &= ~CSTOPB;
break;
case Two: /* Two */
newtio.c_cflag |= CSTOPB;
break;
case OnePointFive: /* OnePointFive */
/* XXX unhandled */
break;
}
/* parity */
newtio.c_iflag &= ~(INPCK | ISTRIP );
switch (parity)
{
case NoneParity: /* None */
newtio.c_cflag &= ~(PARENB | PARODD);
break;
case Odd: /* Odd */
newtio.c_cflag |= PARENB | PARODD;
break;
case Even: /* Even */
newtio.c_cflag &= ~(PARODD);
newtio.c_cflag |= (PARENB);
break;
case Mark: /* Mark */
/* XXX unhandled */
break;
case Space: /* Space */
/* XXX unhandled */
break;
}
newtio.c_iflag &= ~(IXOFF | IXON);
#ifdef CRTSCTS
newtio.c_cflag &= ~CRTSCTS;
#endif /* def CRTSCTS */
switch (handshake)
{
case NoneHandshake: /* None */
/* do nothing */
break;
case RequestToSend: /* RequestToSend (RTS) */
#ifdef CRTSCTS
newtio.c_cflag |= CRTSCTS;
#endif /* def CRTSCTS */
break;
case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
#ifdef CRTSCTS
newtio.c_cflag |= CRTSCTS;
#endif /* def CRTSCTS */
/* fall through */
case XOnXOff: /* XOnXOff */
newtio.c_iflag |= IXOFF | IXON;
break;
}
if (custom_baud_rate == FALSE) {
if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
return FALSE;
} else {
#if __linux__ || defined(HAVE_IOKIT)
/* On Linux to set a custom baud rate, we must set the
* "standard" baud_rate to 38400. On Apple we set it purely
* so that tcsetattr has something to use (and report back later), but
* the Apple specific API is still opaque to these APIs, see:
* https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
*/
if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
return FALSE;
#endif
}
if (tcsetattr (fd, TCSANOW, &newtio) < 0)
return FALSE;
if (custom_baud_rate == TRUE){
#if defined(HAVE_LINUX_SERIAL_H)
struct serial_struct ser;
if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
{
return FALSE;
}
ser.custom_divisor = ser.baud_base / baud_rate;
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST;
if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
{
return FALSE;
}
#elif defined(HAVE_IOKIT)
speed_t speed = baud_rate;
if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
return FALSE;
#else
/* Don't know how to set custom baud rate on this platform. */
return FALSE;
#endif
}
return TRUE;
}
static gint32
get_signal_code (MonoSerialSignal signal)
{
switch (signal) {
case Cd:
return TIOCM_CAR;
case Cts:
return TIOCM_CTS;
case Dsr:
return TIOCM_DSR;
case Dtr:
return TIOCM_DTR;
case Rts:
return TIOCM_RTS;
default:
return 0;
}
/* Not reached */
return 0;
}
static MonoSerialSignal
get_mono_signal_codes (int signals)
{
MonoSerialSignal retval = NoneSignal;
if ((signals & TIOCM_CAR) != 0)
retval |= Cd;
if ((signals & TIOCM_CTS) != 0)
retval |= Cts;
if ((signals & TIOCM_DSR) != 0)
retval |= Dsr;
if ((signals & TIOCM_DTR) != 0)
retval |= Dtr;
if ((signals & TIOCM_RTS) != 0)
retval |= Rts;
return retval;
}
MonoSerialSignal
get_signals (int fd, gint32 *error)
{
int signals;
*error = 0;
if (ioctl (fd, TIOCMGET, &signals) == -1) {
*error = -1;
return NoneSignal;
}
return get_mono_signal_codes (signals);
}
gint32
set_signal (int fd, MonoSerialSignal signal, gboolean value)
{
int signals, expected, activated;
expected = get_signal_code (signal);
if (ioctl (fd, TIOCMGET, &signals) == -1)
return -1;
activated = (signals & expected) != 0;
if (activated == value) /* Already set */
return 1;
if (value)
signals |= expected;
else
signals &= ~expected;
if (ioctl (fd, TIOCMSET, &signals) == -1)
return -1;
return 1;
}
int
breakprop (int fd)
{
return tcsendbreak (fd, 0);
}
gboolean
poll_serial (int fd, gint32 *error, int timeout)
{
struct pollfd pinfo;
*error = 0;
pinfo.fd = fd;
pinfo.events = POLLIN;
pinfo.revents = 0;
while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
/* EINTR is an OK condition, we should not throw in the upper layer an IOException */
if (errno != EINTR){
*error = -1;
return FALSE;
}
}
return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
}
/*
* mono internals should not be used here.
* this serial stuff needs to be implemented with icalls.
* make this at least compile until the code is moved elsewhere
* defined(linux) is wrong, too
*/
void*
list_serial_devices (void)
{
return NULL;
}
#if 0
MonoArray *
list_serial_devices (void)
{
MonoArray *array;
#if defined(linux)
/* Linux serial files are of the form ttyS[0-9]+ */
GSList *l, *list = NULL;
GDir* dir = g_dir_open ("/dev", 0, NULL);
const char *filename;
int i = 0;
while ((filename = g_dir_read_name (dir))) {
if (filename) {
if (!strncmp (filename, "ttyS", 4))
list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
}
}
g_dir_close (dir);
array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
for (l = list; l; l = l->next) {
mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
g_free (l->data);
}
g_slist_free (list);
#else
#warning "list_serial_devices isn't ported to this OS"
#endif
return array;
}
#endif
|