Skip to content

Commit

Permalink
cleanup(ble): gate in-app BLE pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
yhql committed Sep 13, 2023
1 parent 0ad6595 commit 0d278e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Makefile.defines
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ DEFINES += HAVE_BAGL_FONT_INTER_REGULAR_24PX
DEFINES += HAVE_BAGL_FONT_INTER_SEMIBOLD_24PX
DEFINES += HAVE_BAGL_FONT_INTER_MEDIUM_32PX
DEFINES += HAVE_BAGL_FONT_HMALPHAMONO_MEDIUM_32PX
DEFINES += HAVE_INAPP_BLE_PAIRING
DEFINES += HAVE_NBGL
DEFINES += HAVE_PIEZO_SOUND
DEFINES += HAVE_SE_TOUCH
Expand Down Expand Up @@ -212,6 +213,7 @@ DEFINES += HAVE_MCU_SERIAL_STORAGE
DEFINES += HAVE_FONTS
# already defined within apps
# DEFINES += HAVE_BLE
DEFINES += HAVE_INAPP_BLE_PAIRING
DEFINES += HAVE_BATTERY
endif

Expand Down
70 changes: 41 additions & 29 deletions lib_blewbxx_impl/src/ledger_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ static void init_mngr(uint16_t opcode, const uint8_t *buffer, uint16_t length);
static void hci_evt_cmd_complete(const uint8_t *buffer, uint16_t length);
static void hci_evt_le_meta_evt(const uint8_t *buffer, uint16_t length);
static void hci_evt_vendor(const uint8_t *buffer, uint16_t length);
#ifdef HAVE_INAPP_BLE_PAIRING
static void end_pairing_ux(uint8_t pairing_ok);
static void ask_user_pairing_numeric_comparison(uint32_t code);
static void rsp_user_pairing_numeric_comparison(unsigned int status);
static void ask_user_pairing_passkey(void);
static void rsp_user_pairing_passkey(unsigned int status);
#endif // HAVE_INAPP_BLE_PAIRING
static void attribute_modified(const uint8_t *buffer, uint16_t length);
static void write_permit_request(const uint8_t *buffer, uint16_t length);
static void advertising_enable(uint8_t enable);
Expand Down Expand Up @@ -199,18 +201,18 @@ static ledger_ble_data_t ledger_ble_data;

/* Private functions ---------------------------------------------------------*/
static void copy_apdu_to_app(bool update_wait_status) {
// `rx_apdu_length` is already checked to be less than
// `rx_apdu_buffer_max_length` in `process_apdu_chunk()`
memcpy(ledger_protocol_data.rx_dst_buffer,
ledger_protocol_data.rx_apdu_buffer,
ledger_protocol_data.rx_apdu_length);
G_io_app.apdu_length = ledger_protocol_data.rx_apdu_length;
G_io_app.apdu_state = APDU_BLE;
G_io_app.apdu_media = IO_APDU_MEDIA_BLE;
ledger_protocol_data.rx_apdu_length = 0;
if (update_wait_status) {
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
}
// `rx_apdu_length` is already checked to be less than
// `rx_apdu_buffer_max_length` in `process_apdu_chunk()`
memcpy(ledger_protocol_data.rx_dst_buffer,
ledger_protocol_data.rx_apdu_buffer,
ledger_protocol_data.rx_apdu_length);
G_io_app.apdu_length = ledger_protocol_data.rx_apdu_length;
G_io_app.apdu_state = APDU_BLE;
G_io_app.apdu_media = IO_APDU_MEDIA_BLE;
ledger_protocol_data.rx_apdu_length = 0;
if (update_wait_status) {
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
}
}

static void get_device_name(void)
Expand Down Expand Up @@ -540,7 +542,9 @@ static void hci_evt_cmd_complete(const uint8_t *buffer, uint16_t length)
if (ledger_ble_data.connection.connection_handle != 0xFFFF) {
if (G_io_app.disabling_advertising) {
// Connected & ordered to disable ble, force disconnection
#ifdef HAVE_INAPP_BLE_PAIRING
end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_FAILED);
#endif // HAVE_INAPP_BLE_PAIRING
LEDGER_BLE_init();
}
}
Expand Down Expand Up @@ -658,6 +662,7 @@ static void hci_evt_vendor(const uint8_t *buffer, uint16_t length)
}

switch (opcode) {
#ifdef HAVE_INAPP_BLE_PAIRING
case ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE:
LOG_BLE("PAIRING");
switch (buffer[4]) {
Expand Down Expand Up @@ -697,6 +702,7 @@ static void hci_evt_vendor(const uint8_t *buffer, uint16_t length)
LOG_BLE("NUMERIC COMP : %d\n", U4LE(buffer, 4));
ask_user_pairing_numeric_comparison(U4LE(buffer, 4));
break;
#endif // HAVE_INAPP_BLE_PAIRING

case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
attribute_modified(&buffer[4], length - 4);
Expand Down Expand Up @@ -727,7 +733,9 @@ static void hci_evt_vendor(const uint8_t *buffer, uint16_t length)

case ACI_GATT_PROC_TIMEOUT_VSEVT_CODE:
LOG_BLE("PROCEDURE TIMEOUT\n");
#ifdef HAVE_INAPP_BLE_PAIRING
end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_FAILED);
#endif // HAVE_INAPP_BLE_PAIRING
LEDGER_BLE_init();
break;

Expand All @@ -737,6 +745,7 @@ static void hci_evt_vendor(const uint8_t *buffer, uint16_t length)
}
}

