Skip to content

Commit

Permalink
qrmode: add menu option for QR users to set network (mainnet/testnet)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDriver committed Apr 10, 2024
1 parent 54c5067 commit 9dbe79e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 14 deletions.
1 change: 1 addition & 0 deletions main/button_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ typedef enum {
BTN_SETTINGS_OTP_HELP,
BTN_SETTINGS_OTP_EXIT,
BTN_SETTINGS_BLE,
BTN_SETTINGS_NETWORK_TYPE,
BTN_SETTINGS_CHANGE_PIN,
BTN_SETTINGS_CHANGE_PIN_QR,
BTN_SETTINGS_RESET,
Expand Down
70 changes: 66 additions & 4 deletions main/process/dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ gui_activity_t* make_unlocked_settings_activity(void);
gui_activity_t* make_wallet_settings_activity(void);
gui_activity_t* make_device_settings_activity(void);
gui_activity_t* make_authentication_activity(bool initialised_and_pin_unlocked);
gui_activity_t* make_prefs_settings_activity(bool initialised_and_locked);
gui_activity_t* make_prefs_settings_activity(bool initialised_and_locked, gui_view_node_t** qr_mode_network_item);
gui_activity_t* make_display_settings_activity(void);
gui_activity_t* make_info_activity(const char* fw_version);
gui_activity_t* make_device_info_activity(void);
Expand Down Expand Up @@ -836,11 +836,11 @@ static void handle_ble(void)
gui_set_current_activity(act);

#ifndef CONFIG_DEBUG_UNATTENDED_CI
bool ret = gui_activity_wait_event(act, GUI_BUTTON_EVENT, ESP_EVENT_ANY_ID, NULL, &ev_id, NULL, 0);
const bool ret = gui_activity_wait_event(act, GUI_BUTTON_EVENT, ESP_EVENT_ANY_ID, NULL, &ev_id, NULL, 0);
#else
gui_activity_wait_event(act, GUI_BUTTON_EVENT, ESP_EVENT_ANY_ID, NULL, NULL, NULL,
CONFIG_DEBUG_UNATTENDED_CI_TIMEOUT_MS / portTICK_PERIOD_MS);
bool ret = true;
const bool ret = true;
ev_id = BTN_BLE_EXIT;
#endif
if (ret) {
Expand Down Expand Up @@ -1899,6 +1899,56 @@ static void handle_display_battery_volts(void)
handle_info_detail_screen("Battery Volts", power_status);
}

static void update_network_menu_label(gui_view_node_t* network_type_item)
{
JADE_ASSERT(network_type_item);
const network_type_t type = keychain_get_network_type_restriction();
const char* label = type == NETWORK_TYPE_TEST ? "Network: Testnet" : "Network: Mainnet";
gui_update_text(network_type_item, label);
}

static void update_network_carousel_item(gui_view_node_t* network_type_item, const network_type_t type)
{
JADE_ASSERT(network_type_item);
const char* label = type == NETWORK_TYPE_TEST ? "Testnet" : "Mainnet";
gui_update_text(network_type_item, label);
}

static void handle_network_type(gui_view_node_t* network_type_item)
{
JADE_ASSERT(network_type_item);

// Only expected for QR Mode atm
JADE_ASSERT(keychain_get() && keychain_get_userdata() == SOURCE_INTERNAL);

network_type_t type = keychain_get_network_type_restriction();

gui_view_node_t* network_textbox = NULL;
gui_activity_t* const act_network = make_carousel_activity("Network Type", NULL, &network_textbox);
update_network_carousel_item(network_textbox, type);

// Show, and await button click
gui_set_current_activity(act_network);

int32_t ev_id;
while (true) {
if (gui_activity_wait_event(act_network, GUI_EVENT, ESP_EVENT_ANY_ID, NULL, &ev_id, NULL, 0)) {
if (ev_id == GUI_WHEEL_LEFT_EVENT || ev_id == GUI_WHEEL_RIGHT_EVENT) {
type = type == NETWORK_TYPE_TEST ? NETWORK_TYPE_MAIN
: NETWORK_TYPE_TEST; // Just toggle label at this point
update_network_carousel_item(network_textbox, type);
} else if (ev_id == gui_get_click_event()) {
// Done - apply network change
break;
}
}
}

keychain_clear_network_type_restriction();
keychain_set_network_type_restriction(type == NETWORK_TYPE_TEST ? TAG_TESTNET : TAG_MAINNET);
update_network_menu_label(network_type_item);
}

// Create the appropriate 'Settings' menu
static gui_activity_t* create_settings_menu(const bool startup_menu)
{
Expand Down Expand Up @@ -1933,6 +1983,10 @@ static void handle_settings(const bool startup_menu)
// hw initialised and that wallet has been unlocked with PIN (ie not a temporary signer)
const bool hw_pin_unlocked = keychain_get() && keychain_has_pin() && !keychain_has_temporary();

// hw initialised with internal message source (ie. QR-mode)
const bool hw_qr_mode = keychain_get() && keychain_get_userdata() == SOURCE_INTERNAL;
gui_view_node_t* network_type_item = NULL;

// NOTE: menu navigation frees prior screens, as the navigation is
// potentially unbound with all the back and forward buttons.
bool done = false;
Expand Down Expand Up @@ -1986,7 +2040,11 @@ static void handle_settings(const bool startup_menu)
case BTN_SETTINGS_PREFS:
case BTN_SETTINGS_DISPLAY_EXIT:
// Change to 'Preferences' menu (Settings)
act = make_prefs_settings_activity(hw_locked_initialised);
// Only pass the qr_mode_network_item if we're in QR mode
act = make_prefs_settings_activity(hw_locked_initialised, hw_qr_mode ? &network_type_item : NULL);
if (network_type_item) {
update_network_menu_label(network_type_item);
}
break;

case BTN_SETTINGS_DISPLAY:
Expand Down Expand Up @@ -2037,6 +2095,10 @@ static void handle_settings(const bool startup_menu)
handle_idle_timeout();
break;

case BTN_SETTINGS_NETWORK_TYPE:
handle_network_type(network_type_item);
break;

case BTN_SETTINGS_BLE:
handle_ble();
break;
Expand Down
33 changes: 23 additions & 10 deletions main/ui/dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,34 @@ gui_activity_t* make_device_settings_activity(void)
return make_menu_activity("Device", hdrbtns, 2, menubtns, 3);
}

gui_activity_t* make_prefs_settings_activity(const bool initialised_and_locked)
gui_activity_t* make_prefs_settings_activity(const bool initialised_and_locked, gui_view_node_t** network_type_item)
{
JADE_LOGE("make_prefs_settings_activity(%u, %p)", initialised_and_locked, network_type_item);

btn_data_t hdrbtns[] = { { .txt = "=", .font = JADE_SYMBOLS_16x16_FONT, .ev_id = BTN_SETTINGS_PREFS_EXIT },
{ .txt = NULL, .font = GUI_DEFAULT_FONT, .ev_id = GUI_BUTTON_EVENT_NONE } };

btn_data_t menubtns[] = { { .txt = "Display", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_DISPLAY },
{ .txt = "Idle Timeout", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_IDLE_TIMEOUT },
{ .txt = "Bluetooth", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_BLE } };
{ .txt = "Bluetooth", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_BLE },
{ .txt = NULL, .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_NETWORK_TYPE } };

// If Jade is initialised and locked, show the 'change_pin' option.
// If not (ie. is unlocked, or is uninitialised) show the ble option.
if (initialised_and_locked) {
menubtns[2].txt = "Change PIN";
menubtns[2].ev_id = BTN_SETTINGS_CHANGE_PIN;
menubtns[1].txt = "Change PIN";
menubtns[1].ev_id = BTN_SETTINGS_CHANGE_PIN;
}

size_t num_menubtns = 2;

// If qr_mode_network_item status control passed, implies want that button visible
if (network_type_item) {
gui_make_text(network_type_item, "Network:", TFT_WHITE);
gui_set_align(*network_type_item, GUI_ALIGN_CENTER, GUI_ALIGN_MIDDLE);
menubtns[num_menubtns++].content = *network_type_item;
}

return make_menu_activity("Settings", hdrbtns, 2, menubtns, 3);
return make_menu_activity("Settings", hdrbtns, 2, menubtns, num_menubtns);
}

