2019-02-26 18:47:41 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2019-03-16 15:42:36 +00:00
|
|
|
#include <stdbool.h>
|
2019-02-26 18:47:41 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2021-05-23 15:54:24 +00:00
|
|
|
#include "board/aime-dll.h"
|
2019-02-26 18:47:41 +00:00
|
|
|
#include "board/sg-led.h"
|
|
|
|
#include "board/sg-nfc.h"
|
|
|
|
#include "board/sg-reader.h"
|
|
|
|
|
|
|
|
#include "hook/iohook.h"
|
|
|
|
|
|
|
|
#include "hooklib/uart.h"
|
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
#include "util/dump.h"
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
static HRESULT sg_reader_handle_irp(struct irp *irp);
|
|
|
|
static HRESULT sg_reader_handle_irp_locked(struct irp *irp);
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_reader_nfc_poll(void *ctx);
|
|
|
|
static HRESULT sg_reader_nfc_get_aime_id(
|
2019-03-16 15:42:36 +00:00
|
|
|
void *ctx,
|
|
|
|
uint8_t *luid,
|
|
|
|
size_t luid_size);
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm);
|
2019-03-16 15:42:36 +00:00
|
|
|
static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
|
|
|
|
static const struct sg_nfc_ops sg_reader_nfc_ops = {
|
2019-10-19 19:51:10 +00:00
|
|
|
.poll = sg_reader_nfc_poll,
|
|
|
|
.get_aime_id = sg_reader_nfc_get_aime_id,
|
|
|
|
.get_felica_id = sg_reader_nfc_get_felica_id,
|
2019-03-16 15:42:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct sg_led_ops sg_reader_led_ops = {
|
|
|
|
.set_color = sg_reader_led_set_color,
|
|
|
|
};
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
static CRITICAL_SECTION sg_reader_lock;
|
2019-11-03 20:37:29 +00:00
|
|
|
static bool sg_reader_started;
|
|
|
|
static HRESULT sg_reader_start_hr;
|
2019-05-03 21:21:38 +00:00
|
|
|
static struct uart sg_reader_uart;
|
|
|
|
static uint8_t sg_reader_written_bytes[520];
|
|
|
|
static uint8_t sg_reader_readable_bytes[520];
|
|
|
|
static struct sg_nfc sg_reader_nfc;
|
|
|
|
static struct sg_led sg_reader_led;
|
|
|
|
|
2019-06-04 02:28:44 +00:00
|
|
|
HRESULT sg_reader_hook_init(
|
|
|
|
const struct aime_config *cfg,
|
2021-05-23 15:54:24 +00:00
|
|
|
unsigned int port_no,
|
|
|
|
HINSTANCE self)
|
2019-02-26 18:47:41 +00:00
|
|
|
{
|
2021-05-23 15:54:24 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
2019-06-04 02:28:44 +00:00
|
|
|
assert(cfg != NULL);
|
2021-05-23 15:54:24 +00:00
|
|
|
assert(self != NULL);
|
2019-06-04 02:28:44 +00:00
|
|
|
|
|
|
|
if (!cfg->enable) {
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:54:24 +00:00
|
|
|
hr = aime_dll_init(&cfg->dll, self);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
sg_nfc_init(&sg_reader_nfc, 0x00, &sg_reader_nfc_ops, NULL);
|
|
|
|
sg_led_init(&sg_reader_led, 0x08, &sg_reader_led_ops, NULL);
|
2019-02-26 18:47:41 +00:00
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
InitializeCriticalSection(&sg_reader_lock);
|
2019-03-16 15:42:36 +00:00
|
|
|
|
2023-08-15 15:28:19 +00:00
|
|
|
if (!cfg->high_baudrate) {
|
|
|
|
sg_reader_uart.baud.BaudRate = 38400;
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
uart_init(&sg_reader_uart, port_no);
|
|
|
|
sg_reader_uart.written.bytes = sg_reader_written_bytes;
|
|
|
|
sg_reader_uart.written.nbytes = sizeof(sg_reader_written_bytes);
|
|
|
|
sg_reader_uart.readable.bytes = sg_reader_readable_bytes;
|
|
|
|
sg_reader_uart.readable.nbytes = sizeof(sg_reader_readable_bytes);
|
2019-03-16 15:42:36 +00:00
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
return iohook_push_handler(sg_reader_handle_irp);
|
2019-02-26 18:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
static HRESULT sg_reader_handle_irp(struct irp *irp)
|
2019-02-26 18:47:41 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
if (!uart_match_irp(&sg_reader_uart, irp)) {
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&sg_reader_lock);
|
|
|
|
hr = sg_reader_handle_irp_locked(irp);
|
|
|
|
LeaveCriticalSection(&sg_reader_lock);
|
2019-02-26 18:47:41 +00:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
static HRESULT sg_reader_handle_irp_locked(struct irp *irp)
|
2019-02-26 18:47:41 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (irp->op == IRP_OP_WRITE) {
|
|
|
|
dprintf("WRITE:\n");
|
|
|
|
dump_const_iobuf(&irp->write);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (irp->op == IRP_OP_READ) {
|
|
|
|
dprintf("READ:\n");
|
2019-05-03 21:21:38 +00:00
|
|
|
dump_iobuf(&sg_reader_uart.readable);
|
2019-02-26 18:47:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-11-03 20:37:29 +00:00
|
|
|
if (irp->op == IRP_OP_OPEN) {
|
|
|
|
/* Unfortunately the card reader UART gets opened and closed
|
|
|
|
repeatedly */
|
|
|
|
|
|
|
|
if (!sg_reader_started) {
|
|
|
|
dprintf("NFC Assembly: Starting backend DLL\n");
|
2021-05-23 15:54:24 +00:00
|
|
|
hr = aime_dll.init();
|
2019-11-03 20:37:29 +00:00
|
|
|
|
|
|
|
sg_reader_started = true;
|
|
|
|
sg_reader_start_hr = hr;
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("NFC Assembly: Backend error: %x\n", (int) hr);
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = sg_reader_start_hr;
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
hr = uart_handle_irp(&sg_reader_uart, irp);
|
2019-02-26 18:47:41 +00:00
|
|
|
|
|
|
|
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sg_nfc_transact(
|
2019-05-03 21:21:38 +00:00
|
|
|
&sg_reader_nfc,
|
|
|
|
&sg_reader_uart.readable,
|
|
|
|
sg_reader_uart.written.bytes,
|
|
|
|
sg_reader_uart.written.pos);
|
2019-02-26 18:47:41 +00:00
|
|
|
|
|
|
|
sg_led_transact(
|
2019-05-03 21:21:38 +00:00
|
|
|
&sg_reader_led,
|
|
|
|
&sg_reader_uart.readable,
|
|
|
|
sg_reader_uart.written.bytes,
|
|
|
|
sg_reader_uart.written.pos);
|
2019-02-26 18:47:41 +00:00
|
|
|
|
2019-05-03 21:21:38 +00:00
|
|
|
sg_reader_uart.written.pos = 0;
|
2019-02-26 18:47:41 +00:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
2019-03-16 15:42:36 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_reader_nfc_poll(void *ctx)
|
2019-03-16 15:42:36 +00:00
|
|
|
{
|
2021-05-23 15:54:24 +00:00
|
|
|
return aime_dll.nfc_poll(0);
|
2019-03-16 15:42:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_reader_nfc_get_aime_id(
|
2019-03-16 15:42:36 +00:00
|
|
|
void *ctx,
|
|
|
|
uint8_t *luid,
|
|
|
|
size_t luid_size)
|
|
|
|
{
|
2021-05-23 15:54:24 +00:00
|
|
|
return aime_dll.nfc_get_aime_id(0, luid, luid_size);
|
2019-10-19 19:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm)
|
|
|
|
{
|
2021-05-23 15:54:24 +00:00
|
|
|
return aime_dll.nfc_get_felica_id(0, IDm);
|
2019-03-16 15:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
2021-05-23 15:54:24 +00:00
|
|
|
aime_dll.led_set_color(0, r, g, b);
|
2019-03-16 15:42:36 +00:00
|
|
|
}
|