diff options
author | David Härdeman <david@hardeman.nu> | 2017-04-18 21:56:45 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2017-04-18 21:56:45 +0200 |
commit | ae0b71dca66d66d105fa8ba491c1c4559c5813be (patch) | |
tree | 40f4401ac6dd47f449b9d907359a0725bda6bb5b | |
parent | d139eaa3759e23706f51eb7c1ef058cade0f86fc (diff) |
Factor out lirc code to a separate file
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | rcm-server-lirc.c | 86 | ||||
-rw-r--r-- | rcm-server-lirc.h | 11 | ||||
-rw-r--r-- | rcm-server-main.c | 15 | ||||
-rw-r--r-- | rcm-server-udev.c | 62 | ||||
-rw-r--r-- | utils.h | 2 |
6 files changed, 103 insertions, 77 deletions
@@ -19,8 +19,8 @@ RCM_CFLAGS = ${GENERIC_CFLAGS} ${EXTRA_CFLAGS} $(shell pkg-config --cflags ${RCM RCM_LDFLAGS = ${GENERIC_LDFLAGS} ${EXTRA_LDFLAGS} $(shell pkg-config --libs ${RCM_PACKAGES}) RCM_COMPILE = $(CC) $(RCM_CFLAGS) RCM_LINK = $(CC) $(RCM_CFLAGS) $(RCM_LDFLAGS) -RCM_OBJECTS = rcm-server-main.o rcm-server-keymap.o rcm-server-udev.o rcm-server-evdev.o rcm-server-kdb.o shared.o -RCM_HEADERS = rcm-server-main.h rcm-server-keymap.h rcm-server-udev.h rcm-server-evdev.h rcm-server-kdb.h utils.h $(COMMON_HEADERS) +RCM_OBJECTS = rcm-server-main.o rcm-server-keymap.o rcm-server-udev.o rcm-server-lirc.o rcm-server-evdev.o rcm-server-kdb.o shared.o +RCM_HEADERS = rcm-server-main.h rcm-server-keymap.h rcm-server-udev.h rcm-server-lirc.h rcm-server-evdev.h rcm-server-kdb.h utils.h $(COMMON_HEADERS) RCC_PACKAGES = gtk+-3.0 gmodule-2.0 RCC_CFLAGS = ${GENERIC_CFLAGS} ${EXTRA_CFLAGS} $(shell pkg-config --cflags ${RCC_PACKAGES}) 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; +} + diff --git a/rcm-server-lirc.h b/rcm-server-lirc.h new file mode 100644 index 0000000..5edc5fc --- /dev/null +++ b/rcm-server-lirc.h @@ -0,0 +1,11 @@ +#ifndef foorcmserverlirchfoo +#define foorcmserverlirchfoo + +void lirc_write(int fd, const uint32_t *v, unsigned count); + +int lirc_read(sd_event_source *s, int fd, uint32_t revents, void *userdata); + +int lirc_setup(struct device *device, const char *path); + +#endif + diff --git a/rcm-server-main.c b/rcm-server-main.c index 7aadb69..0e245e3 100644 --- a/rcm-server-main.c +++ b/rcm-server-main.c @@ -17,6 +17,7 @@ #include "rcm-server-udev.h" #include "rcm-server-keymap.h" #include "rcm-server-evdev.h" +#include "rcm-server-lirc.h" #include "rcm-server-kdb.h" static struct device * @@ -782,20 +783,6 @@ out: return sd_bus_error_set_errno(error, r); } -static 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)); -} - static int method_transmit(sd_bus_message *m, void *userdata, sd_bus_error *error) { diff --git a/rcm-server-udev.c b/rcm-server-udev.c index d29b4d9..797335b 100644 --- a/rcm-server-udev.c +++ b/rcm-server-udev.c @@ -1,21 +1,18 @@ #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> -#include <stdbool.h> #include <libudev.h> #include <unistd.h> #include <string.h> #include <systemd/sd-bus.h> #include <errno.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-keymap.h" #include "rcm-server-evdev.h" +#include "rcm-server-lirc.h" #include "rcm-server-udev.h" static void @@ -37,63 +34,6 @@ remove_device(struct manager *mgr, struct udev_device *udev) } } -static 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; -} - -static 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; -} - static void add_device(struct manager *mgr, struct udev_device *udev) { @@ -4,6 +4,8 @@ #include <sys/types.h> #include <dirent.h> #include <systemd/sd-bus.h> +#include <stdbool.h> +#include <stdlib.h> struct list_head { struct list_head *next; |