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
00226 void initConfig(void);
00227
00234 Serial(const char *name);
00235
00240 Serial()
00241 {initSerial();};
00242
00243 public:
00250 virtual ~Serial()
00251 {endSerial();};
00252
00257 Serial &operator=(const Serial &from);
00258
00265 sioerror_t setSpeed(unsigned long speed);
00266
00273 sioerror_t setCharBits(int bits);
00274
00281 sioerror_t setParity(sioparity_t parity);
00282
00289 sioerror_t setStopBits(int bits);
00290
00297 sioerror_t setFlowControl(sioflow_t flow);
00298
00305 inline sioerror_t getErrorNumber(void)
00306 {return errid;};
00307
00314 inline char *getErrorString(void)
00315 {return errstr;};
00316
00324 inline int getBufferSize(void)
00325 {return bufsize;};
00326
00336 virtual bool isPending(siopend_t pend, timeout_t timeout = TIMEOUT_INF);
00337 };
00338
00361 class TTYStream : public streambuf, public iostream, public Serial
00362 {
00363 private:
00364 int doallocate();
00365
00366 friend TTYStream& crlf(TTYStream&);
00367 friend TTYStream& lfcr(TTYStream&);
00368
00369 protected:
00370 char *gbuf, *pbuf;
00371
00376 TTYStream();
00377
00382 void Allocate(void);
00383
00388 void endStream(void);
00389
00396 int underflow(void);
00397
00406 int uflow(void);
00407
00415 int overflow(int ch);
00416
00417 public:
00423 TTYStream(const char *filename);
00424
00428 ~TTYStream();
00429
00437 void Interactive(bool flag);
00438
00445 int sync(void);
00446
00458 bool isPending(siopend_t pend, timeout_t timeout = TIMEOUT_INF);
00459 };
00460
00470 class ttystream : public TTYStream
00471 {
00472 public:
00476 ttystream();
00477
00485 ttystream(const char *name);
00486
00492 void open(const char *name);
00493
00497 void close(void);
00498
00502 inline bool operator!()
00503 {return (dev < 0);};
00504 };
00505
00516 class TTYSession : public TTYStream, public Thread
00517 {
00518 public:
00527 TTYSession(const char *name, Semaphore *start = NULL, int pri = 0, int stack = 0);
00528 };
00529
00530 class SerialPort;
00531 class SerialService;
00532
00554 class SerialPort: public Serial, public TimerPort
00555 {
00556 private:
00557 SerialPort *next, *prev;
00558 SerialService *service;
00559 #ifdef __CCXX_USE_POLL
00560 struct pollfd *ufd;
00561 #endif
00562 bool detect_pending;
00563 bool detect_output;
00564 bool detect_disconnect;
00565
00566 friend class SerialService;
00567
00568 protected:
00575 SerialPort(SerialService *svc, const char *name);
00576
00581 virtual ~SerialPort();
00582
00587 void setDetectPending( bool );
00588
00592 bool getDetectPending( void ) const
00593 { return detect_pending; }
00594
00599 void setDetectOutput( bool );
00600
00604 bool getDetectOutput( void ) const
00605 { return detect_output; }
00606
00611 virtual void Expired(void)
00612 {return;};
00613
00619 virtual void Pending(void)
00620 {return;};
00621
00626 virtual void Disconnect(void)
00627 {return;};
00628
00638 inline int Output(void *buf, int len)
00639 {return ::write(dev, (char *)buf, len);};
00640
00644 virtual void Output(void)
00645 {return;};
00646
00656 inline int Input(void *buf, int len)
00657 {return ::read(dev, (char *)buf, len);};
00658
00659 public:
00667 void setTimer(timeout_t timeout = 0);
00668
00674 void incTimer(timeout_t timeout);
00675 };
00676
00699 class SerialService : public Thread, private Mutex
00700 {
00701 private:
00702 fd_set connect;
00703 int iosync[2];
00704 int hiwater;
00705 int count;
00706 SerialPort *first, *last;
00707
00713 void Attach(SerialPort *port);
00714
00720 void Detach(SerialPort *port);
00721
00725 void Run(void);
00726
00727 friend class SerialPort;
00728
00729 protected:
00736 virtual void OnUpdate(unsigned char flag)
00737 {return;};
00738
00743 virtual void OnEvent(void)
00744 {return;};
00745
00752 virtual void OnCallback(SerialPort *port)
00753 {return;};
00754
00755 public:
00765 void Update(unsigned char flag = 0xff);
00766
00773 SerialService(int pri = 0);
00774
00778 ~SerialService();
00779
00786 inline int getCount(void)
00787 {return count;};
00788 };
00789
00790 #ifdef __CCXX_NAMESPACE_H__
00791 #undef __CCXX_NAMESPACE_H__
00792 #include <cc++/namespace.h>
00793 #endif
00794 #endif
00795