Compare commits

...

4 Commits

Author SHA1 Message Date
f2769e094f partial : Cleaned code a bit, prepping for reading Mifare tags
Added two new options in settings :
- One to not error out if a reader is missing
- One to force using a reader using it's name.
2024-02-28 12:09:57 +01:00
50fd77e42c Enhanced polling
Previously, a card could be scanned twice in a row and it's state would be kept in memory so the card would be scanned again as soon as a credit ended.

Added debug strings
2024-02-28 12:09:57 +01:00
d2bef31648 Added ability to insert cards from files
Merged logic to read cards from a local file from CrazyRedMachine's redboard repository.

Added the ability to select a card from the files by pressing and holding a number on the keypad.

Bumped version
2024-02-28 12:09:57 +01:00
719ba98e77 Added ACR122U support
This currently only really works well with felica lite cards (amusement IC).

Older cards or different types (Mifare) should work too but their id won't be correct.

Updated readme

Edited build64.bat
2024-02-28 12:09:57 +01:00
19 changed files with 851 additions and 556 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
.vscode .vscode
build64/*
bin/*
*.dll

View File

@ -1,113 +0,0 @@
#include <windows.h>
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "aimeio/aimeio.h"
#include "aimepcsc/aimepcsc.h"
static struct aimepcsc_context *ctx;
static struct aime_data data;
uint16_t aime_io_get_api_version(void)
{
return 0x0100;
}
HRESULT aime_io_init(void)
{
int ret;
FILE* fp;
ret = AllocConsole();
// someone might already allocated a console - seeing this on fufubot's segatools
if (ret != 0) {
// only when we allocate a console, we need to redirect stdout
freopen_s(&fp, "CONOUT$", "w", stdout);
}
ctx = aimepcsc_create();
if (!ctx) {
return E_OUTOFMEMORY;
}
ret = aimepcsc_init(ctx);
if (ret != 0) {
printf("aimeio-pcsc: failed to initialize: %s\n", aimepcsc_error(ctx));
aimepcsc_destroy(ctx);
ctx = NULL;
return E_FAIL;
}
printf("aimeio-pcsc: initialized with reader: %s\n", aimepcsc_reader_name(ctx));
return S_OK;
}
HRESULT aime_io_nfc_poll(uint8_t unit_no)
{
int ret;
HRESULT hr;
if (unit_no != 0) {
return S_OK;
}
memset(&data, 0, sizeof(data));
ret = aimepcsc_poll(ctx, &data);
if (ret < 0) {
printf("aimeio-pcsc: poll failed: %s\n", aimepcsc_error(ctx));
}
return S_OK;
}
HRESULT aime_io_nfc_get_aime_id(
uint8_t unit_no,
uint8_t *luid,
size_t luid_size)
{
assert(luid != NULL);
if (unit_no != 0 || data.card_type != Mifare || luid_size != data.card_id_len) {
return S_FALSE;
}
memcpy(luid, data.card_id, luid_size);
return S_OK;
}
HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm)
{
uint64_t val;
size_t i;
assert(IDm != NULL);
if (unit_no != 0 || data.card_type != FeliCa || data.card_id_len != 8) {
return S_FALSE;
}
val = 0;
for (i = 0 ; i < 8 ; i++) {
val = (val << 8) | data.card_id[i];
}
*IDm = val;
return S_OK;
}
void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b)
{}

View File

@ -1,11 +0,0 @@
aimeio_lib = shared_library(
'aimeio',
name_prefix : '',
include_directories: inc,
implicit_include_directories : false,
vs_module_defs : 'aimeio.def',
link_with: [aimepcsc_lib],
sources : [
'aimeio.c',
],
)

View File

@ -1,222 +0,0 @@
#include "aimepcsc.h"
#include <stdio.h>
static const uint8_t atr_ios14443_common[] = {0x3B, 0x8F, 0x80, 0x01, 0x80, 0x4F, 0x0C, 0xA0, 0x00, 0x00, 0x03, 0x06};
static const uint8_t cardtype_m1k[] = {0x03, 0x00, 0x01};
static const uint8_t cardtype_felica[] = {0x11, 0x00, 0x3B};
static const uint8_t felica_cmd_readidm[] = {0xFF, 0xCA, 0x00, 0x00, 0x00};
static const uint8_t m1k_cmd_loadkey[] = {0xFF, 0x82, 0x00, 0x00, 0x06, 0x57, 0x43, 0x43, 0x46, 0x76, 0x32};
static const uint8_t m1k_cmd_auth_block2[] = {0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, 0x02, 0x61, 0x00};
static const uint8_t m1k_cmd_read_block2[] = {0xFF, 0xB0, 0x00, 0x02, 0x10};
struct aimepcsc_context {
SCARDCONTEXT hContext;
LPSTR mszReaders;
DWORD pcchReaders;
CHAR last_error[256];
};
#define APDU_SEND(card, cmd, expected_res_len) \
do { \
len = sizeof(buf); \
ret = SCardTransmit(*card, SCARD_PCI_T1, cmd, sizeof(cmd), NULL, buf, &len); \
if (ret != SCARD_S_SUCCESS) { \
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardTransmit failed during " #cmd ": %08lX", (ULONG) ret); \
return -1; \
} \
if (len != expected_res_len || buf[expected_res_len - 2] != 0x90 || buf[expected_res_len - 1] != 0x00) { \
snprintf(ctx->last_error, sizeof(ctx->last_error), #cmd " failed; res_len=%lu, res_code=%02x%02x", len, buf[2], buf[3]); \
return 1; \
} \
} while (0)
static int read_felica_aime(struct aimepcsc_context *ctx, LPSCARDHANDLE card, struct aime_data *data) {
uint8_t buf[32];
DWORD len;
LONG ret;
/* read card ID */
APDU_SEND(card, felica_cmd_readidm, 10);
data->card_id_len = 8;
memcpy(data->card_id, buf, 8);
return 0;
}
static int read_m1k_aime(struct aimepcsc_context *ctx, LPSCARDHANDLE card, struct aime_data *data) {
uint8_t buf[32];
DWORD len;
LONG ret;
/* load key onto reader */
APDU_SEND(card, m1k_cmd_loadkey, 2);
/* authenticate block 2 */
APDU_SEND(card, m1k_cmd_auth_block2, 2);
/* read block 2 */
APDU_SEND(card, m1k_cmd_read_block2, 18);
data->card_id_len = 10;
memcpy(data->card_id, buf + 6, 10);
return 0;
}
struct aimepcsc_context* aimepcsc_create(void) {
struct aimepcsc_context* ctx;
ctx = (struct aimepcsc_context*) malloc(sizeof(struct aimepcsc_context));
if (!ctx) {
return NULL;
}
memset(ctx, 0, sizeof(struct aimepcsc_context));
return ctx;
}
void aimepcsc_destroy(struct aimepcsc_context *ctx) {
free(ctx);
}
int aimepcsc_init(struct aimepcsc_context *ctx) {
LONG ret;
ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &ctx->hContext);
if (ret != SCARD_S_SUCCESS) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardEstablishContext failed: %08lX", (ULONG) ret);
return -1;
}
ctx->pcchReaders = SCARD_AUTOALLOCATE;
ret = SCardListReaders(ctx->hContext, NULL, (LPSTR) &ctx->mszReaders, &ctx->pcchReaders);
if (ret != SCARD_S_SUCCESS) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardListReaders failed: %08lX", (ULONG) ret);
goto errout;
}
return 0;
errout:
SCardReleaseContext(ctx->hContext);
return -1;
}
void aimepcsc_shutdown(struct aimepcsc_context *ctx) {
if (ctx->mszReaders != NULL) {
SCardFreeMemory(ctx->hContext, ctx->mszReaders);
}
SCardReleaseContext(ctx->hContext);
}
int aimepcsc_poll(struct aimepcsc_context *ctx, struct aime_data *data) {
SCARDHANDLE hCard;
SCARD_READERSTATE rs;
LONG ret;
LPBYTE pbAttr = NULL;
DWORD cByte = SCARD_AUTOALLOCATE;
int retval;
retval = 1;
memset(&rs, 0, sizeof(SCARD_READERSTATE));
rs.szReader = ctx->mszReaders;
rs.dwCurrentState = SCARD_STATE_UNAWARE;
/* check if a card is present */
ret = SCardGetStatusChange(ctx->hContext, 0, &rs, 1);
if (ret == SCARD_E_TIMEOUT) {
return 1;
}
if (ret != SCARD_S_SUCCESS) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardGetStatusChange failed: %08lX", (ULONG) ret);
return -1;
}
if (rs.dwEventState & SCARD_STATE_EMPTY) {
return 1;
}
if (!(rs.dwEventState & SCARD_STATE_PRESENT)) {
sprintf(ctx->last_error, "unknown dwCurrentState: %08lX", rs.dwCurrentState);
return -1;
}
/* connect to card */
ret = SCardConnect(ctx->hContext, rs.szReader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, NULL);
if (ret != SCARD_S_SUCCESS) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardConnect failed: %08lX", (ULONG) ret);
return -1;
}
/* get ATR string */
ret = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, (LPBYTE) &pbAttr, &cByte);
if (ret != SCARD_S_SUCCESS) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "SCardGetAttrib failed: %08lX", (ULONG) ret);
return -1;
}
if (cByte != 20) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "invalid ATR length: %lu", cByte);
goto out;
}
/* check ATR */
if (memcmp(pbAttr, atr_ios14443_common, sizeof(atr_ios14443_common)) != 0) {
snprintf(ctx->last_error, sizeof(ctx->last_error), "invalid card type.");
goto out;
}
/* check card type */
if (memcmp(pbAttr + sizeof(atr_ios14443_common), cardtype_m1k, sizeof(cardtype_m1k)) == 0) {
data->card_type = Mifare;
ret = read_m1k_aime(ctx, &hCard, data);
if (ret < 0) {
retval = -1;
goto out;
} else if (ret > 0) {
goto out;
}
} else if (memcmp(pbAttr + sizeof(atr_ios14443_common), cardtype_felica, sizeof(cardtype_felica)) == 0) {
data->card_type = FeliCa;
ret = read_felica_aime(ctx, &hCard, data);
if (ret < 0) {
retval = -1;
goto out;
} else if (ret > 0) {
goto out;
}
} else {
snprintf(ctx->last_error, sizeof(ctx->last_error), "invalid card type.");
goto out;
}
retval = 0;
out:
SCardFreeMemory(ctx->hContext, pbAttr);
SCardDisconnect(hCard, SCARD_LEAVE_CARD);
return retval;
}
const char* aimepcsc_error(struct aimepcsc_context *ctx) {
return ctx->last_error;
}
const char* aimepcsc_reader_name(struct aimepcsc_context *ctx) {
return ctx->mszReaders;
}

