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
00038
00039
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