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',
],
)