aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asio/impl/serial_port_base.ipp
blob: 21323ee73d1007fc5a959c732747215d1f59cef1 (plain)
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
//
// impl/serial_port_base.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ASIO_IMPL_SERIAL_PORT_BASE_IPP
#define ASIO_IMPL_SERIAL_PORT_BASE_IPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include "asio/detail/config.hpp"

#if defined(ASIO_HAS_SERIAL_PORT)

#include <stdexcept>
#include "asio/error.hpp"
#include "asio/serial_port_base.hpp"
#include "asio/detail/throw_exception.hpp"

#if defined(GENERATING_DOCUMENTATION)
# define ASIO_OPTION_STORAGE implementation_defined
#elif defined(ASIO_WINDOWS) || defined(__CYGWIN__)
# define ASIO_OPTION_STORAGE DCB
#else
# define ASIO_OPTION_STORAGE termios
#endif

#include "asio/detail/push_options.hpp"

namespace asio {

ASIO_SYNC_OP_VOID serial_port_base::baud_rate::store(
    ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  storage.BaudRate = value_;
#else
  speed_t baud;
  switch (value_)
  {
  // Do POSIX-specified rates first.
  case 0: baud = B0; break;
  case 50: baud = B50; break;
  case 75: baud = B75; break;
  case 110: baud = B110; break;
  case 134: baud = B134; break;
  case 150: baud = B150; break;
  case 200: baud = B200; break;
  case 300: baud = B300; break;
  case 600: baud = B600; break;
  case 1200: baud = B1200; break;
  case 1800: baud = B1800; break;
  case 2400: baud = B2400; break;
  case 4800: baud = B4800; break;
  case 9600: baud = B9600; break;
  case 19200: baud = B19200; break;
  case 38400: baud = B38400; break;
  // And now the extended ones conditionally.
# ifdef B7200
  case 7200: baud = B7200; break;
# endif
# ifdef B14400
  case 14400: baud = B14400; break;
# endif
# ifdef B57600
  case 57600: baud = B57600; break;
# endif
# ifdef B115200
  case 115200: baud = B115200; break;
# endif
# ifdef B230400
  case 230400: baud = B230400; break;
# endif
# ifdef B460800
  case 460800: baud = B460800; break;
# endif
# ifdef B500000
  case 500000: baud = B500000; break;
# endif
# ifdef B576000
  case 576000: baud = B576000; break;
# endif
# ifdef B921600
  case 921600: baud = B921600; break;
# endif
# ifdef B1000000
  case 1000000: baud = B1000000; break;
# endif
# ifdef B1152000
  case 1152000: baud = B1152000; break;
# endif
# ifdef B2000000
  case 2000000: baud = B2000000; break;
# endif
# ifdef B3000000
  case 3000000: baud = B3000000; break;
# endif
# ifdef B3500000
  case 3500000: baud = B3500000; break;
# endif
# ifdef B4000000
  case 4000000: baud = B4000000; break;
# endif
  default:
    ec = asio::error::invalid_argument;
    ASIO_SYNC_OP_VOID_RETURN(ec);
  }
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
  ::cfsetspeed(&storage, baud);
# else
  ::cfsetispeed(&storage, baud);
  ::cfsetospeed(&storage, baud);
# endif
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

ASIO_SYNC_OP_VOID serial_port_base::baud_rate::load(
    const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  value_ = storage.BaudRate;
#else
  speed_t baud = ::cfgetospeed(&storage);
  switch (baud)
  {
  // First do those specified by POSIX.
  case B0: value_ = 0; break;
  case B50: value_ = 50; break;
  case B75: value_ = 75; break;
  case B110: value_ = 110; break;
  case B134: value_ = 134; break;
  case B150: value_ = 150; break;
  case B200: value_ = 200; break;
  case B300: value_ = 300; break;
  case B600: value_ = 600; break;
  case B1200: value_ = 1200; break;
  case B1800: value_ = 1800; break;
  case B2400: value_ = 2400; break;
  case B4800: value_ = 4800; break;
  case B9600: value_ = 9600; break;
  case B19200: value_ = 19200; break;
  case B38400: value_ = 38400; break;
  // Now conditionally handle a bunch of extended rates.
# ifdef B7200
  case B7200: value_ = 7200; break;
# endif
# ifdef B14400
  case B14400: value_ = 14400; break;
# endif
# ifdef B57600
  case B57600: value_ = 57600; break;
# endif
# ifdef B115200
  case B115200: value_ = 115200; break;
# endif
# ifdef B230400
  case B230400: value_ = 230400; break;
# endif
# ifdef B460800
  case B460800: value_ = 460800; break;
# endif
# ifdef B500000
  case B500000: value_ = 500000; break;
# endif
# ifdef B576000
  case B576000: value_ = 576000; break;
# endif
# ifdef B921600
  case B921600: value_ = 921600; break;
# endif
# ifdef B1000000
  case B1000000: value_ = 1000000; break;
# endif
# ifdef B1152000
  case B1152000: value_ = 1152000; break;
# endif
# ifdef B2000000
  case B2000000: value_ = 2000000; break;
# endif
# ifdef B3000000
  case B3000000: value_ = 3000000; break;
# endif
# ifdef B3500000
  case B3500000: value_ = 3500000; break;
# endif
# ifdef B4000000
  case B4000000: value_ = 4000000; break;
# endif
  default:
    value_ = 0;
    ec = asio::error::invalid_argument;
    ASIO_SYNC_OP_VOID_RETURN(ec);
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

serial_port_base::flow_control::flow_control(
    serial_port_base::flow_control::type t)
  : value_(t)
{
  if (t != none && t != software && t != hardware)
  {
    std::out_of_range ex("invalid flow_control value");
    asio::detail::throw_exception(ex);
  }
}

ASIO_SYNC_OP_VOID serial_port_base::flow_control::store(
    ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  storage.fOutxCtsFlow = FALSE;
  storage.fOutxDsrFlow = FALSE;
  storage.fTXContinueOnXoff = TRUE;
  storage.fDtrControl = DTR_CONTROL_ENABLE;
  storage.fDsrSensitivity = FALSE;
  storage.fOutX = FALSE;
  storage.fInX = FALSE;
  storage.fRtsControl = RTS_CONTROL_ENABLE;
  switch (value_)
  {
  case none:
    break;
  case software:
    storage.fOutX = TRUE;
    storage.fInX = TRUE;
    break;
  case hardware:
    storage.fOutxCtsFlow = TRUE;
    storage.fRtsControl = RTS_CONTROL_HANDSHAKE;
    break;
  default:
    break;
  }
#else
  switch (value_)
  {
  case none:
    storage.c_iflag &= ~(IXOFF | IXON);
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
    storage.c_cflag &= ~CRTSCTS;
# elif defined(__QNXNTO__)
    storage.c_cflag &= ~(IHFLOW | OHFLOW);
# endif
    break;
  case software:
    storage.c_iflag |= IXOFF | IXON;
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
    storage.c_cflag &= ~CRTSCTS;
# elif defined(__QNXNTO__)
    storage.c_cflag &= ~(IHFLOW | OHFLOW);
# endif
    break;
  case hardware:
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
    storage.c_iflag &= ~(IXOFF | IXON);
    storage.c_cflag |= CRTSCTS;
    break;
# elif defined(__QNXNTO__)
    storage.c_iflag &= ~(IXOFF | IXON);
    storage.c_cflag |= (IHFLOW | OHFLOW);
    break;
# else
    ec = asio::error::operation_not_supported;
    ASIO_SYNC_OP_VOID_RETURN(ec);
# endif
  default:
    break;
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

ASIO_SYNC_OP_VOID serial_port_base::flow_control::load(
    const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  if (storage.fOutX && storage.fInX)
  {
    value_ = software;
  }
  else if (storage.fOutxCtsFlow && storage.fRtsControl == RTS_CONTROL_HANDSHAKE)
  {
    value_ = hardware;
  }
  else
  {
    value_ = none;
  }
#else
  if (storage.c_iflag & (IXOFF | IXON))
  {
    value_ = software;
  }
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
  else if (storage.c_cflag & CRTSCTS)
  {
    value_ = hardware;
  }
# elif defined(__QNXNTO__)
  else if (storage.c_cflag & IHFLOW && storage.c_cflag & OHFLOW)
  {
    value_ = hardware;
  }
# endif
  else
  {
    value_ = none;
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

serial_port_base::parity::parity(serial_port_base::parity::type t)
  : value_(t)
{
  if (t != none && t != odd && t != even)
  {
    std::out_of_range ex("invalid parity value");
    asio::detail::throw_exception(ex);
  }
}

ASIO_SYNC_OP_VOID serial_port_base::parity::store(
    ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  switch (value_)
  {
  case none:
    storage.fParity = FALSE;
    storage.Parity = NOPARITY;
    break;
  case odd:
    storage.fParity = TRUE;
    storage.Parity = ODDPARITY;
    break;
  case even:
    storage.fParity = TRUE;
    storage.Parity = EVENPARITY;
    break;
  default:
    break;
  }
#else
  switch (value_)
  {
  case none:
    storage.c_iflag |= IGNPAR;
    storage.c_cflag &= ~(PARENB | PARODD);
    break;
  case even:
    storage.c_iflag &= ~(IGNPAR | PARMRK);
    storage.c_iflag |= INPCK;
    storage.c_cflag |= PARENB;
    storage.c_cflag &= ~PARODD;
    break;
  case odd:
    storage.c_iflag &= ~(IGNPAR | PARMRK);
    storage.c_iflag |= INPCK;
    storage.c_cflag |= (PARENB | PARODD);
    break;
  default:
    break;
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

ASIO_SYNC_OP_VOID serial_port_base::parity::load(
    const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  if (storage.Parity == EVENPARITY)
  {
    value_ = even;
  }
  else if (storage.Parity == ODDPARITY)
  {
    value_ = odd;
  }
  else
  {
    value_ = none;
  }
#else
  if (storage.c_cflag & PARENB)
  {
    if (storage.c_cflag & PARODD)
    {
      value_ = odd;
    }
    else
    {
      value_ = even;
    }
  }
  else
  {
    value_ = none;
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

serial_port_base::stop_bits::stop_bits(
    serial_port_base::stop_bits::type t)
  : value_(t)
{
  if (t != one && t != onepointfive && t != two)
  {
    std::out_of_range ex("invalid stop_bits value");
    asio::detail::throw_exception(ex);
  }
}

ASIO_SYNC_OP_VOID serial_port_base::stop_bits::store(
    ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  switch (value_)
  {
  case one:
    storage.StopBits = ONESTOPBIT;
    break;
  case onepointfive:
    storage.StopBits = ONE5STOPBITS;
    break;
  case two:
    storage.StopBits = TWOSTOPBITS;
    break;
  default:
    break;
  }
#else
  switch (value_)
  {
  case one:
    storage.c_cflag &= ~CSTOPB;
    break;
  case two:
    storage.c_cflag |= CSTOPB;
    break;
  default:
    ec = asio::error::operation_not_supported;
    ASIO_SYNC_OP_VOID_RETURN(ec);
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

ASIO_SYNC_OP_VOID serial_port_base::stop_bits::load(
    const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  if (storage.StopBits == ONESTOPBIT)
  {
    value_ = one;
  }
  else if (storage.StopBits == ONE5STOPBITS)
  {
    value_ = onepointfive;
  }
  else if (storage.StopBits == TWOSTOPBITS)
  {
    value_ = two;
  }
  else
  {
    value_ = one;
  }
#else
  value_ = (storage.c_cflag & CSTOPB) ? two : one;
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

serial_port_base::character_size::character_size(unsigned int t)
  : value_(t)
{
  if (t < 5 || t > 8)
  {
    std::out_of_range ex("invalid character_size value");
    asio::detail::throw_exception(ex);
  }
}

ASIO_SYNC_OP_VOID serial_port_base::character_size::store(
    ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  storage.ByteSize = value_;
#else
  storage.c_cflag &= ~CSIZE;
  switch (value_)
  {
  case 5: storage.c_cflag |= CS5; break;
  case 6: storage.c_cflag |= CS6; break;
  case 7: storage.c_cflag |= CS7; break;
  case 8: storage.c_cflag |= CS8; break;
  default: break;
  }
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

ASIO_SYNC_OP_VOID serial_port_base::character_size::load(
    const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
{
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  value_ = storage.ByteSize;
#else

#pragma GCC diagnostic ignored "-Wduplicated-branches"
  if ((storage.c_cflag & CSIZE) == CS5) { value_ = 5; }
  else if ((storage.c_cflag & CSIZE) == CS6) { value_ = 6; }
  else if ((storage.c_cflag & CSIZE) == CS7) { value_ = 7; }
  else if ((storage.c_cflag & CSIZE) == CS8) { value_ = 8; }
  else
  {
    // Hmmm, use 8 for now.
    value_ = 8;
  }
#pragma GCC diagnostic pop
#endif
  ec = asio::error_code();
  ASIO_SYNC_OP_VOID_RETURN(ec);
}

} // namespace asio

#include "asio/detail/pop_options.hpp"

#undef ASIO_OPTION_STORAGE

#endif // defined(ASIO_HAS_SERIAL_PORT)

#endif // ASIO_IMPL_SERIAL_PORT_BASE_IPP