4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * @author: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
32 #include "bt-service-common.h"
33 #include "bt-service-core-adapter.h"
34 #include "bt-service-event-receiver.h"
35 #include "bt-request-handler.h"
36 #include "bluetooth-api.h"
38 #include "bluetooth-api.h"
39 #include "bluetooth-mesh-api.h"
40 #include "bt-internal-types.h"
41 #include "bt-service-util.h"
42 #include "bt-service-common.h"
43 #include "bt-service-event.h"
45 #include "bt-service-mesh-network.h"
46 #include "bt-service-mesh-cdb.h"
47 #include "bt-service-mesh-main.h"
48 #include "bt-service-mesh-nodes.h"
49 #include "bt-service-mesh-keys.h"
50 #include "bt-service-mesh-util.h"
52 #include "bt-internal-types.h"
54 #include <oal-hardware.h>
55 #include <oal-manager.h>
56 #include <oal-event.h>
57 #include <oal-adapter-mgr.h>
58 #include <oal-device-mgr.h>
61 /* Temp local node structure.
62 Create & save node temporarily till network creation */
66 bluetooth_mesh_vendor_info_t vendor_info;
67 uint8_t node_uuid[16];
69 uint16_t prim_unicast;
73 /* List of CDB's for the created networks */
74 static GSList *cdb_list = NULL;
76 /* List of local ndoes to be created: Free memory once saved in DB */
77 static GSList *temp_nodes = NULL;
79 /* Scanning state: Unprovisioned device scan */
80 static bool bt_mesh_is_scanning = false;
82 /* Provisoning state */
83 static bool bt_mesh_is_provisoning = false;
85 static gint __mesh_compare_hex_uuid(gconstpointer data,
86 gconstpointer user_data)
88 const mesh_local_node_t *u1 = data;
89 const uint8_t *u2 = user_data;
91 retv_if(NULL == u1, -1);
92 retv_if(NULL == u2, -1);
94 return memcmp(u1->node_uuid, u2, 16);
97 static gint __mesh_compare_app_network_uuid(
98 gconstpointer data, gconstpointer user_data)
100 const _bt_mesh_cdb_t *cdb = data;
101 const uint8_t *net_uuid = user_data;
103 retv_if(NULL == cdb, -1);
104 retv_if(NULL == net_uuid, -1);
106 return memcmp(net_uuid, cdb->uuid, 16);
109 static gint __mesh_compare_app_cdb_token(gconstpointer data,
110 gconstpointer user_data)
113 const _bt_mesh_cdb_t *cdb = data;
114 const char *token = user_data;
116 retv_if(NULL == cdb, -1);
117 retv_if(NULL == token, -1);
119 _bt_mesh_util_convert_hex_to_string((uint8_t *) cdb->token,
122 BT_INFO("Mesh: Match Token 1[%s] Token 2 [%s]",
124 return g_strcmp0(token_str, token);
127 static gint __mesh_compare_addr(
128 const void *a, const void *b)
130 const _bt_mesh_group_t *grp = a;
131 uint16_t addr = GPOINTER_TO_UINT(b);
132 BT_INFO("Mesh: Network group addr [0x%2.2x]", grp->grp_addr);
133 BT_INFO("Mesh: To be matched group addr [0x%2.2x]", addr);
135 return (grp->grp_addr - addr);
138 int _bt_mesh_load_app_networks(const char *app_cred)
140 int ret = BLUETOOTH_ERROR_NONE;
142 /* Load Mesh Networks for the current app */
143 if (!_bt_mesh_util_is_directory_exists(MESH_CDB_DEFAULT_DIR_PATH))
144 return BLUETOOTH_ERROR_INTERNAL;
149 static void __bt_mesh_free_temp_node(mesh_local_node_t *tmp)
153 temp_nodes = g_slist_remove(temp_nodes, tmp);
154 g_free((gpointer)tmp->app_cred);
155 g_free((gpointer)tmp->sender);
156 /* Do not free tmp->model_list here
157 This memory is also being used in HAL layer
158 This memory will be freed with the call of __mesh_client_disconnected */
159 tmp->model_list = NULL;
163 int _bt_mesh_network_request_provisioning_data_request(uint8_t net_uuid[],
167 int ret = OAL_STATUS_SUCCESS;
169 char uuid_string[33];
171 _bt_mesh_util_convert_hex_to_string(net_uuid, 16,
172 uuid_string, sizeof(uuid_string));
173 BT_INFO("Mesh: Provisioning Data requested for network [%s]",
176 unicast = _bt_mesh_node_get_next_unicast(net_uuid,
177 MESH_DEFAULT_START_ADDRESS,
178 MESH_DEFAULT_MAX_ADDRESS, count);
179 BT_INFO("Mesh: Network: Got unicast [%4.4x]", unicast);
181 memcpy(uuid.uuid, net_uuid, 16);
182 /* Register Mesh Node */
183 ret = mesh_network_send_provisioning_data(&uuid,
184 MESH_PRIMARY_NET_IDX, unicast);
185 if (ret != OAL_STATUS_SUCCESS) {
186 BT_ERR("ret: %d", ret);
187 return BLUETOOTH_ERROR_INTERNAL;
189 return BLUETOOTH_ERROR_NONE;
192 int _bt_mesh_network_remove_node_configuration(
193 bluetooth_mesh_node_info_t *node)
196 _bt_mesh_cdb_t *cdb_cfg = NULL;
197 uint8_t net_uuid[16];
198 BT_INFO("Mesh: Remove Node Configuration: Unicast [0x%2.2x]",
199 node->primary_unicast);
201 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
202 strlen(node->net_uuid), net_uuid, 16);
205 l = g_slist_find_custom(cdb_list, net_uuid,
206 __mesh_compare_app_network_uuid);
208 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
209 return BLUETOOTH_ERROR_INVALID_PARAM;
212 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
214 if (_bt_mesh_conf_delete_node(cdb_cfg, node->primary_unicast)) {
215 BT_INFO("Mesh: Node Entry deleted from Config DB");
216 if (!_bt_mesh_node_del_node(cdb_cfg, node->primary_unicast)) {
217 BT_ERR("Mesh: Node Entry could not be unloaded from memory");
218 return BLUETOOTH_ERROR_INTERNAL;
221 BT_ERR("Mesh: Node Entry could not be deleted from Config DB");
222 return BLUETOOTH_ERROR_INTERNAL;
224 return BLUETOOTH_ERROR_NONE;
227 void _bt_check_mesh_app_termination(const char *name)
230 _bt_mesh_cdb_t *cdb_cfg = NULL;
231 const char *app_cred = NULL;
232 BT_INFO("Mesh: App terminated [%s]", name);
234 /* TODO: Fetch app cred, when support is added */
236 for (l = cdb_list; l != NULL;) {
237 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
240 if (g_strcmp0(cdb_cfg->owner, name) == 0) {
242 bluetooth_mesh_network_t network;
243 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
245 _bt_mesh_util_convert_hex_to_string((uint8_t *) cdb_cfg->uuid,
246 16, network.uuid, sizeof(network.uuid));
248 BT_INFO("Mesh: Got Network for unloading: UUID [%s]",
251 if (BLUETOOTH_ERROR_NONE != _bt_mesh_network_unload(app_cred, name, &network))
252 BT_ERR("Mesh: Network unloading failed!!");
254 BT_INFO("Mesh: Network unloading Success!!");
257 _bt_mesh_handle_app_termination(name);
260 void _bt_mesh_network_unload_net_configuration(_bt_mesh_cdb_t *cdb_cfg)
262 BT_INFO("Mesh: Unload Network Configuration");
264 /* Unload Network from Keys */
265 _bt_mesh_keys_unload_net(cdb_cfg->uuid);
267 /* Unload Network from Nodes */
268 _bt_mesh_node_unload_net(cdb_cfg->uuid);
270 cdb_list = g_slist_remove(cdb_list, cdb_cfg);
271 _bt_mesh_conf_free(cdb_cfg);
272 BT_INFO("Mesh: CDB freed from memory: Remaining Networks [%d]",
273 g_slist_length(cdb_list));
276 int _bt_mesh_network_remove_net_configuration(
277 bluetooth_mesh_network_t *net)
280 char *file_path = NULL;
281 _bt_mesh_cdb_t *cdb_cfg = NULL;
282 uint8_t net_uuid[16];
283 BT_INFO("Mesh: Remove network Configuration");
285 _bt_mesh_util_convert_string_to_hex(net->uuid,
286 strlen(net->uuid), net_uuid, 16);
289 l = g_slist_find_custom(cdb_list, net_uuid,
290 __mesh_compare_app_network_uuid);
292 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
293 return BLUETOOTH_ERROR_INVALID_PARAM;
296 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
298 /* Create the CDB for the network */
299 file_path = g_strdup_printf("%s.bak", cdb_cfg->cfg_fname);
301 BT_INFO("Mesh: CDB File path[%s]", cdb_cfg->cfg_fname);
302 BT_INFO("Mesh: CDB Backup File path[%s]", file_path);
304 /* Remove the config */
305 if (!_bt_mesh_util_delete_file(cdb_cfg->cfg_fname)) {
307 return BLUETOOTH_ERROR_INTERNAL;
310 /* Remove the config backup */
311 if (!_bt_mesh_util_delete_file(file_path)) {
313 return BLUETOOTH_ERROR_INTERNAL;
315 BT_INFO("Mesh: Config DB file removed!!");
317 cdb_list = g_slist_remove(cdb_list, cdb_cfg);
318 _bt_mesh_conf_free(cdb_cfg);
319 BT_INFO("Mesh: CDB freed from memory");
324 /* Unload Network from Keys */
325 _bt_mesh_keys_unload_net(net_uuid);
327 /* Unload Network from Nodes */
328 _bt_mesh_node_unload_net(net_uuid);
330 BT_INFO("Mesh: Cleanup Done!");
331 return BLUETOOTH_ERROR_NONE;
334 int _bt_mesh_network_create_cdb(int result,
335 const char *sender, const char *app_creds,
336 uint8_t uuid[16], uint8_t token[8],
339 char *dir_path = NULL;
340 char *file_path = NULL;
342 mesh_local_node_t *tmp;
343 _bt_mesh_cdb_t *cdb_cfg = NULL;
344 char uuid_string[33];
345 char token_string[17];
347 _bt_mesh_util_convert_hex_to_string(uuid, 16,
348 uuid_string, sizeof(uuid_string));
349 _bt_mesh_util_convert_hex_to_string(token, 8,
350 token_string, sizeof(token_string));
351 BT_INFO("Mesh: Create CDB request for network UUID [%s] token [%s]",
352 uuid_string, token_string);
353 BT_INFO("Mesh: Temporary node count [%d]",
354 g_slist_length(temp_nodes));
357 l = g_slist_find_custom(temp_nodes, uuid,
358 __mesh_compare_hex_uuid);
360 BT_ERR("Mesh: Temp Node not found for the UUID [%s]",
362 return BLUETOOTH_ERROR_INTERNAL;
365 tmp = (mesh_local_node_t*)l->data;
367 if (result != BLUETOOTH_ERROR_NONE) {
368 /* Free the node structure */
369 __bt_mesh_free_temp_node(tmp);
373 BT_INFO("Mesh: Create CDB for the New Network [%s]",
375 /* Stat/Create directory for new CDB file */
376 dir_path = g_strdup_printf(MESH_CDB_DEFAULT_DIR_PATH"/%s/",
378 BT_INFO("Mesh: Directory path for new Network CDB [%s]",
380 if (!_bt_mesh_util_create_directory(dir_path)) {
382 __bt_mesh_free_temp_node(tmp);
383 return BLUETOOTH_ERROR_INTERNAL;
386 BT_INFO("Mesh: Directory Created successfully");
387 /* Create the CDB for the network */
388 file_path = g_strdup_printf("%s%s_config.json",
389 dir_path, uuid_string);
390 BT_INFO("Mesh: CDB File path[%s]", file_path);
391 BT_INFO("Mesh: CDB App Cred[%s]", app_creds);
392 cdb_cfg = _bt_mesh_conf_database_create(file_path, uuid,
393 token, network, sender, app_creds);
400 /* Free the memory of temporary node
401 which was created during Network creation */
402 __bt_mesh_free_temp_node(tmp);
403 return BLUETOOTH_ERROR_INTERNAL;
405 BT_INFO("Mesh: CDB CFG file created successfully");
407 _bt_mesh_conf_set_unicast_address_range(cdb_cfg,
408 MESH_DEFAULT_START_ADDRESS,
409 MESH_DEFAULT_MAX_ADDRESS);
410 BT_INFO("Mesh: Address range Set for network");
412 /* Create new network for saving network specific Keys */
413 _bt_mesh_keys_load_net(cdb_cfg->uuid);
414 BT_INFO("Mesh: Address range Set for network");
415 _bt_mesh_keys_add_net_key(cdb_cfg->uuid,
416 MESH_PRIMARY_NET_IDX);
417 BT_INFO("Mesh: Primary net key added to network memeory");
418 _bt_mesh_conf_insert_network_key(cdb_cfg,
419 MESH_PRIMARY_NET_IDX,
420 MESH_KEY_REFRESH_PHASE_NONE);
421 BT_INFO("Mesh: Primary net key added to CDB");
423 /* Create new network for saving network specific nodes */
424 _bt_mesh_node_load_net(cdb_cfg->uuid);
425 BT_INFO("Mesh: local Node loaded to memory");
426 _bt_mesh_node_add_node(cdb_cfg->uuid, tmp->node_uuid,
427 tmp->prim_unicast, tmp->num_elems,
428 MESH_PRIMARY_NET_IDX);
429 BT_INFO("Mesh: Added basic info (num elems, UUID, primary NetIDx)");
431 /* Add Primary Node as 1st entry in CDB */
432 _bt_mesh_conf_insert_node_object(cdb_cfg, /* Dev UUID */uuid,
433 tmp->num_elems, tmp->prim_unicast,
434 MESH_PRIMARY_NET_IDX);
435 BT_INFO("Mesh: Added Local node's basic info in CDB");
437 _bt_mesh_conf_set_vendor_info(cdb_cfg, 0x0001, tmp->vendor_info.crpl,
438 tmp->vendor_info.companyid, tmp->vendor_info.versionid,
439 tmp->vendor_info.vendorid, tmp->vendor_info.proxy,
440 tmp->vendor_info.relay, tmp->vendor_info.lpn,
441 tmp->vendor_info.frnd);
443 BT_INFO("Mesh: Vendor Info set successfully");
444 _bt_mesh_conf_set_model_info(cdb_cfg, 0x0001, tmp->model_list);
446 cdb_list = g_slist_append(cdb_list, cdb_cfg);
447 BT_INFO("Mesh: CDB added to list");
449 __bt_mesh_free_temp_node(tmp);
450 BT_INFO("Mesh: temp node freed");
451 return BLUETOOTH_ERROR_NONE;
454 int _bt_mesh_network_unload(const char *app_cred,
455 const char *sender, bluetooth_mesh_network_t *network)
459 _bt_mesh_cdb_t *cdb_cfg;
460 int ret = OAL_STATUS_SUCCESS;
462 BT_INFO("Mesh: Unload Network Configuration");
463 /* If Scanning is going on */
464 if (_bt_mesh_is_provisioning() ||
465 _bt_mesh_is_scanning()) {
466 BT_ERR("Device is buzy..");
467 return BLUETOOTH_ERROR_DEVICE_BUSY;
470 _bt_mesh_util_convert_string_to_hex(network->uuid,
471 strlen(network->uuid), net_uuid.uuid, 16);
473 /* Release Mesh Network */
474 ret = mesh_network_release(&net_uuid);
475 if (ret != OAL_STATUS_SUCCESS) {
476 BT_ERR("ret: %d", ret);
477 return BLUETOOTH_ERROR_INTERNAL;
480 BT_INFO("Mesh: Network released");
483 l = g_slist_find_custom(cdb_list, net_uuid.uuid,
484 __mesh_compare_app_network_uuid);
486 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
487 return BLUETOOTH_ERROR_INVALID_PARAM;
490 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
492 _bt_mesh_network_unload_net_configuration(cdb_cfg);
493 return BLUETOOTH_ERROR_NONE;
496 int _bt_mesh_network_destroy(const char *app_cred,
497 const char *sender, bluetooth_mesh_network_t *network)
499 int ret = OAL_STATUS_SUCCESS;
502 /* If Scanning is going on */
503 if (_bt_mesh_is_provisioning() ||
504 _bt_mesh_is_scanning()) {
505 BT_ERR("Device is buzy..");
506 return BLUETOOTH_ERROR_DEVICE_BUSY;
508 _bt_mesh_util_convert_string_to_hex(network->uuid,
509 strlen(network->uuid), net_uuid.uuid, 16);
510 /* Destroy Mesh Network */
511 ret = mesh_network_destroy(&net_uuid);
512 BT_INFO("Mesh: Network UUID [%s]", network->uuid);
513 if (ret != OAL_STATUS_SUCCESS) {
514 BT_ERR("ret: %d", ret);
515 return BLUETOOTH_ERROR_INTERNAL;
518 return BLUETOOTH_ERROR_NONE;
521 int _bt_mesh_network_scan(const char *app_cred,
523 bluetooth_mesh_network_t *network,
524 bluetooth_mesh_scan_param_t *param)
526 int ret = OAL_STATUS_SUCCESS;
529 /* If Scanning is going on */
530 if (_bt_mesh_is_provisioning() ||
531 _bt_mesh_is_scanning()) {
532 BT_ERR("Device is buzy..");
533 return BLUETOOTH_ERROR_DEVICE_BUSY;
535 _bt_mesh_util_convert_string_to_hex(network->uuid,
536 strlen(network->uuid), net_uuid.uuid, 16);
537 /* Register Mesh Node */
538 ret = mesh_network_start_scan(&net_uuid,
539 (oal_mesh_scan_params_t*) param);
540 if (ret != OAL_STATUS_SUCCESS) {
541 BT_ERR("ret: %d", ret);
542 return BLUETOOTH_ERROR_INTERNAL;
545 /* Set scanning state to true */
546 _bt_mesh_set_scanning_state(true);
547 return BLUETOOTH_ERROR_NONE;
550 int _bt_mesh_network_scan_cancel(const char *app_cred,
551 const char *sender, bluetooth_mesh_network_t *network)
553 int ret = OAL_STATUS_SUCCESS;
556 if (!_bt_mesh_is_scanning()) {
557 BT_INFO("Mesh: Scanning is not ongoing");
558 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
560 _bt_mesh_util_convert_string_to_hex(network->uuid,
561 strlen(network->uuid), net_uuid.uuid, 16);
563 /* Cancel unprovisioned device scan */
564 ret = mesh_network_scan_cancel(&net_uuid);
565 if (ret != OAL_STATUS_SUCCESS) {
566 BT_ERR("ret: %d", ret);
567 return BLUETOOTH_ERROR_INTERNAL;
570 return BLUETOOTH_ERROR_NONE;
573 int _bt_mesh_network_set_provisioner_caps(const char *app_cred,
574 const char *sender, bluetooth_mesh_network_t *network,
575 bluetooth_mesh_provisioner_caps_t *caps)
577 int ret = OAL_STATUS_SUCCESS;
580 _bt_mesh_util_convert_string_to_hex(network->uuid,
581 strlen(network->uuid), net_uuid.uuid, 16);
583 /* Set provisioning capabilities */
584 ret = mesh_network_set_provisioning_capabilities(&net_uuid,
585 (oal_mesh_capabilities_t*) caps);
586 if (ret != OAL_STATUS_SUCCESS) {
587 BT_ERR("ret: %d", ret);
588 return BLUETOOTH_ERROR_INTERNAL;
591 return BLUETOOTH_ERROR_NONE;
594 bool _bt_mesh_is_scanning(void)
596 return bt_mesh_is_scanning;
599 void _bt_mesh_set_scanning_state(bool state)
601 bt_mesh_is_scanning = state;
604 bool _bt_mesh_is_provisioning(void)
606 return bt_mesh_is_provisoning;
609 void _bt_mesh_set_provisioning_state(bool state)
611 bt_mesh_is_provisoning = state;
614 int _bt_mesh_network_provision_device(const char *app_cred,
616 bluetooth_mesh_provisioning_request_t *req)
618 int ret = OAL_STATUS_SUCCESS;
622 /* If Scanning is going on */
623 if (_bt_mesh_is_provisioning() ||
624 _bt_mesh_is_scanning()) {
625 BT_ERR("Device is buzy..");
626 return BLUETOOTH_ERROR_DEVICE_BUSY;
629 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
630 strlen(req->net_uuid), net_uuid.uuid, 16);
631 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
632 strlen(req->dev_uuid), dev_uuid.uuid, 16);
634 ret = mesh_network_provision_device(&net_uuid, &dev_uuid);
636 if (ret != OAL_STATUS_SUCCESS) {
637 BT_ERR("ret: %d", ret);
638 return BLUETOOTH_ERROR_INTERNAL;
641 /* Set Provisioning state */
642 _bt_mesh_set_provisioning_state(true);
644 return BLUETOOTH_ERROR_NONE;
647 int _bt_mesh_authentication_reply(int auth_type,
648 const char *auth_value, gboolean reply)
650 int ret = OAL_STATUS_SUCCESS;
652 if (!_bt_mesh_is_provisioning())
653 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
656 return BLUETOOTH_ERROR_INVALID_PARAM;
658 ret = mesh_authentication_reply(
659 (oal_mesh_variant_authentication_e)auth_type,
662 if (ret != OAL_STATUS_SUCCESS) {
663 BT_ERR("ret: %d", ret);
664 return BLUETOOTH_ERROR_INTERNAL;
667 BT_INFO("Mesh: Scheduled Prov Auth Reply!");
668 return BLUETOOTH_ERROR_NONE;
671 int _bt_mesh_network_get_netkeys(const char *app_cred, const char *sender,
672 bluetooth_mesh_network_t *network, GArray **out_param)
675 _bt_mesh_cdb_t *cdb_cfg = NULL;
678 l = g_slist_find_custom(cdb_list, network->token.token,
679 __mesh_compare_app_cdb_token);
681 return BLUETOOTH_ERROR_INVALID_PARAM;
683 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
685 if (_bt_mesh_keys_get_netkey_list(cdb_cfg->uuid, out_param))
686 return BLUETOOTH_ERROR_NONE;
688 return BLUETOOTH_ERROR_INTERNAL;
691 int _bt_mesh_network_get_appkeys(const char *app_cred, const char *sender,
692 bluetooth_mesh_network_t *network, uint16_t net_idx,
696 _bt_mesh_cdb_t *cdb_cfg = NULL;
699 l = g_slist_find_custom(cdb_list, network->token.token,
700 __mesh_compare_app_cdb_token);
702 return BLUETOOTH_ERROR_INVALID_PARAM;
704 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
706 if (_bt_mesh_keys_get_appkey_list(cdb_cfg->uuid,
708 return BLUETOOTH_ERROR_NONE;
710 return BLUETOOTH_ERROR_INTERNAL;
713 int _bt_mesh_network_node_get_netkeys(const char *app_cred,
714 bluetooth_mesh_node_discover_t *node,
718 _bt_mesh_cdb_t *cdb_cfg = NULL;
719 uint8_t net_uuid[16];
721 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
722 strlen(node->net_uuid), net_uuid, 16);
725 l = g_slist_find_custom(cdb_list, net_uuid,
726 __mesh_compare_app_network_uuid);
728 return BLUETOOTH_ERROR_INVALID_PARAM;
730 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
732 if (_bt_mesh_node_get_all_netkeys(cdb_cfg->uuid,
733 node->unicast, out_param))
734 return BLUETOOTH_ERROR_NONE;
736 return BLUETOOTH_ERROR_INTERNAL;
739 int _bt_mesh_network_node_get_appkeys(const char *app_cred,
740 const char *sender, bluetooth_mesh_node_discover_t *node,
744 _bt_mesh_cdb_t *cdb_cfg = NULL;
745 uint8_t net_uuid[16];
747 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
748 strlen(node->net_uuid), net_uuid, 16);
751 l = g_slist_find_custom(cdb_list, net_uuid,
752 __mesh_compare_app_network_uuid);
754 return BLUETOOTH_ERROR_INVALID_PARAM;
756 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
758 if (_bt_mesh_node_get_all_appkeys(cdb_cfg->uuid,
759 node->unicast, node->netkey_idx, out_param))
760 return BLUETOOTH_ERROR_NONE;
762 return BLUETOOTH_ERROR_INTERNAL;
765 int _bt_mesh_element_get_models(const char *app_cred, const char *sender,
766 bluetooth_mesh_network_t *network, uint16_t unicast,
767 int elem_idx, GArray **out_param)
770 _bt_mesh_cdb_t *cdb_cfg = NULL;
773 l = g_slist_find_custom(cdb_list, network->token.token,
774 __mesh_compare_app_cdb_token);
776 return BLUETOOTH_ERROR_INVALID_PARAM;
778 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
780 if (_bt_mesh_node_get_models(cdb_cfg->uuid, unicast,
781 elem_idx, out_param))
782 return BLUETOOTH_ERROR_NONE;
784 return BLUETOOTH_ERROR_INTERNAL;
787 int _bt_mesh_network_handle_netkey_added(
788 uint8_t net_uuid[], uint16_t netkey_idx)
791 _bt_mesh_cdb_t *cdb_cfg = NULL;
793 BT_INFO("Mesh: Handle Netkey Added");
795 l = g_slist_find_custom(cdb_list, net_uuid,
796 __mesh_compare_app_network_uuid);
798 return BLUETOOTH_ERROR_INVALID_PARAM;
800 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
802 _bt_mesh_keys_add_net_key(cdb_cfg->uuid, netkey_idx);
804 if (!_bt_mesh_conf_insert_network_key(cdb_cfg,
805 netkey_idx, MESH_KEY_REFRESH_PHASE_NONE))
806 return BLUETOOTH_ERROR_INTERNAL;
808 return BLUETOOTH_ERROR_NONE;
811 int _bt_mesh_network_handle_netkey_deleted(
812 uint8_t net_uuid[], uint16_t netkey_idx)
815 _bt_mesh_cdb_t *cdb_cfg = NULL;
817 BT_INFO("Mesh: Handle Netkey Deleted");
819 l = g_slist_find_custom(cdb_list, net_uuid,
820 __mesh_compare_app_network_uuid);
822 return BLUETOOTH_ERROR_INVALID_PARAM;
824 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
826 _bt_mesh_keys_del_net_key(cdb_cfg->uuid, netkey_idx, cdb_cfg);
828 if (!_bt_mesh_conf_delete_network_key(cdb_cfg, netkey_idx))
829 return BLUETOOTH_ERROR_INTERNAL;
831 return BLUETOOTH_ERROR_NONE;
834 int _bt_mesh_network_handle_netkey_updated(
835 uint8_t net_uuid[], uint16_t netkey_idx)
838 _bt_mesh_cdb_t *cdb_cfg = NULL;
840 BT_INFO("Mesh: Handle Netkey Updated");
842 l = g_slist_find_custom(cdb_list, net_uuid,
843 __mesh_compare_app_network_uuid);
845 return BLUETOOTH_ERROR_INVALID_PARAM;
847 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
849 _bt_mesh_keys_set_net_key_phase(cdb_cfg,
850 netkey_idx, MESH_KEY_REFRESH_PHASE_ONE, true);
852 return BLUETOOTH_ERROR_NONE;
855 int _bt_mesh_network_handle_appkey_added(uint8_t net_uuid[],
856 uint16_t netkey_idx, uint16_t appkey_idx)
859 _bt_mesh_cdb_t *cdb_cfg = NULL;
861 BT_INFO("Mesh: Handle Appkey Added");
863 l = g_slist_find_custom(cdb_list, net_uuid,
864 __mesh_compare_app_network_uuid);
866 return BLUETOOTH_ERROR_INVALID_PARAM;
868 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
870 _bt_mesh_keys_add_app_key(cdb_cfg->uuid,
871 netkey_idx, appkey_idx);
873 if (!_bt_mesh_conf_insert_application_key(cdb_cfg,
874 netkey_idx, appkey_idx))
875 return BLUETOOTH_ERROR_INTERNAL;
877 return BLUETOOTH_ERROR_NONE;
880 int _bt_mesh_network_handle_appkey_deleted(uint8_t net_uuid[],
881 uint16_t netkey_idx, uint16_t appkey_idx)
884 _bt_mesh_cdb_t *cdb_cfg = NULL;
886 BT_INFO("Mesh: Handle Appkey Deleted");
888 l = g_slist_find_custom(cdb_list, net_uuid,
889 __mesh_compare_app_network_uuid);
891 return BLUETOOTH_ERROR_INVALID_PARAM;
893 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
895 _bt_mesh_keys_del_app_key(cdb_cfg->uuid, appkey_idx);
897 if (!_bt_mesh_conf_delete_application_key(cdb_cfg, appkey_idx))
898 return BLUETOOTH_ERROR_INTERNAL;
900 return BLUETOOTH_ERROR_NONE;
903 int _bt_mesh_network_set_name(const char *app_cred, const char *sender,
904 bluetooth_mesh_network_t *network)
907 _bt_mesh_cdb_t *cdb_cfg = NULL;
908 BT_INFO("Mesh: Set network name, app_creds [%s]", app_cred);
909 BT_INFO("Mesh: UUID [%s]", network->uuid);
910 BT_INFO("Mesh: Token[%s]", network->token.token);
911 BT_INFO("Mesh: Name to be set[%s]", network->name.name);
914 l = g_slist_find_custom(cdb_list,
915 network->token.token, __mesh_compare_app_cdb_token);
917 return BLUETOOTH_ERROR_INVALID_PARAM;
919 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
920 if (_bt_mesh_conf_set_network_friendly_name(cdb_cfg, network->name.name))
921 return BLUETOOTH_ERROR_NONE;
923 return BLUETOOTH_ERROR_INTERNAL;
926 int _bt_mesh_network_add_netkey(const char *app_cred,
927 const char *sender, bluetooth_mesh_network_t *network)
929 int ret = OAL_STATUS_SUCCESS;
931 _bt_mesh_cdb_t *cdb_cfg = NULL;
933 oal_mesh_key_op_e op = OAL_MESH_KEY_ADD;
937 l = g_slist_find_custom(cdb_list,
938 network->token.token, __mesh_compare_app_cdb_token);
940 return BLUETOOTH_ERROR_INVALID_PARAM;
942 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
944 if (!_bt_mesh_keys_get_new_netkey_index(cdb_cfg->uuid, &idx))
945 return BLUETOOTH_ERROR_INVALID_PARAM;
947 _bt_mesh_util_convert_string_to_hex(network->uuid,
948 strlen(network->uuid), net_uuid.uuid, 16);
950 BT_INFO("Mesh: netkey index to be created [%u]", idx);
951 ret = mesh_network_subnet_execute(&net_uuid, op, idx);
953 if (ret != OAL_STATUS_SUCCESS) {
954 BT_ERR("ret: %d", ret);
955 return BLUETOOTH_ERROR_INTERNAL;
958 return BLUETOOTH_ERROR_NONE;
961 int _bt_mesh_network_delete_netkey(const char *app_cred,
962 const char *sender, bluetooth_mesh_network_t *network,
965 int ret = OAL_STATUS_SUCCESS;
967 _bt_mesh_cdb_t *cdb_cfg = NULL;
969 oal_mesh_key_op_e op = OAL_MESH_KEY_DELETE;
972 l = g_slist_find_custom(cdb_list, network->token.token,
973 __mesh_compare_app_cdb_token);
975 return BLUETOOTH_ERROR_INVALID_PARAM;
977 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
979 /* Check if NetKey entry is present in local Network */
980 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, index))
981 return BLUETOOTH_ERROR_INVALID_PARAM;
983 /* Check if NetKey is already added to one of the provisioned nodes */
984 if (_bt_mesh_node_is_netkey_added_in_network(cdb_cfg->uuid, index)) {
985 BT_INFO("Mesh: NetKey index [0x%2.2x] can not be deleted", index);
986 return BLUETOOTH_ERROR_INVALID_PARAM;
989 _bt_mesh_util_convert_string_to_hex(network->uuid,
990 strlen(network->uuid), net_uuid.uuid, 16);
992 BT_INFO("Mesh: netkey index to be deleted [%u]", index);
993 ret = mesh_network_subnet_execute(&net_uuid, op, index);
995 if (ret != OAL_STATUS_SUCCESS) {
996 BT_ERR("ret: %d", ret);
997 return BLUETOOTH_ERROR_INTERNAL;
1000 return BLUETOOTH_ERROR_NONE;
1003 int _bt_mesh_network_update_netkey(const char *app_cred,
1004 const char *sender, bluetooth_mesh_network_t *network,
1007 int ret = OAL_STATUS_SUCCESS;
1009 _bt_mesh_cdb_t *cdb_cfg = NULL;
1010 oal_uuid_t net_uuid;
1011 oal_mesh_key_op_e op = OAL_MESH_KEY_UPDATE;
1014 l = g_slist_find_custom(cdb_list, network->token.token,
1015 __mesh_compare_app_cdb_token);
1017 return BLUETOOTH_ERROR_INVALID_PARAM;
1019 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1021 /* Check if NetKey entry is present in local Network */
1022 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, index))
1023 return BLUETOOTH_ERROR_INVALID_PARAM;
1025 _bt_mesh_util_convert_string_to_hex(network->uuid,
1026 strlen(network->uuid), net_uuid.uuid, 16);
1028 BT_INFO("Mesh: netkey index to be updated [%u]", index);
1029 ret = mesh_network_subnet_execute(&net_uuid, op, index);
1031 if (ret != OAL_STATUS_SUCCESS) {
1032 BT_ERR("ret: %d", ret);
1033 return BLUETOOTH_ERROR_INTERNAL;
1036 return BLUETOOTH_ERROR_NONE;
1039 int _bt_mesh_network_add_appkey(const char *app_cred,
1040 const char *sender, bluetooth_mesh_network_t *network,
1041 uint16_t netkey_idx)
1043 int ret = OAL_STATUS_SUCCESS;
1045 _bt_mesh_cdb_t *cdb_cfg = NULL;
1046 oal_uuid_t net_uuid;
1047 oal_mesh_key_op_e op = OAL_MESH_KEY_ADD;
1051 l = g_slist_find_custom(cdb_list, network->token.token,
1052 __mesh_compare_app_cdb_token);
1054 return BLUETOOTH_ERROR_INVALID_PARAM;
1056 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1058 /* Check if NetKey entry is present in local Network */
1059 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1060 return BLUETOOTH_ERROR_INVALID_PARAM;
1062 if (!_bt_mesh_keys_get_new_appkey_index(cdb_cfg->uuid, &idx))
1063 return BLUETOOTH_ERROR_INVALID_PARAM;
1065 _bt_mesh_util_convert_string_to_hex(network->uuid,
1066 strlen(network->uuid), net_uuid.uuid, 16);
1068 BT_INFO("Mesh: AppKey index to be created [%u]", idx);
1069 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, idx);
1071 if (ret != OAL_STATUS_SUCCESS) {
1072 BT_ERR("ret: %d", ret);
1073 return BLUETOOTH_ERROR_INTERNAL;
1076 return BLUETOOTH_ERROR_NONE;
1079 int _bt_mesh_network_update_appkey(const char *app_cred,
1080 const char *sender, bluetooth_mesh_network_t *network,
1081 uint16_t netkey_idx, uint16_t appkey_idx)
1083 int ret = OAL_STATUS_SUCCESS;
1085 _bt_mesh_cdb_t *cdb_cfg = NULL;
1086 oal_uuid_t net_uuid;
1087 oal_mesh_key_op_e op = OAL_MESH_KEY_UPDATE;
1090 l = g_slist_find_custom(cdb_list, network->token.token,
1091 __mesh_compare_app_cdb_token);
1093 return BLUETOOTH_ERROR_INVALID_PARAM;
1095 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1097 /* Check if NetKey entry is present in local Network */
1098 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1099 return BLUETOOTH_ERROR_INVALID_PARAM;
1101 _bt_mesh_util_convert_string_to_hex(network->uuid,
1102 strlen(network->uuid), net_uuid.uuid, 16);
1104 BT_INFO("Mesh: AppKey index to be updated [%u]", appkey_idx);
1105 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, appkey_idx);
1107 if (ret != OAL_STATUS_SUCCESS) {
1108 BT_ERR("ret: %d", ret);
1109 return BLUETOOTH_ERROR_INTERNAL;
1112 return BLUETOOTH_ERROR_NONE;
1115 int _bt_mesh_network_delete_appkey(const char *app_cred,
1116 const char *sender, bluetooth_mesh_network_t *network,
1117 uint16_t netkey_idx, uint16_t appkey_idx)
1119 int ret = OAL_STATUS_SUCCESS;
1121 _bt_mesh_cdb_t *cdb_cfg = NULL;
1122 oal_uuid_t net_uuid;
1123 oal_mesh_key_op_e op = OAL_MESH_KEY_DELETE;
1126 l = g_slist_find_custom(cdb_list, network->token.token,
1127 __mesh_compare_app_cdb_token);
1129 return BLUETOOTH_ERROR_INVALID_PARAM;
1131 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1133 /* Check if NetKey entry is present in local Network */
1134 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1135 return BLUETOOTH_ERROR_INVALID_PARAM;
1137 /* Check if AppKey is already added to one of the provisioned nodes */
1138 if (_bt_mesh_node_is_appkey_added_in_network(cdb_cfg->uuid, appkey_idx))
1139 return BLUETOOTH_ERROR_INVALID_PARAM;
1141 _bt_mesh_util_convert_string_to_hex(network->uuid,
1142 strlen(network->uuid), net_uuid.uuid, 16);
1144 BT_INFO("Mesh: AppKey index to be deleted [%u]", appkey_idx);
1145 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, appkey_idx);
1147 if (ret != OAL_STATUS_SUCCESS) {
1148 BT_ERR("ret: %d", ret);
1149 return BLUETOOTH_ERROR_INTERNAL;
1152 return BLUETOOTH_ERROR_NONE;
1155 int _bt_mesh_network_create(const char *app_cred, const char *sender,
1156 const char *network_name, bluetooth_mesh_node_t *node,
1159 int ret = OAL_STATUS_SUCCESS;
1161 BT_INFO("Mesh: App Credential#### [%s] sender [%s] network [%s]",
1162 app_cred, sender, network_name);
1164 /* TODO Handle Buzy status */
1165 /* Sanity Check: CDB directory creation */
1166 if (!_bt_mesh_util_is_directory_exists(MESH_CDB_DEFAULT_DIR_PATH)) {
1167 BT_INFO("MESH: CDB directory does not exist");
1168 if (!_bt_mesh_util_create_directory(MESH_CDB_DEFAULT_DIR_PATH)) {
1169 BT_ERR("MESH: Fail to create Mesh CDB directory");
1170 return BLUETOOTH_ERROR_INTERNAL;
1174 BT_INFO("Mesh: Send Local Network Creation Request to OAL");
1176 /* Register Mesh Node */
1177 ret = mesh_register_node((oal_mesh_node_t*)node, model_list, true);
1178 if (ret != OAL_STATUS_SUCCESS) {
1179 BT_ERR("ret: %d", ret);
1180 return BLUETOOTH_ERROR_INTERNAL;
1183 BT_INFO("Mesh: Request Sent to Stack successfully");
1184 /* Create a temporary node & wait for Network Created event */
1185 mesh_local_node_t *temp = g_malloc0(sizeof(mesh_local_node_t));
1186 memcpy(temp->node_uuid, node->uuid, 16);
1187 temp->num_elems = node->num_elements;
1188 temp->prim_unicast = node->primary_unicast;
1189 temp->sender = g_strdup(sender);
1190 temp->app_cred = g_strdup(app_cred);
1191 temp->vendor_info = node->vendor_info;
1192 temp->model_list = model_list;
1193 temp_nodes = g_slist_append(temp_nodes, temp);
1195 return BLUETOOTH_ERROR_NONE;
1198 int _bt_mesh_network_join(const char *app_cred, const char *sender,
1199 bluetooth_mesh_node_t *node, GSList *model_list)
1201 int ret = OAL_STATUS_SUCCESS;
1203 BT_INFO("Mesh: App Credential#### [%s] sender [%s]",
1206 /* TODO Handle Buzy status */
1207 /* Sanity Check: CDB directory creation */
1208 if (!_bt_mesh_util_is_directory_exists(MESH_CDB_DEFAULT_DIR_PATH)) {
1209 BT_INFO("MESH: CDB directory does not exist");
1210 if (!_bt_mesh_util_create_directory(MESH_CDB_DEFAULT_DIR_PATH)) {
1211 BT_ERR("MESH: Fail to create Mesh CDB directory");
1212 return BLUETOOTH_ERROR_INTERNAL;
1216 BT_INFO("Mesh: Send Join Network Request to OAL");
1217 /* Register Mesh Node */
1218 ret = mesh_register_node((oal_mesh_node_t*)node, model_list, false);
1219 if (ret != OAL_STATUS_SUCCESS) {
1220 BT_ERR("ret: %d", ret);
1221 return BLUETOOTH_ERROR_INTERNAL;
1224 BT_INFO("Mesh: Request Sent to Stack successfully");
1225 /* Create a temporary node & wait for Join event */
1226 mesh_local_node_t *temp = g_malloc0(sizeof(mesh_local_node_t));
1227 memcpy(temp->node_uuid, node->uuid, 16);
1228 temp->num_elems = node->num_elements;
1229 temp->prim_unicast = node->primary_unicast;
1230 temp->sender = g_strdup(sender);
1231 temp->app_cred = g_strdup(app_cred);
1232 temp->vendor_info = node->vendor_info;
1233 temp->model_list = model_list;
1234 temp_nodes = g_slist_append(temp_nodes, temp);
1235 return BLUETOOTH_ERROR_NONE;
1238 int _bt_mesh_cancel_join(const char *app_cred,
1239 const char *sender, bluetooth_mesh_node_info_t *node)
1241 int ret = OAL_STATUS_SUCCESS;
1242 oal_uuid_t node_uuid;
1244 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
1245 strlen(node->net_uuid), node_uuid.uuid, 16);
1246 /* Destroy Mesh Network */
1247 ret = mesh_cancel_join(&node_uuid);
1248 if (ret != OAL_STATUS_SUCCESS) {
1249 BT_ERR("ret: %d", ret);
1250 return BLUETOOTH_ERROR_INTERNAL;
1252 return BLUETOOTH_ERROR_NONE;
1255 bool _bt_mesh_network_save_remote_node_appkey(
1256 uint8_t net_uuid[], uint16_t remote_unicast,
1257 uint16_t netkey_idx, uint16_t appkey_idx)
1260 _bt_mesh_cdb_t *cdb_cfg = NULL;
1262 BT_INFO("Mesh: Add Appkey [0x%2.2x] to node", appkey_idx);
1264 l = g_slist_find_custom(cdb_list, net_uuid,
1265 __mesh_compare_app_network_uuid);
1269 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1271 if (_bt_mesh_node_is_appkey_exists(net_uuid,
1272 remote_unicast, appkey_idx)) {
1273 BT_INFO("Mesh: AppKey is already added to Node");
1276 if (_bt_mesh_node_add_app_key(net_uuid,
1277 remote_unicast, appkey_idx)) {
1278 BT_INFO("Mesh: Add App Key Idx [0x%2.2x] in CDB", appkey_idx);
1279 return _bt_mesh_conf_node_insert_application_key(cdb_cfg,
1280 remote_unicast, appkey_idx);
1283 BT_INFO("Mesh: AppKey idx [0x%2.2x] Failed to add in Node", appkey_idx);
1287 bool _bt_mesh_network_delete_remote_node_appkey(
1288 uint8_t net_uuid[], uint16_t remote_unicast,
1289 uint16_t netkey_idx, uint16_t appkey_idx)
1292 _bt_mesh_cdb_t *cdb_cfg = NULL;
1293 bool is_deleted = false;
1295 BT_INFO("Mesh: Delete Appkey Idx[0x%2.2x]from Node Unicast [0x%2.2x]",
1296 appkey_idx, remote_unicast);
1298 l = g_slist_find_custom(cdb_list, net_uuid,
1299 __mesh_compare_app_network_uuid);
1303 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1305 if (_bt_mesh_node_del_app_key(net_uuid,
1306 remote_unicast, appkey_idx)) {
1307 BT_INFO("Mesh: Removed Appkey from node");
1308 is_deleted = _bt_mesh_conf_node_delete_application_key(cdb_cfg,
1309 remote_unicast, appkey_idx);
1311 BT_INFO("Mesh: AppKey removed from CDB Node Entry");
1313 BT_INFO("Mesh: AppKey could not be removed from CDB Node Entry");
1319 bool _bt_mesh_network_save_remote_node_ttl(
1320 uint8_t net_uuid[], uint16_t remote_unicast,
1324 _bt_mesh_cdb_t *cdb_cfg = NULL;
1327 l = g_slist_find_custom(cdb_list, net_uuid,
1328 __mesh_compare_app_network_uuid);
1332 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1334 return _bt_mesh_conf_node_set_timetolive_value(cdb_cfg,
1335 remote_unicast, ttl);
1338 bool _bt_mesh_network_save_remote_node_netkey(
1339 uint8_t net_uuid[], uint16_t remote_unicast,
1340 uint16_t netkey_idx)
1343 _bt_mesh_cdb_t *cdb_cfg = NULL;
1346 l = g_slist_find_custom(cdb_list, net_uuid,
1347 __mesh_compare_app_network_uuid);
1351 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1353 if (_bt_mesh_node_is_netkey_exists(net_uuid,
1354 remote_unicast, netkey_idx)) {
1355 BT_INFO("Mesh: NetKey is already added to Node");
1358 if (_bt_mesh_node_add_net_key(net_uuid,
1359 remote_unicast, netkey_idx))
1360 return _bt_mesh_conf_node_insert_network_key(cdb_cfg,
1361 remote_unicast, netkey_idx);
1366 bool _bt_mesh_network_delete_remote_node_netkey(
1367 uint8_t net_uuid[], uint16_t remote_unicast,
1368 uint16_t netkey_idx)
1371 _bt_mesh_cdb_t *cdb_cfg = NULL;
1374 l = g_slist_find_custom(cdb_list, net_uuid,
1375 __mesh_compare_app_network_uuid);
1379 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1381 if (_bt_mesh_node_del_net_key(cdb_cfg, net_uuid,
1382 remote_unicast, netkey_idx))
1383 return _bt_mesh_conf_node_delete_network_key(cdb_cfg,
1384 remote_unicast, netkey_idx);
1388 bool _bt_mesh_network_add_model_subscription(
1389 uint8_t net_uuid[], uint16_t remote_unicast,
1390 int ele_idx, uint32_t mod_id, uint16_t group_addr)
1393 _bt_mesh_cdb_t *cdb_cfg = NULL;
1396 l = g_slist_find_custom(cdb_list, net_uuid,
1397 __mesh_compare_app_network_uuid);
1401 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1403 return _bt_mesh_conf_add_model_config_data(cdb_cfg,
1404 remote_unicast, ele_idx, mod_id, group_addr);
1407 bool _bt_mesh_network_delete_model_subscription(
1408 uint8_t net_uuid[], uint16_t remote_unicast,
1409 int ele_idx, uint32_t mod_id, uint16_t group_addr)
1412 _bt_mesh_cdb_t *cdb_cfg = NULL;
1415 l = g_slist_find_custom(cdb_list, net_uuid,
1416 __mesh_compare_app_network_uuid);
1420 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1422 return _bt_mesh_conf_delete_model_config_data(cdb_cfg,
1423 remote_unicast, ele_idx, mod_id, group_addr);
1426 bool _bt_mesh_network_delete_all_model_subscription(
1427 uint8_t net_uuid[], uint16_t remote_unicast,
1428 int ele_idx, uint32_t mod_id)
1431 _bt_mesh_cdb_t *cdb_cfg = NULL;
1434 l = g_slist_find_custom(cdb_list, net_uuid,
1435 __mesh_compare_app_network_uuid);
1439 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1441 return _bt_mesh_conf_delete_all_model_config_data(cdb_cfg,
1442 remote_unicast, ele_idx, mod_id);
1445 bool _bt_mesh_network_overwrite_model_subscription(
1446 uint8_t net_uuid[], uint16_t remote_unicast,
1447 int ele_idx, uint32_t mod_id, uint16_t group_addr)
1450 _bt_mesh_cdb_t *cdb_cfg = NULL;
1453 l = g_slist_find_custom(cdb_list, net_uuid,
1454 __mesh_compare_app_network_uuid);
1458 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1460 return _bt_mesh_conf_overwrite_model_config_data(cdb_cfg,
1461 remote_unicast, ele_idx, mod_id, group_addr);
1464 bool _bt_mesh_network_save_remote_node_composition(
1465 uint8_t net_uuid[], uint16_t remote_unicast,
1466 uint8_t *data, uint16_t data_len)
1469 _bt_mesh_cdb_t *cdb_cfg = NULL;
1472 l = g_slist_find_custom(cdb_list, net_uuid,
1473 __mesh_compare_app_network_uuid);
1477 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1479 return _bt_mesh_conf_set_node_comp_data(cdb_cfg,
1480 remote_unicast, data, data_len);
1483 int _bt_mesh_network_add_remote_node(uint8_t net_uuid[],
1484 uint8_t dev_uuid[], uint16_t unicast, uint8_t count)
1487 _bt_mesh_cdb_t *cdb_cfg = NULL;
1490 l = g_slist_find_custom(cdb_list, net_uuid,
1491 __mesh_compare_app_network_uuid);
1493 return BLUETOOTH_ERROR_INVALID_PARAM;
1495 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1497 _bt_mesh_node_add_node(net_uuid, dev_uuid,
1498 unicast, count, MESH_PRIMARY_NET_IDX);
1500 /* Add Remote Node entry in CDB */
1501 _bt_mesh_conf_insert_node_object(cdb_cfg, /* Dev UUID */ dev_uuid,
1502 count, unicast, MESH_PRIMARY_NET_IDX);
1504 return BLUETOOTH_ERROR_NONE;
1507 bool _bt_mesh_node_get_vendor_features(uint8_t net_uuid[],
1508 uint16_t unicast, bluetooth_mesh_node_features_t *feats)
1511 _bt_mesh_cdb_t *cdb_cfg = NULL;
1512 BT_INFO("Mesh: Attempt to get vendor features: unicast [0x%2.2x]", unicast);
1514 l = g_slist_find_custom(cdb_list, net_uuid,
1515 __mesh_compare_app_network_uuid);
1519 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1521 BT_INFO("Mesh: Read Vendor Features from CDB");
1522 return _bt_mesh_conf_fetch_vendor_specific_info(cdb_cfg, unicast,
1523 &feats->vendor_info.companyid, &feats->vendor_info.vendorid,
1524 &feats->vendor_info.versionid, &feats->vendor_info.crpl,
1525 &feats->features.relay, &feats->features.frnd,
1526 &feats->features.proxy, &feats->features.lpn);
1529 int _bt_mesh_network_load_cdb(int result, const char *sender,
1530 const char *app_creds,
1531 uint8_t uuid[16], uint8_t token[8],
1536 _bt_mesh_cdb_t *cdb_cfg = NULL;
1538 _bt_mesh_util_convert_hex_to_string(token, 8,
1542 l = g_slist_find_custom(cdb_list, token_str,
1543 __mesh_compare_app_cdb_token);
1545 return BLUETOOTH_ERROR_INTERNAL;
1547 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1549 if (result != BLUETOOTH_ERROR_NONE)
1552 /* Create new network for saving network specific Keys */
1553 _bt_mesh_keys_load_net(cdb_cfg->uuid);
1554 if (!_bt_mesh_conf_load_all_keys(cdb_cfg)) {
1555 _bt_mesh_keys_unload_net(cdb_cfg->uuid);
1559 /* Create new network for saving network specific nodes */
1560 _bt_mesh_node_load_net(cdb_cfg->uuid);
1561 if (!_bt_mesh_conf_load_all_nodes(cdb_cfg)) {
1562 _bt_mesh_node_unload_net(cdb_cfg->uuid);
1566 /* Load Groups Created */
1567 cdb_cfg->groups = _bt_mesh_conf_load_group_info(cdb_cfg);
1569 /* Fetch Network name */
1570 *network = (char*)_bt_mesh_conf_get_network_friendly_name(cdb_cfg);
1571 BT_INFO("Mesh: Attached Network [%s]", *network);
1572 return BLUETOOTH_ERROR_NONE;
1574 /* Free the CDB object */
1575 _bt_mesh_conf_free(cdb_cfg);
1576 return BLUETOOTH_ERROR_INTERNAL;
1579 int _bt_mesh_network_load(const char *app_cred,
1580 const char *sender, const char *token)
1582 int ret = OAL_STATUS_SUCCESS;
1584 char *dir_path = NULL;
1585 char *file_path = NULL;
1586 _bt_mesh_cdb_t *cdb_cfg = NULL;
1587 oal_mesh_node_t node;
1588 GSList *models = NULL;
1590 /* Check CDB directory exist or not */
1591 dir_path = g_strdup_printf(MESH_CDB_DEFAULT_DIR_PATH"/%s/", "default");
1592 if (!_bt_mesh_util_is_directory_exists(dir_path)) {
1593 BT_ERR("Mesh: CDB dir [%s]does not exist for app", dir_path);
1595 return BLUETOOTH_ERROR_INTERNAL;
1599 l = g_slist_find_custom(cdb_list, token,
1600 __mesh_compare_app_cdb_token);
1602 BT_INFO("Mesh: Already loaded");
1604 return BLUETOOTH_ERROR_NONE;
1606 BT_INFO("Mesh: Not loaded");
1607 /* Attempt to load CDB's present in app's
1609 const char *filename;
1610 GDir *dir = g_dir_open(dir_path, 0, NULL);
1612 BT_ERR("Mesh: Could not open directory");
1614 return BLUETOOTH_ERROR_INTERNAL;
1616 while ((filename = g_dir_read_name(dir)) != NULL) {
1618 if ((g_file_test(filename, G_FILE_TEST_IS_SYMLINK) == TRUE) ||
1619 (g_str_has_suffix(filename, ".json") == FALSE))
1622 file_path = g_strdup_printf("%s%s",
1623 dir_path, filename);
1624 BT_INFO("Mesh: Token [%s]", token);
1625 BT_INFO("Mesh: File name [%s]", filename);
1626 BT_INFO("Mesh: File Absolute Path [%s]", file_path);
1628 cdb_cfg = _bt_mesh_conf_load(file_path, token);
1644 BT_ERR("Mesh: Could not find CDB for the token!! Possibly not authorized!!");
1645 return BLUETOOTH_ERROR_ACCESS_DENIED;
1648 /* Fill the Mesh node info */
1649 memset(&node, 0x00, sizeof(oal_mesh_node_t));
1650 memcpy(node.uuid.uuid, cdb_cfg->uuid, sizeof(oal_uuid_t));
1651 _bt_mesh_util_convert_string_to_hex(token, strlen(token), node.token.u8, 8);
1652 if (!_bt_mesh_conf_get_element_count(cdb_cfg, &node.num_elements)) {
1653 _bt_mesh_conf_free(cdb_cfg);
1654 return BLUETOOTH_ERROR_INTERNAL;
1656 BT_INFO("Mesh: toatl number of elements [%d]", node.num_elements);
1658 BT_INFO("Mesh:Fetch Vendor specific info");
1659 if (!_bt_mesh_conf_fetch_vendor_specific_info(cdb_cfg, 0x0001 /* Local Node Unicast */,
1660 &node.vendor_info.companyid, &node.vendor_info.vendorid,
1661 &node.vendor_info.versionid, &node.vendor_info.crpl,
1662 &node.vendor_info.relay, &node.vendor_info.frnd,
1663 &node.vendor_info.proxy, &node.vendor_info.lpn)) {
1665 _bt_mesh_conf_free(cdb_cfg);
1666 return BLUETOOTH_ERROR_INTERNAL;
1669 BT_INFO("Mesh: Fill Model informations");
1670 for (int i = 0; i < node.num_elements; i++) {
1672 uint16_t **model_array = _bt_mesh_conf_get_all_model_info(
1673 cdb_cfg, i, &num_models);
1675 _bt_mesh_conf_free(cdb_cfg);
1676 return BLUETOOTH_ERROR_INTERNAL;
1679 for (int j = 0; j < num_models; j++) {
1680 bluetooth_mesh_model_t *mod;
1681 mod = g_malloc0(sizeof(bluetooth_mesh_model_t));
1682 mod->elem_index = i;
1683 mod->model_id = *model_array[j];
1684 models = g_slist_append(models, mod);
1686 /* Free all model(s) */
1687 for (int j = 0; j < num_models; j++)
1688 g_free(model_array[j]);
1689 g_free(model_array);
1692 BT_INFO("Mesh: Node formation done:: Register it!");
1693 /* Register Mesh Node */
1694 ret = mesh_register_node((oal_mesh_node_t*)&node,
1698 //g_slist_free_full(models, g_free);
1700 if (ret != OAL_STATUS_SUCCESS) {
1701 BT_ERR("Mesh: Load Network Failed ret: %d", ret);
1702 _bt_mesh_conf_free(cdb_cfg);
1703 return BLUETOOTH_ERROR_INTERNAL;
1706 /* Save till Network attached */
1707 cdb_list = g_slist_append(cdb_list, cdb_cfg);
1708 return BLUETOOTH_ERROR_NONE;
1711 bool _bt_mesh_network_get_label_uuid_from_sub_addr(
1712 uint8_t net_uuid[], uint16_t sub_addr,
1716 _bt_mesh_cdb_t *cdb_cfg = NULL;
1719 l = g_slist_find_custom(cdb_list, net_uuid,
1720 __mesh_compare_app_network_uuid);
1724 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1726 l1 = g_slist_find_custom(cdb_cfg->groups,
1727 GUINT_TO_POINTER(sub_addr), __mesh_compare_addr);
1732 _bt_mesh_group_t *grp = l1->data;
1733 memcpy(label, grp->label_uuid, 16);
1738 int _bt_mesh_network_remove_group(const char *app_cred,
1739 const char *sender, bluetooth_mesh_network_t *net,
1740 bluetooth_mesh_network_group_info_t *req)
1743 _bt_mesh_cdb_t *cdb_cfg = NULL;
1744 _bt_mesh_group_t *grp;
1745 uint8_t net_uuid[16];
1747 _bt_mesh_util_convert_string_to_hex(net->uuid,
1748 strlen(net->uuid), net_uuid, 16);
1750 l = g_slist_find_custom(cdb_list, net_uuid,
1751 __mesh_compare_app_network_uuid);
1753 return BLUETOOTH_ERROR_INVALID_PARAM;
1755 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1756 BT_INFO("Mesh: Total groups stored in network [%d]",
1757 g_slist_length(cdb_cfg->groups));
1759 l1 = g_slist_find_custom(cdb_cfg->groups,
1760 GUINT_TO_POINTER(req->group_addr), __mesh_compare_addr);
1763 BT_ERR("Mesh: To be Removed Group: [0x%2.2x] Not Found!", req->group_addr);
1764 return BLUETOOTH_ERROR_INVALID_PARAM;
1766 grp = (_bt_mesh_group_t*)l1->data;
1768 /* Do not delete group if a model is subscribed to this */
1769 if (_bt_mesh_is_group_subscribed(cdb_cfg, req->group_addr)) {
1770 BT_ERR("Mesh: Failed to remove Group: [0x%2.2x]", req->group_addr);
1771 return BLUETOOTH_ERROR_AUTHENTICATION_REJECTED;
1774 BT_INFO("Mesh: To be Removed Group: [0x%2.2x]", req->group_addr);
1775 if (!_bt_mesh_conf_delete_group_entry(cdb_cfg, req->group_addr)) {
1776 BT_ERR("Mesh: Failed to remove Group: [0x%2.2x]", req->group_addr);
1777 return BLUETOOTH_ERROR_INTERNAL;
1780 BT_INFO("Mesh: Successfully removed Group: [0x%2.2x]", req->group_addr);
1781 cdb_cfg->groups = g_slist_remove(cdb_cfg->groups, grp);
1782 BT_INFO("Mesh: Total groups after update [%d]",
1783 g_slist_length(cdb_cfg->groups));
1785 return BLUETOOTH_ERROR_NONE;
1788 int _bt_mesh_network_create_group(const char *app_cred,
1789 const char *sender, bluetooth_mesh_network_t *net,
1790 bool is_virtual, uint16_t addr,
1791 bluetooth_mesh_network_group_info_t *req)
1793 GSList *l, *l1, *l2;
1794 _bt_mesh_cdb_t *cdb_cfg = NULL;
1795 uint8_t net_uuid[16];
1797 _bt_mesh_util_convert_string_to_hex(net->uuid,
1798 strlen(net->uuid), net_uuid, 16);
1800 l = g_slist_find_custom(cdb_list, net_uuid,
1801 __mesh_compare_app_network_uuid);
1803 return BLUETOOTH_ERROR_INVALID_PARAM;
1805 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1806 BT_INFO("Mesh: Total Groups present in Network already [%d]",
1807 g_slist_length(cdb_cfg->groups));
1810 uint8_t max_tries = 5;
1811 _bt_mesh_group_t *grp = NULL;
1812 grp = g_malloc0(sizeof(_bt_mesh_group_t));
1813 BT_INFO("Mesh: Network Create Virtual group");
1815 l_getrandom(grp->label_uuid, 16);
1816 _bt_mesh_util_crypto_create_virtual_address(
1817 grp->label_uuid, &grp->grp_addr);
1819 /* For simplicity sake, avoid labels that map to the same hash */
1820 l1 = g_slist_find_custom(cdb_cfg->groups,
1821 GUINT_TO_POINTER(grp->grp_addr), __mesh_compare_addr);
1824 if (!_bt_mesh_conf_insert_group_info(cdb_cfg, grp)) {
1825 BT_ERR("Mesh: unable to save group in Conf DB!!");
1827 return BLUETOOTH_ERROR_INTERNAL;
1829 req->is_virtual = true;
1830 req->group_addr = grp->grp_addr;
1831 _bt_mesh_util_convert_hex_to_string(
1832 (uint8_t *) grp->label_uuid, 16, req->label_uuid,
1833 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1835 cdb_cfg->groups = g_slist_append(cdb_cfg->groups, grp);
1836 BT_INFO("Mesh: Virtual Group[0x%2.2x] inserted in List",
1838 BT_INFO("Mesh: Total groups present in Network after update [%d]",
1839 g_slist_length(cdb_cfg->groups));
1840 return BLUETOOTH_ERROR_NONE;
1848 /* Failed to create a unique hash */
1849 return BLUETOOTH_ERROR_INTERNAL;
1851 BT_INFO("Mesh: Network Create group Addr: [0x%2.2x]", addr);
1853 if (!MESH_IS_GROUP(addr)) {
1854 BT_ERR("Mesh: Group Address [0x%2.2x] is not valid!", addr);
1855 return BLUETOOTH_ERROR_INVALID_PARAM;
1858 l2 = g_slist_find_custom(cdb_cfg->groups,
1859 GUINT_TO_POINTER(addr), __mesh_compare_addr);
1862 _bt_mesh_group_t *grp = l2->data;
1863 req->is_virtual = false;
1864 req->group_addr = grp->grp_addr;
1865 BT_INFO("Mesh: Group already found: addr [0x%2.2x]", grp->grp_addr);
1867 /* Group is not present */
1868 _bt_mesh_group_t *grp = g_malloc0(sizeof(_bt_mesh_group_t));
1869 grp->grp_addr = addr;
1870 if (!_bt_mesh_conf_insert_group_info(cdb_cfg, grp)) {
1871 BT_ERR("Mesh: unable to save group in Conf DB!!");
1873 return BLUETOOTH_ERROR_INTERNAL;
1875 cdb_cfg->groups = g_slist_append(cdb_cfg->groups, grp);
1876 req->is_virtual = false;
1877 req->group_addr = grp->grp_addr;
1878 BT_INFO("Mesh: Group[0x%2.2x] inserted in List",
1881 BT_INFO("Mesh: Total groups present in Network after update [%d]",
1882 g_slist_length(cdb_cfg->groups));
1885 return BLUETOOTH_ERROR_NONE;
1888 int _bt_mesh_network_get_groups(const char *app_cred, const char *sender,
1889 bluetooth_mesh_network_t *network,
1893 _bt_mesh_cdb_t *cdb_cfg = NULL;
1894 uint8_t net_uuid[16];
1896 BT_INFO("Mesh: Get All groups from Network [%s]", network->uuid);
1897 _bt_mesh_util_convert_string_to_hex(network->uuid,
1898 strlen(network->uuid), net_uuid, 16);
1900 l = g_slist_find_custom(cdb_list, net_uuid,
1901 __mesh_compare_app_network_uuid);
1903 return BLUETOOTH_ERROR_INVALID_PARAM;
1905 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1906 BT_INFO("Mesh: Got CDB");
1908 BT_INFO("Mesh: Total groups present in Network [%d]",
1909 g_slist_length(cdb_cfg->groups));
1911 for (l = cdb_cfg->groups; l; l = l->next) {
1912 bluetooth_mesh_network_group_info_t grp;
1913 _bt_mesh_group_t *group = l->data;
1914 memset(&grp, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
1915 g_strlcpy(grp.net_uuid, network->uuid, sizeof(grp.net_uuid));
1916 if (MESH_IS_GROUP(group->grp_addr) && !MESH_IS_VIRTUAL(group->grp_addr)) {
1917 grp.is_virtual = false;
1918 grp.group_addr = group->grp_addr;
1919 BT_INFO("Mesh: Found Non-Virtual group, addr[0x%2.2x]", group->grp_addr);
1920 } else if (MESH_IS_VIRTUAL(group->grp_addr)) {
1921 grp.is_virtual = true;
1922 grp.group_addr = group->grp_addr;
1923 _bt_mesh_util_convert_hex_to_string((uint8_t *) group->label_uuid,
1924 16, grp.label_uuid, sizeof(grp.label_uuid));
1925 BT_INFO("Mesh: Found Virtual group, addr[0x%2.2x]", group->grp_addr);
1926 BT_INFO("Mesh: Label UUID[%s]", grp.label_uuid);
1929 g_array_append_vals(*out_param,
1930 &grp, sizeof(bluetooth_mesh_network_group_info_t));
1933 return BLUETOOTH_ERROR_NONE;
1936 int _bt_mesh_network_get_nodes(const char *app_cred,
1937 const char *sender, bluetooth_mesh_network_t *network,
1941 _bt_mesh_cdb_t *cdb_cfg = NULL;
1944 l = g_slist_find_custom(cdb_list, network->token.token,
1945 __mesh_compare_app_cdb_token);
1947 return BLUETOOTH_ERROR_INVALID_PARAM;
1949 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1951 if (_bt_mesh_node_get_all(cdb_cfg->uuid, out_param))
1952 return BLUETOOTH_ERROR_NONE;
1954 return BLUETOOTH_ERROR_INTERNAL;