View File

@ -1,64 +0,0 @@
#pragma once
#include <windows.h>
#include <winioctl.h>
#include <winscard.h>
#include <stdint.h>
/* opaque context */
struct aimepcsc_context;
/* aime card types */
enum AIME_CARDTYPE {
Mifare = 0x01,
FeliCa = 0x02,
};
/* aime card data */
struct aime_data {
/* AIME_CARDTYPE */
uint8_t card_type;
/* Card ID */
uint8_t card_id[32];
/* Card ID length */
uint8_t card_id_len;
};
/* create new context for aimepcsc
* @return context on success, NULL on failure
*/
struct aimepcsc_context* aimepcsc_create(void);
/* destroy context for aimepcsc */
void aimepcsc_destroy(struct aimepcsc_context *ctx);
/* setup reader (first detected reader will be used)
* @param ctx context
* @return 0 on success, -1 on failure
*/
int aimepcsc_init(struct aimepcsc_context *ctx);
/* shutdown reader */
void aimepcsc_shutdown(struct aimepcsc_context *ctx);
/* poll for card
* @param ctx context
* @param data data to be filled
* @return 0 on success, -1 on failure, 1 on no card
*/
int aimepcsc_poll(struct aimepcsc_context *ctx, struct aime_data *data);
/* get last error
* @param ctx context
* @return error string
*/
const char* aimepcsc_error(struct aimepcsc_context *ctx);
/* get reader name
* @param ctx context
* @return reader name
*/
const char* aimepcsc_reader_name(struct aimepcsc_context *ctx);

