mirror of
https://github.com/stupidcomputer/jsfw.git
synced 2024-12-27 05:47:37 -06:00
housekeeping 2
This commit is contained in:
parent
e11c8b5c48
commit
2bd21ee138
2
client.c
2
client.c
@ -6,12 +6,14 @@
|
|||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.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 <math.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
1
hid.c
1
hid.c
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <linux/input-event-codes.h>
|
||||||
#include <linux/input.h>
|
#include <linux/input.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
2
hid.h
2
hid.h
@ -3,7 +3,7 @@
|
|||||||
#define HID_H_
|
#define HID_H_
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
#include <linux/input.h>
|
#include <linux/input-event-codes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef uint64_t uniq_t;
|
typedef uint64_t uniq_t;
|
||||||
|
107
json.c
107
json.c
@ -4,21 +4,16 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static JSONError jerrno = NoError;
|
static JSONError jerrno = NoError;
|
||||||
static size_t jerr_index = 0;
|
static size_t jerr_index = 0;
|
||||||
|
|
||||||
const char * json_strerr() {
|
const char *json_strerr() { return JSONErrorMessage[jerrno]; }
|
||||||
return JSONErrorMessage[jerrno];
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t json_err_loc() {
|
size_t json_err_loc() { return jerr_index; }
|
||||||
return jerr_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shorthand to set jerno and return -1;
|
// Shorthand to set jerno and return -1;
|
||||||
static inline int set_jerrno(JSONError err) {
|
static inline int set_jerrno(JSONError err) {
|
||||||
@ -279,14 +274,16 @@ static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **r
|
|||||||
header->len = 8;
|
header->len = 8;
|
||||||
|
|
||||||
if (**buf == 't') {
|
if (**buf == 't') {
|
||||||
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
|
if (*buf + 4 > buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (strncmp(*buf, "true", 4) != 0) {
|
if (strncmp(*buf, "true", 4) != 0) {
|
||||||
return set_jerrno(BadKeyword);
|
return set_jerrno(BadKeyword);
|
||||||
}
|
}
|
||||||
*buf += 4;
|
*buf += 4;
|
||||||
*value = 1;
|
*value = 1;
|
||||||
} else if (**buf == 'f') {
|
} else if (**buf == 'f') {
|
||||||
if(*buf + 5 > buf_end) return set_jerrno(SrcOverflow);
|
if (*buf + 5 > buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (strncmp(*buf, "false", 5) != 0) {
|
if (strncmp(*buf, "false", 5) != 0) {
|
||||||
return set_jerrno(BadKeyword);
|
return set_jerrno(BadKeyword);
|
||||||
}
|
}
|
||||||
@ -312,7 +309,8 @@ static int json_parse_null(const char **buf, const char *buf_end, uint8_t **rest
|
|||||||
header->type = (uint32_t)Null;
|
header->type = (uint32_t)Null;
|
||||||
header->len = 0;
|
header->len = 0;
|
||||||
|
|
||||||
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
|
if (*buf + 4 > buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (strncmp(*buf, "null", 4) != 0) {
|
if (strncmp(*buf, "null", 4) != 0) {
|
||||||
return set_jerrno(BadKeyword);
|
return set_jerrno(BadKeyword);
|
||||||
}
|
}
|
||||||
@ -336,16 +334,21 @@ static int json_parse_array(const char **buf, const char *buf_end, uint8_t **res
|
|||||||
|
|
||||||
(*buf)++; // Skip [
|
(*buf)++; // Skip [
|
||||||
// skip initial whitespace
|
// skip initial whitespace
|
||||||
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
|
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
|
||||||
if(*buf == buf_end) return set_jerrno(SrcOverflow);
|
;
|
||||||
|
if (*buf == buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (**buf == ']') {
|
if (**buf == ']') {
|
||||||
header->len = 0;
|
header->len = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
if (json_parse_value(buf, buf_end, dst, dst_end) != 0) return -1;
|
if (json_parse_value(buf, buf_end, dst, dst_end) != 0)
|
||||||
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
|
return -1;
|
||||||
if (*buf == buf_end) return set_jerrno(SrcOverflow);
|
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
|
||||||
|
;
|
||||||
|
if (*buf == buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (**buf == ',') {
|
if (**buf == ',') {
|
||||||
(*buf)++;
|
(*buf)++;
|
||||||
} else if (**buf == ']') {
|
} else if (**buf == ']') {
|
||||||
@ -375,8 +378,10 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
|
|||||||
|
|
||||||
(*buf)++; // Skip {
|
(*buf)++; // Skip {
|
||||||
|
|
||||||
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
|
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
|
||||||
if(*buf == buf_end) return set_jerrno(SrcOverflow);
|
;
|
||||||
|
if (*buf == buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (**buf == '}') {
|
if (**buf == '}') {
|
||||||
header->len = 0;
|
header->len = 0;
|
||||||
return 0;
|
return 0;
|
||||||
@ -384,23 +389,31 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// Skip whitespace before key
|
// Skip whitespace before key
|
||||||
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
|
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
|
||||||
|
;
|
||||||
// Parse key
|
// 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
|
// 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
|
// 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
|
// There should be a colon
|
||||||
if(**buf != ':') return set_jerrno(ObjectBadChar);
|
if (**buf != ':')
|
||||||
|
return set_jerrno(ObjectBadChar);
|
||||||
// Skip colon
|
// Skip colon
|
||||||
(*buf)++;
|
(*buf)++;
|
||||||
// Parse value (takes char of whitespace)
|
// 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
|
// 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 ,)
|
// There should be at least one char (} or ,)
|
||||||
if (*buf == buf_end) return set_jerrno(SrcOverflow);
|
if (*buf == buf_end)
|
||||||
|
return set_jerrno(SrcOverflow);
|
||||||
if (**buf == ',') {
|
if (**buf == ',') {
|
||||||
(*buf)++;
|
(*buf)++;
|
||||||
} else if (**buf == '}') {
|
} else if (**buf == '}') {
|
||||||
@ -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,
|
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)++) {
|
||||||
if(is_whitespace(**buf)) continue;
|
if (is_whitespace(**buf))
|
||||||
|
continue;
|
||||||
|
|
||||||
switch (**buf) {
|
switch (**buf) {
|
||||||
case '"':
|
case '"':
|
||||||
@ -475,8 +489,7 @@ void json_print_value(uint8_t ** buf) {
|
|||||||
printf("%lf", *(double *)*buf);
|
printf("%lf", *(double *)*buf);
|
||||||
*buf += sizeof(double);
|
*buf += sizeof(double);
|
||||||
break;
|
break;
|
||||||
case Boolean:
|
case Boolean: {
|
||||||
{
|
|
||||||
uint64_t value = *(uint64_t *)*buf;
|
uint64_t value = *(uint64_t *)*buf;
|
||||||
if (value == 1) {
|
if (value == 1) {
|
||||||
printf("true");
|
printf("true");
|
||||||
@ -486,13 +499,11 @@ void json_print_value(uint8_t ** buf) {
|
|||||||
printf("(boolean) garbage");
|
printf("(boolean) garbage");
|
||||||
}
|
}
|
||||||
*buf += 8;
|
*buf += 8;
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
case Null:
|
case Null:
|
||||||
printf("null");
|
printf("null");
|
||||||
break;
|
break;
|
||||||
case Array:
|
case Array: {
|
||||||
{
|
|
||||||
uint8_t *end = *buf + header->len;
|
uint8_t *end = *buf + header->len;
|
||||||
printf("[");
|
printf("[");
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -504,10 +515,8 @@ void json_print_value(uint8_t ** buf) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
break;
|
case Object: {
|
||||||
case Object:
|
|
||||||
{
|
|
||||||
uint8_t *end = *buf + header->len;
|
uint8_t *end = *buf + header->len;
|
||||||
printf("{");
|
printf("{");
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -521,8 +530,7 @@ void json_print_value(uint8_t ** buf) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -542,14 +550,12 @@ static void json_adapt_set(uint8_t * buf, JSONAdapter * adapters, size_t adapter
|
|||||||
if (strcmp(path, adapters[i].path) == 0 && header->type == adapters[i].type) {
|
if (strcmp(path, adapters[i].path) == 0 && header->type == adapters[i].type) {
|
||||||
void *p = ptr + adapters[i].offset;
|
void *p = ptr + adapters[i].offset;
|
||||||
switch (header->type) {
|
switch (header->type) {
|
||||||
case String:
|
case String: {
|
||||||
{
|
|
||||||
char *v = malloc(header->len + 1);
|
char *v = malloc(header->len + 1);
|
||||||
strncpy(v, (char *)(buf + sizeof(JSONHeader)), header->len);
|
strncpy(v, (char *)(buf + sizeof(JSONHeader)), header->len);
|
||||||
v[header->len] = '\0';
|
v[header->len] = '\0';
|
||||||
*(char **)p = v;
|
*(char **)p = v;
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
case Number:
|
case Number:
|
||||||
*(double *)p = *(double *)(buf + sizeof(JSONHeader));
|
*(double *)p = *(double *)(buf + sizeof(JSONHeader));
|
||||||
break;
|
break;
|
||||||
@ -561,7 +567,8 @@ static void json_adapt_set(uint8_t * buf, JSONAdapter * adapters, size_t adapter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapter_count, void * ptr, char * full_path, char * path) {
|
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;
|
JSONHeader *header = (JSONHeader *)*buf;
|
||||||
|
|
||||||
switch (header->type) {
|
switch (header->type) {
|
||||||
@ -580,18 +587,15 @@ static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapt
|
|||||||
case Null:
|
case Null:
|
||||||
*buf += sizeof(JSONHeader);
|
*buf += sizeof(JSONHeader);
|
||||||
break;
|
break;
|
||||||
case Array:
|
case Array: {
|
||||||
{
|
|
||||||
*buf += sizeof(JSONHeader);
|
*buf += sizeof(JSONHeader);
|
||||||
uint8_t *end = *buf + header->len;
|
uint8_t *end = *buf + header->len;
|
||||||
for (size_t index = 0; *buf < end; index++) {
|
for (size_t index = 0; *buf < end; index++) {
|
||||||
int len = sprintf(path, ".%lu", index);
|
int len = sprintf(path, ".%lu", index);
|
||||||
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
|
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
break;
|
case Object: {
|
||||||
case Object:
|
|
||||||
{
|
|
||||||
*buf += sizeof(JSONHeader);
|
*buf += sizeof(JSONHeader);
|
||||||
uint8_t *end = *buf + header->len;
|
uint8_t *end = *buf + header->len;
|
||||||
while (*buf < end) {
|
while (*buf < end) {
|
||||||
@ -603,8 +607,7 @@ 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);
|
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
json.h
5
json.h
@ -1,10 +1,9 @@
|
|||||||
// vi:ft=c
|
// vi:ft=c
|
||||||
#ifndef JSON_H_
|
#ifndef JSON_H_
|
||||||
#define JSON_H_
|
#define JSON_H_
|
||||||
#include <stdint.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct __attribute__((packed, aligned(8))) {
|
typedef struct __attribute__((packed, aligned(8))) {
|
||||||
uint32_t type;
|
uint32_t type;
|
||||||
@ -50,7 +49,7 @@ static const char * JSONErrorMessage[JERRORNO_MAX + 1] = {
|
|||||||
"Illegal escape in string",
|
"Illegal escape in string",
|
||||||
"Unexpected character in number",
|
"Unexpected character in number",
|
||||||
"Unexpected character in object",
|
"Unexpected character in object",
|
||||||
"?"
|
"?",
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
2
net.h
2
net.h
@ -1,7 +1,7 @@
|
|||||||
// vi:ft=c
|
// vi:ft=c
|
||||||
#ifndef NET_H_
|
#ifndef NET_H_
|
||||||
#define NET_H_
|
#define NET_H_
|
||||||
#include <linux/input.h>
|
#include <linux/input-event-codes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user