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_EXPORTS) 00036 #define FLEXIPORT_EXPORT __declspec (dllexport) 00037 #else 00038 #define FLEXIPORT_EXPORT __declspec (dllimport) 00039 #endif 00040 #else 00041 #define FLEXIPORT_EXPORT 00042 #endif 00043 00044 #include "flexiport_types.h" 00045 #include "timeout.h" 00046 00051 namespace flexiport 00052 { 00053 00076 class FLEXIPORT_EXPORT Port 00077 { 00078 public: 00079 virtual ~Port (void); 00080 00081 // API common to all ports 00083 virtual void Open (void) = 0; 00084 00086 virtual void Close (void) = 0; 00087 00098 virtual ssize_t Read (void * const buffer, size_t count) = 0; 00099 00110 virtual ssize_t ReadFull (void * const buffer, size_t count) = 0; 00111 00118 virtual ssize_t ReadString (std::string &buffer); 00119 00135 virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator); 00136 00152 virtual ssize_t ReadStringUntil (std::string &buffer, char terminator); 00153 00176 virtual ssize_t ReadLine (char * const buffer, size_t count); 00177 00192 virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); } 00193 00197 virtual ssize_t Skip (size_t count); 00198 00203 virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count); 00204 00208 virtual ssize_t BytesAvailable (void) = 0; 00209 00216 virtual ssize_t BytesAvailableWait (void) = 0; 00217 00227 virtual ssize_t Write (const void * const buffer, size_t count) = 0; 00228 00235 virtual ssize_t WriteFull (const void * const buffer, size_t count); 00236 00246 virtual ssize_t WriteString (const char * const buffer); 00247 virtual ssize_t WriteString (const std::string &buffer) 00248 { return WriteString (buffer.c_str ()); } 00249 00251 virtual void Flush (void) = 0; 00252 00256 virtual void Drain (void) = 0; 00257 00259 virtual std::string GetStatus (void) const; 00260 00261 // Accessor methods. 00263 std::string GetPortType (void) const { return _type; } 00265 void SetDebug (int debug) { _debug = debug; } 00267 int GetDebug (void) const { return _debug; } 00274 virtual void SetTimeout (Timeout timeout) = 0; 00276 virtual Timeout GetTimeout (void) const { return _timeout; } 00279 virtual bool IsBlocking (void) const { return (_timeout._sec != 0 || 00280 _timeout._usec != 0); } 00282 virtual void SetCanRead (bool canRead) = 0; 00284 virtual bool CanRead (void) const { return _canRead; } 00286 virtual void SetCanWrite (bool canWrite) = 0; 00288 virtual bool CanWrite (void) const { return _canWrite; } 00290 virtual bool IsOpen (void) const = 0; 00291 00292 protected: 00293 std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb") 00294 unsigned int _debug; 00295 Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation. 00296 bool _canRead; // If true, this port can be read from. 00297 bool _canWrite; // If true, this port can be written to. 00298 bool _alwaysOpen; // If the port should be kept open for the life of the object (including 00299 // reopening it if necessary). 00300 00301 // Protected constructor to prevent direct creation of this class. 00302 Port (void); 00303 // Constructor for more-direct creation. 00304 Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen); 00305 00306 void ProcessOptions (const std::map<std::string, std::string> &options); 00307 virtual bool ProcessOption (const std::string &option, const std::string &value); 00308 virtual void CheckPort (bool read) = 0; 00309 00310 private: 00311 // Private copy constructor to prevent unintended copying. 00312 Port (const Port&); 00313 void operator= (const Port&); 00314 }; 00315 00316 } // namespace flexiport 00317 00320 #endif // __PORT_H |