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