|
serial.hGo to the documentation of this file.00001 // Copyright (C) 1999-2000 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 APE. 00020 // 00021 // The exception is that, if you link the APE 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 APE 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 APE. If you copy code from other releases into a copy of 00032 // APE, 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 APE, 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 00041 #ifndef __CCXX_SERIAL_H__ 00042 #define __CCXX_SERIAL_H__ 00043 00044 #ifndef __CCXX_THREAD_H__ 00045 #include <cc++/thread.h> 00046 #else 00047 #ifdef __CCXX_NAMESPACE_H__ 00048 #include <cc++/macros.h> 00049 #endif 00050 #endif 00051 00052 #include <iostream.h> 00053 00054 typedef enum 00055 { 00056 SERIAL_SUCCESS = 0, 00057 SERIAL_OPEN_NOTTY, 00058 SERIAL_OPEN_FAILED, 00059 SERIAL_SPEED_INVALID, 00060 SERIAL_FLOW_INVALID, 00061 SERIAL_PARITY_INVALID, 00062 SERIAL_CHARSIZE_INVALID, 00063 SERIAL_STOPBITS_INVALID, 00064 SERIAL_OPTION_INVALID, 00065 SERIAL_RESOURCE_FAILURE, 00066 SERIAL_OUTPUT_ERROR, 00067 SERIAL_INPUT_ERROR, 00068 SERIAL_EXTENDED_ERROR 00069 } sioerror_t; 00070 00071 typedef enum 00072 { 00073 SERIAL_FLOW_NONE, 00074 SERIAL_FLOW_SOFT, 00075 SERIAL_FLOW_HARD, 00076 SERIAL_FLOW_BOTH 00077 } sioflow_t; 00078 00079 typedef enum 00080 { 00081 SERIAL_PARITY_NONE, 00082 SERIAL_PARITY_ODD, 00083 SERIAL_PARITY_EVEN 00084 } sioparity_t; 00085 00086 typedef enum 00087 { 00088 SERIAL_PENDING_INPUT, 00089 SERIAL_PENDING_OUTPUT, 00090 SERIAL_PENDING_ERROR 00091 } siopend_t; 00092 00124 class Serial 00125 { 00126 private: 00127 sioerror_t errid; 00128 char *errstr; 00129 00130 struct 00131 { 00132 bool thrown: 1; 00133 bool linebuf: 1; 00134 } flags; 00135 00136 void *original, *current; 00137 00141 void initSerial(void); 00142 00143 protected: 00144 int dev; 00145 int bufsize; 00146 00154 sioerror_t Error(sioerror_t error, char *errstr = NULL); 00155 00162 inline void Error(char *errstr) 00163 {Error(SERIAL_EXTENDED_ERROR, errstr);}; 00164 00165 00172 inline void setError(bool enable) 00173 {flags.thrown = !enable;}; 00174 00185 int setPacketInput(int size, unsigned char btimer = 0); 00186 00195 int setLineInput(char newline = 13, char nl1 = 0); 00196 00200 void Restore(void); 00201 00205 void flushInput(void); 00206 00210 void flushOutput(void); 00211 00215 void waitOutput(void); 00216 00221 void endSerial(void); 00222 00228 void initConfig(void); 00229 00236 Serial(const char *name); 00237 00242 Serial() 00243 {initSerial();}; 00244 00245 public: 00252 virtual ~Serial() 00253 {endSerial();}; 00254 00259 Serial &operator=(const Serial &from); 00260 00267 sioerror_t setSpeed(unsigned long speed); 00268 00275 sioerror_t setCharBits(int bits); 00276 00283 sioerror_t setParity(sioparity_t parity); 00284 00291 sioerror_t setStopBits(int bits); 00292 00299 sioerror_t setFlowControl(sioflow_t flow); 00300 00306 void toggleDTR(timeout_t millisec); 00307 00311 void sendBreak(void); 00312 00319 inline sioerror_t getErrorNumber(void) 00320 {return errid;}; 00321 00328 inline char *getErrorString(void) 00329 {return errstr;}; 00330 00338 inline int getBufferSize(void) 00339 {return bufsize;}; 00340 00350 virtual bool isPending(siopend_t pend, timeout_t timeout = TIMEOUT_INF); 00351 }; 00352 00375 #if defined(STLPORT) || defined(__KCC) 00376 #define iostream iostream_withassign 00377 #endif 00378 class TTYStream : public streambuf, public iostream, public Serial 00379 { 00380 private: 00381 int doallocate(); 00382 00383 friend TTYStream& crlf(TTYStream&); 00384 friend TTYStream& lfcr(TTYStream&); 00385 00386 protected: 00387 char *gbuf, *pbuf; 00388 00393 TTYStream(); 00394 00399 void Allocate(void); 00400 00405 void endStream(void); 00406 00413 int underflow(void); 00414 00423 int uflow(void); 00424 00432 int overflow(int ch); 00433 00434 public: 00440 TTYStream(const char *filename); 00441 00445 ~TTYStream(); 00446 00454 void Interactive(bool flag); 00455 00462 int sync(void); 00463 00475 bool isPending(siopend_t pend, timeout_t timeout = TIMEOUT_INF); 00476 }; 00477 00487 class ttystream : public TTYStream 00488 { 00489 public: 00493 ttystream(); 00494 00502 ttystream(const char *name); 00503 00509 void open(const char *name); 00510 00514 void close(void); 00515 00519 inline bool operator!() 00520 {return (dev < 0);}; 00521 }; 00522 00533 class TTYSession : public TTYStream, public Thread 00534 { 00535 public: 00544 TTYSession(const char *name, Semaphore *start = NULL, int pri = 0, int stack = 0); 00545 }; 00546 00547 class SerialPort; 00548 class SerialService; 00549 00571 class SerialPort: public Serial, public TimerPort 00572 { 00573 private: 00574 SerialPort *next, *prev; 00575 SerialService *service; 00576 #ifdef __CCXX_USE_POLL 00577 struct pollfd *ufd; 00578 #endif 00579 bool detect_pending; 00580 bool detect_output; 00581 bool detect_disconnect; 00582 00583 friend class SerialService; 00584 00585 protected: 00592 SerialPort(SerialService *svc, const char *name); 00593 00598 virtual ~SerialPort(); 00599 00604 void setDetectPending( bool ); 00605 00609 bool getDetectPending( void ) const 00610 { return detect_pending; } 00611 00616 void setDetectOutput( bool ); 00617 00621 bool getDetectOutput( void ) const 00622 { return detect_output; } 00623 00628 virtual void Expired(void) 00629 {return;}; 00630 00636 virtual void Pending(void) 00637 {return;}; 00638 00643 virtual void Disconnect(void) 00644 {return;}; 00645 00655 inline int Output(void *buf, int len) 00656 {return ::write(dev, (char *)buf, len);}; 00657 00661 virtual void Output(void) 00662 {return;}; 00663 00673 inline int Input(void *buf, int len) 00674 {return ::read(dev, (char *)buf, len);}; 00675 00676 public: 00684 void setTimer(timeout_t timeout = 0); 00685 00691 void incTimer(timeout_t timeout); 00692 }; 00693 00716 class SerialService : public Thread, private Mutex 00717 { 00718 private: 00719 fd_set connect; 00720 int iosync[2]; 00721 int hiwater; 00722 int count; 00723 SerialPort *first, *last; 00724 00730 void Attach(SerialPort *port); 00731 00737 void Detach(SerialPort *port); 00738 00742 void Run(void); 00743 00744 friend class SerialPort; 00745 00746 protected: 00753 virtual void OnUpdate(unsigned char flag) 00754 {return;}; 00755 00760 virtual void OnEvent(void) 00761 {return;}; 00762 00769 virtual void OnCallback(SerialPort *port) 00770 {return;}; 00771 00772 public: 00782 void Update(unsigned char flag = 0xff); 00783 00790 SerialService(int pri = 0); 00791 00795 ~SerialService(); 00796 00803 inline int getCount(void) 00804 {return count;}; 00805 }; 00806 00807 #ifdef __CCXX_NAMESPACE_H__ 00808 #undef __CCXX_NAMESPACE_H__ 00809 #include <cc++/namespace.h> 00810 #endif 00811 #endif 00812 Generated at Fri Mar 23 10:47:54 2001 for CommonC++ by 1.2.1 written by Dimitri van Heesch, © 1997-2000 |