nidas  v1.2-1520
UnixIODevice.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_UNIXIODEVICE_H
27 #define NIDAS_CORE_UNIXIODEVICE_H
28 
29 #include "IODevice.h"
33 
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <unistd.h>
39 
40 #ifdef DEBUG
41 #include <iostream>
42 #endif
43 
44 namespace nidas { namespace core {
45 
49 class UnixIODevice : public IODevice {
50 
51 public:
52 
56  UnixIODevice(): _fd(-1) {}
57 
62  UnixIODevice(const std::string& name):_fd(-1)
63  {
64  setName(name);
65  }
66 
71 
75  int getReadFd() const { return _fd; }
76 
80  int getWriteFd() const { return _fd; }
81 
85  void open(int flags) throw(nidas::util::IOException)
86  {
87  if ((_fd = ::open(getName().c_str(),flags)) < 0)
88  throw nidas::util::IOException(getName(),"open",errno);
89  }
90 
94  size_t read(void *buf, size_t len) throw(nidas::util::IOException)
95  {
96  ssize_t result;
97  if ((result = ::read(_fd,buf,len)) < 0) {
98  if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
99  throw nidas::util::IOException(getName(),"read",errno);
100  }
101  if (result == 0)
102  throw nidas::util::EOFException(getName(),"read");
103  return result;
104  }
105 
109  size_t read(void *buf, size_t len, int msecTimeout) throw(nidas::util::IOException);
110 
114  size_t write(const void *buf, size_t len) throw(nidas::util::IOException)
115  {
116  ssize_t result;
117  if ((result = ::write(_fd,buf,len)) < 0) {
118  if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
119  throw nidas::util::IOException(getName(),"write",errno);
120  }
121  return result;
122  }
123 
124  /*
125  * Perform an ioctl on the device. request is an integer
126  * value which must be supported by the device. Normally
127  * this is a value from a header file for the device.
128  * size_t len parameter is not used.
129  */
130  void ioctl(int request, void* buf, size_t) throw(nidas::util::IOException)
131  {
132  if (::ioctl(_fd,request,buf) < 0)
133  throw nidas::util::IOException(getName(),"ioctl",errno);
134  }
135 
139  void close() throw(nidas::util::IOException)
140  {
141  int fd = _fd;
142  _fd = -1;
143  if (fd >= 0 && ::close(fd) < 0)
144  throw nidas::util::IOException(getName(),"close",errno);
145  }
146 
147 protected:
148 
149  int _fd;
150 
151 };
152 
153 }} // namespace nidas namespace core
154 
155 #endif
void ioctl(int request, void *buf, size_t)
Definition: UnixIODevice.h:130
virtual void setName(const std::string &val)
Set the device name to be opened for this sensor.
Definition: IODevice.h:52
void close()
close the device
Definition: UnixIODevice.h:139
UnixIODevice(const std::string &name)
Constructor, passing the name of the device.
Definition: UnixIODevice.h:62
size_t read(void *buf, size_t len)
Read from the device.
Definition: UnixIODevice.h:94
~UnixIODevice()
Destructor.
Definition: UnixIODevice.h:70
virtual const std::string & getName() const
Definition: IODevice.h:57
size_t write(const void *buf, size_t len)
Write to the device.
Definition: UnixIODevice.h:114
int _fd
Definition: UnixIODevice.h:149
An interface to an IO device.
Definition: IODevice.h:41
A basic Unix I/O device, such as a named pipe, or a watched file.
Definition: UnixIODevice.h:49
int len
Definition: sing.cc:934
int fd
Definition: twod.c:56
UnixIODevice()
Constructor.
Definition: UnixIODevice.h:56
Definition: IOException.h:37
int getReadFd() const
The file descriptor used when reading from this device.
Definition: UnixIODevice.h:75
int getWriteFd() const
The file descriptor used when writing to this device.
Definition: UnixIODevice.h:80
void open(int flags)
open the device.
Definition: UnixIODevice.h:85
Definition: EOFException.h:34