summaryrefslogtreecommitdiff
path: root/rcm-server-udev.c
blob: 82a33a8e6630a31a9b2682671e6c17c6ea4bf8e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <libudev.h>
#include <unistd.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <errno.h>

#include "utils.h"
#include "shared.h"
#include "rcm-server-main.h"
#include "rcm-server-keymap.h"
#include "rcm-server-evdev.h"
#include "rcm-server-lirc.h"
#include "rcm-server-rcdev.h"
#include "rcm-server-udev.h"

static void
udev_device_remove(struct manager *mgr, struct udev_device *udev)
{
	const char *name;
	struct rc_device *rcdev;

	name = udev_device_get_sysname(udev);
	printf("Asked to remove device %s\n", name);

	list_for_each_entry(rcdev, &mgr->rc_devs, list) {
		if (strcmp(rcdev->name, name))
			continue;
		sd_bus_emit_object_removed(mgr->bus, rcdev->path);
		rcdev_free(rcdev);
		mgr->num_rc_devs--;
		break;
	}
}

static void
udev_device_add(struct manager *mgr, struct udev_device *udev)
{
	const char *name;
	const char *tmpstr;
	struct rc_device *rcdev;
	struct udev_enumerate *enumerate;
	struct udev_list_entry *devices, *dev_list_entry;
	int r;

	name = udev_device_get_sysname(udev);
	if (!name)
		return;

	rcdev = rcdev_create(mgr, name);
	if (!rcdev)
		return;

	enumerate = udev_enumerate_new(mgr->udev);
	udev_enumerate_add_match_parent(enumerate, udev);
	udev_enumerate_add_match_sysname(enumerate, "event*");
	udev_enumerate_add_match_sysname(enumerate, "lirc*");
	udev_enumerate_scan_devices(enumerate);
	devices = udev_enumerate_get_list_entry(enumerate);

	udev_list_entry_foreach(dev_list_entry, devices) {
		const char *path;
		struct udev_device *udev_dev;
		const char *devnode;
		const char *subsys;

		path = udev_list_entry_get_name(dev_list_entry);
		if (!path) {
			printf("Failed to get udev name\n");
			continue;
		}

		udev_dev = udev_device_new_from_syspath(mgr->udev, path);
		if (!udev_dev) {
			printf("Failed to create udev device\n");
			continue;
		}

		devnode = udev_device_get_devnode(udev_dev);
		if (!devnode) {
			printf("Failed to determine udev_dev devnode\n");
			goto next;
		}

		subsys = udev_device_get_subsystem(udev_dev);
		if (!subsys) {
			printf("Failed to determine udev_dev subsystem\n");
			goto next;
		}

		if (!strcmp(subsys, "input")) {
			r = evdev_setup(rcdev, devnode);
			if (r < 0) {
				printf("Failed to setup evdev: %s\n", devnode);
				rcdev->error = "Error: Failed to setup evdev";
				goto next;
			}

		} else if (!strcmp(subsys, "lirc")) {
			r = lirc_setup(rcdev, devnode);
			if (r < 0) {
				printf("Failed to setup lirc: %s\n", devnode);
				rcdev->error = "Error: Failed to setup lirc";
				goto next;
			}

		} else {
			printf("Unknown subsystem, ignored %s\n", devnode);
			goto next;
		}

next:
		udev_device_unref(udev_dev);
	}
	udev_enumerate_unref(enumerate);

	tmpstr = udev_device_get_devnode(udev);
	if (tmpstr) {
		r = rcdev_setup(rcdev, tmpstr);
		if (r < 0) {
			printf("Failed to setup rcdev: %s\n", tmpstr);
			rcdev->error = "Error: Failed to setup rcdev";
		}
	}

	tmpstr = udev_device_get_sysattr_value(udev, "uevent");
	if (tmpstr) {
		char tmp[strlen(tmpstr) + 1];
		char *token;

		strcpy(tmp, tmpstr);

		for (token = strtok(tmp, "\n"); token; token = strtok(NULL, "\n")) {
			if (!strncmp(token, "DRV_NAME=", strlen("DRV_NAME=")))
				rcdev->driver_name = strdup(token + strlen("DRV_NAME="));
			else if (!strncmp(token, "NAME=", strlen("NAME=")))
				rcdev->keymap_name = strdup(token + strlen("NAME="));
			else if (!strncmp(token, "DEVNAME=", strlen("DEVNAME=")))
				continue;
			else if (!strncmp(token, "MAJOR=", strlen("MAJOR=")))
				continue;
			else if (!strncmp(token, "MINOR=", strlen("MINOR=")))
				continue;
			else
				printf("Unused uevent: %s\n", token);
		}
	}

	printf("Adding Device Object\n");
	printf("\tPath      : %s\n", udev_device_get_syspath(udev));
	printf("\tNode      : %s\n", udev_device_get_devnode(udev));
	printf("\tSubsystem : %s\n", udev_device_get_subsystem(udev));
	printf("\tDevtype   : %s\n", udev_device_get_devtype(udev));
	printf("\tAction    : %s\n", udev_device_get_action(udev));
	printf("\tName      : %s\n", rcdev->name);
	printf("\tInput name: %s\n", rcdev->input_name);
	printf("\tDriver    : %s\n", rcdev->driver_name);
	printf("\tKernel map: %s\n", rcdev->keymap_name);
	printf("\trcdev fd  : %i\n", rcdev->rcdev_fd);
	printf("\tevdev fd  : %i\n", rcdev->evdev_fd);
	printf("\tLIRC fd   : %i\n", rcdev->lirc_fd);
	printf("\tDBUS path : %s\n", rcdev->path);
	printf("\tProtocols : %s\n",
	       udev_device_get_sysattr_value(udev, "protocols"));

	list_add(&rcdev->list, &mgr->rc_devs);
	mgr->num_rc_devs++;

	sd_bus_emit_object_added(mgr->bus, rcdev->path);
}

