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_COMMONCPP
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 
152  fd_t dev;
153 
154  int bufsize;
155 
161  void open(const char *fname);
162 
167  void close(void);
168 
176  virtual int aRead(char * Data, const int Length);
177 
184  virtual int aWrite(const char * Data, const int Length);
185 
193  Error error(Error error, char *errstr = NULL);
194 
201  inline void error(char *err)
202  {error(errExtended, err);};
203 
204 
211  inline void setError(bool enable)
212  {flags.thrown = !enable;};
213 
224  int setPacketInput(int size, unsigned char btimer = 0);
225 
235  int setLineInput(char newline = 13, char nl1 = 0);
236 
240  void restore(void);
241 
245  void flushInput(void);
246 
250  void flushOutput(void);
251 
255  void waitOutput(void);
256 
261  void endSerial(void);
262 
268  void initConfig(void);
269 
275  {initSerial();};
276 
283  Serial(const char *name);
284 
285 
286 public:
287 
294  virtual ~Serial();
295 
300  Serial &operator=(const Serial &from);
301 
308  Error setSpeed(unsigned long speed);
309 
316  Error setCharBits(int bits);
317 
324  Error setParity(Parity parity);
325 
332  Error setStopBits(int bits);
333 
340  Error setFlowControl(Flow flow);
341 
347  void toggleDTR(timeout_t millisec);
348 
352  void sendBreak(void);
353 
360  inline Error getErrorNumber(void)
361  {return errid;};
362 
369  inline char *getErrorString(void)
370  {return errstr;};
371 
379  inline int getBufferSize(void)
380  {return bufsize;};
381 
391  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
392 };
393 
415 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
416 {
417 private:
418  int doallocate();
419 
420  friend TTYStream& crlf(TTYStream&);
421  friend TTYStream& lfcr(TTYStream&);
422 
423 protected:
424  char *gbuf, *pbuf;
425  timeout_t timeout;
426 
431  TTYStream();
432 
437  void allocate(void);
438 
443  void endStream(void);
444 
451  int underflow(void);
452 
461  int uflow(void);
462 
470  int overflow(int ch);
471 
472 public:
479  TTYStream(const char *filename, timeout_t to = 0);
480 
484  virtual ~TTYStream();
485 
491  inline void setTimeout(timeout_t to)
492  {timeout = to;};
493 
501  void interactive(bool flag);
502 
509  int sync(void);
510 
522  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
523 };
524 
534 class __EXPORT ttystream : public TTYStream
535 {
536 public:
540  ttystream();
541 
549  ttystream(const char *name);
550 
556  void open(const char *name);
557 
561  void close(void);
562 
566  inline bool operator!()
567  {return (dev < 0);};
568 };
569 
580 class __EXPORT TTYSession : public Thread, public TTYStream
581 {
582 public:
590  TTYSession(const char *name, int pri = 0, int stack = 0);
591 
592  virtual ~TTYSession();
593 };
594 
595 #ifndef _MSWINDOWS_
596 
597 // Not support this right now.......
598 //
599 class SerialPort;
600 class SerialService;
601 
623 class __EXPORT SerialPort: public Serial, public TimerPort
624 {
625 private:
626  SerialPort *next, *prev;
627  SerialService *service;
628 #ifdef USE_POLL
629  struct pollfd *ufd;
630 #endif
631  bool detect_pending;
632  bool detect_output;
633  bool detect_disconnect;
634 
635  friend class SerialService;
636 
637 protected:
644  SerialPort(SerialService *svc, const char *name);
645 
650  virtual ~SerialPort();
651 
656  void setDetectPending( bool );
657 
661  inline bool getDetectPending( void ) const
662  { return detect_pending; }
663 
668  void setDetectOutput( bool );
669 
673  inline bool getDetectOutput( void ) const
674  { return detect_output; }
675 
680  virtual void expired(void);
681 
687  virtual void pending(void);
688 
693  virtual void disconnect(void);
694 
704  inline int output(void *buf, int len)
705  {return aWrite((char *)buf, len);};
706 
710  virtual void output(void);
711 
721  inline int input(void *buf, int len)
722  {return aRead((char *)buf, len);};
723 public:
731  void setTimer(timeout_t timeout = 0);
732 
738  void incTimer(timeout_t timeout);
739 };
740 
763 class __EXPORT SerialService : public Thread, private Mutex
764 {
765 private:
766  fd_set connect;
767  int iosync[2];
768  int hiwater;
769  int count;
770  SerialPort *first, *last;
771 
777  void attach(SerialPort *port);
778 
784  void detach(SerialPort *port);
785 
789  void run(void);
790 
791  friend class SerialPort;
792 
793 protected:
800  virtual void onUpdate(unsigned char flag);
801 
806  virtual void onEvent(void);
807 
814  virtual void onCallback(SerialPort *port);
815 
816 public:
826  void update(unsigned char flag = 0xff);
827 
836  SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
837 
841  virtual ~SerialService();
842 
849  inline int getCount(void)
850  {return count;};
851 };
852 
853 #endif
854 
855 #ifdef CCXX_EXCEPTIONS
856 class __EXPORT SerException : public IOException
857 {
858 public:
859  SerException(const String &str) : IOException(str) {};
860 };
861 #endif
862 
863 END_NAMESPACE
864 
865 #endif
866 
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...
A more natural C++ &quot;ttystream&quot; class for use by non-threaded applications.
Definition: serial.h:534
void incTimer(timeout_t timeout)
Set a timeout based on the current time reference value either from object creation or the last setTi...
Timer ports are used to provide synchronized timing events when managed under a &quot;service thread&quot; such...
Definition: thread.h:494
Error getErrorNumber(void)
Often used by a &quot;catch&quot; to fetch the last error of a thrown serial.
Definition: serial.h:360
int getCount(void)
Get current reference count.
Definition: serial.h:849
Common C++ thread class and sychronization objects.
int output(void *buf, int len)
Transmit &quot;send&quot; data to the serial port.
Definition: serial.h:704
int getBufferSize(void)
Get the &quot;buffer&quot; size for buffered operations.
Definition: serial.h:379
void setTimeout(timeout_t to)
Set the timeout control.
Definition: serial.h:491
The SerialService is a thead service object that is meant to service attached serial ports...
Definition: serial.h:763
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: serial.h:673
int input(void *buf, int len)
Receive &quot;input&quot; for pending data from the serial port.
Definition: serial.h:721
bool operator!()
Test to see if stream is opened.
Definition: serial.h:566
virtual int aWrite(const char *Data, const int Length)
Writes to serial device.
virtual bool isPending(Pending pend, timeout_t timeout=ucommon::Timer::inf)
Get the status of pending operations.
char * getErrorString(void)
Often used by a &quot;catch&quot; to fetch the user set error string of a thrown serial.
Definition: serial.h:369
The serial port is an internal class which is attached to and then serviced by a specified SerialServ...
Definition: serial.h:623
void error(char *err)
This service is used to thow application defined serial errors where the application specific error c...
Definition: serial.h:201
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: serial.h:661
void close(void)
Closes the serial device.
The Serial class is used as the base for all serial I/O services under APE.
Definition: serial.h:90
TTY streams are used to represent serial connections that are fully &quot;streamable&quot; objects using C++ st...
Definition: serial.h:415
Serial()
This allows later ttystream class to open and close a serial device.
Definition: serial.h:274
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:580
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
void open(const char *fname)
Opens the serial device.
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:211
GNU Common C++ exception model base classes.