Skip to content

ble: Wait with setting the product string to after orientation #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ bootloader-btc: | build
$(MAKE) -C build bb02-bl-btconly.elf
bootloader-btc-development: | build
$(MAKE) -C build bb02-bl-btconly-development.elf
bootloader-btc-plus-development: | build
$(MAKE) -C build bb02p-bl-btconly-development.elf
bootloader-btc-production: | build
$(MAKE) -C build bb02-bl-btconly-production.elf
factory-setup: | build
Expand Down Expand Up @@ -113,6 +115,8 @@ jlink-flash-bootloader-development: | build
JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi-development.jlink
jlink-flash-bootloader-plus-development: | build
JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-multi-development.jlink
jlink-flash-bootloader-btc-plus-development: | build
JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-btconly-development.jlink
jlink-flash-bootloader-development-locked: | build
JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi-development-locked.jlink
jlink-flash-bootloader: | build
Expand Down
9 changes: 4 additions & 5 deletions src/bootloader/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,12 @@ static size_t _api_command(const uint8_t* input, uint8_t* output, const size_t m

case OP_REBOOT: {
#if PLATFORM_BITBOX02PLUS == 1
// da14531_set_product("", 0, &uart_write_queue);
da14531_reset(&uart_write_queue);
// Send it now, because we are about to reset ourselves
da14531_set_product(NULL, 0, &uart_write_queue);
// Send it now, because we are about to reset ourselves
while (ringbuffer_num(&uart_write_queue)) {
uart_poll(NULL, 0, NULL, &uart_write_queue);
}
#endif

_api_reboot();
} break;

Expand Down Expand Up @@ -1046,7 +1044,8 @@ void bootloader_jump(void)
util_log("Not jumping to firmware");
_compute_is_app_flash_empty();
bootloader_render_default_screen();
if (usb_start(_api_setup) != ERR_NONE) {
_api_setup();
if (usb_start() != ERR_NONE) {
_render_message("Failed to initialize USB", 0);
}
}
18 changes: 16 additions & 2 deletions src/bootloader/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "bootloader.h"
#include "mpu_regions.h"
#include <bootloader/bootloader_version.h>
#include <driver_init.h>
#include <hardfault.h>
#include <platform_config.h>
Expand All @@ -34,7 +35,15 @@
#include <da14531/da14531_protocol.h>
#include <uart.h>
#include <utils_ringbuffer.h>

#if PRODUCT_BITBOX_PLUS_MULTI == 1
#define DEVICE_MODE "{\"p\":\"bb02p-bl-multi\",\"v\":\"" BOOTLOADER_VERSION "\"}"
#elif PRODUCT_BITBOX_PLUS_BTCONLY == 1
#define DEVICE_MODE "{\"p\":\"bb02p-bl-btconly\",\"v\":\"" BOOTLOADER_VERSION "\"}"
#else
#error "unknown product"
#endif
#endif // PLATFORM_BITBOX02PLUS == 1

extern void __attribute__((noreturn)) __stack_chk_fail(void);
void __attribute__((noreturn)) __stack_chk_fail(void)
Expand Down Expand Up @@ -85,10 +94,15 @@ int main(void)
uint16_t uart_read_buf_len = 0;

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

// Set product to bootloader string, this is necessary if we have rebooted from firmware. Must
// be done after usb_processing is initalized to avoid getting request from the app to early.
da14531_handler_current_product = (const uint8_t*)DEVICE_MODE;
da14531_handler_current_product_len = sizeof(DEVICE_MODE) - 1;
da14531_set_product(
da14531_handler_current_product, da14531_handler_current_product_len, &uart_write_queue);

da14531_protocol_init();
#endif
usb_processing_init();
Expand Down
9 changes: 7 additions & 2 deletions src/da14531/da14531.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ void da14531_power_down(struct ringbuffer* uart_out)
}
}

