chusan: added chusanhook, led board fix, config added

credits go to @domeori https://dev.s-ul.net/domeori/segatools/-/tree/mr-imports
This commit is contained in:
2023-07-14 00:58:02 +02:00
parent 90a6f1be7c
commit 2a6a8bf8b2
32 changed files with 2253 additions and 17 deletions

118
chusanhook/chuni-dll.c Normal file
View File

@ -0,0 +1,118 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "chusanhook/chuni-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym chuni_dll_syms[] = {
{
.sym = "chuni_io_jvs_init",
.off = offsetof(struct chuni_dll, jvs_init),
}, {
.sym = "chuni_io_jvs_poll",
.off = offsetof(struct chuni_dll, jvs_poll),
}, {
.sym = "chuni_io_jvs_read_coin_counter",
.off = offsetof(struct chuni_dll, jvs_read_coin_counter),
}, {
.sym = "chuni_io_slider_init",
.off = offsetof(struct chuni_dll, slider_init),
}, {
.sym = "chuni_io_slider_start",
.off = offsetof(struct chuni_dll, slider_start),
}, {
.sym = "chuni_io_slider_stop",
.off = offsetof(struct chuni_dll, slider_stop),
}, {
.sym = "chuni_io_slider_set_leds",
.off = offsetof(struct chuni_dll, slider_set_leds),
}
};
struct chuni_dll chuni_dll;
// Copypasta DLL binding and diagnostic message boilerplate.
// Not much of this lends itself to being easily factored out. Also there
// will be a lot of API-specific branching code here eventually as new API
// versions get defined, so even though these functions all look the same
// now this won't remain the case forever.
HRESULT chuni_dll_init(const struct chuni_dll_config *cfg, HINSTANCE self)
{
uint16_t (*get_api_version)(void);
const struct dll_bind_sym *sym;
HINSTANCE owned;
HINSTANCE src;
HRESULT hr;
assert(cfg != NULL);
assert(self != NULL);
if (cfg->path[0] != L'\0') {
owned = LoadLibraryW(cfg->path);
if (owned == NULL) {
hr = HRESULT_FROM_WIN32(GetLastError());
dprintf("Chunithm IO: Failed to load IO DLL: %lx: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("Chunithm IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "chuni_io_get_api_version");
if (get_api_version != NULL) {
chuni_dll.api_version = get_api_version();
} else {
chuni_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose chuni_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (chuni_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Chunithm IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
chuni_dll.api_version);
goto end;
}
sym = chuni_dll_syms;
hr = dll_bind(&chuni_dll, src, &sym, _countof(chuni_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("Chunithm IO: Custom IO DLL does not provide function "
"\"%s\". Please contact your IO DLL's developer for "
"further assistance.\n",
sym->sym);
goto end;
} else {
dprintf("Internal error: could not reflect \"%s\"\n", sym->sym);
}
}
owned = NULL;
end:
if (owned != NULL) {
FreeLibrary(owned);
}
return hr;
}

24
chusanhook/chuni-dll.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <windows.h>
#include "chuniio/chuniio.h"
struct chuni_dll {
uint16_t api_version;
HRESULT (*jvs_init)(void);
void (*jvs_poll)(uint8_t *opbtn, uint8_t *beams);
void (*jvs_read_coin_counter)(uint16_t *total);
HRESULT (*slider_init)(void);
void (*slider_start)(chuni_io_slider_callback_t callback);
void (*slider_stop)(void);
void (*slider_set_leds)(const uint8_t *rgb);
};
struct chuni_dll_config {
wchar_t path[MAX_PATH];
};
extern struct chuni_dll chuni_dll;
HRESULT chuni_dll_init(const struct chuni_dll_config *cfg, HINSTANCE self);

22
chusanhook/chusanhook.def Normal file
View File

@ -0,0 +1,22 @@
LIBRARY chusanhook
EXPORTS
Direct3DCreate9
aime_io_get_api_version
aime_io_init
aime_io_led_set_color
aime_io_nfc_get_aime_id
aime_io_nfc_get_felica_id
aime_io_nfc_poll
amDllVideoClose @2
amDllVideoGetVBiosVersion @4
amDllVideoOpen @1
amDllVideoSetResolution @3
chuni_io_get_api_version
chuni_io_jvs_init
chuni_io_jvs_poll
chuni_io_jvs_read_coin_counter
chuni_io_slider_init
chuni_io_slider_set_leds
chuni_io_slider_start
chuni_io_slider_stop

122
chusanhook/config.c Normal file
View File

@ -0,0 +1,122 @@
#include <assert.h>
#include <stddef.h>
#include "board/config.h"
#include "hooklib/config.h"
#include "hooklib/dvd.h"
#include "gfxhook/config.h"
#include "platform/config.h"
#include "chusanhook/config.h"
// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
void chuni_dll_config_load(
struct chuni_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
// Workaround for x64/x86 external IO dlls
// path32 for 32bit, path64 for 64bit
// for else.. is that possible? idk
#if defined(ENV32BIT)
GetPrivateProfileStringW(
L"chuniio",
L"path32",
L"",
cfg->path,
_countof(cfg->path),
filename);
#elif defined(ENV64BIT)
GetPrivateProfileStringW(
L"chuniio",
L"path64",
L"",
cfg->path,
_countof(cfg->path),
filename);
#else
#error "Unknown environment"
#endif
}
void slider_config_load(struct slider_config *cfg, const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->enable = GetPrivateProfileIntW(L"slider", L"enable", 1, filename);
}
void led1509306_config_load(struct led1509306_config *cfg, const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
wchar_t tmpstr[16];
memset(cfg->board_number, ' ', sizeof(cfg->board_number));
memset(cfg->chip_number, ' ', sizeof(cfg->chip_number));
cfg->enable = GetPrivateProfileIntW(L"ledstrip", L"enable", 1, filename);
cfg->cvt_port = GetPrivateProfileIntW(L"ledstrip", L"cvt_port", 0, filename);
cfg->fw_ver = GetPrivateProfileIntW(L"ledstrip", L"fw_ver", 0x90, filename);
cfg->fw_sum = GetPrivateProfileIntW(L"ledstrip", L"fw_sum", 0xadf7, filename);
GetPrivateProfileStringW(L"ledstrip", L"board_number", L"15093-06", tmpstr, _countof(tmpstr), filename);
size_t n = wcstombs(cfg->board_number, tmpstr, sizeof(cfg->board_number));
for (int i = n; i < sizeof(cfg->board_number); i++)
{
cfg->board_number[i] = ' ';
}
GetPrivateProfileStringW(L"ledstrip", L"chip_number", L"6710 ", tmpstr, _countof(tmpstr), filename);
n = wcstombs(cfg->chip_number, tmpstr, sizeof(cfg->chip_number));
for (int i = n; i < sizeof(cfg->chip_number); i++)
{
cfg->chip_number[i] = ' ';
}
}
void chusan_hook_config_load(
struct chusan_hook_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
memset(cfg, 0, sizeof(*cfg));
platform_config_load(&cfg->platform, filename);
aime_config_load(&cfg->aime, filename);
dvd_config_load(&cfg->dvd, filename);
io4_config_load(&cfg->io4, filename);
gfx_config_load(&cfg->gfx, filename);
chuni_dll_config_load(&cfg->dll, filename);
slider_config_load(&cfg->slider, filename);
led1509306_config_load(&cfg->led1509306, filename);
}

34
chusanhook/config.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <stddef.h>
#include "board/config.h"
#include "hooklib/dvd.h"
#include "gfxhook/config.h"
#include "platform/config.h"
#include "chusanhook/chuni-dll.h"
#include "chusanhook/slider.h"
#include "chunihook/led1509306.h"
struct chusan_hook_config {
struct platform_config platform;
struct aime_config aime;
struct dvd_config dvd;
struct io4_config io4;
struct gfx_config gfx;
struct chuni_dll_config dll;
struct slider_config slider;
struct led1509306_config led1509306;
};
void chuni_dll_config_load(
struct chuni_dll_config *cfg,
const wchar_t *filename);
void slider_config_load(struct slider_config *cfg, const wchar_t *filename);
void chusan_hook_config_load(
struct chusan_hook_config *cfg,
const wchar_t *filename);

147
chusanhook/dllmain.c Normal file
View File

@ -0,0 +1,147 @@
#include <windows.h>
#include <stddef.h>
#include <stdlib.h>
#include "amex/amex.h"
#include "board/sg-reader.h"
#include "board/vfd.h"
#include "chusanhook/config.h"
#include "chusanhook/io4.h"
#include "chusanhook/slider.h"
#include "chunihook/led1509306.h"
#include "chuniio/chuniio.h"
#include "hook/process.h"
#include "gfxhook/d3d9.h"
#include "gfxhook/gfx.h"
#include "hooklib/serial.h"
#include "hooklib/spike.h"
#include "platform/platform.h"
#include "util/dprintf.h"
static HMODULE chusan_hook_mod;
static process_entry_t chusan_startup;
static struct chusan_hook_config chusan_hook_cfg;
static DWORD CALLBACK chusan_pre_startup(void)
{
HMODULE d3dc;
HMODULE dbghelp;
HRESULT hr;
dprintf("--- Begin chusan_pre_startup ---\n");
/* Pin the D3D shader compiler. This makes startup much faster. */
d3dc = LoadLibraryW(L"D3DCompiler_43.dll");
if (d3dc != NULL) {
dprintf("Pinned shader compiler, hMod=%p\n", d3dc);
} else {
dprintf("Failed to load shader compiler!\n");
}
/* Pin dbghelp so the path hooks apply to it. */
dbghelp = LoadLibraryW(L"dbghelp.dll");
if (dbghelp != NULL) {
dprintf("Pinned debug helper library, hMod=%p\n", dbghelp);
} else {
dprintf("Failed to load debug helper library!\n");
}
/* Config load */
chusan_hook_config_load(&chusan_hook_cfg, L".\\segatools.ini");
/* Hook Win32 APIs */
dvd_hook_init(&chusan_hook_cfg.dvd, chusan_hook_mod);
gfx_hook_init(&chusan_hook_cfg.gfx);
gfx_d3d9_hook_init(&chusan_hook_cfg.gfx, chusan_hook_mod);
serial_hook_init();
/* Initialize emulation hooks */
hr = platform_hook_init(
&chusan_hook_cfg.platform,
"SDHD",
"ACA2",
chusan_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = chuni_dll_init(&chusan_hook_cfg.dll, chusan_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = chusan_io4_hook_init(&chusan_hook_cfg.io4);
if (FAILED(hr)) {
goto fail;
}
hr = slider_hook_init(&chusan_hook_cfg.slider);
if (FAILED(hr)) {
goto fail;
}
hr = led1509306_hook_init(&chusan_hook_cfg.led1509306);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&chusan_hook_cfg.aime, 4, chusan_hook_mod);
if (FAILED(hr)) {
goto fail;
}
/* Initialize debug helpers */
spike_hook_init(L".\\segatools.ini");
dprintf("--- End chusan_pre_startup ---\n");
/* Jump to EXE start address */
return chusan_startup();
fail:
ExitProcess(EXIT_FAILURE);
}
BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)
{
HRESULT hr;
if (cause != DLL_PROCESS_ATTACH) {
return TRUE;
}
chusan_hook_mod = mod;
hr = process_hijack_startup(chusan_pre_startup, &chusan_startup);
if (!SUCCEEDED(hr)) {
dprintf("Failed to hijack process startup: %x\n", (int) hr);
}
return SUCCEEDED(hr);
}

107
chusanhook/io4.c Normal file
View File

@ -0,0 +1,107 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "board/io4.h"
#include "chusanhook/chuni-dll.h"
#include "util/dprintf.h"
struct chunithm_jvs_ir_mask {
uint16_t p1;
uint16_t p2;
};
// Incorrect IR beam mappings retained for backward compatibility
static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks_v1[] = {
{ 0, 1 << 13 },
{ 1 << 13, 0 },
{ 0, 1 << 12 },
{ 1 << 12, 0 },
{ 0, 1 << 11 },
{ 1 << 11, 0 },
};
static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks[] = {
{ 1 << 13, 0 },
{ 0, 1 << 13 },
{ 1 << 12, 0 },
{ 0, 1 << 12 },
{ 1 << 11, 0 },
{ 0, 1 << 11 },
};
static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state);
static uint16_t coins;
static const struct io4_ops chusan_io4_ops = {
.poll = chusan_io4_poll,
};
HRESULT chusan_io4_hook_init(const struct io4_config* cfg)
{
HRESULT hr;
assert(chuni_dll.jvs_init != NULL);
dprintf("USB I/O: Starting IO backend\n");
hr = chuni_dll.jvs_init();
if (FAILED(hr)) {
dprintf("USB I/O: Backend error, I/O disconnected: %x\n", (int)hr);
return hr;
}
io4_hook_init(cfg, &chusan_io4_ops, NULL);
return S_OK;
}
static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state)
{
const struct chunithm_jvs_ir_mask *masks;
uint8_t opbtn;
uint8_t beams;
size_t i;
memset(state, 0, sizeof(*state));
opbtn = 0;
beams = 0;
chuni_dll.jvs_poll(&opbtn, &beams);
if (chuni_dll.api_version >= 0x0101) {
// Use correct mapping
masks = chunithm_jvs_ir_masks;
} else {
// Use backwards-compatible incorrect mapping
masks = chunithm_jvs_ir_masks_v1;
}
if (opbtn & CHUNI_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & CHUNI_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & CHUNI_IO_OPBTN_COIN) {
coins++;
}
state->chutes[0] = coins << 8;
for (i = 0; i < 6; i++) {
/* Beam "press" is active-low hence the ~ */
if (~beams & (1 << i)) {
state->buttons[0] |= masks[i].p1;
state->buttons[1] |= masks[i].p2;
}
}
return S_OK;
}

