LIRC libraries
LinuxInfraredRemoteControl
release.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** release.c ***************************************************************
3 ****************************************************************************
4 *
5 * Copyright (C) 2007 Christoph Bartelmus (lirc@bartelmus.de)
6 *
7 */
8 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/time.h>
24 
25 #ifdef HAVE_KERNEL_LIRC_H
26 #include <linux/lirc.h>
27 #else
28 #include "media/lirc.h"
29 #endif
30 
31 #include "lirc/release.h"
32 #include "lirc/receive.h"
33 #include "lirc/lirc_log.h"
34 
35 static const logchannel_t logchannel = LOG_LIB;
36 
37 static struct timeval release_time;
38 static struct ir_remote* release_remote;
39 static struct ir_ncode* release_ncode;
40 static ir_code release_code;
41 static int release_reps;
42 static lirc_t release_gap;
43 
44 static struct ir_remote* release_remote2;
45 static struct ir_ncode* release_ncode2;
46 static ir_code release_code2;
47 static const char* release_suffix = LIRC_RELEASE_SUFFIX;
48 static char message[PACKET_SIZE + 1];
49 
50 void register_input(void)
51 {
52  struct timeval gap;
53 
54  if (release_remote == NULL)
55  return;
56 
57  timerclear(&gap);
58  gap.tv_usec = release_gap;
59 
60  gettimeofday(&release_time, NULL);
61  timeradd(&release_time, &gap, &release_time);
62 }
63 
64 void register_button_press(struct ir_remote* remote,
65  struct ir_ncode* ncode,
66  ir_code code,
67  int reps)
68 {
69  if (reps == 0 && release_remote != NULL) {
70  release_remote2 = release_remote;
71  release_ncode2 = release_ncode;
72  release_code2 = release_code;
73  }
74 
75  release_remote = remote;
76  release_ncode = ncode;
77  release_code = code;
78  release_reps = reps;
79  /* some additional safety margin */
80  release_gap = upper_limit(remote,
82  - remote->min_gap_length)
83  + receive_timeout(upper_limit(remote,
84  remote->min_gap_length))
85  + 10000;
86  log_trace("release_gap: %lu", release_gap);
88 }
89 
90 void get_release_data(const char** remote_name,
91  const char** button_name,
92  int* reps)
93 {
94  if (release_remote != NULL) {
95  *remote_name = release_remote->name;
96  *button_name = release_ncode->name;
97  *reps = release_reps;
98  } else {
99  *remote_name = *button_name = "(NULL)";
100  *reps = 0;
101  }
102 }
103 
104 void set_release_suffix(const char* s)
105 {
106  release_suffix = s;
107 }
108 
109 void get_release_time(struct timeval* tv)
110 {
111  *tv = release_time;
112 }
113 
114 const char* check_release_event(const char** remote_name,
115  const char** button_name)
116 {
117  int len = 0;
118 
119  if (release_remote2 != NULL) {
120  *remote_name = release_remote2->name;
121  *button_name = release_ncode2->name;
122  len = write_message(message,
123  PACKET_SIZE + 1,
124  release_remote2->name,
125  release_ncode2->name,
126  release_suffix,
127  release_code2,
128  0);
129  release_remote2 = NULL;
130  release_ncode2 = NULL;
131  release_code2 = 0;
132 
133  if (len >= PACKET_SIZE + 1) {
134  log_error("message buffer overflow");
135  return NULL;
136  }
137 
138  log_trace2("check");
139  return message;
140  }
141  return NULL;
142 }
143 
144 const char* trigger_release_event(const char** remote_name,
145  const char** button_name)
146 {
147  int len = 0;
148 
149  if (release_remote != NULL) {
150  release_remote->release_detected = 1;
151  *remote_name = release_remote->name;
152  *button_name = release_ncode->name;
153  len = write_message(message,
154  PACKET_SIZE + 1,
155  release_remote->name,
156  release_ncode->name,
157  release_suffix,
158  release_code,
159  0);
160  timerclear(&release_time);
161  release_remote = NULL;
162  release_ncode = NULL;
163  release_code = 0;
164 
165  if (len >= PACKET_SIZE + 1) {
166  log_error("message buffer overflow");
167  return NULL;
168  }
169  log_trace2("trigger");
170  return message;
171  }
172  return NULL;
173 }
174 
175 const char* release_map_remotes(struct ir_remote* old,
176  struct ir_remote* new,
177  const char** remote_name,
178  const char** button_name)
179 {
180  struct ir_remote* remote;
181  struct ir_ncode* ncode = NULL;
182 
183  if (release_remote2 != NULL) {
184  /* should not happen */
185  log_error("release_remote2 still in use");
186  release_remote2 = NULL;
187  }
188  if (release_remote && is_in_remotes(old, release_remote)) {
189  remote = get_ir_remote(new, release_remote->name);
190  if (remote)
191  ncode = get_code_by_name(remote, release_ncode->name);
192  if (remote && ncode) {
193  release_remote = remote;
194  release_ncode = ncode;
195  } else {
196  return trigger_release_event(remote_name, button_name);
197  }
198  }
199  return NULL;
200 }
One remote as represented in the configuration file.
lirc_t max_total_signal_length
how long is the longest signal including gap
void get_release_time(struct timeval *tv)
Get time for pending release event if it exists, else a noop.
Definition: release.c:109
const char * name
name of remote control
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Return code with given name in remote's list of codes or NULL.
Definition: ir_remote.c:397
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Return ir_remote with given name in remotes list, or NULL if not found.
Definition: ir_remote.c:251
lirc_t min_gap_length
how long is the shortest gap
#define PACKET_SIZE
IR transmission packet size.
Definition: lirc_config.h:84
logchannel_t
Log channels used to filter messages.
Definition: lirc_log.h:53
char * name
Name of command.
#define log_trace2(fmt,...)
Log a trace2 message.
Definition: lirc_log.h:139
#define log_error(fmt,...)
Log an error message.
Definition: lirc_log.h:104
const char * trigger_release_event(const char **remote_name, const char **button_name)
If there is a release event pending clears the release timer and formats a complete client message...
Definition: release.c:144
void register_button_press(struct ir_remote *remote, struct ir_ncode *ncode, ir_code code, int reps)
Set up pending release events for given button, including the release_gap.
Definition: release.c:64
int write_message(char *buffer, size_t size, const char *remote_name, const char *button_name, const char *button_suffix, ir_code code, int reps)
Formats the arguments into a readable string.
Definition: ir_remote.c:713
#define log_trace(fmt,...)
Log a trace message.
Definition: lirc_log.h:129
void register_input(void)
If there is a pending release event, set timer to current time + release_gap.
Definition: release.c:50
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Test if a given remote is in a list of remotes.
Definition: ir_remote.c:239
#define LIRC_RELEASE_SUFFIX
Suffix added to release events.
Definition: lirc_config.h:63
IR Command, corresponding to one (command defining) line of the configuration file.
int release_detected
set by release generator
uint64_t ir_code
Denotes an internal coded representation for an IR transmission.