Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

serial.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2002 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception to the GNU General Public License, permission is 00018 // granted for additional uses of the text contained in its release 00019 // of Common C++. 00020 // 00021 // The exception is that, if you link the Common C++ library with other files 00022 // to produce an executable, this does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public License. 00024 // Your use of that executable is in no way restricted on account of 00025 // linking the Common C++ library code into it. 00026 // 00027 // This exception does not however invalidate any other reasons why 00028 // the executable file might be covered by the GNU General Public License. 00029 // 00030 // This exception applies only to the code released under the 00031 // name Common C++. If you copy code from other releases into a copy of 00032 // Common C++, as the General Public License permits, the exception does 00033 // not apply to the code that you add in this way. To avoid misleading 00034 // anyone as to the status of such modified files, you must delete 00035 // this exception notice from them. 00036 // 00037 // If you write modifications of your own for Common C++, it is your choice 00038 // whether to permit this exception to apply to your modifications. 00039 // If you do not wish that, delete this exception notice. 00040 00046 #ifndef CCXX_SERIAL_H_ 00047 #define CCXX_SERIAL_H_ 00048 00049 #ifndef CCXX_MISSING_H_ 00050 #include <cc++/missing.h> 00051 #endif 00052 00053 #ifndef CCXX_THREAD_H_ 00054 #include <cc++/thread.h> 00055 #endif 00056 00057 #ifndef CCXX_EXCEPTION_H_ 00058 #include <cc++/exception.h> 00059 #endif 00060 00061 #ifndef WIN32 00062 typedef int HANDLE; 00063 #define INVALID_HANDLE_VALUE (-1) 00064 #endif 00065 00066 #ifdef CCXX_NAMESPACES 00067 namespace ost { 00068 #endif 00069 00100 class __EXPORT Serial 00101 { 00102 public: 00103 enum Error 00104 { 00105 errSuccess = 0, 00106 errOpenNoTty, 00107 errOpenFailed, 00108 errSpeedInvalid, 00109 errFlowInvalid, 00110 errParityInvalid, 00111 errCharsizeInvalid, 00112 errStopbitsInvalid, 00113 errOptionInvalid, 00114 errResourceFailure, 00115 errOutput, 00116 errInput, 00117 errTimeout, 00118 errExtended 00119 }; 00120 typedef enum Error Error; 00121 00122 enum Flow 00123 { 00124 flowNone, 00125 flowSoft, 00126 flowHard, 00127 flowBoth 00128 }; 00129 typedef enum Flow Flow; 00130 00131 enum Parity 00132 { 00133 parityNone, 00134 parityOdd, 00135 parityEven 00136 }; 00137 typedef enum Parity Parity; 00138 00139 enum Pending 00140 { 00141 pendingInput, 00142 pendingOutput, 00143 pendingError 00144 }; 00145 typedef enum Pending Pending; 00146 00147 private: 00148 Error errid; 00149 char *errstr; 00150 00151 struct 00152 { 00153 bool thrown: 1; 00154 bool linebuf: 1; 00155 } flags; 00156 00157 void * original; 00158 void * current; 00159 00163 void initSerial(void); 00164 00165 protected: 00166 00167 HANDLE dev; 00168 00169 int bufsize; 00170 00176 void open(const char *fname); 00177 00182 void close(void); 00183 00191 virtual int aRead(char * Data, const int Length); 00192 00199 virtual int aWrite(const char * Data, const int Length); 00200 00208 Error error(Error error, char *errstr = NULL); 00209 00216 inline void error(char *err) 00217 {error(errExtended, err);}; 00218 00219 00226 inline void setError(bool enable) 00227 {flags.thrown = !enable;}; 00228 00239 int setPacketInput(int size, unsigned char btimer = 0); 00240 00250 int setLineInput(char newline = 13, char nl1 = 0); 00251 00255 void restore(void); 00256 00260 void flushInput(void); 00261 00265 void flushOutput(void); 00266 00270 void waitOutput(void); 00271 00276 void endSerial(void); 00277 00283 void initConfig(void); 00284 00289 Serial() 00290 {initSerial();}; 00291 00298 Serial(const char *name); 00299 00300 00301 public: 00302 00309 virtual ~Serial() 00310 {endSerial();}; 00311 00316 Serial &operator=(const Serial &from); 00317 00324 Error setSpeed(unsigned long speed); 00325 00332 Error setCharBits(int bits); 00333 00340 Error setParity(Parity parity); 00341 00348 Error setStopBits(int bits); 00349 00356 Error setFlowControl(Flow flow); 00357 00363 void toggleDTR(timeout_t millisec); 00364 00368 void sendBreak(void); 00369 00376 inline Error getErrorNumber(void) 00377 {return errid;}; 00378 00385 inline char *getErrorString(void) 00386 {return errstr;}; 00387 00395 inline int getBufferSize(void) 00396 {return bufsize;}; 00397 00407 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF); 00408 }; 00409 00431 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream 00432 { 00433 private: 00434 int doallocate(); 00435 00436 friend TTYStream& crlf(TTYStream&); 00437 friend TTYStream& lfcr(TTYStream&); 00438 00439 protected: 00440 char *gbuf, *pbuf; 00441 timeout_t timeout; 00442 00447 TTYStream(); 00448 00453 void allocate(void); 00454 00459 void endStream(void); 00460 00467 int underflow(void); 00468 00477 int uflow(void); 00478 00486 int overflow(int ch); 00487 00488 public: 00495 TTYStream(const char *filename, timeout_t to = 0); 00496 00500 virtual ~TTYStream(); 00501 00507 inline void setTimeout(timeout_t to) 00508 {timeout = to;}; 00509 00517 void interactive(bool flag); 00518 00525 int sync(void); 00526 00538 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF); 00539 }; 00540 00550 class __EXPORT ttystream : public TTYStream 00551 { 00552 public: 00556 ttystream(); 00557 00565 ttystream(const char *name); 00566 00572 void open(const char *name); 00573 00577 void close(void); 00578 00582 inline bool operator!() 00583 {return (dev < 0);}; 00584 }; 00585 00596 class TTYSession : public TTYStream, public Thread 00597 { 00598 public: 00606 TTYSession(const char *name, int pri = 0, int stack = 0); 00607 }; 00608 00609 #ifndef WIN32 00610 00611 // Not support this right now....... 00612 // 00613 class __EXPORT SerialPort; 00614 class __EXPORT SerialService; 00615 00637 class __EXPORT SerialPort: public Serial, public TimerPort 00638 { 00639 private: 00640 SerialPort *next, *prev; 00641 SerialService *service; 00642 #ifdef USE_POLL 00643 struct pollfd *ufd; 00644 #endif 00645 bool detect_pending; 00646 bool detect_output; 00647 bool detect_disconnect; 00648 00649 friend class SerialService; 00650 00651 protected: 00658 SerialPort(SerialService *svc, const char *name); 00659 00664 virtual ~SerialPort(); 00665 00670 void setDetectPending( bool ); 00671 00675 bool getDetectPending( void ) const 00676 { return detect_pending; } 00677 00682 void setDetectOutput( bool ); 00683 00687 bool getDetectOutput( void ) const 00688 { return detect_output; } 00689 00694 virtual void expired(void) 00695 {return;}; 00696 00702 virtual void pending(void) 00703 {return;}; 00704 00709 virtual void disconnect(void) 00710 {return;}; 00711 00721 inline int output(void *buf, int len) 00722 {return aWrite((char *)buf, len);}; 00726 virtual void output(void) 00727 {return;}; 00728 00738 inline int input(void *buf, int len) 00739 {return aRead((char *)buf, len);}; 00740 public: 00748 void setTimer(timeout_t timeout = 0); 00749 00755 void incTimer(timeout_t timeout); 00756 }; 00757 00780 class __EXPORT SerialService : public Thread, private Mutex 00781 { 00782 private: 00783 fd_set connect; 00784 int iosync[2]; 00785 int hiwater; 00786 int count; 00787 SerialPort *first, *last; 00788 00794 void attach(SerialPort *port); 00795 00801 void detach(SerialPort *port); 00802 00806 void run(void); 00807 00808 friend class SerialPort; 00809 00810 protected: 00817 virtual void onUpdate(unsigned char flag) 00818 {return;}; 00819 00824 virtual void onEvent(void) 00825 {return;}; 00826 00833 virtual void onCallback(SerialPort *port) 00834 {return;}; 00835 00836 public: 00846 void update(unsigned char flag = 0xff); 00847 00856 SerialService(int pri = 0, size_t stack = 0, const char *id = NULL); 00857 00861 virtual ~SerialService(); 00862 00869 inline int getCount(void) 00870 {return count;}; 00871 }; 00872 00873 #endif 00874 00875 00876 00877 #ifdef COMMON_STD_EXCEPTION 00878 class __EXPORT SerException : public IOException 00879 { 00880 public: 00881 SerException(const String &str) : IOException(str) {}; 00882 }; 00883 #endif 00884 00885 #ifdef CCXX_NAMESPACES 00886 } 00887 #endif 00888 00889 #endif 00890

Generated on Fri Jan 21 13:36:02 2005 for GNU CommonC++ by doxygen 1.3.8