gui_activity_t* make_display_settings_activity(void)
Expand All @@ -351,11 +362,13 @@ gui_activity_t* make_display_settings_activity(void)
#ifdef CONFIG_BOARD_TYPE_JADE_V1_1
btn_data_t menubtns[]
= { { .txt = "Display Brightness", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_DISPLAY_BRIGHTNESS },
{ .txt = "Idle Timeout", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_IDLE_TIMEOUT },
{ .txt = "Theme", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_DISPLAY_THEME } };
const size_t num_menubtns = 2;
const size_t num_menubtns = 3;
#else
btn_data_t menubtns[] = { { .txt = "Theme", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_DISPLAY_THEME } };
const size_t num_menubtns = 1;
btn_data_t menubtns[] = { { .txt = "Theme", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_DISPLAY_THEME },
{ .txt = "Idle Timeout", .font = GUI_DEFAULT_FONT, .ev_id = BTN_SETTINGS_IDLE_TIMEOUT } };
const size_t num_menubtns = 2;
#endif

return make_menu_activity("Display", hdrbtns, 2, menubtns, num_menubtns);
Expand Down Expand Up @@ -480,7 +493,7 @@ gui_activity_t* make_ble_activity(gui_view_node_t** ble_status_item)
btn_data_t hdrbtns[] = { { .txt = "=", .font = JADE_SYMBOLS_16x16_FONT, .ev_id = BTN_BLE_EXIT },
{ .txt = "?", .font = GUI_TITLE_FONT, .ev_id = BTN_BLE_HELP } };

// menu buttons with bespoke content
// menu button with bespoke content
gui_make_text(ble_status_item, "Status:", TFT_WHITE);
gui_set_align(*ble_status_item, GUI_ALIGN_CENTER, GUI_ALIGN_MIDDLE);

Expand Down

0 comments on commit 9dbe79e

Please sign in to comment.