board: Factor out Chunithm slider

This commit is contained in:
Tau
2019-01-17 14:07:59 -05:00
parent 6416ef6e95
commit 8036ff71ec
6 changed files with 10 additions and 9 deletions

View File

@ -21,9 +21,6 @@ shared_library(
'dllmain.c',
'jvs.c',
'jvs.h',
'slider-cmd.h',
'slider-frame.c',
'slider-frame.h',
'slider-hook.c',
'slider-hook.h',
],

View File

@ -1,36 +0,0 @@
#pragma once
#include "chunihook/slider-frame.h"
enum {
SLIDER_CMD_AUTO_SCAN = 0x01,
SLIDER_CMD_SET_LED = 0x02,
SLIDER_CMD_AUTO_SCAN_START = 0x03,
SLIDER_CMD_AUTO_SCAN_STOP = 0x04,
SLIDER_CMD_RESET = 0x10,
SLIDER_CMD_GET_BOARD_INFO = 0xF0,
};
struct slider_req_set_led {
struct slider_hdr hdr;
struct {
uint8_t unk; /* 0x28, decimal 40. meaning unknown. */
uint8_t rgb[32][3];
} payload;
};
union slider_req_any {
struct slider_hdr hdr;
struct slider_req_set_led set_led;
uint8_t bytes[260];
};
struct slider_resp_get_board_info {
struct slider_hdr hdr;
char version[32];
};
struct slider_resp_auto_scan {
struct slider_hdr hdr;
uint8_t pressure[32];
};

View File

@ -1,180 +0,0 @@
#include <windows.h>
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "chunihook/slider-frame.h"
#include "hook/iobuf.h"
static void slider_frame_sync(struct iobuf *src);
static HRESULT slider_frame_accept(const struct iobuf *dest);
static HRESULT slider_frame_encode_byte(struct iobuf *dest, uint8_t byte);
/* Frame structure:
[0] Sync byte (0xFF)
[1] Command
[2] Payload size
... Payload
[n] Checksum: Negate the sum of all prior bytes (including sync byte!)
Byte stuffing:
0xFD is an escape byte. Un-escape the subsequent byte by adding 1. */
static void slider_frame_sync(struct iobuf *src)
{
size_t i;
for (i = 0 ; i < src->pos && src->bytes[i] != 0xFF ; i++);
src->pos -= i;
memmove(&src->bytes[0], &src->bytes[i], i);
}
static HRESULT slider_frame_accept(const struct iobuf *dest)
{
uint8_t checksum;
size_t i;
if (dest->pos < 2 || dest->pos != dest->bytes[2] + 4) {
return S_FALSE;
}
checksum = 0;
for (i = 0 ; i < dest->pos ; i++) {
checksum += dest->bytes[i];
}
if (checksum != 0) {
return HRESULT_FROM_WIN32(ERROR_CRC);
}
return S_OK;
}
HRESULT slider_frame_decode(struct iobuf *dest, struct iobuf *src)
{
uint8_t byte;
bool escape;
size_t i;
HRESULT hr;
assert(dest != NULL);
assert(dest->bytes != NULL || dest->nbytes == 0);
assert(dest->pos <= dest->nbytes);
assert(src != NULL);
assert(src->bytes != NULL || src->nbytes == 0);
assert(src->pos <= src->nbytes);
slider_frame_sync(src);
dest->pos = 0;
escape = false;
for (i = 0, hr = S_FALSE ; i < src->pos && hr == S_FALSE ; i++) {
/* Step the FSM to unstuff another byte */
byte = src->bytes[i];
if (dest->pos >= dest->nbytes) {
hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
} else if (i == 0) {
dest->bytes[dest->pos++] = byte;
} else if (byte == 0xFF) {
hr = E_FAIL;
} else if (byte == 0xFD) {
if (escape) {
hr = E_FAIL;
}
escape = true;
} else if (escape) {
dest->bytes[dest->pos++] = byte + 1;
escape = false;
} else {
dest->bytes[dest->pos++] = byte;
}
/* Try to accept the packet we've built up so far */
if (SUCCEEDED(hr)) {
hr = slider_frame_accept(dest);
}
}
/* Handle FSM terminal state */
if (hr != S_FALSE) {
/* Frame was either accepted or rejected, remove it from src */
memmove(&src->bytes[0], &src->bytes[i], src->pos - i);
src->pos -= i;
}
return hr;
}
HRESULT slider_frame_encode(
struct iobuf *dest,
const void *ptr,
size_t nbytes)
{
const uint8_t *src;
uint8_t checksum;
uint8_t byte;
size_t i;
HRESULT hr;
assert(dest != NULL);
assert(dest->bytes != NULL || dest->nbytes == 0);
assert(dest->pos <= dest->nbytes);
assert(ptr != NULL);
src = ptr;
assert(nbytes >= 2 && src[0] == 0xFF && src[2] + 3 == nbytes);
if (dest->pos >= dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = 0xFF;
checksum = 0xFF;
for (i = 1 ; i < nbytes ; i++) {
byte = src[i];
checksum += byte;
hr = slider_frame_encode_byte(dest, byte);
if (FAILED(hr)) {
return hr;
}
}
return slider_frame_encode_byte(dest, -checksum);
}
static HRESULT slider_frame_encode_byte(struct iobuf *dest, uint8_t byte)
{
if (byte == 0xFF || byte == 0xFD) {
if (dest->pos + 2 > dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = 0xFD;
dest->bytes[dest->pos++] = byte - 1;
} else {
if (dest->pos + 1 > dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = byte;
}
return S_OK;
}

View File

@ -1,25 +0,0 @@
#pragma once
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
#include "hook/iobuf.h"
enum {
SLIDER_FRAME_SYNC = 0xFF,
};
struct slider_hdr {
uint8_t sync;
uint8_t cmd;
uint8_t nbytes;
};
HRESULT slider_frame_decode(struct iobuf *dest, struct iobuf *src);
HRESULT slider_frame_encode(
struct iobuf *dest,
const void *ptr,
size_t nbytes);

View File

@ -6,11 +6,9 @@
#include <stdint.h>
#include <string.h>
#include "util/dprintf.h"
#include "util/dump.h"
#include "board/slider-cmd.h"
#include "board/slider-frame.h"
#include "chunihook/slider-cmd.h"
#include "chunihook/slider-frame.h"
#include "chunihook/slider-hook.h"
#include "hook/iobuf.h"
@ -18,6 +16,9 @@
#include "hooklib/uart.h"
#include "util/dprintf.h"
#include "util/dump.h"
static HRESULT slider_handle_irp(struct irp *irp);
static HRESULT slider_handle_irp_locked(struct irp *irp);