DevSlashLirc
LircDevice.cpp
Go to the documentation of this file.
1 #include "LircDevice.h"
2 #include "Mode2LircDevice.h"
3 #include "LircCodeLircDevice.h"
4 
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <unistd.h>
10 #include <sys/ioctl.h>
11 
12 const char *LircDevice::defaultFilename = "/dev/lirc0";
13 const char *LircDevice::version = "LircDevice " VERSION;
14 
15 static const int IOCTL_OK = 0;
16 static const int IOCTL_ERROR = -1;
17 
18 LircDevice::LircDevice(const char *path, milliseconds_t beginTimeout_) : valid(false),fileName(path),fileDescriptor(-1),beginTimeout(beginTimeout_) {
19 }
20 
22  fileDescriptor = ::open(fileName, O_RDWR);
23  if (fileDescriptor < 0) {
24  std::cerr << "Error: Cannot open " << fileName << std::endl;
25  return false;
26  }
27 
28  struct stat s;
29  int status = fstat(fileDescriptor, &s);
30  if (status == IOCTL_ERROR) {
31  std::cerr << "Error: Cannot stat " << fileName << std::endl;
32  return false;
33  }
34 
35  if (!S_ISCHR(s.st_mode)) {
36  std::cerr << "Error: " << fileName << " is not a character device" << std::endl;
37  return false;
38  }
39 
40  // man lirc(4) says:
41  // "If a device returns an error code for LIRC_GET_REC_MODE, it is safe to
42  // assume it is not a lirc device."
43  // Therefore the following extremely bizarre code
44  status = ::ioctl(fileDescriptor, LIRC_GET_REC_MODE, &recordingMode);
45  if (status != IOCTL_OK)
46  recordingMode = LIRC_MODE_MODE2;
47 
48  if ((recordingMode != LIRC_MODE_MODE2) && (recordingMode != LIRC_MODE_LIRCCODE)) {
49  std::cerr << "This cannot happen!" << std::endl;
50  return false;
51  }
52 
53  status = ::ioctl(fileDescriptor, LIRC_GET_FEATURES, &features);
54  if (status != IOCTL_OK) {
55  std::cerr << "Error: " << status << std::endl;
56  return false;
57  }
58 
61  : canSend() ? 1
62  : 0;
63 
64  valid = true;
65  return true;
66 }
67 
69 valid(orig.valid),
70 fileName(orig.fileName),
71 fileDescriptor(orig.fileDescriptor),
72 numberTransmitters(orig.numberTransmitters),
73 recordingMode(orig.recordingMode),
74 features(orig.features),
75 beginTimeout(orig.beginTimeout) {
76 }
77 
79  close();
80 }
81 
83  if (fileDescriptor < 0)
84  return false;
85 
86  int status = ::close(fileDescriptor);
87  if (status != IOCTL_OK)
88  std::cerr << "Error when closing " << fileName << ": " << status << std::endl;
89 #ifdef DEBUG
90  else
91  std::cerr << "Successful close of " << fileName << std::endl;
92 #endif
93  fileDescriptor = -1;
94  valid = false;
95  return status == IOCTL_OK;
96 }
97 
98 int LircDevice::ioctl(uint32_t cmd, uint32_t arg) {
99  return ::ioctl(fileDescriptor, cmd, &arg);
100 }
101 
102 void LircDevice::report(const char *name, bool value, std::ostream& stream) const {
103  if (longReport)
104  stream << name << ": " << (value ? "true" : "false") << std::endl;
105  else
106  if (value)
107  stream << name << std::endl;
108 }
109 
110 void LircDevice::report(std::ostream& stream) const {
111  if (!valid)
112  return;
113 
114  stream << "Number of transmitters = " << getNumberTransmitters() << std::endl;
115  report("canSendPulse", canSendPulse(), stream);
116  report("canSendLircCode", canSendLircCode(), stream);
117  report("canSetTransmitterMask", canSetTransmitterMask(), stream);
118  report("canRecMode2", canRecMode2(), stream);
119  report("canRecLircCode", canRecLircCode(), stream);
120  report("canSetRecTimeout", canSetRecTimeout(), stream);
121 
122 #ifdef SUPPORT_NEVER_IMPLEMENTED_OPTIONS
123  report("canSendRaw", canSendRaw(), stream);
124  report("canSendMode2", canSendMode2(), stream);
125  report("canRecRaw", canRecRaw(), stream);
126  report("canRecPulse", canRecPulse(), stream);
127  report("canSetRecCarrier", canSetRecCarrier(), stream);
128  report("canSetRecDutyCycle", canSetRecDutyCycle(), stream);
129  report("canSetRecDutyCycleRange", canSetRecDutyCycleRange(), stream);
130  report("canSetRecCarrierRange", canSetRecCarrierRange(), stream);
131  report("canSetRecFilter", canSetRecFilter(), stream);
132  report("canNotifyDecoder", canNotifyDecoder(), stream);
133  report("canMeasureCarrier", canMeasureCarrier(), stream);
134  report("canUseWidebandReceiver", canUseWidebandReceiver(), stream);
135 #endif //SUPPORT_NEVER_IMPLEMENTED_OPTIONS
136 }
137 
138 bool LircDevice::reportValidity(std::ostream& stream) const {
139  stream << (valid ? "VALID" : "INVALID") << std::endl;
140  return valid;
141 }
bool canSendLircCode() const
Definition: LircDevice.h:83
bool canSendPulse() const
Definition: LircDevice.h:82
int fileDescriptor
Definition: LircDevice.h:31
bool valid
Definition: LircDevice.h:29
uint32_t recordingMode
Definition: LircDevice.h:33
static const char * version
Definition: LircDevice.h:50
const char * fileName
Definition: LircDevice.h:30
virtual bool open()
Definition: LircDevice.cpp:21
bool canRecLircCode() const
Definition: LircDevice.h:85
int ioctl(uint32_t cmd, uint32_t arg)
Definition: LircDevice.cpp:98
#define UINT32_MAX
Definition: InfraredTypes.h:6
bool reportValidity(std::ostream &stream=std::cout) const
Definition: LircDevice.cpp:138
uint32_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:29
virtual ~LircDevice()
Definition: LircDevice.cpp:78
bool canRecMode2() const
Definition: LircDevice.h:84
bool canSend() const
Definition: LircDevice.h:76
void report(const char *name, bool value, std::ostream &stream) const
Definition: LircDevice.cpp:102
static const bool longReport
Flag indicating how to format reports.
Definition: LircDevice.h:27
bool canSetRecTimeout() const
Definition: LircDevice.h:79
unsigned getNumberTransmitters() const
Return the number of transmitters as reported from the device.
Definition: LircDevice.h:92
int setTransmitterMask(uint32_t mask)
Enable the set of transmitters specified in val, which contains a bit mask where each enabled transmi...
Definition: LircDevice.h:103
static const char * defaultFilename
Definition: LircDevice.h:51
LircDevice(const char *path=defaultFilename, milliseconds_t beginTimeout=defaultBeginTimeout)
Definition: LircDevice.cpp:18
This abstract class contains the common functionality of the derived classes.
Definition: LircDevice.h:24
uint32_t features
Definition: LircDevice.h:34
bool canSetTransmitterMask() const
Definition: LircDevice.h:77
bool close()
Definition: LircDevice.cpp:82
unsigned numberTransmitters
Definition: LircDevice.h:32