Skip to content

Commit

Permalink
Merge branch 'base-api' into hsm
Browse files Browse the repository at this point in the history
  • Loading branch information
NickeZ committed Nov 27, 2019
2 parents 51f5865 + 87999f0 commit ed657ee
Show file tree
Hide file tree
Showing 38 changed files with 1,587 additions and 36 deletions.
19 changes: 14 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ set(PLATFORM-BITBOXBASE-SOURCES
)
set(PLATFORM-BITBOXBASE-SOURCES ${PLATFORM-BITBOXBASE-SOURCES} PARENT_SCOPE)

set(BITBOXBASE-FIRMWARE-SOURCES
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_background.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_watchdog.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_screensaver.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_status.c
)
set(BITBOXBASE-FIRMWARE-SOURCES ${BITBOXBASE-FIRMWARE-SOURCES} PARENT_SCOPE)

set(CRYPTOAUTHLIB-SOURCES
${CMAKE_SOURCE_DIR}/src/securechip/securechip.c
)
Expand Down Expand Up @@ -239,8 +247,9 @@ if(CMAKE_CROSSCOMPILING)
get_property(ASF4_MIN_INCLUDES TARGET asf4-drivers-min PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(ASF4_INCLUDES TARGET asf4-drivers PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(CMSIS_INCLUDES TARGET CMSIS PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(WALLY_INCLUDES TARGET wallycore PROPERTY INTERFACE_INCLUDE_DIRECTORIES)

set(RUST_INCLUDES ${INCLUDES} ${SAMD51A_INCLUDES} ${ASF4_INCLUDES} ${ASF4_MIN_INCLUDES} ${CMSIS_INCLUDES})
set(RUST_INCLUDES ${INCLUDES} ${SYSTEMINCLUDES} ${SAMD51A_INCLUDES} ${ASF4_INCLUDES} ${ASF4_MIN_INCLUDES} ${CMSIS_INCLUDES} ${NANOPB_INCLUDE_DIRS} ${WALLY_INCLUDES})

if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
set(RUST_PROFILE "debug")
Expand Down Expand Up @@ -287,7 +296,7 @@ if(CMAKE_CROSSCOMPILING)
# TODO(nc): Add dependency on global "doc" target on this when api-docs branch is merged
add_custom_target(rust-docs
COMMAND
${CMAKE_COMMAND} -E env SYSROOT=${CMAKE_SYSROOT} INCLUDES="${RUST_INCLUDES}" ${CARGO} doc --manifest-path ${LIBBITBOX02_RUST_SOURCE_DIR}/Cargo.toml --target-dir ${CMAKE_BINARY_DIR}/docs-rust ${RUST_CARGO_FLAG} --target thumbv7em-none-eabi
${CMAKE_COMMAND} -E env SYSROOT=${CMAKE_SYSROOT} INCLUDES="${RUST_INCLUDES}" ${CARGO} doc --document-private-items --manifest-path ${LIBBITBOX02_RUST_SOURCE_DIR}/Cargo.toml --target-dir ${CMAKE_BINARY_DIR}/docs-rust ${RUST_CARGO_FLAG} --target thumbv7em-none-eabi
COMMAND
${CMAKE_COMMAND} -E echo "See docs at file://${CMAKE_BINARY_DIR}/docs-rust/thumbv7em-none-eabi/doc/bitbox02_rust/index.html"
)
Expand Down Expand Up @@ -454,13 +463,13 @@ if(CMAKE_CROSSCOMPILING)
target_compile_definitions(firmware-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY APP_BTC)
target_sources(firmware-btc.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES})

target_sources(firmware-bitboxbase.elf PRIVATE bitboxbase/bitboxbase.c)
target_sources(firmware-bitboxbase.elf PRIVATE bitboxbase/bitboxbase.c commander/commander_bitboxbase.c)
target_compile_definitions(firmware-bitboxbase.elf PRIVATE PRODUCT_BITBOX_BASE)
target_sources(firmware-bitboxbase.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES})
target_sources(firmware-bitboxbase.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES} ${BITBOXBASE-FIRMWARE-SOURCES})

target_sources(firmware-bitboxbase-semihosting.elf PRIVATE bitboxbase/bitboxbase.c)
target_compile_definitions(firmware-bitboxbase-semihosting.elf PRIVATE PRODUCT_BITBOX_BASE SEMIHOSTING)
target_sources(firmware-bitboxbase-semihosting.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES})
target_sources(firmware-bitboxbase-semihosting.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES} ${BITBOXBASE-FIRMWARE-SOURCES})

