Date/Time EIP-712 filtering implementation
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include "ui_logic.h"
|
||||
#include "mem.h"
|
||||
#include "mem_utils.h"
|
||||
@@ -39,6 +40,7 @@ typedef enum {
|
||||
#define UI_712_FIELD_SHOWN (1 << 0)
|
||||
#define UI_712_FIELD_NAME_PROVIDED (1 << 1)
|
||||
#define UI_712_AMOUNT_JOIN (1 << 2)
|
||||
#define UI_712_DATETIME (1 << 3)
|
||||
|
||||
typedef struct {
|
||||
s_amount_join joins[MAX_ASSETS];
|
||||
@@ -426,6 +428,42 @@ static bool update_amount_join(const uint8_t *data, uint8_t length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format given data as a human-readable date/time representation
|
||||
*
|
||||
* @param[in] data the data that needs formatting
|
||||
* @param[in] length its length
|
||||
* @return whether it was successful or not
|
||||
*/
|
||||
static bool ui_712_format_datetime(const uint8_t *data, uint8_t length) {
|
||||
struct tm tstruct;
|
||||
int shown_hour;
|
||||
time_t timestamp = u64_from_BE(data, length);
|
||||
|
||||
if (gmtime_r(×tamp, &tstruct) == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (tstruct.tm_hour == 0) {
|
||||
shown_hour = 12;
|
||||
} else {
|
||||
shown_hour = tstruct.tm_hour;
|
||||
if (shown_hour > 12) {
|
||||
shown_hour -= 12;
|
||||
}
|
||||
}
|
||||
snprintf(strings.tmp.tmp,
|
||||
sizeof(strings.tmp.tmp),
|
||||
"%04d-%02d-%02d\n%02d:%02d:%02d %s UTC",
|
||||
tstruct.tm_year + 1900,
|
||||
tstruct.tm_mon + 1,
|
||||
tstruct.tm_mday,
|
||||
shown_hour,
|
||||
tstruct.tm_min,
|
||||
tstruct.tm_sec,
|
||||
(tstruct.tm_hour < 12) ? "AM" : "PM");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to notify of a new field to review in the current struct (key + value)
|
||||
*
|
||||
@@ -497,6 +535,12 @@ bool ui_712_new_field(const void *const field_ptr, const uint8_t *const data, ui
|
||||
}
|
||||
}
|
||||
|
||||
if (ui_ctx->field_flags & UI_712_DATETIME) {
|
||||
if (!ui_712_format_datetime(data, length)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this field is supposed to be displayed
|
||||
if (ui_712_field_shown()) {
|
||||
ui_712_redraw_generic_step();
|
||||
@@ -573,8 +617,9 @@ unsigned int ui_712_reject() {
|
||||
* @param[in] show if this field should be shown on the device
|
||||
* @param[in] name_provided if a substitution name has been provided
|
||||
* @param[in] token_join if this field is part of a token join
|
||||
* @param[in] datetime if this field should be shown and formatted as a date/time
|
||||
*/
|
||||
void ui_712_flag_field(bool show, bool name_provided, bool token_join) {
|
||||
void ui_712_flag_field(bool show, bool name_provided, bool token_join, bool datetime) {
|
||||
if (show) {
|
||||
ui_ctx->field_flags |= UI_712_FIELD_SHOWN;
|
||||
}
|
||||
@@ -584,6 +629,9 @@ void ui_712_flag_field(bool show, bool name_provided, bool token_join) {
|
||||
if (token_join) {
|
||||
ui_ctx->field_flags |= UI_712_AMOUNT_JOIN;
|
||||
}
|
||||
if (datetime) {
|
||||
ui_ctx->field_flags |= UI_712_DATETIME;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user