Skip to content

Commit 7b3e13c

Browse files
committed
Merge branch 'is_ble' into staging-ble
2 parents f71b22f + 7357e99 commit 7b3e13c

File tree

10 files changed

+93
-8
lines changed

10 files changed

+93
-8
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
set(DBB-FIRMWARE-SOURCES
1818
${CMAKE_SOURCE_DIR}/src/bip32.c
19+
${CMAKE_SOURCE_DIR}/src/communication_mode.c
1920
${CMAKE_SOURCE_DIR}/src/firmware_main_loop.c
2021
${CMAKE_SOURCE_DIR}/src/keystore.c
2122
${CMAKE_SOURCE_DIR}/src/random.c

src/communication_mode.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "communication_mode.h"
16+
17+
#include "memory/memory_shared.h"
18+
19+
static bool _usb_hww_request_seen = false;
20+
21+
void communication_mode_ble_disable(void)
22+
{
23+
_usb_hww_request_seen = true;
24+
}
25+
26+
static bool _has_ble(void)
27+
{
28+
static bool has_ble;
29+
static bool has_ble_initialized = false;
30+
if (!has_ble_initialized) {
31+
has_ble = memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS;
32+
has_ble_initialized = true;
33+
}
34+
return has_ble;
35+
}
36+
37+
bool communication_mode_ble_enabled(void)
38+
{
39+
return !_usb_hww_request_seen && _has_ble();
40+
}

src/communication_mode.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _FIRMWARE_COMMUNICATION_MODE_H_
16+
#define _FIRMWARE_COMMUNICATION_MODE_H_
17+
18+
#include <stdbool.h>
19+
20+
/**
21+
* Call this when the first USB request is seen. After this, `communication_mode_ble_enabled()` will
22+
* be false even on Bluetooth enabled devices (USB takes priority).
23+
*/
24+
void communication_mode_ble_disable(void);
25+
26+
/**
27+
* Returns true if this device is Bluetooth-enabled and we have not seen a USB request yet, which
28+
* means we are communicating via Bluetooth.
29+
*/
30+
bool communication_mode_ble_enabled(void);
31+
#endif

src/firmware_main_loop.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "firmware_main_loop.h"
1616

17+
#include "communication_mode.h"
1718
#include "da14531/da14531.h"
1819
#include "da14531/da14531_handler.h"
1920
#include "da14531/da14531_protocol.h"
@@ -44,15 +45,11 @@
4445
// Must be power of 2
4546
#define UART_OUT_BUF_LEN 2048
4647

47-
static bool _usb_hww_request_seen = false;
48-
4948
void firmware_main_loop(void)
5049
{
5150
// This starts the async orientation screen workflow, which is processed by the loop below.
5251
orientation_screen();
5352

54-
bool has_ble = memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS;
55-
5653
// TODO: Send out new BLE product string, so app sees that we are booted
5754
// Set it to the size of the ringbuffer in the UART driver so we can read out all bytes
5855
uint8_t uart_read_buf[USART_0_BUFFER_SIZE] = {0};
@@ -77,7 +74,7 @@ void firmware_main_loop(void)
7774

7875
while (1) {
7976
// Do UART I/O
80-
if (has_ble && !_usb_hww_request_seen) {
77+
if (communication_mode_ble_enabled()) {
8178
if (uart_read_buf_len < sizeof(uart_read_buf) ||
8279
ringbuffer_num(&uart_write_queue) > 0) {
8380
uart_poll(
@@ -105,14 +102,14 @@ void firmware_main_loop(void)
105102
// Do USB Input
106103
if (!hww_data && hid_hww_read(&hww_frame[0])) {
107104
usb_packet_process((const USB_FRAME*)hww_frame);
108-
if (has_ble && !_usb_hww_request_seen) {
105+
if (communication_mode_ble_enabled()) {
109106
// Enqueue a power down command to the da14531
110107
da14531_power_down(&uart_write_queue);
111108
// Flush out the power down command. This will be the last UART communication we do.
112109
while (ringbuffer_num(&uart_write_queue) > 0) {
113110
uart_poll(NULL, 0, NULL, &uart_write_queue);
114111
}
115-
_usb_hww_request_seen = true;
112+
communication_mode_ble_disable();
116113
}
117114
}
118115
#if APP_U2F == 1
@@ -122,7 +119,7 @@ void firmware_main_loop(void)
122119
#endif
123120

124121
// Do UART Output
125-
if (has_ble && !_usb_hww_request_seen) {
122+
if (communication_mode_ble_enabled()) {
126123
struct da14531_protocol_frame* frame = da14531_protocol_poll(
127124
&uart_read_buf[0], &uart_read_buf_len, hww_data, &uart_write_queue);
128125
// da14531_protocol_poll has consumed the data, clear pointer

src/firmware_main_loop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef _FIRMWARE_MAIN_LOOP_H_
1616
#define _FIRMWARE_MAIN_LOOP_H_
1717

18+
#include <stdbool.h>
19+
1820
/**
1921
* Runs the main UI of the bitbox.
2022
*/

src/rust/bitbox02-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const ALLOWLIST_FNS: &[&str] = &[
149149
"wally_free_string",
150150
"wally_get_secp_context",
151151
"wally_sha512",
152+
"communication_mode_ble_enabled",
152153
];
153154

154155
const RUSTIFIED_ENUMS: &[&str] = &[

src/rust/bitbox02-sys/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <bip32.h>
16+
#include <communication_mode.h>
1617
#include <keystore.h>
1718
#include <memory/bitbox02_smarteeprom.h>
1819
#include <memory/memory.h>

src/rust/bitbox02/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ pub fn sha512(msg: &[u8]) -> [u8; 64] {
251251
result
252252
}
253253

254+
#[cfg(not(feature = "testing"))]
255+
pub fn communication_mode_ble_enabled() -> bool {
256+
unsafe { bitbox02_sys::communication_mode_ble_enabled() }
257+
}
258+
259+
#[cfg(feature = "testing")]
260+
pub fn communication_mode_ble_enabled() -> bool {
261+
false
262+
}
263+
254264
#[cfg(test)]
255265
mod tests {
256266
use super::*;

test/simulator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(DBB-FILTERED-SOURCES
3131

3232
set(IGNORE_SOURCES
3333
"src/screen.c"
34+
"src/firmware_main_loop.c"
3435
"src/memory/nvmctrl.c"
3536
"src/memory/smarteeprom.c"
3637
"src/memory/mpu.c"

test/unit-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(DBB-FILTERED-SOURCES
4747

4848
set(IGNORE_SOURCES
4949
"src/screen.c"
50+
"src/firmware_main_loop.c"
5051
"src/memory/nvmctrl.c"
5152
"src/memory/smarteeprom.c"
5253
"src/memory/mpu.c"

0 commit comments

Comments
 (0)