INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
port.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2008 Geoffrey Biggs 00005 * 00006 * flexiport flexible hardware data communications library. 00007 * 00008 * This distribution is licensed to you under the terms described in the LICENSE file included in 00009 * this distribution. 00010 * 00011 * This work is a product of the National Institute of Advanced Industrial Science and Technology, 00012 * Japan. Registration number: H20PRO-881 00013 * 00014 * This file is part of flexiport. 00015 * 00016 * flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU 00017 * Lesser General Public License as published by the Free Software Foundation, either version 3 of 00018 * the License, or (at your option) any later version. 00019 * 00020 * flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 00021 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 * Lesser General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU Lesser General Public License along with flexiport. 00025 * If not, see <http://www.gnu.org/licenses/>. 00026 */ 00027 00028 #ifndef __PORT_H 00029 #define __PORT_H 00030 00031 #include "unistd.h" 00032 #include <string> 00033 #include <map> 00034 00035 #if defined (WIN32) 00036 #if defined (FLEXIPORT_STATIC) 00037 #define FLEXIPORT_EXPORT 00038 #elif defined (FLEXIPORT_EXPORTS) 00039 #define FLEXIPORT_EXPORT __declspec (dllexport) 00040 #else 00041 #define FLEXIPORT_EXPORT __declspec (dllimport) 00042 #endif 00043 #else 00044 #define FLEXIPORT_EXPORT 00045 #endif 00046 00047 #include "flexiport_types.h" 00048 #include "timeout.h" 00049 00054 namespace flexiport 00055 { 00056 00079 class FLEXIPORT_EXPORT Port 00080 { 00081 public: 00082 virtual ~Port (); 00083 00084 // API common to all ports 00086 virtual void Open () = 0; 00087 00089 virtual void Close () = 0; 00090 00101 virtual ssize_t Read (void * const buffer, size_t count) = 0; 00102 00113 virtual ssize_t ReadFull (void * const buffer, size_t count) = 0; 00114 00121 virtual ssize_t ReadString (std::string &buffer); 00122 00138 virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator); 00139 00155 virtual ssize_t ReadStringUntil (std::string &buffer, char terminator); 00156 00179 virtual ssize_t ReadLine (char * const buffer, size_t count); 00180 00195 virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); } 00196 00200 virtual ssize_t Skip (size_t count); 00201 00206 virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count); 00207 00211 virtual ssize_t BytesAvailable () = 0; 00212 00219 virtual ssize_t BytesAvailableWait () = 0; 00220 00230 virtual ssize_t Write (const void * const buffer, size_t count) = 0; 00231 00238 virtual ssize_t WriteFull (const void * const buffer, size_t count); 00239 00249 virtual ssize_t WriteString (const char * const buffer); 00250 virtual ssize_t WriteString (const std::string &buffer) 00251 { return WriteString (buffer.c_str ()); } 00252 00254 virtual void Flush () = 0; 00255 00259 virtual void Drain () = 0; 00260 00262 virtual std::string GetStatus () const; 00263 00264 // Accessor methods. 00266 std::string GetPortType () const { return _type; } 00268 void SetDebug (int debug) { _debug = debug; } 00270 int GetDebug () const { return _debug; } 00277 virtual void SetTimeout (Timeout timeout) = 0; 00279 virtual Timeout GetTimeout () const { return _timeout; } 00282 virtual bool IsBlocking () const { return (_timeout._sec != 0 || 00283 _timeout._usec != 0); } 00285 virtual void SetCanRead (bool canRead) = 0; 00287 virtual bool CanRead () const { return _canRead; } 00289 virtual void SetCanWrite (bool canWrite) = 0; 00291 virtual bool CanWrite () const { return _canWrite; } 00293 virtual bool IsOpen () const = 0; 00294 00295 protected: 00296 std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb") 00297 unsigned int _debug; 00298 Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation. 00299 bool _canRead; // If true, this port can be read from. 00300 bool _canWrite; // If true, this port can be written to. 00301 bool _alwaysOpen; // If the port should be kept open for the life of the object (including 00302 // reopening it if necessary). 00303 00304 // Protected constructor to prevent direct creation of this class. 00305 Port (); 00306 // Constructor for more-direct creation. 00307 Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen); 00308 00309 void ProcessOptions (const std::map<std::string, std::string> &options); 00310 virtual bool ProcessOption (const std::string &option, const std::string &value); 00311 virtual void CheckPort (bool read) = 0; 00312 00313 private: 00314 // Private copy constructor to prevent unintended copying. 00315 Port (const Port&); 00316 void operator= (const Port&); 00317 }; 00318 00319 } // namespace flexiport 00320 00323 #endif // __PORT_H |