View File

@ -1,44 +0,0 @@
#include "aimepcsc.h"
#include <stdio.h>
int main(int argc, char** argv) {
struct aimepcsc_context* ctx;
int ret;
ctx = aimepcsc_create();
if (!ctx) {
return -1;
}
ret = aimepcsc_init(ctx);
if (ret != 0) {
fprintf(stderr, "aimepcsc_init failed: %s\n", aimepcsc_error(ctx));
return -1;
}
fprintf(stderr, "connected to reader: %s; waiting for cards...\n", aimepcsc_reader_name(ctx));
while (1) {
struct aime_data data;
ret = aimepcsc_poll(ctx, &data);
if (ret == 0) {
printf("card detected: ");
printf("type=%s, id=", data.card_type == Mifare ? "Mifare (old Aime)" : "FeliCa (AIC-Aime)");
for (int i = 0; i < data.card_id_len; i++) {
printf("%02X", data.card_id[i]);
}
printf("\n");
} else if (ret == -1) {
fprintf(stderr, "aimepcsc_poll failed: %s\n", aimepcsc_error(ctx));
break;
}
Sleep(500);
}
aimepcsc_shutdown(ctx);
aimepcsc_destroy(ctx);
return 0;
}

View File

@ -1,23 +0,0 @@
winscard_lib = cc.find_library('winscard')
aimepcsc_lib = static_library(
'aimepcsc',
name_prefix : '',
include_directories: inc,
implicit_include_directories : false,
dependencies: [winscard_lib],
sources : [
'aimepcsc.c',
],
)
aimereader = executable(
'aimereader',
name_prefix : '',
include_directories : [inc],
implicit_include_directories : false,
link_with: [aimepcsc_lib],
sources : [
'aimereader.c'
]
)

13
build64.bat Normal file
View File

@ -0,0 +1,13 @@
@echo off
cd /d %~dp0
REM Edit this with the right path to vcvarsall.bat.
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
call "C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" x64
meson setup build64 --buildtype=release
meson configure build64
ninja -C build64
mkdir bin
copy build64\src\aimeio.dll bin\aimeio.dll
copy build64\src\aimetest.exe bin\aimetest.exe

View File

@ -1,10 +0,0 @@
[binaries]
c = 'i686-w64-mingw32-gcc'
ar = 'i686-w64-mingw32-ar'
strip = 'i686-w64-mingw32-strip'
[host_machine]
system = 'windows'
cpu_family = 'x86'
cpu = 'i686'
endian = 'little'

View File

