2023-05-09 15:12:46 -05:00
|
|
|
// Generated file, do not edit (its not like it'll explode if you do, but its better not to)
|
2022-08-29 17:27:03 -05:00
|
|
|
#include "net.h"
|
2022-08-30 11:28:29 -05:00
|
|
|
#include <stdio.h>
|
2022-10-12 12:33:18 -05:00
|
|
|
|
2023-05-09 15:12:46 -05:00
|
|
|
int msg_device_serialize(byte *buf, size_t len, DeviceMessage *msg) {
|
|
|
|
const byte *base_buf = buf;
|
|
|
|
if(len < 2 * MSG_MAGIC_SIZE)
|
2022-08-30 08:37:34 -05:00
|
|
|
return -1;
|
2023-05-09 15:12:46 -05:00
|
|
|
*(MsgMagic*)buf = MSG_MAGIC_START;
|
|
|
|
buf += MSG_MAGIC_SIZE;
|
|
|
|
switch(msg->tag) {
|
|
|
|
case DeviceTagNone:
|
|
|
|
break;
|
|
|
|
case DeviceTagInfo: {
|
|
|
|
*(uint16_t *)buf = DeviceTagInfo;
|
|
|
|
*(uint16_t *)&buf[2] = msg->info.key.len;
|
|
|
|
*(uint8_t *)&buf[4] = msg->info.slot;
|
|
|
|
*(uint8_t *)&buf[5] = msg->info.index;
|
|
|
|
*(uint8_t *)&buf[6] = msg->info.abs.len;
|
|
|
|
*(uint8_t *)&buf[7] = msg->info.rel.len;
|
|
|
|
buf += 8;
|
|
|
|
for(size_t i = 0; i < msg->info.abs.len; i++) {
|
|
|
|
typeof(msg->info.abs.data[i]) e0 = msg->info.abs.data[i];
|
|
|
|
*(uint32_t *)&buf[0] = e0.min;
|
|
|
|
*(uint32_t *)&buf[4] = e0.max;
|
|
|
|
*(uint32_t *)&buf[8] = e0.fuzz;
|
|
|
|
*(uint32_t *)&buf[12] = e0.flat;
|
|
|
|
*(uint32_t *)&buf[16] = e0.res;
|
|
|
|
*(uint16_t *)&buf[20] = e0.id;
|
2022-08-31 11:54:14 -05:00
|
|
|
buf += 24;
|
2022-08-30 08:37:34 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->info.rel.len; i++) {
|
|
|
|
typeof(msg->info.rel.data[i]) e0 = msg->info.rel.data[i];
|
|
|
|
*(uint16_t *)&buf[0] = e0.id;
|
2022-08-30 17:54:56 -05:00
|
|
|
buf += 2;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->info.key.len; i++) {
|
|
|
|
typeof(msg->info.key.data[i]) e0 = msg->info.key.data[i];
|
|
|
|
*(uint16_t *)&buf[0] = e0.id;
|
2022-08-30 17:54:56 -05:00
|
|
|
buf += 2;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagReport: {
|
|
|
|
*(uint16_t *)buf = DeviceTagReport;
|
|
|
|
*(uint16_t *)&buf[2] = msg->report.key.len;
|
|
|
|
*(uint8_t *)&buf[4] = msg->report.slot;
|
|
|
|
*(uint8_t *)&buf[5] = msg->report.index;
|
|
|
|
*(uint8_t *)&buf[6] = msg->report.abs.len;
|
|
|
|
*(uint8_t *)&buf[7] = msg->report.rel.len;
|
|
|
|
buf += 8;
|
|
|
|
for(size_t i = 0; i < msg->report.abs.len; i++) {
|
|
|
|
typeof(msg->report.abs.data[i]) e0 = msg->report.abs.data[i];
|
|
|
|
*(uint32_t *)&buf[0] = e0;
|
2022-08-30 11:28:29 -05:00
|
|
|
buf += 4;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->report.rel.len; i++) {
|
|
|
|
typeof(msg->report.rel.data[i]) e0 = msg->report.rel.data[i];
|
|
|
|
*(uint32_t *)&buf[0] = e0;
|
2022-08-30 11:28:29 -05:00
|
|
|
buf += 4;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->report.key.len; i++) {
|
|
|
|
typeof(msg->report.key.data[i]) e0 = msg->report.key.data[i];
|
|
|
|
*(uint8_t *)&buf[0] = e0;
|
|
|
|
buf += 1;
|
|
|
|
}
|
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagControllerState: {
|
|
|
|
*(uint16_t *)buf = DeviceTagControllerState;
|
|
|
|
*(uint16_t *)&buf[2] = msg->controller_state.index;
|
|
|
|
*(uint8_t *)&buf[4] = msg->controller_state.led[0];
|
|
|
|
*(uint8_t *)&buf[5] = msg->controller_state.led[1];
|
|
|
|
*(uint8_t *)&buf[6] = msg->controller_state.led[2];
|
|
|
|
*(uint8_t *)&buf[7] = msg->controller_state.small_rumble;
|
|
|
|
*(uint8_t *)&buf[8] = msg->controller_state.big_rumble;
|
|
|
|
*(uint8_t *)&buf[9] = msg->controller_state.flash_on;
|
|
|
|
*(uint8_t *)&buf[10] = msg->controller_state.flash_off;
|
|
|
|
buf += 16;
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagRequest: {
|
|
|
|
*(uint16_t *)buf = DeviceTagRequest;
|
|
|
|
msg->request._version = 1UL;
|
|
|
|
*(uint64_t *)&buf[8] = msg->request._version;
|
|
|
|
*(uint16_t *)&buf[16] = msg->request.requests.len;
|
|
|
|
buf += 18;
|
|
|
|
for(size_t i = 0; i < msg->request.requests.len; i++) {
|
|
|
|
typeof(msg->request.requests.data[i]) e0 = msg->request.requests.data[i];
|
|
|
|
*(uint16_t *)&buf[0] = e0.tags.len;
|
2022-10-07 18:36:53 -05:00
|
|
|
buf += 2;
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < e0.tags.len; i++) {
|
|
|
|
typeof(e0.tags.data[i]) e1 = e0.tags.data[i];
|
|
|
|
*(uint16_t *)&buf[0] = e1.name.len;
|
2023-03-18 10:31:33 -05:00
|
|
|
buf += 2;
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < e1.name.len; i++) {
|
|
|
|
typeof(e1.name.data[i]) e2 = e1.name.data[i];
|
|
|
|
*(char *)&buf[0] = e2;
|
|
|
|
buf += 1;
|
2023-03-18 10:31:33 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 1) + 1) << 1);
|
2023-03-18 10:31:33 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 1) + 1) << 1);
|
2022-10-07 18:36:53 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2022-10-07 18:36:53 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
case DeviceTagDestroy: {
|
|
|
|
*(uint16_t *)buf = DeviceTagDestroy;
|
|
|
|
*(uint16_t *)&buf[2] = msg->destroy.index;
|
|
|
|
buf += 8;
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2022-08-29 17:27:03 -05:00
|
|
|
}
|
2022-10-12 12:33:18 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
*(MsgMagic*)buf = MSG_MAGIC_END;
|
|
|
|
buf += MSG_MAGIC_SIZE;
|
|
|
|
if(buf > base_buf + len)
|
2022-10-12 12:33:18 -05:00
|
|
|
return -1;
|
2023-05-09 15:12:46 -05:00
|
|
|
return (int)(buf - base_buf);
|
2022-08-29 17:27:03 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:12:46 -05:00
|
|
|
int msg_device_deserialize(const byte *buf, size_t len, DeviceMessage *msg) {
|
|
|
|
const byte *base_buf = buf;
|
|
|
|
if(len < 2 * MSG_MAGIC_SIZE)
|
2022-08-30 08:37:34 -05:00
|
|
|
return -1;
|
2023-05-09 15:12:46 -05:00
|
|
|
if(*(MsgMagic*)buf != MSG_MAGIC_START)
|
|
|
|
return -1;
|
|
|
|
buf += MSG_MAGIC_SIZE;
|
|
|
|
DeviceTag tag = *(uint16_t*)buf;
|
|
|
|
switch(tag) {
|
|
|
|
case DeviceTagNone:
|
|
|
|
break;
|
|
|
|
case DeviceTagInfo: {
|
|
|
|
msg->tag = DeviceTagInfo;
|
|
|
|
msg->info.key.len = *(uint16_t *)&buf[2];
|
|
|
|
msg->info.slot = *(uint8_t *)&buf[4];
|
|
|
|
msg->info.index = *(uint8_t *)&buf[5];
|
|
|
|
msg->info.abs.len = *(uint8_t *)&buf[6];
|
|
|
|
msg->info.rel.len = *(uint8_t *)&buf[7];
|
|
|
|
buf += 8;
|
|
|
|
for(size_t i = 0; i < msg->info.abs.len; i++) {
|
|
|
|
typeof(&msg->info.abs.data[i]) e0 = &msg->info.abs.data[i];
|
|
|
|
e0->min = *(uint32_t *)&buf[0];
|
|
|
|
e0->max = *(uint32_t *)&buf[4];
|
|
|
|
e0->fuzz = *(uint32_t *)&buf[8];
|
|
|
|
e0->flat = *(uint32_t *)&buf[12];
|
|
|
|
e0->res = *(uint32_t *)&buf[16];
|
|
|
|
e0->id = *(uint16_t *)&buf[20];
|
2022-08-31 11:54:14 -05:00
|
|
|
buf += 24;
|
2022-08-30 08:37:34 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->info.rel.len; i++) {
|
|
|
|
typeof(&msg->info.rel.data[i]) e0 = &msg->info.rel.data[i];
|
|
|
|
e0->id = *(uint16_t *)&buf[0];
|
2022-08-30 17:54:56 -05:00
|
|
|
buf += 2;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->info.key.len; i++) {
|
|
|
|
typeof(&msg->info.key.data[i]) e0 = &msg->info.key.data[i];
|
|
|
|
e0->id = *(uint16_t *)&buf[0];
|
2022-08-30 17:54:56 -05:00
|
|
|
buf += 2;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagReport: {
|
|
|
|
msg->tag = DeviceTagReport;
|
|
|
|
msg->report.key.len = *(uint16_t *)&buf[2];
|
|
|
|
msg->report.slot = *(uint8_t *)&buf[4];
|
|
|
|
msg->report.index = *(uint8_t *)&buf[5];
|
|
|
|
msg->report.abs.len = *(uint8_t *)&buf[6];
|
|
|
|
msg->report.rel.len = *(uint8_t *)&buf[7];
|
|
|
|
buf += 8;
|
|
|
|
for(size_t i = 0; i < msg->report.abs.len; i++) {
|
|
|
|
typeof(&msg->report.abs.data[i]) e0 = &msg->report.abs.data[i];
|
|
|
|
*e0 = *(uint32_t *)&buf[0];
|
2022-08-30 11:28:29 -05:00
|
|
|
buf += 4;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->report.rel.len; i++) {
|
|
|
|
typeof(&msg->report.rel.data[i]) e0 = &msg->report.rel.data[i];
|
|
|
|
*e0 = *(uint32_t *)&buf[0];
|
2022-08-30 11:28:29 -05:00
|
|
|
buf += 4;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
for(size_t i = 0; i < msg->report.key.len; i++) {
|
|
|
|
typeof(&msg->report.key.data[i]) e0 = &msg->report.key.data[i];
|
|
|
|
*e0 = *(uint8_t *)&buf[0];
|
|
|
|
buf += 1;
|
|
|
|
}
|
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagControllerState: {
|
|
|
|
msg->tag = DeviceTagControllerState;
|
|
|
|
msg->controller_state.index = *(uint16_t *)&buf[2];
|
|
|
|
msg->controller_state.led[0] = *(uint8_t *)&buf[4];
|
|
|
|
msg->controller_state.led[1] = *(uint8_t *)&buf[5];
|
|
|
|
msg->controller_state.led[2] = *(uint8_t *)&buf[6];
|
|
|
|
msg->controller_state.small_rumble = *(uint8_t *)&buf[7];
|
|
|
|
msg->controller_state.big_rumble = *(uint8_t *)&buf[8];
|
|
|
|
msg->controller_state.flash_on = *(uint8_t *)&buf[9];
|
|
|
|
msg->controller_state.flash_off = *(uint8_t *)&buf[10];
|
|
|
|
buf += 16;
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
case DeviceTagRequest: {
|
|
|
|
msg->tag = DeviceTagRequest;
|
|
|
|
msg->request._version = *(uint64_t *)&buf[8];
|
|
|
|
msg->request.requests.len = *(uint16_t *)&buf[16];
|
|
|
|
buf += 18;
|
|
|
|
msg->request.requests.data = malloc(msg->request.requests.len * sizeof(typeof(*msg->request.requests.data)));
|
|
|
|
for(size_t i = 0; i < msg->request.requests.len; i++) {
|
|
|
|
typeof(&msg->request.requests.data[i]) e0 = &msg->request.requests.data[i];
|
|
|
|
e0->tags.len = *(uint16_t *)&buf[0];
|
2023-03-18 10:31:33 -05:00
|
|
|
buf += 2;
|
2023-05-09 15:12:46 -05:00
|
|
|
e0->tags.data = malloc(e0->tags.len * sizeof(typeof(*e0->tags.data)));
|
|
|
|
for(size_t i = 0; i < e0->tags.len; i++) {
|
|
|
|
typeof(&e0->tags.data[i]) e1 = &e0->tags.data[i];
|
|
|
|
e1->name.len = *(uint16_t *)&buf[0];
|
2023-03-18 10:31:33 -05:00
|
|
|
buf += 2;
|
2023-05-09 15:12:46 -05:00
|
|
|
e1->name.data = malloc(e1->name.len * sizeof(typeof(*e1->name.data)));
|
|
|
|
for(size_t i = 0; i < e1->name.len; i++) {
|
|
|
|
typeof(&e1->name.data[i]) e2 = &e1->name.data[i];
|
|
|
|
*e2 = *(char *)&buf[0];
|
|
|
|
buf += 1;
|
|
|
|
}
|
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 1) + 1) << 1);
|
2023-03-18 10:31:33 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 1) + 1) << 1);
|
|
|
|
}
|
|
|
|
buf = (byte*)(((((uintptr_t)buf - 1) >> 3) + 1) << 3);
|
|
|
|
if(msg->request._version != 1UL) {
|
|
|
|
printf("Mismatched version: peers aren't the same version, expected 1 got %lu.\n", msg->request._version);
|
|
|
|
msg_device_free(msg);
|
|
|
|
return -1;
|
2022-10-07 18:36:53 -05:00
|
|
|
}
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2022-10-07 18:36:53 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
case DeviceTagDestroy: {
|
|
|
|
msg->tag = DeviceTagDestroy;
|
|
|
|
msg->destroy.index = *(uint16_t *)&buf[2];
|
|
|
|
buf += 8;
|
2022-10-12 12:33:18 -05:00
|
|
|
break;
|
2023-05-09 15:12:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(*(MsgMagic*)buf != MSG_MAGIC_END) {
|
|
|
|
msg_device_free(msg);
|
2022-08-30 08:37:34 -05:00
|
|
|
return -1;
|
2022-08-29 17:27:03 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
buf += MSG_MAGIC_SIZE;
|
|
|
|
if(buf > base_buf + len) {
|
|
|
|
msg_device_free(msg);
|
2022-10-12 12:33:18 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
return (int)(buf - base_buf);
|
2022-08-29 17:27:03 -05:00
|
|
|
}
|
2022-10-07 18:36:53 -05:00
|
|
|
|
2023-05-09 15:12:46 -05:00
|
|
|
void msg_device_free(DeviceMessage *msg) {
|
|
|
|
switch(msg->tag) {
|
|
|
|
case DeviceTagNone:
|
|
|
|
break;
|
|
|
|
case DeviceTagInfo: {
|
|
|
|
break;
|
2022-10-07 18:36:53 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
case DeviceTagReport: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceTagControllerState: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceTagRequest: {
|
|
|
|
for(size_t i = 0; i < msg->request.requests.len; i++) {
|
|
|
|
typeof(msg->request.requests.data[i]) e0 = msg->request.requests.data[i];
|
|
|
|
for(size_t i = 0; i < e0.tags.len; i++) {
|
|
|
|
typeof(e0.tags.data[i]) e1 = e0.tags.data[i];
|
|
|
|
free(e1.name.data);
|
2022-10-12 12:33:18 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
free(e0.tags.data);
|
2022-10-12 12:33:18 -05:00
|
|
|
}
|
2023-05-09 15:12:46 -05:00
|
|
|
free(msg->request.requests.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceTagDestroy: {
|
|
|
|
break;
|
|
|
|
}
|
2022-10-12 12:33:18 -05:00
|
|
|
}
|
|
|
|
}
|