Lines Matching +full:key +full:- +full:int
4 * SPDX-License-Identifier: Apache-2.0
30 * bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key
37 clear:1; /* 1 if key needs clearing, 0 if storing */
86 [0 ... (CONFIG_BT_MESH_CDB_NODE_COUNT - 1)] = {
91 [0 ... (CONFIG_BT_MESH_CDB_SUBNET_COUNT - 1)] = {
96 [0 ... (CONFIG_BT_MESH_CDB_APP_KEY_COUNT - 1)] = {
104 * Check if an address range from addr_start for addr_start + num_elem - 1 is
106 * available after the conflicting range and -EAGAIN will be returned.
108 static int addr_is_free(uint16_t addr_start, uint8_t num_elem, uint16_t *next) in addr_is_free()
110 uint16_t addr_end = addr_start + num_elem - 1; in addr_is_free()
112 int i; in addr_is_free()
117 return -EINVAL; in addr_is_free()
123 if (node->addr == BT_MESH_ADDR_UNASSIGNED) { in addr_is_free()
127 other_start = node->addr; in addr_is_free()
128 other_end = other_start + node->num_elem - 1; in addr_is_free()
135 return -EAGAIN; in addr_is_free()
155 int err, i; in find_lowest_free_addr()
166 } else if (err != -EAGAIN) { in find_lowest_free_addr()
177 static int cdb_net_set(const char *name, size_t len_rd, in cdb_net_set()
181 int err; in cdb_net_set()
213 static int cdb_node_set(const char *name, size_t len_rd, in cdb_node_set()
220 int err; in cdb_node_set()
224 return -ENOENT; in cdb_node_set()
255 return -ENOMEM; in cdb_node_set()
259 atomic_set_bit(node->flags, BT_MESH_CDB_NODE_CONFIGURED); in cdb_node_set()
262 memcpy(node->uuid, val.uuid, 16); in cdb_node_set()
268 bt_mesh_key_assign(&node->dev_key, &tmp); in cdb_node_set()
275 static int cdb_subnet_set(const char *name, size_t len_rd, in cdb_subnet_set()
279 struct net_key_val key; in cdb_subnet_set() local
282 int err; in cdb_subnet_set()
286 return -ENOENT; in cdb_subnet_set()
296 return -ENOENT; in cdb_subnet_set()
304 err = bt_mesh_settings_set(read_cb, cb_arg, &key, sizeof(key)); in cdb_subnet_set()
306 LOG_ERR("Failed to set \'net-key\'"); in cdb_subnet_set()
310 /* One extra copying since key.val[] is from packed structure in cdb_subnet_set()
313 memcpy(&tmp[0], &key.val[0], sizeof(struct bt_mesh_key)); in cdb_subnet_set()
314 memcpy(&tmp[1], &key.val[1], sizeof(struct bt_mesh_key)); in cdb_subnet_set()
319 sub->kr_phase = key.kr_phase; in cdb_subnet_set()
320 bt_mesh_key_assign(&sub->keys[0].net_key, &tmp[0]); in cdb_subnet_set()
321 bt_mesh_key_assign(&sub->keys[1].net_key, &tmp[1]); in cdb_subnet_set()
329 return -ENOMEM; in cdb_subnet_set()
332 sub->kr_phase = key.kr_phase; in cdb_subnet_set()
333 bt_mesh_key_assign(&sub->keys[0].net_key, &tmp[0]); in cdb_subnet_set()
334 bt_mesh_key_assign(&sub->keys[1].net_key, &tmp[1]); in cdb_subnet_set()
341 static int cdb_app_key_set(const char *name, size_t len_rd, in cdb_app_key_set()
345 struct app_key_val key; in cdb_app_key_set() local
348 int err; in cdb_app_key_set()
352 return -ENOENT; in cdb_app_key_set()
369 err = bt_mesh_settings_set(read_cb, cb_arg, &key, sizeof(key)); in cdb_app_key_set()
371 LOG_ERR("Failed to set \'app-key\'"); in cdb_app_key_set()
375 /* One extra copying since key.val[] is from packed structure in cdb_app_key_set()
378 memcpy(&tmp[0], &key.val[0], sizeof(struct bt_mesh_key)); in cdb_app_key_set()
379 memcpy(&tmp[1], &key.val[1], sizeof(struct bt_mesh_key)); in cdb_app_key_set()
383 app = bt_mesh_cdb_app_key_alloc(key.net_idx, app_idx); in cdb_app_key_set()
387 LOG_ERR("No space for a new app key"); in cdb_app_key_set()
388 return -ENOMEM; in cdb_app_key_set()
391 bt_mesh_key_assign(&app->keys[0].app_key, &tmp[0]); in cdb_app_key_set()
392 bt_mesh_key_assign(&app->keys[1].app_key, &tmp[1]); in cdb_app_key_set()
399 static int cdb_set(const char *name, size_t len_rd, in cdb_set()
402 int len; in cdb_set()
407 return -ENOENT; in cdb_set()
419 return -ENOENT; in cdb_set()
434 LOG_WRN("Unknown module key %s", name); in cdb_set()
435 return -ENOENT; in cdb_set()
444 int err; in store_cdb_node()
446 val.net_idx = node->net_idx; in store_cdb_node()
447 val.num_elem = node->num_elem; in store_cdb_node()
450 if (atomic_test_bit(node->flags, BT_MESH_CDB_NODE_CONFIGURED)) { in store_cdb_node()
454 memcpy(val.uuid, node->uuid, 16); in store_cdb_node()
455 memcpy(&val.dev_key, &node->dev_key, sizeof(struct bt_mesh_key)); in store_cdb_node()
457 snprintk(path, sizeof(path), "bt/mesh/cdb/Node/%x", node->addr); in store_cdb_node()
470 int err; in clear_cdb_node()
485 struct net_key_val key; in store_cdb_subnet() local
487 int err; in store_cdb_subnet()
489 LOG_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx, in store_cdb_subnet()
490 bt_hex(&sub->keys[0].net_key, sizeof(struct bt_mesh_key))); in store_cdb_subnet()
492 memcpy(&key.val[0], &sub->keys[0].net_key, sizeof(struct bt_mesh_key)); in store_cdb_subnet()
493 memcpy(&key.val[1], &sub->keys[1].net_key, sizeof(struct bt_mesh_key)); in store_cdb_subnet()
494 key.kr_flag = 0U; /* Deprecated */ in store_cdb_subnet()
495 key.kr_phase = sub->kr_phase; in store_cdb_subnet()
497 snprintk(path, sizeof(path), "bt/mesh/cdb/Subnet/%x", sub->net_idx); in store_cdb_subnet()
499 err = settings_save_one(path, &key, sizeof(key)); in store_cdb_subnet()
510 int err; in clear_cdb_subnet()
525 struct app_key_val key; in store_cdb_app_key() local
527 int err; in store_cdb_app_key()
529 key.net_idx = app->net_idx; in store_cdb_app_key()
530 key.updated = false; in store_cdb_app_key()
531 memcpy(&key.val[0], &app->keys[0].app_key, sizeof(struct bt_mesh_key)); in store_cdb_app_key()
532 memcpy(&key.val[1], &app->keys[1].app_key, sizeof(struct bt_mesh_key)); in store_cdb_app_key()
534 snprintk(path, sizeof(path), "bt/mesh/cdb/AppKey/%x", app->app_idx); in store_cdb_app_key()
536 err = settings_save_one(path, &key, sizeof(key)); in store_cdb_app_key()
547 int err; in clear_cdb_app_key()
558 static void schedule_cdb_store(int flag) in schedule_cdb_store()
573 int i; in cdb_node_update_find()
581 if (update->addr == BT_MESH_ADDR_UNASSIGNED) { in cdb_node_update_find()
586 if (update->addr == addr) { in cdb_node_update_find()
599 LOG_DBG("Node 0x%04x", node->addr); in update_cdb_node_settings()
601 update = cdb_node_update_find(node->addr, &free_slot); in update_cdb_node_settings()
603 update->clear = !store; in update_cdb_node_settings()
612 clear_cdb_node(node->addr); in update_cdb_node_settings()
617 free_slot->addr = node->addr; in update_cdb_node_settings()
618 free_slot->clear = !store; in update_cdb_node_settings()
627 int i; in cdb_key_update_find()
635 if (!update->valid) { in cdb_key_update_find()
640 if (update->app_key != app_key) { in cdb_key_update_find()
644 if (update->key_idx == key_idx) { in cdb_key_update_find()
658 LOG_DBG("NetKeyIndex 0x%03x", sub->net_idx); in update_cdb_subnet_settings()
660 update = cdb_key_update_find(false, sub->net_idx, &free_slot); in update_cdb_subnet_settings()
662 update->clear = clear; in update_cdb_subnet_settings()
671 clear_cdb_subnet(sub->net_idx); in update_cdb_subnet_settings()
676 free_slot->valid = 1U; in update_cdb_subnet_settings()
677 free_slot->key_idx = sub->net_idx; in update_cdb_subnet_settings()
678 free_slot->app_key = 0U; in update_cdb_subnet_settings()
679 free_slot->clear = clear; in update_cdb_subnet_settings()
684 static void update_cdb_app_key_settings(const struct bt_mesh_cdb_app_key *key, in update_cdb_app_key_settings() argument
690 LOG_DBG("AppKeyIndex 0x%03x", key->app_idx); in update_cdb_app_key_settings()
692 update = cdb_key_update_find(true, key->app_idx, &free_slot); in update_cdb_app_key_settings()
694 update->clear = clear; in update_cdb_app_key_settings()
701 store_cdb_app_key(key); in update_cdb_app_key_settings()
703 clear_cdb_app_key(key->app_idx); in update_cdb_app_key_settings()
709 free_slot->valid = 1U; in update_cdb_app_key_settings()
710 free_slot->key_idx = key->app_idx; in update_cdb_app_key_settings()
711 free_slot->app_key = 1U; in update_cdb_app_key_settings()
712 free_slot->clear = clear; in update_cdb_app_key_settings()
724 LOG_DBG("Address range 0x%04x-0x%04x is not free", addr, in addr_assign()
725 addr + num_elem - 1); in addr_assign()
732 int bt_mesh_cdb_create(const uint8_t key[16]) in bt_mesh_cdb_create()
735 int err; in bt_mesh_cdb_create()
739 return -EALREADY; in bt_mesh_cdb_create()
744 return -ENOMEM; in bt_mesh_cdb_create()
747 err = bt_mesh_key_import(BT_MESH_KEY_TYPE_NET, key, &sub->keys[0].net_key); in bt_mesh_cdb_create()
765 int i; in bt_mesh_cdb_clear()
815 int i; in bt_mesh_cdb_subnet_alloc()
824 if (sub->net_idx != BT_MESH_KEY_UNUSED) { in bt_mesh_cdb_subnet_alloc()
828 sub->net_idx = net_idx; in bt_mesh_cdb_subnet_alloc()
838 LOG_DBG("NetIdx 0x%03x store %u", sub->net_idx, store); in bt_mesh_cdb_subnet_del()
844 sub->net_idx = BT_MESH_KEY_UNUSED; in bt_mesh_cdb_subnet_del()
845 bt_mesh_key_destroy(&sub->keys[0].net_key); in bt_mesh_cdb_subnet_del()
846 bt_mesh_key_destroy(&sub->keys[1].net_key); in bt_mesh_cdb_subnet_del()
847 memset(sub->keys, 0, sizeof(sub->keys)); in bt_mesh_cdb_subnet_del()
852 int i; in bt_mesh_cdb_subnet_get()
885 int bt_mesh_cdb_subnet_key_import(struct bt_mesh_cdb_subnet *sub, int key_idx, in bt_mesh_cdb_subnet_key_import()
888 if (!bt_mesh_key_compare(in, &sub->keys[key_idx].net_key)) { in bt_mesh_cdb_subnet_key_import()
892 bt_mesh_key_destroy(&sub->keys[key_idx].net_key); in bt_mesh_cdb_subnet_key_import()
894 return bt_mesh_key_import(BT_MESH_KEY_TYPE_NET, in, &sub->keys[key_idx].net_key); in bt_mesh_cdb_subnet_key_import()
897 int bt_mesh_cdb_subnet_key_export(const struct bt_mesh_cdb_subnet *sub, int key_idx, in bt_mesh_cdb_subnet_key_export()
900 return bt_mesh_key_export(out, &sub->keys[key_idx].net_key); in bt_mesh_cdb_subnet_key_export()
906 int i; in bt_mesh_cdb_node_alloc()
916 if (node->addr == BT_MESH_ADDR_UNASSIGNED) { in bt_mesh_cdb_node_alloc()
917 memcpy(node->uuid, uuid, 16); in bt_mesh_cdb_node_alloc()
918 node->addr = addr; in bt_mesh_cdb_node_alloc()
919 node->num_elem = num_elem; in bt_mesh_cdb_node_alloc()
920 node->net_idx = net_idx; in bt_mesh_cdb_node_alloc()
921 atomic_set(node->flags, 0); in bt_mesh_cdb_node_alloc()
936 LOG_DBG("Node addr 0x%04x store %u", node->addr, store); in bt_mesh_cdb_node_del()
942 if (store && node->addr + node->num_elem > bt_mesh_cdb.lowest_avail_addr) { in bt_mesh_cdb_node_del()
943 bt_mesh_cdb.lowest_avail_addr = node->addr + node->num_elem; in bt_mesh_cdb_node_del()
950 node->addr = BT_MESH_ADDR_UNASSIGNED; in bt_mesh_cdb_node_del()
951 bt_mesh_key_destroy(&node->dev_key); in bt_mesh_cdb_node_del()
952 memset(&node->dev_key, 0, sizeof(node->dev_key)); in bt_mesh_cdb_node_del()
958 /* Address is used as a key to the nodes array. Remove the current entry first, then store in bt_mesh_cdb_node_update()
965 node->addr = addr; in bt_mesh_cdb_node_update()
966 node->num_elem = num_elem; in bt_mesh_cdb_node_update()
975 int i; in bt_mesh_cdb_node_get()
980 if (addr >= node->addr && in bt_mesh_cdb_node_get()
981 addr <= node->addr + node->num_elem - 1) { in bt_mesh_cdb_node_get()
998 int i; in bt_mesh_cdb_node_foreach()
1012 int bt_mesh_cdb_node_key_import(struct bt_mesh_cdb_node *node, const uint8_t in[16]) in bt_mesh_cdb_node_key_import()
1014 if (!bt_mesh_key_compare(in, &node->dev_key)) { in bt_mesh_cdb_node_key_import()
1018 bt_mesh_key_destroy(&node->dev_key); in bt_mesh_cdb_node_key_import()
1020 return bt_mesh_key_import(BT_MESH_KEY_TYPE_DEV, in, &node->dev_key); in bt_mesh_cdb_node_key_import()
1023 int bt_mesh_cdb_node_key_export(const struct bt_mesh_cdb_node *node, uint8_t out[16]) in bt_mesh_cdb_node_key_export()
1025 return bt_mesh_key_export(out, &node->dev_key); in bt_mesh_cdb_node_key_export()
1030 struct bt_mesh_cdb_app_key *key; in bt_mesh_cdb_app_key_alloc() local
1032 int i; in bt_mesh_cdb_app_key_alloc()
1035 key = &bt_mesh_cdb.app_keys[i]; in bt_mesh_cdb_app_key_alloc()
1037 if (key->app_idx == app_idx) { in bt_mesh_cdb_app_key_alloc()
1041 if (key->net_idx != BT_MESH_KEY_UNUSED || vacant_key) { in bt_mesh_cdb_app_key_alloc()
1045 vacant_key = key; in bt_mesh_cdb_app_key_alloc()
1049 vacant_key->net_idx = net_idx; in bt_mesh_cdb_app_key_alloc()
1050 vacant_key->app_idx = app_idx; in bt_mesh_cdb_app_key_alloc()
1056 void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store) in bt_mesh_cdb_app_key_del() argument
1058 LOG_DBG("AppIdx 0x%03x store %u", key->app_idx, store); in bt_mesh_cdb_app_key_del()
1061 update_cdb_app_key_settings(key, false); in bt_mesh_cdb_app_key_del()
1064 key->net_idx = BT_MESH_KEY_UNUSED; in bt_mesh_cdb_app_key_del()
1065 key->app_idx = BT_MESH_KEY_UNUSED; in bt_mesh_cdb_app_key_del()
1066 bt_mesh_key_destroy(&key->keys[0].app_key); in bt_mesh_cdb_app_key_del()
1067 bt_mesh_key_destroy(&key->keys[1].app_key); in bt_mesh_cdb_app_key_del()
1068 memset(key->keys, 0, sizeof(key->keys)); in bt_mesh_cdb_app_key_del()
1073 int i; in bt_mesh_cdb_app_key_get()
1076 struct bt_mesh_cdb_app_key *key = &bt_mesh_cdb.app_keys[i]; in bt_mesh_cdb_app_key_get() local
1078 if (key->net_idx != BT_MESH_KEY_UNUSED && in bt_mesh_cdb_app_key_get()
1079 key->app_idx == app_idx) { in bt_mesh_cdb_app_key_get()
1080 return key; in bt_mesh_cdb_app_key_get()
1087 void bt_mesh_cdb_app_key_store(const struct bt_mesh_cdb_app_key *key) in bt_mesh_cdb_app_key_store() argument
1090 update_cdb_app_key_settings(key, true); in bt_mesh_cdb_app_key_store()
1094 int bt_mesh_cdb_app_key_import(struct bt_mesh_cdb_app_key *key, int key_idx, const uint8_t in[16]) in bt_mesh_cdb_app_key_import() argument
1096 if (!bt_mesh_key_compare(in, &key->keys[key_idx].app_key)) { in bt_mesh_cdb_app_key_import()
1100 bt_mesh_key_destroy(&key->keys[key_idx].app_key); in bt_mesh_cdb_app_key_import()
1102 return bt_mesh_key_import(BT_MESH_KEY_TYPE_APP, in, &key->keys[key_idx].app_key); in bt_mesh_cdb_app_key_import()
1105 int bt_mesh_cdb_app_key_export(const struct bt_mesh_cdb_app_key *key, int key_idx, uint8_t out[16]) in bt_mesh_cdb_app_key_export() argument
1107 return bt_mesh_key_export(out, &key->keys[key_idx].app_key); in bt_mesh_cdb_app_key_export()
1112 int err; in clear_cdb_net()
1125 int err; in store_cdb_pending_net()
1144 int i; in store_cdb_pending_nodes()
1150 if (update->addr == BT_MESH_ADDR_UNASSIGNED) { in store_cdb_pending_nodes()
1154 addr = update->addr; in store_cdb_pending_nodes()
1155 update->addr = BT_MESH_ADDR_UNASSIGNED; in store_cdb_pending_nodes()
1157 LOG_DBG("addr: 0x%04x, clear: %d", addr, update->clear); in store_cdb_pending_nodes()
1159 if (update->clear) { in store_cdb_pending_nodes()
1176 int i; in store_cdb_pending_keys()
1181 if (!update->valid) { in store_cdb_pending_keys()
1185 update->valid = 0U; in store_cdb_pending_keys()
1187 if (update->clear) { in store_cdb_pending_keys()
1188 if (update->app_key) { in store_cdb_pending_keys()
1189 clear_cdb_app_key(update->key_idx); in store_cdb_pending_keys()
1191 clear_cdb_subnet(update->key_idx); in store_cdb_pending_keys()
1194 if (update->app_key) { in store_cdb_pending_keys()
1195 struct bt_mesh_cdb_app_key *key; in store_cdb_pending_keys() local
1197 key = bt_mesh_cdb_app_key_get(update->key_idx); in store_cdb_pending_keys()
1198 if (key) { in store_cdb_pending_keys()
1199 store_cdb_app_key(key); in store_cdb_pending_keys()
1201 LOG_WRN("AppKeyIndex 0x%03x not found", update->key_idx); in store_cdb_pending_keys()
1206 sub = bt_mesh_cdb_subnet_get(update->key_idx); in store_cdb_pending_keys()
1210 LOG_WRN("NetKeyIndex 0x%03x not found", update->key_idx); in store_cdb_pending_keys()