aimeio: Factor out emulated reader assembly app logic

This commit is contained in:
Tau 2019-02-25 21:56:45 -05:00
parent 2cbb18604b
commit 662bfa55ec
8 changed files with 158 additions and 63 deletions

96
aimeio/aimeio.c Normal file
View File

@ -0,0 +1,96 @@
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "aimeio/aimeio.h"
#include "util/crc.h"
#include "util/dprintf.h"
static const char aime_io_path[] = "DEVICE\\aime.txt";
static uint8_t aime_io_luid[10];
HRESULT aime_io_init(void)
{
return S_OK;
}
void aime_io_fini(void)
{
}
HRESULT aime_io_mifare_poll(uint8_t unit_no, uint32_t *uid)
{
HRESULT hr;
FILE *f;
size_t i;
int byte;
int r;
if (unit_no != 0) {
return S_FALSE;
}
hr = S_FALSE;
f = NULL;
if (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) {
goto end;
}
f = fopen(aime_io_path, "r");
if (f == NULL) {
dprintf("Aime DLL: Failed to open %s\n", aime_io_path);
goto end;
}
for (i = 0 ; i < sizeof(aime_io_luid) ; i++) {
r = fscanf(f, "%02x ", &byte);
if (r != 1) {
dprintf("Aime DLL: fscanf[%i] failed: %i\n", (int) i, r);
goto end;
}
aime_io_luid[i] = byte;
}
/* NOTE: We are just arbitrarily using the CRC32 of the LUID here, real
cards do not work like this! However, neither the application code nor
the network protocol care what the UID is, it just has to be a stable
unique identifier for over-the-air NFC communications. */
*uid = crc32(aime_io_luid, sizeof(aime_io_luid), 0);
hr = S_OK;
end:
if (f != NULL) {
fclose(f);
}
return hr;
}
HRESULT aime_io_mifare_read_luid(
uint8_t unit_no,
uint32_t uid,
uint8_t *luid,
size_t luid_size)
{
assert(luid != NULL);
assert(luid_size == sizeof(aime_io_luid));
memcpy(luid, aime_io_luid, luid_size);
return S_OK;
}
void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b)
{}

8
aimeio/aimeio.def Normal file
View File

@ -0,0 +1,8 @@
LIBRARY aimeio
EXPORTS
aime_io_fini
aime_io_init
aime_io_led_set_color
aime_io_mifare_poll
aime_io_mifare_read_luid

16
aimeio/aimeio.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
HRESULT aime_io_init(void);
void aime_io_fini(void);
HRESULT aime_io_mifare_poll(uint8_t unit_no, uint32_t *uid);
HRESULT aime_io_mifare_read_luid(
uint8_t unit_no,
uint32_t uid,
uint8_t *luid,
size_t luid_size);
void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b);

17
aimeio/meson.build Normal file
View File

@ -0,0 +1,17 @@
aimeio_dll = shared_library(
'aimeio',
name_prefix : '',
include_directories: inc,
implicit_include_directories : false,
vs_module_defs : 'aimeio.def',
c_pch : [
'../precompiled.c',
'../precompiled.h',
],
link_with : [
util_lib,
],
sources : [
'aimeio.c',
],
)

View File

