housekeeping

This commit is contained in:
viandoxdev 2022-10-12 22:28:50 +02:00
parent cf4b5d6eb0
commit c6bdbf7fd8
No known key found for this signature in database
GPG Key ID: AF1410C5BC10AA25
15 changed files with 76 additions and 89 deletions

View File

@ -1,9 +1,9 @@
Q=@ Q=@
CC=clang CC=gcc
GCCCFLAGS=-Wno-format-truncation GCCCFLAGS=-Wno-format-truncation
CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE -fsanitize=undefined CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE
LDFLAGS=-lm -fsanitize=undefined LDFLAGS=-lm
BUILD_DIR=./objects BUILD_DIR=./objects
BIN=jsfw BIN=jsfw

View File

@ -1,4 +1,5 @@
#include "client.h" #include "client.h"
#include "const.h" #include "const.h"
#include "json.h" #include "json.h"
#include "net.h" #include "net.h"
@ -10,7 +11,6 @@
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/uinput.h> #include <linux/uinput.h>
#include <math.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h> #include <poll.h>
#include <stdbool.h> #include <stdbool.h>
@ -189,7 +189,8 @@ void device_init(MessageDeviceInfo *dev) {
MessageDeviceInfo *dst = vec_get(&devices_info, dev->index); MessageDeviceInfo *dst = vec_get(&devices_info, dev->index);
memcpy(dst, dev, sizeof(MessageDeviceInfo)); 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); 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);
} }
// Send an event to uinput, device must exist // Send an event to uinput, device must exist
@ -217,8 +218,7 @@ void device_handle_report(MessageDeviceReport *report) {
MessageDeviceInfo *info = vec_get(&devices_info, report->index); MessageDeviceInfo *info = vec_get(&devices_info, report->index);
if (report->abs_count != info->abs_count || report->rel_count != info->rel_count || if (report->abs_count != info->abs_count || report->rel_count != info->rel_count || report->key_count != info->key_count) {
report->key_count != info->key_count) {
printf("CLIENT: Report doesn't match with device info\n"); printf("CLIENT: Report doesn't match with device info\n");
return; return;
} }

1
hid.h
View File

@ -5,6 +5,7 @@
#include "server.h" #include "server.h"
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
// Unique identifier for devices (provided by linux), May be the mac address // Unique identifier for devices (provided by linux), May be the mac address

31
json.c
View File

@ -55,8 +55,7 @@ static int json_parse_value(const char **buf, const char *buf_end, uint8_t **res
const uint8_t *dst_end); // Declaration for recursion const uint8_t *dst_end); // Declaration for recursion
// *dst must be 8 aligned // *dst must be 8 aligned
static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for the header // Ensure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -208,8 +207,7 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
(*buf)++; (*buf)++;
return 0; return 0;
} else if ((c < ' ' && c != '\t') || } else if ((c < ' ' && c != '\t') || c == 0x7f) { // Illegal characters, technically tab isn't allowed either
c == 0x7f) { // Illegal characters, technically tab isn't allowed either
// but it felt weird so I added it // but it felt weird so I added it
jerrno = StringBadChar; jerrno = StringBadChar;
return -1; return -1;
@ -229,8 +227,7 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for header and value // Ensure enough space for header and value
if (*dst + sizeof(JSONHeader) + sizeof(double) >= dst_end) { if (*dst + sizeof(JSONHeader) + sizeof(double) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -349,8 +346,7 @@ static int json_parse_number(const char **buf, const char *buf_end, uint8_t **re
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for header and value // Ensure enough space for header and value
if (*dst + sizeof(JSONHeader) + 8 >= dst_end) { // 8: sizeof(uint64_t) if (*dst + sizeof(JSONHeader) + 8 >= dst_end) { // 8: sizeof(uint64_t)
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -389,8 +385,7 @@ static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **r
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough size for the header (no value) // Ensure enough size for the header (no value)
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -415,8 +410,7 @@ static int json_parse_null(const char **buf, const char *buf_end, uint8_t **rest
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for the header // Ensure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -477,8 +471,7 @@ static int json_parse_array(const char **buf, const char *buf_end, uint8_t **res
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Esnure enough space for the header // Esnure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -557,8 +550,7 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
for (; *buf < buf_end; (*buf)++) { for (; *buf < buf_end; (*buf)++) {
// Ignore initial whitespaces // Ignore initial whitespaces
if (is_whitespace(**buf)) if (is_whitespace(**buf))
@ -736,8 +728,8 @@ static void json_adapt_set_defaults(const JSONAdapter *adapter, void *ptr) {
// path_buffer: points to the begining of the path buffer // path_buffer: points to the begining of the path buffer
// full_path: points to the "current" path // full_path: points to the "current" path
// path: points to the end of the current path (most of the times) // path: points to the end of the current path (most of the times)
static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr, char *path_buffer, static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr, char *path_buffer, char *full_path,
char *full_path, char *path) { char *path) {
JSONHeader *header = (JSONHeader *)*buf; JSONHeader *header = (JSONHeader *)*buf;
if (is_primitive(adapter)) { if (is_primitive(adapter)) {
@ -807,8 +799,7 @@ static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr
for (size_t index = 0; index < len; index++) { for (size_t index = 0; index < len; index++) {
path[0] = '.'; path[0] = '.';
path[1] = '\0'; path[1] = '\0';
json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, path, path);
path, path);
path[0] = '\0'; path[0] = '\0';
} }

12
json.h
View File

@ -2,9 +2,9 @@
#ifndef JSON_H_ #ifndef JSON_H_
#define JSON_H_ #define JSON_H_
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
typedef struct __attribute__((packed, aligned(8))) { typedef struct __attribute__((packed, aligned(8))) {
uint32_t type; uint32_t type;
@ -38,8 +38,6 @@ typedef enum {
JERRORNO_MAX = 10 JERRORNO_MAX = 10
} JSONError; } JSONError;
struct JSONAdapter;
typedef struct { typedef struct {
char *path; char *path;
const struct JSONAdapter *type; const struct JSONAdapter *type;
@ -84,13 +82,7 @@ static const char *JSONErrorMessage[JERRORNO_MAX + 1] = {
}; };
static const char *JSONTypeName[7] = { static const char *JSONTypeName[7] = {
"[Unknown]", "[Unknown]", "String", "Number", "Object", "Array", "Boolean", "Null",
"String",
"Number",
"Object",
"Array",
"Boolean",
"Null",
}; };
const JSONAdapter NumberAdapter = { const JSONAdapter NumberAdapter = {

1
main.c
View File

@ -1,5 +1,4 @@
#include "client.h" #include "client.h"
#include "hid.h"
#include "server.h" #include "server.h"
#include "util.h" #include "util.h"

2
net.c
View File

@ -2,10 +2,10 @@
#include "util.h" #include "util.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0 // Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0
// and writes result to dst // 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) {

2
net.h
View File

@ -1,6 +1,8 @@
// vi:ft=c // vi:ft=c
#ifndef NET_H_ #ifndef NET_H_
#define NET_H_ #define NET_H_
#include "util.h"
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -9,12 +9,12 @@
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <linux/input.h> #include <linux/input.h>
#include <math.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <poll.h> #include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

1
util.c
View File

@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#ifndef __has_builtin #ifndef __has_builtin
#define __has_builtin(_) 0 #define __has_builtin(_) 0

3
vec.c
View File

@ -1,5 +1,6 @@
#include "vec.h" #include "vec.h"
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -40,7 +41,7 @@ static inline void vec_grow(Vec *v, size_t cap) {
void vec_push(Vec *v, void *data) { void vec_push(Vec *v, void *data) {
vec_grow(v, v->len + 1); vec_grow(v, v->len + 1);
memcpy((u_int8_t *)v->data + v->stride * v->len++, data, v->stride); memcpy((uint8_t *)v->data + v->stride * v->len++, data, v->stride);
} }
void vec_pop(Vec *v, void *data) { void vec_pop(Vec *v, void *data) {

2
vec.h
View File

@ -1,8 +1,8 @@
// vi:ft=c // vi:ft=c
#ifndef VEC_H_ #ifndef VEC_H_
#define VEC_H_ #define VEC_H_
#include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h>
#define vec_of(type) vec_new(sizeof(type)) #define vec_of(type) vec_new(sizeof(type))