Skip to content

Commit

Permalink
Remove textual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Z4karia authored Apr 12, 2024
1 parent 105dc21 commit 0adc3f0
Show file tree
Hide file tree
Showing 17 changed files with 24 additions and 416 deletions.
1 change: 0 additions & 1 deletion app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ typedef enum {

typedef enum {
tx_json = 0,
tx_textual
} tx_type_e;

typedef enum {
Expand Down
5 changes: 2 additions & 3 deletions app/src/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ parser_error_t parser_parse(parser_context_t *ctx,
parser_error_t parser_validate(const parser_context_t *ctx);

//// returns the number of items in the current parsing context
parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items);
parser_error_t parser_getNumItems(uint8_t *num_items);

// retrieves a readable output for each field / page
parser_error_t parser_getItem(const parser_context_t *ctx,
uint8_t displayIdx,
parser_error_t parser_getItem(uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);
Expand Down
7 changes: 3 additions & 4 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static parser_tx_t tx_obj;

const char *tx_parse(tx_type_e type)
{
if (type != tx_json && type != tx_textual) {
if (type != tx_json) {
return parser_getErrorDescription(parser_value_out_of_range);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ void tx_parse_reset()

zxerr_t tx_getNumItems(uint8_t *num_items)
{
parser_error_t err = parser_getNumItems(&ctx_parsed_tx, num_items);
parser_error_t err = parser_getNumItems(num_items);

if (err != parser_ok)
{
Expand All @@ -138,8 +138,7 @@ zxerr_t tx_getItem(int8_t displayIdx,
return zxerr_no_data;
}

parser_error_t err = parser_getItem(&ctx_parsed_tx,
displayIdx,
parser_error_t err = parser_getItem(displayIdx,
outKey, outKeyLen,
outVal, outValLen,
pageIdx, pageCount);
Expand Down
1 change: 0 additions & 1 deletion app/src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const char *tx_parse(tx_type_e type);

/// Return the number of items in the transaction
zxerr_t tx_getNumItems(uint8_t *num_items);
zxerr_t tx_getTextualNumItems(uint8_t *num_items);

/// Gets an specific item from the transaction (including paging)
zxerr_t tx_getItem(int8_t displayIdx,
Expand Down
226 changes: 8 additions & 218 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ parser_error_t parser_parse(parser_context_t *ctx,

CHECK_PARSER_ERR(parser_init_context(ctx, data, dataLen))
ctx->tx_obj = tx_obj;
if (tx_obj->tx_type == tx_textual) {
CHECK_PARSER_ERR(_read_text_tx(ctx, tx_obj))
} else {
CHECK_PARSER_ERR(_read_json_tx(ctx, tx_obj))
}
CHECK_PARSER_ERR(_read_json_tx(ctx, tx_obj))

extraDepthLevel = false;
return parser_ok;
Expand All @@ -69,51 +65,23 @@ parser_error_t parser_validate(const parser_context_t *ctx) {

// Iterate through all items to check that all can be shown and are valid
uint8_t numItems = 0;
CHECK_PARSER_ERR(parser_getNumItems(ctx, &numItems))
CHECK_PARSER_ERR(parser_getNumItems(&numItems))

char tmpKey[40];
char tmpVal[40];
uint8_t pageCount = 0;
for (uint8_t idx = 0; idx < numItems; idx++) {
CHECK_PARSER_ERR(parser_getItem(ctx, idx, tmpKey, sizeof(tmpKey), tmpVal, sizeof(tmpVal), 0, &pageCount))
CHECK_PARSER_ERR(parser_getItem(idx, tmpKey, sizeof(tmpKey), tmpVal, sizeof(tmpVal), 0, &pageCount))
}
return parser_ok;
}

parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
parser_error_t parser_getNumItems(uint8_t *num_items) {
*num_items = 0;
if (ctx->tx_obj->tx_type == tx_textual) {
*num_items = app_mode_expert() ? (uint8_t) ctx->tx_obj->tx_text.n_containers : (uint8_t) (ctx->tx_obj->tx_text.n_containers - ctx->tx_obj->tx_text.n_expert);
return parser_ok;
}

return tx_display_numItems(num_items);
}

__Z_INLINE bool parser_areEqual(uint16_t tokenIdx, const char *expected) {
if (parser_tx_obj.tx_json.json.tokens[tokenIdx].type != JSMN_STRING) {
return false;
}

int32_t len = parser_tx_obj.tx_json.json.tokens[tokenIdx].end - parser_tx_obj.tx_json.json.tokens[tokenIdx].start;
if (len < 0) {
return false;
}

if (strlen(expected) != (size_t) len) {
return false;
}

const char *p = parser_tx_obj.tx_json.tx + parser_tx_obj.tx_json.json.tokens[tokenIdx].start;
for (int32_t i = 0; i < len; i++) {
if (expected[i] != *(p + i)) {
return false;
}
}

return true;
}

__Z_INLINE bool parser_isAmount(char *key) {
if (strcmp(key, "msgs/value/coins") == 0){
return true;
Expand Down Expand Up @@ -291,176 +259,7 @@ __Z_INLINE parser_error_t parser_formatAmount(uint16_t amountToken,
return parser_formatAmountItem(showItemTokenIdx, outVal, outValLen, showPageIdx, &dummy);
}

__Z_INLINE parser_error_t parser_screenPrint(const parser_context_t *ctx,
Cbor_container *container,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {

//verification assures that content + title < size(tmp), to be used in string manipulation
if (container->screen.titleLen > MAX_TITLE_SIZE || container->screen.contentLen > MAX_CONTENT_SIZE) {
return parser_unexpected_value;
}
MEMZERO(ctx->tx_obj->tx_text.tmpBuffer, sizeof(ctx->tx_obj->tx_text.tmpBuffer));
char *tmp = (char*) ctx->tx_obj->tx_text.tmpBuffer;
size_t tmp_len = sizeof(ctx->tx_obj->tx_text.tmpBuffer);
char out[OUTPUT_HANDLER_SIZE] = {0};

// No Tittle screen
if (container->screen.titleLen == 0) {
MEMCPY(tmp, container->screen.contentPtr, container->screen.contentLen);
CHECK_PARSER_ERR(tx_display_translation(out, sizeof(out),tmp, container->screen.contentLen))

for (uint8_t i = 0; i < container->screen.indent; i++) {
z_str3join(out, sizeof(out), SCREEN_INDENT, "");
}

snprintf(outKey, outKeyLen, " ");
pageString(outVal, outValLen, out, pageIdx, pageCount);
return parser_ok;
}

//Translate output, cpy to tmp to assure it ends in \0
MEMZERO(tmp, tmp_len);
if(container->screen.contentPtr == NULL) {
return parser_unexpected_value;
}
MEMCPY(tmp, container->screen.contentPtr, container->screen.contentLen);
CHECK_PARSER_ERR(tx_display_translation(out, sizeof(out), tmp,container->screen.contentLen))

uint8_t titleLen = container->screen.titleLen + container->screen.indent;
//Title needs to be truncated, so we concat title witn content
if ((titleLen > PRINTABLE_TITLE_SIZE ) || (outValLen > 0 && ((strlen(out)/outValLen) >= 1
&& titleLen > PRINTABLE_PAGINATED_TITLE_SIZE))) {

char key[MAX_TITLE_SIZE + 2] = {0};
MEMCPY(key, TITLE_TRUNCATE_REPLACE, strlen(TITLE_TRUNCATE_REPLACE));
for (uint8_t i = 0; i < container->screen.indent; i++) {
z_str3join(key, sizeof(key), SCREEN_INDENT, "");
}

MEMZERO(ctx->tx_obj->tx_text.tmpBuffer, sizeof(ctx->tx_obj->tx_text.tmpBuffer));
if(container->screen.titlePtr == NULL) {
return parser_unexpected_value;
}
MEMCPY(tmp, container->screen.titlePtr, container->screen.titleLen);
MEMCPY(tmp + container->screen.titleLen,": ",2);
MEMCPY(tmp + container->screen.titleLen + 2, out, sizeof(out) - container->screen.titleLen -2);
snprintf(outKey, outKeyLen, "%s", key);
pageString(outVal, outValLen, tmp, pageIdx, pageCount);
return parser_ok;
}

//Normal print case - Prepare title
char key[MAX_TITLE_SIZE + 2] = {0};
if(container->screen.titlePtr == NULL) {
return parser_unexpected_value;
}
MEMCPY(key, container->screen.titlePtr, container->screen.titleLen);
for (uint8_t i = 0; i < container->screen.indent; i++) {
z_str3join(key, sizeof(key), SCREEN_INDENT, "");
}
snprintf(outKey, outKeyLen, "%s", key);
pageString(outVal, outValLen, out, pageIdx, pageCount);

return parser_ok;
}

__Z_INLINE parser_error_t parser_getScreenInfo(const parser_context_t *ctx,
Cbor_container *container,
uint8_t index) {
CborValue it;
CborValue containerArray_ptr;
CborValue mapStruct_ptr;
INIT_CBOR_PARSER(ctx, mapStruct_ptr)

PARSER_ASSERT_OR_ERROR(!cbor_value_at_end(&mapStruct_ptr), parser_unexpected_buffer_end)
PARSER_ASSERT_OR_ERROR(cbor_value_is_map(&mapStruct_ptr), parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&mapStruct_ptr, &it))
CHECK_CBOR_MAP_ERR(cbor_value_advance(&it))
CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&it, &containerArray_ptr))

for (int i = 0; i < index ; i ++) {
CHECK_CBOR_MAP_ERR(cbor_value_advance(&containerArray_ptr))
}

CborValue data;
CHECK_CBOR_MAP_ERR(cbor_value_get_map_length(&containerArray_ptr, &container->n_field))
CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&containerArray_ptr, &data))
CHECK_PARSER_ERR(cbor_get_containerInfo(&data, container))

return parser_ok;
}

__Z_INLINE parser_error_t parser_getNextNonExpert(const parser_context_t *ctx,
Cbor_container *container,
uint8_t displayIdx) {

PARSER_ASSERT_OR_ERROR(displayIdx < ctx->tx_obj->tx_text.n_containers, parser_unexpected_value);

uint8_t non_expert = 0;
for (size_t containerIdx = 0; containerIdx < ctx->tx_obj->tx_text.n_containers; containerIdx++) {
parser_getScreenInfo(ctx, container, containerIdx);
if (!container->screen.expert) {
non_expert++;
} else {
continue;
}
if (non_expert == displayIdx + 1) {
break;
}
PARSER_ASSERT_OR_ERROR(non_expert <= displayIdx, parser_unexpected_value);
}
return parser_ok;
}

__Z_INLINE parser_error_t parser_getTextualItem(const parser_context_t *ctx,
uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
*pageCount = 0;

MEMZERO(outKey, outKeyLen);
MEMZERO(outVal, outValLen);

uint8_t numItems;
CHECK_PARSER_ERR(parser_getNumItems(ctx, &numItems))
PARSER_ASSERT_OR_ERROR((numItems != 0), parser_unexpected_number_items)
PARSER_ASSERT_OR_ERROR((displayIdx < numItems), parser_display_idx_out_of_range)

CHECK_APP_CANARY()

Cbor_container container;
container.screen.titlePtr = NULL;
container.screen.titleLen = 0;
container.screen.contentPtr = NULL;
container.screen.contentLen = 0;
container.screen.indent = 0;
container.screen.expert = false;
CHECK_PARSER_ERR(parser_getScreenInfo(ctx, &container, displayIdx))

// title and content can be Null depending on the screen for chain id they cant be null
if (container.screen.titlePtr != NULL && container.screen.contentPtr != NULL) {
if (!strncmp(container.screen.titlePtr, "Chain id", container.screen.titleLen)){
if(!strncmp(container.screen.contentPtr, "0", container.screen.contentLen) ||
!strncmp(container.screen.contentPtr, "1", container.screen.contentLen)) {
return parser_unexpected_chain;
}
}
}

if (!app_mode_expert()) {
CHECK_PARSER_ERR(parser_getNextNonExpert(ctx, &container, displayIdx))
}

CHECK_PARSER_ERR(parser_screenPrint(ctx, &container, outKey, outKeyLen, outVal, outValLen, pageIdx, pageCount))

return parser_ok;
}

__Z_INLINE parser_error_t parser_getJsonItem(const parser_context_t *ctx,
uint8_t displayIdx,
__Z_INLINE parser_error_t parser_getJsonItem(uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
Expand All @@ -472,7 +271,7 @@ __Z_INLINE parser_error_t parser_getJsonItem(const parser_context_t *ctx,
MEMZERO(outVal, outValLen);

uint8_t numItems;
CHECK_PARSER_ERR(parser_getNumItems(ctx, &numItems))
CHECK_PARSER_ERR(parser_getNumItems(&numItems))
CHECK_APP_CANARY()

if (numItems == 0) {
Expand Down Expand Up @@ -508,24 +307,15 @@ __Z_INLINE parser_error_t parser_getJsonItem(const parser_context_t *ctx,
return parser_ok;
}

parser_error_t parser_getItem(const parser_context_t *ctx,
uint8_t displayIdx,
parser_error_t parser_getItem(uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {

if (ctx->tx_obj->tx_type == tx_textual) {
CHECK_PARSER_ERR(parser_getTextualItem(ctx,displayIdx,
CHECK_PARSER_ERR(parser_getJsonItem(displayIdx,
outKey, outKeyLen,
outVal, outValLen,
pageIdx, pageCount));

} else {
CHECK_PARSER_ERR(parser_getJsonItem(ctx,displayIdx,
outKey, outKeyLen,
outVal, outValLen,
pageIdx, pageCount));
}

return parser_ok;
}
54 changes: 1 addition & 53 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,56 +114,4 @@ parser_error_t _read_json_tx(parser_context_t *c, __Z_UNUSED parser_tx_t *v) {
parser_tx_obj.tx_json.filter_msg_from_count = 0;

return parser_ok;
}

parser_error_t _read_text_tx(parser_context_t *c, parser_tx_t *v) {
CborValue it;
CborValue mapStruct_ptr;
CHECK_APP_CANARY()
INIT_CBOR_PARSER(c, mapStruct_ptr)
CHECK_APP_CANARY()

//Make sure we have a map/struct
PARSER_ASSERT_OR_ERROR(cbor_value_is_map(&mapStruct_ptr), parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&mapStruct_ptr, &it))

//Make sure we have screen_key set to 1
int screen_key = 0;
PARSER_ASSERT_OR_ERROR(cbor_value_is_integer(&it), parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_get_int(&it, &screen_key))
PARSER_ASSERT_OR_ERROR(screen_key == 1, parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_advance(&it))

//Make sure we have an array of containers and check size
PARSER_ASSERT_OR_ERROR(cbor_value_is_array(&it), parser_unexpected_type)

CHECK_CBOR_MAP_ERR(cbor_value_get_array_length(&it, &v->tx_text.n_containers))
// Limit max fields to 255
PARSER_ASSERT_OR_ERROR((v->tx_text.n_containers > 0 && v->tx_text.n_containers <= UINT8_MAX), parser_unexpected_number_items)

CborValue containerArray_ptr;
CborValue data;
Cbor_container container;

// Enter array of containers
CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&it, &containerArray_ptr))

for (size_t i = 0; i < v->tx_text.n_containers; i++) {
MEMZERO(&container, sizeof(container));

CHECK_CBOR_MAP_ERR(cbor_value_get_map_length(&containerArray_ptr, &container.n_field))
PARSER_ASSERT_OR_ERROR((container.n_field > 0 && container.n_field < 5), parser_unexpected_value)

CHECK_CBOR_MAP_ERR(cbor_value_enter_container(&containerArray_ptr, &data))
CHECK_PARSER_ERR(cbor_check_expert(&data, &container))

v->tx_text.n_expert += container.screen.expert ? 1 : 0;
CHECK_CBOR_MAP_ERR(cbor_value_advance(&containerArray_ptr))
}
CHECK_CBOR_MAP_ERR(cbor_value_leave_container(&it, &containerArray_ptr))

// End of buffer does not match end of parsed data
PARSER_ASSERT_OR_ERROR(it.source.ptr == c->buffer + c->bufferLen, parser_cbor_unexpected_EOF)

return parser_ok;
}
}
Loading

0 comments on commit 0adc3f0

Please sign in to comment.