static int
udev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
	struct manager *mgr = userdata;
	struct udev_device *udev_dev;

	if (revents & EPOLLHUP) {
		fprintf(stderr, "udev connection closed!\n");
		return 0;
	}

	if (!(revents & EPOLLIN)) {
		fprintf(stderr, "unexpected udev event: %" PRIu32 "\n", revents);
		return 0;
	}

	while ((udev_dev = udev_monitor_receive_device(mgr->udev_mon))) {
		printf("Read device: %s\n", udev_device_get_syspath(udev_dev));
		if (!strcmp(udev_device_get_action(udev_dev), "add"))
			udev_device_add(mgr, udev_dev);
		else if (!strcmp(udev_device_get_action(udev_dev), "remove"))
			udev_device_remove(mgr, udev_dev);
		udev_device_unref(udev_dev);
	}

	return 1;
}

int
udev_setup(struct manager *mgr)
{
	struct udev_enumerate *enumerate;
	struct udev_list_entry *devices, *dev_list_entry;
	struct udev_device *udev_dev;

	mgr->udev = udev_new();
	if (!mgr->udev)
		return -ENOMEM;

	mgr->udev_mon = udev_monitor_new_from_netlink(mgr->udev, "udev");
	udev_monitor_filter_add_match_subsystem_devtype(mgr->udev_mon, "rc", NULL);
	udev_monitor_enable_receiving(mgr->udev_mon);

	enumerate = udev_enumerate_new(mgr->udev);
	udev_enumerate_add_match_subsystem(enumerate, "rc");
	udev_enumerate_scan_devices(enumerate);
	devices = udev_enumerate_get_list_entry(enumerate);

	udev_list_entry_foreach(dev_list_entry, devices) {
		const char *path;

		path = udev_list_entry_get_name(dev_list_entry);
		if (!path) {
			printf("Failed to get udev name\n");
			continue;
		}

		udev_dev = udev_device_new_from_syspath(mgr->udev, path);
		if (!udev_dev) {
			printf("Failed to create udev device\n");
			continue;
		}

		udev_device_add(mgr, udev_dev);

		udev_device_unref(udev_dev);
	}
	udev_enumerate_unref(enumerate);

	return sd_event_add_io(mgr->event, &mgr->udev_ev,
			       udev_monitor_get_fd(mgr->udev_mon),
			       EPOLLIN, udev_read, mgr);
}

void
udev_close(struct manager *mgr)
{
	if (!mgr)
		return;

	udev_monitor_unref(mgr->udev_mon);
	udev_unref(mgr->udev);
}