Skip to content

ble-bootloader: Add device name to default screen #1451

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

Open
wants to merge 1 commit into
base: staging/ble
Choose a base branch
from
Open
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: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ set(QTOUCH-SOURCES ${DRIVER-SOURCES} PARENT_SCOPE)

# The additional files required for the plus platform
set(PLATFORM-BITBOX02-PLUS-SOURCES
${CMAKE_SOURCE_DIR}/src/uart.c
${CMAKE_SOURCE_DIR}/src/communication_mode.c
${CMAKE_SOURCE_DIR}/src/da14531/crc.c
${CMAKE_SOURCE_DIR}/src/da14531/da14531.c
${CMAKE_SOURCE_DIR}/src/da14531/da14531_protocol.c
${CMAKE_SOURCE_DIR}/src/da14531/da14531_handler.c
${CMAKE_SOURCE_DIR}/src/communication_mode.c
${CMAKE_SOURCE_DIR}/src/uart.c
)
set(PLATFORM-BITBOX02-PLUS-SOURCES ${PLATFORM-BITBOX02-PLUS-SOURCES} PARENT_SCOPE)

Expand Down
18 changes: 17 additions & 1 deletion src/bootloader/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <driver_init.h>
#include <flags.h>
#include <memory/memory.h>
#include <memory/memory_shared.h>
#include <memory/nvmctrl.h>
#include <pukcc/curve_p256.h>
Expand All @@ -40,6 +41,7 @@
#endif

#if PLATFORM_BITBOX02PLUS == 1
#include <communication_mode.h>
#include <da14531/da14531.h>
#include <da14531/da14531_protocol.h>
#include <uart.h>
Expand Down Expand Up @@ -303,10 +305,24 @@ void bootloader_render_default_screen(void)
{
UG_ClearBuffer();
_load_logo();
#if PLATFORM_BITBOX02PLUS == 1
if (communication_mode_ble_enabled() &&
da14531_connected_state < DA14531_CONNECTED_CONNECTED_SECURED) {
UG_PutString(0, SCREEN_HEIGHT - 9 * 2 - 5, "See the BitBoxApp", false);
char buf[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
memory_random_name(buf);
UG_PutString(0, SCREEN_HEIGHT - 9, buf, false);
} else {
if (_is_app_flash_empty) {
UG_PutString(0, SCREEN_HEIGHT - 9, "Let's get started!", false);
}
}
#else
if (_is_app_flash_empty) {
UG_PutString(0, SCREEN_HEIGHT - 9 * 2, "Let's get started!", false);
}
UG_PutString(0, SCREEN_HEIGHT - 9, "See the BitBoxApp", false);
#endif
UG_SendBuffer();
}

Expand All @@ -323,7 +339,6 @@ void bootloader_render_ble_confirm_screen(bool confirmed)
snprintf(code_str, sizeof(code_str), "%06u", (unsigned)pairing_code_int);
UG_ClearBuffer();
uint16_t check_width = IMAGE_DEFAULT_CHECKMARK_HEIGHT + IMAGE_DEFAULT_CHECKMARK_HEIGHT / 2 - 1;
UG_FontSelect(&font_font_a_11X10);
if (confirmed) {
UG_PutString(15, 0, "Confirm on app", false);
} else {
Expand All @@ -333,6 +348,7 @@ void bootloader_render_ble_confirm_screen(bool confirmed)
}
UG_FontSelect(&font_monogram_5X9);
UG_PutString(45, SCREEN_HEIGHT / 2 - 9, code_str, false);
UG_FontSelect(&font_font_a_9X9);
UG_SendBuffer();
}
#endif
Expand Down
23 changes: 17 additions & 6 deletions src/bootloader/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
#endif

#if PLATFORM_BITBOX02PLUS == 1
#include <communication_mode.h>
#include <da14531/da14531.h>
#include <da14531/da14531_handler.h>
#include <da14531/da14531_protocol.h>
#include <memory/memory.h>
#include <memory/memory_shared.h>
#include <uart.h>
#include <utils_ringbuffer.h>
Expand Down Expand Up @@ -95,9 +97,8 @@ int main(void)
uint16_t uart_read_buf_len = 0;

ringbuffer_init(&uart_write_queue, &uart_write_buf, UART_OUT_BUF_LEN);
bool usb_hww_request_seen = false;
if (!memory_ble_enabled()) {
usb_hww_request_seen = true;
communication_mode_ble_disable();
}

// Set product to bootloader string, this is necessary if we have rebooted from firmware. Must
Expand All @@ -107,14 +108,23 @@ int main(void)
da14531_set_product(
da14531_handler_current_product, da14531_handler_current_product_len, &uart_write_queue);