void da14531_set_product(const char* product, uint16_t product_len, struct ringbuffer* uart_out)
void da14531_set_product(
volatile const uint8_t* product,
volatile uint16_t product_len,
struct ringbuffer* uart_out)
{
uint8_t payload[64] = {0};
payload[0] = CTRL_CMD_PRODUCT_STRING;
memcpy(&payload[1], product, product_len);
for (int i = 0; i < product_len; i++) {
payload[1 + i] = product[i];
}
uint8_t tmp[128];
uint16_t tmp_len = da14531_protocol_format(
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, &payload[0], 1 + product_len);
Expand Down
7 changes: 5 additions & 2 deletions src/da14531/da14531.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ void da14531_power_down(struct ringbuffer* uart_out);

void da14531_reset(struct ringbuffer* uart_out);

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

#endif
13 changes: 7 additions & 6 deletions src/da14531/da14531_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include <ui/components/ui_images.h>
#include <ui/fonts/monogram_5X9.h>

// These are set from interrupt context in the orientation workflow :/ therefore they need to be
// volatile
volatile const uint8_t* da14531_handler_current_product = NULL;
volatile uint16_t da14531_handler_current_product_len = 0;

struct da14531_ctrl_frame {
enum da14531_protocol_packet_type type;
uint16_t payload_length; // includes length of cmd
Expand Down Expand Up @@ -205,12 +210,8 @@ static void _ctrl_handler(struct da14531_ctrl_frame* frame, struct ringbuffer* q
} break;
case CTRL_CMD_PRODUCT_STRING: {
// util_log("da14531: get device mode");
#if defined(BOOTLOADER)
#define DEVICE_MODE "{\"p\":\"bb02p-bl-multi\",\"v\":\"1.1.0\"}"
#else
#define DEVICE_MODE "{\"p\":\"bb02p-multi\",\"v\":\"9.22.0\"}"
#endif
da14531_set_product(DEVICE_MODE, sizeof(DEVICE_MODE) - 1, queue);
da14531_set_product(
da14531_handler_current_product, da14531_handler_current_product_len, queue);
} break;
case CTRL_CMD_IDENTITY_ADDRESS: {
// util_log("da14531: get addr");
Expand Down
3 changes: 3 additions & 0 deletions src/da14531/da14531_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "da14531_protocol.h"
#include <utils_ringbuffer.h>

extern volatile const uint8_t* da14531_handler_current_product;
extern volatile uint16_t da14531_handler_current_product_len;

void da14531_handler(struct da14531_protocol_frame* frame, struct ringbuffer* queue);

#endif
7 changes: 6 additions & 1 deletion src/da14531/da14531_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ static struct da14531_protocol_frame* _serial_link_in_poll(
case SERIAL_LINK_STATE_READING: {
int len = self->buf_in_len;
for (int i = 0; i < len; i++) {
// util_log("i:%d,b:%02x,l:%u", i, self->buf_in[i], self->buf_in_len);
// util_log(
// "i:%d,b:%02x,l:%u,h:%s",
// i,
// self->buf_in[i],
// self->buf_in_len,
// util_dbg_hex(&self->frame[0], 10));
self->buf_in_len--;
// Reset firmware loader on STX
if (self->buf_in[i] == STX) {
Expand Down
12 changes: 4 additions & 8 deletions src/firmware_main_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,17 @@

void firmware_main_loop(void)
{
// This starts the async orientation screen workflow, which is processed by the loop below.
orientation_screen();

// TODO: Send out new BLE product string, so app sees that we are booted
// Set it to the size of the ringbuffer in the UART driver so we can read out all bytes
// Set the size of uart_read_buf to the size of the ringbuffer in the UART driver so we can read
// out all bytes
uint8_t uart_read_buf[USART_0_BUFFER_SIZE] = {0};
uint16_t uart_read_buf_len = 0;

struct ringbuffer uart_write_queue;
uint8_t uart_write_buf[UART_OUT_BUF_LEN];
ringbuffer_init(&uart_write_queue, &uart_write_buf, UART_OUT_BUF_LEN);

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

const uint8_t* hww_data = NULL;
uint8_t hww_frame[USB_REPORT_SIZE] = {0};
Expand Down
2 changes: 1 addition & 1 deletion src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void _ble_clear_product(void)
struct ringbuffer uart_queue;
uint8_t uart_queue_buf[64];
ringbuffer_init(&uart_queue, &uart_queue_buf[0], sizeof(uart_queue_buf));
da14531_set_product("", 0, &uart_queue);
da14531_set_product(NULL, 0, &uart_queue);
while (ringbuffer_num(&uart_queue)) {
#ifndef TESTING
uart_poll(NULL, 0, NULL, &uart_queue);
Expand Down
12 changes: 3 additions & 9 deletions src/usb/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "usb_desc_bitbox02plus.h"
#include "usb_size.h"
#include "usbdc.h"
#include <da14531/da14531.h>
#include <da14531/da14531_handler.h>
#include <memory/memory_shared.h>
#if APP_U2F == 1
#include "u2f.h"
Expand Down Expand Up @@ -46,7 +48,6 @@ static uint8_t _descriptor_bytes_bitbox02plus[] = {
static struct usbd_descriptors _descriptor_bitbox02plus[] = {
{_descriptor_bytes_bitbox02plus,
_descriptor_bytes_bitbox02plus + sizeof(_descriptor_bytes_bitbox02plus)}};
static void (*_on_hww_init)(void) = NULL;
static void _hww_endpoint_available(void);
#if APP_U2F == 1
static void _u2f_endpoint_available(void);
Expand All @@ -58,9 +59,6 @@ static void _hww_endpoint_available(void)
if (!hid_hww_is_enabled()) {
return;
}
if (_on_hww_init != NULL) {
_on_hww_init();
}
hid_hww_setup();
}

Expand All @@ -71,7 +69,6 @@ static void _u2f_endpoint_available(void)
if (!hid_u2f_is_enabled()) {
return;
};
u2f_device_setup();
hid_u2f_setup();
}
#endif
Expand All @@ -87,7 +84,7 @@ static void _timeout_cb(const struct timer_task* const timer_task)

static bool _usb_enabled = false;

int32_t usb_start(void (*on_hww_init)(void))
int32_t usb_start(void)
{
#if !defined(TESTING) && APP_U2F == 1
static struct timer_task Timer_task;
Expand All @@ -105,7 +102,6 @@ int32_t usb_start(void (*on_hww_init)(void))
if (ret != 0) {
return ret;
}
_on_hww_init = on_hww_init;
ret = hid_hww_init(_hww_endpoint_available);
if (ret != 0) {
return ret;
Expand All @@ -125,8 +121,6 @@ int32_t usb_start(void (*on_hww_init)(void))
break;
}
usbdc_attach();
#else
(void)on_hww_init;
#endif
_usb_enabled = true;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/usb/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Start the USB HID interfaces.
*/
int32_t usb_start(void (*on_hww_init)(void));
int32_t usb_start(void);

/**
* Stop the USB interfaces.
Expand Down
56 changes: 54 additions & 2 deletions src/workflow/orientation_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,71 @@
#include <hal_timer.h>
#include <platform/driver_init.h>
#endif
#include <da14531/da14531.h>
#include <da14531/da14531_handler.h>
#include <hww.h>
#include <memory/memory_shared.h>
#include <screen.h>
#include <ui/components/lockscreen.h>
#include <ui/components/orientation_arrows.h>
#include <ui/screen_stack.h>
#include <usb/usb.h>
#include <utils_ringbuffer.h>
#include <version.h>

#if APP_U2F
#include <u2f.h>
#endif

#ifndef TESTING
#define IDLE_PERIOD_MS 1300

// Currently we have one firmware for both BB02 and BB02_PLUS, and only the
// PRODUCT_BITBOX_MULTI/BTCONLY definitions apply. The PRODUCT_BITBOX_PLUS_MULTI/BTCONLY defs
// currently only apply in the bootloader, which we don't need here.
#if PRODUCT_BITBOX_MULTI == 1
#define PRODUCT_STRING_SUFFIX "multi"
#elif PRODUCT_BITBOX_BTCONLY == 1
#define PRODUCT_STRING_SUFFIX "btconly"
#elif PRODUCT_BITBOX02_FACTORYSETUP == 1
// Dummy, not actually needed, but this file is currently needlessly compiled for factorysetup.
#define PRODUCT_STRING_SUFFIX "factory"
#else
#error "unknown edition"
#endif

#define DEVICE_MODE \
"{\"p\":\"bb02p-" PRODUCT_STRING_SUFFIX "\",\"v\":\"" DIGITAL_BITBOX_VERSION "\"}"

static struct timer_task _idle_timer_task = {0};

struct select_orientation_data {
struct ringbuffer* uart_out_queue;
};

static struct select_orientation_data _data = {0};

static void _idle_timer_cb(const struct timer_task* const timer_task)
{
(void)timer_task;
usb_start(hww_setup);

// Setup usb_processing handlers
hww_setup();
#if APP_U2F
u2f_device_setup();
#endif

// hww handler in usb_process must be setup before we can allow ble connections
if (memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS) {
da14531_handler_current_product = (const uint8_t*)DEVICE_MODE;
da14531_handler_current_product_len = sizeof(DEVICE_MODE) - 1;
da14531_set_product(
da14531_handler_current_product,
da14531_handler_current_product_len,
_data.uart_out_queue);
}

usb_start();
ui_screen_stack_push(lockscreen_create());
}
#endif
Expand All @@ -57,7 +106,10 @@ static void _select_orientation_done(bool upside_down, void* cb_param)
#endif
}

void orientation_screen(void)
void orientation_screen(struct ringbuffer* uart_out_queue)
{
#ifndef TESTING
_data.uart_out_queue = uart_out_queue;
#endif
ui_screen_stack_push(orientation_arrows_create(_select_orientation_done, NULL));
}
4 changes: 3 additions & 1 deletion src/workflow/orientation_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef __ORIENTATION_SCREEN_H
#define __ORIENTATION_SCREEN_H

void orientation_screen(void);
#include <utils_ringbuffer.h>

void orientation_screen(struct ringbuffer* uart_out_queue);

#endif // __ORIENTATION_SCREEN_H