Skip to content

Commit

Permalink
Merge pull request #78 from cosmos/dev_textual_title_truncation
Browse files Browse the repository at this point in the history
Dev textual title truncation
  • Loading branch information
chcmedeiros authored Jan 27, 2023
2 parents 9a78592 + 922c3ea commit 157b1fa
Show file tree
Hide file tree
Showing 116 changed files with 330 additions and 340 deletions.
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=2
# This is the `spec_version` field of `Runtime`
APPVERSION_N=35
# This is the patch version of this release
APPVERSION_P=1
APPVERSION_P=2
39 changes: 32 additions & 7 deletions app/src/cbor/cbor_parser_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,41 @@ static parser_error_t cbor_check_optFields(CborValue *data, Cbor_container *cont

static parser_error_t cbor_check_screen(CborValue *data, Cbor_container *container) {
int screen_key;
//check screen Key
//check title Key
PARSER_ASSERT_OR_ERROR(cbor_value_is_integer(data), parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_get_int(data, &screen_key))
PARSER_ASSERT_OR_ERROR(screen_key==SCREEN_KEY_ID, parser_unexpected_type)
if (screen_key != TITLE_KEY_ID) {
PARSER_ASSERT_OR_ERROR(screen_key==CONTENT_KEY_ID, parser_unexpected_type)

// No title
container->screen.titlePtr = NULL;
container->screen.titleLen = 0;
CHECK_CBOR_MAP_ERR(cbor_value_advance(data))

//get content ptr
READ_STRING_PTR_SIZE(data, container->screen.contentPtr, container->screen.contentLen)
PARSER_ASSERT_OR_ERROR(container->screen.contentLen <= MAX_CONTENT_SIZE, parser_unexpected_value)
return parser_ok;
}

CHECK_CBOR_MAP_ERR(cbor_value_advance(data))

//get screen
READ_STRING_PTR_SIZE(data, container->screen.dataPtr, container->screen.len)
PARSER_ASSERT_OR_ERROR(container->screen.len <= MAX_SCREEN_SIZE, parser_unexpected_value)
//get title ptr
READ_STRING_PTR_SIZE(data, container->screen.titlePtr, container->screen.titleLen)
PARSER_ASSERT_OR_ERROR(container->screen.titleLen <= MAX_CONTENT_SIZE, parser_unexpected_value)

CHECK_CBOR_MAP_ERR(cbor_value_advance(data));

//check content Key
PARSER_ASSERT_OR_ERROR(cbor_value_is_integer(data), parser_unexpected_type)
CHECK_CBOR_MAP_ERR(cbor_value_get_int(data, &screen_key))
PARSER_ASSERT_OR_ERROR(screen_key==CONTENT_KEY_ID, parser_unexpected_type)

CHECK_CBOR_MAP_ERR(cbor_value_advance(data))

//get content ptr
READ_STRING_PTR_SIZE(data, container->screen.contentPtr, container->screen.contentLen)
PARSER_ASSERT_OR_ERROR(container->screen.contentLen <= MAX_CONTENT_SIZE, parser_unexpected_value)

return parser_ok;
}
Expand All @@ -91,8 +116,8 @@ parser_error_t cbor_get_containerInfo(CborValue *data, Cbor_container *container
CHECK_PARSER_ERR(cbor_check_screen(data, container))
CHECK_CBOR_MAP_ERR(cbor_value_advance(data))

if (container->n_field > 1) {
container->n_field -= 1;
if (container->n_field > 2) {
container->n_field -= 2;
CHECK_PARSER_ERR(cbor_check_optFields(data, container))
} else {
container->screen.indent = 0;
Expand Down
7 changes: 5 additions & 2 deletions app/src/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ extern "C" {
#define OUTPUT_HANDLER_SIZE 500
#endif

#define MAX_SCREEN_SIZE 600
#define MAX_KEY_LENGTH 25
#define MAX_CONTENT_SIZE 600
#define MAX_TITLE_SIZE 40
#define PRINTABLE_TITLE_SIZE 17
#define PRINTABLE_PAGINATED_TITLE_SIZE 10
#define SCREEN_BREAK ":"
#define SCREEN_INDENT ">"
#define TITLE_TRUNCATE_REPLACE "---"
#define END_OF_STRING_SIZE

const char *parser_getErrorDescription(parser_error_t err);
Expand Down
77 changes: 38 additions & 39 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,19 @@ __Z_INLINE parser_error_t parser_screenPrint(const parser_context_t *ctx,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {

if (container->screen.len > MAX_SCREEN_SIZE) {
//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;
MEMCPY(tmp, container->screen.dataPtr, container->screen.len);

//Get screen key
char *context = NULL;
char *tmp_key = strtok_r(tmp, SCREEN_BREAK, &context);
if (tmp_key == NULL || strlen(tmp_key) > MAX_SCREEN_SIZE) {
return parser_unexpected_value;
}

//Get screen val, if null we have content only screen
char *val = strtok_r(NULL, SCREEN_BREAK, &context);

size_t tmp_len = sizeof(ctx->tx_obj->tx_text.tmpBuffer);
char out[OUTPUT_HANDLER_SIZE] = {0};

// No Tittle screen
if (val == NULL) {
tx_display_translation(out, sizeof(out),tmp_key);
if (container->screen.titleLen == 0) {
MEMCPY(tmp, container->screen.contentPtr, container->screen.contentLen);
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, "");
}
Expand All @@ -335,34 +327,39 @@ __Z_INLINE parser_error_t parser_screenPrint(const parser_context_t *ctx,
return parser_ok;
}

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

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

val += (val[0] == ' ' ? 1 :0);
if (val[strlen(val)-1] == ' ' || val[strlen(val)-1] == '@' ) {
if (strlen(val)+1 > OUTPUT_HANDLER_SIZE) {
return parser_unexpected_value;
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 &&
(strlen(out)%outValLen) != 0 && 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, "");
}
strncat(val,"@",strlen(val)+1);
}

tx_display_translation(out, sizeof(out),val);
// Is content paginated ? trim tittle to 11 char
if (outValLen > 0 && (strlen(out)/outValLen) >= 1 && (strlen(out)%outValLen) != 0) {
key[11] = 0;
MEMZERO(ctx->tx_obj->tx_text.tmpBuffer, sizeof(ctx->tx_obj->tx_text.tmpBuffer));
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};
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;
}
Expand Down Expand Up @@ -429,8 +426,10 @@ __Z_INLINE parser_error_t parser_getTextualItem(const parser_context_t *ctx,
CHECK_APP_CANARY()

Cbor_container container;
container.screen.dataPtr = NULL;
container.screen.len = 0;
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))
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ parser_error_t _read_text_tx(parser_context_t *c, parser_tx_t *v) {
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 < 4), parser_unexpected_value)
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))
Expand Down
15 changes: 9 additions & 6 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ extern "C" {
#include "cbor.h"

#define MAX_NUMBER_SCREENS 50
#define SCREEN_KEY_ID 1
#define INDENT_KEY_ID 2
#define EXPERT_KEY_ID 3
#define TITLE_KEY_ID 1
#define CONTENT_KEY_ID 2
#define INDENT_KEY_ID 3
#define EXPERT_KEY_ID 4

typedef struct screen_arg_t {
char *dataPtr;
size_t len;
char *titlePtr;
size_t titleLen;
char *contentPtr;
size_t contentLen;
uint8_t indent;
bool expert;
} screen_arg_t;

typedef struct tx_textual_t{
size_t n_containers;
uint8_t n_expert;
uint8_t tmpBuffer[600];
uint8_t tmpBuffer[650];
} tx_textual_t;

typedef struct {
Expand Down
15 changes: 12 additions & 3 deletions app/src/tx_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,11 @@ static const ascii_subst_t ascii_substitutions[] = {
{0x0B, 'v'}, {0x5C, '\\'},
};

parser_error_t tx_display_translation(char *dst, uint16_t dstLen, char *src) {
parser_error_t tx_display_translation(char *dst, uint16_t dstLen, char *src, uint16_t srcLen) {
MEMZERO(dst, dstLen);
char *p = src;
uint8_t count = 0;
uint8_t verified_bytes = 0;

while (*p) {
utf8_int32_t tmp_codepoint = 0;
Expand Down Expand Up @@ -556,17 +557,25 @@ parser_error_t tx_display_translation(char *dst, uint16_t dstLen, char *src) {
swapped = (swapped >> 16) & 0xFFFF;
}

if(dstLen < bytes_to_print) {
if (dstLen < bytes_to_print) {
return parser_unexpected_value;
}

char buf[18] = {0};
array_to_hexstr(buf, sizeof(buf), (uint8_t *)&swapped, 8);
array_to_hexstr(buf, sizeof(buf), (uint8_t *) &swapped, 8);
for (int i = 0; i < bytes_to_print; i++) {
*dst++ = (buf[i] >= 'a' && buf[i] <= 'z') ? (buf[i] - 32) : buf[i];
ASSERT_PTR_BOUNDS(count, dstLen);
}
}
verified_bytes ++;
}

if (src[srcLen - 1] == ' ' || src[srcLen - 1] == '@') {
if (src[dstLen - 1] + 1 > dstLen) {
return parser_unexpected_value;
}
*dst++ = '@';
}

// Terminate string
Expand Down
2 changes: 1 addition & 1 deletion app/src/tx_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ parser_error_t tx_display_numItems(uint8_t *num_items);

parser_error_t tx_display_make_friendly();

parser_error_t tx_display_translation(char *dst, uint16_t dstLen, char *src);
parser_error_t tx_display_translation(char *dst, uint16_t dstLen, char *src, uint16_t srcLen);
//---------------------------------------------

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 157b1fa

Please sign in to comment.