From 6eb14a6ed1af75f2cea2f5773cefeffbf9f35e9b Mon Sep 17 00:00:00 2001 From: whowechina Date: Sun, 14 Apr 2024 16:17:26 +0800 Subject: [PATCH] Begin to work on bana protocol --- firmware/include/bana.h | 22 +++++++++++ firmware/src/CMakeLists.txt | 2 +- firmware/src/commands.c | 38 +++++++++++++----- firmware/src/config.c | 2 +- firmware/src/config.h | 2 +- firmware/src/lib/bana.c | 79 +++++++++++++++++++++++++++++++++++++ firmware/src/main.c | 13 +++++- 7 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 firmware/include/bana.h create mode 100644 firmware/src/lib/bana.c diff --git a/firmware/include/bana.h b/firmware/include/bana.h new file mode 100644 index 0000000..ef260c6 --- /dev/null +++ b/firmware/include/bana.h @@ -0,0 +1,22 @@ +/* + * Bandai Namco Protocol + * WHowe + */ + +#ifndef BANA_H +#define BANA_H + +#include +#include + +/* return true if accepts a byte, false if rejects */ +typedef void (*bana_putc_func)(uint8_t byte); + +void bana_init(bana_putc_func putc_func); + +bool bana_feed(int c); + +/* bana activity expires at this time */ +uint64_t bana_expire_time(); + +#endif diff --git a/firmware/src/CMakeLists.txt b/firmware/src/CMakeLists.txt index 52d19bf..f532822 100644 --- a/firmware/src/CMakeLists.txt +++ b/firmware/src/CMakeLists.txt @@ -29,5 +29,5 @@ function(make_firmware board board_def) COMMAND cp ${board}.uf2 ${CMAKE_CURRENT_LIST_DIR}/..) endfunction() -add_library(aic lib/aime.c lib/pn532.c lib/pn5180.c lib/nfc.c) +add_library(aic lib/aime.c lib/bana.c lib/pn532.c lib/pn5180.c lib/nfc.c) make_firmware(aic_pico BOARD_AIC_PICO) diff --git a/firmware/src/commands.c b/firmware/src/commands.c index b323408..d5319ea 100644 --- a/firmware/src/commands.c +++ b/firmware/src/commands.c @@ -42,7 +42,13 @@ static void handle_display() printf(" Level: [%d ~ %d]\n", aic_cfg->light.min, aic_cfg->light.max); printf("[AIME]\n"); printf(" Virtual AIC: %s\n", aic_cfg->virtual_aic ? "ON" : "OFF"); - printf(" Mode: %d (%s)\n", aic_cfg->aime_mode, aime_get_mode_string()); + + printf(" Mode: %s\n", aic_cfg->mode == 0 ? "aime0" : + aic_cfg->mode == 1 ? "aime1" : + aic_cfg->mode == 0x10 ? "bana" : "not set"); + if ((aic_cfg->mode & 0xf0) == 0) { + printf(" Pattern: %s\n", aime_get_mode_string()); + } } static void handle_save() @@ -94,22 +100,36 @@ static void handle_virtual(int argc, char *argv[]) static void handle_mode(int argc, char *argv[]) { - const char *usage = "Usage: mode <0:1>\n"; + const char *usage = "Usage: mode \n" + " aime0: Sega Aime 0\n" + " aime1: Sega Aime 1\n" + " bana: Bandai Namco\n"; if (argc != 1) { printf("%s", usage); return; } - const char *commands[] = { "0", "1" }; - int match = cli_match_prefix(commands, 2, argv[0]); - if (match < 0) { - printf("%s", usage); - return; + const char *commands[] = { "aime0", "aime1", "bana" }; + int match = cli_match_prefix(commands, 3, argv[0]); + switch (match) { + case 0: + aic_cfg->mode = 0x00; + break; + case 1: + aic_cfg->mode = 0x01; + break; + case 2: + aic_cfg->mode = 0x10; + break; + default: + printf("%s", usage); + return; } - aic_cfg->aime_mode = match; + if ((aic_cfg->mode & 0xf0) == 0) { + aime_set_mode(aic_cfg->mode); + } - aime_set_mode(match); config_changed(); } diff --git a/firmware/src/config.c b/firmware/src/config.c index 591964e..76a275e 100644 --- a/firmware/src/config.c +++ b/firmware/src/config.c @@ -14,7 +14,7 @@ aic_cfg_t *aic_cfg; static aic_cfg_t default_cfg = { .light = { .min = 24, .max = 128, .rgb = true, .led = true }, .virtual_aic = true, - .aime_mode = 1, + .mode = 0, }; aic_runtime_t *aic_runtime; diff --git a/firmware/src/config.h b/firmware/src/config.h index 239568d..38eec42 100644 --- a/firmware/src/config.h +++ b/firmware/src/config.h @@ -17,7 +17,7 @@ typedef struct __attribute__((packed)) { bool led; } light; bool virtual_aic; - uint8_t aime_mode; + uint8_t mode; uint32_t reserved; } aic_cfg_t; diff --git a/firmware/src/lib/bana.c b/firmware/src/lib/bana.c new file mode 100644 index 0000000..bc87907 --- /dev/null +++ b/firmware/src/lib/bana.c @@ -0,0 +1,79 @@ +/* + * Bandai Namco NFC Reader + * WHowe + * + * Use NFC Module to read BANA + */ + +#include +#include +#include + +#include "bsp/board.h" +#include "hardware/gpio.h" +#include "hardware/i2c.h" + +#include "nfc.h" +#include "bana.h" + +#define BANA_EXPIRE_TIME 5000000ULL + +static void putc_trap(uint8_t byte) +{ +} + +static bana_putc_func bana_putc = putc_trap; + +void bana_init(bana_putc_func putc_func) +{ + bana_putc = putc_func; +} + +/* +static uint8_t mifare_keys[2][6]; // 'KeyA' and 'KeyB' + +static union __attribute__((packed)) { + struct { + }; + uint8_t raw[256]; +} response; + +static union __attribute__((packed)) { + struct { + }; + uint8_t raw[256]; +} request; + +struct { + bool active; + uint8_t len; + uint8_t check_sum; + bool escaping; + uint64_t time; +} req_ctx; +*/ + +typedef struct __attribute__((packed)) { + uint8_t count; + uint8_t type; + uint8_t id_len; + union { + struct { + uint8_t idm[8]; + uint8_t pmm[8]; + }; + uint8_t uid[6]; + }; +} card_info_t; + +static uint64_t expire_time; + +bool bana_feed(int c) +{ + return true; +} + +uint64_t bana_expire_time() +{ + return expire_time; +} diff --git a/firmware/src/main.c b/firmware/src/main.c index fff0316..aeb11c2 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -175,7 +175,11 @@ static void aime_run() uint8_t buf[32]; int count = tud_cdc_n_read(aime_intf, buf, sizeof(buf)); for (int i = 0; i < count; i++) { - aime_feed(buf[i]); + if ((aic_cfg->mode & 0xf0) == 0) { + aime_feed(buf[i]); + } else { + //bana_feed(buf[i]); + } } } } @@ -229,7 +233,12 @@ void init() aime_init(cdc_aime_putc); aime_virtual_aic(aic_cfg->virtual_aic); - aime_set_mode(aic_cfg->aime_mode); + + if ((aic_cfg->mode & 0x0f) == 0) { + aime_set_mode(aic_cfg->mode); + } + + //bana_init(); cli_init("aic_pico>", "\n << AIC Pico >>\n" " https://github.com/whowechina\n\n");