// Set device name, the MCU and BLE chip will probably not have the same name after a reset of
// only the MCU.
char buf[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
memory_random_name(buf);
da14531_set_name(buf, strlen(buf), &uart_write_queue);

// Ask for the current conection state
da14531_get_connection_state(&uart_write_queue);

da14531_protocol_init();
#endif
usb_processing_init();

while (1) {
// Do UART I/O
#if PLATFORM_BITBOX02PLUS == 1
if (!usb_hww_request_seen) {
if (communication_mode_ble_enabled()) {
if (uart_read_buf_len < sizeof(uart_read_buf) ||
ringbuffer_num(&uart_write_queue) > 0) {
// screen_sprintf_debug(1000, "uart poll");
Expand All @@ -132,19 +142,20 @@ int main(void)
if (!hww_data && hid_hww_read(&hww_frame[0])) {
usb_packet_process((const USB_FRAME*)hww_frame);
#if PLATFORM_BITBOX02PLUS == 1
if (!usb_hww_request_seen) {
if (communication_mode_ble_enabled()) {
// Enqueue a power down command to the da14531
da14531_power_down(&uart_write_queue);
// Flush out the power down command. This will be the last UART communication we do.
while (ringbuffer_num(&uart_write_queue) > 0) {
uart_poll(NULL, 0, NULL, &uart_write_queue);
}
usb_hww_request_seen = true;
communication_mode_ble_disable();
bootloader_render_default_screen();
}
#endif
}
#if PLATFORM_BITBOX02PLUS == 1
if (!usb_hww_request_seen) {
if (communication_mode_ble_enabled()) {
struct da14531_protocol_frame* frame = da14531_protocol_poll(
&uart_read_buf[0], &uart_read_buf_len, hww_data, &uart_write_queue);
// da14531_protocol_poll has consumed the data, clear pointer
Expand Down
30 changes: 30 additions & 0 deletions src/da14531/da14531.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "util.h"
#include "utils_ringbuffer.h"

enum da14531_connected_state da14531_connected_state = DA14531_CONNECTED_ADVERTISING;

void da14531_reset(struct ringbuffer* uart_out)
{
util_log("da14531_reset");
Expand Down Expand Up @@ -64,3 +66,31 @@ void da14531_set_product(
ringbuffer_put(uart_out, tmp[i]);
}
}

void da14531_set_name(const char* name, size_t name_len, struct ringbuffer* uart_out)
{
uint8_t payload[64] = {0};
payload[0] = CTRL_CMD_DEVICE_NAME;
memcpy(&payload[1], name, name_len);
uint8_t tmp[128];
uint16_t tmp_len = da14531_protocol_format(
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, &payload[0], 1 + name_len);
ASSERT(tmp_len <= sizeof(tmp));
ASSERT(ringbuffer_num(uart_out) + tmp_len <= uart_out->size);
for (int i = 0; i < tmp_len; i++) {
ringbuffer_put(uart_out, tmp[i]);
}
}

void da14531_get_connection_state(struct ringbuffer* uart_out)
{
uint8_t payload = CTRL_CMD_BLE_STATUS;
uint8_t tmp[16];
uint16_t tmp_len = da14531_protocol_format(
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, &payload, 1);
ASSERT(tmp_len <= sizeof(tmp));
ASSERT(ringbuffer_num(uart_out) + tmp_len <= uart_out->size);
for (int i = 0; i < tmp_len; i++) {
ringbuffer_put(uart_out, tmp[i]);
}
}
12 changes: 12 additions & 0 deletions src/da14531/da14531.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#define CTRL_CMD_BLE_POWER_DOWN 12
#define CTRL_CMD_DEBUG 254

enum da14531_connected_state {
DA14531_CONNECTED_ADVERTISING = 0,
DA14531_CONNECTED_CONNECTED = 1,
DA14531_CONNECTED_CONNECTED_SECURED = 2,
};

extern enum da14531_connected_state da14531_connected_state;

void da14531_power_down(struct ringbuffer* uart_out);

void da14531_reset(struct ringbuffer* uart_out);
Expand All @@ -44,4 +52,8 @@ void da14531_set_product(
volatile uint16_t product_len,
struct ringbuffer* uart_out);

void da14531_set_name(const char* name, size_t name_len, struct ringbuffer* uart_out);

void da14531_get_connection_state(struct ringbuffer* uart_out);

#endif
3 changes: 3 additions & 0 deletions src/da14531/da14531_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ static void _ctrl_handler(struct da14531_ctrl_frame* frame, struct ringbuffer* q
#endif
} break;
case CTRL_CMD_BLE_STATUS:
if (frame->cmd_data[0] < 3) {
da14531_connected_state = frame->cmd_data[0];
}
// util_log("da14531: BLE status update");
#if defined(BOOTLOADER)
bootloader_pairing_request = false;
Expand Down