From 799ae949a7d6e78ea54a3639848755579d33759c Mon Sep 17 00:00:00 2001 From: Andrey Perminov Date: Mon, 20 Apr 2020 12:34:21 -0700 Subject: [PATCH] Introduced bmlite callback functions Change-Id: I6f8502b06733b1bebfe700dbe78407eeb0e256ff --- BMLite_example/inc/bep_host_if.h | 74 +++++++++++++++++ BMLite_example/src/bep_host_if.c | 132 +++++++++++++++---------------- BMLite_example/src/main.c | 57 ++++++++++++- 3 files changed, 189 insertions(+), 74 deletions(-) diff --git a/BMLite_example/inc/bep_host_if.h b/BMLite_example/inc/bep_host_if.h index b05033d..1480d66 100644 --- a/BMLite_example/inc/bep_host_if.h +++ b/BMLite_example/inc/bep_host_if.h @@ -29,6 +29,80 @@ #define REMOVE_ID_ALL_TEMPLATES 0U + +typedef enum { + BMLITE_ERROR_OK = 0, + BMLITE_ERROR_CAPTURE, + BMLITE_ERROR_CAPTURE_START, + BMLITE_ERROR_ENROLL_START, + BMLITE_ERROR_ENROLL_ADD, + BMLITE_ERROR_ENROLL_FINISH, + BMLITE_ERROR_WRONG_ANSWER, + BMLITE_ERROR_FINGER_WAIT, + BMLITE_ERROR_IDENTYFY, + BMLITE_ERROR_TEMPLATE_SAVE, + BMLITE_ERROR_TEMPLATE_DELETE, + BMLITE_ERROR_TEMPLATE_COUNT, + BMLITE_ERROR_TEMPLATE_GETIDS, + BMLITE_ERROR_IMAGE_EXTRACT, + BMLITE_ERROR_IMAGE_GETSIZE, + BMLITE_ERROR_IMAGE_GET, + BMLITE_ERROR_GETVERSION, + BMLITE_ERROR_SW_RESET, + BMLITE_ERROR_CALIBRATE, + BMLITE_ERROR_CALIBRATE_DELETE, + BMLITE_ERROR_SEND_CMD +} bmlite_error_t; + +/** + * @brief Error Callback function + * + * @param[in] Callback Error Code + * @param[in] BEP result code + */ +void bmlite_on_error(bmlite_error_t error, int32_t value); + +/** + * @brief Starting Capture Callback function + */ +void bmlite_on_start_capture(); + +/** + * @brief Finishing Capture Callback function + */ +void bmlite_on_finish_capture(); + +/** + * @brief Starting Enroll Callback function + */ +void bmlite_on_start_enroll(); + +/** + * @brief Finishing Enroll Callback function + */ +void bmlite_on_finish_enroll(); + +/** + * @brief Starting Capture for Enroll Callback function + */ +void bmlite_on_start_enrollcapture(); + +/** + * @brief Finishing Capture for Enroll Callback function + */ +void bmlite_on_finish_enrollcapture(); + +/** + * @brief Starting Identify Callback function + */ +void bmlite_on_identify_start(); + +/** + * @brief Finishing Identify Callback function + */ +void bmlite_on_identify_finish(); + + /** * @brief Sends HCP commands for capturing an image in Bio MCU * diff --git a/BMLite_example/src/bep_host_if.c b/BMLite_example/src/bep_host_if.c index b3911be..3560945 100644 --- a/BMLite_example/src/bep_host_if.c +++ b/BMLite_example/src/bep_host_if.c @@ -37,23 +37,28 @@ #define RECEIVE_TIMEOUT 10 -#ifdef DEBUG -#define log_debug(format, ...) printf(format, ##__VA_ARGS__) -#else -#define log_debug(format, ...) -#endif -// #define log_info(format, ...) printf(format, ##__VA_ARGS__) -// #define log_error(format, ...) printf(format, ##__VA_ARGS__) - -#define log_info(format, ...) -#define log_error(format, ...) - - - /** Maximum attempts for capture image */ static const uint8_t MAX_CAPTURE_ATTEMPTS = 15U; static const uint16_t CAPTURE_TIMEOUT = 3000; +/** + * @brief Mock callback functions + */ +__attribute__((weak)) void bmlite_on_error(bmlite_error_t error, int32_t value) { (void)error; (void)value; } + +__attribute__((weak)) void bmlite_on_start_capture() {} +__attribute__((weak)) void bmlite_on_finish_capture() {} + +__attribute__((weak)) void bmlite_on_finish_enroll() {} +__attribute__((weak)) void bmlite_on_start_enroll() {} + +__attribute__((weak)) void bmlite_on_start_enrollcapture() {} +__attribute__((weak)) void bmlite_on_finish_enrollcapture() {} + +__attribute__((weak)) void bmlite_on_identify_start() {} +__attribute__((weak)) void bmlite_on_identify_finish() {} + + /** * @brief Helper function for sending HCP commands @@ -84,7 +89,6 @@ static fpc_bep_result_t send_command_args2(fpc_com_chain_t *chain, fpc_hcp_cmd_t if (arg_key1 != ARG_NONE) { if (!fpc_hcp_arg_add(&command, arg_key1, arg_data1_length, false, arg_data1)) { - log_error("%s:%u Could not add arg:%u\n", __func__, __LINE__, arg_key1); bep_result = FPC_BEP_RESULT_NO_MEMORY; goto exit; } @@ -92,7 +96,6 @@ static fpc_bep_result_t send_command_args2(fpc_com_chain_t *chain, fpc_hcp_cmd_t if (arg_key2 != ARG_NONE) { if (!fpc_hcp_arg_add(&command, arg_key2, arg_data2_length, false, arg_data2)) { - log_error("%s:%u Could not add arg:%u\n", __func__, __LINE__, arg_key2); bep_result = FPC_BEP_RESULT_NO_MEMORY; goto exit; } @@ -102,7 +105,7 @@ static fpc_bep_result_t send_command_args2(fpc_com_chain_t *chain, fpc_hcp_cmd_t bep_result = com_to_bep_result(com_result); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u ERROR %d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_SEND_CMD, bep_result); } exit: @@ -162,7 +165,6 @@ static fpc_bep_result_t receive_result_args2(fpc_com_chain_t *chain, if (arg_data) { bep_result = *(int8_t *)arg_data->data; } else { - log_error("%s Result argument missing\n", __func__); bep_result = FPC_BEP_RESULT_INVALID_ARGUMENT; } if (bep_result != FPC_BEP_RESULT_OK) { @@ -175,7 +177,6 @@ static fpc_bep_result_t receive_result_args2(fpc_com_chain_t *chain, if (arg_data && arg_data->size <= arg_data1_length) { memcpy(arg_data1, arg_data->data, arg_data->size); } else { - log_error("%s %d argument missing\n", __func__, arg_key1); bep_result = FPC_BEP_RESULT_INVALID_ARGUMENT; goto exit; } @@ -186,10 +187,7 @@ static fpc_bep_result_t receive_result_args2(fpc_com_chain_t *chain, arg_data = fpc_hcp_arg_get(&response, arg_key2); if (arg_data && arg_data->size <= arg_data2_length) { memcpy(arg_data2, arg_data->data, arg_data->size); - } else { - /* Not an error since the second argument is optional */ - log_debug("%s %d argument missing\n", __func__, arg_key2); - } + } } exit: @@ -214,18 +212,16 @@ fpc_bep_result_t bep_capture(fpc_com_chain_t *chain, uint16_t timeout) { fpc_bep_result_t bep_result; - log_info("Put finger on sensor\n"); - /* Capture finger down */ bep_result = send_command(chain, CMD_CAPTURE, ARG_TIMEOUT, &timeout, sizeof(timeout)); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u Error transmitting CMD_CAPTURE\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_CAPTURE_START, bep_result); return bep_result; } - platform_set_led(BMLITE_LED_STATUS_WAITTOUCH); + bmlite_on_start_capture(); bep_result = receive_result_no_args(chain); - platform_set_led(BMLITE_LED_STATUS_READY); + bmlite_on_finish_capture(); return bep_result; } @@ -239,47 +235,43 @@ fpc_bep_result_t bep_enroll_finger(fpc_com_chain_t *chain) /* Enroll start */ bep_result = send_command(chain, CMD_ENROLL, ARG_START, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s, ERROR line:%u\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_ENROLL_START, bep_result); goto exit; } bep_result = receive_result_no_args(chain); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); goto exit; } + bmlite_on_start_enroll(); + for (uint8_t i = 0; i < MAX_CAPTURE_ATTEMPTS; ++i) { + bmlite_on_start_enrollcapture(); bep_result = bep_capture(chain, CAPTURE_TIMEOUT); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Capture failed\n"); + bmlite_on_error(BMLITE_ERROR_CAPTURE, bep_result); break; } - log_info("Capture done. Remove finger from sensor\n"); - - if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving, result=%d\n", __func__, __LINE__, bep_result); - continue; - } + bmlite_on_finish_enrollcapture(); /* Enroll add */ bep_result = send_command(chain, CMD_ENROLL, ARG_ADD, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_ENROLL_ADD, bep_result); continue; } bep_result = receive_result_args1(chain, ARG_COUNT, &samples_remaining, sizeof(samples_remaining)); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); continue; } - log_info("Enroll samples remaining: %d\n", samples_remaining); - if (samples_remaining == 0U) { enroll_done = true; break; @@ -287,14 +279,14 @@ fpc_bep_result_t bep_enroll_finger(fpc_com_chain_t *chain) bep_result = send_command(chain, CMD_WAIT, ARG_FINGER_UP, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_FINGER_WAIT, bep_result); continue; } /* Wait for finger to be lifted from sensor */ bep_result = receive_result_no_args(chain); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); continue; } } @@ -303,11 +295,12 @@ fpc_bep_result_t bep_enroll_finger(fpc_com_chain_t *chain) if (bep_result == FPC_BEP_RESULT_OK) { bep_result = receive_result_no_args(chain); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_ENROLL_FINISH, bep_result); } } exit: + bmlite_on_finish_enroll(); return (!enroll_done) ? FPC_BEP_RESULT_GENERAL_ERROR : bep_result; } @@ -317,33 +310,33 @@ fpc_bep_result_t bep_identify_finger(fpc_com_chain_t *chain, uint16_t *template_ *match = false; + bmlite_on_identify_start(); bep_result = bep_capture(chain, CAPTURE_TIMEOUT); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Capture failed result=%d\n", bep_result); - return bep_result; + bmlite_on_error(BMLITE_ERROR_CAPTURE, bep_result); + goto exit; } - log_info("Capture done. Remove finger from sensor\n"); - bep_result = bep_image_extract(chain); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Extract failed\n"); - return bep_result; + goto exit; } bep_result = send_command(chain, CMD_IDENTIFY, ARG_NONE, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Identify failed\n"); - return bep_result; + bmlite_on_error(BMLITE_ERROR_IDENTYFY, bep_result); + goto exit; } bep_result = receive_result_args2(chain, ARG_MATCH, match, sizeof(bool), ARG_ID, template_id, sizeof(uint16_t)); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Identify failed\n"); - return bep_result; + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); + goto exit; } +exit: + bmlite_on_identify_finish(); return bep_result; } @@ -354,7 +347,7 @@ fpc_bep_result_t bep_save_template(fpc_com_chain_t *chain, uint16_t template_id) bep_result = send_command_args2(chain, CMD_TEMPLATE, ARG_SAVE, NULL, 0, ARG_ID, &template_id, sizeof(template_id)); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_TEMPLATE_SAVE, bep_result); return bep_result; } @@ -373,7 +366,7 @@ fpc_bep_result_t bep_delete_template(fpc_com_chain_t *chain, uint16_t template_i ARG_ID, &template_id, sizeof(template_id)); } if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_TEMPLATE_DELETE, bep_result); return bep_result; } @@ -386,13 +379,13 @@ fpc_bep_result_t bep_get_template_count(fpc_com_chain_t *chain, uint32_t *templa bep_result = send_command(chain, CMD_STORAGE_TEMPLATE, ARG_COUNT, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u ERROR sending CMD_STORAGE_TEMPLATE\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_TEMPLATE_COUNT, bep_result); return bep_result; } bep_result = receive_result_args1(chain, ARG_COUNT, template_count, sizeof(template_count[0])); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); return bep_result; } @@ -406,14 +399,15 @@ fpc_bep_result_t bep_get_template_ids(fpc_com_chain_t *chain, uint16_t *template bep_result = send_command(chain, CMD_STORAGE_TEMPLATE, ARG_ID, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u ERROR sending CMD_STORAGE_TEMPLATE\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_TEMPLATE_GETIDS, bep_result); return bep_result; } bep_result = receive_result_args1(chain, ARG_DATA, template_ids, nof_templates * sizeof(template_ids[0])); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR receiving status=%d\n", __func__, __LINE__, bep_result); + bmlite_on_error(BMLITE_ERROR_WRONG_ANSWER, bep_result); + return bep_result; } @@ -426,7 +420,7 @@ fpc_bep_result_t bep_image_extract(fpc_com_chain_t *chain) bep_result = send_command(chain, CMD_IMAGE, ARG_EXTRACT, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Extract failed\n"); + bmlite_on_error(BMLITE_ERROR_IMAGE_EXTRACT, bep_result); return bep_result; } @@ -439,12 +433,10 @@ fpc_bep_result_t bep_image_get_size(fpc_com_chain_t *chain, uint32_t *size) bep_result = send_command(chain, CMD_IMAGE, ARG_SIZE, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Extract failed\n"); + bmlite_on_error(BMLITE_ERROR_IMAGE_GETSIZE, bep_result); return bep_result; } - log_info("Downloading image data...\n"); - return receive_result_args1(chain, ARG_SIZE, size, sizeof(size)); } @@ -454,7 +446,7 @@ fpc_bep_result_t bep_image_get(fpc_com_chain_t *chain, uint8_t *data, uint32_t s bep_result = send_command(chain, CMD_IMAGE, ARG_UPLOAD, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("Extract failed\n"); + bmlite_on_error(BMLITE_ERROR_IMAGE_GET, bep_result); return bep_result; } @@ -467,7 +459,7 @@ fpc_bep_result_t bep_version(fpc_com_chain_t *chain, char *version, int len) bep_result = send_command_args2(chain, CMD_INFO, ARG_GET, NULL, 0, ARG_VERSION, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s, ERROR line:%u\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_GETVERSION, bep_result); return bep_result; } @@ -480,7 +472,7 @@ fpc_bep_result_t bep_reset(fpc_com_chain_t *chain) bep_result = send_command_no_args(chain, CMD_RESET); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_SW_RESET, bep_result); return bep_result; } @@ -493,7 +485,7 @@ fpc_bep_result_t bep_sensor_calibrate(fpc_com_chain_t *chain) bep_result = send_command_no_args(chain, CMD_STORAGE_CALIBRATION); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_CALIBRATE, bep_result); return bep_result; } @@ -506,7 +498,7 @@ fpc_bep_result_t bep_sensor_calibrate_remove(fpc_com_chain_t *chain) bep_result = send_command(chain, CMD_STORAGE_CALIBRATION, ARG_DELETE, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_CALIBRATE_DELETE, bep_result); return bep_result; } @@ -519,7 +511,7 @@ fpc_bep_result_t bep_sensor_wait_for_finger(fpc_com_chain_t *chain, uint16_t tim bep_result = send_command(chain, CMD_WAIT, ARG_FINGER_DOWN, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_FINGER_WAIT, bep_result); return bep_result; } @@ -533,7 +525,7 @@ fpc_bep_result_t bep_sensor_wait_finger_not_present(fpc_com_chain_t *chain, uint bep_result = send_command(chain, CMD_WAIT, ARG_FINGER_UP, NULL, 0); if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u, ERROR\n", __func__, __LINE__); + bmlite_on_error(BMLITE_ERROR_FINGER_WAIT, bep_result); return bep_result; } diff --git a/BMLite_example/src/main.c b/BMLite_example/src/main.c index df47b84..2bda4bc 100644 --- a/BMLite_example/src/main.c +++ b/BMLite_example/src/main.c @@ -35,6 +35,59 @@ #include "platform.h" // #include "fpc_hal_interfaces.h" +void bmlite_on_error(bmlite_error_t error, int32_t value) +{ + if(value != FPC_BEP_RESULT_TIMEOUT) { + platform_set_led(3); + platform_timebase_busy_wait(500); + platform_set_led(0); + platform_timebase_busy_wait(500); + platform_set_led(3); + platform_timebase_busy_wait(500); + platform_set_led(0); + platform_timebase_busy_wait(500); + } else { + platform_set_led(3); + platform_timebase_busy_wait(100); + platform_set_led(0); + } +} + +// void bmlite_on_start_capture(); +// void bmlite_on_finish_capture(); + +void bmlite_on_finish_enroll() +{ + +} + +void bmlite_on_start_enroll() +{ + platform_set_led(1); + platform_timebase_busy_wait(500); + platform_set_led(2); + platform_timebase_busy_wait(500); +} + +void bmlite_on_start_enrollcapture() +{ + platform_set_led(3); +} + +void bmlite_on_finish_enrollcapture() +{ + platform_set_led(0); + platform_timebase_busy_wait(500); +} + +void bmlite_on_identify_start() +{ + platform_set_led(0); +} + +// void bmlite_on_identify_finish(); + + int main (int argc, char **argv) { int baudrate = 4000000; @@ -71,12 +124,8 @@ int main (int argc, char **argv) // nothing hapened } else if (btn_time < 5000) { // Enroll - platform_set_led(BMLITE_LED_STATUS_STARTENROLL); - platform_timebase_busy_wait(500); res = bep_enroll_finger(&hcp_chain); res = bep_save_template(&hcp_chain, current_id++); - platform_set_led(BMLITE_LED_STATUS_STARTENROLL); - platform_timebase_busy_wait(500); } else { // Erase All templates platform_set_led(BMLITE_LED_STATUS_DELETE_TEMPLATES);