/usr/include/SerialStreamBuf.h is in libserial-dev 0.6.0~rc1-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | /*
* Time-stamp: <02-Nov-2008 14:52:35 pagey>
*
* $Id: SerialStreamBuf.h,v 1.9 2005-10-17 00:19:12 crayzeewulf Exp $
*
*
*/
#ifndef _SerialStreamBuf_h_
#define _SerialStreamBuf_h_
#include <termios.h>
#include <unistd.h>
#include <iosfwd>
#include <streambuf>
#include <string>
#include <SerialPort.h>
extern "C++" {
namespace LibSerial {
/**
* This is the streambuf subclass used by SerialStream. This
* subclass takes care of opening the serial port file in the
* required modes and providing the corresponding file
* descriptor to SerialStream so that various parameters
* associated with the serial port can be set. Several
* features of this streambuf class resemble those of
* std::filebuf, however this class it not made a subclass of
* filebuf because we need access to the file descriptor
* associated with the serial port and the standard filebuf
* does not provide access to it.
*
* At present, this class uses unbuffered I/O and all calls
* to setbuf() will be ignored.
*
* @author $Author: crayzeewulf $ <A HREF="pagey@gnudom.org">Manish P. Pagey</A>
* @version $Id: SerialStreamBuf.h,v 1.9 2005-10-17 00:19:12 crayzeewulf Exp $
* */
class SerialStreamBuf : public std::streambuf {
public:
/** @name Typedefs
*/
//@{
//@}
/** @name Enumerations
*/
//@{
/**
* The baud rates currently supported by the SUS-2 general
* terminal interface specification. Note that B0 is not
* supported because it is not really a baud rate (it
* causes the modem to hang up i.e. drop DTR). Use the
* close() method instead.
*
* @deprecated This enumeration will be removed in
* revision 0.7.x of libserial. It is here for backward
* compatibility with 0.5.x releases. Please use the
* corresponding enumeration from SerialPort class
* instead.
*/
enum BaudRateEnum {
BAUD_50 = SerialPort::BAUD_50,
BAUD_75 = SerialPort::BAUD_75,
BAUD_110 = SerialPort::BAUD_110,
BAUD_134 = SerialPort::BAUD_134,
BAUD_150 = SerialPort::BAUD_150,
BAUD_200 = SerialPort::BAUD_200,
BAUD_300 = SerialPort::BAUD_300,
BAUD_600 = SerialPort::BAUD_600,
BAUD_1200 = SerialPort::BAUD_1200,
BAUD_1800 = SerialPort::BAUD_1800,
BAUD_2400 = SerialPort::BAUD_2400,
BAUD_4800 = SerialPort::BAUD_4800,
BAUD_9600 = SerialPort::BAUD_9600,
BAUD_19200 = SerialPort::BAUD_19200,
BAUD_38400 = SerialPort::BAUD_38400,
BAUD_57600 = SerialPort::BAUD_57600,
BAUD_115200 = SerialPort::BAUD_115200,
BAUD_230400 = SerialPort::BAUD_230400,
#ifdef __linux__
BAUD_460800 = SerialPort::BAUD_460800,
#endif
BAUD_DEFAULT = SerialPort::BAUD_DEFAULT,
BAUD_INVALID
} ;
/**
* The allowed values of character sizes that can be used
* during the serial communication.
*
* @deprecated This enumeration is deprecated. It will be
* removed in version 0.7.0. It is here for backward
* compatibility with version 0.5.x. Please use
* SerialPort::CharacterSize instead.
*/
enum CharSizeEnum {
CHAR_SIZE_5 = SerialPort::CHAR_SIZE_5,
CHAR_SIZE_6 = SerialPort::CHAR_SIZE_6,
CHAR_SIZE_7 = SerialPort::CHAR_SIZE_7,
CHAR_SIZE_8 = SerialPort::CHAR_SIZE_8,
CHAR_SIZE_DEFAULT = SerialPort::CHAR_SIZE_DEFAULT,
CHAR_SIZE_INVALID
} ;
/**
* The allowed values of the parity associated with the
* serial port communications.
*
* @deprecated This enumeration is deprecated and will be
* removed in version 0.7.0. Please use SerialPort::Parity
* instead.
*/
enum ParityEnum {
PARITY_EVEN = SerialPort::PARITY_EVEN,
PARITY_ODD = SerialPort::PARITY_ODD,
PARITY_NONE = SerialPort::PARITY_NONE,
PARITY_DEFAULT = SerialPort::PARITY_DEFAULT,
PARITY_INVALID //!< Invalid parity value.
} ;
/**
* The values of the flow control settings for a serial
* port.
*
* @deprecated This enumeration has been deprecated and
* will be removed in version 0.7.0. Please use
* SerialPort::FlowControl instead.
*/
enum FlowControlEnum {
FLOW_CONTROL_HARD = SerialPort::FLOW_CONTROL_HARD,
FLOW_CONTROL_SOFT = SerialPort::FLOW_CONTROL_SOFT,
FLOW_CONTROL_NONE = SerialPort::FLOW_CONTROL_NONE,
FLOW_CONTROL_DEFAULT = SerialPort::FLOW_CONTROL_DEFAULT,
FLOW_CONTROL_INVALID //!< Invalid flow control setting.
} ;
//@}
/* ------------------------------------------------------------
* Public Static Members
* ------------------------------------------------------------
*/
/** @name Public static members.
*/
//@{
/**
* The default value of the baud rate of the serial port.
*
* @deprecated Please use SerialPort::BAUD_DEFAULT
* instead.
*/
static const BaudRateEnum DEFAULT_BAUD ;
/**
* The default value of the character size used during the
* serial communication.
*
* @deprecated Please use SerialPort::CHAR_SIZE_DEFAULT
* instead.
*/
static const CharSizeEnum DEFAULT_CHAR_SIZE ;
/**
* The default number of stop bits used.
*
* @deprecated Please use SerialPort::STOP_BITS_DEFAULT
* instead.
*/
static const short DEFAULT_NO_OF_STOP_BITS ;
/**
* The default parity setting.
*
* @deprecated Please use SerialPort::PARITY_DEFAULT
* instead.
*/
static const ParityEnum DEFAULT_PARITY ;
/**
* The default flow control setting.
*
* @deprecated Please use SerialPort::FLOW_CONTROL_DEFAULT
* instead.
*/
static const FlowControlEnum DEFAULT_FLOW_CONTROL ;
/**
* The default character buffer size.
*
* @deprecated VMIN and VTIME will not be supported
* starting from version 0.7.0. Methods of SerialPort
* class provide better mechanisms for implementing read
* and write timeouts.
*/
static const short DEFAULT_VMIN ;
/**
* The default character buffer timing.
*
* @deprecated VMIN and VTIME will not be supported
* starting from version 0.7.0. Methods of SerialPort
* class provide better mechanisms for implementing read
* and write timeouts.
*/
static const short DEFAULT_VTIME ;
//@}
/** @name Constructors and Destructor
*/
//@{
/**
* The default constructor.
*/
SerialStreamBuf() ;
/**
* The destructor.
*/
~SerialStreamBuf() ;
//@}
/** @name Other Public Methods
*/
//@{
/** Returns true if a previous call to open() succeeded (returned
a non-null value) and there has been no intervening call to
close.
*/
bool is_open() const ;
/** If is_open() != <tt>false</tt>, returns a null
pointer. Otherwise, initializes the <tt>streambuf</tt> as
required. It then opens a file, if possible, whose name is
given as the string <tt>filename</tt> using the system call
<tt>std::open(filename.c_str(), flags)</tt>. The value of
parameter <tt>flags</tt> is obtained from the value of the
parameter mode. At present, only <tt>ios_base::in</tt>,
<tt>ios_base::out</tt>, and
(<tt>ios_base::in|ios_base::out</tt>) make sense for a
serial port and hence all other settings result in the call
to fail. The value of <tt>flags</tt> is obtained as:
<br>
<tt>flags = u_flags | O_NOCTTY</tt>
<br>
where <tt>u_flags</tt> is obtained from the following table
depending on the value of the parameter mode:
<table align="center">
<tr>
<td> <b><tt>in</tt></b> </td>
<td> <b><tt>out</tt></b> </td>
<td> <b><tt>u_flags</tt></b> </td>
</tr>
<tr>
<td> + </td>
<td> </td>
<td> <tt>O_RDONLY</tt> </td>
</tr>
<tr>
<td> </td>
<td> + </td>
<td> <tt>O_WRONLY</tt> </td>
</tr>
<tr>
<td> + </td>
<td> + </td>
<td> <tt>O_RDWR</tt> </td>
</tr>
</table>
@return If the <tt>open</tt>() system call succeeds the
method returns <tt>this</tt>. If the call fails, then it
returns a null pointer.
*/
SerialStreamBuf* open( const std::string filename,
std::ios_base::openmode mode =
std::ios_base::in | std::ios_base::out ) ;
/** If is_open() == false, returns a null pointer. If a put area
exists, calls overflow(EOF) to flush characters. Finally it
closes the file by calling
<tt>std::close(mFileDescriptor)</tt> where mFileDescriptor
is the value returned by the last call to open().
For the implementation of the corresponding function in
class filebuf, if the last virtual member function called on
<tt>*this</tt> (between underflow, overflow,
<tt>seekoff</tt>, and <tt>seekpos</tt>) was overflow then it
calls <tt>a_codecvt.unshift</tt> (possible several times) to
determine a termination sequence, inserts those characters
and calls overflow(EOF) again. However, <b>this is not
implemented here yet</b>.
<b>Postcondition</b>: is_open() == <tt>false<tt>
@return <tt>this</tt> on success, a null pointer otherwise.
*/
SerialStreamBuf* close() ;
/** Initialize the serial communication parameters to their
default values.
*/
int SetParametersToDefault() ;
/** If is_open() != true, return -1. Otherwise, set the baud
rate of the associated serial port. Return the baud rate
on success and BAUD_INVALID on failure.
*/
const BaudRateEnum SetBaudRate(const BaudRateEnum baudRate ) ;
/** Return the current baud rate of the serial port. If the baud
rate is not set to a valid value then it returns
BAUD_INVALID.
*/
const BaudRateEnum BaudRate() const ;
/** Set the character size to be used during serial
communication. It returns the character size on success and
CHAR_SIZE_INVALID on failure.
*/
const CharSizeEnum SetCharSize(const CharSizeEnum charSize) ;
/** Return the character size currently being used for serial
communication.
*/
const CharSizeEnum CharSize() const ;
/** Set the number of stop bits used during serial
communication. The only valid values are 1 and 2.
@param stop_bits The number of stop bits. (1 or 2).
@return The number of stop bits or -1 on failure.
*/
short SetNumOfStopBits(short numOfStopBits) ;
/** Get the number of stop bits being used during serial
communication.
@return The number of stop bits.
*/
short NumOfStopBits() const ;
/** Set the parity for serial communication.
@param parity The parity value.
*/
const ParityEnum SetParity(const ParityEnum parityType) ;
/** Get the current parity setting for the serial port.
@return The parity setting for the serial port.
*/
const ParityEnum Parity() const ;
/** Use the specified flow control.
*/
const FlowControlEnum SetFlowControl(const FlowControlEnum flowControlType) ;
/** Return the current flow control setting.
*/
const FlowControlEnum FlowControl() const ;
/**
* Set the minimum number of characters for non-canonical
* reads. See VMIN in man termios(3).
*/
const short SetVMin( short vtime ) ;
/**
* Get the VMIN value for the device. This represents the
* minimum number of characters for non-canonical reads.
*/
const short VMin() const;
/**
* Set character buffer timeout in 10ths of a second. This
* applies to non-canonical reads.
*/
const short SetVTime( short vtime ) ;
/**
* Get the current timeout value for non-canonical reads
* in deciseconds.
*/
const short VTime() const;
//@}
/** @name Operators
*/
//@{
//@}
/* ------------------------------------------------------------
* Friends
* ------------------------------------------------------------
*/
protected:
/* ------------------------------------------------------------
* Protected Data Members
* ------------------------------------------------------------
*/
/** Character used to signal that I/O can start while using
software flow control with the serial port.
*/
static const char CTRL_Q = 0x11 ;
/** Character used to signal that I/O should stop while using
software flow control with the serial port.
*/
static const char CTRL_S = 0x13 ;
/* ------------------------------------------------------------
* Protected Methods
* ------------------------------------------------------------
*/
/** Performs an operation that is defined separately for each
class derived from streambuf. The default behavior is to do
nothing if gptr() is non-null and gptr()!=egptr(). Also,
setbuf(0, 0) usually means unbuffered I/O and setbuf(p, n)
means use p[0]...p[n-1] to hold the buffered characters. In
general, this method implements the subclass's notion of
getting memory for the buffered characters.
In the case of SerialStreamBuf, we want to keep using
unbuffered I/O. Hence, using this method has no effect at
present.
*/
virtual std::streambuf* setbuf( char_type*,
std::streamsize ) ;
/** Reads upto n characters from the serial port and returns
them through the character array located at s.
@return The number of characters actually read from the
serial port.
*/
virtual std::streamsize xsgetn( char_type* s,
std::streamsize n ) ;
/** Check, wether input is available on the port.
If you call \c SerialStream::in_avail, this method will be
called to check for available input.
\code
while( serial_port.rdbuf()->in_avail() > 0 ) {
serial_port.get(ch);
...
}
\endcode */
virtual std::streamsize showmanyc();
/** Reads and returns the next character from the associated
serial port if one otherwise returns traits::eof(). This
method is used for buffered I/O while uflow() is called for
unbuffered I/O.
@return The next character from the serial port.
*/
virtual int_type underflow() ;
/** Reads and returns the next character from the associated
serial port if one otherwise returns traits::eof(). This
method is used for unbuffered I/O while underflow() is
called for unbuffered I/O.
@return The next character from the serial port.
*/
virtual int_type uflow() ;
/** This function is called when a putback of a character
fails. This must be implemented for unbuffered I/O as all
streambuf subclasses are required to provide putback of at
least on character.
*/
virtual int_type pbackfail(int_type c = traits_type::eof()) ;
/** Writes upto n characters from the character sequence at s to
the serial port associated with the buffer.
@return The number of characters that were successfully
written to the serial port.
*/
virtual std::streamsize xsputn( const char_type* s,
std::streamsize n ) ;
/** Writes the specified character to the associated
serial port.
@return The character c.
*/
virtual int_type overflow(int_type c) ;
private:
/* ------------------------------------------------------------
* Private Data Members
* ------------------------------------------------------------
*/
//
// The copy constructor and the assignment operator are
// declared private but never defined. This allows the
// compiler to catch attempts to copy instances of this
// class.
//
SerialStreamBuf( const SerialStreamBuf& ) ;
SerialStreamBuf& operator=( const SerialStreamBuf& ) ;
/**
* We use unbuffered I/O for the serial port. However, we
* need to provide the putback of atleast one
* character. This character contains the putback
* character.
*/
char mPutbackChar ;
/**
* True if a putback value is available in mPutbackChar.
*/
bool mPutbackAvailable ;
/**
* The file descriptor associated with the serial port.
*/
int mFileDescriptor ;
/* ------------------------------------------------------------
* Private Methods
* ------------------------------------------------------------
*/
/**
* This routine is called by open() in order to
* initialize some parameters of the serial port and
* setting its parameters to default values.
*
* @return -1 on failure and some other value on success.
*/
int InitializeSerialPort() ;
} ; // class SerialStreamBuf
inline
SerialStreamBuf::SerialStreamBuf() :
mPutbackChar(0),
mPutbackAvailable(false),
mFileDescriptor(-1)
{
setbuf(0, 0) ;
return ;
}
inline
SerialStreamBuf::~SerialStreamBuf()
{
if( this->is_open() )
{
this->close() ;
}
return ;
}
inline
bool
SerialStreamBuf::is_open() const
{
return (-1 != mFileDescriptor) ;
}
inline
std::streambuf*
SerialStreamBuf::setbuf(char_type *, std::streamsize)
{
return std::streambuf::setbuf(0, 0) ;
}
inline
SerialStreamBuf*
SerialStreamBuf::close()
{
//
// Return a null pointer if the serial port is not currently open.
//
if( this->is_open() == false ) {
return 0 ;
}
//
// Otherwise, close the serial port and set the file descriptor
// to an invalid value.
//
if( -1 == ::close(mFileDescriptor) ) {
//
// If the close failed then return a null pointer.
//
return 0 ;
} else {
//
// Set the file descriptor to an invalid value, -1.
//
mFileDescriptor = -1 ;
//
// On success, return "this" as required by the C++ standard.
//
return this ;
}
}
inline
std::streambuf::int_type
SerialStreamBuf::uflow()
{
int_type next_ch = underflow() ;
mPutbackAvailable = false ;
return next_ch ;
}
} // namespace LibSerial
} // extern "C++"
#endif // #ifndef _SerialStreamBuf_h_
|