ucommon
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 COMMONCPP_SERIAL_H_
45 #define COMMONCPP_SERIAL_H_
46 
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
49 #endif
50 
51 #ifndef COMMONCPP_THREAD_H_
52 #include <commoncpp/thread.h>
53 #endif
54 
55 #ifndef COMMMONCPP_EXCEPTION_H_
56 #include <commoncpp/exception.h>
57 #endif
58 
59 NAMESPACE_COMMONCPP
60 
91 class __EXPORT Serial
92 {
93 public:
94  enum Error {
95  errSuccess = 0,
96  errOpenNoTty,
97  errOpenFailed,
98  errSpeedInvalid,
99  errFlowInvalid,
100  errParityInvalid,
101  errCharsizeInvalid,
102  errStopbitsInvalid,
103  errOptionInvalid,
104  errResourceFailure,
105  errOutput,
106  errInput,
107  errTimeout,
108  errExtended
109  };
110  typedef enum Error Error;
111 
112  enum Flow {
113  flowNone,
114  flowSoft,
115  flowHard,
116  flowBoth
117  };
118  typedef enum Flow Flow;
119 
120  enum Parity {
121  parityNone,
122  parityOdd,
123  parityEven
124  };
125  typedef enum Parity Parity;
126 
127  enum Pending {
128  pendingInput,
129  pendingOutput,
130  pendingError
131  };
132  typedef enum Pending Pending;
133 
134 private:
135  Error errid;
136  char *errstr;
137 
138  struct {
139  bool thrown: 1;
140  bool linebuf: 1;
141  } flags;
142 
143  void * original;
144  void * current;
145 
149  void initSerial(void);
150 
151 protected:
152 
153  fd_t dev;
154 
155  int bufsize;
156 
162  void open(const char *fname);
163 
168  void close(void);
169 
177  virtual int aRead(char * Data, const int Length);
178 
185  virtual int aWrite(const char * Data, const int Length);
186 
194  Error error(Error error, char *errstr = NULL);
195 
202  inline void error(char *err)
203  {error(errExtended, err);};
204 
205 
212  inline void setError(bool enable)
213  {flags.thrown = !enable;};
214 
225  int setPacketInput(int size, unsigned char btimer = 0);
226 
236  int setLineInput(char newline = 13, char nl1 = 0);
237 
241  void restore(void);
242 
246  void flushInput(void);
247 
251  void flushOutput(void);
252 
256  void waitOutput(void);
257 
262  void endSerial(void);
263 
269  void initConfig(void);
270 
276  {initSerial();};
277 
284  Serial(const char *name);
285 
286 
287 public:
288 
295  virtual ~Serial();
296 
301  Serial &operator=(const Serial &from);
302 
309  Error setSpeed(unsigned long speed);
310 
317  Error setCharBits(int bits);
318 
325  Error setParity(Parity parity);
326 
333  Error setStopBits(int bits);
334 
341  Error setFlowControl(Flow flow);
342 
348  void toggleDTR(timeout_t millisec);
349 
353  void sendBreak(void);
354 
361  inline Error getErrorNumber(void)
362  {return errid;};
363 
370  inline char *getErrorString(void)
371  {return errstr;};
372 
380  inline int getBufferSize(void)
381  {return bufsize;};
382 
392  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
393 };
394 
416 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
417 {
418 private:
419  int doallocate();
420 
421  friend TTYStream& crlf(TTYStream&);
422  friend TTYStream& lfcr(TTYStream&);
423 
424 protected:
425  char *gbuf, *pbuf;
426  timeout_t timeout;
427 
432  TTYStream();
433 
438  void allocate(void);
439 
444  void endStream(void);
445 
452  int underflow(void);
453 
462  int uflow(void);
463 
471  int overflow(int ch);
472 
473 public:
480  TTYStream(const char *filename, timeout_t to = 0);
481 
485  virtual ~TTYStream();
486 
492  inline void setTimeout(timeout_t to)
493  {timeout = to;};
494 
502  void interactive(bool flag);
503 
510  int sync(void);
511 
523  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
524 };
525 
535 class __EXPORT ttystream : public TTYStream
536 {
537 public:
541  ttystream();
542 
550  ttystream(const char *name);
551 
557  void open(const char *name);
558 
562  void close(void);
563 
567  inline bool operator!()
568  {return (dev < 0);};
569 };
570 
581 class __EXPORT TTYSession : public Thread, public TTYStream
582 {
583 public:
591  TTYSession(const char *name, int pri = 0, int stack = 0);
592 
593  virtual ~TTYSession();
594 };
595 
596 #ifndef _MSWINDOWS_
597 
598 // Not support this right now.......
599 //
600 class SerialPort;
601 class SerialService;
602 
624 class __EXPORT SerialPort: public Serial, public TimerPort
625 {
626 private:
627  SerialPort *next, *prev;
628  SerialService *service;
629 #ifdef USE_POLL
630  struct pollfd *ufd;
631 #endif
632  bool detect_pending;
633  bool detect_output;
634  bool detect_disconnect;
635 
636  friend class SerialService;
637 
638 protected:
645  SerialPort(SerialService *svc, const char *name);
646 
651  virtual ~SerialPort();
652 
657  void setDetectPending( bool );
658 
662  inline bool getDetectPending( void ) const
663  { return detect_pending; }
664 
669  void setDetectOutput( bool );
670 
674  inline bool getDetectOutput( void ) const
675  { return detect_output; }
676 
681  virtual void expired(void);
682 
688  virtual void pending(void);
689 
694  virtual void disconnect(void);
695 
705  inline int output(void *buf, int len)
706  {return aWrite((char *)buf, len);};
707 
711  virtual void output(void);
712 
722  inline int input(void *buf, int len)
723  {return aRead((char *)buf, len);};
724 public:
732  void setTimer(timeout_t timeout = 0);
733 
739  void incTimer(timeout_t timeout);
740 };
741 
764 class __EXPORT SerialService : public Thread, private Mutex
765 {
766 private:
767  fd_set connect;
768  int iosync[2];
769  int hiwater;
770  int count;
771  SerialPort *first, *last;
772 
778  void attach(SerialPort *port);
779 
785  void detach(SerialPort *port);
786 
790  void run(void);
791 
792  friend class SerialPort;
793 
794 protected:
801  virtual void onUpdate(unsigned char flag);
802 
807  virtual void onEvent(void);
808 
815  virtual void onCallback(SerialPort *port);
816 
817 public:
827  void update(unsigned char flag = 0xff);
828 
837  SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
838 
842  virtual ~SerialService();
843 
850  inline int getCount(void)
851  {return count;};
852 };
853 
854 #endif
855 
856 #ifdef CCXX_EXCEPTIONS
857 class __EXPORT SerException : public IOException
858 {
859 public:
860  SerException(const String &str) : IOException(str) {};
861 };
862 #endif
863 
864 END_NAMESPACE
865 
866 #endif
867 
TTY streams are used to represent serial connections that are fully &quot;streamable&quot; objects using C++ st...
Definition: serial.h:416
void setError(bool enable)
This method is used to turn the error handler on or off for &quot;throwing&quot; execptions by manipulating the...
Definition: serial.h:212
void setTimeout(timeout_t to)
Set the timeout control.
Definition: serial.h:492
void error(char *err)
This service is used to thow application defined serial errors where the application specific error c...
Definition: serial.h:202
void open(const char *fname)
Opens the serial device.
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: serial.h:662
int getCount(void)
Get current reference count.
Definition: serial.h:850
The SerialService is a thead service object that is meant to service attached serial ports...
Definition: serial.h:764
virtual int aRead(char *Data, const int Length)
Reads from serial device.
The TTYSession aggragates a TTYStream and a Common C++ Thread which is assumed to be the execution co...
Definition: serial.h:581
int input(void *buf, int len)
Receive &quot;input&quot; for pending data from the serial port.
Definition: serial.h:722
Common C++ thread class and sychronization objects.
void setTimer(timeout_t timeout=0)
Set a new start time for the object based on when this call is made and optionally activate the timer...
Error getErrorNumber(void)
Often used by a &quot;catch&quot; to fetch the last error of a thrown serial.
Definition: serial.h:361
int getBufferSize(void)
Get the &quot;buffer&quot; size for buffered operations.
Definition: serial.h:380
void incTimer(timeout_t timeout)
Set a timeout based on the current time reference value either from object creation or the last setTi...
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
virtual bool isPending(Pending pend, timeout_t timeout=ucommon::Timer::inf)
Get the status of pending operations.
Serial()
This allows later ttystream class to open and close a serial device.
Definition: serial.h:275
int output(void *buf, int len)
Transmit &quot;send&quot; data to the serial port.
Definition: serial.h:705
GNU Common C++ exception model base classes.
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: serial.h:674
bool operator!()
Test to see if stream is opened.
Definition: serial.h:567
The serial port is an internal class which is attached to and then serviced by a specified SerialServ...
Definition: serial.h:624
virtual int aWrite(const char *Data, const int Length)
Writes to serial device.
Timer ports are used to provide synchronized timing events when managed under a &quot;service thread&quot; such...
Definition: thread.h:468
void close(void)
Closes the serial device.
A more natural C++ &quot;ttystream&quot; class for use by non-threaded applications.
Definition: serial.h:535
char * getErrorString(void)
Often used by a &quot;catch&quot; to fetch the user set error string of a thrown serial.
Definition: serial.h:370
The Serial class is used as the base for all serial I/O services under APE.
Definition: serial.h:91