aciomgr: cleanup and address comments

This commit is contained in:
Will Xyen
2021-03-16 22:06:21 +00:00
committed by Will Xyen
parent f7dc5f1fb4
commit c3ed8990aa
6 changed files with 67 additions and 50 deletions
+1
View File
@@ -5,4 +5,5 @@ libs_aciomgr := \
util \ util \
src_aciomgr := \ src_aciomgr := \
dllmain.c \
manager.c \ manager.c \
+16
View File
@@ -0,0 +1,16 @@
#include <windows.h>
#include "manager.h"
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
{
if (reason == DLL_PROCESS_ATTACH) {
_aciomgr_init();
}
if (reason == DLL_PROCESS_DETACH) {
_aciomgr_fini();
}
return TRUE;
}
+25 -35
View File
@@ -14,7 +14,7 @@
struct aciomgr_port_dispatcher { struct aciomgr_port_dispatcher {
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
size_t references; atomic_size_t references;
struct aciodrv_device_ctx *device; struct aciodrv_device_ctx *device;
char path[MAX_PORT_PATH_LENGTH]; char path[MAX_PORT_PATH_LENGTH];
int baud; int baud;
@@ -23,10 +23,8 @@ struct aciomgr_port_dispatcher {
static void _aciomgr_setup_port_dispatcher( static void _aciomgr_setup_port_dispatcher(
struct aciomgr_port_dispatcher *dispatcher, const char *path, int baud); struct aciomgr_port_dispatcher *dispatcher, const char *path, int baud);
static void _aciomgr_destroy_port_dispatcher( static void
struct aciomgr_port_dispatcher *dispatcher); _aciomgr_destroy_port_dispatcher(struct aciomgr_port_dispatcher *dispatcher);
static void _aciomgr_init();
static void _aciomgr_fini();
// DLL-globals // DLL-globals
static atomic_bool running; static atomic_bool running;
@@ -52,17 +50,17 @@ static void _aciomgr_setup_port_dispatcher(
dispatcher->references = 0; dispatcher->references = 0;
} }
static void _aciomgr_destroy_port_dispatcher( static void
struct aciomgr_port_dispatcher *dispatcher) _aciomgr_destroy_port_dispatcher(struct aciomgr_port_dispatcher *dispatcher)
{ {
aciodrv_device_close(dispatcher->device); aciodrv_device_close(dispatcher->device);
DeleteCriticalSection(&dispatcher->cs); DeleteCriticalSection(&dispatcher->cs);
} }
static void _aciomgr_init() void _aciomgr_init()
{ {
if (running) { if (running) {
// warn log_warning("_aciomgr_init called when already running");
_aciomgr_fini(); _aciomgr_fini();
} }
InitializeCriticalSection(&mgr_cs); InitializeCriticalSection(&mgr_cs);
@@ -71,11 +69,12 @@ static void _aciomgr_init()
running = true; running = true;
} }
static void _aciomgr_fini() void _aciomgr_fini()
{ {
if (!running) { if (!running) {
// warn log_warning("_aciomgr_fini called when not running");
} }
running = false; running = false;
DeleteCriticalSection(&mgr_cs); DeleteCriticalSection(&mgr_cs);
array_fini(&active_ports); array_fini(&active_ports);
@@ -93,17 +92,17 @@ void aciomgr_set_loggers(
struct aciomgr_port_dispatcher *aciomgr_port_init(const char *path, int baud) struct aciomgr_port_dispatcher *aciomgr_port_init(const char *path, int baud)
{ {
if (!running) { if (!running) {
// warn log_warning("aciomgr_port_init: called when not running");
return NULL; return NULL;
} }
if (strlen(path) >= MAX_PORT_PATH_LENGTH) { if (strlen(path) >= MAX_PORT_PATH_LENGTH) {
// warn log_warning("aciomgr_port_init: path too long");
return NULL; return NULL;
} }
struct aciomgr_port_dispatcher* entry; struct aciomgr_port_dispatcher *entry;
EnterCriticalSection(&mgr_cs); EnterCriticalSection(&mgr_cs);
@@ -143,7 +142,7 @@ done:
void aciomgr_port_fini(struct aciomgr_port_dispatcher *dispatcher) void aciomgr_port_fini(struct aciomgr_port_dispatcher *dispatcher)
{ {
if (!running) { if (!running) {
// warn log_warning("aciomgr_port_fini: called when not running");
return; return;
} }
@@ -157,7 +156,8 @@ void aciomgr_port_fini(struct aciomgr_port_dispatcher *dispatcher)
} }
// this function don't require the lock // this function don't require the lock
uint8_t aciomgr_get_node_count(struct aciomgr_port_dispatcher *dispatcher) { uint8_t aciomgr_get_node_count(struct aciomgr_port_dispatcher *dispatcher)
{
return aciodrv_device_get_node_count(dispatcher->device); return aciodrv_device_get_node_count(dispatcher->device);
} }
@@ -165,9 +165,10 @@ uint8_t aciomgr_get_node_count(struct aciomgr_port_dispatcher *dispatcher) {
bool aciomgr_get_node_product_ident( bool aciomgr_get_node_product_ident(
struct aciomgr_port_dispatcher *dispatcher, struct aciomgr_port_dispatcher *dispatcher,
uint8_t node_id, uint8_t node_id,
char product[4]) char product[ACIOMGR_NODE_PRODUCT_CODE_LEN])
{ {
return aciodrv_device_get_node_product_ident(dispatcher->device, node_id, product); return aciodrv_device_get_node_product_ident(
dispatcher->device, node_id, product);
} }
bool aciomgr_port_submit_packet( bool aciomgr_port_submit_packet(
@@ -178,7 +179,8 @@ bool aciomgr_port_submit_packet(
// CS's although lightweight, may still be a burden, short circuit // CS's although lightweight, may still be a burden, short circuit
if (dispatcher->references > 1) { if (dispatcher->references > 1) {
EnterCriticalSection(&dispatcher->cs); EnterCriticalSection(&dispatcher->cs);
bool response = aciodrv_send_and_recv(dispatcher->device, msg, max_resp_size); bool response =
aciodrv_send_and_recv(dispatcher->device, msg, max_resp_size);
LeaveCriticalSection(&dispatcher->cs); LeaveCriticalSection(&dispatcher->cs);
return response; return response;
} }
@@ -186,8 +188,8 @@ bool aciomgr_port_submit_packet(
return aciodrv_send_and_recv(dispatcher->device, msg, max_resp_size); return aciodrv_send_and_recv(dispatcher->device, msg, max_resp_size);
} }
struct aciodrv_device_ctx *aciomgr_port_checkout( struct aciodrv_device_ctx *
struct aciomgr_port_dispatcher *dispatcher) aciomgr_port_checkout(struct aciomgr_port_dispatcher *dispatcher)
{ {
if (dispatcher->references > 1) { if (dispatcher->references > 1) {
EnterCriticalSection(&dispatcher->cs); EnterCriticalSection(&dispatcher->cs);
@@ -196,21 +198,9 @@ struct aciodrv_device_ctx *aciomgr_port_checkout(
return dispatcher->device; return dispatcher->device;
} }
void aciomgr_port_checkin(struct aciomgr_port_dispatcher *dispatcher) { void aciomgr_port_checkin(struct aciomgr_port_dispatcher *dispatcher)
{
if (dispatcher->references > 1) { if (dispatcher->references > 1) {
LeaveCriticalSection(&dispatcher->cs); LeaveCriticalSection(&dispatcher->cs);
} }
} }
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
{
if (reason == DLL_PROCESS_ATTACH) {
_aciomgr_init();
}
if (reason == DLL_PROCESS_DETACH) {
_aciomgr_fini();
}
return TRUE;
}
+12 -3
View File
@@ -7,9 +7,15 @@
#include "bemanitools/glue.h" #include "bemanitools/glue.h"
#define ACIOMGR_NODE_PRODUCT_CODE_LEN 4
struct aciodrv_device_ctx; struct aciodrv_device_ctx;
struct aciomgr_port_dispatcher; struct aciomgr_port_dispatcher;
// internal
void _aciomgr_init();
void _aciomgr_fini();
/** /**
* The first function that will be called on your DLL. You will be supplied * The first function that will be called on your DLL. You will be supplied
* with four function pointers that may be used to log messages to the game's * with four function pointers that may be used to log messages to the game's
@@ -48,7 +54,10 @@ uint8_t aciomgr_get_node_count(struct aciomgr_port_dispatcher *dispatcher);
* @return True on success, false on error. If True the variable product * @return True on success, false on error. If True the variable product
* contains the identifier of the queried node. * contains the identifier of the queried node.
*/ */
bool aciomgr_get_node_product_ident(struct aciomgr_port_dispatcher *dispatcher, uint8_t node_id, char product[4]); bool aciomgr_get_node_product_ident(
struct aciomgr_port_dispatcher *dispatcher,
uint8_t node_id,
char product[ACIOMGR_NODE_PRODUCT_CODE_LEN]);
/** /**
* Submit and wait for the response of the specified packet * Submit and wait for the response of the specified packet
@@ -69,8 +78,8 @@ bool aciomgr_port_submit_packet(
* @param dispatcher dispatcher context from aciomgr_port_init * @param dispatcher dispatcher context from aciomgr_port_init
* @return the device context * @return the device context
*/ */
struct aciodrv_device_ctx *aciomgr_port_checkout( struct aciodrv_device_ctx *
struct aciomgr_port_dispatcher *dispatcher); aciomgr_port_checkout(struct aciomgr_port_dispatcher *dispatcher);
/** /**
* Checkin the device handler that this thread holds * Checkin the device handler that this thread holds
+13 -10
View File
@@ -19,6 +19,7 @@
#include "util/log.h" #include "util/log.h"
#define NUMBER_OF_EMULATED_READERS 2 #define NUMBER_OF_EMULATED_READERS 2
#define INVALID_NODE_ID -1
static const uint8_t eam_io_keypad_mappings[16] = {EAM_IO_KEYPAD_DECIMAL, static const uint8_t eam_io_keypad_mappings[16] = {EAM_IO_KEYPAD_DECIMAL,
EAM_IO_KEYPAD_3, EAM_IO_KEYPAD_3,
@@ -49,24 +50,24 @@ void eam_io_set_loggers(
log_formatter_t warning, log_formatter_t warning,
log_formatter_t fatal) log_formatter_t fatal)
{ {
/* Pass logger functions on to aciomgr so that it has somewhere to write
its own log output. */
aciomgr_set_loggers(misc, info, warning, fatal); aciomgr_set_loggers(misc, info, warning, fatal);
log_to_external(misc, info, warning, fatal); log_to_external(misc, info, warning, fatal);
} }
static bool check_if_icca(int node_id) { // all of these are referred to internally as ICCA
char product[4]; static bool check_if_icca(int node_id)
{
char product[ACIOMGR_NODE_PRODUCT_CODE_LEN];
aciomgr_get_node_product_ident(acio_manager_ctx, node_id, product); aciomgr_get_node_product_ident(acio_manager_ctx, node_id, product);
if (!memcmp(product, "ICCA", 4)) { if (!memcmp(product, "ICCA", ACIOMGR_NODE_PRODUCT_CODE_LEN)) {
return true; return true;
} }
if (!memcmp(product, "ICCB", 4)) { if (!memcmp(product, "ICCB", ACIOMGR_NODE_PRODUCT_CODE_LEN)) {
return true; return true;
} }
if (!memcmp(product, "ICCC", 4)) { if (!memcmp(product, "ICCC", ACIOMGR_NODE_PRODUCT_CODE_LEN)) {
return true; return true;
} }
@@ -107,12 +108,14 @@ bool eam_io_init(
} }
struct aciodrv_device_ctx *device = aciomgr_port_checkout(acio_manager_ctx); struct aciodrv_device_ctx *device = aciomgr_port_checkout(acio_manager_ctx);
for (uint8_t i = 0; i < NUMBER_OF_EMULATED_READERS; i++) { for (uint8_t i = 0; i < NUMBER_OF_EMULATED_READERS; i++) {
icca_node_id[i] = -1; icca_node_id[i] = INVALID_NODE_ID;
for (uint8_t nid = 0; nid < aciomgr_get_node_count(acio_manager_ctx); ++nid) { for (uint8_t nid = 0; nid < aciomgr_get_node_count(acio_manager_ctx); ++nid) {
if (check_if_icca(nid)) { if (check_if_icca(nid)) {
bool existing_reader = false; bool existing_reader = false;
for (uint8_t j = 0; j < i; j++) { for (uint8_t j = 0; j < i; j++) {
if (nid == icca_node_id[j]) { if (nid == icca_node_id[j]) {
existing_reader = true; existing_reader = true;
@@ -195,7 +198,7 @@ uint8_t eam_io_read_card(uint8_t unit_no, uint8_t *card_id, uint8_t nbytes)
bool eam_io_card_slot_cmd(uint8_t unit_no, uint8_t cmd) bool eam_io_card_slot_cmd(uint8_t unit_no, uint8_t cmd)
{ {
// this node is not setup, just return "success"" // this node is not setup, just return "success""
if (icca_node_id[unit_no] == -1) { if (icca_node_id[unit_no] == INVALID_NODE_ID) {
return true; return true;
} }
@@ -231,7 +234,7 @@ bool eam_io_card_slot_cmd(uint8_t unit_no, uint8_t cmd)
bool eam_io_poll(uint8_t unit_no) bool eam_io_poll(uint8_t unit_no)
{ {
// this node is not setup, just return "success"" // this node is not setup, just return "success""
if (icca_node_id[unit_no] == -1) { if (icca_node_id[unit_no] == INVALID_NODE_ID) {
return true; return true;
} }
-2
View File
@@ -45,8 +45,6 @@ void sdvx_io_set_loggers(
log_formatter_t warning, log_formatter_t warning,
log_formatter_t fatal) log_formatter_t fatal)
{ {
/* Pass logger functions on to aciomgr so that it has somewhere to write
its own log output. */
aciomgr_set_loggers(misc, info, warning, fatal); aciomgr_set_loggers(misc, info, warning, fatal);
sdvx_io_log_misc = misc; sdvx_io_log_misc = misc;