Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stax : fix BLE pairing in apps #423

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/os_io_seproxyhal.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ 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);
void io_seph_ux_accept_pairing(unsigned char status);
#endif // HAVE_BLE
void io_seph_ux_redisplay(void);

Expand Down
1 change: 1 addition & 0 deletions include/seproxyhal_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,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
Expand Down
1 change: 1 addition & 0 deletions lib_blewbxx_impl/include/ledger_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
{ \
Expand Down
41 changes: 31 additions & 10 deletions lib_blewbxx_impl/src/ledger_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,12 +747,16 @@ 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.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);
ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST;
ux_params.len = sizeof(ux_params.u.pairing_request);
#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;
ledger_ble_data.pairing_in_progress = 1;
#endif // !TARGET_STAX
os_ux(&ux_params);
}

Expand All @@ -779,28 +783,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.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);
ux_params.ux_id = BOLOS_UX_ASYNCHMODAL_PAIRING_REQUEST;
ux_params.len = sizeof(ux_params.u.pairing_request);
#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;
ledger_ble_data.pairing_in_progress = 1;
#endif // !TARGET_STAX
os_ux(&ux_params);
}

static void rsp_user_pairing_passkey(unsigned int status)
{
if (status != BOLOS_UX_OK) { // BLE_TODO
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);
}
aci_gap_pass_key_resp(ledger_ble_data.connection.connection_handle,
ledger_ble_data.pairing_code);
}

static void attribute_modified(uint8_t *buffer, uint16_t length)
Expand Down Expand Up @@ -1146,6 +1157,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);
Expand Down
11 changes: 0 additions & 11 deletions lib_ux_stax/ux.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,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);
Expand Down
26 changes: 25 additions & 1 deletion src/os_io_seproxyhal.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,28 @@ 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)
case SEPROXYHAL_TAG_UX_CMD_REDISPLAY:
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);
Expand Down Expand Up @@ -1081,6 +1095,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,
Expand Down