forked from SciKit-Surgery/ndicapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndicapi_serial_unix.cxx
474 lines (412 loc) · 12.1 KB
/
ndicapi_serial_unix.cxx
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
/*=======================================================================
Copyright (c) 2000-2005 Atamai, Inc.
Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:
1) Redistribution of the source code, in verbatim or modified
form, must retain the above copyright notice, this license,
the following disclaimer, and any notices that refer to this
license and/or the following disclaimer.
2) Redistribution in binary form must include the above copyright
notice, a copy of this license and the following disclaimer
in the documentation or with other materials provided with the
distribution.
3) Modified copies of the source code must be clearly marked as such,
and must not be misrepresented as verbatim copies of the source code.
THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=======================================================================*/
// This file contains the platform-dependent portions of the source code
// that talk to the serial port. All these methods
// are of the form ndiSerialXX().
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
//----------------------------------------------------------------------------
// Some static variables to keep track of which ports are open, so that
// we can restore the comm parameters (baud rate etc) when they are closed.
// Restoring the comm parameters is just part of being a good neighbor.
#define NDI_MAX_SAVE_STATE 4
static int ndi_open_handles[4] = { -1, -1, -1, -1 };
static struct termios ndi_save_termios[4];
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialOpen(const char* device)
{
static struct flock fl = { F_WRLCK, 0, 0, 0 }; /* for file locking */
static struct flock fu = { F_UNLCK, 0, 0, 0 }; /* for file unlocking */
int serial_port;
struct termios t;
int i;
/* port is readable/writable and is (for now) non-blocking */
serial_port = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_port == -1)
{
return -1; /* bail out on error */
}
/* restore blocking now that the port is open (we just didn't want */
/* the port to block while we were trying to open it) */
fcntl(serial_port, F_SETFL, 0);
/* get exclusive lock on the serial port */
/* on many unices, this has no effect for device files */
if (fcntl(serial_port, F_SETLK, &fl))
{
close(serial_port);
return -1;
}
/* get I/O information */
if (tcgetattr(serial_port, &t) == -1)
{
fcntl(serial_port, F_SETLK, &fu);
close(serial_port);
return -1;
}
/* save the serial port state so that it can be restored when
the serial port is closed in ndiSerialClose() */
for (i = 0; i < NDI_MAX_SAVE_STATE; i++)
{
if (ndi_open_handles[i] == serial_port || ndi_open_handles[i] == -1)
{
ndi_open_handles[i] = serial_port;
tcgetattr(serial_port, &ndi_save_termios[i]);
break;
}
}
/* clear everything specific to terminals */
t.c_lflag = 0;
t.c_iflag = 0;
t.c_oflag = 0;
t.c_cc[VMIN] = 0; /* use constant, not interval timeout */
t.c_cc[VTIME] = TIMEOUT_PERIOD / 100; /* wait for 5 secs max */
if (tcsetattr(serial_port, TCSANOW, &t) == -1) /* set I/O information */
{
if (i < NDI_MAX_SAVE_STATE) /* if we saved the state, forget the state */
{
ndi_open_handles[i] = -1;
}
fcntl(serial_port, F_SETLK, &fu);
close(serial_port);
return -1;
}
tcflush(serial_port, TCIOFLUSH); /* flush the buffers for good luck */
return serial_port;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiSerialClose(int serial_port)
{
static struct flock fu = { F_UNLCK, 0, 0, 0 }; /* for file unlocking */
int i;
/* restore the comm port state to from before it was opened */
for (i = 0; i < NDI_MAX_SAVE_STATE; i++)
{
if (ndi_open_handles[i] == serial_port && ndi_open_handles[i] != -1)
{
tcsetattr(serial_port, TCSANOW, &ndi_save_termios[i]);
ndi_open_handles[i] = -1;
break;
}
}
/* release our lock on the serial port */
fcntl(serial_port, F_SETLK, &fu);
close(serial_port);
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialCheckDSR(int serial_port)
{
#if defined(linux) || defined(__linux__)
int bits;
/* get the bits to see if DSR is set (i.e. if device is connected) */
if (ioctl(serial_port, TIOCMGET, &bits) >= 0)
{
return ((bits & TIOCM_DSR) != 0);
}
#endif
/* if called failed for any reason, return success for robustness */
return 1;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialBreak(int serial_port)
{
tcflush(serial_port, TCIOFLUSH); /* clear input/output buffers */
tcsendbreak(serial_port, 0); /* send the break */
return 0;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialFlush(int serial_port, int buffers)
{
int flushtype = TCIOFLUSH;
if (buffers == NDI_IFLUSH)
{
flushtype = TCIFLUSH;
}
else if (buffers == NDI_OFLUSH)
{
flushtype = TCOFLUSH;
}
tcflush(serial_port, flushtype); /* clear input/output buffers */
return 0;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialComm(int serial_port, int baud, const char mode[4], int handshake)
{
struct termios t;
int newbaud;
#if defined(linux) || defined(__linux__)
switch (baud)
{
case 9600:
newbaud = B9600;
break;
case 14400:
return -1;
case 19200:
newbaud = B19200;
break;
case 38400:
newbaud = B38400;
break;
case 57600:
newbaud = B57600;
break;
case 115200:
newbaud = B115200;
break;
case 230400:
newbaud = B230400;
break;
default:
return -1;
}
#elif defined(sgi) && defined(__NEW_MAX_BAUD)
switch (baud)
{
case 9600:
newbaud = 9600;
break;
case 14400:
return -1;
case 19200:
newbaud = 19200;
break;
case 38400:
newbaud = 38400;
break;
case 57600:
newbaud = 57600;
break;
case 115200:
newbaud = 115200;
break;
default:
return -1;
}
#else
switch (baud)
{
case 9600:
newbaud = B9600;
break;
case 14400:
return -1;
case 19200:
newbaud = B19200;
break;
case 38400:
newbaud = B38400;
break;
case 57600:
return -1;
case 115200:
return -1;
default:
return -1;
}
#endif
tcgetattr(serial_port, &t); /* get I/O information */
t.c_cflag &= ~CSIZE; /* clear flags */
#if defined(linux) || defined(__linux__)
t.c_cflag &= ~CBAUD;
t.c_cflag |= newbaud; /* set baud rate */
#elif defined(sgi) && defined(__NEW_MAX_BAUD)
t.c_ospeed = newbaud;
#else
t.c_cflag &= ~CBAUD;
t.c_cflag |= newbaud; /* set baud rate */
#endif
if (mode[0] == '8') /* set data bits */
{
t.c_cflag |= CS8;
}
else if (mode[0] == '7')
{
t.c_cflag |= CS7;
}
else
{
return -1;
}
if (mode[1] == 'N') /* set parity */
{
t.c_cflag &= ~PARENB;
t.c_cflag &= ~PARODD;
}
else if (mode[1] == 'O')
{
t.c_cflag |= PARENB;
t.c_cflag |= PARODD;
}
else if (mode[1] == 'E')
{
t.c_cflag |= PARENB;
t.c_cflag &= ~PARODD;
}
else
{
return -1;
}
if (mode[2] == '1') /* set stop bits */
{
t.c_cflag &= ~CSTOPB;
}
else if (mode[2] == '2')
{
t.c_cflag |= CSTOPB;
}
else
{
return -1;
}
if (handshake)
{
#ifdef sgi
t.c_cflag |= CNEW_RTSCTS; /* enable hardware handshake */
#else
t.c_cflag |= CRTSCTS;
#endif
}
else
{
#ifdef sgi
t.c_cflag &= ~CNEW_RTSCTS; /* turn off hardware handshake */
#else
t.c_cflag &= ~CRTSCTS;
#endif
}
return tcsetattr(serial_port, TCSADRAIN, &t); /* set I/O information */
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialTimeout(int serial_port, int milliseconds)
{
struct termios t;
if (tcgetattr(serial_port, &t) == -1)
{
return -1;
}
t.c_cc[VMIN] = 0; /* use constant, not interval timout */
t.c_cc[VTIME] = milliseconds / 100; /* wait time is in 10ths of a second */
if (tcsetattr(serial_port, TCSANOW, &t) == -1)
{
return -1;
}
return 0;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialWrite(int serial_port, const char* text, int n)
{
int i = 0;
int m;
while (n > 0)
{
if ((m = write(serial_port, &text[i], n)) == -1)
{
if (errno == EAGAIN) /* system canceled us, retry */
{
m = 0;
}
else
{
return -1; /* IO error occurred */
}
}
n -= m; /* n is number of chars left to write */
i += m; /* i is the number of chars written */
}
return i; /* return the number of characters written */
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialRead(int serial_port, char* reply, int numberOfBytesToRead, bool isBinary, int* errorCode)
{
int totalNumberOfBytesRead = 0;
int totalNumberOfBytesToRead = numberOfBytesToRead;
int numberOfBytesRead;
bool binarySizeCalculated = false;
do
{
if ((numberOfBytesRead = read(serial_port, &reply[totalNumberOfBytesRead], numberOfBytesToRead)) == -1)
{
if (errno == EAGAIN) /* canceled, so retry */
{
numberOfBytesRead = 0;
}
else
{
return -1; /* IO error occurred */
}
}
else if (numberOfBytesRead == 0) /* no characters read, must have timed out */
{
return 0;
}
totalNumberOfBytesRead += numberOfBytesRead;
if ((!isBinary && reply[totalNumberOfBytesRead - 1] == '\r') /* done when carriage return received (ASCII) or when ERROR... received (binary)*/
|| (isBinary && strncmp(reply, "ERROR", 5) == 0 && reply[totalNumberOfBytesRead - 1] == '\r'))
{
break;
}
if (isBinary && !binarySizeCalculated && reply[0] == (char)0xc4 && reply[1] == (char)0xa5)
{
// recalculate n based on the reply length (reported from ndi device) and the amount of data received so far
unsigned short size = ((unsigned char)reply[2] | (unsigned char)reply[3] << 8) + 8; // 8 bytes -> 2 for Start Sequence (a5c4), 2 for reply length, 2 for header CRC, 2 for CRC16
totalNumberOfBytesToRead = size;
}
}
while (totalNumberOfBytesRead != totalNumberOfBytesToRead);
return totalNumberOfBytesRead;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialSleep(int serial_port, int milliseconds)
{
#ifdef USE_NANOSLEEP
struct timespec sleep_time, dummy;
sleep_time.tv_sec = milliseconds / 1000;
sleep_time.tv_nsec = (milliseconds - sleep_time.tv_sec * 1000) * 1000000;
nanosleep(&sleep_time, &dummy);
#else /* use usleep instead */
/* some unices like IRIX can't usleep for more than 1 second,
so break usleep into 500 millisecond chunks */
while (milliseconds > 500)
{
usleep(500000);
milliseconds -= 500;
}
usleep(milliseconds * 1000);
#endif
return 0;
}