Skip to content

Commit

Permalink
Extract common ui_stream functions in ui_stream_common for clarity
Browse files Browse the repository at this point in the history
Remove redundant code
  • Loading branch information
ajinkyaraj-23 committed Sep 20, 2024
1 parent 1d12e8f commit ea83d89
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
/_build
/app/.target
/app/bin
Expand Down
5 changes: 0 additions & 5 deletions app/src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ dispatch(command_t *cmd)
TZ_FAIL(EXC_CLASS);
}

if (tz_ui_stream_get_cb_type() == SCREEN_QUIT) {
PRINTF("[ERROR] received instruction whilst on Quit screen\n");
TZ_FAIL(EXC_UNEXPECTED_STATE);
}

// clang-format off
switch (cmd->ins) {
case INS_VERSION: f = handle_apdu_version; break;
Expand Down
15 changes: 0 additions & 15 deletions app/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@
#define MAX_APDU_SIZE 235
#define MAX_SIGNATURE_SIZE 100
#define ERROR_CODE_SIZE 15
/**
* @brief Home screen pages in order
*
*/
typedef enum {
#ifdef HAVE_BAGL
SCREEN_HOME = 0,
#else
SCREEN_CLEAR_SIGN = 0,
SCREEN_BLIND_SIGN,
#endif
SCREEN_VERSION,
SCREEN_SETTINGS,
SCREEN_QUIT,
} screen_t;

/**
* @brief State of the app
Expand Down
85 changes: 0 additions & 85 deletions app/src/ui_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ static void change_screen_right(void);
static void redisplay(void);

const bagl_icon_details_t C_icon_rien = {0, 0, 1, NULL, NULL};
#endif // HAVE_BAGL

void drop_last_screen(void);
void push_str(const char *text, size_t len, char **out);

// Model

#ifdef HAVE_BAGL
void
tz_ui_stream_init(void (*cb)(tz_ui_cb_type_t cb_type))
{
Expand All @@ -61,62 +54,7 @@ tz_ui_stream_init(void (*cb)(tz_ui_cb_type_t cb_type))

FUNC_LEAVE();
}
#endif

#ifdef HAVE_BAGL
void
tz_ui_stream_close(void)
{
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("void"));
if (s->full) {
PRINTF("trying to close already closed stream display");
THROW(EXC_UNKNOWN);
}
s->full = true;
FUNC_LEAVE();
}
#endif // HAVE_BAGL

size_t
tz_ui_stream_push_all(tz_ui_cb_type_t cb_type, const char *title,
const char *value, tz_ui_layout_type_t layout_type,
tz_ui_icon_t icon)
{
size_t obuflen;
size_t i = 0;

FUNC_ENTER(("cb_type=%d title=%s value=%s", cb_type, title, value));

obuflen = strlen(value);
do {
i += tz_ui_stream_push(cb_type, title, value + i, layout_type, icon);
PRINTF("[DEBUG] pushed %d in total\n", i);
} while (i < obuflen);

FUNC_LEAVE();
return i;
}

size_t
tz_ui_stream_push(tz_ui_cb_type_t cb_type, const char *title,
const char *value, tz_ui_layout_type_t layout_type,
tz_ui_icon_t icon)
{
return tz_ui_stream_pushl(cb_type, title, value, -1, layout_type, icon);
}

tz_ui_cb_type_t
tz_ui_stream_get_cb_type(void)
{
tz_ui_stream_t *s = &global.stream;
size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;

return s->screens[bucket].cb_type;
}

#ifdef HAVE_BAGL
static void
pred(void)
{
Expand Down Expand Up @@ -561,26 +499,3 @@ drop_last_screen(void)
TZ_POSTAMBLE;
}
#endif

void
push_str(const char *text, size_t len, char **out)
{
bool can_fit = false;

TZ_PREAMBLE(("%s", text));

if (len == 0) {
*out = NULL;
TZ_SUCCEED();
}

TZ_CHECK(ui_strings_can_fit(len, &can_fit));
while (!can_fit) {
TZ_CHECK(drop_last_screen());
TZ_CHECK(ui_strings_can_fit(len, &can_fit));
}

TZ_CHECK(ui_strings_push(text, len, out));

TZ_POSTAMBLE;
}
4 changes: 4 additions & 0 deletions app/src/ui_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,7 @@ void tz_reject(void);
*/
void tz_reject_ui(void);
#endif

void drop_last_screen(void);

void push_str(const char *text, size_t len, char **out);
82 changes: 82 additions & 0 deletions app/src/ui_stream_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// Created by ajinkyaraj on 14/09/24.
//
#include "exception.h"
#include "globals.h"
#include "ui_strings.h"
#include "ui_stream.h"

size_t
tz_ui_stream_push_all(tz_ui_cb_type_t cb_type, const char *title,
const char *value, tz_ui_layout_type_t layout_type,
tz_ui_icon_t icon)
{
size_t obuflen;
size_t i = 0;

FUNC_ENTER(("cb_type=%d title=%s value=%s", cb_type, title, value));

obuflen = strlen(value);
do {
i += tz_ui_stream_push(cb_type, title, value + i, layout_type, icon);
PRINTF("[DEBUG] pushed %d in total\n", i);
} while (i < obuflen);

FUNC_LEAVE();
return i;
}

size_t
tz_ui_stream_push(tz_ui_cb_type_t cb_type, const char *title,
const char *value, tz_ui_layout_type_t layout_type,
tz_ui_icon_t icon)
{
return tz_ui_stream_pushl(cb_type, title, value, -1, layout_type, icon);
}

tz_ui_cb_type_t
tz_ui_stream_get_cb_type(void)
{
tz_ui_stream_t *s = &global.stream;
size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;

return s->screens[bucket].cb_type;
}

void
push_str(const char *text, size_t len, char **out)
{
bool can_fit = false;

TZ_PREAMBLE(("%s", text));

if (len == 0) {
*out = NULL;
TZ_SUCCEED();
}

TZ_CHECK(ui_strings_can_fit(len, &can_fit));
while (!can_fit) {
TZ_CHECK(drop_last_screen());
TZ_CHECK(ui_strings_can_fit(len, &can_fit));
}

TZ_CHECK(ui_strings_push(text, len, out));

TZ_POSTAMBLE;
}

void
tz_ui_stream_close(void)
{
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("full=%d", s->full));
if (s->full) {
PRINTF("trying to close already closed stream display");
THROW(EXC_UNKNOWN);
}
s->full = true;

FUNC_LEAVE();
}
14 changes: 0 additions & 14 deletions app/src/ui_stream_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ tz_ui_stream(void)
}

FUNC_LEAVE();
return;
}

void
Expand Down Expand Up @@ -280,20 +279,7 @@ tz_ui_stream_start(void)
FUNC_LEAVE();
}

void
tz_ui_stream_close(void)
{
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("full=%d", s->full));
if (s->full) {
PRINTF("trying to close already closed stream display");
THROW(EXC_UNKNOWN);
}
s->full = true;

FUNC_LEAVE();
}

bool
tz_ui_nav_cb(void)
Expand Down

0 comments on commit ea83d89

Please sign in to comment.