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 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 ost {
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  fd_t dev;
153  int bufsize;
154 
160  void open(const char *fname);
161 
166  void close(void);
167 
175  virtual int aRead(char * Data, const int Length);
176 
183  virtual int aWrite(const char * Data, const int Length);
184 
192  Error error(Error error, char *errstr = NULL);
193 
200  inline void error(char *err)
201  {error(errExtended, err);}
202 
203 
210  inline void setError(bool enable)
211  {flags.thrown = !enable;}
212 
223  int setPacketInput(int size, unsigned char btimer = 0);
224 
234  int setLineInput(char newline = 13, char nl1 = 0);
235 
239  void restore(void);
240 
244  void flushInput(void);
245 
249  void flushOutput(void);
250 
254  void waitOutput(void);
255 
260  void endSerial(void);
261 
267  void initConfig(void);
268 
274  {initSerial();}
275 
282  Serial(const char *name);
283 
284 
285 public:
286 
293  virtual ~Serial();
294 
299  Serial &operator=(const Serial &from);
300 
307  Error setSpeed(unsigned long speed);
308 
315  Error setCharBits(int bits);
316 
323  Error setParity(Parity parity);
324 
331  Error setStopBits(int bits);
332 
339  Error setFlowControl(Flow flow);
340 
346  void toggleDTR(timeout_t millisec);
347 
351  void sendBreak(void);
352 
359  inline Error getErrorNumber(void)
360  {return errid;}
361 
368  inline char *getErrorString(void)
369  {return errstr;}
370 
378  inline int getBufferSize(void)
379  {return bufsize;}
380 
390  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
391 };
392 
414 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
415 {
416 private:
417  int doallocate();
418 
419  friend TTYStream& crlf(TTYStream&);
420  friend TTYStream& lfcr(TTYStream&);
421 
422 protected:
423  char *gbuf, *pbuf;
424  timeout_t timeout;
425 
430  TTYStream();
431 
436  void allocate(void);
437 
442  void endStream(void);
443 
450  int underflow(void);
451 
460  int uflow(void);
461 
469  int overflow(int ch);
470 
471 public:
478  TTYStream(const char *filename, timeout_t to = 0);
479 
483  virtual ~TTYStream();
484 
490  inline void setTimeout(timeout_t to)
491  {timeout = to;}
492 
500  void interactive(bool flag);
501 
508  int sync(void);
509 
521  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
522 };
523 
533 class __EXPORT ttystream : public TTYStream
534 {
535 public:
539  ttystream();
540 
548  ttystream(const char *name);
549 
555  void open(const char *name);
556 
560  void close(void);
561 
565  inline bool operator!()
566  {return (dev < 0);}
567 };
568 
579 class __EXPORT TTYSession : public Thread, public TTYStream
580 {
581 public:
589  TTYSession(const char *name, int pri = 0, int stack = 0);
590 
591  virtual ~TTYSession();
592 };
593 
594 #ifndef _MSWINDOWS_
595 
596 // Not support this right now.......
597 //
598 class SerialPort;
599 class SerialService;
600 
622 class __EXPORT SerialPort: public Serial, public TimerPort
623 {
624 private:
625  SerialPort *next, *prev;
626  SerialService *service;
627 #ifdef USE_POLL
628  struct pollfd *ufd;
629 #endif
630  bool detect_pending;
631  bool detect_output;
632  bool detect_disconnect;
633 
634  friend class SerialService;
635 
636 protected:
643  SerialPort(SerialService *svc, const char *name);
644 
649  virtual ~SerialPort();
650 
655  void setDetectPending( bool );
656 
660  inline bool getDetectPending( void ) const
661  { return detect_pending; }
662 
667  void setDetectOutput( bool );
668 
672  inline bool getDetectOutput( void ) const
673  { return detect_output; }
674 
679  virtual void expired(void);
680 
686  virtual void pending(void);
687 
692  virtual void disconnect(void);
693 
703  inline int output(void *buf, int len)
704  {return aWrite((char *)buf, len);}
705 
709  virtual void output(void);
710 
720  inline int input(void *buf, int len)
721  {return aRead((char *)buf, len);}
722 
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 } // namespace ost
864 
865 #endif
A more natural C++ "ttystream" class for use by non-threaded applications.
Definition: serial.h:533
The TTYSession aggragates a TTYStream and a Common C++ Thread which is assumed to be the execution co...
Definition: serial.h:579
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:210
void error(char *err)
This service is used to thow application defined serial errors where the application specific error c...
Definition: serial.h:200
Definition: address.h:59
bool operator!()
Test to see if stream is opened.
Definition: serial.h:565
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: serial.h:660
int getCount(void)
Get current reference count.
Definition: serial.h:849
void setTimeout(timeout_t to)
Set the timeout control.
Definition: serial.h:490
The SerialService is a thead service object that is meant to service attached serial ports...
Definition: serial.h:763
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition: thread.h:497
int input(void *buf, int len)
Receive "input" for pending data from the serial port.
Definition: serial.h:720
Error getErrorNumber(void)
Often used by a "catch" to fetch the last error of a thrown serial.
Definition: serial.h:359
int getBufferSize(void)
Get the "buffer" size for buffered operations.
Definition: serial.h:378
Serial()
This allows later ttystream class to open and close a serial device.
Definition: serial.h:273
int output(void *buf, int len)
Transmit "send" data to the serial port.
Definition: serial.h:703
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: serial.h:672
The serial port is an internal class which is attached to and then serviced by a specified SerialServ...
Definition: serial.h:622
TTY streams are used to represent serial connections that are fully "streamable" objects using C++ st...
Definition: serial.h:414
Common C++ thread class and sychronization objects.
GNU Common C++ exception model base classes.
char * getErrorString(void)
Often used by a "catch" to fetch the user set error string of a thrown serial.
Definition: serial.h:368
The Serial class is used as the base for all serial I/O services under APE.
Definition: serial.h:91