@@ -62,7 +62,7 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
62
62
LOG_INF ("Primary Element: 0x%04x" , addr );
63
63
LOG_DBG ("net_idx 0x%04x flags 0x%02x iv_index 0x%04x" , net_idx , flags , iv_index );
64
64
65
- if (atomic_test_and_set_bit (bt_mesh .flags , BT_MESH_VALID )) {
65
+ if (atomic_test_bit (bt_mesh .flags , BT_MESH_VALID )) {
66
66
return - EALREADY ;
67
67
}
68
68
@@ -74,14 +74,12 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
74
74
comp = bt_mesh_comp_get ();
75
75
if (comp == NULL ) {
76
76
LOG_ERR ("Failed to get node composition" );
77
- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
78
77
return - EINVAL ;
79
78
}
80
79
81
80
subnet = bt_mesh_cdb_subnet_get (net_idx );
82
81
if (!subnet ) {
83
82
LOG_ERR ("No subnet with idx %d" , net_idx );
84
- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
85
83
return - ENOENT ;
86
84
}
87
85
@@ -90,7 +88,6 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
90
88
comp -> elem_count , net_idx );
91
89
if (node == NULL ) {
92
90
LOG_ERR ("Failed to allocate database node" );
93
- atomic_clear_bit (bt_mesh .flags , BT_MESH_VALID );
94
91
return - ENOMEM ;
95
92
}
96
93
@@ -108,42 +105,43 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
108
105
net_key );
109
106
if (err ) {
110
107
LOG_ERR ("Failed to import cdb network key" );
111
- goto end ;
108
+ goto error_exit ;
112
109
}
113
- bt_mesh_cdb_subnet_store (subnet );
114
110
115
111
addr = node -> addr ;
116
112
bt_mesh_cdb_iv_update (iv_index , BT_MESH_IV_UPDATE (flags ));
117
113
118
114
err = bt_mesh_cdb_node_key_import (node , dev_key );
119
115
if (err ) {
120
116
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 ;
126
118
}
127
119
}
128
120
129
121
err = bt_mesh_key_import (BT_MESH_KEY_TYPE_DEV , dev_key , & mesh_dev_key );
130
122
if (err ) {
131
123
LOG_ERR ("Failed to import device key" );
132
- goto end ;
124
+ goto error_exit ;
133
125
}
134
126
is_dev_key_valid = true;
135
127
136
128
err = bt_mesh_key_import (BT_MESH_KEY_TYPE_NET , net_key , & mesh_net_key );
137
129
if (err ) {
138
130
LOG_ERR ("Failed to import network key" );
139
- goto end ;
131
+ goto error_exit ;
140
132
}
141
133
is_net_key_valid = true;
142
134
143
135
err = bt_mesh_net_create (net_idx , flags , & mesh_net_key , iv_index );
144
136
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 );
147
145
}
148
146
149
147
bt_mesh_net_settings_commit ();
@@ -163,18 +161,21 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
163
161
bt_mesh_net_store ();
164
162
}
165
163
164
+ atomic_set_bit (bt_mesh .flags , BT_MESH_VALID );
166
165
bt_mesh_start ();
167
166
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);
171
172
}
172
173
173
- if (err && is_dev_key_valid ) {
174
+ if (is_dev_key_valid ) {
174
175
bt_mesh_key_destroy (& mesh_dev_key );
175
176
}
176
177
177
- if (err && is_net_key_valid ) {
178
+ if (is_net_key_valid ) {
178
179
bt_mesh_key_destroy (& mesh_net_key );
179
180
}
180
181
0 commit comments