@@ -322,12 +322,40 @@ static int send_request(enum coap_msgtype msgtype, enum coap_method method,
322
322
return ret ;
323
323
}
324
324
325
+ static bool install_update_cb_sha256 (void )
326
+ {
327
+ u8_t image_hash [TC_SHA256_DIGEST_SIZE ];
328
+ char buffer [3 ], sha256_image_dowloaded [TC_SHA256_BLOCK_SIZE + 1 ];
329
+ int i , buffer_len = 0 ;
330
+
331
+ if (tc_sha256_final (image_hash , & ctx .sha256sum ) < 1 ) {
332
+ LOG_ERR ("Could not finish sha256sum" );
333
+ return false;
334
+ }
335
+
336
+ memset (& sha256_image_dowloaded , 0 , TC_SHA256_BLOCK_SIZE + 1 );
337
+ for (i = 0 ; i < TC_SHA256_DIGEST_SIZE ; i ++ ) {
338
+ snprintk (buffer , sizeof (buffer ), "%02x" , image_hash [i ]);
339
+ buffer_len = buffer_len + strlen (buffer );
340
+ strncat (& sha256_image_dowloaded [i ], buffer ,
341
+ MIN (TC_SHA256_BLOCK_SIZE , buffer_len ));
342
+ }
343
+
344
+ if (strncmp (sha256_image_dowloaded ,
345
+ update_info .sha256sum_image ,
346
+ strlen (update_info .sha256sum_image )) != 0 ) {
347
+ LOG_ERR ("SHA256SUM of image are not the same" );
348
+ ctx .code_status = UPDATEHUB_DOWNLOAD_ERROR ;
349
+ return false;
350
+ }
351
+
352
+ return true;
353
+ }
354
+
325
355
static void install_update_cb (void )
326
356
{
327
357
struct coap_packet response_packet ;
328
- char buffer [3 ], sha256_image_dowloaded [TC_SHA256_BLOCK_SIZE + 1 ];
329
358
u8_t * data = k_malloc (MAX_DOWNLOAD_DATA );
330
- int i , buffer_len = 0 ;
331
359
int rcvd = -1 ;
332
360
333
361
if (data == NULL ) {
@@ -383,26 +411,7 @@ static void install_update_cb(void)
383
411
}
384
412
385
413
if (ctx .downloaded_size == ctx .block .total_size ) {
386
- u8_t image_hash [TC_SHA256_DIGEST_SIZE ];
387
-
388
- if (tc_sha256_final (image_hash , & ctx .sha256sum ) < 1 ) {
389
- LOG_ERR ("Could not finish sha256sum" );
390
- ctx .code_status = UPDATEHUB_DOWNLOAD_ERROR ;
391
- goto cleanup ;
392
- }
393
-
394
- memset (& sha256_image_dowloaded , 0 , TC_SHA256_BLOCK_SIZE + 1 );
395
- for (i = 0 ; i < TC_SHA256_DIGEST_SIZE ; i ++ ) {
396
- snprintk (buffer , sizeof (buffer ), "%02x" , image_hash [i ]);
397
- buffer_len = buffer_len + strlen (buffer );
398
- strncat (& sha256_image_dowloaded [i ], buffer ,
399
- MIN (TC_SHA256_BLOCK_SIZE , buffer_len ));
400
- }
401
-
402
- if (strncmp (sha256_image_dowloaded ,
403
- update_info .sha256sum_image ,
404
- strlen (update_info .sha256sum_image )) != 0 ) {
405
- LOG_ERR ("SHA256SUM of image are not the same" );
414
+ if (!install_update_cb_sha256 ())
406
415
ctx .code_status = UPDATEHUB_DOWNLOAD_ERROR ;
407
416
goto cleanup ;
408
417
}
0 commit comments