Flexiport  2.0.0
port.h
Go to the documentation of this file.
1 /* Flexiport
2  *
3  * Header file for the base Port class.
4  *
5  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
6  * RT-Synthesis Research Group
7  * Intelligent Systems Research Institute,
8  * National Institute of Advanced Industrial Science and Technology (AIST),
9  * Japan
10  * All rights reserved.
11  *
12  * This file is part of Flexiport.
13  *
14  * Flexiport is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation; either version 2.1 of the License,
17  * or (at your option) any later version.
18  *
19  * Flexiport is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with Flexiport. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #ifndef __PORT_H
30 #define __PORT_H
31 
32 #include <string>
33 #include <map>
34 
35 #if defined (WIN32)
36  #if defined (FLEXIPORT_STATIC)
37  #define FLEXIPORT_EXPORT
38  #elif defined (flexiport_EXPORTS)
39  #define FLEXIPORT_EXPORT __declspec (dllexport)
40  #else
41  #define FLEXIPORT_EXPORT __declspec (dllimport)
42  #endif
43 #else
44  #define FLEXIPORT_EXPORT
45 #endif
46 
48 #include <flexiport/timeout.h>
49 
54 namespace flexiport
55 {
56 
82 {
83  public:
84  virtual ~Port ();
85 
86  // API common to all ports
88  virtual void Open () = 0;
89 
91  virtual void Close () = 0;
92 
107  virtual ssize_t Read (void * const buffer, size_t count) = 0;
108 
119  virtual ssize_t ReadFull (void * const buffer, size_t count) = 0;
120 
127  virtual ssize_t ReadString (std::string &buffer);
128 
146  virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator);
147 
167  virtual ssize_t ReadStringUntil (std::string &buffer, char terminator);
168 
197  virtual ssize_t ReadLine (char * const buffer, size_t count);
198 
217  virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); }
218 
223  virtual ssize_t Skip (size_t count);
224 
230  virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count);
231 
236  virtual ssize_t BytesAvailable () = 0;
237 
245  virtual ssize_t BytesAvailableWait () = 0;
246 
257  virtual ssize_t Write (const void * const buffer, size_t count) = 0;
258 
266  virtual ssize_t WriteFull (const void * const buffer, size_t count);
267 
278  virtual ssize_t WriteString (const char * const buffer);
279  virtual ssize_t WriteString (const std::string &buffer)
280  { return WriteString (buffer.c_str ()); }
281 
283  virtual void Flush () = 0;
284 
289  virtual void Drain () = 0;
290 
292  virtual std::string GetStatus () const;
293 
294  // Accessor methods.
296  std::string GetPortType () const { return _type; }
298  void SetDebug (int debug) { _debug = debug; }
300  int GetDebug () const { return _debug; }
308  virtual void SetTimeout (Timeout timeout) = 0;
310  virtual Timeout GetTimeout () const { return _timeout; }
313  virtual bool IsBlocking () const { return (_timeout._sec != 0 ||
314  _timeout._usec != 0); }
316  virtual void SetCanRead (bool canRead) = 0;
318  virtual bool CanRead () const { return _canRead; }
320  virtual void SetCanWrite (bool canWrite) = 0;
322  virtual bool CanWrite () const { return _canWrite; }
324  virtual bool IsOpen () const = 0;
325 
326  protected:
327  std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb")
328  unsigned int _debug;
329  Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation.
330  bool _canRead; // If true, this port can be read from.
331  bool _canWrite; // If true, this port can be written to.
332  bool _alwaysOpen; // If the port should be kept open for the life of the object (including
333  // reopening it if necessary).
334 
335  // Protected constructor to prevent direct creation of this class.
336  Port ();
337  // Constructor for more-direct creation.
338  Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen);
339 
340  void ProcessOptions (const std::map<std::string, std::string> &options);
341  virtual bool ProcessOption (const std::string &option, const std::string &value);
342  virtual void CheckPort (bool read) = 0;
343 
344  private:
345  // Private copy constructor to prevent unintended copying.
346  Port (const Port&);
347  void operator= (const Port&);
348 };
349 
350 } // namespace flexiport
351 
354 #endif // __PORT_H
355 
bool _alwaysOpen
Definition: port.h:332
bool _canWrite
Definition: port.h:331
#define FLEXIPORT_EXPORT
Definition: port.h:44
unsigned int _debug
Definition: port.h:328
int GetDebug() const
Get the debug level.
Definition: port.h:300
void SetDebug(int debug)
Set the debug level.
Definition: port.h:298
virtual ssize_t ReadLine(std::string &buffer)
Read a new-line terminated string of data.
Definition: port.h:217
virtual bool CanRead() const
Get the read permissions of the port.
Definition: port.h:318
virtual bool IsBlocking() const
Get the blocking property of the port.
Definition: port.h:313
virtual Timeout GetTimeout() const
Get the timeout.
Definition: port.h:310
Base Port class.
Definition: port.h:81
virtual bool CanWrite() const
Get the write permissions of the port.
Definition: port.h:322
Timeout _timeout
Definition: port.h:329
bool _canRead
Definition: port.h:330
virtual ssize_t WriteString(const std::string &buffer)
Definition: port.h:279
std::string GetPortType() const
Get the port type.
Definition: port.h:296
std::string _type
Definition: port.h:327
An object used to represent timeouts.
Definition: timeout.h:63