@@ -28,12 +28,13 @@ BUILD_ASSERT(BT_MESH_MODEL_OP_LEN(RPR_OP_PDU_SEND) == 2, "Assumes PDU send is a
28
28
}
29
29
30
30
enum {
31
- RPR_CLI_LINK_OPEN ,
32
- RPR_CLI_NUM_FLAGS ,
31
+ BEARER_LINK_IDLE ,
32
+ BEARER_LINK_OPENING ,
33
+ BEARER_LINK_OPENED ,
33
34
};
34
35
35
36
static struct {
36
- ATOMIC_DEFINE ( flags , RPR_CLI_NUM_FLAGS ) ;
37
+ int link ;
37
38
const struct prov_bearer_cb * cb ;
38
39
struct bt_mesh_rpr_cli * cli ;
39
40
struct {
@@ -54,22 +55,23 @@ static void link_report(struct bt_mesh_rpr_cli *cli,
54
55
struct pb_remote_ctx ctx = { cli , srv };
55
56
56
57
if (link -> state == BT_MESH_RPR_LINK_ACTIVE &&
57
- !atomic_test_and_set_bit (bearer .flags , RPR_CLI_LINK_OPEN )) {
58
+ bearer .link == BEARER_LINK_OPENING ) {
59
+ bearer .link = BEARER_LINK_OPENED ;
58
60
LOG_DBG ("Opened" );
59
61
bearer .cb -> link_opened (& pb_remote_cli , & ctx );
60
62
return ;
61
63
}
62
64
63
- if (link -> state != BT_MESH_RPR_LINK_IDLE ||
64
- !atomic_test_and_clear_bit (bearer .flags , RPR_CLI_LINK_OPEN )) {
65
- return ;
66
- }
65
+ if (link -> state == BT_MESH_RPR_LINK_IDLE &&
66
+ bearer .link != BEARER_LINK_IDLE ) {
67
+ bearer .link = BEARER_LINK_IDLE ;
67
68
68
- LOG_DBG ("Closed (%u)" , link -> status );
69
- bearer .cb -> link_closed (& pb_remote_cli , & ctx ,
70
- ((link -> status == BT_MESH_RPR_SUCCESS ) ?
71
- PROV_BEARER_LINK_STATUS_SUCCESS :
72
- PROV_BEARER_LINK_STATUS_FAIL ));
69
+ LOG_DBG ("Closed (%u)" , link -> status );
70
+ bearer .cb -> link_closed (& pb_remote_cli , & ctx ,
71
+ ((link -> status == BT_MESH_RPR_SUCCESS ) ?
72
+ PROV_BEARER_LINK_STATUS_SUCCESS :
73
+ PROV_BEARER_LINK_STATUS_FAIL ));
74
+ }
73
75
}
74
76
75
77
static void tx_complete (struct bt_mesh_rpr_cli * cli , int err , void * cb_data )
@@ -142,13 +144,13 @@ static int handle_link_report(struct bt_mesh_model *mod, struct bt_mesh_msg_ctx
142
144
tx_complete (cli , - ECANCELED , cb_data );
143
145
}
144
146
145
- k_work_reschedule (& cli -> link .timeout , K_SECONDS (cli -> link .time ));
146
-
147
147
if (cli -> link .srv .addr != srv .addr ) {
148
148
LOG_DBG ("Link report from unknown server 0x%04x" , srv .addr );
149
149
return 0 ;
150
150
}
151
151
152
+ k_work_reschedule (& cli -> link .timeout , K_SECONDS (cli -> link .time ));
153
+
152
154
cli -> link .state = link .state ;
153
155
154
156
LOG_DBG ("0x%04x: status: %u state: %u" , srv .addr , link .status ,
@@ -205,8 +207,6 @@ static int handle_pdu_outbound_report(struct bt_mesh_model *mod, struct bt_mesh_
205
207
void * cb_data ;
206
208
uint8_t num ;
207
209
208
- k_work_reschedule (& cli -> link .timeout , K_SECONDS (cli -> link .time ));
209
-
210
210
if (srv .addr != cli -> link .srv .addr ) {
211
211
LOG_WRN ("Outbound report from unknown server 0x%04x" , srv .addr );
212
212
return 0 ;
@@ -216,6 +216,7 @@ static int handle_pdu_outbound_report(struct bt_mesh_model *mod, struct bt_mesh_
216
216
217
217
LOG_DBG ("0x%04x: %u" , srv .addr , num );
218
218
219
+ k_work_reschedule (& cli -> link .timeout , K_SECONDS (cli -> link .time ));
219
220
220
221
if (!bt_mesh_msg_ack_ctx_match (& cli -> ack_ctx , RPR_OP_PDU_OUTBOUND_REPORT ,
221
222
srv .addr , & cb_data ) ||
@@ -358,7 +359,7 @@ static void link_timeout(struct k_work *work)
358
359
struct bt_mesh_rpr_cli * cli = CONTAINER_OF (k_work_delayable_from_work (work ),
359
360
struct bt_mesh_rpr_cli , link .timeout );
360
361
361
- if (cli -> link . state != BT_MESH_RPR_LINK_IDLE ) {
362
+ if (bearer . link != BEARER_LINK_IDLE ) {
362
363
LOG_DBG ("" );
363
364
link_closed (cli , BT_MESH_RPR_ERR_LINK_CLOSED_BY_CLIENT );
364
365
}
@@ -578,65 +579,31 @@ static int link_open_prov(struct bt_mesh_rpr_cli *cli,
578
579
uint8_t timeout )
579
580
{
580
581
struct bt_mesh_msg_ctx ctx = LINK_CTX (srv , false);
581
- int err ;
582
-
583
- if (cli -> link .srv .addr != BT_MESH_ADDR_UNASSIGNED ) {
584
- return - EBUSY ;
585
- }
586
582
587
583
BT_MESH_MODEL_BUF_DEFINE (buf , RPR_OP_LINK_OPEN , 17 );
588
584
bt_mesh_model_msg_init (& buf , RPR_OP_LINK_OPEN );
589
585
590
586
net_buf_simple_add_mem (& buf , uuid , 16 );
591
587
592
- if (timeout ) {
593
- cli -> link .time = timeout ;
594
- }
595
-
596
588
if (cli -> link .time != LINK_TIMEOUT_SECONDS_DEFAULT ) {
597
589
net_buf_simple_add_u8 (& buf , cli -> link .time );
598
590
}
599
591
600
- link_init (cli , srv );
601
-
602
- LOG_DBG ("" );
603
-
604
- err = bt_mesh_model_send (cli -> mod , & ctx , & buf , NULL , NULL );
605
- if (err ) {
606
- link_reset (cli );
607
- }
608
-
609
- return err ;
592
+ return bt_mesh_model_send (cli -> mod , & ctx , & buf , NULL , NULL );
610
593
}
611
594
612
595
static int link_open_node (struct bt_mesh_rpr_cli * cli ,
613
596
const struct bt_mesh_rpr_node * srv ,
614
597
enum bt_mesh_rpr_node_refresh type )
615
598
{
616
599
struct bt_mesh_msg_ctx ctx = LINK_CTX (srv , false);
617
- int err ;
618
-
619
- if (cli -> link .srv .addr != BT_MESH_ADDR_UNASSIGNED ) {
620
- return - EBUSY ;
621
- }
622
600
623
601
BT_MESH_MODEL_BUF_DEFINE (buf , RPR_OP_LINK_OPEN , 1 );
624
602
bt_mesh_model_msg_init (& buf , RPR_OP_LINK_OPEN );
625
603
626
604
net_buf_simple_add_u8 (& buf , type );
627
605
628
- cli -> link .time = LINK_TIMEOUT_SECONDS_DEFAULT ;
629
-
630
- link_init (cli , srv );
631
-
632
- LOG_DBG ("" );
633
-
634
- err = bt_mesh_model_send (cli -> mod , & ctx , & buf , NULL , NULL );
635
- if (err ) {
636
- link_reset (cli );
637
- }
638
-
639
- return err ;
606
+ return bt_mesh_model_send (cli -> mod , & ctx , & buf , NULL , NULL );
640
607
}
641
608
642
609
static int link_close (struct bt_mesh_rpr_cli * cli ,
@@ -732,24 +699,37 @@ static int pb_link_open(const uint8_t uuid[16], uint8_t timeout,
732
699
const struct prov_bearer_cb * cb , void * cb_data )
733
700
{
734
701
struct pb_remote_ctx * ctx = cb_data ;
702
+ struct bt_mesh_rpr_cli * cli = ctx -> cli ;
703
+ const struct bt_mesh_rpr_node * srv = ctx -> srv ;
735
704
int err ;
736
705
706
+ if (cli -> link .srv .addr != BT_MESH_ADDR_UNASSIGNED ) {
707
+ return - EBUSY ;
708
+ }
709
+
737
710
bearer .cli = ctx -> cli ;
738
711
bearer .cb = cb ;
739
712
713
+ cli -> link .time = timeout ? timeout : LINK_TIMEOUT_SECONDS_DEFAULT ;
714
+
715
+ LOG_DBG ("timeout: %d" , cli -> link .time );
716
+
717
+ link_init (cli , srv );
718
+
740
719
if (uuid ) {
741
- err = link_open_prov (ctx -> cli , ctx -> srv , uuid , timeout );
720
+ err = link_open_prov (cli , srv , uuid , timeout );
742
721
} else {
743
- err = link_open_node (ctx -> cli , ctx -> srv , ctx -> refresh );
722
+ err = link_open_node (cli , srv , ctx -> refresh );
744
723
}
745
724
746
725
if (err ) {
726
+ link_reset (cli );
747
727
return err ;
748
728
}
749
729
750
- atomic_clear ( bearer .flags ) ;
730
+ bearer .link = BEARER_LINK_OPENING ;
751
731
752
- return err ;
732
+ return 0 ;
753
733
}
754
734
755
735
static void pb_link_close (enum prov_bearer_link_status status )
0 commit comments