[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / TTY_IO.cpp
blob9918ec0dceb2310c32a447c51183a335ef1adaf7
1 // $Id: TTY_IO.cpp 82271 2008-07-09 09:23:03Z olli $
3 #include "ace/TTY_IO.h"
4 #include "ace/OS_NS_errno.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_strings.h"
8 #if defined (ACE_HAS_TERMIOS)
9 # include "ace/os_include/os_termios.h"
10 #elif defined (ACE_HAS_TERMIO)
11 # include <termio.h>
12 #endif
14 ACE_RCSID (ace,
15 TTY_IO,
16 "$Id: TTY_IO.cpp 82271 2008-07-09 09:23:03Z olli $")
18 namespace
20 const char ACE_TTY_IO_NONE[] = "none";
21 #if defined (ACE_HAS_TERMIOS) || defined (ACE_HAS_TERMIO) || defined (ACE_WIN32)
22 const char ACE_TTY_IO_ODD[] = "odd";
23 const char ACE_TTY_IO_EVEN[] = "even";
24 #endif
25 #if defined (ACE_WIN32)
26 const char ACE_TTY_IO_MARK[] = "mark";
27 const char ACE_TTY_IO_SPACE[] = "space";
28 #endif /* ACE_WIN32 */
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
33 ACE_TTY_IO::Serial_Params::Serial_Params (void)
35 baudrate = 9600;
36 xonlim = 0;
37 xofflim = 0;
38 readmincharacters = 0;
39 readtimeoutmsec = 10000;
40 paritymode = ACE_TTY_IO_NONE;
41 ctsenb = false;
42 rtsenb = 0;
43 xinenb = false;
44 xoutenb = false;
45 modem = false;
46 rcvenb = true;
47 dsrenb = false;
48 dtrdisable = false;
49 databits = 8;
50 stopbits = 1;
53 // Interface for reading/writing serial device parameters
55 int ACE_TTY_IO::control (Control_Mode cmd, Serial_Params *arg) const
57 #if defined (ACE_HAS_TERMIOS) || defined (ACE_HAS_TERMIO)
59 #if defined (ACE_HAS_TERMIOS)
60 struct termios devpar;
61 speed_t newbaudrate = 0;
62 if (tcgetattr (get_handle () , &devpar) == -1)
63 #elif defined (TCGETS)
64 struct termios devpar;
65 unsigned int newbaudrate = 0;
66 if (this->ACE_IO_SAP::control (TCGETS, static_cast<void*>(&devpar)) == -1)
67 #elif defined (TCGETA)
68 struct termio devpar;
69 unsigned int newbaudrate = 0;
70 if (this->ACE_IO_SAP::control (TCGETA, static_cast<void*>(&devpar)) == -1)
71 #else
72 errno = ENOSYS;
73 #endif /* ACE_HAS_TERMIOS */
74 return -1;
76 switch (cmd)
78 case SETPARAMS:
79 switch (arg->baudrate)
81 #if defined (B0)
82 case 0: newbaudrate = B0; break;
83 #endif /* B0 */
84 #if defined (B50)
85 case 50: newbaudrate = B50; break;
86 #endif /* B50 */
87 #if defined (B75)
88 case 75: newbaudrate = B75; break;
89 #endif /* B75 */
90 #if defined (B110)
91 case 110: newbaudrate = B110; break;
92 #endif /* B110 */
93 #if defined (B134)
94 case 134: newbaudrate = B134; break;
95 #endif /* B134 */
96 #if defined (B150)
97 case 150: newbaudrate = B150; break;
98 #endif /* B150 */
99 #if defined (B200)
100 case 200: newbaudrate = B200; break;
101 #endif /* B200 */
102 #if defined (B300)
103 case 300: newbaudrate = B300; break;
104 #endif /* B300 */
105 #if defined (B600)
106 case 600: newbaudrate = B600; break;
107 #endif /* B600 */
108 #if defined (B1200)
109 case 1200: newbaudrate = B1200; break;
110 #endif /* B1200 */
111 #if defined (B1800)
112 case 1800: newbaudrate = B1800; break;
113 #endif /* B1800 */
114 #if defined (B2400)
115 case 2400: newbaudrate = B2400; break;
116 #endif /* B2400 */
117 #if defined (B4800)
118 case 4800: newbaudrate = B4800; break;
119 #endif /* B4800 */
120 #if defined (B9600)
121 case 9600: newbaudrate = B9600; break;
122 #endif /* B9600 */
123 #if defined (B19200)
124 case 19200: newbaudrate = B19200; break;
125 #endif /* B19200 */
126 #if defined (B38400)
127 case 38400: newbaudrate = B38400; break;
128 #endif /* B38400 */
129 #if defined (B56000)
130 case 56000: newbaudrate = B56000; break;
131 #endif /* B56000 */
132 #if defined (B57600)
133 case 57600: newbaudrate = B57600; break;
134 #endif /* B57600 */
135 #if defined (B76800)
136 case 76800: newbaudrate = B76800; break;
137 #endif /* B76800 */
138 #if defined (B115200)
139 case 115200: newbaudrate = B115200; break;
140 #endif /* B115200 */
141 #if defined (B128000)
142 case 128000: newbaudrate = B128000; break;
143 #endif /* B128000 */
144 #if defined (B153600)
145 case 153600: newbaudrate = B153600; break;
146 #endif /* B153600 */
147 #if defined (B230400)
148 case 230400: newbaudrate = B230400; break;
149 #endif /* B230400 */
150 #if defined (B307200)
151 case 307200: newbaudrate = B307200; break;
152 #endif /* B307200 */
153 #if defined (B256000)
154 case 256000: newbaudrate = B256000; break;
155 #endif /* B256000 */
156 #if defined (B460800)
157 case 460800: newbaudrate = B460800; break;
158 #endif /* B460800 */
159 #if defined (B500000)
160 case 500000: newbaudrate = B500000; break;
161 #endif /* B500000 */
162 #if defined (B576000)
163 case 576000: newbaudrate = B576000; break;
164 #endif /* B576000 */
165 #if defined (B921600)
166 case 921600: newbaudrate = B921600; break;
167 #endif /* B921600 */
168 #if defined (B1000000)
169 case 1000000: newbaudrate = B1000000; break;
170 #endif /* B1000000 */
171 #if defined (B1152000)
172 case 1152000: newbaudrate = B1152000; break;
173 #endif /* B1152000 */
174 #if defined (B1500000)
175 case 1500000: newbaudrate = B1500000; break;
176 #endif /* B1500000 */
177 #if defined (B2000000)
178 case 2000000: newbaudrate = B2000000; break;
179 #endif /* B2000000 */
180 #if defined (B2500000)
181 case 2500000: newbaudrate = B2500000; break;
182 #endif /* B2500000 */
183 #if defined (B3000000)
184 case 3000000: newbaudrate = B3000000; break;
185 #endif /* B3000000 */
186 #if defined (B3500000)
187 case 3500000: newbaudrate = B3500000; break;
188 #endif /* B3500000 */
189 #if defined (B4000000)
190 case 4000000: newbaudrate = B4000000; break;
191 #endif /* B4000000 */
192 default:
193 return -1;
196 #if defined (ACE_HAS_TERMIOS)
197 // Can you really have different input and output baud rates?!
198 if (cfsetospeed (&devpar, newbaudrate) == -1)
199 return -1;
200 if (cfsetispeed (&devpar, newbaudrate) == -1)
201 return -1;
202 #else
203 devpar.c_cflag &= ~CBAUD;
204 # if defined (CBAUDEX)
205 devpar.c_cflag &= ~CBAUDEX;
206 # endif /* CBAUDEX */
207 devpar.c_cflag |= newbaudrate;
208 #endif /* ACE_HAS_TERMIOS */
210 devpar.c_cflag &= ~CSIZE;
211 switch (arg->databits)
213 case 5:
214 devpar.c_cflag |= CS5;
215 break;
216 case 6:
217 devpar.c_cflag |= CS6;
218 break;
219 case 7:
220 devpar.c_cflag |= CS7;
221 break;
222 case 8:
223 devpar.c_cflag |= CS8;
224 break;
225 default:
226 return -1;
229 switch (arg->stopbits)
231 case 1:
232 devpar.c_cflag &= ~CSTOPB;
233 break;
234 case 2:
235 devpar.c_cflag |= CSTOPB;
236 break;
237 default:
238 return -1;
241 if (arg->paritymode)
243 if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_ODD) == 0)
245 devpar.c_cflag |= PARENB;
246 devpar.c_cflag |= PARODD;
248 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_EVEN) == 0)
250 devpar.c_cflag |= PARENB;
251 devpar.c_cflag &= ~PARODD;
253 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_NONE) == 0)
254 devpar.c_cflag &= ~PARENB;
255 else
256 return -1;
258 else
260 devpar.c_cflag &= ~PARENB;
263 #if defined (CNEW_RTSCTS)
264 if ((arg->ctsenb) || (arg->rtsenb)) // Enable RTS/CTS protocol
265 devpar.c_cflag |= CNEW_RTSCTS;
266 else
267 devpar.c_cflag &= ~CNEW_RTSCTS;
268 #elif defined (CRTSCTS)
269 if ((arg->ctsenb) || (arg->rtsenb)) // Enable RTS/CTS protocol
270 devpar.c_cflag |= CRTSCTS;
271 else
272 devpar.c_cflag &= ~CRTSCTS;
273 #endif /* NEW_RTSCTS || CRTSCTS */
275 #if defined (CREAD)
276 // Enable/disable receiver
277 if (arg->rcvenb)
278 devpar.c_cflag |= CREAD;
279 else
280 devpar.c_cflag &= ~CREAD;
281 #endif /* CREAD */
283 #if defined (HUPCL)
284 // Cause DTR to drop after port close.
285 devpar.c_cflag |= HUPCL;
286 #endif /* HUPCL */
288 #if defined (CLOCAL)
289 // If device is not a modem set to local device.
290 if (arg->modem)
291 devpar.c_cflag &= ~CLOCAL;
292 else
293 devpar.c_cflag |= CLOCAL;
294 #endif /* CLOCAL */
296 devpar.c_iflag = IGNPAR | INPCK;
297 if (arg->databits < 8)
298 devpar.c_iflag |= ISTRIP;
300 #if defined (IGNBRK)
301 // If device is not a modem set to ignore break points
302 if(arg->modem)
303 devpar.c_iflag &= ~IGNBRK;
304 else
305 devpar.c_iflag |= IGNBRK;
306 #endif /* IGNBRK */
308 #if defined (IXOFF)
309 // Enable/disable software flow control on input
310 if (arg->xinenb)
311 devpar.c_iflag |= IXOFF;
312 else
313 devpar.c_iflag &= ~IXOFF;
314 #endif /* IXOFF */
316 #if defined (IXON)
317 // Enable/disable software flow control on output
318 if (arg->xoutenb)
319 devpar.c_iflag |= IXON;
320 else
321 devpar.c_iflag &= ~IXON;
322 #endif /* IXON */
324 #if defined (ICANON)
325 // Enable noncanonical input processing mode
326 devpar.c_lflag &= ~ICANON;
327 #endif /* ICANON */
329 #if defined (ECHO)
330 // Disable echoing of input characters
331 devpar.c_lflag &= ~ECHO;
332 #endif /* ECHO */
334 #if defined (ECHOE)
335 // Disable echoing erase chareacter as BS-SP-BS
336 devpar.c_lflag &= ~ECHOE;
337 #endif /* ECHOE */
339 #if defined (ISIG)
340 // Disable SIGINTR, SIGSUSP, SIGDSUSP and SIGQUIT signals
341 devpar.c_lflag &= ~ISIG;
342 #endif /* ISIG */
344 #if defined (OPOST)
345 // Disable post-processing of output data
346 devpar.c_oflag &= ~OPOST;
347 #endif /* OPOST */
349 if (arg->readtimeoutmsec < 0)
351 // Settings for infinite timeout.
352 devpar.c_cc[VTIME] = 0;
353 // In case of infinite timeout [VMIN] must be at least 1.
354 if (arg->readmincharacters > UCHAR_MAX)
355 devpar.c_cc[VMIN] = UCHAR_MAX;
356 else if (arg->readmincharacters < 1)
357 devpar.c_cc[VMIN] = 1;
358 else
359 devpar.c_cc[VMIN] = static_cast<unsigned char>(arg->readmincharacters);
361 else
363 devpar.c_cc[VTIME] = static_cast<unsigned char>(arg->readtimeoutmsec / 100);
365 if (arg->readmincharacters > UCHAR_MAX)
366 devpar.c_cc[VMIN] = UCHAR_MAX;
367 else if (arg->readmincharacters < 1)
368 devpar.c_cc[VMIN] = 0;
369 else
370 devpar.c_cc[VMIN] = static_cast<unsigned char>(arg->readmincharacters);
373 #if defined (TIOCMGET)
374 int status;
375 this->ACE_IO_SAP::control (TIOCMGET, &status);
377 if (arg->dtrdisable)
378 status &= ~TIOCM_DTR;
379 else
380 status |= TIOCM_DTR;
382 this->ACE_IO_SAP::control (TIOCMSET, &status);
383 #endif /* definded (TIOCMGET) */
385 #if defined (ACE_HAS_TERMIOS)
386 return tcsetattr (get_handle (), TCSANOW, &devpar);
387 #elif defined (TCSETS)
388 return this->ACE_IO_SAP::control (TCSETS, static_cast<void*>(&devpar));
389 #elif defined (TCSETA)
390 return this->ACE_IO_SAP::control (TCSETA, static_cast<void*>(&devpar));
391 #else
392 errno = ENOSYS;
393 return -1;
394 #endif /* ACE_HAS_TERMIOS */
396 case GETPARAMS:
397 return -1; // Not yet implemented.
398 default:
399 return -1; // Wrong cmd.
401 #elif defined (ACE_WIN32)
402 switch (cmd)
404 case SETPARAMS:
405 DCB dcb;
406 dcb.DCBlength = sizeof dcb;
407 if (!::GetCommState (this->get_handle (), &dcb))
409 ACE_OS::set_errno_to_last_error ();
410 return -1;
413 dcb.BaudRate = arg->baudrate;
415 switch (arg->databits)
417 case 4:
418 case 5:
419 case 6:
420 case 7:
421 case 8:
422 dcb.ByteSize = arg->databits;
423 break;
424 default:
425 return -1;
428 switch (arg->stopbits)
430 case 1:
431 dcb.StopBits = ONESTOPBIT;
432 break;
433 case 2:
434 dcb.StopBits = TWOSTOPBITS;
435 break;
436 default:
437 return -1;
440 if (arg->paritymode)
442 dcb.fParity = TRUE;
443 if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_ODD) == 0)
444 dcb.Parity = ODDPARITY;
445 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_EVEN) == 0)
446 dcb.Parity = EVENPARITY;
447 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_NONE) == 0)
448 dcb.Parity = NOPARITY;
449 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_MARK) == 0)
450 dcb.Parity = MARKPARITY;
451 else if (ACE_OS::strcasecmp (arg->paritymode, ACE_TTY_IO_SPACE) == 0)
452 dcb.Parity = SPACEPARITY;
453 else
454 return -1;
456 else
458 dcb.fParity = FALSE;
459 dcb.Parity = NOPARITY;
462 // Enable/disable RTS protocol.
463 switch (arg->rtsenb)
465 case 1:
466 dcb.fRtsControl = RTS_CONTROL_ENABLE;
467 break;
468 case 2:
469 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
470 break;
471 case 3:
472 dcb.fRtsControl = RTS_CONTROL_TOGGLE;
473 break;
474 default:
475 dcb.fRtsControl = RTS_CONTROL_DISABLE;
478 // Enable/disable CTS protocol.
479 if (arg->ctsenb)
480 dcb.fOutxCtsFlow = TRUE;
481 else
482 dcb.fOutxCtsFlow = FALSE;
484 // Enable/disable DSR protocol.
485 if (arg->dsrenb)
486 dcb.fOutxDsrFlow = TRUE;
487 else
488 dcb.fOutxDsrFlow = FALSE;
490 // Disable/enable DTR protocol
491 if (arg->dtrdisable)
492 dcb.fDtrControl = DTR_CONTROL_DISABLE;
493 else
494 dcb.fDtrControl = DTR_CONTROL_ENABLE;
496 // Enable/disable software flow control on input
497 if (arg->xinenb)
498 dcb.fInX = TRUE;
499 else
500 dcb.fInX = FALSE;
502 // Enable/disable software flow control on output
503 if (arg->xoutenb)
504 dcb.fOutX = TRUE;
505 else
506 dcb.fOutX = FALSE;
508 // Always set limits unless set to negative to use default.
509 if (arg->xonlim >= 0)
510 dcb.XonLim = static_cast<WORD>(arg->xonlim);
511 if (arg->xofflim >= 0)
512 dcb.XoffLim = static_cast<WORD>(arg->xofflim);
514 dcb.fAbortOnError = FALSE;
515 dcb.fErrorChar = FALSE;
516 dcb.fNull = FALSE;
517 dcb.fBinary = TRUE;
519 if (!::SetCommState (this->get_handle (), &dcb))
521 ACE_OS::set_errno_to_last_error ();
522 return -1;
525 COMMTIMEOUTS timeouts;
526 if (!::GetCommTimeouts (this->get_handle(), &timeouts))
528 ACE_OS::set_errno_to_last_error ();
529 return -1;
532 if (arg->readtimeoutmsec < 0)
534 // Settings for infinite timeout.
535 timeouts.ReadIntervalTimeout = 0;
536 timeouts.ReadTotalTimeoutMultiplier = 0;
537 timeouts.ReadTotalTimeoutConstant = 0;
539 else if (arg->readtimeoutmsec == 0)
541 // Return immediately if no data in the input buffer.
542 timeouts.ReadIntervalTimeout = MAXDWORD;
543 timeouts.ReadTotalTimeoutMultiplier = 0;
544 timeouts.ReadTotalTimeoutConstant = 0;
546 else
548 // Wait for specified timeout for char to arrive before returning.
549 timeouts.ReadIntervalTimeout = MAXDWORD;
550 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
551 timeouts.ReadTotalTimeoutConstant = arg->readtimeoutmsec;
554 if (!::SetCommTimeouts (this->get_handle (), &timeouts))
556 ACE_OS::set_errno_to_last_error ();
557 return -1;
560 return 0;
562 case GETPARAMS:
563 ACE_NOTSUP_RETURN (-1); // Not yet implemented.
564 default:
565 return -1; // Wrong cmd.
567 } // arg switch
568 #else
569 ACE_UNUSED_ARG (cmd);
570 ACE_UNUSED_ARG (arg);
571 ACE_NOTSUP_RETURN (-1);
572 #endif /* ACE_HAS_TERMIOS || ACE_HAS_TERMIO */
575 #if defined (ACE_NEEDS_DEV_IO_CONVERSION)
576 ACE_TTY_IO::operator ACE_DEV_IO &()
578 return static_cast<ACE_DEV_IO &>(*this);
580 #endif /* ACE_NEEDS_DEV_IO_CONVERSION */
582 ACE_END_VERSIONED_NAMESPACE_DECL