2 // impl/serial_port_base.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_IMPL_SERIAL_PORT_BASE_IPP
13 #define BOOST_ASIO_IMPL_SERIAL_PORT_BASE_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
24 #include <boost/asio/error.hpp>
25 #include <boost/asio/serial_port_base.hpp>
26 #include <boost/asio/detail/throw_exception.hpp>
28 #if defined(GENERATING_DOCUMENTATION)
29 # define BOOST_ASIO_OPTION_STORAGE implementation_defined
30 #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
31 # define BOOST_ASIO_OPTION_STORAGE DCB
33 # define BOOST_ASIO_OPTION_STORAGE termios
36 #include <boost/asio/detail/push_options.hpp>
41 BOOST_ASIO_SYNC_OP_VOID serial_port_base::baud_rate::store(
42 BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
44 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
45 storage.BaudRate = value_;
50 // Do POSIX-specified rates first.
51 case 0: baud = B0; break;
52 case 50: baud = B50; break;
53 case 75: baud = B75; break;
54 case 110: baud = B110; break;
55 case 134: baud = B134; break;
56 case 150: baud = B150; break;
57 case 200: baud = B200; break;
58 case 300: baud = B300; break;
59 case 600: baud = B600; break;
60 case 1200: baud = B1200; break;
61 case 1800: baud = B1800; break;
62 case 2400: baud = B2400; break;
63 case 4800: baud = B4800; break;
64 case 9600: baud = B9600; break;
65 case 19200: baud = B19200; break;
66 case 38400: baud = B38400; break;
67 // And now the extended ones conditionally.
69 case 7200: baud = B7200; break;
72 case 14400: baud = B14400; break;
75 case 57600: baud = B57600; break;
78 case 115200: baud = B115200; break;
81 case 230400: baud = B230400; break;
84 case 460800: baud = B460800; break;
87 case 500000: baud = B500000; break;
90 case 576000: baud = B576000; break;
93 case 921600: baud = B921600; break;
96 case 1000000: baud = B1000000; break;
99 case 1152000: baud = B1152000; break;
102 case 2000000: baud = B2000000; break;
105 case 3000000: baud = B3000000; break;
108 case 3500000: baud = B3500000; break;
111 case 4000000: baud = B4000000; break;
114 ec = boost::asio::error::invalid_argument;
115 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
117 # if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
118 ::cfsetspeed(&storage, baud);
120 ::cfsetispeed(&storage, baud);
121 ::cfsetospeed(&storage, baud);
124 ec = boost::system::error_code();
125 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
128 BOOST_ASIO_SYNC_OP_VOID serial_port_base::baud_rate::load(
129 const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
131 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
132 value_ = storage.BaudRate;
134 speed_t baud = ::cfgetospeed(&storage);
137 // First do those specified by POSIX.
138 case B0: value_ = 0; break;
139 case B50: value_ = 50; break;
140 case B75: value_ = 75; break;
141 case B110: value_ = 110; break;
142 case B134: value_ = 134; break;
143 case B150: value_ = 150; break;
144 case B200: value_ = 200; break;
145 case B300: value_ = 300; break;
146 case B600: value_ = 600; break;
147 case B1200: value_ = 1200; break;
148 case B1800: value_ = 1800; break;
149 case B2400: value_ = 2400; break;
150 case B4800: value_ = 4800; break;
151 case B9600: value_ = 9600; break;
152 case B19200: value_ = 19200; break;
153 case B38400: value_ = 38400; break;
154 // Now conditionally handle a bunch of extended rates.
156 case B7200: value_ = 7200; break;
159 case B14400: value_ = 14400; break;
162 case B57600: value_ = 57600; break;
165 case B115200: value_ = 115200; break;
168 case B230400: value_ = 230400; break;
171 case B460800: value_ = 460800; break;
174 case B500000: value_ = 500000; break;
177 case B576000: value_ = 576000; break;
180 case B921600: value_ = 921600; break;
183 case B1000000: value_ = 1000000; break;
186 case B1152000: value_ = 1152000; break;
189 case B2000000: value_ = 2000000; break;
192 case B3000000: value_ = 3000000; break;
195 case B3500000: value_ = 3500000; break;
198 case B4000000: value_ = 4000000; break;
202 ec = boost::asio::error::invalid_argument;
203 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
206 ec = boost::system::error_code();
207 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
210 serial_port_base::flow_control::flow_control(
211 serial_port_base::flow_control::type t)
214 if (t != none && t != software && t != hardware)
216 std::out_of_range ex("invalid flow_control value");
217 boost::asio::detail::throw_exception(ex);
221 BOOST_ASIO_SYNC_OP_VOID serial_port_base::flow_control::store(
222 BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
224 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
225 storage.fOutxCtsFlow = FALSE;
226 storage.fOutxDsrFlow = FALSE;
227 storage.fTXContinueOnXoff = TRUE;
228 storage.fDtrControl = DTR_CONTROL_ENABLE;
229 storage.fDsrSensitivity = FALSE;
230 storage.fOutX = FALSE;
231 storage.fInX = FALSE;
232 storage.fRtsControl = RTS_CONTROL_ENABLE;
238 storage.fOutX = TRUE;
242 storage.fOutxCtsFlow = TRUE;
243 storage.fRtsControl = RTS_CONTROL_HANDSHAKE;
252 storage.c_iflag &= ~(IXOFF | IXON);
253 # if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
254 storage.c_cflag &= ~CRTSCTS;
255 # elif defined(__QNXNTO__)
256 storage.c_cflag &= ~(IHFLOW | OHFLOW);
260 storage.c_iflag |= IXOFF | IXON;
261 # if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
262 storage.c_cflag &= ~CRTSCTS;
263 # elif defined(__QNXNTO__)
264 storage.c_cflag &= ~(IHFLOW | OHFLOW);
268 # if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
269 storage.c_iflag &= ~(IXOFF | IXON);
270 storage.c_cflag |= CRTSCTS;
272 # elif defined(__QNXNTO__)
273 storage.c_iflag &= ~(IXOFF | IXON);
274 storage.c_cflag |= (IHFLOW | OHFLOW);
277 ec = boost::asio::error::operation_not_supported;
278 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
284 ec = boost::system::error_code();
285 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
288 BOOST_ASIO_SYNC_OP_VOID serial_port_base::flow_control::load(
289 const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
291 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
292 if (storage.fOutX && storage.fInX)
296 else if (storage.fOutxCtsFlow && storage.fRtsControl == RTS_CONTROL_HANDSHAKE)
305 if (storage.c_iflag & (IXOFF | IXON))
309 # if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
310 else if (storage.c_cflag & CRTSCTS)
314 # elif defined(__QNXNTO__)
315 else if (storage.c_cflag & IHFLOW && storage.c_cflag & OHFLOW)
325 ec = boost::system::error_code();
326 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
329 serial_port_base::parity::parity(serial_port_base::parity::type t)
332 if (t != none && t != odd && t != even)
334 std::out_of_range ex("invalid parity value");
335 boost::asio::detail::throw_exception(ex);
339 BOOST_ASIO_SYNC_OP_VOID serial_port_base::parity::store(
340 BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
342 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
346 storage.fParity = FALSE;
347 storage.Parity = NOPARITY;
350 storage.fParity = TRUE;
351 storage.Parity = ODDPARITY;
354 storage.fParity = TRUE;
355 storage.Parity = EVENPARITY;
364 storage.c_iflag |= IGNPAR;
365 storage.c_cflag &= ~(PARENB | PARODD);
368 storage.c_iflag &= ~(IGNPAR | PARMRK);
369 storage.c_iflag |= INPCK;
370 storage.c_cflag |= PARENB;
371 storage.c_cflag &= ~PARODD;
374 storage.c_iflag &= ~(IGNPAR | PARMRK);
375 storage.c_iflag |= INPCK;
376 storage.c_cflag |= (PARENB | PARODD);
382 ec = boost::system::error_code();
383 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
386 BOOST_ASIO_SYNC_OP_VOID serial_port_base::parity::load(
387 const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
389 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
390 if (storage.Parity == EVENPARITY)
394 else if (storage.Parity == ODDPARITY)
403 if (storage.c_cflag & PARENB)
405 if (storage.c_cflag & PARODD)
419 ec = boost::system::error_code();
420 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
423 serial_port_base::stop_bits::stop_bits(
424 serial_port_base::stop_bits::type t)
427 if (t != one && t != onepointfive && t != two)
429 std::out_of_range ex("invalid stop_bits value");
430 boost::asio::detail::throw_exception(ex);
434 BOOST_ASIO_SYNC_OP_VOID serial_port_base::stop_bits::store(
435 BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
437 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
441 storage.StopBits = ONESTOPBIT;
444 storage.StopBits = ONE5STOPBITS;
447 storage.StopBits = TWOSTOPBITS;
456 storage.c_cflag &= ~CSTOPB;
459 storage.c_cflag |= CSTOPB;
462 ec = boost::asio::error::operation_not_supported;
463 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
466 ec = boost::system::error_code();
467 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
470 BOOST_ASIO_SYNC_OP_VOID serial_port_base::stop_bits::load(
471 const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
473 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
474 if (storage.StopBits == ONESTOPBIT)
478 else if (storage.StopBits == ONE5STOPBITS)
480 value_ = onepointfive;
482 else if (storage.StopBits == TWOSTOPBITS)
491 value_ = (storage.c_cflag & CSTOPB) ? two : one;
493 ec = boost::system::error_code();
494 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
497 serial_port_base::character_size::character_size(unsigned int t)
502 std::out_of_range ex("invalid character_size value");
503 boost::asio::detail::throw_exception(ex);
507 BOOST_ASIO_SYNC_OP_VOID serial_port_base::character_size::store(
508 BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
510 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
511 storage.ByteSize = value_;
513 storage.c_cflag &= ~CSIZE;
516 case 5: storage.c_cflag |= CS5; break;
517 case 6: storage.c_cflag |= CS6; break;
518 case 7: storage.c_cflag |= CS7; break;
519 case 8: storage.c_cflag |= CS8; break;
523 ec = boost::system::error_code();
524 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
527 BOOST_ASIO_SYNC_OP_VOID serial_port_base::character_size::load(
528 const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
530 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
531 value_ = storage.ByteSize;
533 if ((storage.c_cflag & CSIZE) == CS5) { value_ = 5; }
534 else if ((storage.c_cflag & CSIZE) == CS6) { value_ = 6; }
535 else if ((storage.c_cflag & CSIZE) == CS7) { value_ = 7; }
536 else if ((storage.c_cflag & CSIZE) == CS8) { value_ = 8; }
539 // Hmmm, use 8 for now.
543 ec = boost::system::error_code();
544 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
550 #include <boost/asio/detail/pop_options.hpp>
552 #undef BOOST_ASIO_OPTION_STORAGE
554 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
556 #endif // BOOST_ASIO_IMPL_SERIAL_PORT_BASE_IPP