mirror of
https://github.com/stupidcomputer/jsfw.git
synced 2024-12-26 21:42:23 -06:00
server is mostly done
This commit is contained in:
parent
afe99fc054
commit
fd719fc1b1
7
hid.c
7
hid.c
@ -57,6 +57,13 @@ void setup_device(PhysicalDevice *dev) {
|
|||||||
dev->device_info.rel_count = 0;
|
dev->device_info.rel_count = 0;
|
||||||
dev->device_info.key_count = 0;
|
dev->device_info.key_count = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < ABS_CNT; i++)
|
||||||
|
dev->mapping.abs_indices[i] = -1;
|
||||||
|
for(int i = 0; i < REL_CNT; i++)
|
||||||
|
dev->mapping.key_indices[i] = -1;
|
||||||
|
for(int i = 0; i < KEY_CNT; i++)
|
||||||
|
dev->mapping.key_indices[i] = -1;
|
||||||
|
|
||||||
uint8_t bits[EV_MAX] = {};
|
uint8_t bits[EV_MAX] = {};
|
||||||
uint8_t feat_bits[KEY_MAX] = {};
|
uint8_t feat_bits[KEY_MAX] = {};
|
||||||
ioctl(dev->event, EVIOCGBIT(0, EV_MAX), bits);
|
ioctl(dev->event, EVIOCGBIT(0, EV_MAX), bits);
|
||||||
|
80
net.c
80
net.c
@ -1,4 +1,5 @@
|
|||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
Message msg_device_info() {
|
Message msg_device_info() {
|
||||||
MessageDeviceInfo m;
|
MessageDeviceInfo m;
|
||||||
@ -17,18 +18,20 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *dst) {
|
|||||||
uint8_t code_byte = buf[0];
|
uint8_t code_byte = buf[0];
|
||||||
MessageCode code = (MessageCode)code_byte;
|
MessageCode code = (MessageCode)code_byte;
|
||||||
|
|
||||||
|
uint8_t abs, rel, key;
|
||||||
|
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case DeviceInfo:
|
case DeviceInfo:
|
||||||
if (len < 3)
|
if (len < 3)
|
||||||
return -1;
|
return -1;
|
||||||
uint8_t abs = buf[1];
|
abs = buf[1];
|
||||||
uint8_t rel = buf[2];
|
rel = buf[2];
|
||||||
uint8_t key = buf[3];
|
key = buf[3];
|
||||||
buf += 4;
|
buf += 4;
|
||||||
if (MSS_DEVICE_INFO(abs, rel, key) > len)
|
if (MSS_DEVICE_INFO(abs, rel, key) > len)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
dst->code = code;
|
dst->device_info.code = code;
|
||||||
dst->device_info.abs_count = abs;
|
dst->device_info.abs_count = abs;
|
||||||
dst->device_info.rel_count = rel;
|
dst->device_info.rel_count = rel;
|
||||||
dst->device_info.key_count = key;
|
dst->device_info.key_count = key;
|
||||||
@ -55,9 +58,34 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *dst) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
case DeviceReport:
|
case DeviceReport:
|
||||||
if (len < MSS_DEVICE_REPORT)
|
if (len < 3)
|
||||||
return -1;
|
return -1;
|
||||||
dst->code = code;
|
|
||||||
|
abs = buf[1];
|
||||||
|
rel = buf[2];
|
||||||
|
key = buf[3];
|
||||||
|
buf += 4;
|
||||||
|
if (len < MSS_DEVICE_REPORT(abs, rel, key))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
dst->device_report.code = code;
|
||||||
|
dst->device_report.abs_count = abs;
|
||||||
|
dst->device_report.rel_count = rel;
|
||||||
|
dst->device_report.key_count = key;
|
||||||
|
|
||||||
|
for (int i = 0; i < abs; i++) {
|
||||||
|
dst->device_report.abs[i] = *(uint32_t *)buf;
|
||||||
|
buf += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rel; i++) {
|
||||||
|
dst->device_report.rel[i] = *(uint32_t *)buf;
|
||||||
|
buf += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < key; i++)
|
||||||
|
dst->device_report.key[i] = *(buf++);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
case DeviceDestroy:
|
case DeviceDestroy:
|
||||||
if (len < MSS_DEVICE_DESTROY)
|
if (len < MSS_DEVICE_DESTROY)
|
||||||
@ -82,18 +110,19 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *dst) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The indices have to match with msg_deserialize
|
|
||||||
int msg_serialize(uint8_t *buf, size_t len, Message *msg) {
|
int msg_serialize(uint8_t *buf, size_t len, Message *msg) {
|
||||||
// If len is 0 we can't serialize any message
|
// If len is 0 we can't serialize any message
|
||||||
if (len-- == 0)
|
if (len-- == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
uint8_t abs, rel, key;
|
||||||
|
|
||||||
switch (msg->code) {
|
switch (msg->code) {
|
||||||
case DeviceInfo:; // semicolon needed here
|
case DeviceInfo:
|
||||||
uint8_t abs = msg->device_info.abs_count;
|
abs = msg->device_info.abs_count;
|
||||||
uint8_t rel = msg->device_info.rel_count;
|
rel = msg->device_info.rel_count;
|
||||||
uint8_t key = msg->device_info.key_count;
|
key = msg->device_info.key_count;
|
||||||
if (len < MSS_DEVICE_INFO(abs, rel, len))
|
if (len < MSS_DEVICE_INFO(abs, rel, key))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
buf[0] = (uint8_t)msg->code;
|
buf[0] = (uint8_t)msg->code;
|
||||||
@ -123,11 +152,31 @@ int msg_serialize(uint8_t *buf, size_t len, Message *msg) {
|
|||||||
|
|
||||||
return MSS_DEVICE_INFO(abs, rel, key) + 1;
|
return MSS_DEVICE_INFO(abs, rel, key) + 1;
|
||||||
case DeviceReport:
|
case DeviceReport:
|
||||||
if (len < MSS_DEVICE_REPORT)
|
abs = msg->device_report.abs_count;
|
||||||
|
rel = msg->device_report.rel_count;
|
||||||
|
key = msg->device_report.key_count;
|
||||||
|
if (len < MSS_DEVICE_REPORT(abs, rel, key))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
buf[0] = (uint8_t)msg->code;
|
buf[0] = (uint8_t)msg->code;
|
||||||
return MSS_DEVICE_REPORT + 1;
|
buf[1] = abs;
|
||||||
|
buf[2] = rel;
|
||||||
|
buf[3] = key;
|
||||||
|
buf += 4;
|
||||||
|
|
||||||
|
for (int i = 0; i < abs; i++) {
|
||||||
|
*(uint32_t *)buf = msg->device_report.abs[i];
|
||||||
|
buf += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rel; i++) {
|
||||||
|
*(uint32_t *)buf = msg->device_report.rel[i];
|
||||||
|
buf += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < key; i++)
|
||||||
|
*(buf++) = msg->device_report.key[i];
|
||||||
|
|
||||||
|
return MSS_DEVICE_REPORT(abs, rel, key) + 1;
|
||||||
case DeviceDestroy:
|
case DeviceDestroy:
|
||||||
if (len < MSS_DEVICE_DESTROY)
|
if (len < MSS_DEVICE_DESTROY)
|
||||||
return -1;
|
return -1;
|
||||||
@ -148,6 +197,7 @@ int msg_serialize(uint8_t *buf, size_t len, Message *msg) {
|
|||||||
buf[7] = msg->controller_state.flash_off;
|
buf[7] = msg->controller_state.flash_off;
|
||||||
return MSS_CONTROLLER_STATE + 1;
|
return MSS_CONTROLLER_STATE + 1;
|
||||||
default:
|
default:
|
||||||
|
printf("ERR(msg_serialize): Trying to serialize unknown message of code %d\n", msg->code);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
net.h
11
net.h
@ -12,6 +12,7 @@ typedef enum {
|
|||||||
ControllerState = 4,
|
ControllerState = 4,
|
||||||
} MessageCode;
|
} MessageCode;
|
||||||
|
|
||||||
|
// TODO: replace counts by uint16_t
|
||||||
typedef struct {
|
typedef struct {
|
||||||
MessageCode code;
|
MessageCode code;
|
||||||
|
|
||||||
@ -36,8 +37,16 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
MessageCode code;
|
MessageCode code;
|
||||||
|
|
||||||
|
uint8_t abs_count;
|
||||||
|
uint8_t rel_count;
|
||||||
|
uint8_t key_count;
|
||||||
|
|
||||||
|
uint32_t abs[ABS_CNT];
|
||||||
|
uint32_t rel[REL_CNT];
|
||||||
|
uint8_t key[KEY_CNT];
|
||||||
} MessageDeviceReport;
|
} MessageDeviceReport;
|
||||||
#define MSS_DEVICE_REPORT 0
|
#define MSS_DEVICE_REPORT(abs, rel, key) (3 + abs * 4 + rel * 4 + key * 1)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
MessageCode code;
|
MessageCode code;
|
||||||
|
75
server.c
75
server.c
@ -47,18 +47,28 @@ void *server_handle_conn(void *args_) {
|
|||||||
int len = msg_serialize(buf, 2048, (Message *)&dev.device_info);
|
int len = msg_serialize(buf, 2048, (Message *)&dev.device_info);
|
||||||
write(args->socket, buf, len);
|
write(args->socket, buf, len);
|
||||||
|
|
||||||
struct pollfd pfd[2] = {};
|
struct pollfd pfds[2] = {};
|
||||||
pfd[0].fd = args->socket;
|
struct pollfd *socket_poll = &pfds[0];
|
||||||
pfd[0].events = POLLIN;
|
struct pollfd *event_poll = &pfds[1];
|
||||||
pfd[1].fd = dev.event;
|
|
||||||
pfd[1].events = POLLIN;
|
socket_poll->fd = args->socket;
|
||||||
|
socket_poll->events = POLLIN;
|
||||||
|
event_poll->fd = dev.event;
|
||||||
|
event_poll->events = POLLIN;
|
||||||
|
|
||||||
|
MessageDeviceReport report = {};
|
||||||
|
|
||||||
|
report.code = DeviceReport;
|
||||||
|
report.abs_count = dev.device_info.abs_count;
|
||||||
|
report.rel_count = dev.device_info.rel_count;
|
||||||
|
report.key_count = dev.device_info.key_count;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int rc = poll(pfd, 1, -1);
|
int rc = poll(pfds, 2, -1);
|
||||||
if (rc < 0) // error (connection closed)
|
if (rc < 0) // error (connection closed)
|
||||||
goto conn_end;
|
goto conn_end;
|
||||||
|
|
||||||
if (pfd[0].revents & POLLIN) {
|
if (socket_poll->revents & POLLIN) {
|
||||||
int len = recv(args->socket, buf, 2048, 0);
|
int len = recv(args->socket, buf, 2048, 0);
|
||||||
if (len <= 0)
|
if (len <= 0)
|
||||||
goto conn_end;
|
goto conn_end;
|
||||||
@ -66,8 +76,8 @@ void *server_handle_conn(void *args_) {
|
|||||||
Message msg;
|
Message msg;
|
||||||
if (msg_deserialize(buf, len, &msg) == 0) {
|
if (msg_deserialize(buf, len, &msg) == 0) {
|
||||||
|
|
||||||
if(msg.code == ControllerState) {
|
if (msg.code == ControllerState) {
|
||||||
apply_controller_state(&dev, (MessageControllerState*)&msg);
|
apply_controller_state(&dev, (MessageControllerState *)&msg);
|
||||||
} else {
|
} else {
|
||||||
printf("CONN(%d): Illegal message\n", args->id);
|
printf("CONN(%d): Illegal message\n", args->id);
|
||||||
}
|
}
|
||||||
@ -76,8 +86,53 @@ void *server_handle_conn(void *args_) {
|
|||||||
printf("CONN(%d): Couldn't parse message.\n", args->id);
|
printf("CONN(%d): Couldn't parse message.\n", args->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pfd[1].revents & POLLIN) {
|
if (event_poll->revents & POLLIN) {
|
||||||
|
struct input_event event;
|
||||||
|
int len = read(dev.event, &event, sizeof(struct input_event));
|
||||||
|
if (len <= 0)
|
||||||
|
goto conn_end;
|
||||||
|
if (len < sizeof(struct input_event)) {
|
||||||
|
printf("CONN(%d): error reading event\n", args->id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == EV_SYN) {
|
||||||
|
int len = msg_serialize(buf, 2048, (Message *)&report);
|
||||||
|
|
||||||
|
if (len < 0) {
|
||||||
|
printf("CONN(%d): Couldn't serialize report %d\n", args->id, len);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
write(args->socket, buf, len);
|
||||||
|
} else if (event.type == EV_ABS) {
|
||||||
|
int index = dev.mapping.abs_indices[event.code];
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
printf("CONN(%d): Invalid abs\n", args->id);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
report.abs[index] = event.value;
|
||||||
|
} else if (event.type == EV_REL) {
|
||||||
|
int index = dev.mapping.rel_indices[event.code];
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
printf("CONN(%d): Invalid rel\n", args->id);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
report.rel[index] = event.value;
|
||||||
|
} else if (event.type == EV_KEY) {
|
||||||
|
int index = dev.mapping.key_indices[event.code];
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
printf("CONN(%d): Invalid key\n", args->id);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
report.key[index] = !!event.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user