Skip to content

Commit

Permalink
Stax : fix BLE pairing in apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Portron committed Sep 15, 2023
1 parent af0acac commit a0e2fec
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 39 deletions.
3 changes: 2 additions & 1 deletion include/os_io_seproxyhal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
1 change: 1 addition & 0 deletions include/seproxyhal_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
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) { \
unsigned char se_serial[8] = {0}; \
Expand Down
73 changes: 47 additions & 26 deletions lib_blewbxx_impl/src/ledger_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
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 @@ -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);
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 @@ -307,14 +307,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 @@ -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,
Expand Down

0 comments on commit a0e2fec

Please sign in to comment.