UCommon
serial.h
Go to the documentation of this file.
1// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015-2020 Cherokees of Idaho.
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
44#ifndef COMMONCPP_SERIAL_H_
45#define COMMONCPP_SERIAL_H_
46
47#ifndef COMMONCPP_CONFIG_H_
48#include <commoncpp/config.h>
49#endif
50
51#ifndef COMMONCPP_THREAD_H_
52#include <commoncpp/thread.h>
53#endif
54
55#ifndef COMMMONCPP_EXCEPTION_H_
56#include <commoncpp/exception.h>
57#endif
58
59namespace ost {
60
91class __EXPORT Serial
92{
93public:
94 enum Error {
95 errSuccess = 0,
96 errOpenNoTty,
97 errOpenFailed,
98 errSpeedInvalid,
99 errFlowInvalid,
100 errParityInvalid,
101 errCharsizeInvalid,
102 errStopbitsInvalid,
103 errOptionInvalid,
104 errResourceFailure,
105 errOutput,
106 errInput,
107 errTimeout,
108 errExtended
109 };
110 typedef enum Error Error;
111
112 enum Flow {
113 flowNone,
114 flowSoft,
115 flowHard,
116 flowBoth
117 };
118 typedef enum Flow Flow;
119
120 enum Parity {
121 parityNone,
122 parityOdd,
123 parityEven
124 };
125 typedef enum Parity Parity;
126
127 enum Pending {
128 pendingInput,
129 pendingOutput,
130 pendingError
131 };
132 typedef enum Pending Pending;
133
134private:
135 Error errid;
136 char *errstr;
137
138 struct {
139 bool thrown: 1;
140 bool linebuf: 1;
141 } flags;
142
143 void *original;
144 void *current;
145
149 void initSerial(void);
150
151protected:
152 fd_t dev;
153 int bufsize;
154
160 void open(const char *fname);
161
166 void close(void);
167
175 virtual int aRead(char * Data, const int Length);
176
183 virtual int aWrite(const char * Data, const int Length);
184
192 Error error(Error error, char *errstr = NULL);
193
200 inline void error(char *err) {
201 error(errExtended, err);
202 }
203
204
211 inline void setError(bool enable) {
212 flags.thrown = !enable;
213 }
214
225 int setPacketInput(int size, uint8_t btimer = 0);
226
236 int setLineInput(char newline = 13, char nl1 = 0);
237
241 void restore(void);
242
246 void flushInput(void);
247
251 void flushOutput(void);
252
256 void waitOutput(void);
257
262 void endSerial(void);
263
269 void initConfig(void);
270
275 Serial() {
276 initSerial();
277 }
278
285 Serial(const char *name);
286
287
288public:
289
296 virtual ~Serial();
297
302 Serial &operator=(const Serial &from);
303
310 Error setSpeed(unsigned long speed);
311
318 Error setCharBits(int bits);
319
326 Error setParity(Parity parity);
327
334 Error setStopBits(int bits);
335
342 Error setFlowControl(Flow flow);
343
349 void toggleDTR(timeout_t millisec);
350
354 void sendBreak(void);
355
362 inline Error getErrorNumber(void) const {
363 return errid;
364 }
365
372 inline char *getErrorString(void) const {
373 return errstr;
374 }
375
383 inline int getBufferSize(void) const {
384 return bufsize;
385 }
386
396 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
397};
398
420class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream
421{
422private:
423 int doallocate();
424
425 friend TTYStream& crlf(TTYStream&);
426 friend TTYStream& lfcr(TTYStream&);
427
428 __DELETE_COPY(TTYStream);
429
430protected:
431 char *gbuf, *pbuf;
432 timeout_t timeout;
433
438 TTYStream();
439
444 void allocate(void);
445
450 void endStream(void);
451
458 int underflow(void) __OVERRIDE;
459
468 int uflow(void) __OVERRIDE;
469
477 int overflow(int ch) __OVERRIDE;
478
479public:
486 TTYStream(const char *filename, timeout_t to = 0);
487
491 virtual ~TTYStream();
492
498 inline void setTimeout(timeout_t to) {
499 timeout = to;
500 }
501
509 void interactive(bool flag);
510
517 int sync(void) __OVERRIDE;
518
530 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF) __OVERRIDE;
531};
532
542class __EXPORT ttystream : public TTYStream
543{
544private:
545 __DELETE_COPY(ttystream);
546
547public:
551 ttystream();
552
560 ttystream(const char *name);
561
567 void open(const char *name);
568
572 void close(void);
573
577 inline bool operator!() {
578 return (dev < 0);
579 }
580};
581
592class __EXPORT TTYSession : public Thread, public TTYStream
593{
594private:
595 __DELETE_COPY(TTYSession);
596
597public:
605 TTYSession(const char *name, int pri = 0, int stack = 0);
606
607 virtual ~TTYSession();
608};
609
610#ifndef _MSWINDOWS_
611
612// Not support this right now.......
613//
614class SerialPort;
615class SerialService;
616
638class __EXPORT SerialPort: public Serial, public TimerPort
639{
640private:
641 SerialPort *next, *prev;
642 SerialService *service;
643#ifdef USE_POLL
644 struct pollfd *ufd;
645#endif
646 bool detect_pending;
647 bool detect_output;
648 bool detect_disconnect;
649
650 friend class SerialService;
651
652 __DELETE_COPY(SerialPort);
653
654protected:
661 SerialPort(SerialService *svc, const char *name);
662
667 virtual ~SerialPort();
668
673 void setDetectPending( bool );
674
678 inline bool getDetectPending( void ) const {
679 return detect_pending;
680 }
681
686 void setDetectOutput( bool );
687
691 inline bool getDetectOutput( void ) const {
692 return detect_output;
693 }
694
699 virtual void expired(void);
700
706 virtual void pending(void);
707
712 virtual void disconnect(void);
713
723 inline int output(void *buf, int len) {
724 return aWrite((char *)buf, len);
725 }
726
730 virtual void output(void);
731
741 inline int input(void *buf, int len) {
742 return aRead((char *)buf, len);
743 }
744
745public:
753 void setTimer(timeout_t timeout = 0);
754
760 void incTimer(timeout_t timeout);
761};
762
785class __EXPORT SerialService : public Thread, private Mutex
786{
787private:
788 fd_set connect;
789 int iosync[2];
790 int hiwater;
791 int count;
792 SerialPort *first, *last;
793
794 __DELETE_COPY(SerialService);
795
801 void attach(SerialPort *port);
802
808 void detach(SerialPort *port);
809
813 void run(void) __OVERRIDE;
814
815 friend class SerialPort;
816
817protected:
824 virtual void onUpdate(uint8_t flag);
825
830 virtual void onEvent(void);
831
838 virtual void onCallback(SerialPort *port);
839
840public:
850 void update(uint8_t flag = 0xff);
851
860 SerialService(int pri = 0, size_t stack = 0, const char *id = NULL);
861
865 virtual ~SerialService();
866
873 inline int getCount(void) const {
874 return count;
875 }
876};
877
878#endif
879
880#ifdef CCXX_EXCEPTIONS
881class __EXPORT SerException : public IOException
882{
883public:
884 SerException(const String &str) : IOException(str) {}
885};
886#endif
887
888} // namespace ost
889
890#endif
GNU Common C++ exception model base classes.
Common C++ thread class and sychronization objects.