@@ -62,7 +62,7 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
6262 LOG_INF ("Primary Element: 0x%04x" , addr );
6363 LOG_DBG ("net_idx 0x%04x flags 0x%02x iv_index 0x%04x" , net_idx , flags , iv_index );
6464
65- if (atomic_test_and_set_bit (bt_mesh .flags , BT_MESH_VALID )) {
65+ if (atomic_test_bit (bt_mesh .flags , BT_MESH_VALID )) {
6666 return - EALREADY ;
6767 }
6868
@@ -74,14 +74,12 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
7474 comp = bt_mesh_comp_get ();
7575 if (comp == NULL ) {
7676 LOG_ERR ("Failed to get node composition" );
77- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
7877 return - EINVAL ;
7978 }
8079
8180 subnet = bt_mesh_cdb_subnet_get (net_idx );
8281 if (!subnet ) {
8382 LOG_ERR ("No subnet with idx %d" , net_idx );
84- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
8583 return - ENOENT ;
8684 }
8785
@@ -90,7 +88,6 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
9088 comp -> elem_count , net_idx );
9189 if (node == NULL ) {
9290 LOG_ERR ("Failed to allocate database node" );
93- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
9491 return - ENOMEM ;
9592 }
9693
@@ -108,42 +105,43 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
108105 net_key );
109106 if (err ) {
110107 LOG_ERR ("Failed to import cdb network key" );
111- goto end ;
108+ goto error_exit ;
112109 }
113- bt_mesh_cdb_subnet_store (subnet );
114110
115111 addr = node -> addr ;
116112 bt_mesh_cdb_iv_update (iv_index , BT_MESH_IV_UPDATE (flags ));
117113
118114 err = bt_mesh_cdb_node_key_import (node , dev_key );
119115 if (err ) {
120116 LOG_ERR ("Failed to import cdb device key" );
121- goto end ;
122- }
123-
124- if (IS_ENABLED (CONFIG_BT_SETTINGS )) {
125- bt_mesh_cdb_node_store (node );
117+ goto error_exit ;
126118 }
127119 }
128120
129121 err = bt_mesh_key_import (BT_MESH_KEY_TYPE_DEV , dev_key , & mesh_dev_key );
130122 if (err ) {
131123 LOG_ERR ("Failed to import device key" );
132- goto end ;
124+ goto error_exit ;
133125 }
134126 is_dev_key_valid = true;
135127
136128 err = bt_mesh_key_import (BT_MESH_KEY_TYPE_NET , net_key , & mesh_net_key );
137129 if (err ) {
138130 LOG_ERR ("Failed to import network key" );
139- goto end ;
131+ goto error_exit ;
140132 }
141133 is_net_key_valid = true;
142134
143135 err = bt_mesh_net_create (net_idx , flags , & mesh_net_key , iv_index );
144136 if (err ) {
145- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
146- goto end ;
137+ LOG_ERR ("Failed to create network" );
138+ goto error_exit ;
139+ }
140+
141+ if (IS_ENABLED (CONFIG_BT_MESH_CDB ) &&
142+ atomic_test_bit (bt_mesh_cdb .flags , BT_MESH_CDB_VALID )) {
143+ bt_mesh_cdb_subnet_store (subnet );
144+ bt_mesh_cdb_node_store (node );
147145 }
148146
149147 bt_mesh_net_settings_commit ();
@@ -163,18 +161,21 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
163161 bt_mesh_net_store ();
164162 }
165163
164+ atomic_set_bit (bt_mesh .flags , BT_MESH_VALID );
166165 bt_mesh_start ();
167166
168- end :
169- if (err && node != NULL && IS_ENABLED (CONFIG_BT_MESH_CDB )) {
170- bt_mesh_cdb_node_del (node , true);
167+ return 0 ;
168+
169+ error_exit :
170+ if (node != NULL && IS_ENABLED (CONFIG_BT_MESH_CDB )) {
171+ bt_mesh_cdb_node_del (node , false);
171172 }
172173
173- if (err && is_dev_key_valid ) {
174+ if (is_dev_key_valid ) {
174175 bt_mesh_key_destroy (& mesh_dev_key );
175176 }
176177
177- if (err && is_net_key_valid ) {
178+ if (is_net_key_valid ) {
178179 bt_mesh_key_destroy (& mesh_net_key );
179180 }
180181
0 commit comments