GNU CommonC++
serial.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_SERIAL_H_
45 #define CCXX_SERIAL_H_
46 
47 #ifndef CCXX_MISSING_H_
48 #include <cc++/missing.h>
49 #endif
50 
51 #ifndef CCXX_THREAD_H_
52 #include <cc++/thread.h>
53 #endif
54 
55 #ifndef CCXX_EXCEPTION_H_
56 #include <cc++/exception.h>
57 #endif
58 
59 #ifndef WIN32
60 typedef int HANDLE;
61 #define INVALID_HANDLE_VALUE (-1)
62 #endif
63 
64 #ifdef CCXX_NAMESPACES
65 namespace ost {
66 #endif
67 
99 {
100 public:
101  enum Error {
102  errSuccess = 0,
115  errExtended
116  };
117  typedef enum Error Error;
118 
119  enum Flow {
123  flowBoth
124  };
125  typedef enum Flow Flow;
126 
127  enum Parity {
130  parityEven
131  };
132  typedef enum Parity Parity;
133 
134  enum Pending {
137  pendingError
138  };
139  typedef enum Pending Pending;
140 
141 private:
142  Error errid;
143  char *errstr;
144 
145  struct {
146  bool thrown: 1;
147  bool linebuf: 1;
148  } flags;
149 
150  void * original;
151  void * current;
152 
156  void initSerial(void);
157 
158 protected:
159 
161 
162  int bufsize;
163 
169  void open(const char *fname);
170 
175  void close(void);
176 
184  virtual int aRead(char * Data, const int Length);
185 
192  virtual int aWrite(const char * Data, const int Length);
193 
201  Error error(Error error, char *errstr = NULL);
202 
209  inline void error(char *err)
210  {error(errExtended, err);};
211 
212 
219  inline void setError(bool enable)
220  {flags.thrown = !enable;};
221 
232  int setPacketInput(int size, unsigned char btimer = 0);
233 
243  int setLineInput(char newline = 13, char nl1 = 0);
244 
248  void restore(void);
249 
253  void flushInput(void);
254 
258  void flushOutput(void);
259 
263  void waitOutput(void);
264 
269  void endSerial(void);
270 
276  void initConfig(void);
277 
283  {initSerial();};
284 
291  Serial(const char *name);
292 
293 
294 public:
295 
302  virtual ~Serial();
303 
308  Serial &operator=(const Serial &from);
309 
316  Error setSpeed(unsigned long speed);
317 
324  Error setCharBits(int bits);
325 
333 
340  Error setStopBits(int bits);
341 
349 
355  void toggleDTR(timeout_t millisec);
356 
360  void sendBreak(void);
361 
368  inline Error getErrorNumber(void)
369  {return errid;};
370 
377  inline char *getErrorString(void)
378  {return errstr;};
379 
387  inline int getBufferSize(void)
388  {return bufsize;};
389 
399  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
400 };
401 
423 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
424 {
425 private:
426  int doallocate();
427 
430 
431 protected:
432  char *gbuf, *pbuf;
434 
440 
445  void allocate(void);
446 
451  void endStream(void);
452 
459  int underflow(void);
460 
469  int uflow(void);
470 
478  int overflow(int ch);
479 
480 public:
487  TTYStream(const char *filename, timeout_t to = 0);
488 
492  virtual ~TTYStream();
493 
499  inline void setTimeout(timeout_t to)
500  {timeout = to;};
501 
509  void interactive(bool flag);
510 
517  int sync(void);
518 
530  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
531 };
532 
543 {
544 public:
549 
557  ttystream(const char *name);
558 
564  void open(const char *name);
565 
569  void close(void);
570 
574  inline bool operator!()
575  {return (dev < 0);};
576 };
577 
588 class __EXPORT TTYSession : public Thread, public TTYStream
589 {
590 public:
598  TTYSession(const char *name, int pri = 0, int stack = 0);
599 
600  virtual ~TTYSession();
601 };
602 
603 #ifndef WIN32
604 
605 // Not support this right now.......
606 //
607 class __EXPORT SerialPort;
608 class __EXPORT SerialService;
609 
631 class __EXPORT SerialPort: public Serial, public TimerPort
632 {
633 private:
634  SerialPort *next, *prev;
635  SerialService *service;
636 #ifdef USE_POLL
637  struct pollfd *ufd;
638 #endif
639  bool detect_pending;
640  bool detect_output;
641  bool detect_disconnect;
642 
643  friend class SerialService;
644 
645 protected:
652  SerialPort(SerialService *svc, const char *name);
653 
658  virtual ~SerialPort();
659 
664  void setDetectPending( bool );
665 
669  inline bool getDetectPending( void ) const
670  { return detect_pending; }
671 
676  void setDetectOutput( bool );
677 
681  inline bool getDetectOutput( void ) const
682  { return detect_output; }
683 
688  virtual void expired(void);
689 
695  virtual void pending(void);
696 
701  virtual void disconnect(void);
702 
712  inline int output(void *buf, int len)
713  {return aWrite((char *)buf, len);};
714 
718  virtual void output(void);
719 
729  inline int input(void *buf, int len)
730  {return aRead((char *)buf, len);};
731 public:
739  void setTimer(timeout_t timeout = 0);
740 
746  void incTimer(timeout_t timeout);
747 };
748 
771 class __EXPORT SerialService : public Thread, private Mutex
772 {
773 private:
774  fd_set connect;
775  int iosync[2];
776  int hiwater;
777  int count;
778  SerialPort *first, *last;
779 
785  void attach(SerialPort *port);
786 
792  void detach(SerialPort *port);
793 
797  void run(void);
798 
799  friend class SerialPort;
800 
801 protected:
808  virtual void onUpdate(unsigned char flag);
809 
814  virtual void onEvent(void);
815 
822  virtual void onCallback(SerialPort *port);
823 
824 public:
834  void update(unsigned char flag = 0xff);
835 
844  SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
845 
849  virtual ~SerialService();
850 
857  inline int getCount(void)
858  {return count;};
859 };
860 
861 #endif
862 
863 
864 
865 #ifdef COMMON_STD_EXCEPTION
866 class __EXPORT SerException : public IOException
867 {
868 public:
869  SerException(const String &str) : IOException(str) {};
870 };
871 #endif
872 
873 #ifdef CCXX_NAMESPACES
874 }
875 #endif
876 
877 #endif
878 
ost::Serial::bufsize
int bufsize
Definition: serial.h:162
ost::Serial::flowHard
@ flowHard
Definition: serial.h:122
ost::Serial::error
Error error(Error error, char *errstr=NULL)
This service is used to throw all serial errors which usually occur during the serial constructor.
ost::Serial::errInput
@ errInput
Definition: serial.h:113
ost::SerialPort
The serial port is an internal class which is attached to and then serviced by a specified SerialServ...
Definition: serial.h:632
ost::TTYStream::overflow
int overflow(int ch)
This streambuf method is used to write the output buffer through the established tty port.
ost::SerialPort::SerialPort
SerialPort(SerialService *svc, const char *name)
Construct a tty serial port for a named serial device.
ost::Serial::flowSoft
@ flowSoft
Definition: serial.h:121
ost::TTYStream::sync
int sync(void)
Flushes the stream input and out buffers, writes pending output.
ost::SerialPort::setTimer
void setTimer(timeout_t timeout=0)
Derived setTimer to notify the service thread pool of changes in expected timeout.
ost::SerialPort::~SerialPort
virtual ~SerialPort()
Disconnect the Serial Port from the service pool thread and shutdown the port.
ost::TTYSession::~TTYSession
virtual ~TTYSession()
ost::Serial::errOutput
@ errOutput
Definition: serial.h:112
ost::Serial::errOpenNoTty
@ errOpenNoTty
Definition: serial.h:103
ost::Thread
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition: thread.h:1094
ost::Serial::parityNone
@ parityNone
Definition: serial.h:128
ost::Serial::flushOutput
void flushOutput(void)
Used to flush any pending output data.
timeout_t
unsigned long timeout_t
Definition: thread.h:74
ost::Serial::setParity
Error setParity(Parity parity)
Set parity mode.
ost::Serial::endSerial
void endSerial(void)
Used as the default destructor for ending serial I/O services.
ost::Serial::errCharsizeInvalid
@ errCharsizeInvalid
Definition: serial.h:108
ost::ttystream::close
void close(void)
Close method for a tty stream.
HANDLE
int HANDLE
Definition: serial.h:60
ost::SerialPort::expired
virtual void expired(void)
Called by the service thread when the objects timer has expired.
ost::Serial::~Serial
virtual ~Serial()
The serial base class may be "thrown" as a result on an error, and the "catcher" may then choose to d...
ost::TTYStream::underflow
int underflow(void)
This streambuf method is used to load the input buffer through the established tty serial port.
ost::Serial::open
void open(const char *fname)
Opens the serial device.
ost::SerialPort::disconnect
virtual void disconnect(void)
Called by the service thread when an exception has occured such as a hangup.
ost::Mutex
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:187
ost::Serial::thrown
bool thrown
Definition: serial.h:146
ost::Serial::errOpenFailed
@ errOpenFailed
Definition: serial.h:104
ost::SerialPort::getDetectOutput
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: serial.h:681
ost::Serial::errStopbitsInvalid
@ errStopbitsInvalid
Definition: serial.h:109
ost::Serial::getErrorString
char * getErrorString(void)
Often used by a "catch" to fetch the user set error string of a thrown serial.
Definition: serial.h:377
ost::SerialService::getCount
int getCount(void)
Get current reference count.
Definition: serial.h:857
ost::Serial::Error
Error
Definition: serial.h:101
ost::error
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Definition: applog.h:541
ost::Serial
The Serial class is used as the base for all serial I/O services under APE.
Definition: serial.h:99
ost::TTYStream::lfcr
friend TTYStream & lfcr(TTYStream &)
ost::ttystream::open
void open(const char *name)
Open method for a tty stream.
ost::TTYSession::TTYSession
TTYSession(const char *name, int pri=0, int stack=0)
Create TTY stream that will be managed by it's own thread.
ost::Serial::errSpeedInvalid
@ errSpeedInvalid
Definition: serial.h:105
ost::SerialPort::output
virtual void output(void)
Perform when output is available for sending data.
ost::Serial::Parity
Parity
Definition: serial.h:127
ost::ttystream
A more natural C++ "ttystream" class for use by non-threaded applications.
Definition: serial.h:543
ost::Serial::parityOdd
@ parityOdd
Definition: serial.h:129
ost::TTYStream::setTimeout
void setTimeout(timeout_t to)
Set the timeout control.
Definition: serial.h:499
ost::Serial::setError
void setError(bool enable)
This method is used to turn the error handler on or off for "throwing" execptions by manipulating the...
Definition: serial.h:219
ost::ttystream::ttystream
ttystream(const char *name)
Construct and "open" a tty stream object.
ost::Serial::linebuf
bool linebuf
Definition: serial.h:147
ost::SerialService::~SerialService
virtual ~SerialService()
Terminate the service thread and update attached objects.
ost::SerialPort::getDetectPending
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: serial.h:669
ost::TTYStream
TTY streams are used to represent serial connections that are fully "streamable" objects using C++ st...
Definition: serial.h:424
ost::Serial::Pending
Pending
Definition: serial.h:134
ost::Serial::sendBreak
void sendBreak(void)
Send the "break" signal.
ost::Serial::getErrorNumber
Error getErrorNumber(void)
Often used by a "catch" to fetch the last error of a thrown serial.
Definition: serial.h:368
ost::SerialService::onUpdate
virtual void onUpdate(unsigned char flag)
A virtual handler for processing user defined update requests (1-254) which have been posted through ...
ost::Serial::setCharBits
Error setCharBits(int bits)
Set character size.
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::Serial::restore
void restore(void)
Restore serial device to the original settings at time of open.
ost::Serial::flushInput
void flushInput(void)
Used to flush the input waiting queue.
ost::TTYStream::allocate
void allocate(void)
Used to allocate the buffer space needed for iostream operations.
ost::Serial::flowNone
@ flowNone
Definition: serial.h:120
ost::SerialPort::output
int output(void *buf, int len)
Transmit "send" data to the serial port.
Definition: serial.h:712
ost::TTYStream::TTYStream
TTYStream(const char *filename, timeout_t to=0)
Create and open a tty serial port.
ost::Serial::toggleDTR
void toggleDTR(timeout_t millisec)
Set the DTR mode off momentarily.
ost::SerialPort::incTimer
void incTimer(timeout_t timeout)
Derived incTimer to notify the service thread pool of a change in expected timeout.
ost::Serial::getBufferSize
int getBufferSize(void)
Get the "buffer" size for buffered operations.
Definition: serial.h:387
ost::Serial::setLineInput
int setLineInput(char newline=13, char nl1=0)
Set "line buffering" read mode and specifies the newline character to be used in seperating line reco...
ost::SerialService::SerialService
SerialService(int pri=0, size_t stack=0, const char *id=NULL)
Create a service thread for attaching serial ports.
ost::TTYStream::TTYStream
TTYStream()
This constructor is used to derive "ttystream", a more C++ style version of the TTYStream class.
ost::Serial::Serial
Serial()
This allows later ttystream class to open and close a serial device.
Definition: serial.h:282
ost::Serial::Flow
Flow
Definition: serial.h:119
ost::Serial::error
void error(char *err)
This service is used to thow application defined serial errors where the application specific error c...
Definition: serial.h:209
ost::Serial::aRead
virtual int aRead(char *Data, const int Length)
Reads from serial device.
ost::Serial::errResourceFailure
@ errResourceFailure
Definition: serial.h:111
ost::SerialPort::setDetectPending
void setDetectPending(bool)
Used to indicate if the service thread should monitor pending data for us.
ost::Serial::operator=
Serial & operator=(const Serial &from)
Serial ports may also be duplecated by the assignment operator.
ost::Serial::pendingInput
@ pendingInput
Definition: serial.h:135
ost::Serial::pendingOutput
@ pendingOutput
Definition: serial.h:136
ost::Serial::setStopBits
Error setStopBits(int bits)
Set number of stop bits.
missing.h
substitute functions which may be missing in target platform libc.
ost::TTYStream::interactive
void interactive(bool flag)
Set tty mode to buffered or "interactive".
ost::SerialService::update
void update(unsigned char flag=0xff)
Notify service thread that a port has been added or removed, or a timer changed, so that a new schedu...
ost
Definition: address.h:64
ost::Serial::dev
HANDLE dev
Definition: serial.h:160
ost::TTYStream::uflow
int uflow(void)
This streambuf method is used for doing unbuffered reads through the establish tty serial port when i...
ost::Serial::errOptionInvalid
@ errOptionInvalid
Definition: serial.h:110
ost::Serial::errParityInvalid
@ errParityInvalid
Definition: serial.h:107
ost::TTYStream::isPending
bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
ost::ttystream::operator!
bool operator!()
Test to see if stream is opened.
Definition: serial.h:574
ost::Serial::setPacketInput
int setPacketInput(int size, unsigned char btimer=0)
Set packet read mode and "size" of packet read buffer.
ost::Serial::errTimeout
@ errTimeout
Definition: serial.h:114
ost::SerialPort::setDetectOutput
void setDetectOutput(bool)
Used to indicate if output ready monitoring should be performed by the service thread.
ost::TTYSession
The TTYSession aggragates a TTYStream and a Common C++ Thread which is assumed to be the execution co...
Definition: serial.h:589
ost::Serial::aWrite
virtual int aWrite(const char *Data, const int Length)
Writes to serial device.
ost::Serial::waitOutput
void waitOutput(void)
Used to wait until all output has been sent.
ost::SerialService::onCallback
virtual void onCallback(SerialPort *port)
A virtual handler for adding support for additional callback events into SerialPort.
ost::SerialPort::input
int input(void *buf, int len)
Receive "input" for pending data from the serial port.
Definition: serial.h:729
exception.h
GNU Common C++ exception model base classes.
ost::TTYStream::~TTYStream
virtual ~TTYStream()
End the tty stream and cleanup.
ost::Serial::setSpeed
Error setSpeed(unsigned long speed)
Set serial port speed for both input and output.
ost::Serial::Serial
Serial(const char *name)
A serial object may be constructed from a named file on the file system.
ost::TimerPort
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition: thread.h:1760
ost::SerialService
The SerialService is a thead service object that is meant to service attached serial ports.
Definition: serial.h:772
ost::Serial::close
void close(void)
Closes the serial device.
ost::Serial::errFlowInvalid
@ errFlowInvalid
Definition: serial.h:106
ost::Serial::initConfig
void initConfig(void)
Used to initialize a newly opened serial file handle.
ost::SerialPort::pending
virtual void pending(void)
Called by the service thread when input data is pending for this tty port.
thread.h
Synchronization and threading services.
ost::ttystream::ttystream
ttystream()
Construct an unopened "ttystream" object.
ost::TTYStream::crlf
friend TTYStream & crlf(TTYStream &)
ost::TTYStream::timeout
timeout_t timeout
Definition: serial.h:433
ost::Serial::isPending
virtual bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
ost::Serial::setFlowControl
Error setFlowControl(Flow flow)
Set flow control.
ost::TTYStream::endStream
void endStream(void)
Used to terminate the buffer space and clean up the tty connection.
ost::TTYStream::pbuf
char * pbuf
Definition: serial.h:432
TIMEOUT_INF
#define TIMEOUT_INF
Definition: thread.h:115
ost::SerialService::onEvent
virtual void onEvent(void)
A virtual handler for event loop calls.