Skip to content

Commit 1d52702

Browse files
committed
Merge branch 'lockscreen_name'
2 parents a4aca07 + 75c079f commit 1d52702

File tree

8 files changed

+146
-6
lines changed

8 files changed

+146
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately.
77
## Firmware
88

99
### [Unreleased]
10+
- Display device name on screen before unlock
1011
- Bitcoin: add support for payment requests
1112
- Bitcoin: allow multisig accounts at arbitrary keypaths
1213
- Bitcoin: allow spendung UTXOs at very high BIP-44 address indices

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ set(DBB-FIRMWARE-UI-SOURCES
8787
${CMAKE_SOURCE_DIR}/src/ui/components/keyboard_switch.c
8888
${CMAKE_SOURCE_DIR}/src/ui/components/orientation_arrows.c
8989
${CMAKE_SOURCE_DIR}/src/ui/components/info_centered.c
90+
${CMAKE_SOURCE_DIR}/src/ui/components/lockscreen.c
9091
${CMAKE_SOURCE_DIR}/src/ui/components/menu.c
9192
${CMAKE_SOURCE_DIR}/src/ui/components/status.c
9293
${CMAKE_SOURCE_DIR}/src/ui/components/image.c

src/memory/memory.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
/********* Definitions and read/write helper functions ****************/
3838

39+
const char* MEMORY_DEFAULT_DEVICE_NAME = "My BitBox";
40+
3941
// Documentation of all appData chunks and their contents. A chunk is defined as
4042
// 16 pages, which is the erase granularity: changing any byte in the page
4143
// involves erases and writing all 16 pages. One page is 512 bytes. The MCU has

src/memory/memory.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ USE_RESULT bool memory_reset_hww(void);
6060
*/
6161
USE_RESULT bool memory_cleanup_smarteeprom(void);
6262

63-
#define MEMORY_DEFAULT_DEVICE_NAME "My BitBox"
63+
// Default device name if no name was set by the user.
64+
extern const char* MEMORY_DEFAULT_DEVICE_NAME;
6465
// Don't change this without proper memory layout migration! (see chunk_1_t in
6566
// memory.c)
6667
#define MEMORY_DEVICE_NAME_MAX_LEN (64)
@@ -69,7 +70,8 @@ USE_RESULT bool memory_cleanup_smarteeprom(void);
6970
// size (including the null terminator) is MEMORY_DEVICE_NAME_MAX_LEN bytes.
7071
USE_RESULT bool memory_set_device_name(const char* name);
7172

72-
// name_out must have MEMORY_DEVICE_NAME_MAX_LEN bytes in size.
73+
// name_out must have MEMORY_DEVICE_NAME_MAX_LEN bytes in size. Returns `MEMORY_DEFAULT_DEVICE_NAME`
74+
// if no device name is set.
7375
void memory_get_device_name(char* name_out);
7476

7577
/**

src/ui/components/info_centered.c

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

1515
#include "info_centered.h"
16-
#include "../event.h"
16+
#include "../ui_util.h"
1717
#include "button.h"
1818
#include "label.h"
1919

2020
#include <hardfault.h>
2121
#include <screen.h>
22-
#include <touch/gestures.h>
2322

2423
#include <string.h>
2524

src/ui/components/lockscreen.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2021 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+
#include "lockscreen.h"
16+
#include "../ui_util.h"
17+
#include "label.h"
18+
19+
#include <hardfault.h>
20+
#include <memory/memory.h>
21+
#include <screen.h>
22+
#include <string.h>
23+
#include <touch/gestures.h>
24+
#include <ui/fonts/arial_fonts.h>
25+
26+
/********************************** Component Functions **********************************/
27+
28+
/**
29+
* Collects all component functions.
30+
*/
31+
static const component_functions_t _component_functions = {
32+
.cleanup = ui_util_component_cleanup,
33+
.render = ui_util_component_render_subcomponents,
34+
.on_event = ui_util_on_event_noop,
35+
};
36+
37+
/********************************** Create Instance **********************************/
38+
39+
// Outputs `in` as is if it can be rendered to fit in `max_width`.
40+
// If it can't, it is truncated (with appended "...") to a size where it fits.
41+
static void _truncate_to_fit(
42+
const char* in,
43+
char* out,
44+
size_t out_len,
45+
const UG_FONT* font,
46+
UG_S16 max_width)
47+
{
48+
if (out == NULL || out_len == 0) {
49+
return;
50+
}
51+
if (in[0] == 0) {
52+
out[0] = 0;
53+
return;
54+
}
55+
UG_S16 width = 0;
56+
UG_S16 height = 0;
57+
UG_FontSelect(font);
58+
UG_MeasureStringCentered(&width, &height, in);
59+
60+
// Name fits without truncation.
61+
if (width <= max_width) {
62+
snprintf(out, MEMORY_DEVICE_NAME_MAX_LEN, "%s", in);
63+
return;
64+
}
65+
66+
// Truncate if too long to a size where "<name>..." fits.
67+
size_t truncate_len = strlen(in) - 1;
68+
do {
69+
// truncate at `truncate_len`.
70+
snprintf(out, out_len, "%.*s...", (int)truncate_len, in);
71+
truncate_len--;
72+
UG_MeasureStringCentered(&width, &height, out);
73+
} while (truncate_len > 0 && width >= max_width);
74+
}
75+
76+
component_t* lockscreen_create(void)
77+
{
78+
component_t* component = malloc(sizeof(component_t));
79+
if (!component) {
80+
Abort("Error: malloc lockscreen component");
81+
}
82+
memset(component, 0, sizeof(component_t));
83+
component->f = &_component_functions;
84+
85+
component->dimension.width = SCREEN_WIDTH;
86+
component->dimension.height = SCREEN_HEIGHT;
87+
88+
const UG_FONT* device_name_font = &font_font_a_9X9;
89+
90+
char device_name[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
91+
memory_get_device_name(device_name);
92+
// Show nothing if the name is the default name.
93+
if (STREQ(device_name, MEMORY_DEFAULT_DEVICE_NAME)) {
94+
device_name[0] = 0;
95+
}
96+
97+
char display_name[MEMORY_DEVICE_NAME_MAX_LEN + 3] = {0};
98+
_truncate_to_fit(
99+
device_name,
100+
display_name,
101+
sizeof(display_name),
102+
device_name_font,
103+
component->dimension.width);
104+
ui_util_add_sub_component(
105+
component, label_create("See the BitBoxApp", NULL, CENTER, component));
106+
ui_util_add_sub_component(
107+
component, label_create(display_name, device_name_font, CENTER_BOTTOM, component));
108+
109+
return component;
110+
}

src/ui/components/lockscreen.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 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 _LOCKSCREEN_H_
16+
#define _LOCKSCREEN_H_
17+
18+
#include <ui/component.h>
19+
20+
/**
21+
* Creates the screen shown when the device is locked.
22+
*/
23+
component_t* lockscreen_create(void);
24+
25+
#endif

src/workflow/idle_workflow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <hww.h>
2121
#include <platform_config.h>
22-
#include <ui/components/info_centered.h>
22+
#include <ui/components/lockscreen.h>
2323
#include <ui/components/waiting.h>
2424
#include <ui/screen_stack.h>
2525
#include <ui/ugui/ugui.h>
@@ -34,7 +34,7 @@
3434
static void _init_communication(void)
3535
{
3636
usb_start(hww_setup);
37-
ui_screen_stack_push(info_centered_create("See the BitBoxApp", NULL));
37+
ui_screen_stack_push(lockscreen_create());
3838
}
3939

4040
void idle_workflow_blocking(void)

0 commit comments

Comments
 (0)