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 <string> 00032 #include <map> 00033 00034 #if defined (WIN32) 00035 #if defined (FLEXIPORT_STATIC) 00036 #define FLEXIPORT_EXPORT 00037 #elif defined (FLEXIPORT_EXPORTS) 00038 #define FLEXIPORT_EXPORT __declspec (dllexport) 00039 #else 00040 #define FLEXIPORT_EXPORT __declspec (dllimport) 00041 #endif 00042 #else 00043 #define FLEXIPORT_EXPORT 00044 #endif 00045 00046 #include "flexiport_types.h" 00047 #include "timeout.h" 00048 00053 namespace flexiport 00054 { 00055 00078 class FLEXIPORT_EXPORT Port 00079 { 00080 public: 00081 virtual ~Port (); 00082 00083 // API common to all ports 00085 virtual void Open () = 0; 00086 00088 virtual void Close () = 0; 00089 00100 virtual ssize_t Read (void * const buffer, size_t count) = 0; 00101 00112 virtual ssize_t ReadFull (void * const buffer, size_t count) = 0; 00113 00120 virtual ssize_t ReadString (std::string &buffer); 00121 00137 virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator); 00138 00154 virtual ssize_t ReadStringUntil (std::string &buffer, char terminator); 00155 00178 virtual ssize_t ReadLine (char * const buffer, size_t count); 00179 00194 virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); } 00195 00199 virtual ssize_t Skip (size_t count); 00200 00205 virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count); 00206 00210 virtual ssize_t BytesAvailable () = 0; 00211 00218 virtual ssize_t BytesAvailableWait () = 0; 00219 00229 virtual ssize_t Write (const void * const buffer, size_t count) = 0; 00230 00237 virtual ssize_t WriteFull (const void * const buffer, size_t count); 00238 00248 virtual ssize_t WriteString (const char * const buffer); 00249 virtual ssize_t WriteString (const std::string &buffer) 00250 { return WriteString (buffer.c_str ()); } 00251 00253 virtual void Flush () = 0; 00254 00258 virtual void Drain () = 0; 00259 00261 virtual std::string GetStatus () const; 00262 00263 // Accessor methods. 00265 std::string GetPortType () const { return _type; } 00267 void SetDebug (int debug) { _debug = debug; } 00269 int GetDebug () const { return _debug; } 00276 virtual void SetTimeout (Timeout timeout) = 0; 00278 virtual Timeout GetTimeout () const { return _timeout; } 00281 virtual bool IsBlocking () const { return (_timeout._sec != 0 || 00282 _timeout._usec != 0); } 00284 virtual void SetCanRead (bool canRead) = 0; 00286 virtual bool CanRead () const { return _canRead; } 00288 virtual void SetCanWrite (bool canWrite) = 0; 00290 virtual bool CanWrite () const { return _canWrite; } 00292 virtual bool IsOpen () const = 0; 00293 00294 protected: 00295 std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb") 00296 unsigned int _debug; 00297 Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation. 00298 bool _canRead; // If true, this port can be read from. 00299 bool _canWrite; // If true, this port can be written to. 00300 bool _alwaysOpen; // If the port should be kept open for the life of the object (including 00301 // reopening it if necessary). 00302 00303 // Protected constructor to prevent direct creation of this class. 00304 Port (); 00305 // Constructor for more-direct creation. 00306 Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen); 00307 00308 void ProcessOptions (const std::map<std::string, std::string> &options); 00309 virtual bool ProcessOption (const std::string &option, const std::string &value); 00310 virtual void CheckPort (bool read) = 0; 00311 00312 private: 00313 // Private copy constructor to prevent unintended copying. 00314 Port (const Port&); 00315 void operator= (const Port&); 00316 }; 00317 00318 } // namespace flexiport 00319 00322 #endif // __PORT_H |