00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef COMMONCPP_SERIAL_H_
00044 #define COMMONCPP_SERIAL_H_
00045
00046 #ifndef COMMONCPP_CONFIG_H_
00047 #include <commoncpp/config.h>
00048 #endif
00049
00050 #ifndef COMMONCPP_THREAD_H_
00051 #include <commoncpp/thread.h>
00052 #endif
00053
00054 #ifndef COMMMONCPP_EXCEPTION_H_
00055 #include <commoncpp/exception.h>
00056 #endif
00057
00058 NAMESPACE_COMMONCPP
00059
00090 class __EXPORT Serial
00091 {
00092 public:
00093 enum Error {
00094 errSuccess = 0,
00095 errOpenNoTty,
00096 errOpenFailed,
00097 errSpeedInvalid,
00098 errFlowInvalid,
00099 errParityInvalid,
00100 errCharsizeInvalid,
00101 errStopbitsInvalid,
00102 errOptionInvalid,
00103 errResourceFailure,
00104 errOutput,
00105 errInput,
00106 errTimeout,
00107 errExtended
00108 };
00109 typedef enum Error Error;
00110
00111 enum Flow {
00112 flowNone,
00113 flowSoft,
00114 flowHard,
00115 flowBoth
00116 };
00117 typedef enum Flow Flow;
00118
00119 enum Parity {
00120 parityNone,
00121 parityOdd,
00122 parityEven
00123 };
00124 typedef enum Parity Parity;
00125
00126 enum Pending {
00127 pendingInput,
00128 pendingOutput,
00129 pendingError
00130 };
00131 typedef enum Pending Pending;
00132
00133 private:
00134 Error errid;
00135 char *errstr;
00136
00137 struct {
00138 bool thrown: 1;
00139 bool linebuf: 1;
00140 } flags;
00141
00142 void * original;
00143 void * current;
00144
00148 void initSerial(void);
00149
00150 protected:
00151
00152 fd_t dev;
00153
00154 int bufsize;
00155
00161 void open(const char *fname);
00162
00167 void close(void);
00168
00176 virtual int aRead(char * Data, const int Length);
00177
00184 virtual int aWrite(const char * Data, const int Length);
00185
00193 Error error(Error error, char *errstr = NULL);
00194
00201 inline void error(char *err)
00202 {error(errExtended, err);};
00203
00204
00211 inline void setError(bool enable)
00212 {flags.thrown = !enable;};
00213
00224 int setPacketInput(int size, unsigned char btimer = 0);
00225
00235 int setLineInput(char newline = 13, char nl1 = 0);
00236
00240 void restore(void);
00241
00245 void flushInput(void);
00246
00250 void flushOutput(void);
00251
00255 void waitOutput(void);
00256
00261 void endSerial(void);
00262
00268 void initConfig(void);
00269
00274 Serial()
00275 {initSerial();};
00276
00283 Serial(const char *name);
00284
00285
00286 public:
00287
00294 virtual ~Serial();
00295
00300 Serial &operator=(const Serial &from);
00301
00308 Error setSpeed(unsigned long speed);
00309
00316 Error setCharBits(int bits);
00317
00324 Error setParity(Parity parity);
00325
00332 Error setStopBits(int bits);
00333
00340 Error setFlowControl(Flow flow);
00341
00347 void toggleDTR(timeout_t millisec);
00348
00352 void sendBreak(void);
00353
00360 inline Error getErrorNumber(void)
00361 {return errid;};
00362
00369 inline char *getErrorString(void)
00370 {return errstr;};
00371
00379 inline int getBufferSize(void)
00380 {return bufsize;};
00381
00391 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
00392 };
00393
00415 class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
00416 {
00417 private:
00418 int doallocate();
00419
00420 friend TTYStream& crlf(TTYStream&);
00421 friend TTYStream& lfcr(TTYStream&);
00422
00423 protected:
00424 char *gbuf, *pbuf;
00425 timeout_t timeout;
00426
00431 TTYStream();
00432
00437 void allocate(void);
00438
00443 void endStream(void);
00444
00451 int underflow(void);
00452
00461 int uflow(void);
00462
00470 int overflow(int ch);
00471
00472 public:
00479 TTYStream(const char *filename, timeout_t to = 0);
00480
00484 virtual ~TTYStream();
00485
00491 inline void setTimeout(timeout_t to)
00492 {timeout = to;};
00493
00501 void interactive(bool flag);
00502
00509 int sync(void);
00510
00522 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
00523 };
00524
00534 class __EXPORT ttystream : public TTYStream
00535 {
00536 public:
00540 ttystream();
00541
00549 ttystream(const char *name);
00550
00556 void open(const char *name);
00557
00561 void close(void);
00562
00566 inline bool operator!()
00567 {return (dev < 0);};
00568 };
00569
00580 class __EXPORT TTYSession : public Thread, public TTYStream
00581 {
00582 public:
00590 TTYSession(const char *name, int pri = 0, int stack = 0);
00591
00592 virtual ~TTYSession();
00593 };
00594
00595 #ifndef _MSWINDOWS_
00596
00597
00598
00599 class SerialPort;
00600 class SerialService;
00601
00623 class __EXPORT SerialPort: public Serial, public TimerPort
00624 {
00625 private:
00626 SerialPort *next, *prev;
00627 SerialService *service;
00628 #ifdef USE_POLL
00629 struct pollfd *ufd;
00630 #endif
00631 bool detect_pending;
00632 bool detect_output;
00633 bool detect_disconnect;
00634
00635 friend class SerialService;
00636
00637 protected:
00644 SerialPort(SerialService *svc, const char *name);
00645
00650 virtual ~SerialPort();
00651
00656 void setDetectPending( bool );
00657
00661 inline bool getDetectPending( void ) const
00662 { return detect_pending; }
00663
00668 void setDetectOutput( bool );
00669
00673 inline bool getDetectOutput( void ) const
00674 { return detect_output; }
00675
00680 virtual void expired(void);
00681
00687 virtual void pending(void);
00688
00693 virtual void disconnect(void);
00694
00704 inline int output(void *buf, int len)
00705 {return aWrite((char *)buf, len);};
00706
00710 virtual void output(void);
00711
00721 inline int input(void *buf, int len)
00722 {return aRead((char *)buf, len);};
00723 public:
00731 void setTimer(timeout_t timeout = 0);
00732
00738 void incTimer(timeout_t timeout);
00739 };
00740
00763 class __EXPORT SerialService : public Thread, private Mutex
00764 {
00765 private:
00766 fd_set connect;
00767 int iosync[2];
00768 int hiwater;
00769 int count;
00770 SerialPort *first, *last;
00771
00777 void attach(SerialPort *port);
00778
00784 void detach(SerialPort *port);
00785
00789 void run(void);
00790
00791 friend class SerialPort;
00792
00793 protected:
00800 virtual void onUpdate(unsigned char flag);
00801
00806 virtual void onEvent(void);
00807
00814 virtual void onCallback(SerialPort *port);
00815
00816 public:
00826 void update(unsigned char flag = 0xff);
00827
00836 SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
00837
00841 virtual ~SerialService();
00842
00849 inline int getCount(void)
00850 {return count;};
00851 };
00852
00853 #endif
00854
00855 #ifdef CCXX_EXCEPTIONS
00856 class __EXPORT SerException : public IOException
00857 {
00858 public:
00859 SerException(const String &str) : IOException(str) {};
00860 };
00861 #endif
00862
00863 END_NAMESPACE
00864
00865 #endif
00866