#define _GNU_SOURCE #include #include #include #include #include #include #include #include #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)); } static int lirc_read(sd_event_source *s, int fd, uint32_t revents, void *userdata) { struct rc_device *rcdev = userdata; uint8_t buf[100]; ssize_t bytes_read; if (fd != rcdev->lirc_fd) fprintf(stderr, "lirc fd mismatch: %i != %i\n", rcdev->lirc_fd, fd); if (revents & EPOLLHUP) { fprintf(stderr, "lirc connection closed!\n"); lirc_close(rcdev); 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 rc_device *rcdev, const char *path) { int r; if (!rcdev) return -EINVAL; if (rcdev->lirc_fd >= 0) { printf("Multiple lirc devices!?\n"); return 0; } rcdev->lirc_fd = open(path, O_RDWR | O_NONBLOCK); if (rcdev->lirc_fd < 0) { printf("Failed to open lirc device %s: %s\n", path, strerror(errno)); return -errno; } if (sd_event_add_io(rcdev->mgr->event, &rcdev->lirc_ev, rcdev->lirc_fd, EPOLLIN, lirc_read, rcdev) < 0) { printf("Failed to add event source for lirc device %s: %s\n", path, strerror(errno)); r = -errno; lirc_close(rcdev); return r; } return 0; } void lirc_close(struct rc_device *rcdev) { if (!rcdev) return; if (rcdev->lirc_fd < 0) return; close(rcdev->lirc_fd); rcdev->lirc_fd = -1; }