Skip to content

Commit 3341d06

Browse files
committed
ble: Wait with setting the product string to after orientation
usb_processing wasn't being setup until AFTER usb was enabled. This was a problem beacuse the app is so fast that it manages to send the first requests over ble before usb is setup. Since "usb_processing" is unrelated to the usb driver, we initialize it before we initialize the usb driver.
1 parent e2561b9 commit 3341d06

File tree

13 files changed

+88
-36
lines changed

13 files changed

+88
-36
lines changed

src/bootloader/bootloader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,8 @@ void bootloader_jump(void)
10221022
util_log("Not jumping to firmware");
10231023
_compute_is_app_flash_empty();
10241024
_render_default_screen();
1025-
if (usb_start(_api_setup) != ERR_NONE) {
1025+
_api_setup();
1026+
if (usb_start() != ERR_NONE) {
10261027
_render_message("Failed to initialize USB", 0);
10271028
}
10281029
}

src/bootloader/startup.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ int main(void)
8585
uint16_t uart_read_buf_len = 0;
8686

8787
ringbuffer_init(&uart_write_queue, &uart_write_buf, UART_OUT_BUF_LEN);
88-
#define DEVICE_MODE "{\"p\":\"bb02p-bl-multi\",\"v\":\"1.1.0\"}"
89-
da14531_set_product(DEVICE_MODE, sizeof(DEVICE_MODE) - 1, &uart_write_queue);
9088
bool usb_hww_request_seen = false;
9189

90+
// Set product to bootloader string, this is necessary if we have rebooted from firmware. Must
91+
// be done after usb_processing is initalized to avoid getting request from the app to early.
92+
#define DEVICE_MODE "{\"p\":\"bb02p-bl-multi\",\"v\":\"1.1.0\"}"
93+
da14531_handler_current_product = (const uint8_t*)DEVICE_MODE;
94+
da14531_handler_current_product_len = sizeof(DEVICE_MODE) - 1;
95+
da14531_set_product(
96+
da14531_handler_current_product, da14531_handler_current_product_len, &uart_write_queue);
97+
9298
da14531_protocol_init();
9399
#endif
94100
usb_processing_init();

src/da14531/da14531.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ void da14531_power_down(struct ringbuffer* uart_out)
4545
}
4646
}
4747

