From a0e2fec1795eb7da782ddd9c0fad58859d8a44a3 Mon Sep 17 00:00:00 2001 From: Stephane Portron Date: Fri, 15 Sep 2023 17:08:07 +0200 Subject: [PATCH] Stax : fix BLE pairing in apps --- include/os_io_seproxyhal.h | 3 +- include/seproxyhal_protocol.h | 1 + lib_blewbxx_impl/include/ledger_ble.h | 1 + lib_blewbxx_impl/src/ledger_ble.c | 73 +++++++++++++++++---------- lib_ux_stax/ux.c | 11 ---- src/os_io_seproxyhal.c | 26 +++++++++- 6 files changed, 76 insertions(+), 39 deletions(-) diff --git a/include/os_io_seproxyhal.h b/include/os_io_seproxyhal.h index e43ce042a..3fdf0ec0c 100644 --- a/include/os_io_seproxyhal.h +++ b/include/os_io_seproxyhal.h @@ -299,7 +299,8 @@ void io_seproxyhal_play_tune(tune_index_e tune_index); void io_seph_ble_enable(unsigned char enable); void io_seph_ble_clear_bond_db(void); void io_seph_ble_name_changed(void); -#endif // HAVE_BLE +void io_seph_ux_accept_pairing(unsigned char status); +#endif // HAVE_BLE void io_seph_ux_redisplay(void); /** diff --git a/include/seproxyhal_protocol.h b/include/seproxyhal_protocol.h index 20dade09e..325445609 100644 --- a/include/seproxyhal_protocol.h +++ b/include/seproxyhal_protocol.h @@ -185,6 +185,7 @@ #define SEPROXYHAL_TAG_UX_CMD_BLE_RESET_PAIRINGS 0x02 #define SEPROXYHAL_TAG_UX_CMD_REDISPLAY 0x03 #define SEPROXYHAL_TAG_UX_CMD_BLE_NAME_CHANGED 0x04 +#define SEPROXYHAL_TAG_UX_CMD_ACCEPT_PAIRING 0x05 #ifdef HAVE_SERIALIZED_NBGL #define SEPROXYHAL_TAG_NBGL_SERIALIZED 0x5C diff --git a/lib_blewbxx_impl/include/ledger_ble.h b/lib_blewbxx_impl/include/ledger_ble.h index 4b7ba2c29..5dbeae3f5 100644 --- a/lib_blewbxx_impl/include/ledger_ble.h +++ b/lib_blewbxx_impl/include/ledger_ble.h @@ -39,6 +39,7 @@ void LEDGER_BLE_send(uint8_t* packet, uint16_t packet_length); void LEDGER_BLE_receive(void); void LEDGER_BLE_enable_advertising(uint8_t enable); void LEDGER_BLE_reset_pairings(void); +void LEDGER_BLE_accept_pairing(uint8_t status); #define LEDGER_BLE_get_mac_address(address) { \ unsigned char se_serial[8] = {0}; \ diff --git a/lib_blewbxx_impl/src/ledger_ble.c b/lib_blewbxx_impl/src/ledger_ble.c index 696ea4296..8b078d12d 100644 --- a/lib_blewbxx_impl/src/ledger_ble.c +++ b/lib_blewbxx_impl/src/ledger_ble.c @@ -758,14 +758,18 @@ static void ask_user_pairing_numeric_comparison(uint32_t code) { bolos_ux_params_t ux_params; - ux_params.u.pairing_request.type = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST_NUMCOMP; - ux_params.u.pairing_request.pairing_info_len = 6; - SPRINTF(ux_params.u.pairing_request.pairing_info, "%06d", (unsigned int)code); - ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST; - ux_params.len = sizeof(ux_params.u.pairing_request); - G_io_asynch_ux_callback.asynchmodal_end_callback = rsp_user_pairing_numeric_comparison; - ledger_ble_data.pairing_in_progress = 1; - os_ux(&ux_params); + ux_params.u.pairing_request.type = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST_NUMCOMP; + ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST; + ux_params.len = sizeof(ux_params.u.pairing_request); + ledger_ble_data.pairing_in_progress = 1; + ux_params.u.pairing_request.pairing_info_len = 6; + SPRINTF(ux_params.u.pairing_request.pairing_info, "%06d", (unsigned int) code); +#ifdef TARGET_STAX + G_io_asynch_ux_callback.asynchmodal_end_callback = NULL; +#else // !TARGET_STAX + G_io_asynch_ux_callback.asynchmodal_end_callback = rsp_user_pairing_numeric_comparison; +#endif // !TARGET_STAX + os_ux(&ux_params); } static void rsp_user_pairing_numeric_comparison(unsigned int status) @@ -788,28 +792,35 @@ static void ask_user_pairing_passkey(void) { bolos_ux_params_t ux_params; - ledger_ble_data.pairing_code = cx_rng_u32_range_func(0, 1000000, cx_rng_u32); - ux_params.u.pairing_request.type = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST_PASSKEY; - ux_params.u.pairing_request.pairing_info_len = 6; - SPRINTF(ux_params.u.pairing_request.pairing_info, "%06d", ledger_ble_data.pairing_code); - ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST; - ux_params.len = sizeof(ux_params.u.pairing_request); - G_io_asynch_ux_callback.asynchmodal_end_callback = rsp_user_pairing_passkey; - ledger_ble_data.pairing_in_progress = 1; - os_ux(&ux_params); + ux_params.u.pairing_request.type = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST_PASSKEY; + ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST; + ux_params.len = sizeof(ux_params.u.pairing_request); + ledger_ble_data.pairing_in_progress = 1; + ledger_ble_data.pairing_code = cx_rng_u32_range_func(0, 1000000, cx_rng_u32); + ux_params.u.pairing_request.pairing_info_len = 6; + SPRINTF(ux_params.u.pairing_request.pairing_info, "%06d", ledger_ble_data.pairing_code); +#ifdef TARGET_STAX + G_io_asynch_ux_callback.asynchmodal_end_callback = NULL; +#else // !TARGET_STAX + G_io_asynch_ux_callback.asynchmodal_end_callback = rsp_user_pairing_passkey; +#endif // !TARGET_STAX + os_ux(&ux_params); } static void rsp_user_pairing_passkey(unsigned int status) { - if (status != BOLOS_UX_OK) { // BLE_TODO - end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_ACCEPT_PASSKEY); - ledger_ble_data.pairing_code = cx_rng_u32_range_func(0, 1000000, cx_rng_u32); - } - else { - end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_CANCEL_PASSKEY); - } - aci_gap_pass_key_resp(ledger_ble_data.connection.connection_handle, - ledger_ble_data.pairing_code); + if (status == BOLOS_UX_OK) { + end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_ACCEPT_PASSKEY); + ledger_ble_data.pairing_code = cx_rng_u32_range_func(0, 1000000, cx_rng_u32); + aci_gap_pass_key_resp(ledger_ble_data.connection.connection_handle, + ledger_ble_data.pairing_code); + } + else if (status == BOLOS_UX_IGNORE) { + ledger_ble_data.pairing_in_progress = 0; + } + else { + end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_CANCEL_PASSKEY); + } } static void attribute_modified(uint8_t *buffer, uint16_t length) @@ -1171,6 +1182,16 @@ void LEDGER_BLE_reset_pairings(void) } } +void LEDGER_BLE_accept_pairing(uint8_t status) +{ + if (ledger_ble_data.pairing_in_progress == 1) { + rsp_user_pairing_numeric_comparison(status); + } + else if (ledger_ble_data.pairing_in_progress == 2) { + rsp_user_pairing_passkey(status); + } +} + int hci_send_req(struct hci_request *p_cmd, uint8_t async) { UNUSED(async); diff --git a/lib_ux_stax/ux.c b/lib_ux_stax/ux.c index 349f43db3..7b3800af0 100644 --- a/lib_ux_stax/ux.c +++ b/lib_ux_stax/ux.c @@ -35,17 +35,6 @@ static bool ux_forward_event(bool ignoring_app_if_ux_busy) { G_ux_params.len = 0; os_ux(&G_ux_params); G_ux_params.len = os_sched_last_status(TASK_BOLOS_UX); -#ifdef HAVE_BLE - if (G_ux.asynchmodal_end_callback && - (os_ux_get_status(BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST) != 0)) { - asynchmodal_end_callback_t cb = G_ux.asynchmodal_end_callback; - // reset G_ux.asynchmodal_end_callback for next time - G_ux.asynchmodal_end_callback = NULL; - cb(os_ux_get_status(BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST)); - // for app to redraw/refresh itself - G_ux_params.len = BOLOS_UX_REDRAW; - } -#endif // HAVE_BLE if (G_ux_params.len == BOLOS_UX_REDRAW) { // enable drawing according to UX decision nbgl_objAllowDrawing(true); diff --git a/src/os_io_seproxyhal.c b/src/os_io_seproxyhal.c index 65a48cc55..aa290ebaf 100644 --- a/src/os_io_seproxyhal.c +++ b/src/os_io_seproxyhal.c @@ -307,6 +307,11 @@ unsigned int io_seproxyhal_handle_event(void) { G_io_app.name_changed = 1; io_seph_ble_enable(0); break; + + case SEPROXYHAL_TAG_UX_CMD_ACCEPT_PAIRING: + LEDGER_BLE_accept_pairing(G_io_seproxyhal_spi_buffer[4]); + return 1; + break; #endif // HAVE_BLE #if !defined(HAVE_BOLOS) && defined(HAVE_BAGL) @@ -314,7 +319,16 @@ unsigned int io_seproxyhal_handle_event(void) { ux_stack_redisplay(); return 1; break; -#endif // HAVE_BOLOS +#endif // HAVE_BOLOS && HAVE_BAGL + +#if !defined(HAVE_BOLOS) && defined(HAVE_NBGL) + case SEPROXYHAL_TAG_UX_CMD_REDISPLAY: + nbgl_objAllowDrawing(true); + nbgl_screenRedraw(); + nbgl_refresh(); + return 1; + break; +#endif // HAVE_BOLOS && HAVE_NBGL default: return io_event(CHANNEL_SPI); @@ -1009,6 +1023,16 @@ void io_seph_ux_redisplay(void) io_seproxyhal_spi_send(G_io_seproxyhal_spi_buffer, 4); } +void io_seph_ux_accept_pairing(unsigned char status) +{ + G_io_seproxyhal_spi_buffer[0] = SEPROXYHAL_TAG_UX_CMD; + G_io_seproxyhal_spi_buffer[1] = 0; + G_io_seproxyhal_spi_buffer[2] = 2; + G_io_seproxyhal_spi_buffer[3] = SEPROXYHAL_TAG_UX_CMD_ACCEPT_PAIRING; + G_io_seproxyhal_spi_buffer[4] = status; + io_seproxyhal_spi_send(G_io_seproxyhal_spi_buffer, 5); +} + static const unsigned char seph_io_usb_disconnect[] = { SEPROXYHAL_TAG_USB_CONFIG, 0,