5
chusanhook/io4.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <windows.h>
HRESULT chusan_io4_hook_init(const struct io4_config *cfg);

389
chusanhook/led1509306.c Normal file
View File

@ -0,0 +1,389 @@
#include <windows.h>
#include <assert.h>
#include <process.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "board/led1509306-cmd.h"
#include "board/led1509306-frame.h"
#include "chunihook/led1509306.h"
#include "hook/iobuf.h"
#include "hook/iohook.h"
#include "hooklib/uart.h"
#include "util/dprintf.h"
#include "util/dump.h"
static HRESULT led1509306_handle_irp(struct irp *irp);
static HRESULT led1509306_handle_irp_locked(int board, struct irp *irp);
static HRESULT led1509306_req_dispatch(int board, const struct led1509306_req_any *req);
static HRESULT led1509306_req_reset(int board, const struct led1509306_req_any *req);
static HRESULT led1509306_req_get_board_info(int board);
static HRESULT led1509306_req_get_fw_sum(int board);
static HRESULT led1509306_req_get_protocol_ver(int board);
static HRESULT led1509306_req_get_board_status(int board);
static HRESULT led1509306_req_set_led(int board, const struct led1509306_req_any *req);
static HRESULT led1509306_req_set_disable_response(int board, const struct led1509306_req_any *req);
static HRESULT led1509306_req_set_timeout(int board, const struct led1509306_req_any *req);
static char led1509306_board_num[8];
static char led1509306_chip_num[5];
static uint8_t led1509306_fw_ver;
static uint16_t led1509306_fw_sum;
static uint8_t led1509306_board_adr = 2;
static uint8_t led1509306_host_adr = 1;
#define led1509306_nboards 2
typedef struct {
CRITICAL_SECTION lock;
struct uart boarduart;
uint8_t written_bytes[520];
uint8_t readable_bytes[520];
bool enable_response;
} _led1509306_per_board_vars;
_led1509306_per_board_vars led1509306_per_board_vars[led1509306_nboards];
HRESULT led1509306_hook_init(const struct led1509306_config *cfg)
{
assert(cfg != NULL);
if (!cfg->enable) {
return S_FALSE;
}
memcpy(led1509306_board_num, cfg->board_number, sizeof(led1509306_board_num));
memcpy(led1509306_chip_num, cfg->chip_number, sizeof(led1509306_chip_num));
led1509306_fw_ver = cfg->fw_ver;
led1509306_fw_sum = cfg->fw_sum;
int com_ports[2];
if (!cfg->cvt_port) {
// SP mode: COM20, COM21
com_ports[0] = 20;
com_ports[1] = 21;
} else {
// CVT mode: COM3, COM23
com_ports[0] = 2;
com_ports[1] = 3;
}
for (int i = 0; i < led1509306_nboards; i++)
{
_led1509306_per_board_vars *v = &led1509306_per_board_vars[i];
InitializeCriticalSection(&v->lock);
uart_init(&v->boarduart, com_ports[i]);
v->boarduart.written.bytes = v->written_bytes;
v->boarduart.written.nbytes = sizeof(v->written_bytes);
v->boarduart.readable.bytes = v->readable_bytes;
v->boarduart.readable.nbytes = sizeof(v->readable_bytes);
v->enable_response = true;
}
return iohook_push_handler(led1509306_handle_irp);
}
static HRESULT led1509306_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
for (int i = 0; i < led1509306_nboards; i++)
{
_led1509306_per_board_vars *v = &led1509306_per_board_vars[i];
struct uart *boarduart = &v->boarduart;
if (uart_match_irp(boarduart, irp))
{
CRITICAL_SECTION lock = v->lock;
EnterCriticalSection(&lock);
hr = led1509306_handle_irp_locked(i, irp);
LeaveCriticalSection(&lock);
return hr;
}
}
return iohook_invoke_next(irp);
}
static HRESULT led1509306_handle_irp_locked(int board, struct irp *irp)
{
struct led1509306_req_any req;
struct iobuf req_iobuf;
HRESULT hr;
struct uart *boarduart = &led1509306_per_board_vars[board].boarduart;
hr = uart_handle_irp(boarduart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX Buffer:\n");
dump_iobuf(&boarduart->written);
#endif
req_iobuf.bytes = (byte*)&req;
req_iobuf.nbytes = sizeof(req.hdr) + sizeof(req.cmd) + sizeof(req.payload);
req_iobuf.pos = 0;
hr = led1509306_frame_decode(&req_iobuf, &boarduart->written);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Chunithm LED Strip: Deframe error: %x\n", (int) hr);
}
return hr;
}
#if 0
dprintf("Deframe Buffer:\n");
dump_iobuf(&req_iobuf);
#endif
hr = led1509306_req_dispatch(board, &req);
if (FAILED(hr)) {
dprintf("Chunithm LED Strip: Processing error: %x\n", (int) hr);
}
}
}
static HRESULT led1509306_req_dispatch(int board, const struct led1509306_req_any *req)
{
switch (req->cmd) {
case LED_15093_06_CMD_RESET:
return led1509306_req_reset(board, req);
case LED_15093_06_CMD_BOARD_INFO:
return led1509306_req_get_board_info(board);
case LED_15093_06_CMD_FW_SUM:
return led1509306_req_get_fw_sum(board);
case LED_15093_06_CMD_PROTOCOL_VER:
return led1509306_req_get_protocol_ver(board);
case LED_15093_06_CMD_BOARD_STATUS:
return led1509306_req_get_board_status(board);
case LED_15093_06_CMD_SET_LED:
return led1509306_req_set_led(board, req);
case LED_15093_06_CMD_SET_DISABLE_RESPONSE:
return led1509306_req_set_disable_response(board, req);
case LED_15093_06_CMD_SET_TIMEOUT:
return led1509306_req_set_timeout(board, req);
default:
dprintf("Chunithm LED Strip: Unhandled command %02x\n", req->cmd);
return S_OK;
}
}
static HRESULT led1509306_req_reset(int board, const struct led1509306_req_any *req)
{
dprintf("Chunithm LED Strip: Reset (board %u, type %02x)\n", board, req->payload[0]);
if (req->payload[0] != 0xd9)
dprintf("Chunithm LED Strip: Warning -- Unknown reset type %02x\n", req->payload[0]);
led1509306_per_board_vars[board].enable_response = true;
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_RESET;
resp.report = 1;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_get_board_info(int board)
{
dprintf("Chunithm LED Strip: Get board info (board %u)\n", board);
struct led1509306_resp_board_info resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = sizeof(resp.data) + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_BOARD_INFO;
resp.report = 1;
memcpy(resp.data.board_num, led1509306_board_num, sizeof(resp.data.board_num));
resp.data._0a = 0x0a;
memcpy(resp.data.chip_num, led1509306_chip_num, sizeof(resp.data.chip_num));
resp.data._ff = 0xff;
resp.data.fw_ver = led1509306_fw_ver;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_get_fw_sum(int board)
{
dprintf("Chunithm LED Strip: Get firmware checksum (board %u)\n", board);
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 2 + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_FW_SUM;
resp.report = 1;
resp.data[0] = (led1509306_fw_sum >> 8) & 0xff;
resp.data[1] = led1509306_fw_sum & 0xff;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_get_protocol_ver(int board)
{
dprintf("Chunithm LED Strip: Get protocol version (board %u)\n", board);
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 3 + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_PROTOCOL_VER;
resp.report = 1;
resp.data[0] = 1;
resp.data[1] = 1;
resp.data[2] = 4;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_get_board_status(int board)
{
dprintf("Chunithm LED Strip: Get board status (board %u)\n", board);
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 4 + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_BOARD_STATUS;
resp.report = 1;
resp.data[0] = 0;
resp.data[1] = 0;
resp.data[2] = 0;
resp.data[3] = 0;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_set_led(int board, const struct led1509306_req_any *req)
{
// dprintf("Chunithm LED Strip: Set LED (board %u)\n", board);
if (!led1509306_per_board_vars[board].enable_response)
return S_OK;
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_SET_LED;
resp.report = 1;
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_set_disable_response(int board, const struct led1509306_req_any *req)
{
dprintf("Chunithm LED Strip: Disable LED responses (board %u)\n", board);
led1509306_per_board_vars[board].enable_response = !req->payload[0];
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 1 + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_SET_DISABLE_RESPONSE;
resp.report = 1;
resp.data[0] = req->payload[0];
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}
static HRESULT led1509306_req_set_timeout(int board, const struct led1509306_req_any *req)
{
dprintf("Chunithm LED Strip: Set timeout (board %u)\n", board);
// not actually implemented, but respond correctly anyway
struct led1509306_resp_any resp;
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = LED_15093_06_FRAME_SYNC;
resp.hdr.dest_adr = led1509306_host_adr;
resp.hdr.src_adr = led1509306_board_adr;
resp.hdr.nbytes = 2 + 3;
resp.status = 1;
resp.cmd = LED_15093_06_CMD_SET_TIMEOUT;
resp.report = 1;
resp.data[0] = req->payload[0];
resp.data[1] = req->payload[1];
return led1509306_frame_encode(&led1509306_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes);
}

15
chusanhook/led1509306.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
struct led1509306_config {
bool enable;
char board_number[8];
char chip_number[5];
uint8_t fw_ver;
uint16_t fw_sum;
};
HRESULT led1509306_hook_init(const struct led1509306_config *cfg);

34
chusanhook/meson.build Normal file
View File

@ -0,0 +1,34 @@
shared_library(
'chusanhook',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'chusanhook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),
],
link_with : [
aimeio_lib,
board_lib,
chuniio_lib,
gfxhook_lib,
hooklib_lib,
platform_lib,
util_lib,
],
sources : [
'chuni-dll.c',
'chuni-dll.h',
'config.c',
'config.h',
'dllmain.c',
'io4.c',
'io4.h',
'slider.c',
'slider.h',
'led1509306.c',
'led1509306.h',
],
)

249
chusanhook/slider.c Normal file
View File

@ -0,0 +1,249 @@
#include <windows.h>
#include <assert.h>
#include <process.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "board/slider-cmd.h"
#include "board/slider-frame.h"
#include "chusanhook/chuni-dll.h"
#include "chusanhook/slider.h"
#include "hook/iobuf.h"
#include "hook/iohook.h"
#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);
static HRESULT slider_req_dispatch(const union slider_req_any *req);
static HRESULT slider_req_reset(void);
static HRESULT slider_req_get_board_info(void);
static HRESULT slider_req_auto_scan_start(void);
static HRESULT slider_req_auto_scan_stop(void);
static HRESULT slider_req_set_led(const struct slider_req_set_led *req);
static void slider_res_auto_scan(const uint8_t *state);
static CRITICAL_SECTION slider_lock;
static struct uart slider_uart;
static uint8_t slider_written_bytes[520];
static uint8_t slider_readable_bytes[520];
HRESULT slider_hook_init(const struct slider_config *cfg)
{
assert(cfg != NULL);
assert(chuni_dll.slider_init != NULL);
if (!cfg->enable) {
return S_FALSE;
}
InitializeCriticalSection(&slider_lock);
uart_init(&slider_uart, 1);
slider_uart.written.bytes = slider_written_bytes;
slider_uart.written.nbytes = sizeof(slider_written_bytes);
slider_uart.readable.bytes = slider_readable_bytes;
slider_uart.readable.nbytes = sizeof(slider_readable_bytes);
return iohook_push_handler(slider_handle_irp);
}
static HRESULT slider_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (!uart_match_irp(&slider_uart, irp)) {
return iohook_invoke_next(irp);
}
EnterCriticalSection(&slider_lock);
hr = slider_handle_irp_locked(irp);
LeaveCriticalSection(&slider_lock);
return hr;
}
static HRESULT slider_handle_irp_locked(struct irp *irp)
{
union slider_req_any req;
struct iobuf req_iobuf;
HRESULT hr;
if (irp->op == IRP_OP_OPEN) {
dprintf("Chunithm slider: Starting backend\n");
hr = chuni_dll.slider_init();
if (FAILED(hr)) {
dprintf("Chunithm slider: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&slider_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX Buffer:\n");
dump_iobuf(&slider_uart.written);
#endif
req_iobuf.bytes = req.bytes;
req_iobuf.nbytes = sizeof(req.bytes);
req_iobuf.pos = 0;
hr = slider_frame_decode(&req_iobuf, &slider_uart.written);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Chunithm slider: Deframe error: %x\n", (int) hr);
}
return hr;
}
#if 0
dprintf("Deframe Buffer:\n");
dump_iobuf(&req_iobuf);
#endif
hr = slider_req_dispatch(&req);
if (FAILED(hr)) {
dprintf("Chunithm slider: Processing error: %x\n", (int) hr);
}
}
}
static HRESULT slider_req_dispatch(const union slider_req_any *req)
{
switch (req->hdr.cmd) {
case SLIDER_CMD_RESET:
return slider_req_reset();
case SLIDER_CMD_GET_BOARD_INFO:
return slider_req_get_board_info();
case SLIDER_CMD_SET_LED:
return slider_req_set_led(&req->set_led);
case SLIDER_CMD_AUTO_SCAN_START:
return slider_req_auto_scan_start();
case SLIDER_CMD_AUTO_SCAN_STOP:
return slider_req_auto_scan_stop();
default:
dprintf("Unhandled command %02x\n", req->hdr.cmd);
return S_OK;
}
}
static HRESULT slider_req_reset(void)
{
struct slider_hdr resp;
dprintf("Chunithm slider: Reset\n");
resp.sync = 0xFF;
resp.cmd = SLIDER_CMD_RESET;
resp.nbytes = 0;
return slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
}
static HRESULT slider_req_get_board_info(void)
{
struct slider_resp_get_board_info resp;
dprintf("Chunithm slider: Get firmware version\n");
memset(&resp, 0, sizeof(resp));
resp.hdr.sync = SLIDER_FRAME_SYNC;
resp.hdr.cmd = SLIDER_CMD_GET_BOARD_INFO;
resp.hdr.nbytes = sizeof(resp.version);
strcpy_s(
resp.version,
sizeof(resp.version),
"15330 \xA0" "06712\xFF" "\x90");
return slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
}
static HRESULT slider_req_auto_scan_start(void)
{
assert(chuni_dll.slider_start != NULL);
dprintf("Chunithm slider: Start slider notifications\n");
chuni_dll.slider_start(slider_res_auto_scan);
/* This message is not acknowledged */
return S_OK;
}
static HRESULT slider_req_auto_scan_stop(void)
{
struct slider_hdr resp;
assert(chuni_dll.slider_stop != NULL);
dprintf("Chunithm slider: Stop slider notifications\n");
/* IO DLL worker thread might attempt to invoke the callback (which needs
to take slider_lock, which we are currently holding) before noticing that
it needs to shut down. Unlock here so that we don't deadlock in that
situation. */
LeaveCriticalSection(&slider_lock);
chuni_dll.slider_stop();
EnterCriticalSection(&slider_lock);
resp.sync = SLIDER_FRAME_SYNC;
resp.cmd = SLIDER_CMD_AUTO_SCAN_STOP;
resp.nbytes = 0;
return slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
}
static HRESULT slider_req_set_led(const struct slider_req_set_led *req)
{
assert(chuni_dll.slider_set_leds != NULL);
chuni_dll.slider_set_leds(req->payload.rgb);
/* This message is not acknowledged */
return S_OK;
}
static void slider_res_auto_scan(const uint8_t *state)
{
struct slider_resp_auto_scan resp;
resp.hdr.sync = SLIDER_FRAME_SYNC;
resp.hdr.cmd = SLIDER_CMD_AUTO_SCAN;
resp.hdr.nbytes = sizeof(resp.pressure);
memcpy(resp.pressure, state, sizeof(resp.pressure));
EnterCriticalSection(&slider_lock);
slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
LeaveCriticalSection(&slider_lock);
}

11
chusanhook/slider.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
struct slider_config {
bool enable;
};
HRESULT slider_hook_init(const struct slider_config *cfg);