housekeeping 2

This commit is contained in:
viandoxdev 2022-08-31 18:59:06 +02:00
parent e11c8b5c48
commit 2bd21ee138
No known key found for this signature in database
GPG Key ID: AF1410C5BC10AA25
8 changed files with 202 additions and 196 deletions

View File

@ -6,12 +6,14 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <math.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

1
hid.c
View File

@ -4,6 +4,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <pthread.h>
#include <stdbool.h>

2
hid.h
View File

@ -3,7 +3,7 @@
#define HID_H_
#include "net.h"
#include <linux/input.h>
#include <linux/input-event-codes.h>
#include <stdint.h>
typedef uint64_t uniq_t;

207
json.c
View File

@ -4,21 +4,16 @@
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static JSONError jerrno = NoError;
static size_t jerr_index = 0;
const char * json_strerr() {
return JSONErrorMessage[jerrno];
}
const char *json_strerr() { return JSONErrorMessage[jerrno]; }
size_t json_err_loc() {
return jerr_index;
}
size_t json_err_loc() { return jerr_index; }
// Shorthand to set jerno and return -1;
static inline int set_jerrno(JSONError err) {
@ -278,16 +273,18 @@ static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **r
header->type = (uint32_t)Boolean;
header->len = 8;
if(**buf == 't') {
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
if(strncmp(*buf, "true", 4) != 0) {
if (**buf == 't') {
if (*buf + 4 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "true", 4) != 0) {
return set_jerrno(BadKeyword);
}
*buf += 4;
*value = 1;
} else if(**buf == 'f') {
if(*buf + 5 > buf_end) return set_jerrno(SrcOverflow);
if(strncmp(*buf, "false", 5) != 0) {
} else if (**buf == 'f') {
if (*buf + 5 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "false", 5) != 0) {
return set_jerrno(BadKeyword);
}
*buf += 5;
@ -312,8 +309,9 @@ static int json_parse_null(const char **buf, const char *buf_end, uint8_t **rest
header->type = (uint32_t)Null;
header->len = 0;
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
if(strncmp(*buf, "null", 4) != 0) {
if (*buf + 4 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "null", 4) != 0) {
return set_jerrno(BadKeyword);
}
*buf += 4;
@ -330,25 +328,30 @@ static int json_parse_array(const char **buf, const char *buf_end, uint8_t **res
JSONHeader *header = (JSONHeader *)(*dst);
*dst += sizeof(JSONHeader);
uint8_t * dst_arr_start = *dst;
uint8_t *dst_arr_start = *dst;
header->type = (uint32_t)Array;
(*buf)++; // Skip [
// skip initial whitespace
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if(*buf == buf_end) return set_jerrno(SrcOverflow);
if(**buf == ']') {
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ']') {
header->len = 0;
return 0;
}
while(1) {
if (json_parse_value(buf, buf_end, dst, dst_end) != 0) return -1;
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if (*buf == buf_end) return set_jerrno(SrcOverflow);
if(**buf == ',') {
while (1) {
if (json_parse_value(buf, buf_end, dst, dst_end) != 0)
return -1;
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ',') {
(*buf)++;
} else if(**buf == ']') {
} else if (**buf == ']') {
(*buf)++;
break;
} else {
@ -369,41 +372,51 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
JSONHeader *header = (JSONHeader *)(*dst);
*dst += sizeof(JSONHeader);
uint8_t * dst_obj_start = *dst;
uint8_t *dst_obj_start = *dst;
header->type = (uint32_t)Object;
(*buf)++; // Skip {
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if(*buf == buf_end) return set_jerrno(SrcOverflow);
if(**buf == '}') {
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == '}') {
header->len = 0;
return 0;
}
while(1) {
while (1) {
// Skip whitespace before key
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// Parse key
if (json_parse_string(buf, buf_end, dst, dst_end) != 0) return -1;
if (json_parse_string(buf, buf_end, dst, dst_end) != 0)
return -1;
// Skip whitespace after key
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// There should be at least one char
if(*buf == buf_end) return set_jerrno(SrcOverflow);
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
// There should be a colon
if(**buf != ':') return set_jerrno(ObjectBadChar);
if (**buf != ':')
return set_jerrno(ObjectBadChar);
// Skip colon
(*buf)++;
// Parse value (takes char of whitespace)
if (json_parse_value(buf, buf_end, dst, dst_end) != 0) return -1;
if (json_parse_value(buf, buf_end, dst, dst_end) != 0)
return -1;
// Skip whitespace after value
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// There should be at least one char (} or ,)
if (*buf == buf_end) return set_jerrno(SrcOverflow);
if(**buf == ',') {
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ',') {
(*buf)++;
} else if(**buf == '}') {
} else if (**buf == '}') {
(*buf)++;
break;
} else {
@ -418,7 +431,8 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst,
const uint8_t *dst_end) {
for (; *buf < buf_end; (*buf)++) {
if(is_whitespace(**buf)) continue;
if (is_whitespace(**buf))
continue;
switch (**buf) {
case '"':
@ -453,20 +467,20 @@ static int json_parse_value(const char **buf, const char *buf_end, uint8_t **res
return 0;
}
int json_parse(const char * src, size_t src_len, uint8_t * dst, size_t dst_len) {
int json_parse(const char *src, size_t src_len, uint8_t *dst, size_t dst_len) {
memset(dst, 0, dst_len);
const char * buf = src;
const char * buf_end = src + src_len;
uint8_t * dst_end = dst + dst_len;
const char *buf = src;
const char *buf_end = src + src_len;
uint8_t *dst_end = dst + dst_len;
int rc = json_parse_value(&buf, buf_end, &dst, dst_end);
jerr_index = buf - src;
return rc;
}
void json_print_value(uint8_t ** buf) {
JSONHeader * header = (JSONHeader*) *buf;
void json_print_value(uint8_t **buf) {
JSONHeader *header = (JSONHeader *)*buf;
*buf += sizeof(header);
switch(header->type) {
switch (header->type) {
case String:
printf("\"%.*s\"", header->len, *buf);
*buf += align_8(header->len);
@ -475,60 +489,54 @@ void json_print_value(uint8_t ** buf) {
printf("%lf", *(double *)*buf);
*buf += sizeof(double);
break;
case Boolean:
{
uint64_t value = *(uint64_t*)*buf;
if(value == 1) {
case Boolean: {
uint64_t value = *(uint64_t *)*buf;
if (value == 1) {
printf("true");
} else if(value == 0) {
} else if (value == 0) {
printf("false");
} else {
printf("(boolean) garbage");
}
*buf += 8;
}
break;
} break;
case Null:
printf("null");
break;
case Array:
{
uint8_t * end = *buf + header->len;
case Array: {
uint8_t *end = *buf + header->len;
printf("[");
while(1) {
while (1) {
json_print_value(buf);
if(*buf < end) {
if (*buf < end) {
printf(", ");
} else {
printf("]");
break;
}
}
}
break;
case Object:
{
uint8_t * end = *buf + header->len;
} break;
case Object: {
uint8_t *end = *buf + header->len;
printf("{");
while(1) {
while (1) {
json_print_value(buf);
printf(":");
json_print_value(buf);
if(*buf < end) {
if (*buf < end) {
printf(",");
} else {
printf("}");
break;
}
}
}
break;
} break;
}
}
struct Test {
double a;
char * b;
char *b;
};
const JSONAdapter TestAdapter[] = {
@ -536,35 +544,34 @@ const JSONAdapter TestAdapter[] = {
{".b", String, offsetof(struct Test, b)},
};
static void json_adapt_set(uint8_t * buf, JSONAdapter * adapters, size_t adapter_count, void * ptr, char * path) {
JSONHeader * header = (JSONHeader *)buf;
for(int i = 0; i < adapter_count; i++) {
if(strcmp(path, adapters[i].path) == 0 && header->type == adapters[i].type) {
void * p = ptr + adapters[i].offset;
switch(header->type) {
case String:
{
char * v = malloc(header->len + 1);
static void json_adapt_set(uint8_t *buf, JSONAdapter *adapters, size_t adapter_count, void *ptr, char *path) {
JSONHeader *header = (JSONHeader *)buf;
for (int i = 0; i < adapter_count; i++) {
if (strcmp(path, adapters[i].path) == 0 && header->type == adapters[i].type) {
void *p = ptr + adapters[i].offset;
switch (header->type) {
case String: {
char *v = malloc(header->len + 1);
strncpy(v, (char *)(buf + sizeof(JSONHeader)), header->len);
v[header->len] = '\0';
*(char**)p = v;
}
break;
*(char **)p = v;
} break;
case Number:
*(double*)p = *(double*)(buf + sizeof(JSONHeader));
*(double *)p = *(double *)(buf + sizeof(JSONHeader));
break;
case Boolean:
*(bool*)p = *(uint64_t*)(buf + sizeof(JSONHeader)) == 1;
*(bool *)p = *(uint64_t *)(buf + sizeof(JSONHeader)) == 1;
break;
}
}
}
}
static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapter_count, void * ptr, char * full_path, char * path) {
JSONHeader * header = (JSONHeader *)*buf;
static void json_adapt_priv(uint8_t **buf, JSONAdapter *adapters, size_t adapter_count, void *ptr,
char *full_path, char *path) {
JSONHeader *header = (JSONHeader *)*buf;
switch(header->type) {
switch (header->type) {
case String:
json_adapt_set(*buf, adapters, adapter_count, ptr, full_path);
*buf += sizeof(JSONHeader) + align_8(header->len);
@ -580,22 +587,19 @@ static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapt
case Null:
*buf += sizeof(JSONHeader);
break;
case Array:
{
case Array: {
*buf += sizeof(JSONHeader);
uint8_t * end = *buf + header->len;
for(size_t index = 0; *buf < end; index++) {
uint8_t *end = *buf + header->len;
for (size_t index = 0; *buf < end; index++) {
int len = sprintf(path, ".%lu", index);
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
}
}
break;
case Object:
{
} break;
case Object: {
*buf += sizeof(JSONHeader);
uint8_t * end = *buf + header->len;
while(*buf < end) {
JSONHeader * key_header = (JSONHeader*)*buf;
uint8_t *end = *buf + header->len;
while (*buf < end) {
JSONHeader *key_header = (JSONHeader *)*buf;
*buf += sizeof(JSONHeader);
int len = sprintf(path, ".%.*s", key_header->len, *buf);
@ -603,12 +607,11 @@ static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapt
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
}
}
break;
} break;
}
}
void json_adapt(uint8_t * buf, JSONAdapter * adapters, size_t adapter_count, void * ptr) {
void json_adapt(uint8_t *buf, JSONAdapter *adapters, size_t adapter_count, void *ptr) {
char path[512] = ".";
json_adapt_priv(&buf, adapters, adapter_count, ptr, path, path);
}

15
json.h
View File

@ -1,10 +1,9 @@
// vi:ft=c
#ifndef JSON_H_
#define JSON_H_
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <stddef.h>
#include <stdint.h>
typedef struct __attribute__((packed, aligned(8))) {
uint32_t type;
@ -39,7 +38,7 @@ typedef enum {
} JSONError;
#ifdef JSON_C_
static const char * JSONErrorMessage[JERRORNO_MAX + 1] = {
static const char *JSONErrorMessage[JERRORNO_MAX + 1] = {
"No error",
"Destination buffer is not big enough",
"Source buffer overflowed before parsing finished",
@ -50,19 +49,19 @@ static const char * JSONErrorMessage[JERRORNO_MAX + 1] = {
"Illegal escape in string",
"Unexpected character in number",
"Unexpected character in object",
"?"
"?",
};
#endif
typedef struct {
char * path;
char *path;
JSONType type;
size_t offset;
} JSONAdapter;
void json_adapt(uint8_t * buf, JSONAdapter * adapters, size_t adapter_count, void * ptr);
int json_parse(const char * src, size_t src_len, uint8_t * dst, size_t dst_len);
const char * json_strerr();
void json_adapt(uint8_t *buf, JSONAdapter *adapters, size_t adapter_count, void *ptr);
int json_parse(const char *src, size_t src_len, uint8_t *dst, size_t dst_len);
const char *json_strerr();
size_t json_err_loc();
#endif

4
net.c
View File

@ -13,7 +13,7 @@ Message msg_device_info() {
// Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0
// and writes result to dst
int msg_deserialize(const uint8_t *buf, size_t len, Message * restrict dst) {
int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
// Decrement len so that it becomes the len of the data without the code.
if (len-- < 1)
return -1;
@ -118,7 +118,7 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message * restrict dst) {
}
// Serialize the message msg in buf, buf must be at least 4 aligned. Returns -1 on error (buf not big enough);
int msg_serialize(uint8_t * restrict buf, size_t len, const Message *msg) {
int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) {
// If len is 0 we can't serialize any message
if (len-- == 0)
return -1;

6
net.h
View File

@ -1,7 +1,7 @@
// vi:ft=c
#ifndef NET_H_
#define NET_H_
#include <linux/input.h>
#include <linux/input-event-codes.h>
#include <stdint.h>
#include <stdlib.h>
@ -71,7 +71,7 @@ typedef union {
MessageControllerState controller_state;
} Message;
int msg_deserialize(const uint8_t *buf, size_t len, Message * restrict dst);
int msg_serialize(uint8_t * restrict buf, size_t len, const Message *msg);
int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst);
int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg);
#endif

View File

@ -2,6 +2,7 @@
#include "net.h"
#include "util.h"
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
@ -46,8 +47,8 @@ void *server_handle_conn(void *args_) {
char *closing_message = "";
int len = msg_serialize(buf, 2048, (Message *)&dev.device_info);
if(len > 0) {
if(write(args->socket, buf, len) == -1) {
if (len > 0) {
if (write(args->socket, buf, len) == -1) {
perror("SERVER: Couldn't send device info, ");
closing_message = "Socket error";
goto conn_end;