nidas v1.2.3
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
44namespace nidas { namespace core {
45
49class UnixIODevice : public IODevice {
50
51public:
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
87 void open(int flags)
88 {
89 if ((_fd = ::open(getName().c_str(),flags)) < 0)
90 throw nidas::util::IOException(getName(),"open",errno);
91 }
92
98 size_t read(void *buf, size_t len)
99 {
100 ssize_t result;
101 if ((result = ::read(_fd,buf,len)) < 0) {
102 if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
103 throw nidas::util::IOException(getName(),"read",errno);
104 }
105 if (result == 0)
106 throw nidas::util::EOFException(getName(),"read");
107 return result;
108 }
109
115 size_t read(void *buf, size_t len, int msecTimeout);
116
122 size_t write(const void *buf, size_t len)
123 {
124 ssize_t result;
125 if ((result = ::write(_fd,buf,len)) < 0) {
126 if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
127 throw nidas::util::IOException(getName(),"write",errno);
128 }
129 return result;
130 }
131
132 /*
133 * Perform an ioctl on the device. request is an integer
134 * value which must be supported by the device. Normally
135 * this is a value from a header file for the device.
136 * size_t len parameter is not used.
137 *
138 * @throws nidas::util::IOException
139 */
140 void ioctl(int request, void* buf, size_t)
141 {
142 if (::ioctl(_fd,request,buf) < 0)
143 throw nidas::util::IOException(getName(),"ioctl",errno);
144 }
145
151 void close()
152 {
153 int fd = _fd;
154 _fd = -1;
155 if (fd >= 0 && ::close(fd) < 0)
156 throw nidas::util::IOException(getName(),"close",errno);
157 }
158
159protected:
160
161 int _fd;
162
163};
164
165}} // namespace nidas namespace core
166
167#endif
An interface to an IO device.
Definition IODevice.h:42
virtual const std::string & getName() const
Definition IODevice.h:57
virtual void setName(const std::string &val)
Set the device name to be opened for this sensor.
Definition IODevice.h:52
A basic Unix I/O device, such as a named pipe, or a watched file.
Definition UnixIODevice.h:49
~UnixIODevice()
Destructor.
Definition UnixIODevice.h:70
int _fd
Definition UnixIODevice.h:161
int getWriteFd() const
The file descriptor used when writing to this device.
Definition UnixIODevice.h:80
size_t read(void *buf, size_t len)
Read from the device.
Definition UnixIODevice.h:98
void ioctl(int request, void *buf, size_t)
Definition UnixIODevice.h:140
size_t write(const void *buf, size_t len)
Write to the device.
Definition UnixIODevice.h:122
UnixIODevice()
Constructor.
Definition UnixIODevice.h:56
int getReadFd() const
The file descriptor used when reading from this device.
Definition UnixIODevice.h:75
void close()
close the device
Definition UnixIODevice.h:151
void open(int flags)
open the device.
Definition UnixIODevice.h:87
UnixIODevice(const std::string &name)
Constructor, passing the name of the device.
Definition UnixIODevice.h:62
Definition EOFException.h:34
Definition IOException.h:37
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition Sample.cc:70
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int len
Definition sing.cc:948
int fd
Definition twod.c:56