48-
void da14531_set_product(const char* product, uint16_t product_len, struct ringbuffer* uart_out)
48+
void da14531_set_product(
49+
volatile const uint8_t* product,
50+
volatile uint16_t product_len,
51+
struct ringbuffer* uart_out)
4952
{
5053
uint8_t payload[64] = {0};
5154
payload[0] = CTRL_CMD_PRODUCT_STRING;
52-
memcpy(&payload[1], product, product_len);
55+
for (int i = 0; i < product_len; i++) {
56+
payload[1 + i] = product[i];
57+
}
5358
uint8_t tmp[128];
5459
uint16_t tmp_len = da14531_protocol_format(
5560
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, &payload[0], 1 + product_len);

src/da14531/da14531.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ void da14531_power_down(struct ringbuffer* uart_out);
3636

3737
void da14531_reset(struct ringbuffer* uart_out);
3838

39-
// product is an array of characters to be set as product characteristic
39+
// product is an array of characters to be set as product characteristic (not null terminated)
4040
// procuct_len is the number of characters in the product array
4141
// uart_out is the queue where to put the outgoing serially encoded bytes
42-
void da14531_set_product(const char* product, uint16_t product_len, struct ringbuffer* uart_out);
42+
void da14531_set_product(
43+
volatile const uint8_t* product,
44+
volatile uint16_t product_len,
45+
struct ringbuffer* uart_out);
4346

4447
#endif

src/da14531/da14531_handler.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include <ui/components/confirm.h>
2525
#include <ui/fonts/monogram_5X9.h>
2626

27+
// These are set from interrupt context in the orientation workflow :/ therefore they need to be
28+
// volatile
29+
volatile const uint8_t* da14531_handler_current_product = NULL;
30+
volatile uint16_t da14531_handler_current_product_len = 0;
31+
2732
struct da14531_ctrl_frame {
2833
enum da14531_protocol_packet_type type;
2934
uint16_t payload_length; // includes length of cmd
@@ -212,12 +217,8 @@ static void _ctrl_handler(struct da14531_ctrl_frame* frame, struct ringbuffer* q
212217
} break;
213218
case CTRL_CMD_PRODUCT_STRING: {
214219
// util_log("da14531: get device mode");
215-
#if defined(BOOTLOADER)
216-
#define DEVICE_MODE "{\"p\":\"bb02p-bl-multi\",\"v\":\"1.1.0\"}"
217-
#else
218-
#define DEVICE_MODE "{\"p\":\"bb02p-multi\",\"v\":\"9.22.0\"}"
219-
#endif
220-
da14531_set_product(DEVICE_MODE, sizeof(DEVICE_MODE) - 1, queue);
220+
da14531_set_product(
221+
da14531_handler_current_product, da14531_handler_current_product_len, queue);
221222
} break;
222223
case CTRL_CMD_IDENTITY_ADDRESS: {
223224
// util_log("da14531: get addr");

src/da14531/da14531_handler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "da14531_protocol.h"
1919
#include <utils_ringbuffer.h>
2020

21+
extern volatile const uint8_t* da14531_handler_current_product;
22+
extern volatile uint16_t da14531_handler_current_product_len;
23+
2124
void da14531_handler(struct da14531_protocol_frame* frame, struct ringbuffer* queue);
2225

2326
#endif

src/da14531/da14531_protocol.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ static struct da14531_protocol_frame* _serial_link_in_poll(
242242
case SERIAL_LINK_STATE_READING: {
243243
int len = self->buf_in_len;
244244
for (int i = 0; i < len; i++) {
245-
// util_log("i:%d,b:%02x,l:%u", i, self->buf_in[i], self->buf_in_len);
245+
// util_log(
246+
// "i:%d,b:%02x,l:%u,h:%s",
247+
// i,
248+
// self->buf_in[i],
249+
// self->buf_in_len,
250+
// util_dbg_hex(&self->frame[0], 10));
246251
self->buf_in_len--;
247252
// Reset firmware loader on STX
248253
if (self->buf_in[i] == STX) {

src/firmware_main_loop.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,17 @@
4747

4848
void firmware_main_loop(void)
4949
{
50-
// This starts the async orientation screen workflow, which is processed by the loop below.
51-
orientation_screen();
52-
53-
// TODO: Send out new BLE product string, so app sees that we are booted
54-
// Set it to the size of the ringbuffer in the UART driver so we can read out all bytes
50+
// Set the size of uart_read_buf to the size of the ringbuffer in the UART driver so we can read
51+
// out all bytes
5552
uint8_t uart_read_buf[USART_0_BUFFER_SIZE] = {0};
5653
uint16_t uart_read_buf_len = 0;
5754

5855
struct ringbuffer uart_write_queue;
5956
uint8_t uart_write_buf[UART_OUT_BUF_LEN];
6057
ringbuffer_init(&uart_write_queue, &uart_write_buf, UART_OUT_BUF_LEN);
6158

62-
// Immediately enqueue the new firmware product string
63-
#define DEVICE_MODE "{\"p\":\"bb02p-multi\",\"v\":\"9.22.0\"}"
64-
da14531_set_product(DEVICE_MODE, sizeof(DEVICE_MODE) - 1, &uart_write_queue);
59+
// This starts the async orientation screen workflow, which is processed by the loop below.
60+
orientation_screen(&uart_write_queue);
6561

6662
const uint8_t* hww_data = NULL;
6763
uint8_t hww_frame[USB_REPORT_SIZE] = {0};

src/system.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void _ble_clear_product(void)
2828
struct ringbuffer uart_queue;
2929
uint8_t uart_queue_buf[64];
3030
ringbuffer_init(&uart_queue, &uart_queue_buf[0], sizeof(uart_queue_buf));
31-
da14531_set_product("", 0, &uart_queue);
31+
da14531_set_product(NULL, 0, &uart_queue);
3232
while (ringbuffer_num(&uart_queue)) {
3333
#ifndef TESTING
3434
uart_poll(NULL, 0, NULL, &uart_queue);

src/usb/usb.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "usb_desc_bitbox02plus.h"
2020
#include "usb_size.h"
2121
#include "usbdc.h"
22+
#include <da14531/da14531.h>
23+
#include <da14531/da14531_handler.h>
2224
#include <memory/memory_shared.h>
2325
#if APP_U2F == 1
2426
#include "u2f.h"
@@ -46,7 +48,6 @@ static uint8_t _descriptor_bytes_bitbox02plus[] = {
4648
static struct usbd_descriptors _descriptor_bitbox02plus[] = {
4749
{_descriptor_bytes_bitbox02plus,
4850
_descriptor_bytes_bitbox02plus + sizeof(_descriptor_bytes_bitbox02plus)}};
49-
static void (*_on_hww_init)(void) = NULL;
5051
static void _hww_endpoint_available(void);
5152
#if APP_U2F == 1
5253
static void _u2f_endpoint_available(void);
@@ -58,9 +59,6 @@ static void _hww_endpoint_available(void)
5859
if (!hid_hww_is_enabled()) {
5960
return;
6061
}
61-
if (_on_hww_init != NULL) {
62-
_on_hww_init();
63-
}
6462
hid_hww_setup();
6563
}
6664

@@ -71,7 +69,6 @@ static void _u2f_endpoint_available(void)
7169
if (!hid_u2f_is_enabled()) {
7270
return;
7371
};
74-
u2f_device_setup();
7572
hid_u2f_setup();
7673
}
7774
#endif
@@ -87,7 +84,7 @@ static void _timeout_cb(const struct timer_task* const timer_task)
8784

8885
static bool _usb_enabled = false;
8986

90-
int32_t usb_start(void (*on_hww_init)(void))
87+
int32_t usb_start(void)
9188
{
9289
#if !defined(TESTING) && APP_U2F == 1
9390
static struct timer_task Timer_task;
@@ -105,7 +102,6 @@ int32_t usb_start(void (*on_hww_init)(void))
105102
if (ret != 0) {
106103
return ret;
107104
}
108-
_on_hww_init = on_hww_init;
109105
ret = hid_hww_init(_hww_endpoint_available);
110106
if (ret != 0) {
111107
return ret;
@@ -125,8 +121,6 @@ int32_t usb_start(void (*on_hww_init)(void))
125121
break;
126122
}
127123
usbdc_attach();
128-
#else
129-
(void)on_hww_init;
130124
#endif
131125
_usb_enabled = true;
132126
return 0;

0 commit comments

Comments
 (0)