DevSlashLirc
LircDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <linux/lirc.h>
4 #include <iostream>
5 #include "InfraredTypes.h"
6 #include "version.h"
7 
8 // In the future, this may be turned into an abstract class having derived classes
9 // FileLircDevice, FIFOLircDevice, TcpLircDevice etc.
10 // For now, this is "FileLirdDevice" supporting /dev/lirc as described
11 // by lirc(4).)
12 
13 // The lirc(4) contains a number of never implemented options, like
14 // LIRC_CAN_REC_PULSE and LIRC_CAN_SET_REC_CARRIER_RANGE. I believe that this
15 // project is more useful if these are NOT supported. To support (a subset) of
16 // them, define the following symbol.
17 
18 //#define SUPPORT_NEVER_IMPLEMENTED_OPTIONS
19 
24 class LircDevice {
25 protected:
27  static const bool longReport = false;
28 
29  bool valid;
30  const char *fileName;
33  uint32_t recordingMode; //LIRC_MODE_MODE2 or LIRC_MODE_LIRCCODE
34  uint32_t features;
36 
37  void report(const char *name, bool value, std::ostream& stream) const;
38  bool reportValidity(std::ostream &stream = std::cout) const;
39 
40  int ioctl(uint32_t cmd, uint32_t arg);
41 
42  bool canDo(uint32_t mask) const { return mask & features; }
43 
45  LircDevice(const LircDevice& orig);
46  virtual bool open();
47 
48 public:
49  bool close();
50  static const char *version;
51  static const char *defaultFilename; // = "/dev/lirc0";
52  static const milliseconds_t defaultBeginTimeout = 5000U;
53  static const uint32_t INVALID = UINT32_MAX;
54 
55  virtual ~LircDevice();
56 
57  bool isValid() const { return valid; }
58 
60  return beginTimeout;
61  }
62 
64  beginTimeout = timeout;
65  }
66 
71  virtual void report(std::ostream& stream = std::cout) const;
72 
73  virtual bool isMode2() const = 0;
74  virtual bool isLircCode() const = 0;
75 
76  bool canSend() const { return canDo(LIRC_CAN_SEND_MASK); }
77  bool canSetTransmitterMask() const { return canDo(LIRC_CAN_SET_TRANSMITTER_MASK); }
78  bool canRec() const { return canDo(LIRC_CAN_REC_MASK); }
79  bool canSetRecTimeout() const { return canDo(LIRC_CAN_SET_REC_TIMEOUT); }
80 
81  // These are sort-of redundant, but let's leave them in
82  bool canSendPulse() const { return canDo(LIRC_CAN_SEND_PULSE); }
83  bool canSendLircCode() const { return canDo(LIRC_CAN_SEND_LIRCCODE); }
84  bool canRecMode2() const { return canDo(LIRC_CAN_REC_MODE2); }
85  bool canRecLircCode() const { return canDo(LIRC_CAN_REC_LIRCCODE); }
86 
87 
92  unsigned getNumberTransmitters() const { return numberTransmitters; }
93 
103  int setTransmitterMask(uint32_t mask) {
104  return ioctl(LIRC_SET_TRANSMITTER_MASK, mask);
105  }
106 
116  int setRecTimeout(int arg) {
117  return ioctl(LIRC_SET_REC_TIMEOUT, arg);
118  }
119 
124  int setRecTimeoutReports(bool enable) {
125  return ioctl(LIRC_SET_REC_TIMEOUT_REPORTS, (int) enable);
126  }
127 
128 #ifdef SUPPORT_NEVER_IMPLEMENTED_OPTIONS
129  bool canSendRaw() const {
130  return canDo(LIRC_CAN_SEND_RAW);
131  }
132 
133  bool canSendMode2() const {
134  return canDo(LIRC_CAN_SEND_MODE2);
135  }
136 
137  bool canRecRaw() const {
138  return canDo(LIRC_CAN_REC_RAW);
139  }
140 
141  bool canRecPulse() const {
142  return canDo(LIRC_CAN_REC_PULSE);
143  }
144 
145  bool canSetRecCarrier() const {
146  return canDo(LIRC_CAN_SET_REC_CARRIER);
147  }
148 
149  bool canSetRecDutyCycle() const {
150  return canDo(LIRC_CAN_SET_REC_DUTY_CYCLE);
151  }
152 
153  bool canSetRecDutyCycleRange() const {
154  return canDo(LIRC_CAN_SET_REC_DUTY_CYCLE_RANGE);
155  }
156 
157  bool canSetRecCarrierRange() const {
158  return canDo(LIRC_CAN_SET_REC_CARRIER_RANGE);
159  }
160 
161  bool canSetRecFilter() const {
162  return canDo(LIRC_CAN_SET_REC_FILTER);
163  }
164 
165  bool canNotifyDecoder() const {
166  return canDo(LIRC_CAN_NOTIFY_DECODE);
167  }
168 
169  // This would have been nice, but unfortunately,
170  // no lirc hardware implements it...
171  bool canMeasureCarrier() const {
172  return canDo(LIRC_CAN_MEASURE_CARRIER);
173  }
174 
175  bool canUseWidebandReceiver() const {
176  return canDo(LIRC_CAN_USE_WIDEBAND_RECEIVER);
177  }
178 
179  bool canRecLircCode() const {
180  return canDo(LIRC_CAN_REC_LIRCCODE);
181  }
182 
183 #endif // SUPPORT_NEVER_IMPLEMENTED_OPTIONS
184 };
185 
186 // There is a number of never implemented options in the API. We do not
187 // ("presenty") implement these. For reference, they are listed here:
188 // (copied from lirc.h)
189 
190 // LIRC_SET_REC_MODE (int)
191 // Set the receive mode. val is either LIRC_MODE_LIRCCODE or
192 // LIRC_MODE_MODE2.
193 //
194 // LIRC_GET_LENGTH (void)
195 // Return the length of the returned codes for LIRC_MODE_LIRCCODE-
196 // type drivers, otherwise fail with the error ENOIOCTLCMD.
197 //
198 // LIRC_GET_SEND_MODE (void)
199 // Return the send mode. Currently only LIRC_MODE_PULSE is sup-
200 // ported.
201 //
202 // LIRC_SET_SEND_MODE (int)
203 // Set the send mode. Currently serves no purpose since only
204 // LIRC_MODE_PULSE is supported.
205 
206 // Implemented in ModeLircDevice:
207 // /** Set the modulation frequency. The argument is the frequency (Hz).*/
208 // int setSendCarrier(int frequency) {
209 // return sendIoctl(LIRC_SET_SEND_CARRIER, frequency);
210 // }
211 //
212 // Implemented in ModeLircDevice:
213 // Set the carrier duty cycle. val is a number in the range
214 // [0,100] which describes the pulse width as a percentage of the
215 // total cycle. Currently, no special meaning is defined for 0 or
216 // 100, but the values are reserved for future use.*/
217 // int setSendDutyCycle(int dutyCycle) {
218 // return sendIoctl(LIRC_SET_SEND_DUTY_CYCLE, dutyCycle);
219 // }
220 
221 // LIRC_GET_MIN_TIMEOUT (void), LIRC_GET_MAX_TIMEOUT (void)
222 // Some devices have internal timers that can be used to detect
223 // when there's no IR activity for a long time. This can help
224 // lircd(8) in detecting that an IR signal is finished and can
225 // speed up the decoding process. These operations return integer
226 // values with the minimum/maximum timeout that can be set
227 // (microseconds). Some devices have a fixed timeout. For such
228 // drivers, LIRC_GET_MIN_TIMEOUT and LIRC_GET_MAX_TIMEOUT will
229 // return the same value.
230 //
231 //
232 // LIRC_SET_REC_TIMEOUT_REPORTS (int)
233 // Enable (val is 1) or disable (val is 0) timeout packages in
234 // LIRC_MODE_MODE2. By default, timeout reports should be turned
235 // off.
236 //
237 // LIRC_SET_REC_CARRIER (int)
238 // Set the receive carrier frequency (Hz).
239 //
240 // LIRC_SET_REC_CARRIER_RANGE (int)
241 // After opening device, the first use of this operation sets the
242 // lower bound of the carrier range, and the second use sets the
243 // upper bound (Hz).
244 //
245 // LIRC_SET_MEASURE_CARRIER_MODE (int)
246 // Enable (val is 1) or disable (val is 0) the measure mode. If
247 // enabled, from the next key press on, the driver will send
248 // LIRC_MODE2_FREQUENCY packets. By default this should be turned
249 // off.
250 //
251 // LIRC_GET_REC_RESOLUTION (void)
252 // Return the driver resolution (microseconds).
253 //
254 // LIRC_GET_MIN_FILTER_PULSE (void), LIRC_GET_MAX_FILTER_PULSE (void)
255 // Some devices are able to filter out spikes in the incoming sig-
256 // nal using given filter rules. These ioctls return the hardware
257 // capabilities that describe the bounds of the possible filters.
258 // Filter settings depend on the IR protocols that are expected.
259 // lircd(8) derives the settings from all protocols definitions
260 // found in its lircd.conf(5) config file.
261 //
262 // LIRC_GET_MIN_FILTER_SPACE (void), LIRC_GET_MAX_FILTER_SPACE (void)
263 // See LIRC_GET_MIN_FILTER_PULSE.
264 //
265 // LIRC_SET_REC_FILTER (int)
266 // Pulses/spaces shorter than this (microseconds) are filtered out
267 // by hardware.
268 //
269 // LIRC_SET_REC_FILTER_PULSE (int), LIRC_SET_REC_FILTER_SPACE (int)
270 // Pulses/spaces shorter than this (microseconds) are filtered out
271 // by hardware. If filters cannot be set independently for
272 // pulse/space, the corresponding ioctls must return an error and
273 // LIRC_SET_REC_FILTER should be used instead.
274 
275 // LIRC_SET_WIDEBAND_RECEIVER (int)
276 // Some devices are equipped with a special wide band receiver
277 // which is intended to be used to learn the output of an existing
278 // remote. This ioctl can be used to enable (val equals 1) or dis-
279 // able (val equals 0) this functionality. This might be useful
280 // for devices that otherwise have narrow band receivers that pre-
281 // vent them to be used with certain remotes. Wide band receivers
282 // may also be more precise. On the other hand its disadvantage
283 // usually is reduced range of reception.
284 //
285 // Note: wide band receiver may be implicitly enabled if you enable
286 // carrier reports. In that case, it will be disabled as soon as
287 // you disable carrier reports. Trying to disable a wide band
288 // receiver while carrier reports are active will do nothing.
289 //
290 // LIRC_SETUP_START (void), LIRC_SETUP_END (void)
291 // Setting of several driver parameters can be optimized by brack-
292 // eting the actual ioctl calls LIRC_SETUP_START and LIRC_SET-
293 // UP_END. When a driver receives a LIRC_SETUP_START ioctl, it can
294 // choose to not commit further setting changes to the hardware
295 // until a LIRC_SETUP_END is received. But this is open to the
296 // driver implementation and every driver must also handle parame-
297 // ter changes which are not encapsulated by LIRC_SETUP_START and
298 // LIRC_SETUP_END. Drivers can also choose to ignore these ioctls.
299 //
300 // LIRC_NOTIFY_DECODE (void)
301 // This ioctl is called by lircd(8) whenever a successful decoding
302 // of an incoming IR signal is possible. This can be used by sup-
303 // porting hardware to give visual user feedback, for example by
304 // flashing an LED.
305 
bool canSendLircCode() const
Definition: LircDevice.h:83
bool canSendPulse() const
Definition: LircDevice.h:82
bool canRec() const
Definition: LircDevice.h:78
virtual bool isLircCode() const =0
int fileDescriptor
Definition: LircDevice.h:31
static const milliseconds_t defaultBeginTimeout
Definition: LircDevice.h:52
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
void setBeginTimeout(milliseconds_t timeout)
Definition: LircDevice.h:63
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
int setRecTimeoutReports(bool enable)
Enable or disable timeout packages.
Definition: LircDevice.h:124
#define UINT32_MAX
Definition: InfraredTypes.h:6
bool reportValidity(std::ostream &stream=std::cout) const
Definition: LircDevice.cpp:138
bool isValid() const
Definition: LircDevice.h:57
uint32_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:29
static const uint32_t INVALID
Definition: LircDevice.h:53
virtual ~LircDevice()
Definition: LircDevice.cpp:78
bool canRecMode2() const
Definition: LircDevice.h:84
bool canSend() const
Definition: LircDevice.h:76
virtual bool isMode2() const =0
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
milliseconds_t getBeginTimeout() const
Definition: LircDevice.h:59
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
milliseconds_t beginTimeout
Definition: LircDevice.h:35
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 canDo(uint32_t mask) const
Definition: LircDevice.h:42
This file defines some general data types that are used in the library.
int setRecTimeout(int arg)
Set the integer value for IR inactivity timeout (microseconds).
Definition: LircDevice.h:116
bool canSetTransmitterMask() const
Definition: LircDevice.h:77
bool close()
Definition: LircDevice.cpp:82
unsigned numberTransmitters
Definition: LircDevice.h:32