Flexiport 2.0.0
|
00001 /* Flexiport 00002 * 00003 * Header file for the base Port class. 00004 * 00005 * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp 00006 * RT-Synthesis Research Group 00007 * Intelligent Systems Research Institute, 00008 * National Institute of Advanced Industrial Science and Technology (AIST), 00009 * Japan 00010 * All rights reserved. 00011 * 00012 * This file is part of Flexiport. 00013 * 00014 * Flexiport is free software; you can redistribute it and/or modify it 00015 * under the terms of the GNU Lesser General Public License as published 00016 * by the Free Software Foundation; either version 2.1 of the License, 00017 * or (at your option) any later version. 00018 * 00019 * Flexiport is distributed in the hope that it will be useful, but 00020 * WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * 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 00025 * License along with Flexiport. If not, see 00026 * <http://www.gnu.org/licenses/>. 00027 */ 00028 00029 #ifndef __PORT_H 00030 #define __PORT_H 00031 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/flexiport_types.h> 00048 #include <flexiport/timeout.h> 00049 00054 namespace flexiport 00055 { 00056 00081 class FLEXIPORT_EXPORT Port 00082 { 00083 public: 00084 virtual ~Port (); 00085 00086 // API common to all ports 00088 virtual void Open () = 0; 00089 00091 virtual void Close () = 0; 00092 00107 virtual ssize_t Read (void * const buffer, size_t count) = 0; 00108 00119 virtual ssize_t ReadFull (void * const buffer, size_t count) = 0; 00120 00127 virtual ssize_t ReadString (std::string &buffer); 00128 00146 virtual ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator); 00147 00167 virtual ssize_t ReadStringUntil (std::string &buffer, char terminator); 00168 00197 virtual ssize_t ReadLine (char * const buffer, size_t count); 00198 00217 virtual ssize_t ReadLine (std::string &buffer) { return ReadStringUntil (buffer, '\n'); } 00218 00223 virtual ssize_t Skip (size_t count); 00224 00230 virtual ssize_t SkipUntil (uint8_t terminator, unsigned int count); 00231 00236 virtual ssize_t BytesAvailable () = 0; 00237 00245 virtual ssize_t BytesAvailableWait () = 0; 00246 00257 virtual ssize_t Write (const void * const buffer, size_t count) = 0; 00258 00266 virtual ssize_t WriteFull (const void * const buffer, size_t count); 00267 00278 virtual ssize_t WriteString (const char * const buffer); 00279 virtual ssize_t WriteString (const std::string &buffer) 00280 { return WriteString (buffer.c_str ()); } 00281 00283 virtual void Flush () = 0; 00284 00289 virtual void Drain () = 0; 00290 00292 virtual std::string GetStatus () const; 00293 00294 // Accessor methods. 00296 std::string GetPortType () const { return _type; } 00298 void SetDebug (int debug) { _debug = debug; } 00300 int GetDebug () const { return _debug; } 00308 virtual void SetTimeout (Timeout timeout) = 0; 00310 virtual Timeout GetTimeout () const { return _timeout; } 00313 virtual bool IsBlocking () const { return (_timeout._sec != 0 || 00314 _timeout._usec != 0); } 00316 virtual void SetCanRead (bool canRead) = 0; 00318 virtual bool CanRead () const { return _canRead; } 00320 virtual void SetCanWrite (bool canWrite) = 0; 00322 virtual bool CanWrite () const { return _canWrite; } 00324 virtual bool IsOpen () const = 0; 00325 00326 protected: 00327 std::string _type; // Port type string (e.g. "tcp" or "serial" or "usb") 00328 unsigned int _debug; 00329 Timeout _timeout; // Timeout in milliseconds. Set to zero for non-blocking operation. 00330 bool _canRead; // If true, this port can be read from. 00331 bool _canWrite; // If true, this port can be written to. 00332 bool _alwaysOpen; // If the port should be kept open for the life of the object (including 00333 // reopening it if necessary). 00334 00335 // Protected constructor to prevent direct creation of this class. 00336 Port (); 00337 // Constructor for more-direct creation. 00338 Port (unsigned int debug, Timeout timeout, bool canRead, bool canWrite, bool alwaysOpen); 00339 00340 void ProcessOptions (const std::map<std::string, std::string> &options); 00341 virtual bool ProcessOption (const std::string &option, const std::string &value); 00342 virtual void CheckPort (bool read) = 0; 00343 00344 private: 00345 // Private copy constructor to prevent unintended copying. 00346 Port (const Port&); 00347 void operator= (const Port&); 00348 }; 00349 00350 } // namespace flexiport 00351 00354 #endif // __PORT_H 00355