jsfw/net.h

123 lines
2.8 KiB
C
Raw Normal View History

2022-08-30 17:54:56 -05:00
// vi:ft=c
#ifndef NET_H_
#define NET_H_
2022-10-12 15:28:50 -05:00
#include "util.h"
2022-08-31 11:59:06 -05:00
#include <linux/input-event-codes.h>
2022-08-30 08:37:34 -05:00
#include <stdint.h>
#include <stdlib.h>
2022-08-29 17:27:03 -05:00
2022-10-12 12:33:18 -05:00
#define MAGIC_TYPE uint32_t
#define MAGIC_SIZE sizeof(MAGIC_TYPE)
static const MAGIC_TYPE MAGIC_BEG = 0xDEADCAFE;
static const MAGIC_TYPE MAGIC_END = 0xCAFEDEAD;
2023-03-18 12:41:17 -05:00
// Align n to the next MAGIC boundary
static inline size_t align_m(uintptr_t n) { return (((n - 1) >> 2) + 1) << 2; }
2022-10-12 12:33:18 -05:00
2022-08-29 17:27:03 -05:00
typedef enum {
2022-10-07 18:36:53 -05:00
NoMessage = 0,
2022-08-30 08:37:34 -05:00
DeviceInfo = 1,
DeviceReport = 2,
DeviceDestroy = 3,
2022-08-29 17:27:03 -05:00
ControllerState = 4,
2022-10-07 18:36:53 -05:00
Request = 5,
2022-08-29 17:27:03 -05:00
} MessageCode;
// Alignment 4
2022-08-29 17:27:03 -05:00
typedef struct {
MessageCode code;
// + 1 byte of padding
2022-08-29 17:27:03 -05:00
2023-03-18 12:41:17 -05:00
uint8_t slot;
uint8_t index;
2022-10-07 18:36:53 -05:00
2022-08-30 17:54:56 -05:00
uint16_t abs_count;
uint16_t rel_count;
uint16_t key_count;
2022-08-30 08:37:34 -05:00
2022-08-30 17:56:56 -05:00
uint16_t abs_id[ABS_CNT];
// + 2 bytes of padding per abs
2022-08-30 08:37:34 -05:00
uint32_t abs_min[ABS_CNT];
uint32_t abs_max[ABS_CNT];
uint32_t abs_fuzz[ABS_CNT];
uint32_t abs_flat[ABS_CNT];
uint32_t abs_res[ABS_CNT];
2022-08-30 17:54:56 -05:00
uint16_t rel_id[REL_CNT];
2022-08-30 08:37:34 -05:00
2022-08-30 17:54:56 -05:00
uint16_t key_id[KEY_CNT];
2022-08-29 17:27:03 -05:00
} MessageDeviceInfo;
2022-10-07 18:36:53 -05:00
#define MSS_DEVICE_INFO(abs, rel, key) (10 + abs * 24 + rel * 2 + key * 2 + 1)
2022-08-30 08:37:34 -05:00
// MSS -> Message Serialized Size:
// Size of the data of the message when serialized (no alignment / padding)
2022-08-29 17:27:03 -05:00
// 4 aligned
2022-08-29 17:27:03 -05:00
typedef struct {
MessageCode code;
// + 1 byte of padding
2023-03-18 12:41:17 -05:00
uint8_t slot;
uint8_t index;
2022-08-30 11:28:29 -05:00
2022-08-30 17:54:56 -05:00
uint16_t abs_count;
uint16_t rel_count;
uint16_t key_count;
2022-08-30 11:28:29 -05:00
uint32_t abs[ABS_CNT];
uint32_t rel[REL_CNT];
uint8_t key[KEY_CNT];
2022-08-29 17:27:03 -05:00
} MessageDeviceReport;
2022-10-12 12:33:18 -05:00
#define MSS_DEVICE_REPORT(abs, rel, key) (11 + abs * 4 + rel * 4 + align_4(key))
2022-08-29 17:27:03 -05:00
// 1 aligned
2022-08-30 08:37:34 -05:00
typedef struct {
2022-08-29 17:27:03 -05:00
MessageCode code;
2022-10-07 18:36:53 -05:00
// + 1 byte of padding
2022-08-30 08:37:34 -05:00
2022-10-07 18:36:53 -05:00
uint16_t index;
uint8_t led[3];
uint8_t small_rumble;
uint8_t big_rumble;
uint8_t flash_on;
uint8_t flash_off;
2022-08-29 17:27:03 -05:00
} MessageControllerState;
2022-10-07 18:36:53 -05:00
#define MSS_CONTROLLER_STATE 10
2023-03-18 10:31:33 -05:00
typedef struct {
char **tags;
uint16_t count;
} TagList;
2022-10-07 18:36:53 -05:00
typedef struct {
MessageCode code;
2023-03-18 10:31:33 -05:00
// + 1 byte of padding
2022-10-07 18:36:53 -05:00
2023-03-18 10:31:33 -05:00
TagList *requests;
2022-10-07 18:36:53 -05:00
uint16_t request_count;
} MessageRequest;
2023-03-18 10:31:33 -05:00
#define MSS_REQUEST(count) (2 + 2 * count + 1)
2022-10-07 18:36:53 -05:00
typedef struct {
MessageCode code;
// + 1 byte of padding
uint16_t index;
} MessageDestroy;
#define MSS_DESTROY 3
2022-08-29 17:27:03 -05:00
typedef union {
2022-08-30 08:37:34 -05:00
MessageCode code;
2022-10-07 18:36:53 -05:00
MessageRequest request;
MessageDestroy destroy;
2022-08-30 08:37:34 -05:00
MessageDeviceInfo device_info;
MessageDeviceReport device_report;
2022-08-29 17:27:03 -05:00
MessageControllerState controller_state;
} Message;
2022-10-07 18:36:53 -05:00
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);
void msg_free(Message *msg);
2022-10-12 15:28:50 -05:00
void print_message_buffer(const uint8_t *buf, int len);
2022-08-27 19:29:43 -05:00
#endif