Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FileSystemCommPort.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_FileSystemCommPort_h_
00003 #define INCLUDED_FileSystemCommPort_h_
00004 
00005 #include "local/CommPort.h"
00006 #include "Shared/plist.h"
00007 #include <Wireless/netstream.h>
00008 #include <ios>
00009 
00010 //! Provides CommPort interface to file system devices, such as serial ports
00011 /*! Pass a path to use a file system device, or see NetworkCommPort for a network interface */
00012 class FileSystemCommPort : public CommPort, public virtual plist::PrimitiveListener {
00013 public:
00014   //! constructor (see also sub-class constructor in protected section)
00015   explicit FileSystemCommPort(const std::string& name)
00016     : CommPort(autoRegisterFileSystemCommPort,name),
00017     path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00018     rbuf(), wbuf(), curloc(), curmode(), openedCnt(0)
00019   {
00020     addEntry("Path",path,"Path of file system object being accessed");
00021     addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00022   }
00023   
00024   //! destructor, checks that the file descriptor has already been closed
00025   virtual ~FileSystemCommPort() {
00026     lock.lock();
00027     if(openedCnt>0)
00028       connectionError("File descriptor still open in FileSystemCommPort destructor",true,NULL,false);
00029   }
00030   
00031   virtual std::string getClassName() const { return autoRegisterFileSystemCommPort; }
00032   
00033   virtual basic_netbuf<std::ios::char_type>& getReadStreambuf() { return rbuf; }
00034   virtual basic_netbuf<std::ios::char_type>& getWriteStreambuf() { return wbuf; }
00035   virtual bool isReadable() { return rbuf.is_open(); }
00036   virtual bool isWriteable() { return wbuf.is_open(); }
00037   
00038   //! tries to have #rbuf and/or #wbuf open #path, subject to #mode 
00039   virtual bool open();
00040   
00041   //! closes #rbuf and #wbuf
00042   virtual bool close();
00043   
00044   //! watches #path, triggers a close() and re-open() if it changes
00045   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00046   
00047   plist::Primitive<std::string> path; //!< path of file system object being accessed
00048   plist::Primitive<int> mode; //!< mode bitmask to pass to the open() call (see std::ios_base::openmode)
00049   
00050 protected:
00051   explicit FileSystemCommPort(const std::string& classname, const std::string& instancename)
00052     : CommPort(classname,instancename), 
00053     path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00054     rbuf(), wbuf(), curloc(), curmode(), openedCnt(0)
00055   {
00056     addEntry("Path",path,"Path of file system object being accessed");
00057     addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00058   }
00059   
00060   //! Displays message on stderr and if @a fatal is set, calls closeFD()
00061   /*! Need the useClassName parameter because we can't trust autoRegisterFileSystemCommPort to
00062    *  still be valid during static destruction, so that's false when called from destructor */
00063   virtual void connectionError(const std::string& msg, bool fatal, const char* sysmsg, bool useClassName=true) {
00064     if(useClassName && getClassName()!=instanceName)
00065       std::cerr << getClassName() << " \"" << instanceName << "\": ";
00066     else
00067       std::cerr << instanceName << ": ";
00068     std::cerr << msg;
00069     if(sysmsg!=NULL && sysmsg[0]!='\0')
00070       std::cerr << ": " << sysmsg;
00071     std::cerr << std::endl;
00072     if(fatal && (rbuf.is_open() || wbuf.is_open())) {
00073       openedCnt=1;
00074       close();
00075     }
00076   }
00077   
00078   basic_netbuf<std::ios::char_type> rbuf;
00079   basic_netbuf<std::ios::char_type> wbuf;
00080   std::string curloc;
00081   std::ios_base::openmode curmode;
00082   unsigned int openedCnt;
00083   
00084   //! holds the class name, set via registration with the CommPort registry
00085   static const std::string autoRegisterFileSystemCommPort;
00086 };
00087 
00088 /*! @file
00089  * @brief Describes FileDescriptorMotionHook, which provides a file descriptor interface for hardware "drivers"
00090  * @author Ethan Tira-Thompson (ejt) (Creator)
00091  */
00092 
00093 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:38 2016 by Doxygen 1.6.3