Skip to content

Commit

Permalink
fix(ble): remove globals usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yhql committed Aug 22, 2023
1 parent d1a25c7 commit f63ffe2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
2 changes: 2 additions & 0 deletions include/ledger_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef struct ledger_protocol_s {
uint16_t rx_apdu_length;
uint16_t rx_apdu_offset;

uint8_t *rx_dst_buffer;

uint16_t mtu;
uint8_t mtu_negotiated;
} ledger_protocol_t;
Expand Down
3 changes: 2 additions & 1 deletion lib_blewbxx_impl/include/ledger_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
/* Exported functions prototypes--------------------------------------------- */
void LEDGER_BLE_init(void);
void LEDGER_BLE_send(const uint8_t* packet, uint16_t packet_length);
void LEDGER_BLE_receive(void);
void LEDGER_BLE_receive(const uint8_t* spi_buffer);
void LEDGER_BLE_set_recv_buffer(uint8_t * buffer, uint16_t buffer_length);
void LEDGER_BLE_enable_advertising(uint8_t enable);
void LEDGER_BLE_reset_pairings(void);

Expand Down
106 changes: 52 additions & 54 deletions lib_blewbxx_impl/src/ledger_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ static ledger_protocol_t ledger_protocol_data;
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;
}
}

static void get_device_name(void)
{
memset(ledger_ble_data.device_name, 0,
Expand Down Expand Up @@ -505,13 +520,7 @@ static void hci_evt_cmd_complete(const uint8_t *buffer, uint16_t length)
if ( (ledger_protocol_data.rx_apdu_length)
&&(ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE)
) {
memcpy(G_io_apdu_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;
copy_apdu_to_app(false);
}
}
else {
Expand Down Expand Up @@ -854,14 +863,7 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
LOG_BLE("Transfer failed 0x%04x\n", U2BE(ledger_ble_data.resp, 0));
G_io_app.transfer_mode = 0;
check_transfer_mode(G_io_app.transfer_mode);
memcpy(G_io_apdu_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;
ledger_protocol_data.rx_apdu_length = 0;
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
G_io_app.apdu_media = IO_APDU_MEDIA_BLE;
copy_apdu_to_app(true);
}
else if (ledger_ble_data.resp_length) {
LEDGER_PROTOCOL_tx(ledger_ble_data.resp, ledger_ble_data.resp_length);
Expand All @@ -870,14 +872,7 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
}
}
else {
memcpy(G_io_apdu_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;
ledger_protocol_data.rx_apdu_length = 0;
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
G_io_app.apdu_media = IO_APDU_MEDIA_BLE;
copy_apdu_to_app(true);
}
}
else if (ledger_protocol_data.tx_chunk_length >= 2) {
Expand Down Expand Up @@ -915,14 +910,7 @@ static void write_permit_request(const uint8_t *buffer, uint16_t length)
data_length,
&buffer[3]);
if (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) {
memcpy(G_io_apdu_buffer,
ledger_protocol_data.rx_apdu_buffer,
ledger_protocol_data.rx_apdu_length);
G_io_app.apdu_length = ledger_protocol_data.rx_apdu_length;
ledger_protocol_data.rx_apdu_length = 0;
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
G_io_app.apdu_media = IO_APDU_MEDIA_BLE; // for application code
G_io_app.apdu_state = APDU_BLE; // for next call to io_exchange
copy_apdu_to_app(true);
}
}
else {
Expand Down Expand Up @@ -1027,10 +1015,20 @@ void LEDGER_BLE_init(void)
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;
#else
ledger_protocol_data.rx_dst_buffer = G_io_apdu_buffer;
#endif
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));
}


void LEDGER_BLE_send(const uint8_t* packet, uint16_t packet_length)
{
Expand Down Expand Up @@ -1063,20 +1061,20 @@ void LEDGER_BLE_send(const uint8_t* packet, uint16_t packet_length)
}
}

