GNU CommonC++
serial.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 CCXX_SERIAL_H_
44 #define CCXX_SERIAL_H_
45 
46 #ifndef CCXX_MISSING_H_
47 #include <cc++/missing.h>
48 #endif
49 
50 #ifndef CCXX_THREAD_H_
51 #include <cc++/thread.h>
52 #endif
53 
54 #ifndef CCXX_EXCEPTION_H_
55 #include <cc++/exception.h>
56 #endif
57 
58 #ifndef WIN32
59 typedef int HANDLE;
60 #define INVALID_HANDLE_VALUE (-1)
61 #endif
62 
63 #ifdef CCXX_NAMESPACES
64 namespace ost {
65 #endif
66 
98 {
99 public:
100  enum Error {
101  errSuccess = 0,
114  errExtended
115  };
116  typedef enum Error Error;
117 
118  enum Flow {
122  flowBoth
123  };
124  typedef enum Flow Flow;
125 
126  enum Parity {
129  parityEven
130  };
131  typedef enum Parity Parity;
132 
133  enum Pending {
136  pendingError
137  };
138  typedef enum Pending Pending;
139 
140 private:
141  Error errid;
142  char *errstr;
143 
144  struct {
145  bool thrown: 1;
146  bool linebuf: 1;
147  } flags;
148 
149  void * original;
150  void * current;
151 
155  void initSerial(void);
156 
157 protected:
158 
160 
161  int bufsize;
162 
168  void open(const char *fname);
169 
174  void close(void);
175 
183  virtual int aRead(char * Data, const int Length);
184 
191  virtual int aWrite(const char * Data, const int Length);
192 
200  Error error(Error error, char *errstr = NULL);
201 
208  inline void error(char *err)
209  {error(errExtended, err);};
210 
211 
218  inline void setError(bool enable)
219  {flags.thrown = !enable;};
220 
231  int setPacketInput(int size, unsigned char btimer = 0);
232 
242  int setLineInput(char newline = 13, char nl1 = 0);
243 
247  void restore(void);
248 
252  void flushInput(void);
253 
257  void flushOutput(void);
258 
262  void waitOutput(void);
263 
268  void endSerial(void);
269 
275  void initConfig(void);
276 
282  {initSerial();};
283 
290  Serial(const char *name);
291 
292 
293 public:
294 
301  virtual ~Serial();
302 
307  Serial &operator=(const Serial &from);
308 
315  Error setSpeed(unsigned long speed);
316 
323  Error setCharBits(int bits);
324 
331  Error setParity(Parity parity);
332 
339  Error setStopBits(int bits);
340 
347  Error setFlowControl(Flow flow);
348 
354  void toggleDTR(timeout_t millisec);
355 
359  void sendBreak(void);
360 
367  inline Error getErrorNumber(void)
368  {return errid;};
369 
376  inline char *getErrorString(void)
377  {return errstr;};
378 
386  inline int getBufferSize(void)
387  {return bufsize;};
388 
398  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
399 };
400 
422 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
423 {
424 private:
425  int doallocate();
426 
427  friend TTYStream& crlf(TTYStream&);
428  friend TTYStream& lfcr(TTYStream&);
429 
430 protected:
431  char *gbuf, *pbuf;
433 
438  TTYStream();
439 
444  void allocate(void);
445 
450  void endStream(void);
451 
458  int underflow(void);
459 
468  int uflow(void);
469 
477  int overflow(int ch);
478 
479 public:
486  TTYStream(const char *filename, timeout_t to = 0);
487 
491  virtual ~TTYStream();
492 
498  inline void setTimeout(timeout_t to)
499  {timeout = to;};
500 
508  void interactive(bool flag);
509 
516  int sync(void);
517 
529  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
530 };
531 
542 {
543 public:
547  ttystream();
548 
556  ttystream(const char *name);
557 
563  void open(const char *name);
564 
568  void close(void);
569 
573  inline bool operator!()
574  {return (dev < 0);};
575 };
576 
587 class __EXPORT TTYSession : public Thread, public TTYStream
588 {
589 public:
597  TTYSession(const char *name, int pri = 0, int stack = 0);
598 
599  virtual ~TTYSession();
600 };
601 
602 #ifndef WIN32
603 
604 // Not support this right now.......
605 //
608 
630 class __EXPORT SerialPort: public Serial, public TimerPort
631 {
632 private:
633  SerialPort *next, *prev;
634  SerialService *service;
635 #ifdef USE_POLL
636  struct pollfd *ufd;
637 #endif
638  bool detect_pending;
639  bool detect_output;
640  bool detect_disconnect;
641 
642  friend class SerialService;
643 
644 protected:
651  SerialPort(SerialService *svc, const char *name);
652 
657  virtual ~SerialPort();
658 
663  void setDetectPending( bool );
664 
668  inline bool getDetectPending( void ) const
669  { return detect_pending; }
670 
675  void setDetectOutput( bool );
676 
680  inline bool getDetectOutput( void ) const
681  { return detect_output; }
682 
687  virtual void expired(void);
688 
694  virtual void pending(void);
695 
700  virtual void disconnect(void);
701 
711  inline int output(void *buf, int len)
712  {return aWrite((char *)buf, len);};
713 
717  virtual void output(void);
718 
728  inline int input(void *buf, int len)
729  {return aRead((char *)buf, len);};
730 public:
738  void setTimer(timeout_t timeout = 0);
739 
745  void incTimer(timeout_t timeout);
746 };
747 
770 class __EXPORT SerialService : public Thread, private Mutex
771 {
772 private:
773  fd_set connect;
774  int iosync[2];
775  int hiwater;
776  int count;
777  SerialPort *first, *last;
778 
784  void attach(SerialPort *port);
785 
791  void detach(SerialPort *port);
792 
796  void run(void);
797 
798  friend class SerialPort;
799 
800 protected:
807  virtual void onUpdate(unsigned char flag);
808 
813  virtual void onEvent(void);
814 
821  virtual void onCallback(SerialPort *port);
822 
823 public:
833  void update(unsigned char flag = 0xff);
834 
843  SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
844 
848  virtual ~SerialService();
849 
856  inline int getCount(void)
857  {return count;};
858 };
859 
860 #endif
861 
862 
863 
864 #ifdef COMMON_STD_EXCEPTION
865 class __EXPORT SerException : public IOException
866 {
867 public:
868  SerException(const String &str) : IOException(str) {};
869 };
870 #endif
871 
872 #ifdef CCXX_NAMESPACES
873 }
874 #endif
875 
876 #endif
877