LRAUV  revA
UartStream.h
Go to the documentation of this file.
1 
10 #ifndef UARTSTREAM_H_
11 #define UARTSTREAM_H_
12 
13 #include "data/ElementURI.h"
14 #include "io/IOStream.h"
15 #include "logger/Logger.h"
16 #include "utils/Timestamp.h"
17 
18 #include <termios.h>
19 
26 class UartStream: public IOStream
27 {
28 public:
29 
31  typedef enum { B_50 ,// = B50,
32  B_75 ,// = B75,
33  B_110 ,// = B110,
34  B_134 ,// = B134,
35  B_150 ,// = B150,
36  B_200 ,// = B200,
37  B_300 ,// = B300,
38  B_600 ,// = B600,
39  B_1200 ,// = B1200,
40  B_1800 ,// = B1800,
41  B_2400 ,// = B2400,
42  B_4800 ,// = B4800,
43  B_9600 ,// = B9600,
44  B_19200 ,// = B19200,
45  B_38400 ,// = B38400,
46  B_57600 ,// = B57600,
47  B_115200, // = B115200,
48  B_230400, // = B230400,
49  /*B_460800, // = B460800,
50  B_921600, // = B921600, */
51  } Baudrate;
52 
54  typedef enum { NOT_UART, // Non-UART file or device
55  UART1_TX0, // High Speed UART
56  UART2_TX1, // High Speed UART
57  UART3_S1, // Standard UART. Half duplex handshaking available
58  UART4_S2, // Standard UART
59  UART5_S0, // Standard UART
60  UART6_S3, // Standard UART. Half duplex handshaking available
61  UART7_TX2, // High Speed UART
62  UART_A0, // Load controller UART#0
63  UART_A1, // Load controller UART#1
64  UART_A2, // Load controller UART#2
65  UART_A3, // Load controller UART#3
66  UART_A4, // Load controller UART#4
67  UART_A5, // Load controller UART#5
68  UART_A6, // Load controller UART#6
69  UART_A7, // Load controller UART#7
70  UART_B0, // Load controller UART#8
71  UART_B1, // Load controller UART#9
72  UART_B2, // Load controller UART#10
73  UART_B3, // Load controller UART#11
74  UART_B4, // Load controller UART#12
75  UART_B5, // Load controller UART#13
76  UART_B6, // Load controller UART#14
77  UART_B7, // Load controller UART#15
78  } UartId;
79 
81  typedef enum { OK = 0,
90  } UartError;
91 
93  UartStream( const ConfigURI& uartCfg, const ConfigURI& baudCfg, const double timeoutSec,
94  Logger& logger, size_t bufferSize = 1, bool blocking = false );
95 
97  ~UartStream();
98 
100  const char* getName()
101  {
102  return uartIdToString( uartId_ );
103  }
104 
106  {
108  }
109 
111 
113  OutStream& write( const char* data, size_t length = 0xFFFFFFFF );
114 
116  inline UartStream& readLine( char* buf, size_t bufsize )
117  {
118  return readLines( buf, bufsize, 1 );
119  }
120 
122  UartStream& readLines( char* buf, size_t bufsize, size_t lines );
123 
124  // Fills buffer with given number of bytes unless timeout is exceeded
125  InStream& read( char* buf, size_t num );
126 
127  // Fills buffer until character is matched unless timeout is exceeded
128  // Do not read more than num bytes
129  UartStream& readUntil( char* buf, size_t num, const unsigned char match, bool flushUntil = false )
130  {
131  return readUntil( buf, num, ( const char* )&match, 1, flushUntil );
132  }
133 
134  // Fills buffer until character string is matched unless timeout is exceeded
135  // Do not read more than num bytes
136  // If flushUntil is true, flush bytes up until match.
137  UartStream& readUntil( char* buf, size_t num, const char* match, unsigned int matchLen, bool flushUntil = false );
138 
139  // Fills buffer until character string is matched unless timeout is exceeded
140  // Do not read more than num bytes
141  // If flushUntil is true, flush bytes up until match.
142  UartStream& readUntil( char* buf, size_t num, const unsigned char* match, unsigned int matchLen, bool flushUntil = false );
143 
144  // Returns true if the buffer contains the indicated byte
145  size_t canReadUntil( const unsigned char match );
146 
147  // Returns true if the buffer contains the indicated bytes
148  size_t canReadUntil( const char* match, int matchLen = 0, int maxCheck = 0 );
149 
150  // Returns true if the buffer contains the indicated bytes
151  size_t canReadUntil( const unsigned char* match, int matchLen = 0, int maxCheck = 0 );
152 
156 
159  UartStream& flush( int num = -1 );
160 
161  UartStream& close();
162 
163  UartStream& setStartTimeout( const double timeout )
164  {
165  startTimeout_ = timeout;
166  return *this;
167  }
168 
170  {
172  return *this;
173  }
174 
175  const char* uartIdToString( UartId uart );
176 
178  {
179  return lastError_;
180  }
181 
182  bool hasError()
183  {
184  return lastError_ != OK;
185  }
186 
187  const char* errorString();
188 
189  virtual bool eof()
190  {
191  return isReadable();
192  }
193 
194  virtual bool isReadable()
195  {
196  return ( serPortFd_ != -1 );
197  }
198 
199  virtual bool isWritable()
200  {
201  return ( serPortFd_ != -1 );
202  }
203 
204  void waitForBufferEmpty();
205 
206  // Returns number of data bytes in the input buffer after polling the serial port
207  size_t dataAvailable();
208 
209  // Returns number of data bytes in the input buffer
210  size_t dataBuffered();
211 
212  void setDebug( bool debugOn )
213  {
214  debug_ = debugOn;
215  }
216 
217  void enableUART( void );
218 
219  void disableUART( void );
220 
221  void fillInBuffer();
222 
223 private:
224 
225  // Note that the copy constructor below is private and not given a body.
226  // Any attempt to call it will return a compiler error.
227  UartStream( const UartStream& old ); // disallow copy constructor
228 
240  size_t bufferSize_;
242  bool blocking_;
243  bool debug_;
244 
245  void serGetChar( unsigned char* c, int* success, const Timespan& timeout = Timespan::INVALID_TIMESPAN );
247 
248  static Str LookupFilename( const ConfigURI& uartCfg, Logger& logger );
249  static Baudrate LookupBaudrate( const ConfigURI& baudCfg, Logger& logger );
250 
251  const char* uartToDevice( UartId uart );
252  static UartId DeviceToUART( const Str& name );
253  static const speed_t BaudToSpeed( Baudrate baud );
254  static const Timespan SLEEP_TIME;
255 
256 };
257 
258 #endif /*UARTSTREAM_H_*/
259 
Client-side interface for injecting log data into the log queue.
Definition: Logger.h:30
Definition: UartStream.h:43
Definition: UartStream.h:88
char pushedChar_
Definition: UartStream.h:238
virtual bool eof()
Indicates if the end of file has been reached.
Definition: UartStream.h:189
void enableUART(void)
Definition: UartStream.cpp:936
Definition: UartStream.h:59
Definition: UartStream.h:58
static const speed_t BaudToSpeed(Baudrate baud)
Definition: UartStream.cpp:1267
Definition: UartStream.h:63
This is a rather abstract class that can be implemented by classes that want to send and receive stre...
Definition: IOStream.h:30
void initializePort(UartStream::UartId)
Definition: UartStream.cpp:438
virtual bool isReadable()
indicates whether the stream can actually be read from.
Definition: UartStream.h:194
size_t canReadUntil(const unsigned char match)
Definition: UartStream.cpp:256
UartStream & readLine(char *buf, size_t bufsize)
Fills buffer until: buffer size is exceeded, CR, LF, or ETX character is received, or the serial timeout has expired.
Definition: UartStream.h:116
virtual bool isWritable()
indicates whether the stream can actually be written to.
Definition: UartStream.h:199
Definition: UartStream.h:40
Definition: UartStream.h:82
Definition: UartStream.h:57
static Str LookupFilename(const ConfigURI &uartCfg, Logger &logger)
Definition: UartStream.cpp:1315
Logger logger_
Definition: UartStream.h:237
Definition: UartStream.h:77
UartStream::Baudrate defaultBaudrate_
Definition: UartStream.h:232
UartStream::UartError lastError_
Definition: UartStream.h:236
size_t bufferSize_
Definition: UartStream.h:240
Baudrate
Baudrates as per termios.
Definition: UartStream.h:31
UartStream & close()
Definition: UartStream.cpp:225
void disableUART(void)
Definition: UartStream.cpp:981
Definition: UartStream.h:44
UartStream & open()
Definition: UartStream.h:105
static Baudrate LookupBaudrate(const ConfigURI &baudCfg, Logger &logger)
Definition: UartStream.cpp:1322
int bufferPos_
Definition: UartStream.h:241
Definition: UartStream.h:89
Definition: UartStream.h:69
const char * getName()
Returns uart name.
Definition: UartStream.h:100
void waitForBufferEmpty()
Definition: UartStream.cpp:339
Definition: UartStream.h:86
void setDebug(bool debugOn)
Definition: UartStream.h:212
Definition: UartStream.h:33
UartStream::UartError getError()
Definition: UartStream.h:177
UartId
UARTS.
Definition: UartStream.h:54
Timespan class, represents a delta of time.
Definition: Timestamp.h:248
const char * uartIdToString(UartId uart)
Definition: UartStream.cpp:1185
OutStream & write(const char *data, size_t length=0xFFFFFFFF)
If length is unspecified, strlen is used to determine the length.
Definition: UartStream.cpp:199
bool blocking_
Definition: UartStream.h:242
UartStream & readUntil(char *buf, size_t num, const unsigned char match, bool flushUntil=false)
Definition: UartStream.h:129
Definition: UartStream.h:62
Definition: UartStream.h:36
Implements both InStream and OutStream for the LPC3XXX Standard UART.
Definition: UartStream.h:26
Definition: UartStream.h:54
UartStream & setStartTimeout(const double timeout)
Definition: UartStream.h:163
Contains the IOStream and NullIOStream class declarations.
void serGetChar(unsigned char *c, int *success, const Timespan &timeout=Timespan::INVALID_TIMESPAN)
Definition: UartStream.cpp:857
Definition: UartStream.h:55
Definition: ElementURI.h:166
This is a very abstract class that can be implemented by classes that want to receive stream input...
Definition: InStream.h:29
Definition: UartStream.h:39
bool hasError()
Definition: UartStream.h:182
static UartId DeviceToUART(const Str &name)
Definition: UartStream.cpp:1083
Definition: UartStream.h:87
Definition: UartStream.h:61
Str filename_
Definition: UartStream.h:230
Definition: UartStream.h:42
Definition: UartStream.h:37
Definition: UartStream.h:71
UartStream & resetStartTimeout()
Definition: UartStream.h:169
Definition: UartStream.h:74
Definition: UartStream.h:48
Timespan betweenTimeout_
Definition: UartStream.h:235
Definition: UartStream.h:72
Timespan startTimeout_
Definition: UartStream.h:233
Replacement for standard template class string.
Definition: Str.h:12
UartStream & flush(int num=-1)
Flushes any characters out of the buffer until there are no more characters, or if num is positive...
Definition: UartStream.cpp:791
Definition: UartStream.h:66
Definition: UartStream.h:65
UartStream & openAtBaudrate(UartStream::Baudrate baud)
Definition: UartStream.cpp:114
UartError
UART Errors.
Definition: UartStream.h:81
Contains the ElementURI class definition.
Definition: UartStream.h:76
Definition: UartStream.h:56
Definition: UartStream.h:84
void fillInBuffer()
Definition: UartStream.cpp:909
Contains the Logger class definition.
Definition: UartStream.h:47
Definition: UartStream.h:60
Definition: UartStream.h:85
Definition: UartStream.h:83
Definition: UartStream.h:34
size_t dataAvailable()
Definition: UartStream.cpp:236
Definition: UartStream.h:64
InStream & read(char *buf, size_t num)
reads num bytes from stream to the buffer
Definition: UartStream.cpp:573
Definition: UartStream.h:70
Definition: UartStream.h:38
Definition: UartStream.h:31
Definition: UartStream.h:73
Definition: UartStream.h:41
This is a very abstract class that can be implemented by classes that want to send stream output...
Definition: OutStream.h:41
UartStream & flushCRLF()
Flushes CR and LF characters out of the buffer until there are no more characters.
Definition: UartStream.cpp:829
Contains the Timestamp and Timespan class declarations.
Timespan defaultStartTimeout_
Definition: UartStream.h:234
~UartStream()
Destructor.
Definition: UartStream.cpp:102
Definition: UartStream.h:68
char * pushedChars_
Definition: UartStream.h:239
UartId uartId_
Definition: UartStream.h:231
Definition: UartStream.h:67
Definition: UartStream.h:35
const char * uartToDevice(UartId uart)
Definition: UartStream.cpp:1026
Definition: UartStream.h:45
UartStream & readLines(char *buf, size_t bufsize, size_t lines)
Fills buffer with given number of lines (CR/LF) until buffer size is exceeded or timeout has expired ...
Definition: UartStream.cpp:507
Definition: UartStream.h:32
const char * errorString()
Definition: UartStream.cpp:1241
static const Timespan INVALID_TIMESPAN
Constant value to represent an invalid timespan.
Definition: Timestamp.h:252
UartStream(const ConfigURI &uartCfg, const ConfigURI &baudCfg, const double timeoutSec, Logger &logger, size_t bufferSize=1, bool blocking=false)
Constructor.
Definition: UartStream.cpp:72
Definition: UartStream.h:75
Definition: UartStream.h:81
static const Timespan SLEEP_TIME
A 10 millisecond sleep.
Definition: UartStream.h:254
bool debug_
Definition: UartStream.h:243
Definition: UartStream.h:46
size_t dataBuffered()
Definition: UartStream.cpp:246
int serPortFd_
Definition: UartStream.h:229