| LIRC libraries LinuxInfraredRemoteControl

drv_admin.c
Go to the documentation of this file.
1
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13
14 #include <stdio.h>
15 #include <dirent.h>
16 #include <dlfcn.h>
17
18 #include "lirc/driver.h"
19 #include "lirc/drv_admin.h"
20 #include "lirc/lirc_options.h"
21 #include "lirc_log.h"
22
23 #include "driver.h"
24
25 static const logchannel_t logchannel = LOG_LIB;
26
27 static const char* const PLUGIN_FILE_EXTENSION = "so";
28
29
31 #define MAX_PLUGINS 256
32
33 extern struct driver drv;
36 typedef struct {
37 char* array[MAX_PLUGINS];
38 int size;
39 } char_array;
40
42 static void* last_plugin = NULL;
43
45 const struct driver drv_null = {
46 .name = "null",
47 .device = "/dev/null",
48 .features = 0,
49 .send_mode = 0,
50 .rec_mode = 0,
51 .code_length = 0,
52 .init_func = NULL,
53 .deinit_func = NULL,
54 .send_func = NULL,
55 .rec_func = NULL,
56 .decode_func = NULL,
57 .readdata = NULL,
58 .drvctl_func = default_drvctl,
59 .open_func = default_open,
60 .close_func = default_close,
61 .api_version = 2,
62 .driver_version = "0.9.2"
63 };
64
65
71 static int ends_with_so(const char* str)
72 {
73 char* dot = strrchr(str, '.');
74
75 return (NULL == dot) ? 0 : strcmp(dot + 1, PLUGIN_FILE_EXTENSION) == 0;
76 }
77
78
80 static int line_cmp(const void* arg1, const void* arg2)
81 {
82 return strcmp(*(const char**)arg1, *(const char**)arg2);
83 }
84
85
87 static struct driver* add_hw_name(struct driver* hw, void* arg)
88 {
89 char_array* a = (char_array*)arg;
90
91 if (a->size >= MAX_PLUGINS) {
92 log_error("Too many plugins(%d)", MAX_PLUGINS);
93 return hw;
94 }
95 a->array[a->size] = strdup(hw->name);
96 a->size += 1;
97 return NULL;
98 }
99
100
101 static struct driver* match_hw_name(struct driver* drv, void* name)
102 {
103 // drv_guest_func. Returns hw if hw->name == name, else NULL.
104 if (drv == (struct driver*)NULL || name == NULL)
105 return (struct driver*)NULL;
106 if (strcasecmp(drv->name, (char*)name) == 0)
107 return drv;
108 return (struct driver*)NULL;
109 }
110
111
112 static struct driver*
113 visit_plugin(const char* path, drv_guest_func func, void* arg)
114 {
115 // Apply func(hw, arg) for all drivers found in plugin on path.
116 struct driver** drivers;
117 struct driver* result = (struct driver*)NULL;
118
119 (void)dlerror();
120 if (last_plugin != NULL)
121 dlclose(last_plugin);
122 last_plugin = dlopen(path, RTLD_NOW);
123 if (last_plugin == NULL) {
124 log_error(dlerror());
125 return result;
126 }
127 drivers = (struct driver**)dlsym(last_plugin, "hardwares");
128 if (drivers == (struct driver**)NULL) {
129 log_warn("No hardwares entrypoint found in %s", path);
130 } else {
131 for (; *drivers; drivers++) {
132 if ((*drivers)->name == NULL) {
133 log_warn("No driver name in %s", path);
134 continue;
135 }
136 result = (*func)(*drivers, arg);
137 if (result != (struct driver*)NULL)
138 break;
139 }
140 }
141 return result;
142 }
143
144
145 /* Apply plugin_guest(path, drv_guest, arg) to all so-files in dir. */
146 static struct driver* for_each_plugin_in_dir(const char* dirpath,
147 plugin_guest_func plugin_guest,
148 drv_guest_func drv_guest,
149 void* arg)
150 {
151 DIR* dir;
152 struct dirent* ent;
153 struct driver* result = (struct driver*)NULL;
154 char path[128];
155 char buff[128];
156
157 dir = opendir(dirpath);
158 if (dir == NULL) {
159 log_info("Cannot open plugindir %s", dirpath);
160 return (struct driver*)NULL;
161 }
162 while ((ent = readdir(dir)) != NULL) {
163 if (!ends_with_so(ent->d_name))
164 continue;
165 strncpy(buff, dirpath, sizeof(buff) - 1);
166 if (buff[strlen(buff) - 1] == '/')
167 buff[strlen(buff) - 1] = '\0';
168 snprintf(path, sizeof(path),
169 "%s/%s", buff, ent->d_name);
170 result = plugin_guest(path, drv_guest, arg);
171 if (result != (struct driver*)NULL)
172 break;
173 }
174 closedir(dir);
175 return result;
176 }
177
178
179 static struct driver* for_each_path(plugin_guest_func plg_guest,
180 drv_guest_func drv_guest,
181 void* arg,
182 const char* pluginpath_arg)
183 {
184 const char* pluginpath;
185 char* tmp_path;
186 char* s;
187 struct driver* result = (struct driver*)NULL;
188
189 if (pluginpath_arg == NULL) {
190 pluginpath = ciniparser_getstring(lirc_options,
191 "lircd:plugindir",
192 getenv(PLUGINDIR_VAR));
193 if (pluginpath == NULL)
194 pluginpath = PLUGINDIR;
195 } else {
196 pluginpath = pluginpath_arg;
197 }
198 if (strchr(pluginpath, ':') == (char*)NULL) {
199 return for_each_plugin_in_dir(pluginpath,
200 plg_guest,
201 drv_guest,
202 arg);
203 }
204 tmp_path = alloca(strlen(pluginpath) + 1);
205 strncpy(tmp_path, pluginpath, strlen(pluginpath) + 1);
206 for (s = strtok(tmp_path, ":"); s != NULL; s = strtok(NULL, ":")) {
207 result = for_each_plugin_in_dir(s,
208 plg_guest,
209 drv_guest,
210 arg);
211 if (result != (struct driver*)NULL)
212 break;
213 }
214 return result;
215 }
216
217
218 struct driver* for_each_driver(drv_guest_func func,
219 void* arg,
220 const char* pluginpath)
221 {
222 return for_each_path(visit_plugin, func, arg, pluginpath);
223 }
224
225
226 void for_each_plugin(plugin_guest_func plugin_guest,
227 void* arg,
228 const char* pluginpath)
229 {
230 for_each_path(plugin_guest, NULL, arg, pluginpath);
231 }
232
233
238 void hw_print_drivers(FILE* file)
239 {
240 char_array names;
241 int i;
242
243 names.size = 0;
244 if (for_each_driver(add_hw_name, (void*)&names, NULL) != NULL) {
245 fprintf(stderr, "Too many plugins (%d)\n", MAX_PLUGINS);
246 return;
247 }
248 qsort(names.array, names.size, sizeof(char*), line_cmp);
249 for (i = 0; i < names.size; i += 1) {
250 fprintf(file, "%s\n", names.array[i]);
251 free(names.array[i]);
252 }
253 }
254
255
256 int hw_choose_driver(const char* name)
257 {
258 struct driver* found;
259
260 if (name == NULL) {
261 memcpy(&drv, &drv_null, sizeof(struct driver));
262 return 0;
263 }
264 if (strcasecmp(name, "dev/input") == 0)
265 /* backwards compatibility */
266 name = "devinput";
267 found = for_each_driver(match_hw_name, (void*)name, NULL);
268 if (found != (struct driver*)NULL) {
269 memcpy(&drv, found, sizeof(struct driver));
270 return 0;
271 }
272 return -1;
273 }
ciniparser_getstring
const char * ciniparser_getstring(dictionary *d, const char *key, char *def)
Get the string associated to a key.
Definition: ciniparser.c:286
default_close
int default_close(void)
Definition: driver.c:79
plugin_guest_func
struct driver *(* plugin_guest_func)(const char *, drv_guest_func, void *)
Definition: drv_admin.h:41
lirc_log.h
Logging functionality.
driver.h
Interface to the userspace drivers.
log_warn
#define log_warn(fmt,...)
Definition: lirc_log.h:109
MAX_PLUGINS
#define MAX_PLUGINS
Definition: drv_admin.c:31
drv_null
const struct driver drv_null
Definition: drv_admin.c:45
logchannel_t
logchannel_t
Definition: lirc_log.h:53
PLUGINDIR
#define PLUGINDIR
Definition: lirc_config.h:80
log_error
#define log_error(fmt,...)
Definition: lirc_log.h:104
for_each_plugin
void for_each_plugin(plugin_guest_func plugin_guest, void *arg, const char *pluginpath)
Definition: drv_admin.c:226
hw_print_drivers
void hw_print_drivers(FILE *file)
Prints all drivers known to the system to the file given as argument.
Definition: drv_admin.c:238
drv
struct driver drv
Definition: driver.c:22
drv_guest_func
struct driver *(* drv_guest_func)(struct driver *, void *)
Definition: drv_admin.h:32
PLUGINDIR_VAR
#define PLUGINDIR_VAR
Definition: lirc_config.h:101
default_drvctl
int default_drvctl(unsigned int fd, void *arg)
Definition: driver.c:84
for_each_driver
struct driver * for_each_driver(drv_guest_func func, void *arg, const char *pluginpath)
Definition: drv_admin.c:218
driver
Definition: driver.h:127
hw_choose_driver
int hw_choose_driver(const char *name)
Definition: drv_admin.c:256
driver::name
const char * name
Definition: driver.h:219
default_open
int default_open(const char *path)
Definition: driver.c:64
log_info
#define log_info(fmt,...)
Definition: lirc_log.h:114