Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FileSystemCommPort.cc

Go to the documentation of this file.
00001 #include "FileSystemCommPort.h"
00002 #include <fcntl.h>
00003 
00004 const std::string FileSystemCommPort::autoRegisterFileSystemCommPort = CommPort::getRegistry().registerType<FileSystemCommPort>("FileSystemCommPort");
00005 
00006 bool FileSystemCommPort::open() {
00007   if(openedCnt++>0)
00008     return true;
00009   path.addPrimitiveListener(this);
00010   mode.addPrimitiveListener(this);
00011   curloc=path;
00012   curmode=static_cast<std::ios_base::openmode>((int)mode);
00013   bool ans=true;
00014   if(curmode & std::ios_base::out) {
00015     int wrmode = O_WRONLY | O_CREAT | O_NOCTTY;
00016     if(curmode & (std::ios_base::app | std::ios_base::ate))
00017       wrmode |= O_APPEND;
00018     if(curmode & std::ios_base::trunc)
00019       wrmode |= O_TRUNC;
00020     int fd = ::open(path.c_str(),wrmode,0x777);
00021     //std::cout << "open write " << fd << std::endl;
00022     if(fd<0) {
00023       connectionError("Could not open '"+path+"' for writing",false,strerror(errno));
00024       ans=false;
00025     } else {
00026       wbuf.adoptFD(fd);
00027     }
00028   }
00029   if(curmode & std::ios_base::in) {
00030     int fd = ::open(path.c_str(),O_RDONLY | O_NOCTTY);
00031     //std::cout << "open read " << fd << std::endl;
00032     if(fd<0) {
00033       connectionError("Could not open '"+path+"' for reading",false,strerror(errno));
00034       ans=false;
00035     } else {
00036       rbuf.adoptFD(fd);
00037     }
00038   }
00039   opened();
00040   return ans;
00041 }
00042 
00043 bool FileSystemCommPort::close() {
00044   if(openedCnt==0)
00045     std::cerr << "Warning: FileSystemCommPort close() without open()" << std::endl;
00046   if(--openedCnt>0)
00047     return false;
00048   closing();
00049   path.removePrimitiveListener(this);
00050   mode.removePrimitiveListener(this);
00051   rbuf.close();
00052   wbuf.close();
00053   return true;
00054 }
00055 
00056 void FileSystemCommPort::plistValueChanged(const plist::PrimitiveBase& pl) {
00057   if(&pl==&path) {
00058     if(path!=curloc) {
00059       unsigned int tmp=openedCnt;
00060       openedCnt=1; // fake out close() and open() to make sure they trigger the action
00061       close();
00062       open();
00063       openedCnt=tmp; // reset original reference count
00064     }
00065   } else if(&pl==&mode) {
00066     if(mode!=curmode) {
00067       std::cerr << "Cannot change access mode while file is open" << std::endl;
00068     }
00069   } else {
00070     std::cerr << "Unhandled value change in " << getClassName() << ": " << pl.get() << std::endl;
00071   }
00072 }
00073 
00074 /*! @file
00075  * @brief 
00076  * @author Ethan Tira-Thompson (ejt) (Creator)
00077  */

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