Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: Mesh: Prevent duplicate cdb appkeys #69571

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/zephyr/bluetooth/mesh/cdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ void bt_mesh_cdb_node_foreach(bt_mesh_cdb_node_func_t func, void *user_data);
*
* @param net_idx NetIdx of the subnet.
*
* @return The new subnet or NULL if it cannot be allocated.
* @return The new subnet or NULL if it cannot be allocated due to
* lack of resources or the subnet has been already allocated.
*/
struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_alloc(uint16_t net_idx);

Expand Down Expand Up @@ -328,7 +329,8 @@ int bt_mesh_cdb_subnet_key_export(const struct bt_mesh_cdb_subnet *sub, int key_
* @param net_idx NetIdx of NetKey that the application key is bound to.
* @param app_idx AppIdx of the application key.
*
* @return The new application key or NULL if it cannot be allocated.
* @return The new application key or NULL if it cannot be allocated due to
* lack of resources or the key has been already allocated.
*/
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx,
uint16_t app_idx);
Expand Down
22 changes: 15 additions & 7 deletions subsys/bluetooth/mesh/cdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct bt_mesh_cdb bt_mesh_cdb = {
},
.app_keys = {
[0 ... (CONFIG_BT_MESH_CDB_APP_KEY_COUNT - 1)] = {
.app_idx = BT_MESH_KEY_UNUSED,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.net_idx = BT_MESH_KEY_UNUSED,
}
},
Expand Down Expand Up @@ -1024,26 +1025,32 @@ int bt_mesh_cdb_node_key_export(const struct bt_mesh_cdb_node *node, uint8_t out
return bt_mesh_key_export(out, &node->dev_key);
}

struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx,
uint16_t app_idx)
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx, uint16_t app_idx)
{
struct bt_mesh_cdb_app_key *key;
struct bt_mesh_cdb_app_key *vacant_key = NULL;
int i;

for (i = 0; i < ARRAY_SIZE(bt_mesh_cdb.app_keys); ++i) {
alxelax marked this conversation as resolved.
Show resolved Hide resolved
key = &bt_mesh_cdb.app_keys[i];

if (key->net_idx != BT_MESH_KEY_UNUSED) {
if (key->app_idx == app_idx) {
return NULL;
PavelVPV marked this conversation as resolved.
Show resolved Hide resolved
}

if (key->net_idx != BT_MESH_KEY_UNUSED || vacant_key) {
continue;
}

key->net_idx = net_idx;
key->app_idx = app_idx;
vacant_key = key;
}

return key;
if (vacant_key) {
vacant_key->net_idx = net_idx;
vacant_key->app_idx = app_idx;
}

return NULL;
return vacant_key;
}

void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
Expand All @@ -1055,6 +1062,7 @@ void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
}

key->net_idx = BT_MESH_KEY_UNUSED;
key->app_idx = BT_MESH_KEY_UNUSED;
alxelax marked this conversation as resolved.
Show resolved Hide resolved
bt_mesh_key_destroy(&key->keys[0].app_key);
bt_mesh_key_destroy(&key->keys[1].app_key);
memset(key->keys, 0, sizeof(key->keys));
Expand Down
Loading