INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         
port.h
1 /*
2  * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics
3  * http://gearbox.sf.net/
4  * Copyright (c) 2008 Geoffrey Biggs
5  *
6  * flexiport flexible hardware data communications library.
7  *
8  * This distribution is licensed to you under the terms described in the LICENSE file included in
9  * this distribution.
10  *
11  * This work is a product of the National Institute of Advanced Industrial Science and Technology,
12  * Japan. Registration number: H20PRO-881
13  *
14  * This file is part of flexiport.
15  *
16  * flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU
17  * Lesser General Public License as published by the Free Software Foundation, either version 3 of
18  * the License, or (at your option) any later version.
19  *
20  * flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
21  * even the implied warranty of 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 License along with flexiport.
25  * If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef __PORT_H
29 #define __PORT_H
30 
31 #include "unistd.h"
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 
47 #include "flexiport_types.h"
48 #include "timeout.h"
49 
54 namespace flexiport
55 {
56 
79 class FLEXIPORT_EXPORT Port
80 {
81  public:
82  virtual ~Port ();
83 
84  // API common to all ports
86  virtual void Open () = 0;
87 
89  virtual void Close () = 0;
90 
101  virtual ssize_t Read (void * const buffer, size_t count) = 0;
102 
113  virtual ssize_t ReadFull (void * const buffer, size_t count) = 0;
114 
121  virtual ssize_t ReadString (std::string &buffer);
122 
138  virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator);
139 
155  virtual ssize_t ReadStringUntil (std::string &buffer, char terminator);
156 
179  virtual ssize_t ReadLine (char * const buffer, size_t count);
180 
195  virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); }
196 
200  virtual ssize_t Skip (size_t count);
201 
206  virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count);
207 
211  virtual ssize_t BytesAvailable () = 0;
212 
219  virtual ssize_t BytesAvailableWait () = 0;
220 
230  virtual ssize_t Write (const void * const buffer, size_t count) = 0;
231 
238  virtual ssize_t WriteFull (const void * const buffer, size_t count);
239 
249  virtual ssize_t WriteString (const char * const buffer);
250  virtual ssize_t WriteString (const std::string &buffer)
251  { return WriteString (buffer.c_str ()); }
252 
254  virtual void Flush () = 0;
255 
259  virtual void Drain () = 0;
260 
262  virtual std::string GetStatus () const;
263 
264  // Accessor methods.
266  std::string GetPortType () const { return _type; }
268  void SetDebug (int debug) { _debug = debug; }
270  int GetDebug () const { return _debug; }
277  virtual void SetTimeout (Timeout timeout) = 0;
279  virtual Timeout GetTimeout () const { return _timeout; }
282  virtual bool IsBlocking () const { return (_timeout._sec != 0 ||
283  _timeout._usec != 0); }
285  virtual void SetCanRead (bool canRead) = 0;
287  virtual bool CanRead () const { return _canRead; }
289  virtual void SetCanWrite (bool canWrite) = 0;
291  virtual bool CanWrite () const { return _canWrite; }
293  virtual bool IsOpen () const = 0;
294 
295  protected:
296  std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb")
297  unsigned int _debug;
298  Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation.
299  bool _canRead; // If true, this port can be read from.
300  bool _canWrite; // If true, this port can be written to.
301  bool _alwaysOpen; // If the port should be kept open for the life of the object (including
302  // reopening it if necessary).
303 
304  // Protected constructor to prevent direct creation of this class.
305  Port ();
306  // Constructor for more-direct creation.
307  Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen);
308 
309  void ProcessOptions (const std::map<std::string, std::string> &options);
310  virtual bool ProcessOption (const std::string &option, const std::string &value);
311  virtual void CheckPort (bool read) = 0;
312 
313  private:
314  // Private copy constructor to prevent unintended copying.
315  Port (const Port&);
316  void operator= (const Port&);
317 };
318 
319 } // namespace flexiport
320 
323 #endif // __PORT_H
std::string GetPortType() const
Get the port type.
Definition: port.h:266
int GetDebug() const
Get the debug level.
Definition: port.h:270
virtual Timeout GetTimeout() const
Get the timeout.
Definition: port.h:279
virtual bool CanRead() const
Get the read permissions of the port.
Definition: port.h:287
void SetDebug(int debug)
Set the debug level.
Definition: port.h:268
virtual bool IsBlocking() const
Get the blocking property of the port. If the timeout is non-zero, the port will block.
Definition: port.h:282
An object used to represent timeouts.
Definition: timeout.h:62
FlexiPort data communications library.
Base Port class.
Definition: port.h:79
virtual bool CanWrite() const
Get the write permissions of the port.
Definition: port.h:291
virtual ssize_t ReadLine(std::string &buffer)
Read a new-line terminated string of data.
Definition: port.h:195
 

Generated for GearBox by  doxygen 1.4.5