Skip to content

Commit f106e2e

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 f106e2e

13 files changed

+77
-35
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ 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(const uint8_t* product, uint16_t product_len, struct ringbuffer* uart_out)
4949
{
5050
uint8_t payload[64] = {0};
5151
payload[0] = CTRL_CMD_PRODUCT_STRING;

src/da14531/da14531.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ 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(const uint8_t* product, uint16_t product_len, struct ringbuffer* uart_out);
4343

4444
#endif

src/da14531/da14531_handler.c

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

27+
const uint8_t* da14531_handler_current_product = NULL;
28+
uint8_t da14531_handler_current_product_len = 0;
29+
2730
struct da14531_ctrl_frame {
2831
enum da14531_protocol_packet_type type;
2932
uint16_t payload_length; // includes length of cmd
@@ -212,12 +215,8 @@ static void _ctrl_handler(struct da14531_ctrl_frame* frame, struct ringbuffer* q
212215
} break;
213216
case CTRL_CMD_PRODUCT_STRING: {
214217
// 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);
218+
da14531_set_product(
219+
da14531_handler_current_product, da14531_handler_current_product_len, queue);
221220
} break;
222221
case CTRL_CMD_IDENTITY_ADDRESS: {
223222
// 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 const uint8_t* da14531_handler_current_product;
22+
extern uint8_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;

src/usb/usb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Start the USB HID interfaces.
2323
*/
24-
int32_t usb_start(void (*on_hww_init)(void));
24+
int32_t usb_start(void);
2525

2626
/**
2727
* Stop the USB interfaces.

src/workflow/orientation_screen.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,55 @@
1919
#include <hal_timer.h>
2020
#include <platform/driver_init.h>
2121
#endif
22+
#include <da14531/da14531.h>
23+
#include <da14531/da14531_handler.h>
2224
#include <hww.h>
25+
#include <memory/memory_shared.h>
2326
#include <screen.h>
2427
#include <ui/components/lockscreen.h>
2528
#include <ui/components/orientation_arrows.h>
2629
#include <ui/screen_stack.h>
2730
#include <usb/usb.h>
31+
#include <utils_ringbuffer.h>
32+
33+
#if APP_U2F
34+
#include <u2f.h>
35+
#endif
2836

2937
#ifndef TESTING
3038
#define IDLE_PERIOD_MS 1300
3139

40+
#define DEVICE_MODE "{\"p\":\"bb02p-multi\",\"v\":\"9.22.0\"}"
41+
3242
static struct timer_task _idle_timer_task = {0};
3343

44+
struct select_orientation_data {
45+
struct ringbuffer* uart_out_queue;
46+
};
47+
48+
static struct select_orientation_data _data = {0};
49+
3450
static void _idle_timer_cb(const struct timer_task* const timer_task)
3551
{
3652
(void)timer_task;
37-
usb_start(hww_setup);
53+
54+
// Setup usb_processing handlers
55+
hww_setup();
56+
#if APP_U2F
57+
u2f_device_setup();
58+
#endif
59+
60+
// hww handler in usb_process must be setup before we can allow ble connections
61+
if (memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS) {
62+
da14531_handler_current_product = (const uint8_t*)DEVICE_MODE;
63+
da14531_handler_current_product_len = sizeof(DEVICE_MODE) - 1;
64+
da14531_set_product(
65+
da14531_handler_current_product,
66+
da14531_handler_current_product_len,
67+
_data.uart_out_queue);
68+
}
69+
70+
usb_start();
3871
ui_screen_stack_push(lockscreen_create());
3972
}
4073
#endif
@@ -57,7 +90,10 @@ static void _select_orientation_done(bool upside_down, void* cb_param)
5790
#endif
5891
}
5992

60-
void orientation_screen(void)
93+
void orientation_screen(struct ringbuffer* uart_out_queue)
6194
{
95+
#ifndef TESTING
96+
_data.uart_out_queue = uart_out_queue;
97+
#endif
6298
ui_screen_stack_push(orientation_arrows_create(_select_orientation_done, NULL));
6399
}

src/workflow/orientation_screen.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef __ORIENTATION_SCREEN_H
1616
#define __ORIENTATION_SCREEN_H
1717

18-
void orientation_screen(void);
18+
#include <utils_ringbuffer.h>
19+
20+
void orientation_screen(struct ringbuffer* uart_out_queue);
1921

2022
#endif // __ORIENTATION_SCREEN_H

0 commit comments

Comments
 (0)