@@ -70,6 +70,7 @@ typedef struct {
70
70
net_6lowpan_mode_e operating_mode ;
71
71
net_6lowpan_mode_extension_e operating_mode_extension ;
72
72
int8_t network_interface_id ;
73
+ bool configured_and_connected ;
73
74
} wisun_tasklet_data_str_t ;
74
75
75
76
typedef struct {
@@ -81,8 +82,6 @@ typedef NS_LIST_HEAD(wisun_certificate_entry_t, link) cert_list_t;
81
82
typedef struct {
82
83
cert_list_t own_certificates_list ;
83
84
cert_list_t trusted_certificates_list ;
84
- bool remove_own_certificates : 1 ;
85
- bool remove_trusted_certificates : 1 ;
86
85
} wisun_certificates_t ;
87
86
88
87
/* Tasklet data */
@@ -106,9 +105,9 @@ static void wisun_tasklet_main(arm_event_s *event);
106
105
static void wisun_tasklet_network_state_changed (mesh_connection_status_t status );
107
106
static void wisun_tasklet_parse_network_event (arm_event_s * event );
108
107
static void wisun_tasklet_configure_and_connect_to_network (void );
109
- static void wisun_tasklet_clear_stored_certificates (void ) ;
110
- static int wisun_tasklet_store_certificate_data (const uint8_t * cert , uint16_t cert_len , const uint8_t * cert_key , uint16_t cert_key_len , bool remove_own , bool remove_trusted , bool trusted_cert );
111
- static int wisun_tasklet_add_stored_certificates (void ) ;
108
+ static void wisun_tasklet_clear_stored_certificates (int8_t clear_trusted , int8_t clear_own ) ;
109
+ static int wisun_tasklet_store_certificate_data (const uint8_t * cert , uint16_t cert_len , const uint8_t * cert_key , uint16_t cert_key_len , bool trusted_cert );
110
+ static int wisun_tasklet_add_stored_certificates (int8_t * trusted_set , int8_t * own_set ) ;
112
111
static void wisun_tasklet_statistics_do_start (void );
113
112
114
113
/*
@@ -231,6 +230,11 @@ static void wisun_tasklet_parse_network_event(arm_event_s *event)
231
230
static void wisun_tasklet_configure_and_connect_to_network (void )
232
231
{
233
232
int status ;
233
+
234
+ if (wisun_tasklet_data_ptr -> configured_and_connected ) {
235
+ return ;
236
+ }
237
+
234
238
fhss_timer_t * fhss_timer_ptr = & fhss_functions ;
235
239
236
240
if (MBED_CONF_MBED_MESH_API_WISUN_DEVICE_TYPE == MESH_DEVICE_TYPE_WISUN_BORDER_ROUTER ) {
@@ -246,7 +250,9 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
246
250
wisun_tasklet_data_ptr -> operating_mode ,
247
251
wisun_tasklet_data_ptr -> operating_mode_extension );
248
252
249
- if (wisun_tasklet_add_stored_certificates () != 0 ) {
253
+ int8_t trusted_set = false;
254
+ int8_t own_set = false;
255
+ if (wisun_tasklet_add_stored_certificates (& trusted_set , & own_set ) != 0 ) {
250
256
tr_error ("Can't set Wi-SUN certificates" );
251
257
return ;
252
258
}
@@ -278,38 +284,41 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
278
284
}
279
285
280
286
#if defined(MBED_CONF_MBED_MESH_API_CERTIFICATE_HEADER )
281
- arm_certificate_entry_s trusted_cert = {
282
- .cert = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE ,
283
- .key = NULL ,
284
- .cert_len = 0 ,
285
- .key_len = 0
286
- };
287
+ if (!trusted_set ) {
288
+ arm_certificate_entry_s trusted_cert = {
289
+ .cert = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE ,
290
+ .key = NULL ,
291
+ .cert_len = 0 ,
292
+ .key_len = 0
293
+ };
287
294
#ifdef MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE_LEN
288
- trusted_cert .cert_len = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE_LEN ;
295
+ trusted_cert .cert_len = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE_LEN ;
289
296
#else
290
- trusted_cert .cert_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE ) + 1 ;
297
+ trusted_cert .cert_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE ) + 1 ;
291
298
#endif
292
- arm_network_trusted_certificates_remove ();
293
- arm_network_trusted_certificate_add ((const arm_certificate_entry_s * )& trusted_cert );
294
-
295
- arm_certificate_entry_s own_cert = {
296
- .cert = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE ,
297
- .key = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY ,
298
- .cert_len = 0 ,
299
- .key_len = 0
300
- };
299
+ arm_network_trusted_certificates_remove ();
300
+ arm_network_trusted_certificate_add ((const arm_certificate_entry_s * )& trusted_cert );
301
+ }
302
+ if (!own_set ) {
303
+ arm_certificate_entry_s own_cert = {
304
+ .cert = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE ,
305
+ .key = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY ,
306
+ .cert_len = 0 ,
307
+ .key_len = 0
308
+ };
301
309
#ifdef MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_LEN
302
- own_cert .cert_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_LEN ;
310
+ own_cert .cert_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_LEN ;
303
311
#else
304
- own_cert .cert_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE ) + 1 ;
312
+ own_cert .cert_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE ) + 1 ;
305
313
#endif
306
314
#ifdef MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY_LEN
307
- own_cert .key_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY_LEN ;
315
+ own_cert .key_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY_LEN ;
308
316
#else
309
- own_cert .key_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY ) + 1 ;
317
+ own_cert .key_len = strlen ((const char * ) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY ) + 1 ;
310
318
#endif
311
- arm_network_own_certificates_remove ();
312
- arm_network_own_certificate_add ((const arm_certificate_entry_s * )& own_cert );
319
+ arm_network_own_certificates_remove ();
320
+ arm_network_own_certificate_add ((const arm_certificate_entry_s * )& own_cert );
321
+ }
313
322
#endif
314
323
315
324
if (statistics_started ) {
@@ -318,6 +327,7 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
318
327
319
328
status = arm_nwk_interface_up (wisun_tasklet_data_ptr -> network_interface_id );
320
329
if (status >= 0 ) {
330
+ wisun_tasklet_data_ptr -> configured_and_connected = true;
321
331
wisun_tasklet_data_ptr -> tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED ;
322
332
tr_info ("Start Wi-SUN Bootstrap" );
323
333
wisun_tasklet_network_state_changed (MESH_BOOTSTRAP_STARTED );
@@ -326,6 +336,7 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
326
336
tr_err ("Bootstrap start failed, %d" , status );
327
337
wisun_tasklet_network_state_changed (MESH_BOOTSTRAP_START_FAILED );
328
338
}
339
+
329
340
}
330
341
331
342
/*
@@ -338,7 +349,7 @@ static void wisun_tasklet_network_state_changed(mesh_connection_status_t status)
338
349
}
339
350
}
340
351
341
- static int wisun_tasklet_store_certificate_data (const uint8_t * cert , uint16_t cert_len , const uint8_t * cert_key , uint16_t cert_key_len , bool remove_own , bool remove_trusted , bool trusted_cert )
352
+ static int wisun_tasklet_store_certificate_data (const uint8_t * cert , uint16_t cert_len , const uint8_t * cert_key , uint16_t cert_key_len , bool trusted_cert )
342
353
{
343
354
if (wisun_certificates_ptr == NULL ) {
344
355
wisun_certificates_ptr = (wisun_certificates_t * )ns_dyn_mem_alloc (sizeof (wisun_certificates_t ));
@@ -347,23 +358,11 @@ static int wisun_tasklet_store_certificate_data(const uint8_t *cert, uint16_t ce
347
358
}
348
359
ns_list_init (& wisun_certificates_ptr -> own_certificates_list );
349
360
ns_list_init (& wisun_certificates_ptr -> trusted_certificates_list );
350
- wisun_certificates_ptr -> remove_own_certificates = false;
351
- wisun_certificates_ptr -> remove_trusted_certificates = false;
352
- }
353
-
354
- if (remove_own ) {
355
- wisun_certificates_ptr -> remove_own_certificates = true;
356
- return 0 ;
357
- }
358
-
359
- if (remove_trusted ) {
360
- wisun_certificates_ptr -> remove_trusted_certificates = true;
361
- return 0 ;
362
361
}
363
362
364
363
wisun_certificate_entry_t * ws_cert_entry_store = (wisun_certificate_entry_t * )ns_dyn_mem_alloc (sizeof (wisun_certificate_entry_t ));
365
364
if (!ws_cert_entry_store ) {
366
- wisun_tasklet_clear_stored_certificates ();
365
+ wisun_tasklet_clear_stored_certificates (true, true );
367
366
return -1 ;
368
367
}
369
368
@@ -387,27 +386,33 @@ static int wisun_tasklet_store_certificate_data(const uint8_t *cert, uint16_t ce
387
386
return 0 ;
388
387
}
389
388
390
- static void wisun_tasklet_clear_stored_certificates (void )
389
+ static void wisun_tasklet_clear_stored_certificates (int8_t clear_trusted , int8_t clear_own )
391
390
{
392
391
if (!wisun_certificates_ptr ) {
393
392
return ;
394
393
}
395
394
396
- ns_list_foreach_safe (wisun_certificate_entry_t , trusted_cert_entry , & wisun_certificates_ptr -> trusted_certificates_list ) {
397
- ns_list_remove (& wisun_certificates_ptr -> trusted_certificates_list , trusted_cert_entry );
398
- ns_dyn_mem_free (trusted_cert_entry );
395
+ if (clear_trusted ) {
396
+ ns_list_foreach_safe (wisun_certificate_entry_t , trusted_cert_entry , & wisun_certificates_ptr -> trusted_certificates_list ) {
397
+ ns_list_remove (& wisun_certificates_ptr -> trusted_certificates_list , trusted_cert_entry );
398
+ ns_dyn_mem_free (trusted_cert_entry );
399
+ }
399
400
}
400
401
401
- ns_list_foreach_safe (wisun_certificate_entry_t , own_cert_entry , & wisun_certificates_ptr -> own_certificates_list ) {
402
- ns_list_remove (& wisun_certificates_ptr -> own_certificates_list , own_cert_entry );
403
- ns_dyn_mem_free (own_cert_entry );
402
+ if (clear_own ) {
403
+ ns_list_foreach_safe (wisun_certificate_entry_t , own_cert_entry , & wisun_certificates_ptr -> own_certificates_list ) {
404
+ ns_list_remove (& wisun_certificates_ptr -> own_certificates_list , own_cert_entry );
405
+ ns_dyn_mem_free (own_cert_entry );
406
+ }
404
407
}
405
408
406
- ns_dyn_mem_free (wisun_certificates_ptr );
407
- wisun_certificates_ptr = NULL ;
409
+ if (ns_list_is_empty (& wisun_certificates_ptr -> trusted_certificates_list ) && ns_list_is_empty (& wisun_certificates_ptr -> own_certificates_list )) {
410
+ ns_dyn_mem_free (wisun_certificates_ptr );
411
+ wisun_certificates_ptr = NULL ;
412
+ }
408
413
}
409
414
410
- static int wisun_tasklet_add_stored_certificates (void )
415
+ static int wisun_tasklet_add_stored_certificates (int8_t * trusted_set , int8_t * own_set )
411
416
{
412
417
int8_t status = 0 ;
413
418
@@ -416,15 +421,15 @@ static int wisun_tasklet_add_stored_certificates(void)
416
421
return 0 ;
417
422
}
418
423
419
- if (wisun_certificates_ptr -> remove_own_certificates ) {
420
- status = arm_network_own_certificates_remove ();
424
+ if (! ns_list_is_empty ( & wisun_certificates_ptr -> trusted_certificates_list ) ) {
425
+ status = arm_network_trusted_certificates_remove ();
421
426
if (status != 0 ) {
422
427
goto CERTIFICATE_SET_END ;
423
428
}
424
429
}
425
430
426
- if (wisun_certificates_ptr -> remove_trusted_certificates ) {
427
- status = arm_network_trusted_certificates_remove ();
431
+ if (! ns_list_is_empty ( & wisun_certificates_ptr -> own_certificates_list ) ) {
432
+ status = arm_network_own_certificates_remove ();
428
433
if (status != 0 ) {
429
434
goto CERTIFICATE_SET_END ;
430
435
}
@@ -435,17 +440,19 @@ static int wisun_tasklet_add_stored_certificates(void)
435
440
if (status != 0 ) {
436
441
goto CERTIFICATE_SET_END ;
437
442
}
443
+ * trusted_set = true;
438
444
}
439
445
440
446
ns_list_foreach (wisun_certificate_entry_t , cert_entry , & wisun_certificates_ptr -> own_certificates_list ) {
441
447
status = arm_network_own_certificate_add (& cert_entry -> arm_cert_entry );
442
448
if (status != 0 ) {
443
449
goto CERTIFICATE_SET_END ;
444
450
}
451
+ * own_set = true;
445
452
}
446
453
447
454
CERTIFICATE_SET_END :
448
- wisun_tasklet_clear_stored_certificates ();
455
+ wisun_tasklet_clear_stored_certificates (true, true );
449
456
450
457
return status ;
451
458
}
@@ -481,6 +488,7 @@ int8_t wisun_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id
481
488
wisun_tasklet_data_ptr -> mesh_api_cb = callback ;
482
489
wisun_tasklet_data_ptr -> network_interface_id = nwk_interface_id ;
483
490
wisun_tasklet_data_ptr -> tasklet_state = TASKLET_STATE_INITIALIZED ;
491
+ wisun_tasklet_data_ptr -> configured_and_connected = false;
484
492
485
493
if (re_connecting == false) {
486
494
wisun_tasklet_data_ptr -> tasklet = eventOS_event_handler_create (& wisun_tasklet_main ,
@@ -509,6 +517,7 @@ int8_t wisun_tasklet_disconnect(bool send_cb)
509
517
wisun_tasklet_network_state_changed (MESH_DISCONNECTED );
510
518
}
511
519
}
520
+ wisun_tasklet_data_ptr -> configured_and_connected = false;
512
521
wisun_tasklet_data_ptr -> mesh_api_cb = NULL ;
513
522
}
514
523
return status ;
@@ -522,6 +531,7 @@ void wisun_tasklet_init(void)
522
531
memset (wisun_tasklet_data_ptr , 0 , sizeof (wisun_tasklet_data_str_t ));
523
532
wisun_tasklet_data_ptr -> tasklet_state = TASKLET_STATE_CREATED ;
524
533
wisun_tasklet_data_ptr -> network_interface_id = INVALID_INTERFACE_ID ;
534
+ wisun_tasklet_data_ptr -> configured_and_connected = false;
525
535
}
526
536
}
527
537
@@ -542,7 +552,7 @@ int8_t wisun_tasklet_network_init(int8_t device_id)
542
552
543
553
int wisun_tasklet_set_own_certificate (uint8_t * cert , uint16_t cert_len , uint8_t * cert_key , uint16_t cert_key_len )
544
554
{
545
- if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID ) {
555
+ if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID && wisun_tasklet_data_ptr -> configured_and_connected ) {
546
556
// interface already active write certificates to stack.
547
557
arm_certificate_entry_s arm_cert_entry ;
548
558
arm_cert_entry .cert = cert ;
@@ -552,22 +562,40 @@ int wisun_tasklet_set_own_certificate(uint8_t *cert, uint16_t cert_len, uint8_t
552
562
return arm_network_own_certificate_add (& arm_cert_entry );
553
563
}
554
564
// Stack is inactive store the certificates and activate when connect() called
555
- return wisun_tasklet_store_certificate_data (cert , cert_len , cert_key , cert_key_len , false, false, false );
565
+ return wisun_tasklet_store_certificate_data (cert , cert_len , cert_key , cert_key_len , false);
556
566
}
557
567
558
568
int wisun_tasklet_remove_own_certificates (void )
559
569
{
560
- return wisun_tasklet_store_certificate_data (NULL , 0 , NULL , 0 , true, false, false);
570
+ if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID && wisun_tasklet_data_ptr -> configured_and_connected ) {
571
+ int8_t status = arm_network_own_certificates_remove ();
572
+ if (status != 0 ) {
573
+ return -1 ;
574
+ }
575
+ return 0 ;
576
+ }
577
+
578
+ wisun_tasklet_clear_stored_certificates (false, true);
579
+ return 0 ;
561
580
}
562
581
563
582
int wisun_tasklet_remove_trusted_certificates (void )
564
583
{
565
- return wisun_tasklet_store_certificate_data (NULL , 0 , NULL , 0 , false, true, false);
584
+ if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID && wisun_tasklet_data_ptr -> configured_and_connected ) {
585
+ int8_t status = arm_network_trusted_certificates_remove ();
586
+ if (status != 0 ) {
587
+ return -1 ;
588
+ }
589
+ return 0 ;
590
+ }
591
+
592
+ wisun_tasklet_clear_stored_certificates (true, false);
593
+ return 0 ;
566
594
}
567
595
568
596
int wisun_tasklet_set_trusted_certificate (uint8_t * cert , uint16_t cert_len )
569
597
{
570
- if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID ) {
598
+ if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr -> network_interface_id != INVALID_INTERFACE_ID && wisun_tasklet_data_ptr -> configured_and_connected ) {
571
599
// interface already active write certificates to stack.
572
600
arm_certificate_entry_s arm_cert_entry ;
573
601
arm_cert_entry .cert = cert ;
@@ -577,7 +605,7 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len)
577
605
return arm_network_trusted_certificate_add (& arm_cert_entry );
578
606
}
579
607
// Stack is inactive store the certificates and activate when connect() called
580
- return wisun_tasklet_store_certificate_data (cert , cert_len , NULL , 0 , false, false, true);
608
+ return wisun_tasklet_store_certificate_data (cert , cert_len , NULL , 0 , true);
581
609
}
582
610
583
611
int wisun_tasklet_statistics_start (void )
0 commit comments