#ifdef HAVE_INAPP_BLE_PAIRING
static void end_pairing_ux(uint8_t pairing_ok)
{
bolos_ux_params_t ux_params;
Expand Down Expand Up @@ -811,6 +820,7 @@ static void rsp_user_pairing_passkey(unsigned int status)
aci_gap_pass_key_resp(ledger_ble_data.connection.connection_handle,
ledger_ble_data.pairing_code);
}
#endif // HAVE_INAPP_BLE_PAIRING

static void attribute_modified(const uint8_t *buffer, uint16_t length)
{
Expand Down Expand Up @@ -995,21 +1005,21 @@ void LEDGER_BLE_init(void)
ledger_ble_data.state = BLE_STATE_INITIALIZING;
ledger_ble_data.init_step = BLE_INIT_STEP_IDLE;

memset(&ledger_protocol_data, 0, sizeof(ledger_protocol_data));
ledger_protocol_data.rx_apdu_buffer = ledger_ble_data.apdu_buffer;
ledger_protocol_data.rx_apdu_buffer_max_length = sizeof(ledger_ble_data.apdu_buffer);
memset(&ledger_protocol_data, 0, sizeof(ledger_protocol_data));
ledger_protocol_data.rx_apdu_buffer = ledger_ble_data.apdu_buffer;
ledger_protocol_data.rx_apdu_buffer_max_length = sizeof(ledger_ble_data.apdu_buffer);
#ifdef HAVE_LOCAL_APDU_BUFFER
ledger_protocol_data.rx_dst_buffer = NULL;
ledger_protocol_data.rx_dst_buffer = NULL;
#else
ledger_protocol_data.rx_dst_buffer = G_io_apdu_buffer;
ledger_protocol_data.rx_dst_buffer = G_io_apdu_buffer;
#endif
LEDGER_PROTOCOL_init(&ledger_protocol_data);
LEDGER_PROTOCOL_init(&ledger_protocol_data);

init_mngr(0, NULL, 0);
}
void LEDGER_BLE_set_recv_buffer(uint8_t * buffer, uint16_t buffer_length) {
ledger_protocol_data.rx_dst_buffer = buffer;
ledger_protocol_data.rx_apdu_buffer_max_length = MIN(buffer_length, sizeof(ledger_ble_data.apdu_buffer));
ledger_protocol_data.rx_dst_buffer = buffer;
ledger_protocol_data.rx_apdu_buffer_max_length = MIN(buffer_length, sizeof(ledger_ble_data.apdu_buffer));
}


Expand Down Expand Up @@ -1051,7 +1061,9 @@ void LEDGER_BLE_receive(const uint8_t* spi_buffer)
ledger_ble_data.connection.encrypted = 0;
ledger_ble_data.transfer_mode_enable = 0;
G_io_app.transfer_mode = 0;
#ifdef HAVE_INAPP_BLE_PAIRING
end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_FAILED);
#endif // HAVE_INAPP_BLE_PAIRING
if (spi_buffer[9] != 0x28) { // Error code : Instant Passed
start_advertising();
}
Expand Down Expand Up @@ -1146,15 +1158,15 @@ int hci_send_req(struct hci_request *p_cmd, uint8_t async)

uint16_t opcode = ((p_cmd->ocf) & 0x03ff) | ((p_cmd->ogf) << 10);

uint8_t tag_buffer[5];
tag_buffer[0] = SEPROXYHAL_TAG_BLE_SEND;
tag_buffer[1] = (p_cmd->clen+2)>>8;
tag_buffer[2] = (p_cmd->clen+2);
tag_buffer[3] = opcode >>8;
tag_buffer[4] = opcode;
io_seproxyhal_spi_send(tag_buffer, 5);
io_seproxyhal_spi_send(p_cmd->cparam, p_cmd->clen);
return 0;
uint8_t tag_buffer[5];
tag_buffer[0] = SEPROXYHAL_TAG_BLE_SEND;
tag_buffer[1] = (p_cmd->clen+2)>>8;
tag_buffer[2] = (p_cmd->clen+2);
tag_buffer[3] = opcode >>8;
tag_buffer[4] = opcode;
io_seproxyhal_spi_send(tag_buffer, 5);
io_seproxyhal_spi_send(p_cmd->cparam, p_cmd->clen);
return 0;
}

void BLE_power(unsigned char powered, const char *discovered_name)
Expand Down

0 comments on commit 0d278e4

Please sign in to comment.