target_link_libraries(firmware-bitboxbase.elf PRIVATE bitbox02_rust)
add_dependencies(firmware-bitboxbase.elf rust rust-bindgen)
Expand Down
13 changes: 13 additions & 0 deletions src/bitboxbase/bitboxbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bitboxbase_background.h"
#include "bitboxbase_screensaver.h"
#include "bitboxbase_watchdog.h"
#include "common_main.h"
#include "driver_init.h"
#include "firmware_main_loop.h"
#include "hardfault.h"
#include "hww.h"
#include "platform_init.h"
#include "qtouch.h"
#include "rust/bitbox02_rust.h"
#include "screen.h"
#include "ui/oled/oled.h"
#include "ui/screen_process.h"
Expand All @@ -45,7 +49,16 @@ int main(void)
hww_setup();
common_main();
traceln("%s", "Device initialized");
bitboxbase_watchdog_init();
bitboxbase_screensaver_init();
bitboxbase_background();
for (;;) {
if (bitboxbase_watchdog_check() && bitboxbase_state_get() != BBBWaiting) {
bitboxbase_state_set_not_alive();
}
if (bitboxbase_screensaver_check()) {
oled_off();
}
screen_process();
usart_receive();
usb_processing_process(usb_processing_hww());
Expand Down
125 changes: 125 additions & 0 deletions src/bitboxbase/bitboxbase_background.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2019 Shift Cryptosecurity AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bitboxbase_background.h"
#include "bitboxbase_screensaver.h"
#include "bitboxbase_status.h"
#include "screen.h"
#include <hardfault.h>
#include <leds.h>
#include <rust/bitbox02_rust.h>
#include <string.h>
#include <ui/component.h>
#include <ui/components/image.h>
#include <ui/components/label.h>
#include <ui/components/ui_images.h>
#include <ui/event.h>
#include <ui/oled/oled.h>
#include <ui/screen_stack.h>
#include <ui/ui_util.h>

static void _render(component_t* component);
static void _on_event(const event_t* event, component_t* component);

static component_functions_t _component_functions = {
.cleanup = ui_util_component_cleanup,
.render = _render,
.on_event = _on_event,
};

void _render(component_t* component)
{
component_t* label = component->sub_components.sub_components[1];
char buf[100];
bitboxbase_state_get_description(buf, sizeof(buf));
switch (bitboxbase_state_get()) {
case BBBNotAlive:
leds_turn_big_led(0, LED_COLOR_RED);
label_update(label, "Error, please restart");
break;
case BBBWaiting:
leds_turn_big_led(0, LED_COLOR_GREEN);
label_update(label, "System starting...");
break;
case BBBIdle:
if (bitboxbase_config_led_mode_get() < OnWarning) {
leds_turn_big_led(0, LED_COLOR_GREEN);
} else {
leds_turn_big_led(0, LED_COLOR_NONE);
}
label_update(label, buf);
break;
case BBBWorking:
leds_turn_big_led(0, LED_COLOR_BLUE);
label_update(label, buf);
break;
case BBBWarning:
if (bitboxbase_config_led_mode_get() < OnError) {
leds_turn_big_led(0, LED_COLOR_YELLOW);
} else {
leds_turn_big_led(0, LED_COLOR_NONE);
}
label_update(label, buf);
break;
case BBBError:
leds_turn_big_led(0, LED_COLOR_RED);
label_update(label, buf);
break;
default:
Abort("Internal error");
break;
}
ui_util_component_render_subcomponents(component);
}

void _on_event(const event_t* event, component_t* component)
{
(void)component;
bitboxbase_screensaver_reset();
oled_init();
if (event->id == EVENT_BUTTON_SHORT_TAP) {
bitboxbase_status();
}
}

static component_t* _create(void)
{
component_t* show_logo = malloc(sizeof(component_t));
if (!show_logo) {
Abort("Error: malloc show_logo");
}
memset(show_logo, 0, sizeof(component_t));
show_logo->f = &_component_functions;
show_logo->dimension.width = 128;
show_logo->dimension.height = 64;
component_t* bb2_logo = image_create(
IMAGE_BB2_LOGO,
sizeof(IMAGE_BB2_LOGO),
IMAGE_BB2_LOGO_W,
IMAGE_BB2_LOGO_H,
CENTER,
show_logo);
component_t* loading_label = label_create("Booting...", NULL, RIGHT_BOTTOM, show_logo);
ui_util_add_sub_component(show_logo, bb2_logo);
ui_util_add_sub_component(show_logo, loading_label);
return show_logo;
}

void bitboxbase_background(void)
{
leds_turn_big_led(0, LED_COLOR_NONE);
leds_turn_big_led(1, LED_COLOR_NONE);
component_t* b = _create();
ui_screen_stack_push(b);
}
15 changes: 15 additions & 0 deletions src/bitboxbase/bitboxbase_background.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2019 Shift Cryptosecurity AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

void bitboxbase_background(void);
68 changes: 68 additions & 0 deletions src/bitboxbase/bitboxbase_screensaver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2019 Shift Cryptosecurity AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bitboxbase_screensaver.h"
#include <hal_timer.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

// The periodicity to increment the timeout counter
#define INTERVAL_MS 1000
// Number of periods we wait until we raise an alarm.
#define TIMEOUT 120

extern struct timer_descriptor TIMER_0;

static volatile uint32_t timeout_counter;

static void _timer_task_cb(const struct timer_task* const timer_task)
{
(void)timer_task;
timeout_counter++;
}

static void _timer_config(void)
{
static struct timer_task Timer_task;
Timer_task.interval = INTERVAL_MS;
Timer_task.cb = _timer_task_cb;
Timer_task.mode = TIMER_TASK_REPEAT;

timer_stop(&TIMER_0);
timer_add_task(&TIMER_0, &Timer_task);
timer_start(&TIMER_0);
}

// Initialize screensaver
void bitboxbase_screensaver_init(void)
{
bitboxbase_screensaver_reset();
_timer_config();
}

// Checks the timer
bool bitboxbase_screensaver_check(void)
{
if (timeout_counter > TIMEOUT) {
return true;
}
return false;
}

// Resets the screensaver
void bitboxbase_screensaver_reset(void)
{
timeout_counter = 0;
}
41 changes: 41 additions & 0 deletions src/bitboxbase/bitboxbase_screensaver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 Shift Cryptosecurity AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _BITBOXBASE_SCREENSAVER_H
#define _BITBOXBASE_SCREENSAVER_H

#include <stdbool.h>

/**
* @file
*
* bitboxbase screensaver is a timer that keeps track of whether to turn off the display or not.
*/

/**
* Initialize counter
*/
void bitboxbase_screensaver_init(void);

/**
* Check if screensaver should be enabled
*/
bool bitboxbase_screensaver_check(void);

/**
* Reset counter
*/
void bitboxbase_screensaver_reset(void);

#endif
Loading

0 comments on commit ed657ee

Please sign in to comment.