@ -1,10 +0,0 @@
[binaries]
c = 'x86_64-w64-mingw32-gcc'
ar = 'x86_64-w64-mingw32-ar'
strip = 'x86_64-w64-mingw32-strip'
[host_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

View File

@ -1,44 +1,9 @@
project( project(
'aimeio-pcsc', 'aimeio_scard',
'c', 'c',
version: '0.0.1', version : '0.0.2'
default_options: [
'warning_level=3',
],
) )
add_project_arguments(
'-DCOBJMACROS',
'-DDIRECTINPUT_VERSION=0x0800',
'-DWIN32_LEAN_AND_MEAN',
'-D_WIN32_WINNT=_WIN32_WINNT_WIN7',
'-DMINGW_HAS_SECURE_API=1',
'-Wno-unused',
language: 'c',
)
cc = meson.get_compiler('c')
if cc.get_id() != 'msvc'
add_project_arguments(
'-ffunction-sections',
'-fdata-sections',
'-flto', # Enable Link-Time Optimization
language: 'c',
)
add_project_link_arguments(
'-Wl,--enable-stdcall-fixup',
'-Wl,--exclude-all-symbols',
'-Wl,--gc-sections',
'-static-libgcc',
'-flto', # Enable Link-Time Optimization
'-Wl,-s', # Strip debug symbols
language: 'c',
)
endif
inc = include_directories('.') inc = include_directories('.')
subdir('aimepcsc') subdir('src/')
subdir('aimeio')

View File

@ -1,28 +1,67 @@
aimeio-pcsc # AimeIO CardReader
---
PC/SC-based Aime card reader for `segatools`. `aimeio-pcsc` allows you to use PC/SC compliant smart card readers as your Aime card reader in `segatools`. `aimeio-pcsc` only properly supports the old Mifare Classic 1K-based Aime cards. This allows you to a smartcard reader (specifically the acr122u) with segatools
If you scan a newer AIC-based Aime, its FeliCa IDm will be provided to the game. The game will not see the correct "access code," but the IDm should be unique to each card so that particular card can still track your plays. # Acknowledgments
Tested on SONY's PaSoRi RC-S300. Other readers should, in theory, also work. This is a plugin destined to be used with [Dniel97](https://gitea.tendokyu.moe/Dniel97)'s [segatools fork](https://gitea.tendokyu.moe/Dniel97/segatools) (but should work on others too).
### Usage SmartCard implementation taken from [spicetools](https://github.com/spicetools/spicetools)
To test if your card reader is supported, run `aimereader.exe` and try read your Aime card. Initial project made by [nat](https://gitea.tendokyu.moe/nat/aimeio-pcsc)
All the logic for reading cards from a file has been taken from [CrazyRedMachine](https://github.com/CrazyRedMachine)'s [RedBoard](https://github.com/CrazyRedMachine/RedBoard) aimeio.
# Usage
To use it with a game, copy `aimeio.dll` to your `segatools` folder and add the following to your `segatools.ini`: To use it with a game, copy `aimeio.dll` to your `segatools` folder and add the following to your `segatools.ini`:
```ini ```ini
[aimeio] [aimeio]
path=aimeio.dll path=aimeio.dll
scan=0x0D ;Sets the key which will be used to insert a card in game. The default is 'Return'
;Everything below this line is optional.
;aimePath= ;Manually specify an aime.txt file
;felicaPath= ;Manually specify a felica.txt file
;felicaGen=0 ;Generate a new random card if it's missing from the file
;debug=0 ;Display function calls
``` ```
### Build ## Scanning cards
On Linux: If you scan a newer AIC-based Aime, its FeliCa IDm will be provided to the game. The game will not see the correct "access code," but the IDm should be unique to each card so that particular card can still track your plays.
```sh As for Mifare cards, their access code won't be 1:1 with a real reader (i still need to fix this). You can still use them and they will work though !
meson setup --cross cross-mingw-64.txt b64
ninja -C b64 ## Inserting cards
By pressing the key you have set in segatools.ini and holding it for a few moments, you will insert a card set in either aime.txt or felica.txt
You can have multiple cards in those files!
By holding a number from 0 to 9 on your keypad, and then holding enter, you can choose what card to insert.
For example...
```text
aime.txt
---
1a7f3e925cb866d45a3b
a5d04d668bc529e35aaa
``` ```
By only pressing the insert key, you will insert card0, "1a7f3e925cb866d45a3b".
If you hold 1 on your numpad and press the insert key, card1, "a5d04d668bc529e35aaa" will be inserted !
This works for up to 10 cards.
# Build
To build this, you'll need two things :
- [Meson 1.1.0](https://mesonbuild.com)
- [Build Tools pour Visual Studio 2022](https://visualstudio.microsoft.com/fr/downloads/)
Once you've edited your build64.bat file to point to your local installation of the VS2022 build tools, run build64.bat and the output will be located in `bin/aime.dll`.

331
src/aimeio.c Normal file
View File

@ -0,0 +1,331 @@
#include "aimeio.h"
#include "scard/scard.h"
// Reader Thread
static HANDLE READER_POLL_THREAD;
static bool READER_POLL_STOP_FLAG;
static struct aime_io_config aime_io_cfg;
static struct card_data card_data;
#pragma region CONFIG
static void aime_io_config_read(struct aime_io_config *cfg, const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->debug = GetPrivateProfileIntW(
L"aimeio",
L"debug",
0,
filename);
GetPrivateProfileStringW(
L"aimeio",
L"aimePath",
L"DEVICE\\aime.txt",
cfg->aime_path,
_countof(cfg->aime_path),
filename);
GetPrivateProfileStringW(
L"aimeio",
L"felicaPath",
L"DEVICE\\felica.txt",
cfg->felica_path,
_countof(cfg->felica_path),
filename);
GetPrivateProfileStringW(
L"aimeio",
L"readerName",
L"",
cfg->reader_name,
_countof(cfg->reader_name),
filename);
cfg->reader_optional = GetPrivateProfileIntW(
L"aimeio",
L"readerOptional",
0,
filename);
cfg->vk_scan = GetPrivateProfileIntW(
L"aimeio",
L"scan",
VK_RETURN,
filename);
if (aime_io_cfg.debug)
printf("DEBUG: aime_io_config_read(filename : %ls). \r\n", filename);
}
static HRESULT aime_io_read_id_file(const wchar_t *path, uint8_t *bytes, size_t nbytes, int LineToRead)
{
HRESULT hr;
FILE *f;
size_t i;
int byte;
long offset;
int currentLine = 0;
f = _wfopen(path, L"r");
if (f == NULL) // If the file doesn't exist, we bail.
return S_FALSE;
memset(bytes, 0, nbytes);
// Calculate the offset to the start of the desired line
offset = 0;
while (currentLine < LineToRead)
{
int c = fgetc(f);
if (c == EOF)
{
// If the end of the file is reached before the desired line, print an error
printf("aime_io_read_id_file: %S: Error: Line %d does not exist\n", path, LineToRead);
hr = E_FAIL;
goto end;
}
if (c == '\n')
currentLine++;
offset++;
}
// Seek to the calculated offset
fseek(f, offset, SEEK_SET);
// Read the desired line and extract hexadecimal values
for (i = 0; i < nbytes; i++)
{
int r = fscanf(f, "%02x", &byte);
if (r != 1)
{
printf("aime_io_read_id_file: %S: Error parsing line %d\n", path, LineToRead);
hr = E_FAIL;
goto end;
}
bytes[i] = byte;
}
// Check if the line is not nbytes long
if (fgetc(f) != '\n' && !feof(f))
{
printf("aime_io_read_id_file: %S: Error: Line %d is not %zu bytes long\n", path, LineToRead, nbytes);
hr = E_FAIL;
goto end;
}
hr = S_OK;
end:
if (f != NULL)
{
fclose(f);
}
return hr;
}
#pragma endregion
#pragma region READER SPECIFIC
static unsigned int __stdcall reader_poll_thread_proc(void *ctx)
{
if (aime_io_cfg.debug)
printf("DEBUG: reader_poll_thread_proc(). \r\n");
while (!READER_POLL_STOP_FLAG)
{
if (card_data.card_type != 0) // Halting polling once a card is found, waiting for the game to read it's value.
scard_poll(&card_data);
}
return 0;
}
#pragma endregion
#pragma region AIME
uint16_t aime_io_get_api_version(void)
{
return 0x0100;
}
HRESULT aime_io_init(void)
{
// At init we want to open a console...
int ret = AllocConsole();
FILE *fp;
// someone might already allocated a console - seeing this on fufubot's segatools
if (ret != 0)
freopen_s(&fp, "CONOUT$", "w", stdout); // only when we allocate a console, we need to redirect stdout
memset(&card_data, 0, sizeof(card_data)); // Init card_data structure
// We then read the segatools config file to get settings.
aime_io_config_read(&aime_io_cfg, L".\\segatools.ini");
// Find and initialize reader(s)
printf("aime_io_init: Initializing SmartCard\n");
if (!scard_init(aime_io_cfg))
{
// If we couldn't init reader, error out.
printf("aime_io_init: Couldn't init SmartCard\n");
if (!aime_io_cfg.reader_optional)
return E_FAIL;
// If however the readerOptional flag is set to 1 in segatools.ini, continue with keyboard only.
printf("aime_io_init: Reader is optional, using keyboard only !\n");
return S_OK;
}
printf("aime_io_init: Starting reader thread.\n");
// Start reader thread
READER_POLL_STOP_FLAG = false;
READER_POLL_THREAD = (HANDLE)_beginthreadex(
NULL,
0,
reader_poll_thread_proc,
NULL,
0,
NULL);
return S_OK;
}
HRESULT aime_io_nfc_poll(uint8_t unit_no)
{
if (aime_io_cfg.debug)
printf("\n\nDEBUG: aime_io_nfc_poll(unit_no %d). \r\n", unit_no);
if (unit_no != 0)
return S_OK;
bool sense;
HRESULT hr;
// Don't do anything more if the scan key is not held
sense = GetAsyncKeyState(aime_io_cfg.vk_scan) & 0x8000;
if (!sense)
{
return S_OK;
}
// Set which card we want to read (we will read the x'th line in the card's file, x being determined by which key is pressed on the keypad).
int card = 0;
for (int key = VK_NUMPAD0; key <= VK_NUMPAD9; key++)
{
short keyState = GetAsyncKeyState(key);
if (keyState & 0x8000) // Check if the most significant bit is set, indicating that the key is down
{
card = key - VK_NUMPAD0;
continue;
}
}
printf("aime_io_nfc_poll: Attempting to read card %d from file. \r\n", card);
// Try AiMe IC
hr = aime_io_read_id_file(
aime_io_cfg.aime_path,
card_data.card_id,
10,
card);
if (SUCCEEDED(hr) && hr != S_FALSE)
{
card_data.card_type = Mifare;
return S_OK;
}
// Try FeliCa IC
hr = aime_io_read_id_file(
aime_io_cfg.felica_path,
card_data.card_id,
8,
card);
if (SUCCEEDED(hr) && hr != S_FALSE)
{
card_data.card_type = FeliCa;
return S_OK;
}
return S_OK;
}
HRESULT aime_io_nfc_get_aime_id(uint8_t unit_no, uint8_t *luid, size_t luid_size)
{
if (aime_io_cfg.debug)
printf("DEBUG: aime_io_nfc_get_aime_id(unit_no : %d). \r\n", unit_no);
assert(luid != NULL);
assert(luid_size == 10);
if (unit_no != 0)
return S_FALSE;
if (card_data.card_type == Mifare)
{
memcpy(luid, card_data.card_id, luid_size);
printf("aime_io_nfc_get_aime_id: Read Aime card from file with uid %02X%02X %02X%02X %02X%02X %02X%02X %02X%02X\r\n", card_data.card_id[0], card_data.card_id[1], card_data.card_id[2], card_data.card_id[3], card_data.card_id[4], card_data.card_id[5], card_data.card_id[6], card_data.card_id[7], card_data.card_id[8], card_data.card_id[9]);
memset(&card_data, 0, sizeof(card_data)); // Reset card_data structure
return S_OK;
}
return S_FALSE;
}
HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm)
{
if (aime_io_cfg.debug)
printf("DEBUG: aime_io_nfc_get_felica_id(unit_no : %d). \r\n", unit_no);
uint64_t val;
size_t i;
assert(IDm != NULL);
if (unit_no != 0)
return S_FALSE;
if (card_data.card_type == FeliCa)
{
val = 0;
for (i = 0; i < 8; i++)
val = (val << 8) | card_data.card_id[i];
*IDm = val;
printf("aime_io_nfc_get_felica_id: Read FeliCa card from file with uid %02X%02X %02X%02X %02X%02X %02X%02X\r\n", card_data.card_id[0], card_data.card_id[1], card_data.card_id[2], card_data.card_id[3], card_data.card_id[4], card_data.card_id[5], card_data.card_id[6], card_data.card_id[7]);
memset(&card_data, 0, sizeof(card_data)); // Reset card_data structure
return S_OK;
}
// if (HasCard)
// {
// polling = false;
// HasCard = false;
// uint64_t val;
// for (int i = 0; i < 8; i++)
// {
// val = (val << 8) | UID[i];
// }
// *IDm = val;
// printf("aime_io_nfc_get_felica_id: FeliCa card has been scanned ! %llx\r\n", val);
// return S_OK;
// }
return S_FALSE;
}
void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b)
{
if (aime_io_cfg.debug)
printf("DEBUG: aime_io_led_set_color(unit_no : %d, r : %d, g : %d, b : %d). \r\n", unit_no, r, g, b);
}
#pragma endregion

View File

@ -4,6 +4,6 @@ EXPORTS
aime_io_get_api_version aime_io_get_api_version
aime_io_init aime_io_init
aime_io_led_set_color aime_io_led_set_color
aime_io_nfc_poll
aime_io_nfc_get_aime_id aime_io_nfc_get_aime_id
aime_io_nfc_get_felica_id aime_io_nfc_get_felica_id
aime_io_nfc_poll

View File

@ -1,9 +1,23 @@
#pragma once #pragma once
#include <windows.h> #include <windows.h>
#include <assert.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct aime_io_config
{
bool debug;
wchar_t aime_path[MAX_PATH];
wchar_t felica_path[MAX_PATH];
wchar_t reader_name[MAX_PATH];
bool reader_optional;
uint8_t vk_scan;
};
/* /*
Get the version of the Aime IO API that this DLL supports. This function Get the version of the Aime IO API that this DLL supports. This function
@ -48,10 +62,7 @@ HRESULT aime_io_nfc_poll(uint8_t unit_no);
Minimum API version: 0x0100 Minimum API version: 0x0100
*/ */
HRESULT aime_io_nfc_get_aime_id( HRESULT aime_io_nfc_get_aime_id(uint8_t unit_no, uint8_t *luid, size_t luid_size);
uint8_t unit_no,
uint8_t *luid,
size_t luid_size);
/* /*
Attempt to read out a FeliCa card ID ("IDm"). The following are examples Attempt to read out a FeliCa card ID ("IDm"). The following are examples

74
src/aimetest.c Normal file
View File

@ -0,0 +1,74 @@
#include <windows.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "aimeio.h"
int main()
{
printf("AIMETEST\r\n---------\r\n");
// printf("api version = %04x\r\n", chuni_io_get_api_version()); /* not compatible with older dlls */
printf("aime_io_init() : \n");
switch (aime_io_init())
{
case E_FAIL:
printf("AIMETEST: aime_io_init() returned E_FAIL. Reader is either missing or incompatible !\r\n");
return E_FAIL;
case S_OK:
printf("AIMETEST: aime_io_init() returned S_OK !\r\n");
break;
default:
printf("AIMETEST: aime_io_init() returned an unknown state !\r\n");
return E_FAIL;
}
// printf("aime_io_led_set_color(red) : ");
// aime_io_led_set_color(0, 255, 0, 0);
// printf("OK\r\n");
// Sleep(2000);
// printf("aime_io_led_set_color(green) : ");
// aime_io_led_set_color(0, 0, 255, 0);
// printf("OK\r\n");
// Sleep(2000);
// printf("aime_io_led_set_color(blue) : ");
// aime_io_led_set_color(0, 0, 0, 255);
// printf("OK\r\n");
// Sleep(2000);
// aime_io_led_set_color(0, 0, 0, 0);
printf("AIMETEST: Running input loop. Press Ctrl+C to exit.\r\n");
uint8_t luid[10] = {0};
uint64_t IDm = 0;
while (1)
{
if (aime_io_nfc_poll(0) == S_OK)
{
if (aime_io_nfc_get_felica_id(0, &IDm) == S_OK)
{
// aime_io_led_set_color(0, 0, 255, 0);
printf("AIMETEST: Found FeliCa card with uid %llx\r\n\n", IDm);
}
if (aime_io_nfc_get_aime_id(0, luid, 10) == S_OK)
{
// aime_io_led_set_color(0, 0, 0, 255);
printf("AIMETEST: Found old card with uid ");
for (int i = 0; i < 10; i++)
{
printf("%02x ", luid[i]);
}
printf("\r\n\n");
}
Sleep(500);
// printf("poll ok but no card?!\r\n");
}
// Sleep(300);
// aime_io_led_set_color(0, 0, 0, 0);
}
return 0;
}

26
src/meson.build Normal file
View File

@ -0,0 +1,26 @@
compiler = meson.get_compiler('c')
hid_lib = compiler.find_library('hid')
winscard_lib = compiler.find_library('winscard')
setupapi_lib = compiler.find_library('setupapi')
deps = [
hid_lib,
winscard_lib,
setupapi_lib,
]
aimeio_dll = shared_library(
'aimeio',
implicit_include_directories : false,
vs_module_defs : 'aimeio.def',
sources: [
'aimeio.c',
'scard/scard.c',
],
dependencies: deps,
include_directories: [
inc
]
)
executable('aimetest', 'aimetest.c', link_with : aimeio_dll)

272
src/scard/scard.c Normal file
View File

@ -0,0 +1,272 @@
#include "scard.h"
#define MAX_APDU_SIZE 255
int readCooldown = 500;
// set to detect all cards, reduce polling rate to 500ms.
// based off acr122u reader, see page 26 in api document.
// https://www.acs.com.hk/en/download-manual/419/API-ACR122U-2.04.pdf
#define PARAM_POLLRATE 0xDFu
static const BYTE PARAM_SET_PICC[5] = {0xFFu, 0x00u, 0x51u, PARAM_POLLRATE, 0x00u};
static const BYTE COMMAND_GET_UID[5] = {0xFFu, 0xCAu, 0x00u, 0x00u, 0x00u};
// return bytes from device
#define PICC_SUCCESS 0x90u
#define PICC_ERROR 0x63u
enum scard_atr_protocol
{
SCARD_ATR_PROTOCOL_ISO14443_PART3 = 0x03,
SCARD_ATR_PROTOCOL_FELICA_212K = 0x11,
};
// winscard_config_t WINSCARD_CONFIG;
static SCARDCONTEXT hContext = 0;
static SCARD_READERSTATE reader_state;
static LONG lRet = 0;
bool scard_init(struct aime_io_config config)
{
if ((lRet = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hContext)) != SCARD_S_SUCCESS)
{
// log_warning("scard", "failed to establish SCard context: {}", bin2hex(&lRet, sizeof(LONG)));
return FALSE;
}
// get list of readers
LPTSTR reader_list = NULL;
auto pcchReaders = SCARD_AUTOALLOCATE;
lRet = SCardListReaders(hContext, NULL, (LPTSTR)&reader_list, &pcchReaders);
switch (lRet)
{
case SCARD_E_NO_READERS_AVAILABLE:
printf("scard_init: No readers available\n");
return FALSE;
case SCARD_S_SUCCESS:
LPTSTR reader_name = NULL;
int readerNameLen = 0;
// Iterate through the multi-string to get individual reader names
printf("scard_init: listing all readers : ");
while (*reader_list != '\0')
{
printf("%s, ", reader_list);
reader_list += strlen(reader_list) + 1;
}
printf("\r\n", reader_list);
// if the readerName array is populated, replace the first reader in the list
char ReaderCharArray[sizeof(config.reader_name)];
wcstombs(ReaderCharArray, config.reader_name, sizeof(ReaderCharArray));
if (strcmp(ReaderCharArray, "") != 0)
{
size_t newLen = strlen(ReaderCharArray) + 1;
LPSTR newMszReaders = (LPSTR)malloc(newLen);
if (newMszReaders != NULL)
{
// Copy the new selected reader
strcpy(newMszReaders, ReaderCharArray);
// Update the original pointer to the new modified list
reader_list = newMszReaders;
}
printf("scard_init: Forced using reader : %hs\n", ReaderCharArray);
}
// Connect to reader and send PICC operating params command
SCARDHANDLE hCard;
DWORD dwActiveProtocol;
lRet = SCardConnect(hContext, reader_list, SCARD_SHARE_DIRECT, 0, &hCard, &dwActiveProtocol);
if (lRet != SCARD_S_SUCCESS)
{
printf("scard_init: Error connecting to the reader: 0x%08X\n", lRet);
return FALSE;
}
printf("scard_init: Connected to reader: %s, sending PICC params\n", reader_list);
// set the reader params
DWORD cbRecv = MAX_APDU_SIZE;
BYTE pbRecv[MAX_APDU_SIZE];
lRet = SCardControl(hCard, SCARD_CTL_CODE(3500), PARAM_SET_PICC, sizeof(PARAM_SET_PICC), pbRecv, cbRecv, &cbRecv);
Sleep(100);
if (lRet != SCARD_S_SUCCESS)
{
printf("scard_init: Error setting PICC params : 0x%08X\n", lRet);
return FALSE;
}
if (cbRecv > 2 && pbRecv[0] != PICC_SUCCESS && pbRecv[1] != PARAM_POLLRATE)
{
printf("scard_init: PICC params not valid 0x%02X != 0x%02X\n", pbRecv[1], PARAM_POLLRATE);
return FALSE;
}
// Disconnect from reader
if ((lRet = SCardDisconnect(hCard, SCARD_LEAVE_CARD)) != SCARD_S_SUCCESS)
printf("scard_init: Failed SCardDisconnect : 0x%08X\n", lRet);
// Extract the relevant names from the multi-string.
readerNameLen = lstrlen(reader_list);
reader_name = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(TCHAR) * (readerNameLen + 1));
memcpy(reader_name, &reader_list, (size_t)(readerNameLen + 1));
if (reader_name)
printf("scard_init: Using reader : %s\n", reader_name);
memset(&reader_state, 0, sizeof(SCARD_READERSTATE));
reader_state.szReader = reader_name;
return TRUE;
default:
printf("scard_init: Failed SCardListReaders: 0x%08X\n", lRet);
return FALSE;
}
}
void scard_poll(struct card_data *card_data)
{
lRet = SCardGetStatusChange(hContext, readCooldown, &reader_state, 1);
if (lRet == SCARD_E_TIMEOUT)
{
return;
}
else if (lRet != SCARD_S_SUCCESS)
{
printf("scard_poll: Failed SCardGetStatusChange: 0x%08X\n", lRet);
return;
}
if (!(reader_state.dwEventState & SCARD_STATE_CHANGED))
return;
DWORD newState = reader_state.dwEventState ^ SCARD_STATE_CHANGED;
bool wasCardPresent = (reader_state.dwCurrentState & SCARD_STATE_PRESENT) > 0;
if (newState & SCARD_STATE_UNAVAILABLE)
{
printf("scard_poll: New card state: unavailable\n");
Sleep(readCooldown);
}
else if (newState & SCARD_STATE_EMPTY)
{
printf("scard_poll: New card state: empty\n");
// scard_clear(unit_no);
}
else if (newState & SCARD_STATE_PRESENT && !wasCardPresent)
{
printf("scard_poll: New card state: present\n");
scard_update(card_data, hContext, reader_state.szReader);
}
reader_state.dwCurrentState = reader_state.dwEventState;
return;
}
void scard_update(struct card_data *card_data, SCARDCONTEXT _hContext, LPCTSTR _readerName)
{
printf("scard_update: Update on reader : %s\n", reader_state.szReader);
// Connect to the smart card.
SCARDHANDLE hCard;
DWORD dwActiveProtocol;
for (int retry = 0; retry < 100; retry++) // retry times has to be increased since poll rate is set to 500ms
{
if ((lRet = SCardConnect(_hContext, _readerName, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol)) == SCARD_S_SUCCESS)
break;
Sleep(20);
}
if (lRet != SCARD_S_SUCCESS)
{
printf("scard_update: Error connecting to the card: 0x%08X\n", lRet);
return;
}
// set the reader params
LPCSCARD_IO_REQUEST pci = dwActiveProtocol == SCARD_PROTOCOL_T1 ? SCARD_PCI_T1 : SCARD_PCI_T0;
DWORD cbRecv = MAX_APDU_SIZE;
BYTE pbRecv[MAX_APDU_SIZE];
// Read ATR to determine card type.
TCHAR szReader[200];
DWORD cchReader = 200;
BYTE atr[32];
DWORD cByteAtr = 32;
lRet = SCardStatus(hCard, szReader, &cchReader, NULL, NULL, atr, &cByteAtr);
if (lRet != SCARD_S_SUCCESS)
{
printf("scard_update: Error getting card status: 0x%08X\n", lRet);
return;
}
// Only care about 20-byte ATRs returned by arcade-type smart cards
if (cByteAtr != 20)
{
printf("scard_update: Ignoring card with len(%zu) = %02x (%08X)\n", sizeof(cByteAtr), (unsigned int)atr, cByteAtr);
return;
}
printf("scard_update: atr Return: len(%zu) = %02x (%08X)\n", sizeof(cByteAtr), (unsigned int)atr, cByteAtr);
// Figure out if we should reverse the UID returned by the card based on the ATR protocol
BYTE cardProtocol = atr[12];
if (cardProtocol == SCARD_ATR_PROTOCOL_ISO14443_PART3)
{
printf("scard_update: Card protocol: ISO14443_PART3\n");
card_data->card_type = Mifare;
}
else if (cardProtocol == SCARD_ATR_PROTOCOL_FELICA_212K) // Handling FeliCa
{
printf("scard_update: Card protocol: FELICA_212K\n");
card_data->card_type = FeliCa;
// Read mID
cbRecv = MAX_APDU_SIZE;
if ((lRet = SCardTransmit(hCard, pci, COMMAND_GET_UID, sizeof(COMMAND_GET_UID), NULL, pbRecv, &cbRecv)) != SCARD_S_SUCCESS)
{
printf("scard_update: Error querying card UID: 0x%08X\n", lRet);
return;
}
if (cbRecv > 1 && pbRecv[0] == PICC_ERROR)
{
printf("scard_update: UID query failed\n");
return;
}
if ((lRet = SCardDisconnect(hCard, SCARD_LEAVE_CARD)) != SCARD_S_SUCCESS)
printf("scard_update: Failed SCardDisconnect: 0x%08X\n", lRet);
if (cbRecv < 8)
{
printf("scard_update: Padding card uid to 8 bytes\n");
memset(&pbRecv[cbRecv], 0, 8 - cbRecv);
}
else if (cbRecv > 8)
printf("scard_update: taking first 8 bytes of len(uid) = %02X\n", cbRecv);
card_data->card_id_len = 8;
memcpy(card_data->card_id, pbRecv, 8);
}
else
{
printf("scard_update: Unknown NFC Protocol: 0x%02X\n", cardProtocol);
return;
}
// Copy UID to struct, reversing if necessary
// card_info_t card_info;
// if (shouldReverseUid)
// for (DWORD i = 0; i < 8; i++)
// card_info.uid[i] = pbRecv[7 - i];
// else
// memcpy(card_info.uid, pbRecv, 8);
// for (int i = 0; i < 8; ++i)
// buf[i] = card_info.uid[i];
}

58
src/scard/scard.h Normal file
View File

@ -0,0 +1,58 @@
/**
* MIT-License
* Copyright (c) 2018 by nolm <nolan@nolm.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Modified version.
*/
#pragma once
#include <winscard.h>
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <src/aimeio.h>
/* card types */
enum AIME_CARDTYPE
{
Mifare = 0x01,
FeliCa = 0x02,
};
// Structure containing card_type, card_id and card_id_len
struct card_data
{
/* CARDTYPE */
uint8_t card_type;
/* Card ID */
uint8_t card_id[32];
/* Card ID length */
uint8_t card_id_len;
};
bool scard_init(struct aime_io_config config);
void scard_poll(struct card_data *card_data);
void scard_update(struct card_data *card_data, SCARDCONTEXT _hContext, LPCTSTR _readerName);