void LEDGER_BLE_receive(void)
void LEDGER_BLE_receive(const uint8_t* spi_buffer)
{
if (G_io_seproxyhal_spi_buffer[3] == HCI_EVENT_PKT_TYPE) {
switch (G_io_seproxyhal_spi_buffer[4]) {
if (spi_buffer[3] == HCI_EVENT_PKT_TYPE) {
switch (spi_buffer[4]) {

case HCI_DISCONNECTION_COMPLETE_EVT_CODE:
LOG_BLE("HCI DISCONNECTION COMPLETE code %02X\n", G_io_seproxyhal_spi_buffer[9]);
LOG_BLE("HCI DISCONNECTION COMPLETE code %02X\n", spi_buffer[9]);
ledger_ble_data.connection.connection_handle = 0xFFFF;
ledger_ble_data.advertising_enabled = 0;
ledger_ble_data.connection.encrypted = 0;
ledger_ble_data.transfer_mode_enable = 0;
G_io_app.transfer_mode = 0;
end_pairing_ux(BOLOS_UX_ASYNCHMODAL_PAIRING_STATUS_FAILED);
if (G_io_seproxyhal_spi_buffer[9] != 0x28) { // Error code : Instant Passed
if (spi_buffer[9] != 0x28) { // Error code : Instant Passed
start_advertising();
}
else {
Expand All @@ -1086,8 +1084,8 @@ void LEDGER_BLE_receive(void)
break;

case HCI_ENCRYPTION_CHANGE_EVT_CODE:
if (U2LE(G_io_seproxyhal_spi_buffer, 7) == ledger_ble_data.connection.connection_handle) {
if (G_io_seproxyhal_spi_buffer[9]) {
if (U2LE(spi_buffer, 7) == ledger_ble_data.connection.connection_handle) {
if (spi_buffer[9]) {
LOG_BLE("Link encrypted\n");
ledger_ble_data.connection.encrypted = 1;
}
Expand All @@ -1097,14 +1095,14 @@ void LEDGER_BLE_receive(void)
}
}
else {
LOG_BLE("HCI ENCRYPTION CHANGE EVT %d on connection handle \n", G_io_seproxyhal_spi_buffer[9],
U2LE(G_io_seproxyhal_spi_buffer, 7));
LOG_BLE("HCI ENCRYPTION CHANGE EVT %d on connection handle \n", spi_buffer[9],
U2LE(spi_buffer, 7));
}
break;

case HCI_COMMAND_COMPLETE_EVT_CODE:
hci_evt_cmd_complete(&G_io_seproxyhal_spi_buffer[6],
G_io_seproxyhal_spi_buffer[5]);
hci_evt_cmd_complete(&spi_buffer[6],
spi_buffer[5]);
break;

case HCI_COMMAND_STATUS_EVT_CODE:
Expand All @@ -1116,13 +1114,13 @@ void LEDGER_BLE_receive(void)
break;

case HCI_LE_META_EVT_CODE:
hci_evt_le_meta_evt(&G_io_seproxyhal_spi_buffer[6],
G_io_seproxyhal_spi_buffer[5]);
hci_evt_le_meta_evt(&spi_buffer[6],
spi_buffer[5]);
break;

case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
hci_evt_vendor(&G_io_seproxyhal_spi_buffer[6],
G_io_seproxyhal_spi_buffer[5]);
hci_evt_vendor(&spi_buffer[6],
spi_buffer[5]);
break;

default:
Expand Down Expand Up @@ -1173,14 +1171,14 @@ int hci_send_req(struct hci_request *p_cmd, uint8_t async)

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

G_io_seproxyhal_spi_buffer[0] = SEPROXYHAL_TAG_BLE_SEND;
G_io_seproxyhal_spi_buffer[1] = (p_cmd->clen+2)>>8;
G_io_seproxyhal_spi_buffer[2] = (p_cmd->clen+2);
G_io_seproxyhal_spi_buffer[3] = opcode >>8;
G_io_seproxyhal_spi_buffer[4] = opcode;
memmove(G_io_seproxyhal_spi_buffer+5, p_cmd->cparam, p_cmd->clen);
io_seproxyhal_spi_send(G_io_seproxyhal_spi_buffer, 3+p_cmd->clen+2);

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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/os_io_seproxyhal.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ unsigned int io_seproxyhal_handle_event(void) {

#ifdef HAVE_BLE
case SEPROXYHAL_TAG_BLE_RECV_EVENT:
LEDGER_BLE_receive();
LEDGER_BLE_receive(G_io_seproxyhal_spi_buffer);
return 1;
#endif // HAVE_BLE

Expand Down

0 comments on commit f63ffe2

Please sign in to comment.