From bc2928804cabaf858a33f7629f531578a1f8d4c2 Mon Sep 17 00:00:00 2001 From: Tau Date: Tue, 26 Feb 2019 13:47:41 -0500 Subject: [PATCH] board/sg-reader.c: Factor out NFC ass'y emu Combine the NFC module emulator with the LED module emulator and a virtual UART to build a higher-level reusable building block. --- board/meson.build | 2 + board/sg-reader.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++ board/sg-reader.h | 28 +++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 board/sg-reader.c create mode 100644 board/sg-reader.h diff --git a/board/meson.build b/board/meson.build index 4b7a4dd..79dea26 100644 --- a/board/meson.build +++ b/board/meson.build @@ -22,6 +22,8 @@ board_lib = static_library( 'sg-nfc.c', 'sg-nfc.h', 'sg-nfc-cmd.h', + 'sg-reader.c', + 'sg-reader.h', 'slider-cmd.h', 'slider-frame.c', 'slider-frame.h', diff --git a/board/sg-reader.c b/board/sg-reader.c new file mode 100644 index 0000000..a39107e --- /dev/null +++ b/board/sg-reader.c @@ -0,0 +1,103 @@ +#include + +#include +#include + +#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" + +static HRESULT sg_reader_handle_irp_locked( + struct sg_reader *reader, + struct irp *irp); + +void sg_reader_init( + struct sg_reader *reader, + unsigned int port_no, + const struct sg_nfc_ops *nfc_ops, + const struct sg_led_ops *led_ops, + void *ops_ctx) +{ + assert(reader != NULL); + assert(nfc_ops != NULL); + assert(led_ops != NULL); + + sg_nfc_init(&reader->nfc, 0x00, nfc_ops, ops_ctx); + sg_led_init(&reader->led, 0x08, led_ops, ops_ctx); + + InitializeCriticalSection(&reader->lock); + + uart_init(&reader->uart, port_no); + reader->uart.written.bytes = reader->written_bytes; + reader->uart.written.nbytes = sizeof(reader->written_bytes); + reader->uart.readable.bytes = reader->readable_bytes; + reader->uart.readable.nbytes = sizeof(reader->readable_bytes); +} + +HRESULT sg_reader_handle_irp(struct sg_reader *reader, struct irp *irp) +{ + HRESULT hr; + + assert(reader != NULL); + assert(irp != NULL); + + if (!uart_match_irp(&reader->uart, irp)) { + return iohook_invoke_next(irp); + } + + EnterCriticalSection(&reader->lock); + hr = sg_reader_handle_irp_locked(reader, irp); + LeaveCriticalSection(&reader->lock); + + return hr; +} + +static HRESULT sg_reader_handle_irp_locked( + struct sg_reader *reader, + struct irp *irp) +{ + 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"); + dump_iobuf(&reader->uart.readable); + } +#endif + + hr = uart_handle_irp(&reader->uart, irp); + + if (FAILED(hr) || irp->op != IRP_OP_WRITE) { + return hr; + } + + sg_nfc_transact( + &reader->nfc, + &reader->uart.readable, + reader->uart.written.bytes, + reader->uart.written.pos); + + sg_led_transact( + &reader->led, + &reader->uart.readable, + reader->uart.written.bytes, + reader->uart.written.pos); + + reader->uart.written.pos = 0; + + return hr; +} diff --git a/board/sg-reader.h b/board/sg-reader.h new file mode 100644 index 0000000..d14769e --- /dev/null +++ b/board/sg-reader.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include + +#include "board/sg-led.h" +#include "board/sg-nfc.h" + +#include "hooklib/uart.h" + +struct sg_reader { + CRITICAL_SECTION lock; + struct uart uart; + uint8_t written_bytes[520]; + uint8_t readable_bytes[520]; + struct sg_nfc nfc; + struct sg_led led; +}; + +void sg_reader_init( + struct sg_reader *reader, + unsigned int port_no, + const struct sg_nfc_ops *nfc_ops, + const struct sg_led_ops *led_ops, + void *ops_ctx); + +HRESULT sg_reader_handle_irp(struct sg_reader *reader, struct irp *irp);