From c3ed8990aaae290d81307aae415c09377da8e517 Mon Sep 17 00:00:00 2001 From: Will Xyen Date: Mon, 15 Mar 2021 19:44:25 -0700 Subject: [PATCH] aciomgr: cleanup and address comments --- src/main/aciomgr/Module.mk | 1 + src/main/aciomgr/dllmain.c | 16 +++++++++ src/main/aciomgr/manager.c | 60 +++++++++++++------------------- src/main/aciomgr/manager.h | 15 ++++++-- src/main/eamio-icca/eamio-icca.c | 23 ++++++------ src/main/sdvxio-kfca/sdvxio.c | 2 -- 6 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 src/main/aciomgr/dllmain.c diff --git a/src/main/aciomgr/Module.mk b/src/main/aciomgr/Module.mk index 5bc35f8..0a17e8c 100644 --- a/src/main/aciomgr/Module.mk +++ b/src/main/aciomgr/Module.mk @@ -5,4 +5,5 @@ libs_aciomgr := \ util \ src_aciomgr := \ + dllmain.c \ manager.c \ diff --git a/src/main/aciomgr/dllmain.c b/src/main/aciomgr/dllmain.c new file mode 100644 index 0000000..2d4b6f0 --- /dev/null +++ b/src/main/aciomgr/dllmain.c @@ -0,0 +1,16 @@ +#include + +#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; +} diff --git a/src/main/aciomgr/manager.c b/src/main/aciomgr/manager.c index c444a19..6a81134 100644 --- a/src/main/aciomgr/manager.c +++ b/src/main/aciomgr/manager.c @@ -14,7 +14,7 @@ struct aciomgr_port_dispatcher { CRITICAL_SECTION cs; - size_t references; + atomic_size_t references; struct aciodrv_device_ctx *device; char path[MAX_PORT_PATH_LENGTH]; int baud; @@ -23,10 +23,8 @@ struct aciomgr_port_dispatcher { static void _aciomgr_setup_port_dispatcher( struct aciomgr_port_dispatcher *dispatcher, const char *path, int baud); -static void _aciomgr_destroy_port_dispatcher( - struct aciomgr_port_dispatcher *dispatcher); -static void _aciomgr_init(); -static void _aciomgr_fini(); +static void +_aciomgr_destroy_port_dispatcher(struct aciomgr_port_dispatcher *dispatcher); // DLL-globals static atomic_bool running; @@ -52,17 +50,17 @@ static void _aciomgr_setup_port_dispatcher( dispatcher->references = 0; } -static void _aciomgr_destroy_port_dispatcher( - struct aciomgr_port_dispatcher *dispatcher) +static void +_aciomgr_destroy_port_dispatcher(struct aciomgr_port_dispatcher *dispatcher) { aciodrv_device_close(dispatcher->device); DeleteCriticalSection(&dispatcher->cs); } -static void _aciomgr_init() +void _aciomgr_init() { if (running) { - // warn + log_warning("_aciomgr_init called when already running"); _aciomgr_fini(); } InitializeCriticalSection(&mgr_cs); @@ -71,11 +69,12 @@ static void _aciomgr_init() running = true; } -static void _aciomgr_fini() +void _aciomgr_fini() { if (!running) { - // warn + log_warning("_aciomgr_fini called when not running"); } + running = false; DeleteCriticalSection(&mgr_cs); array_fini(&active_ports); @@ -93,17 +92,17 @@ void aciomgr_set_loggers( struct aciomgr_port_dispatcher *aciomgr_port_init(const char *path, int baud) { if (!running) { - // warn + log_warning("aciomgr_port_init: called when not running"); return NULL; } if (strlen(path) >= MAX_PORT_PATH_LENGTH) { - // warn + log_warning("aciomgr_port_init: path too long"); return NULL; } - struct aciomgr_port_dispatcher* entry; + struct aciomgr_port_dispatcher *entry; EnterCriticalSection(&mgr_cs); @@ -143,7 +142,7 @@ done: void aciomgr_port_fini(struct aciomgr_port_dispatcher *dispatcher) { if (!running) { - // warn + log_warning("aciomgr_port_fini: called when not running"); return; } @@ -157,7 +156,8 @@ void aciomgr_port_fini(struct aciomgr_port_dispatcher *dispatcher) } // 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); } @@ -165,9 +165,10 @@ uint8_t aciomgr_get_node_count(struct aciomgr_port_dispatcher *dispatcher) { bool aciomgr_get_node_product_ident( struct aciomgr_port_dispatcher *dispatcher, 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( @@ -178,7 +179,8 @@ bool aciomgr_port_submit_packet( // CS's although lightweight, may still be a burden, short circuit if (dispatcher->references > 1) { 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); return response; } @@ -186,8 +188,8 @@ bool aciomgr_port_submit_packet( return aciodrv_send_and_recv(dispatcher->device, msg, max_resp_size); } -struct aciodrv_device_ctx *aciomgr_port_checkout( - struct aciomgr_port_dispatcher *dispatcher) +struct aciodrv_device_ctx * +aciomgr_port_checkout(struct aciomgr_port_dispatcher *dispatcher) { if (dispatcher->references > 1) { EnterCriticalSection(&dispatcher->cs); @@ -196,21 +198,9 @@ struct aciodrv_device_ctx *aciomgr_port_checkout( 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) { 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; -} diff --git a/src/main/aciomgr/manager.h b/src/main/aciomgr/manager.h index 00f52ce..fbdc632 100644 --- a/src/main/aciomgr/manager.h +++ b/src/main/aciomgr/manager.h @@ -7,9 +7,15 @@ #include "bemanitools/glue.h" +#define ACIOMGR_NODE_PRODUCT_CODE_LEN 4 + struct aciodrv_device_ctx; 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 * 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 * 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 @@ -69,8 +78,8 @@ bool aciomgr_port_submit_packet( * @param dispatcher dispatcher context from aciomgr_port_init * @return the device context */ -struct aciodrv_device_ctx *aciomgr_port_checkout( - struct aciomgr_port_dispatcher *dispatcher); +struct aciodrv_device_ctx * +aciomgr_port_checkout(struct aciomgr_port_dispatcher *dispatcher); /** * Checkin the device handler that this thread holds diff --git a/src/main/eamio-icca/eamio-icca.c b/src/main/eamio-icca/eamio-icca.c index 37b3e20..1cdf9a6 100644 --- a/src/main/eamio-icca/eamio-icca.c +++ b/src/main/eamio-icca/eamio-icca.c @@ -19,6 +19,7 @@ #include "util/log.h" #define NUMBER_OF_EMULATED_READERS 2 +#define INVALID_NODE_ID -1 static const uint8_t eam_io_keypad_mappings[16] = {EAM_IO_KEYPAD_DECIMAL, EAM_IO_KEYPAD_3, @@ -49,24 +50,24 @@ void eam_io_set_loggers( log_formatter_t warning, 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); log_to_external(misc, info, warning, fatal); } -static bool check_if_icca(int node_id) { - char product[4]; +// all of these are referred to internally as ICCA +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); - if (!memcmp(product, "ICCA", 4)) { + if (!memcmp(product, "ICCA", ACIOMGR_NODE_PRODUCT_CODE_LEN)) { return true; } - if (!memcmp(product, "ICCB", 4)) { + if (!memcmp(product, "ICCB", ACIOMGR_NODE_PRODUCT_CODE_LEN)) { return true; } - if (!memcmp(product, "ICCC", 4)) { + if (!memcmp(product, "ICCC", ACIOMGR_NODE_PRODUCT_CODE_LEN)) { return true; } @@ -107,12 +108,14 @@ bool eam_io_init( } struct aciodrv_device_ctx *device = aciomgr_port_checkout(acio_manager_ctx); + 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) { if (check_if_icca(nid)) { bool existing_reader = false; + for (uint8_t j = 0; j < i; j++) { if (nid == icca_node_id[j]) { 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) { // 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; } @@ -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) { // 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; } diff --git a/src/main/sdvxio-kfca/sdvxio.c b/src/main/sdvxio-kfca/sdvxio.c index 4ae1af5..e709af8 100644 --- a/src/main/sdvxio-kfca/sdvxio.c +++ b/src/main/sdvxio-kfca/sdvxio.c @@ -45,8 +45,6 @@ void sdvx_io_set_loggers( log_formatter_t warning, 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); sdvx_io_log_misc = misc;