2022-08-30 11:45:53 -05:00
|
|
|
#include "client.h"
|
2022-08-31 16:24:26 -05:00
|
|
|
#include "const.h"
|
2022-08-31 11:54:14 -05:00
|
|
|
#include "json.h"
|
2022-08-30 17:54:56 -05:00
|
|
|
#include "net.h"
|
|
|
|
#include "util.h"
|
2022-10-07 18:36:53 -05:00
|
|
|
#include "vec.h"
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <fcntl.h>
|
2022-08-31 11:59:06 -05:00
|
|
|
#include <linux/input-event-codes.h>
|
2022-08-30 17:54:56 -05:00
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/uinput.h>
|
2022-08-31 11:54:14 -05:00
|
|
|
#include <math.h>
|
2022-08-30 17:54:56 -05:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdbool.h>
|
2022-08-31 11:59:06 -05:00
|
|
|
#include <stddef.h>
|
2022-08-30 12:08:36 -05:00
|
|
|
#include <stdint.h>
|
2022-08-30 17:54:56 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-08-31 11:54:14 -05:00
|
|
|
#include <sys/ioctl.h>
|
2022-08-30 17:54:56 -05:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int fifo_attempt = 0;
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
static struct sockaddr_in server_addr = {0};
|
|
|
|
static char server_addrp[64] = {0};
|
2022-08-30 17:54:56 -05:00
|
|
|
static uint16_t server_port = -1;
|
|
|
|
|
|
|
|
static struct pollfd poll_fds[2];
|
|
|
|
static struct pollfd *fifo_poll = &poll_fds[0];
|
|
|
|
static struct pollfd *socket_poll = &poll_fds[1];
|
|
|
|
static int fifo = -1;
|
|
|
|
static int sock = -1;
|
|
|
|
// static to avoid having this on the stack because a message is about 2kb in memory
|
2022-10-07 18:36:53 -05:00
|
|
|
static Message message;
|
|
|
|
|
|
|
|
static Vec devices_fd;
|
|
|
|
static Vec devices_info;
|
|
|
|
|
|
|
|
static ClientConfig config;
|
|
|
|
static MessageRequest device_request;
|
|
|
|
|
|
|
|
static void default_fifo_path(void *ptr) { *(char **)ptr = (char *)FIFO_PATH; }
|
|
|
|
static void default_retry_delay(void *ptr) { *(struct timespec *)ptr = CONNECTION_RETRY_DELAY; }
|
|
|
|
static void default_vendor(void *ptr) { *(int32_t *)ptr = VIRTUAL_DEVICE_VENDOR; }
|
|
|
|
static void default_product(void *ptr) { *(int32_t *)ptr = VIRTUAL_DEVICE_PRODUCT; }
|
|
|
|
static void default_name(void *ptr) { *(char **)ptr = (char *)VIRTUAL_DEVICE_NAME; }
|
|
|
|
static void default_to_white(void *ptr) {
|
|
|
|
uint8_t *color = ptr;
|
|
|
|
color[0] = 255;
|
|
|
|
color[1] = 255;
|
|
|
|
color[2] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const JSONPropertyAdapter ControllerStateAdapterProps[] = {
|
|
|
|
{".led_color", &StringAdapter, offsetof(MessageControllerState, led), default_to_white, tsf_hex_to_color },
|
|
|
|
{".rumble.0", &NumberAdapter, offsetof(MessageControllerState, small_rumble), default_to_zero_u8, tsf_num_to_u8_clamp},
|
|
|
|
{".rumble.1", &NumberAdapter, offsetof(MessageControllerState, big_rumble), default_to_zero_u8, tsf_num_to_u8_clamp},
|
|
|
|
{".flash.0", &NumberAdapter, offsetof(MessageControllerState, flash_on), default_to_zero_u8, tsf_num_to_u8_clamp},
|
|
|
|
{".flash.1", &NumberAdapter, offsetof(MessageControllerState, flash_off), default_to_zero_u8, tsf_num_to_u8_clamp},
|
|
|
|
{".index", &NumberAdapter, offsetof(MessageControllerState, index), default_to_zero_u32, tsf_num_to_int }
|
|
|
|
};
|
|
|
|
static const JSONAdapter ControllerStateAdapter = {
|
|
|
|
.props = (JSONPropertyAdapter *)ControllerStateAdapterProps,
|
|
|
|
.prop_count = sizeof(ControllerStateAdapterProps) / sizeof(JSONPropertyAdapter),
|
|
|
|
.size = sizeof(MessageControllerState),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const JSONPropertyAdapter ControllerAdapterProps[] = {
|
|
|
|
{".tag", &StringAdapter, offsetof(ClientController, tag), default_to_null, NULL },
|
|
|
|
{".vendor", &StringAdapter, offsetof(ClientController, device_vendor), default_vendor, tsf_hex_to_i32},
|
|
|
|
{".product", &StringAdapter, offsetof(ClientController, device_product), default_product, tsf_hex_to_i32},
|
|
|
|
{".name", &StringAdapter, offsetof(ClientController, device_name), default_name, NULL },
|
2022-08-31 11:54:14 -05:00
|
|
|
};
|
2022-10-07 18:36:53 -05:00
|
|
|
static const JSONAdapter ControllerAdapter = {
|
|
|
|
.props = ControllerAdapterProps,
|
|
|
|
.prop_count = sizeof(ControllerAdapterProps) / sizeof(JSONPropertyAdapter),
|
|
|
|
.size = sizeof(ClientController),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const JSONPropertyAdapter ClientConfigAdapterProps[] = {
|
|
|
|
{".controllers[]", &ControllerAdapter, offsetof(ClientConfig, controllers), default_to_null, NULL },
|
|
|
|
{".fifo_path", &StringAdapter, offsetof(ClientConfig, fifo_path), default_fifo_path, NULL },
|
|
|
|
{".retry_delay", &NumberAdapter, offsetof(ClientConfig, retry_delay), default_retry_delay, tsf_numsec_to_timespec}
|
|
|
|
};
|
|
|
|
static const JSONAdapter ConfigAdapter = {
|
|
|
|
.props = ClientConfigAdapterProps,
|
|
|
|
.prop_count = sizeof(ClientConfigAdapterProps) / sizeof(JSONPropertyAdapter),
|
|
|
|
.size = sizeof(ClientConfig),
|
|
|
|
};
|
|
|
|
|
|
|
|
void destroy_devices() {
|
|
|
|
for (int i = 0; i < config.controller_count; i++) {
|
|
|
|
int fd = *(int *)vec_get(&devices_fd, i);
|
|
|
|
MessageDeviceInfo *info = vec_get(&devices_info, i);
|
|
|
|
|
|
|
|
if (info->code == DeviceInfo) {
|
|
|
|
ioctl(fd, UI_DEV_DESTROY);
|
|
|
|
info->code = NoMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool device_exists(int index) {
|
|
|
|
if (index >= devices_info.len) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-31 11:54:14 -05:00
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
MessageDeviceInfo *info = vec_get(&devices_info, index);
|
|
|
|
return info->code == DeviceInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void device_destroy(int index) {
|
|
|
|
if (index >= devices_info.len) {
|
2022-08-30 17:54:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
int fd = *(int *)vec_get(&devices_fd, index);
|
|
|
|
|
|
|
|
MessageDeviceInfo *info = vec_get(&devices_info, index);
|
|
|
|
|
|
|
|
if (info->code == DeviceInfo) {
|
|
|
|
ioctl(fd, UI_DEV_DESTROY);
|
|
|
|
info->code = NoMessage;
|
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void device_init(MessageDeviceInfo *dev) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (dev->index >= devices_info.len) {
|
|
|
|
printf("CLIENT: Got wrong device index\n");
|
|
|
|
return;
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
device_destroy(dev->index);
|
|
|
|
|
|
|
|
int fd = *(int*)vec_get(&devices_fd, dev->index);
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
// Abs
|
|
|
|
if (dev->abs_count > 0) {
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_ABS);
|
|
|
|
for (int i = 0; i < dev->abs_count; i++) {
|
2022-10-07 18:36:53 -05:00
|
|
|
struct uinput_abs_setup setup = {0};
|
|
|
|
|
|
|
|
setup.code = dev->abs_id[i];
|
|
|
|
setup.absinfo.minimum = dev->abs_min[i];
|
|
|
|
setup.absinfo.maximum = dev->abs_max[i];
|
|
|
|
setup.absinfo.fuzz = dev->abs_fuzz[i];
|
|
|
|
setup.absinfo.flat = dev->abs_flat[i];
|
|
|
|
setup.absinfo.resolution = dev->abs_res[i];
|
|
|
|
setup.absinfo.value = 0;
|
2022-08-30 17:54:56 -05:00
|
|
|
ioctl(fd, UI_ABS_SETUP, &setup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rel
|
|
|
|
if (dev->rel_count > 0) {
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_REL);
|
|
|
|
for (int i = 0; i < dev->rel_count; i++) {
|
|
|
|
ioctl(fd, UI_SET_RELBIT, dev->rel_id[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key
|
|
|
|
if (dev->key_count > 0) {
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_KEY);
|
|
|
|
for (int i = 0; i < dev->key_count; i++) {
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, dev->key_id[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
ClientController *ctr = &config.controllers[dev->index];
|
|
|
|
|
|
|
|
struct uinput_setup setup = {0};
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
setup.id.bustype = BUS_VIRTUAL;
|
2022-10-07 18:36:53 -05:00
|
|
|
setup.id.vendor = ctr->device_vendor;
|
|
|
|
setup.id.product = ctr->device_product;
|
2022-08-31 16:24:26 -05:00
|
|
|
setup.id.version = VIRTUAL_DEVICE_VERSION;
|
2022-10-07 18:36:53 -05:00
|
|
|
strncpy(setup.name, ctr->device_name, UINPUT_MAX_NAME_SIZE);
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
ioctl(fd, UI_DEV_SETUP, &setup);
|
|
|
|
ioctl(fd, UI_DEV_CREATE);
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
MessageDeviceInfo * dst = vec_get(&devices_info, dev->index);
|
|
|
|
|
|
|
|
memcpy(dst, dev, sizeof(MessageDeviceInfo));
|
|
|
|
printf("CLIENT: Got device [%d]: '%s' (abs: %d, rel: %d, key: %d)\n", dev->index, ctr->device_name, dev->abs_count, dev->rel_count, dev->key_count);
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// Send an event to uinput, device must exist
|
2022-10-07 18:36:53 -05:00
|
|
|
bool device_emit(int index, uint16_t type, uint16_t id, uint32_t value) {
|
|
|
|
if(index >= devices_fd.len) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = *(int*) vec_get(&devices_fd, index);
|
|
|
|
struct input_event event = {0};
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
event.type = type;
|
|
|
|
event.code = id;
|
|
|
|
event.value = value;
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
return write(fd, &event, sizeof(event)) != sizeof(event);
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// Update device with report
|
2022-08-30 17:54:56 -05:00
|
|
|
void device_handle_report(MessageDeviceReport *report) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (!device_exists(report->index)) {
|
|
|
|
printf("CLIENT: [%d] Got report before device info\n", report->index);
|
2022-08-30 17:54:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
MessageDeviceInfo * info = vec_get(&devices_info, report->index);
|
|
|
|
|
|
|
|
if (report->abs_count != info->abs_count || report->rel_count != info->rel_count ||
|
|
|
|
report->key_count != info->key_count) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("CLIENT: Report doesn't match with device info\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < report->abs_count; i++) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (device_emit(report->index, EV_ABS, info->abs_id[i], report->abs[i]) != 0) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("CLIENT: Error writing abs event to uinput\n");
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < report->rel_count; i++) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (device_emit(report->index, EV_REL, info->rel_id[i], report->rel[i]) != 0) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("CLIENT: Error writing rel event to uinput\n");
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < report->key_count; i++) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (device_emit(report->index, EV_KEY, info->key_id[i], (uint32_t)(!report->key[i]) - 1) != 0) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("CLIENT: Error writing key event to uinput\n");
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
2022-08-31 16:24:26 -05:00
|
|
|
// Reports are sent by the server every time the server receives an EV_SYN from the physical device, so we
|
|
|
|
// send one when we receive the report to match
|
2022-10-07 18:36:53 -05:00
|
|
|
device_emit(report->index, EV_SYN, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_devices() {
|
|
|
|
devices_fd = vec_of(int);
|
|
|
|
devices_info = vec_of(MessageDeviceInfo);
|
|
|
|
|
|
|
|
MessageDeviceInfo no_info = {0};
|
|
|
|
no_info.code = NoMessage;
|
|
|
|
|
|
|
|
for (int i = 0; i < config.controller_count; i++) {
|
|
|
|
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("CLIENT: Can't open /dev/uinput, aborting now");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec_push(&devices_fd, &fd);
|
|
|
|
vec_push(&devices_info, &no_info);
|
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_fifo();
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// (Re)Open the fifo
|
2022-08-30 17:54:56 -05:00
|
|
|
void open_fifo() {
|
|
|
|
close(fifo);
|
2022-10-07 18:36:53 -05:00
|
|
|
fifo = open(config.fifo_path, O_RDONLY | O_NONBLOCK);
|
2022-08-30 17:54:56 -05:00
|
|
|
if (fifo < 0 && fifo_attempt == 0) {
|
|
|
|
fifo_attempt++;
|
2022-10-07 18:36:53 -05:00
|
|
|
unlink(config.fifo_path);
|
2022-08-30 17:54:56 -05:00
|
|
|
setup_fifo();
|
|
|
|
} else if (fifo < 0) {
|
|
|
|
panicf("CLIENT: Couldn't open fifo, aborting\n");
|
|
|
|
}
|
2022-08-31 16:24:26 -05:00
|
|
|
fifo_attempt = 0;
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// Ensure the fifo exists and opens it (also setup poll_fd)
|
2022-08-30 17:54:56 -05:00
|
|
|
void setup_fifo() {
|
|
|
|
mode_t prev = umask(0);
|
2022-10-07 18:36:53 -05:00
|
|
|
mkfifo(config.fifo_path, 0666);
|
2022-08-30 17:54:56 -05:00
|
|
|
umask(prev);
|
|
|
|
|
|
|
|
open_fifo();
|
|
|
|
|
|
|
|
fifo_poll->fd = fifo;
|
|
|
|
fifo_poll->events = POLLIN;
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// (Re)Connect to the server
|
2022-08-30 17:54:56 -05:00
|
|
|
void connect_server() {
|
2022-10-07 18:36:53 -05:00
|
|
|
while (true) {
|
2022-08-30 17:54:56 -05:00
|
|
|
if (sock > 0) {
|
2022-08-31 16:24:26 -05:00
|
|
|
// Close previous connection
|
2022-08-30 17:54:56 -05:00
|
|
|
shutdown(sock, SHUT_RDWR);
|
2022-10-07 18:36:53 -05:00
|
|
|
destroy_devices();
|
2022-08-30 17:54:56 -05:00
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2022-08-31 16:24:26 -05:00
|
|
|
if (sock < 0) {
|
2022-08-30 17:54:56 -05:00
|
|
|
panicf("Couldn't create socket\n");
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) {
|
2022-10-07 18:36:53 -05:00
|
|
|
printf("CLIENT: Couldn't connect to %s:%d, retrying in %lu.%09lus\n", server_addrp, server_port,
|
|
|
|
config.retry_delay.tv_sec, config.retry_delay.tv_nsec);
|
|
|
|
nanosleep(&config.retry_delay, NULL);
|
2022-08-30 17:54:56 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-31 16:24:26 -05:00
|
|
|
// Set non blocking, only do that after connection (instead of with SOCK_NONBLOCK at socket creation)
|
|
|
|
// because we want to block on the connection itself
|
2022-08-30 17:54:56 -05:00
|
|
|
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
socket_poll->fd = sock;
|
|
|
|
printf("CLIENT: Connected !\n");
|
2022-10-07 18:36:53 -05:00
|
|
|
|
|
|
|
uint8_t buf[2048] __attribute__((aligned(4)));
|
|
|
|
|
|
|
|
int len = msg_serialize(buf, 2048, (Message *)&device_request);
|
|
|
|
if (len > 0) {
|
|
|
|
if (send(sock, buf, len, 0) > 0) {
|
|
|
|
printf("CLIENT: Sent device request\n");
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// Setup server address and connects to it (+ setup poll_fd)
|
2022-08-30 17:54:56 -05:00
|
|
|
void setup_server(char *address, uint16_t port) {
|
|
|
|
// setup address
|
|
|
|
server_addr.sin_family = AF_INET;
|
2022-08-31 16:24:26 -05:00
|
|
|
|
|
|
|
if (inet_pton(AF_INET, address, &server_addr.sin_addr) == 0) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("CLIENT: failed to parse address '%s', defaulting to 0.0.0.0 (localhost)\n", address);
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
inet_ntop(AF_INET, &server_addr.sin_addr, server_addrp, 64);
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
server_port = port;
|
|
|
|
server_addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
socket_poll->events = POLLIN;
|
|
|
|
|
|
|
|
connect_server();
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
void build_device_request() {
|
|
|
|
char **tags = malloc(config.controller_count * sizeof(char *));
|
|
|
|
for (int i = 0; i < config.controller_count; i++) {
|
|
|
|
tags[i] = config.controllers[i].tag;
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
2022-10-07 18:36:53 -05:00
|
|
|
|
|
|
|
device_request.code = Request;
|
|
|
|
device_request.request_count = config.controller_count;
|
|
|
|
device_request.requests = tags;
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:36:53 -05:00
|
|
|
void client_run(char *address, uint16_t port, char *config_path) {
|
|
|
|
// Parse the config
|
|
|
|
{
|
|
|
|
FILE *configfd = fopen(config_path, "r");
|
|
|
|
if (configfd == NULL) {
|
|
|
|
perror("CLIENT: Couldn't open config file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *cbuf = malloc(8192);
|
|
|
|
uint8_t *jbuf = (uint8_t *)cbuf + 4096;
|
|
|
|
|
|
|
|
int len = fread(cbuf, 1, 4096, configfd);
|
|
|
|
if (json_parse(cbuf, len, jbuf, 4096) != 0) {
|
|
|
|
printf("CLIENT: Couldn't parse config, %s (at index %lu)\n", json_strerr(), json_errloc());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_adapt(jbuf, &ConfigAdapter, &config);
|
|
|
|
|
|
|
|
free(cbuf);
|
|
|
|
fclose(configfd);
|
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
|
|
|
|
setup_fifo();
|
2022-10-07 18:36:53 -05:00
|
|
|
build_device_request();
|
2022-08-30 17:54:56 -05:00
|
|
|
setup_server(address, port);
|
2022-10-07 18:36:53 -05:00
|
|
|
setup_devices();
|
2022-08-30 17:54:56 -05:00
|
|
|
|
2022-08-31 11:54:14 -05:00
|
|
|
uint8_t buf[2048] __attribute__((aligned(4)));
|
|
|
|
uint8_t json_buf[2048] __attribute__((aligned(8)));
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
while (1) {
|
|
|
|
int rc = poll(poll_fds, 2, -1);
|
|
|
|
if (rc < 0) {
|
2022-10-07 18:36:53 -05:00
|
|
|
perror("CLIENT: Error on poll");
|
2022-08-30 17:54:56 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:54:14 -05:00
|
|
|
if (fifo_poll->revents & POLLIN || fifo_poll->revents & POLLHUP || fifo_poll->revents & POLLERR) {
|
2022-08-30 17:54:56 -05:00
|
|
|
int len = read(fifo, buf, 2048);
|
|
|
|
if (len <= 0) {
|
|
|
|
open_fifo();
|
|
|
|
} else {
|
|
|
|
// We've got data from the fifo
|
2022-08-31 11:54:14 -05:00
|
|
|
int rc = json_parse((char *)buf, len, json_buf, 2048);
|
|
|
|
if (rc < 0) {
|
2022-10-07 18:36:53 -05:00
|
|
|
printf("CLIENT: Error when parsing fifo message as json (%s at index %lu)\n", json_strerr(), json_errloc());
|
2022-08-31 11:54:14 -05:00
|
|
|
} else {
|
|
|
|
MessageControllerState msg;
|
2022-10-07 18:36:53 -05:00
|
|
|
msg.code = ControllerState;
|
|
|
|
json_adapt(json_buf, &ControllerStateAdapter, &msg);
|
2022-08-31 11:54:14 -05:00
|
|
|
|
|
|
|
int len = msg_serialize(buf, 2048, (Message *)&msg);
|
|
|
|
if (len > 0) {
|
|
|
|
if (send(sock, buf, len, 0) > 0) {
|
|
|
|
printf("CLIENT: Sent controller state: #%02x%02x%02x flash: (%d, %d) rumble: "
|
2022-10-07 18:36:53 -05:00
|
|
|
"(%d, %d) -> [%d]\n",
|
|
|
|
msg.led[0], msg.led[1], msg.led[2], msg.flash_on, msg.flash_off, msg.small_rumble,
|
|
|
|
msg.big_rumble, msg.index);
|
2022-08-31 11:54:14 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A broken or closed socket produces a POLLIN event, so we check for error on the recv
|
|
|
|
if (socket_poll->revents & POLLIN) {
|
|
|
|
int len = recv(sock, buf, 2048, 0);
|
|
|
|
if (len <= 0) {
|
|
|
|
printf("CLIENT: Lost connection to server, reconnecting\n");
|
|
|
|
connect_server();
|
2022-10-07 18:36:53 -05:00
|
|
|
// we can use continue here because there's nothing after, unlike above for fifo (this reduces
|
2022-08-31 16:24:26 -05:00
|
|
|
// indentation instead of needing an else block)
|
2022-08-30 17:54:56 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've got data from the server
|
|
|
|
if (msg_deserialize(buf, len, &message) != 0) {
|
|
|
|
printf("CLIENT: Couldn't parse message (code: %d, len: %d)\n", buf[0], len);
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
int l = len > 100 ? 100 : len;
|
2022-08-30 17:56:56 -05:00
|
|
|
for (int i = 0; i < l; i++) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("%02x", buf[i]);
|
|
|
|
}
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:56:56 -05:00
|
|
|
if (len > 100) {
|
2022-08-30 17:54:56 -05:00
|
|
|
printf(" ... %d more bytes", len - 100);
|
|
|
|
}
|
2022-08-31 16:24:26 -05:00
|
|
|
|
2022-08-30 17:54:56 -05:00
|
|
|
printf("\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.code == DeviceInfo) {
|
2022-10-07 18:36:53 -05:00
|
|
|
if (device_exists(message.device_info.index)) {
|
|
|
|
printf("CLIENT: Got more than one device info for same device\n");
|
2022-08-31 16:24:26 -05:00
|
|
|
}
|
2022-08-30 17:54:56 -05:00
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
device_init((MessageDeviceInfo *)&message);
|
2022-10-07 18:36:53 -05:00
|
|
|
printf("CLIENT: Got device %d\n", message.device_info.index);
|
2022-08-30 17:54:56 -05:00
|
|
|
} else if (message.code == DeviceReport) {
|
|
|
|
device_handle_report((MessageDeviceReport *)&message);
|
2022-10-07 18:36:53 -05:00
|
|
|
} else if (message.code == DeviceDestroy) {
|
|
|
|
device_destroy(message.destroy.index);
|
|
|
|
printf("CLIENT: Lost device %d\n", message.destroy.index);
|
2022-08-30 17:54:56 -05:00
|
|
|
} else {
|
|
|
|
printf("CLIENT: Illegal message\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|