diff options
Diffstat (limited to 'rcm-server-lirc.c')
-rw-r--r-- | rcm-server-lirc.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/rcm-server-lirc.c b/rcm-server-lirc.c new file mode 100644 index 0000000..150df8b --- /dev/null +++ b/rcm-server-lirc.c @@ -0,0 +1,86 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <systemd/sd-bus.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include "utils.h" +#include "shared.h" +#include "rcm-server-main.h" +#include "rcm-server-lirc.h" + +void +lirc_write(int fd, const uint32_t *v, unsigned count) +{ + unsigned nv[count]; + unsigned i; + size_t l = count * sizeof(uint32_t); + + for (i = 0; i < count; i++) + nv[i] = v[i]; + + printf("Write to lirc fd of %zu bytes returned %zi errno is %i aka %s\n", + l, write(fd, nv, l), errno, strerror(errno)); +} + +int +lirc_read(sd_event_source *s, int fd, uint32_t revents, void *userdata) +{ + struct device *device = userdata; + uint8_t buf[100]; + ssize_t bytes_read; + + if (fd != device->lirc_fd) + fprintf(stderr, "lirc fd mismatch: %i != %i\n", device->lirc_fd, fd); + + if (revents & EPOLLHUP) { + fprintf(stderr, "lirc connection closed!\n"); + close(fd); + device->lirc_fd = -1; + return 0; + } + + if (!(revents & EPOLLIN)) { + fprintf(stderr, "unexpected lirc event: %" PRIu32 "\n", revents); + return 0; + } + + while ((bytes_read = read(fd, buf, sizeof(buf))) > 0) + printf("Read %zi bytes from lirc dev\n", bytes_read); + + return 0; +} + +int +lirc_setup(struct device *device, const char *path) +{ + if (!device) + return -EINVAL; + + if (device->lirc_fd >= 0) { + printf("Multiple lirc devices!?\n"); + return 0; + } + + device->lirc_fd = open(path, O_RDWR | O_NONBLOCK); + if (device->lirc_fd < 0) { + printf("Failed to open lirc device %s: %s\n", path, strerror(errno)); + return -errno; + } + + if (sd_event_add_io(device->mgr->event, &device->lirc_ev, + device->lirc_fd, EPOLLIN, lirc_read, device) < 0) { + printf("Failed to add event source for lirc device %s: %s\n", + path, strerror(errno)); + close(device->lirc_fd); + device->lirc_fd = -1; + return -errno; + } + + return 0; +} + |