/[projects]/smsdaemon/SerialPort.h
ViewVC logotype

Annotation of /smsdaemon/SerialPort.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations) (download)
Mon Jun 9 18:15:53 2008 UTC (15 years, 11 months ago) by torben
File MIME type: text/plain
File size: 14048 byte(s)
Added first basic edition of smsdaemon.

So far sending & receiving sms works and a basic sample plugin is implemented.

1 torben 26 /***************************************************************************
2     * Copyright (C) 2004 by Manish Pagey *
3     * crayzeewulf@users.sourceforge.net
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 General Public License *
16     * along with this program; if not, write to the *
17     * Free Software Foundation, Inc., *
18     * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19     ***************************************************************************/
20     #ifndef _SerialPort_h_
21     #define _SerialPort_h_
22    
23    
24     #include <string>
25     #include <vector>
26     #include <stdexcept>
27     #include <termios.h>
28    
29    
30     /**
31     *
32     * @note This class attaches a handler to the SIGIO signal to detect
33     * the data arriving at a serial port. However, this signal handler
34     * will also call any signal handler that is already attached to
35     * this signal. However, if other parts of the application attach a
36     * signal handler to SIGIO after constructing an instance of SIGIO,
37     * they must ensure that they call the existing signal handler.
38     * Otherwise, it may not be possible to receive any data through
39     * the serial port using this class.
40     *
41     * :FIXME: Provide examples of the above potential problem.
42     *
43     * @todo The current implementation does not check if another process has
44     * locked the serial port device and does not lock the serial port device after
45     * opening it. This has been observed to cause problems while using this
46     * library while other programs such as minicom are also accessing the same device.
47     * It will be useful to lock the serial port device when it is being used by
48     * this class.
49     */
50     class SerialPort
51     {
52     public:
53     /**
54     * The allowed set of baud rates.
55     */
56     enum BaudRate {
57     BAUD_50 = B50,
58     BAUD_75 = B75,
59     BAUD_110 = B110,
60     BAUD_134 = B134,
61     BAUD_150 = B150,
62     BAUD_200 = B200,
63     BAUD_300 = B300,
64     BAUD_600 = B600,
65     BAUD_1200 = B1200,
66     BAUD_1800 = B1800,
67     BAUD_2400 = B2400,
68     BAUD_4800 = B4800,
69     BAUD_9600 = B9600,
70     BAUD_19200 = B19200,
71     BAUD_38400 = B38400,
72     BAUD_57600 = B57600,
73     BAUD_115200 = B115200,
74     BAUD_230400 = B230400,
75     //
76     // Bug#1318912
77     // B460800 is defined on Linux but not on Mac OS X. What about other
78     // operating systems ?
79     //
80     #ifdef __linux__
81     BAUD_460800 = B460800,
82     #endif
83     BAUD_DEFAULT = BAUD_57600
84     } ;
85    
86     enum CharacterSize {
87     CHAR_SIZE_5 = CS5, //!< 5 bit characters.
88     CHAR_SIZE_6 = CS6, //!< 6 bit characters.
89     CHAR_SIZE_7 = CS7, //!< 7 bit characters.
90     CHAR_SIZE_8 = CS8, //!< 8 bit characters.
91     CHAR_SIZE_DEFAULT = CHAR_SIZE_8
92     } ;
93    
94     enum StopBits {
95     STOP_BITS_1, //! 1 stop bit.
96     STOP_BITS_2, //! 2 stop bits.
97     STOP_BITS_DEFAULT = STOP_BITS_1
98     } ;
99    
100     enum Parity {
101     PARITY_EVEN, //!< Even parity.
102     PARITY_ODD, //!< Odd parity.
103     PARITY_NONE, //!< No parity i.e. parity checking disabled.
104     PARITY_DEFAULT = PARITY_NONE
105     } ;
106    
107     enum FlowControl {
108     FLOW_CONTROL_HARD,
109     // FLOW_CONTROL_SOFT,
110     FLOW_CONTROL_NONE,
111     FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
112     } ;
113    
114     class NotOpen : public std::logic_error
115     {
116     public:
117     NotOpen(const std::string& whatArg) :
118     logic_error(whatArg) { }
119     } ;
120    
121     class OpenFailed : public std::runtime_error
122     {
123     public:
124     OpenFailed(const std::string& whatArg) :
125     runtime_error(whatArg) { }
126     } ;
127    
128     class AlreadyOpen : public std::logic_error
129     {
130     public:
131     AlreadyOpen( const std::string& whatArg ) :
132     logic_error(whatArg) { }
133     } ;
134    
135     class UnsupportedBaudRate : public std::runtime_error
136     {
137     public:
138     UnsupportedBaudRate( const std::string& whatArg ) :
139     runtime_error(whatArg) { }
140     } ;
141    
142     class ReadTimeout : public std::runtime_error
143     {
144     public:
145     ReadTimeout() : runtime_error( "Read timeout" ) { }
146     } ;
147    
148     /**
149     * Constructor for a serial port.
150     */
151     explicit SerialPort( const std::string& serialPortName ) ;
152    
153     /**
154     * Destructor.
155     */
156     ~SerialPort() throw() ;
157    
158     /**
159     * Open the serial port with the specified settings. A serial port
160     * cannot be used till it is open.
161     *
162     * @throw AlreadyOpen This exception is thrown if the serial port
163     * is already open.
164     *
165     * @throw OpenFailed This exception is thrown if the serial port
166     * could not be opened.
167     *
168     * @throw std::invalid_argument This exception is thrown if an
169     * invalid parameter value is specified.
170     */
171     void
172     Open( const BaudRate baudRate = BAUD_DEFAULT,
173     const CharacterSize charSize = CHAR_SIZE_DEFAULT,
174     const Parity parityType = PARITY_DEFAULT,
175     const StopBits stopBits = STOP_BITS_DEFAULT,
176     const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
177     throw( AlreadyOpen,
178     OpenFailed,
179     UnsupportedBaudRate,
180     std::invalid_argument ) ;
181    
182     /**
183     * Check if the serial port is open for I/O.
184     */
185     bool
186     IsOpen() const ;
187    
188     /**
189     * Close the serial port. All settings of the serial port will be
190     * lost and no more I/O can be performed on the serial port.
191     *
192     * @throw NotOpen Thrown if this method is called while the serial
193     * port is not open.
194     *
195     */
196     void
197     Close()
198     throw(NotOpen) ;
199    
200     /**
201     * Set the baud rate for the serial port to the specified value
202     * (baudRate).
203     *
204     * @throw NotOpen Thrown if this method is called while the serial
205     * port is not open.
206     *
207     * @throw std::invalid_argument Thrown if an invalid baud rate is
208     * specified.
209     */
210     void
211     SetBaudRate( const BaudRate baudRate )
212     throw( UnsupportedBaudRate,
213     NotOpen,
214     std::invalid_argument ) ;
215    
216     /**
217     * Get the current baud rate for the serial port.
218     *
219     * @throw NotOpen Thrown if this method is called while the serial
220     * port is not open.
221     */
222     BaudRate
223     GetBaudRate() const
224     throw( NotOpen,
225     std::runtime_error ) ;
226    
227     /**
228     * Set the character size for the serial port.
229     *
230     * @throw NotOpen Thrown if this method is called while the serial
231     * port is not open.
232     *
233     * @throw std::invalid_argument Thrown if an invalid character
234     * size is specified.
235     */
236     void
237     SetCharSize( const CharacterSize charSize )
238     throw( NotOpen,
239     std::invalid_argument ) ;
240     /**
241     * Get the current character size for the serial port.
242     *
243     * @throw NotOpen Thrown if this method is called while the serial
244     * port is not open.
245     *
246     */
247     CharacterSize
248     GetCharSize() const
249     throw(NotOpen) ;
250    
251     /**
252     * Set the parity type for the serial port.
253     *
254     * @throw NotOpen Thrown if this method is called while the serial
255     * port is not open.
256     *
257     * @throw std::invalid_argument Thrown if an invalid parity is
258     * specified.
259     */
260     void
261     SetParity( const Parity parityType )
262     throw( NotOpen,
263     std::invalid_argument ) ;
264    
265     /**
266     * Get the parity type for the serial port.
267     *
268     * @throw NotOpen Thrown if this method is called while the serial
269     * port is not open.
270     *
271     */
272     Parity
273     GetParity() const
274     throw(NotOpen) ;
275    
276     /**
277     * Set the number of stop bits to be used with the serial port.
278     *
279     * @throw NotOpen Thrown if this method is called while the serial
280     * port is not open.
281     *
282     * @throw std::invalid_argument Thrown if an invalid number of
283     * stop bits is specified.
284     */
285     void
286     SetNumOfStopBits( const StopBits numOfStopBits )
287     throw( NotOpen,
288     std::invalid_argument ) ;
289    
290     /**
291     * Get the number of stop bits currently being used by the serial
292     * port.
293     *
294     * @throw NotOpen Thrown if this method is called while the serial
295     * port is not open.
296     *
297     */
298     StopBits
299     GetNumOfStopBits() const
300     throw(NotOpen) ;
301    
302     /**
303     * Set flow control.
304     *
305     * @throw NotOpen Thrown if this method is called while the serial
306     * port is not open.
307     *
308     * @throw std::invalid_argument Thrown if an invalid flow control
309     * is specified.
310     */
311     void
312     SetFlowControl( const FlowControl flowControl )
313     throw( NotOpen,
314     std::invalid_argument ) ;
315    
316     /**
317     * Get the current flow control setting.
318     *
319     * @throw NotOpen Thrown if this method is called while the serial
320     * port is not open.
321     *
322     */
323     FlowControl
324     GetFlowControl() const
325     throw( NotOpen ) ;
326    
327     /**
328     * Check if data is available at the input of the serial port.
329     *
330     * @throw NotOpen Thrown if this method is called while the serial
331     * port is not open.
332     *
333     */
334     bool
335     IsDataAvailable() const
336     throw(NotOpen) ;
337    
338     /**
339     * Read a single byte from the serial port. If no data is
340     * available in the specified number of milliseconds (msTimeout),
341     * then this method will throw ReadTimeout exception. If msTimeout
342     * is 0, then this method will block till data is available.
343     */
344     unsigned char
345     ReadByte( const unsigned int msTimeout = 0 )
346     throw( NotOpen,
347     ReadTimeout,
348     std::runtime_error ) ;
349    
350     /**
351     * Read the specified number of bytes from the serial port. The
352     * method will timeout if no data is received in the specified
353     * number of milliseconds (msTimeout). If msTimeout is 0, then
354     * this method will block till all requested bytes are
355     * received. If numOfBytes is zero, then this method will keep
356     * reading data till no more data is available at the serial
357     * port. In all cases, all read data is available in dataBuffer on
358     * return from this method.
359     */
360     typedef std::vector<unsigned char> DataBuffer ;
361     void
362     Read( DataBuffer& dataBuffer,
363     const unsigned int numOfBytes = 0,
364     const unsigned int msTimeout = 0 )
365     throw( NotOpen,
366     ReadTimeout,
367     std::runtime_error ) ;
368    
369    
370     /**
371     * Read a line of characters from the serial port.
372     */
373     const std::string
374     ReadLine( const unsigned int msTimeout = 0,
375     const char lineTerminator = '\n' )
376     throw( NotOpen,
377     ReadTimeout,
378     std::runtime_error ) ;
379    
380     /**
381     * Send a single byte to the serial port.
382     *
383     * @throw NotOpen Thrown if this method is called while the serial
384     * port is not open.
385     */
386     void
387     WriteByte(const unsigned char dataByte)
388     throw( NotOpen,
389     std::runtime_error ) ;
390    
391     /**
392     * Write the data from the specified vector to the serial port.
393     */
394     void
395     Write(const DataBuffer& dataBuffer)
396     throw( NotOpen,
397     std::runtime_error ) ;
398    
399     /**
400     * Write a string to the serial port.
401     */
402     void
403     Write(const std::string& dataString)
404     throw( NotOpen,
405     std::runtime_error ) ;
406    
407     /**
408     * Set the DTR line to the specified value.
409     */
410     void
411     SetDtr( const bool dtrState = true )
412     throw( NotOpen,
413     std::runtime_error ) ;
414    
415     /**
416     * Get the status of the DTR line.
417     */
418     bool
419     GetDtr() const
420     throw( NotOpen,
421     std::runtime_error ) ;
422    
423     /**
424     * Set the RTS line to the specified value.
425     */
426     void
427     SetRts( const bool rtsState = true )
428     throw( NotOpen,
429     std::runtime_error ) ;
430    
431     /**
432     * Get the status of the RTS line.
433     */
434     bool
435     GetRts() const
436     throw( NotOpen,
437     std::runtime_error ) ;
438    
439     //void
440     //SetCts( const bool ctsState = true )
441     // throw( NotOpen,
442     // std::runtime_error ) ;
443    
444     bool
445     GetCts() const
446     throw( NotOpen,
447     std::runtime_error ) ;
448    
449     //void
450     //SetDsr( const bool dsrState = true )
451     // throw( NotOpen,
452     // std::runtime_error ) ;
453    
454     bool
455     GetDsr() const
456     throw( NotOpen,
457     std::runtime_error ) ;
458     private:
459     /**
460     * Prevent copying of objects of this class by declaring the copy
461     * constructor private. This method is never defined.
462     */
463     SerialPort( const SerialPort& otherSerialPort ) ;
464    
465     /**
466     * Prevent copying of objects of this class by declaring the assignment
467     * operator private. This method is never defined.
468     */
469     SerialPort& operator=(const SerialPort& otherSerialPort ) ;
470    
471     /*
472     * Forward declaration of the implementation class folowing the
473     * PImpl idiom.
474     */
475     class SerialPortImpl ;
476    
477     /**
478     * Pointer to implementation class instance.
479     */
480     SerialPortImpl* mSerialPortImpl ;
481     } ;
482    
483     #endif // #ifndef _SerialPort_h_

  ViewVC Help
Powered by ViewVC 1.1.20