Skip to content

Commit 3f4221d

Browse files
nandojvejhedberg
authored andcommitted
lib: updatehub: Refact to use bin2hex
Use bin2hex instead inline conversion. Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent 4d949ee commit 3f4221d

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

lib/updatehub/shell.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ static int cmd_info(const struct shell *shell, size_t argc, char **argv)
5555
ARG_UNUSED(argc);
5656
ARG_UNUSED(argv);
5757

58-
char *device_id = k_malloc(DEVICE_ID_MAX_SIZE);
58+
char *device_id = k_malloc(DEVICE_ID_HEX_MAX_SIZE);
5959
char *firmware_version = k_malloc(BOOT_IMG_VER_STRLEN_MAX);
6060

61+
updatehub_get_device_identity(device_id, DEVICE_ID_HEX_MAX_SIZE);
6162
updatehub_get_firmware_version(firmware_version, BOOT_IMG_VER_STRLEN_MAX);
62-
updatehub_get_device_identity(device_id, DEVICE_ID_MAX_SIZE);
6363

6464
shell_fprintf(shell, SHELL_NORMAL, "Unique device id: %s\n",
6565
device_id);

lib/updatehub/updatehub.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ static struct update_info {
7575

7676
static struct k_delayed_work updatehub_work_handle;
7777

78+
static int bin2hex_str(u8_t *bin, size_t bin_len, char *str, size_t str_buf_len)
79+
{
80+
if (bin == NULL || str == NULL) {
81+
return -1;
82+
}
83+
84+
/* ensures at least an empty string */
85+
if (str_buf_len < 1) {
86+
return -2;
87+
}
88+
89+
memset(str, 0, str_buf_len);
90+
/* str_buf_len - 1 ensure space for \0 */
91+
bin2hex(bin, bin_len, str, str_buf_len - 1);
92+
93+
return 0;
94+
}
95+
7896
static void wait_fds(void)
7997
{
8098
if (poll(ctx.fds, ctx.nfds, NETWORK_TIMEOUT) < 0) {
@@ -93,8 +111,6 @@ static int metadata_hash_get(char *metadata)
93111
{
94112
struct tc_sha256_state_struct sha256sum;
95113
unsigned char hash[TC_SHA256_DIGEST_SIZE];
96-
char buffer[3];
97-
int buffer_len = 0;
98114

99115
if (tc_sha256_init(&sha256sum) == 0) {
100116
return -1;
@@ -108,13 +124,9 @@ static int metadata_hash_get(char *metadata)
108124
return -1;
109125
}
110126

111-
memset(update_info.package_uid, 0, SHA256_HEX_DIGEST_SIZE);
112-
for (int i = 0; i < TC_SHA256_DIGEST_SIZE; i++) {
113-
snprintk(buffer, sizeof(buffer), "%02x",
114-
hash[i]);
115-
buffer_len = buffer_len + strlen(buffer);
116-
strncat(&update_info.package_uid[i], buffer,
117-
MIN(SHA256_HEX_DIGEST_SIZE - 1, buffer_len));
127+
if (bin2hex_str(hash, TC_SHA256_DIGEST_SIZE,
128+
update_info.package_uid, SHA256_HEX_DIGEST_SIZE)) {
129+
return -1;
118130
}
119131

120132
return 0;
@@ -326,26 +338,22 @@ static int send_request(enum coap_msgtype msgtype, enum coap_method method,
326338

327339
static bool install_update_cb_sha256(void)
328340
{
329-
u8_t image_hash[TC_SHA256_DIGEST_SIZE];
330-
char buffer[3], sha256_image_dowloaded[SHA256_HEX_DIGEST_SIZE];
331-
int i, buffer_len = 0;
341+
u8_t hash[TC_SHA256_DIGEST_SIZE];
342+
char sha256[SHA256_HEX_DIGEST_SIZE];
332343

333-
if (tc_sha256_final(image_hash, &ctx.sha256sum) < 1) {
344+
if (tc_sha256_final(hash, &ctx.sha256sum) < 1) {
334345
LOG_ERR("Could not finish sha256sum");
335346
return false;
336347
}
337348

338-
memset(&sha256_image_dowloaded, 0, SHA256_HEX_DIGEST_SIZE);
339-
for (i = 0; i < TC_SHA256_DIGEST_SIZE; i++) {
340-
snprintk(buffer, sizeof(buffer), "%02x", image_hash[i]);
341-
buffer_len = buffer_len + strlen(buffer);
342-
strncat(&sha256_image_dowloaded[i], buffer,
343-
MIN(SHA256_HEX_DIGEST_SIZE - 1, buffer_len));
349+
if (bin2hex_str(hash, TC_SHA256_DIGEST_SIZE,
350+
sha256, SHA256_HEX_DIGEST_SIZE)) {
351+
LOG_ERR("Could not create sha256sum hex representation");
352+
return false;
344353
}
345354

346-
if (strncmp(sha256_image_dowloaded,
347-
update_info.sha256sum_image,
348-
strlen(update_info.sha256sum_image)) != 0) {
355+
if (strncmp(sha256, update_info.sha256sum_image,
356+
SHA256_HEX_DIGEST_SIZE) != 0) {
349357
LOG_ERR("SHA256SUM of image are not the same");
350358
ctx.code_status = UPDATEHUB_DOWNLOAD_ERROR;
351359
return false;
@@ -499,15 +507,15 @@ static int report(enum updatehub_state state)
499507
struct report report;
500508
int ret = -1;
501509
const char *exec = state_name(state);
502-
char *device_id = k_malloc(DEVICE_ID_MAX_SIZE);
510+
char *device_id = k_malloc(DEVICE_ID_HEX_MAX_SIZE);
503511
char *firmware_version = k_malloc(BOOT_IMG_VER_STRLEN_MAX);
504512

505513
if (device_id == NULL || firmware_version == NULL) {
506514
LOG_ERR("Could not alloc device_id or firmware_version memory");
507515
goto error;
508516
}
509517

510-
if (!updatehub_get_device_identity(device_id, DEVICE_ID_MAX_SIZE)) {
518+
if (!updatehub_get_device_identity(device_id, DEVICE_ID_HEX_MAX_SIZE)) {
511519
goto error;
512520
}
513521

@@ -624,7 +632,7 @@ enum updatehub_response updatehub_probe(void)
624632

625633
char *metadata = k_malloc(MAX_PAYLOAD_SIZE);
626634
char *metadata_copy = k_malloc(MAX_PAYLOAD_SIZE);
627-
char *device_id = k_malloc(DEVICE_ID_MAX_SIZE);
635+
char *device_id = k_malloc(DEVICE_ID_HEX_MAX_SIZE);
628636
char *firmware_version = k_malloc(BOOT_IMG_VER_STRLEN_MAX);
629637

630638
size_t sha256size;
@@ -647,7 +655,7 @@ enum updatehub_response updatehub_probe(void)
647655
goto error;
648656
}
649657

650-
if (!updatehub_get_device_identity(device_id, DEVICE_ID_MAX_SIZE)) {
658+
if (!updatehub_get_device_identity(device_id, DEVICE_ID_HEX_MAX_SIZE)) {
651659
ctx.code_status = UPDATEHUB_METADATA_ERROR;
652660
goto error;
653661
}

lib/updatehub/updatehub_device.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@
77

88
bool updatehub_get_device_identity(char *id, int id_max_len)
99
{
10-
int i, id_len = 0;
11-
char buf[3];
12-
u8_t hwinfo_id[id_max_len];
10+
u8_t hwinfo_id[DEVICE_ID_BIN_MAX_SIZE];
1311
size_t length;
1412

15-
length = hwinfo_get_device_id(hwinfo_id, sizeof(hwinfo_id) - 1);
13+
length = hwinfo_get_device_id(hwinfo_id, DEVICE_ID_BIN_MAX_SIZE);
1614
if (length <= 0) {
1715
return false;
1816
}
1917

2018
memset(id, 0, id_max_len);
19+
length = bin2hex(hwinfo_id, length, id, id_max_len - 1);
2120

22-
for (i = 0; i < length; i++) {
23-
snprintk(buf, sizeof(buf), "%02x", hwinfo_id[i]);
24-
id_len = strlen(id);
25-
strncat(id, buf, id_max_len - id_len);
26-
}
27-
28-
return true;
21+
return length > 0;
2922
}

lib/updatehub/updatehub_device.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include <zephyr.h>
1111
#include <drivers/hwinfo.h>
1212

13-
#define DEVICE_ID_MAX_SIZE 65
13+
#define DEVICE_ID_BIN_MAX_SIZE 64
14+
#define DEVICE_ID_HEX_MAX_SIZE ((DEVICE_ID_BIN_MAX_SIZE * 2) + 1)
1415

1516
bool updatehub_get_device_identity(char *id, int id_max_len);
1617

0 commit comments

Comments
 (0)