fix doc example typo
[boost.git] / boost / asio / impl / serial_port_base.ipp
blob0470ff2ced2347555e2a71a094f9cdcb6a636de4
1 //
2 // serial_port_base.ipp
3 // ~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
7 //
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_SERIAL_PORT_BASE_IPP
13 #define BOOST_ASIO_SERIAL_PORT_BASE_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 namespace boost {
22 namespace asio {
24 inline serial_port_base::baud_rate::baud_rate(unsigned int rate)
25   : value_(rate)
29 inline unsigned int serial_port_base::baud_rate::value() const
31   return value_;
34 inline boost::system::error_code serial_port_base::baud_rate::store(
35     BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
37 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
38   storage.BaudRate = value_;
39 #else
40   speed_t baud;
41   switch (value_)
42   {
43   // Do POSIX-specified rates first.
44   case 0: baud = B0; break;
45   case 50: baud = B50; break;
46   case 75: baud = B75; break;
47   case 110: baud = B110; break;
48   case 134: baud = B134; break;
49   case 150: baud = B150; break;
50   case 200: baud = B200; break;
51   case 300: baud = B300; break;
52   case 600: baud = B600; break;
53   case 1200: baud = B1200; break;
54   case 1800: baud = B1800; break;
55   case 2400: baud = B2400; break;
56   case 4800: baud = B4800; break;
57   case 9600: baud = B9600; break;
58   case 19200: baud = B19200; break;
59   case 38400: baud = B38400; break;
60   // And now the extended ones conditionally.
61 # ifdef B7200
62   case 7200: baud = B7200; break;
63 # endif
64 # ifdef B14400
65   case 14400: baud = B14400; break;
66 # endif
67 # ifdef B57600
68   case 57600: baud = B57600; break;
69 # endif
70 # ifdef B115200
71   case 115200: baud = B115200; break;
72 # endif
73 # ifdef B230400
74   case 230400: baud = B230400; break;
75 # endif
76 # ifdef B460800
77   case 460800: baud = B460800; break;
78 # endif
79 # ifdef B500000
80   case 500000: baud = B500000; break;
81 # endif
82 # ifdef B576000
83   case 576000: baud = B576000; break;
84 # endif
85 # ifdef B921600
86   case 921600: baud = B921600; break;
87 # endif
88 # ifdef B1000000
89   case 1000000: baud = B1000000; break;
90 # endif
91 # ifdef B1152000
92   case 1152000: baud = B1152000; break;
93 # endif
94 # ifdef B2000000
95   case 2000000: baud = B2000000; break;
96 # endif
97 # ifdef B3000000
98   case 3000000: baud = B3000000; break;
99 # endif
100 # ifdef B3500000
101   case 3500000: baud = B3500000; break;
102 # endif
103 # ifdef B4000000
104   case 4000000: baud = B4000000; break;
105 # endif
106   default:
107     baud = B0;
108     ec = boost::asio::error::invalid_argument;
109     return ec;
110   }
111 # if defined(_BSD_SOURCE)
112   ::cfsetspeed(&storage, baud);
113 # else
114   ::cfsetispeed(&storage, baud);
115   ::cfsetospeed(&storage, baud);
116 # endif
117 #endif
118   ec = boost::system::error_code();
119   return ec;
122 inline boost::system::error_code serial_port_base::baud_rate::load(
123     const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
125 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
126   value_ = storage.BaudRate;
127 #else
128   speed_t baud = ::cfgetospeed(&storage);
129   switch (baud)
130   {
131   // First do those specified by POSIX.
132   case B0: value_ = 0; break;
133   case B50: value_ = 50; break;
134   case B75: value_ = 75; break;
135   case B110: value_ = 110; break;
136   case B134: value_ = 134; break;
137   case B150: value_ = 150; break;
138   case B200: value_ = 200; break;
139   case B300: value_ = 300; break;
140   case B600: value_ = 600; break;
141   case B1200: value_ = 1200; break;
142   case B1800: value_ = 1800; break;
143   case B2400: value_ = 2400; break;
144   case B4800: value_ = 4800; break;
145   case B9600: value_ = 9600; break;
146   case B19200: value_ = 19200; break;
147   case B38400: value_ = 38400; break;
148   // Now conditionally handle a bunch of extended rates.
149 # ifdef B7200
150   case B7200: value_ = 7200; break;
151 # endif
152 # ifdef B14400
153   case B14400: value_ = 14400; break;
154 # endif
155 # ifdef B57600
156   case B57600: value_ = 57600; break;
157 # endif
158 # ifdef B115200
159   case B115200: value_ = 115200; break;
160 # endif
161 # ifdef B230400
162   case B230400: value_ = 230400; break;
163 # endif
164 # ifdef B460800
165   case B460800: value_ = 460800; break;
166 # endif
167 # ifdef B500000
168   case B500000: value_ = 500000; break;
169 # endif
170 # ifdef B576000
171   case B576000: value_ = 576000; break;
172 # endif
173 # ifdef B921600
174   case B921600: value_ = 921600; break;
175 # endif
176 # ifdef B1000000
177   case B1000000: value_ = 1000000; break;
178 # endif
179 # ifdef B1152000
180   case B1152000: value_ = 1152000; break;
181 # endif
182 # ifdef B2000000
183   case B2000000: value_ = 2000000; break;
184 # endif
185 # ifdef B3000000
186   case B3000000: value_ = 3000000; break;
187 # endif
188 # ifdef B3500000
189   case B3500000: value_ = 3500000; break;
190 # endif
191 # ifdef B4000000
192   case B4000000: value_ = 4000000; break;
193 # endif
194   default:
195     value_ = 0;
196     ec = boost::asio::error::invalid_argument;
197     return ec;
198   }
199 #endif
200   ec = boost::system::error_code();
201   return ec;
204 inline serial_port_base::flow_control::flow_control(
205     serial_port_base::flow_control::type t)
206   : value_(t)
208   if (t != none && t != software && t != hardware)
209     throw std::out_of_range("invalid flow_control value");
212 inline serial_port_base::flow_control::type
213 serial_port_base::flow_control::value() const
215   return value_;
218 inline boost::system::error_code serial_port_base::flow_control::store(
219     BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
221 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
222   storage.fOutxCtsFlow = FALSE;
223   storage.fOutxDsrFlow = FALSE;
224   storage.fTXContinueOnXoff = TRUE;
225   storage.fDtrControl = DTR_CONTROL_ENABLE;
226   storage.fDsrSensitivity = FALSE;
227   storage.fOutX = FALSE;
228   storage.fInX = FALSE;
229   storage.fRtsControl = RTS_CONTROL_ENABLE;
230   switch (value_)
231   {
232   case none:
233     break;
234   case software:
235     storage.fOutX = TRUE;
236     storage.fInX = TRUE;
237     break;
238   case hardware:
239     storage.fOutxCtsFlow = TRUE;
240     storage.fRtsControl = RTS_CONTROL_HANDSHAKE;
241     break;
242   default:
243     break;
244   }
245 #else
246   switch (value_)
247   {
248   case none:
249     storage.c_iflag &= ~(IXOFF | IXON);
250 # if defined(_BSD_SOURCE)
251     storage.c_cflag &= ~CRTSCTS;
252 # endif
253     break;
254   case software:
255     storage.c_iflag |= IXOFF | IXON;
256 # if defined(_BSD_SOURCE)
257     storage.c_cflag &= ~CRTSCTS;
258 # endif
259     break;
260   case hardware:
261 # if defined(_BSD_SOURCE)
262     storage.c_iflag &= ~(IXOFF | IXON);
263     storage.c_cflag |= CRTSCTS;
264     break;
265 # else
266     ec = boost::asio::error::operation_not_supported;
267     return ec;
268 # endif
269   default:
270     break;
271   }
272 #endif
273   ec = boost::system::error_code();
274   return ec;
277 inline boost::system::error_code serial_port_base::flow_control::load(
278     const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
280 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
281   if (storage.fOutX && storage.fInX)
282   {
283     value_ = software;
284   }
285   else if (storage.fOutxCtsFlow && storage.fRtsControl == RTS_CONTROL_HANDSHAKE)
286   {
287     value_ = hardware;
288   }
289   else
290   {
291     value_ = none;
292   }
293 #else
294   if (storage.c_iflag & (IXOFF | IXON))
295   {
296     value_ = software;
297   }
298 # if defined(_BSD_SOURCE)
299   else if (storage.c_cflag & CRTSCTS)
300   {
301     value_ = hardware;
302   }
303 # endif
304   else
305   {
306     value_ = none;
307   }
308 #endif
309   ec = boost::system::error_code();
310   return ec;
313 inline serial_port_base::parity::parity(serial_port_base::parity::type t)
314   : value_(t)
316   if (t != none && t != odd && t != even)
317     throw std::out_of_range("invalid parity value");
320 inline serial_port_base::parity::type serial_port_base::parity::value() const
322   return value_;
325 inline boost::system::error_code serial_port_base::parity::store(
326     BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
328 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
329   switch (value_)
330   {
331   case none:
332     storage.fParity = FALSE;
333     storage.Parity = NOPARITY;
334     break;
335   case odd:
336     storage.fParity = TRUE;
337     storage.Parity = ODDPARITY;
338     break;
339   case even:
340     storage.fParity = TRUE;
341     storage.Parity = EVENPARITY;
342     break;
343   default:
344     break;
345   }
346 #else
347   switch (value_)
348   {
349   case none:
350     storage.c_iflag |= IGNPAR;
351     storage.c_cflag &= ~(PARENB | PARODD);
352     break;
353   case even:
354     storage.c_iflag &= ~(IGNPAR | PARMRK);
355     storage.c_iflag |= INPCK;
356     storage.c_cflag |= PARENB;
357     storage.c_cflag &= ~PARODD;
358     break;
359   case odd:
360     storage.c_iflag &= ~(IGNPAR | PARMRK);
361     storage.c_iflag |= INPCK;
362     storage.c_cflag |= (PARENB | PARODD);
363     break;
364   default:
365     break;
366   }
367 #endif
368   ec = boost::system::error_code();
369   return ec;
372 inline boost::system::error_code serial_port_base::parity::load(
373     const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
375 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
376   if (storage.Parity == EVENPARITY)
377   {
378     value_ = even;
379   }
380   else if (storage.Parity == ODDPARITY)
381   {
382     value_ = odd;
383   }
384   else
385   {
386     value_ = none;
387   }
388 #else
389   if (storage.c_cflag & PARENB)
390   {
391     if (storage.c_cflag & PARODD)
392     {
393       value_ = odd;
394     }
395     else
396     {
397       value_ = even;
398     }
399   }
400   else
401   {
402     value_ = none;
403   }
404 #endif
405   ec = boost::system::error_code();
406   return ec;
409 inline serial_port_base::stop_bits::stop_bits(
410     serial_port_base::stop_bits::type t)
411   : value_(t)
413   if (t != one && t != onepointfive && t != two)
414     throw std::out_of_range("invalid stop_bits value");
417 inline serial_port_base::stop_bits::type
418 serial_port_base::stop_bits::value() const
420   return value_;
423 inline boost::system::error_code serial_port_base::stop_bits::store(
424     BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
426 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
427   switch (value_)
428   {
429   case one:
430     storage.StopBits = ONESTOPBIT;
431     break;
432   case onepointfive:
433     storage.StopBits = ONE5STOPBITS;
434     break;
435   case two:
436     storage.StopBits = TWOSTOPBITS;
437     break;
438   default:
439     break;
440   }
441 #else
442   switch (value_)
443   {
444   case one:
445     storage.c_cflag &= ~CSTOPB;
446     break;
447   case two:
448     storage.c_cflag |= CSTOPB;
449     break;
450   default:
451     ec = boost::asio::error::operation_not_supported;
452     return ec;
453   }
454 #endif
455   ec = boost::system::error_code();
456   return ec;
459 inline boost::system::error_code serial_port_base::stop_bits::load(
460     const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
462 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
463   if (storage.StopBits == ONESTOPBIT)
464   {
465     value_ = one;
466   }
467   else if (storage.StopBits == ONE5STOPBITS)
468   {
469     value_ = onepointfive;
470   }
471   else if (storage.StopBits == TWOSTOPBITS)
472   {
473     value_ = two;
474   }
475   else
476   {
477     value_ = one;
478   }
479 #else
480   value_ = (storage.c_cflag & CSTOPB) ? two : one;
481 #endif
482   ec = boost::system::error_code();
483   return ec;
486 inline serial_port_base::character_size::character_size(unsigned int t)
487   : value_(t)
489   if (t < 5 || t > 8)
490     throw std::out_of_range("invalid character_size value");
493 inline unsigned int serial_port_base::character_size::value() const
495   return value_;
498 inline boost::system::error_code serial_port_base::character_size::store(
499     BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
501 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
502   storage.ByteSize = value_;
503 #else
504   storage.c_cflag &= ~CSIZE;
505   switch (value_)
506   {
507   case 5: storage.c_cflag |= CS5; break;
508   case 6: storage.c_cflag |= CS6; break;
509   case 7: storage.c_cflag |= CS7; break;
510   case 8: storage.c_cflag |= CS8; break;
511   default: break;
512   }
513 #endif
514   ec = boost::system::error_code();
515   return ec;
518 inline boost::system::error_code serial_port_base::character_size::load(
519     const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
521 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
522   value_ = storage.ByteSize;
523 #else
524   if ((storage.c_cflag & CSIZE) == CS5) { value_ = 5; }
525   else if ((storage.c_cflag & CSIZE) == CS6) { value_ = 6; }
526   else if ((storage.c_cflag & CSIZE) == CS7) { value_ = 7; }
527   else if ((storage.c_cflag & CSIZE) == CS8) { value_ = 8; }
528   else
529   {
530     // Hmmm, use 8 for now.
531     value_ = 8;
532   }
533 #endif
534   ec = boost::system::error_code();
535   return ec;
538 } // namespace asio
539 } // namespace boost
541 #include <boost/asio/detail/pop_options.hpp>
543 #endif // BOOST_ASIO_SERIAL_PORT_BASE_IPP