nidas  v1.2-1520
IODevice.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 #ifndef NIDAS_CORE_IODEVICE_H
27 #define NIDAS_CORE_IODEVICE_H
28 
30 #include <nidas/util/IOException.h>
31 
32 #include <sys/ioctl.h>
33 
34 namespace nidas { namespace core {
35 
41 class IODevice
42 {
43 public:
44 
46 
47  virtual ~IODevice() {}
48 
52  virtual void setName(const std::string& val)
53  {
54  _devname = val;
55  }
56 
57  virtual const std::string& getName() const
58  {
59  return _devname;
60  }
61 
67  virtual void open(int flags)
69 
73  virtual int getReadFd() const = 0;
74 
78  virtual int getWriteFd() const = 0;
79 
83  virtual size_t read(void *buf, size_t len) throw(nidas::util::IOException) = 0;
84 
88  virtual size_t read(void *buf, size_t len,int msecTimeout) throw(nidas::util::IOException) = 0;
89 
114  virtual size_t getBytesAvailable() const throw(nidas::util::IOException)
115  {
116  int nbytes;
117  int err = ::ioctl(getReadFd(),FIONREAD,&nbytes);
118  if (err < 0)
119  throw nidas::util::IOException(getName(),"ioctl FIONREAD",errno);
120  return nbytes;
121  }
122 
126  virtual size_t write(const void *buf, size_t len) throw(nidas::util::IOException) = 0;
127 
128  /*
129  * Perform an ioctl on the device. request is an integer
130  * value which must be supported by the device. Normally
131  * this is a value from a header file for the device.
132  */
133  virtual void ioctl(int request, void* buf, size_t len)
134  throw(nidas::util::IOException) = 0;
135 
139  virtual void close() throw(nidas::util::IOException) = 0;
140 
144  // bool reopenOnIOException() const { return false; }
145 
146 private:
147 
148  std::string _devname;
149 
153  IODevice(const IODevice&);
154 
158  IODevice& operator=(const IODevice&);
159 
160 
161 };
162 
163 }} // namespace nidas namespace core
164 
165 #endif
IODevice()
Definition: IODevice.h:45
virtual void setName(const std::string &val)
Set the device name to be opened for this sensor.
Definition: IODevice.h:52
virtual int getWriteFd() const =0
The file descriptor used when writing to this sensor.
virtual size_t write(const void *buf, size_t len)=0
Write to the sensor.
#define err(format, arg...)
Definition: ck_lams.cc:55
virtual const std::string & getName() const
Definition: IODevice.h:57
virtual ~IODevice()
Definition: IODevice.h:47
An interface to an IO device.
Definition: IODevice.h:41
virtual size_t read(void *buf, size_t len)=0
Read from the sensor.
virtual int getReadFd() const =0
The file descriptor used when reading from this sensor.
int len
Definition: sing.cc:934
Definition: IOException.h:37
std::string _devname
Whether to reopen this sensor on an IOException.
Definition: IODevice.h:148
virtual size_t getBytesAvailable() const
Return how many bytes are available to read on this IODevice.
Definition: IODevice.h:114
virtual void open(int flags)=0
Open the device.
virtual void ioctl(int request, void *buf, size_t len)=0
virtual void close()=0
Close the device.
Definition: InvalidParameterException.h:35