@ -2,7 +2,8 @@
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include "aimeio/aimeio.h"
#include "board/sg-led.h" #include "board/sg-led.h"
#include "board/sg-nfc.h" #include "board/sg-nfc.h"
@ -13,7 +14,6 @@
#include "hooklib/uart.h" #include "hooklib/uart.h"
#include "util/crc.h"
#include "util/dprintf.h" #include "util/dprintf.h"
#include "util/dump.h" #include "util/dump.h"
@ -40,16 +40,21 @@ static const struct sg_led_ops com12_led_ops = {
static struct sg_nfc com12_nfc; static struct sg_nfc com12_nfc;
static struct sg_led com12_led; static struct sg_led com12_led;
static const char com12_aime_path[] = "DEVICE\\aime.txt";
static CRITICAL_SECTION com12_lock; static CRITICAL_SECTION com12_lock;
static struct uart com12_uart; static struct uart com12_uart;
static uint8_t com12_written_bytes[520]; static uint8_t com12_written_bytes[520];
static uint8_t com12_readable_bytes[520]; static uint8_t com12_readable_bytes[520];
static uint8_t com12_aime_luid[10];
void com12_hook_init(void) HRESULT com12_hook_init(void)
{ {
HRESULT hr;
hr = aime_io_init();
if (FAILED(hr)) {
return hr;
}
sg_nfc_init(&com12_nfc, 0x00, &com12_nfc_ops, NULL); sg_nfc_init(&com12_nfc, 0x00, &com12_nfc_ops, NULL);
sg_led_init(&com12_led, 0x08, &com12_led_ops, NULL); sg_led_init(&com12_led, 0x08, &com12_led_ops, NULL);
@ -61,7 +66,7 @@ void com12_hook_init(void)
com12_uart.readable.bytes = com12_readable_bytes; com12_uart.readable.bytes = com12_readable_bytes;
com12_uart.readable.nbytes = sizeof(com12_readable_bytes); com12_uart.readable.nbytes = sizeof(com12_readable_bytes);
iohook_push_handler(com12_handle_irp); return iohook_push_handler(com12_handle_irp);
} }
static HRESULT com12_handle_irp(struct irp *irp) static HRESULT com12_handle_irp(struct irp *irp)
@ -124,70 +129,19 @@ static HRESULT com12_handle_irp_locked(struct irp *irp)
static HRESULT com12_mifare_poll(void *ctx, uint32_t *uid) static HRESULT com12_mifare_poll(void *ctx, uint32_t *uid)
{ {
HRESULT hr; return aime_io_mifare_poll(0, uid);
FILE *f;
size_t i;
int byte;
int r;
hr = S_FALSE;
f = NULL;
if (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) {
goto end;
}
f = fopen(com12_aime_path, "r");
if (f == NULL) {
dprintf("Aime reader: Failed to open %s\n", com12_aime_path);
goto end;
}
for (i = 0 ; i < sizeof(com12_aime_luid) ; i++) {
r = fscanf(f, "%02x ", &byte);
if (r != 1) {
dprintf("Aime reader: fscanf[%i] failed: %i\n", (int) i, r);
goto end;
}
com12_aime_luid[i] = byte;
}
/* NOTE: We are just arbitrarily using the CRC32 of the LUID here, real
cards do not work like this! However, neither the application code nor
the network protocol care what the UID is, it just has to be a stable
unique identifier for over-the-air NFC communications. */
*uid = crc32(com12_aime_luid, sizeof(com12_aime_luid), 0);
hr = S_OK;
end:
if (f != NULL) {
fclose(f);
}
return hr;
} }
static HRESULT com12_mifare_read_luid( static HRESULT com12_mifare_read_luid(
void *ctx, void *ctx,
uint32_t uid, uint32_t uid,
uint8_t *luid, uint8_t *luid,
size_t nbytes) size_t luid_size)
{ {
assert(luid != NULL); return aime_io_mifare_read_luid(0, uid, luid, luid_size);
assert(nbytes == sizeof(com12_aime_luid));
memcpy(luid, com12_aime_luid, nbytes);
return S_OK;
} }
static void com12_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b) static void com12_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b)
{ {
aime_io_led_set_color(0, r, g, b);
} }

View File

@ -1,3 +1,5 @@
#pragma once #pragma once
void com12_hook_init(void); #include <windows.h>
HRESULT com12_hook_init(void);

View File

@ -12,6 +12,7 @@ shared_library(
capnhook.get_variable('hooklib_dep'), capnhook.get_variable('hooklib_dep'),
], ],
link_with : [ link_with : [
aimeio_dll,
board_lib, board_lib,
util_lib, util_lib,
], ],

View File

@ -31,6 +31,7 @@ subdir('jvs')
subdir('nu') subdir('nu')
subdir('util') subdir('util')
subdir('aimeio')
subdir('cardhook') subdir('cardhook')
subdir('chunihook') subdir('chunihook')
subdir('minihook') subdir('minihook')