ucommon
serial.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 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 Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef COMMONCPP_SERIAL_H_
44 #define COMMONCPP_SERIAL_H_
45 
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
48 #endif
49 
50 #ifndef COMMONCPP_THREAD_H_
51 #include <commoncpp/thread.h>
52 #endif
53 
54 #ifndef COMMMONCPP_EXCEPTION_H_
55 #include <commoncpp/exception.h>
56 #endif
57 
58 namespace ost {
59 
90 class __EXPORT Serial
91 {
92 public:
93  enum Error {
94  errSuccess = 0,
95  errOpenNoTty,
96  errOpenFailed,
97  errSpeedInvalid,
98  errFlowInvalid,
99  errParityInvalid,
100  errCharsizeInvalid,
101  errStopbitsInvalid,
102  errOptionInvalid,
103  errResourceFailure,
104  errOutput,
105  errInput,
106  errTimeout,
107  errExtended
108  };
109  typedef enum Error Error;
110 
111  enum Flow {
112  flowNone,
113  flowSoft,
114  flowHard,
115  flowBoth
116  };
117  typedef enum Flow Flow;
118 
119  enum Parity {
120  parityNone,
121  parityOdd,
122  parityEven
123  };
124  typedef enum Parity Parity;
125 
126  enum Pending {
127  pendingInput,
128  pendingOutput,
129  pendingError
130  };
131  typedef enum Pending Pending;
132 
133 private:
134  Error errid;
135  char *errstr;
136 
137  struct {
138  bool thrown: 1;
139  bool linebuf: 1;
140  } flags;
141 
142  void *original;
143  void *current;
144 
148  void initSerial(void);
149 
150 protected:
151  fd_t dev;
152  int bufsize;
153 
159  void open(const char *fname);
160 
165  void close(void);
166 
174  virtual int aRead(char * Data, const int Length);
175 
182  virtual int aWrite(const char * Data, const int Length);
183 
191  Error error(Error error, char *errstr = NULL);
192 
199  inline void error(char *err)
200  {error(errExtended, err);}
201 
202 
209  inline void setError(bool enable)
210  {flags.thrown = !enable;}
211 
222  int setPacketInput(int size, unsigned char btimer = 0);
223 
233  int setLineInput(char newline = 13, char nl1 = 0);
234 
238  void restore(void);
239 
243  void flushInput(void);
244 
248  void flushOutput(void);
249 
253  void waitOutput(void);
254 
259  void endSerial(void);
260 
266  void initConfig(void);
267 
273  {initSerial();}
274 
281  Serial(const char *name);
282 
283 
284 public:
285 
292  virtual ~Serial();
293 
298  Serial &operator=(const Serial &from);
299 
306  Error setSpeed(unsigned long speed);
307 
314  Error setCharBits(int bits);
315 
322  Error setParity(Parity parity);
323 
330  Error setStopBits(int bits);
331 
338  Error setFlowControl(Flow flow);
339 
345  void toggleDTR(timeout_t millisec);
346 
350  void sendBreak(void);
351 
358  inline Error getErrorNumber(void)
359  {return errid;}
360 
367  inline char *getErrorString(void)
368  {return errstr;}
369 
377  inline int getBufferSize(void)
378  {return bufsize;}
379 
389  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
390 };
391 
413 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
414 {
415 private:
416  int doallocate();
417 
418  friend TTYStream& crlf(TTYStream&);
419  friend TTYStream& lfcr(TTYStream&);
420 
421 protected:
422  char *gbuf, *pbuf;
423  timeout_t timeout;
424 
429  TTYStream();
430 
435  void allocate(void);
436 
441  void endStream(void);
442 
449  int underflow(void);
450 
459  int uflow(void);
460 
468  int overflow(int ch);
469 
470 public:
477  TTYStream(const char *filename, timeout_t to = 0);
478 
482  virtual ~TTYStream();
483 
489  inline void setTimeout(timeout_t to)
490  {timeout = to;}
491 
499  void interactive(bool flag);
500 
507  int sync(void);
508 
520  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
521 };
522 
532 class __EXPORT ttystream : public TTYStream
533 {
534 public:
538  ttystream();
539 
547  ttystream(const char *name);
548 
554  void open(const char *name);
555 
559  void close(void);
560 
564  inline bool operator!()
565  {return (dev < 0);}
566 };
567 
578 class __EXPORT TTYSession : public Thread, public TTYStream
579 {
580 public:
588  TTYSession(const char *name, int pri = 0, int stack = 0);
589 
590  virtual ~TTYSession();
591 };
592 
593 #ifndef _MSWINDOWS_
594 
595 // Not support this right now.......
596 //
597 class SerialPort;
598 class SerialService;
599 
621 class __EXPORT SerialPort: public Serial, public TimerPort
622 {
623 private:
624  SerialPort *next, *prev;
625  SerialService *service;
626 #ifdef USE_POLL
627  struct pollfd *ufd;
628 #endif
629  bool detect_pending;
630  bool detect_output;
631  bool detect_disconnect;
632 
633  friend class SerialService;
634 
635 protected:
642  SerialPort(SerialService *svc, const char *name);
643 
648  virtual ~SerialPort();
649 
654  void setDetectPending( bool );
655 
659  inline bool getDetectPending( void ) const
660  { return detect_pending; }
661 
666  void setDetectOutput( bool );
667 
671  inline bool getDetectOutput( void ) const
672  { return detect_output; }
673 
678  virtual void expired(void);
679 
685  virtual void pending(void);
686 
691  virtual void disconnect(void);
692 
702  inline int output(void *buf, int len)
703  {return aWrite((char *)buf, len);}
704 
708  virtual void output(void);
709 
719  inline int input(void *buf, int len)
720  {return aRead((char *)buf, len);}
721 
722 public:
730  void setTimer(timeout_t timeout = 0);
731 
737  void incTimer(timeout_t timeout);
738 };
739 
762 class __EXPORT SerialService : public Thread, private Mutex
763 {
764 private:
765  fd_set connect;
766  int iosync[2];
767  int hiwater;
768  int count;
769  SerialPort *first, *last;
770 
776  void attach(SerialPort *port);
777 
783  void detach(SerialPort *port);
784 
788  void run(void);
789 
790  friend class SerialPort;
791 
792 protected:
799  virtual void onUpdate(unsigned char flag);
800 
805  virtual void onEvent(void);
806 
813  virtual void onCallback(SerialPort *port);
814 
815 public:
825  void update(unsigned char flag = 0xff);
826 
835  SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
836 
840  virtual ~SerialService();
841 
848  inline int getCount(void)
849  {return count;}
850 };
851 
852 #endif
853 
854 #ifdef CCXX_EXCEPTIONS
855 class __EXPORT SerException : public IOException
856 {
857 public:
858  SerException(const String &str) : IOException(str) {}
859 };
860 #endif
861 
862 } // namespace ost
863 
864 #endif
void setTimeout(timeout_t to)
Set the timeout control.
Definition: serial.h:489
TTY streams are used to represent serial connections that are fully "streamable" objects using C++ st...
Definition: serial.h:413
The TTYSession aggragates a TTYStream and a Common C++ Thread which is assumed to be the execution co...
Definition: serial.h:578
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:209
Common C++ thread class and sychronization objects.
void error(char *err)
This service is used to thow application defined serial errors where the application specific error c...
Definition: serial.h:199
Definition: address.h:58
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: serial.h:659
int getCount(void)
Get current reference count.
Definition: serial.h:848
The SerialService is a thead service object that is meant to service attached serial ports...
Definition: serial.h:762
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition: thread.h:496
int input(void *buf, int len)
Receive "input" for pending data from the serial port.
Definition: serial.h:719
Error getErrorNumber(void)
Often used by a "catch" to fetch the last error of a thrown serial.
Definition: serial.h:358
int getBufferSize(void)
Get the "buffer" size for buffered operations.
Definition: serial.h:377
Serial()
This allows later ttystream class to open and close a serial device.
Definition: serial.h:272
int output(void *buf, int len)
Transmit "send" data to the serial port.
Definition: serial.h:702
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: serial.h:671
bool operator!()
Test to see if stream is opened.
Definition: serial.h:564
The serial port is an internal class which is attached to and then serviced by a specified SerialServ...
Definition: serial.h:621
A more natural C++ "ttystream" class for use by non-threaded applications.
Definition: serial.h:532
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:334
char * getErrorString(void)
Often used by a "catch" to fetch the user set error string of a thrown serial.
Definition: serial.h:367
The Serial class is used as the base for all serial I/O services under APE.
Definition: serial.h:90
GNU Common C++ exception model base classes.