2019-03-16 16:57:17 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "board/slider-cmd.h"
|
|
|
|
#include "board/slider-frame.h"
|
|
|
|
|
2019-05-03 21:59:23 +02:00
|
|
|
#include "divahook/slider.h"
|
2019-03-16 16:57:17 +01:00
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
#include "divaio/divaio.h"
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
#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);
|
2019-12-30 01:59:35 +01:00
|
|
|
static HRESULT slider_req_nop(uint8_t cmd);
|
2019-03-16 16:57:17 +01:00
|
|
|
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);
|
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
static void slider_res_auto_scan(const uint8_t *pressure);
|
2019-03-16 16:57:17 +01:00
|
|
|
|
|
|
|
static CRITICAL_SECTION slider_lock;
|
|
|
|
static struct uart slider_uart;
|
|
|
|
static uint8_t slider_written_bytes[520];
|
|
|
|
static uint8_t slider_readable_bytes[520];
|
|
|
|
|
2019-12-29 18:28:32 +01:00
|
|
|
HRESULT slider_hook_init(const struct slider_config *cfg)
|
2019-03-16 16:57:17 +01:00
|
|
|
{
|
2019-12-29 18:28:32 +01:00
|
|
|
assert(cfg != NULL);
|
2019-12-30 03:20:54 +01:00
|
|
|
|
2019-12-29 18:28:32 +01:00
|
|
|
if (!cfg->enable) {
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
InitializeCriticalSection(&slider_lock);
|
|
|
|
|
|
|
|
uart_init(&slider_uart, 11);
|
|
|
|
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);
|
|
|
|
|
2019-11-06 00:03:16 +01:00
|
|
|
return iohook_push_handler(slider_handle_irp);
|
2019-03-16 16:57:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-11-03 19:01:03 +01:00
|
|
|
if (irp->op == IRP_OP_OPEN) {
|
|
|
|
dprintf("Diva slider: Starting backend DLL\n");
|
2021-06-06 15:39:45 +02:00
|
|
|
hr = diva_io_slider_init();
|
2019-11-03 19:01:03 +01:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Diva slider: Backend DLL error: %x\n", (int) hr);
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
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("Diva 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("Diva 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_AUTO_SCAN_START:
|
|
|
|
return slider_req_auto_scan_start();
|
|
|
|
|
|
|
|
case SLIDER_CMD_AUTO_SCAN_STOP:
|
|
|
|
return slider_req_auto_scan_stop();
|
|
|
|
|
2019-12-30 03:48:35 +01:00
|
|
|
case SLIDER_CMD_SET_LED:
|
2019-03-16 16:57:17 +01:00
|
|
|
return slider_req_set_led(&req->set_led);
|
|
|
|
|
2019-12-30 01:59:35 +01:00
|
|
|
case SLIDER_CMD_DIVA_UNK_09:
|
|
|
|
case SLIDER_CMD_DIVA_UNK_0A:
|
|
|
|
return slider_req_nop(req->hdr.cmd);
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
default:
|
|
|
|
dprintf("Unhandled command %02x\n", req->hdr.cmd);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 01:59:35 +01:00
|
|
|
static HRESULT slider_req_nop(uint8_t cmd)
|
|
|
|
{
|
|
|
|
struct slider_hdr resp;
|
|
|
|
|
2019-12-30 03:20:54 +01:00
|
|
|
dprintf("Diva slider: No-op cmd 0x%#02x\n", cmd);
|
2019-12-30 01:59:35 +01:00
|
|
|
|
|
|
|
resp.sync = SLIDER_FRAME_SYNC;
|
|
|
|
resp.cmd = cmd;
|
|
|
|
resp.nbytes = 0;
|
|
|
|
|
|
|
|
return slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
static HRESULT slider_req_reset(void)
|
|
|
|
{
|
|
|
|
struct slider_hdr resp;
|
|
|
|
|
|
|
|
dprintf("Diva 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("Diva 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);
|
|
|
|
|
|
|
|
memset(resp.version, 0, sizeof(resp.version));
|
|
|
|
strcpy_s(
|
|
|
|
resp.version,
|
|
|
|
sizeof(resp.version),
|
2019-12-30 01:59:35 +01:00
|
|
|
"15275 \xA0" "06687\xFF" "\x90");
|
2019-03-16 16:57:17 +01:00
|
|
|
|
|
|
|
return slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT slider_req_auto_scan_start(void)
|
|
|
|
{
|
|
|
|
dprintf("Diva slider: Start slider thread\n");
|
2019-05-03 20:47:09 +02:00
|
|
|
diva_io_slider_start(slider_res_auto_scan);
|
2019-03-16 16:57:17 +01:00
|
|
|
|
|
|
|
/* This message is not acknowledged */
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT slider_req_auto_scan_stop(void)
|
|
|
|
{
|
|
|
|
struct slider_hdr resp;
|
|
|
|
|
|
|
|
dprintf("Diva slider: Stop slider thread\n");
|
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
/* 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. */
|
2019-03-16 16:57:17 +01:00
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
LeaveCriticalSection(&slider_lock);
|
|
|
|
diva_io_slider_stop();
|
|
|
|
EnterCriticalSection(&slider_lock);
|
2019-03-16 16:57:17 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* This message is not acknowledged */
|
2019-05-03 20:47:09 +02:00
|
|
|
diva_io_slider_set_leds(req->payload.rgb);
|
|
|
|
|
2019-03-16 16:57:17 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
static void slider_res_auto_scan(const uint8_t *pressure)
|
2019-03-16 16:57:17 +01:00
|
|
|
{
|
|
|
|
struct slider_resp_auto_scan resp;
|
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
resp.hdr.sync = SLIDER_FRAME_SYNC;
|
|
|
|
resp.hdr.cmd = SLIDER_CMD_AUTO_SCAN;
|
|
|
|
resp.hdr.nbytes = sizeof(resp.pressure);
|
|
|
|
memcpy(resp.pressure, pressure, sizeof(resp.pressure));
|
2019-03-16 16:57:17 +01:00
|
|
|
|
2019-05-03 20:47:09 +02:00
|
|
|
EnterCriticalSection(&slider_lock);
|
|
|
|
slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
|
|
|
|
LeaveCriticalSection(&slider_lock);
|
2019-03-16 16:57:17 +01:00
|
|
|
}
|