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-nodes.h"
48 #include "bt-service-mesh-keys.h"
49 #include "bt-service-mesh-util.h"
51 #include "bt-internal-types.h"
53 #include <oal-hardware.h>
54 #include <oal-manager.h>
55 #include <oal-event.h>
56 #include <oal-adapter-mgr.h>
57 #include <oal-device-mgr.h>
60 /* Temp local node structure.
61 Create & save node temporarily till network creation */
65 bluetooth_mesh_vendor_info_t vendor_info;
66 uint8_t node_uuid[16];
68 uint16_t prim_unicast;
72 /* List of CDB's for the created networks */
73 static GSList *cdb_list = NULL;
75 /* List of local ndoes to be created: Free memory once saved in DB */
76 static GSList *temp_nodes = NULL;
78 /* Scanning state: Unprovisioned device scan */
79 static bool bt_mesh_is_scanning = false;
81 /* Provisoning state */
82 static bool bt_mesh_is_provisoning = false;
84 static gint __mesh_compare_hex_uuid(gconstpointer data,
85 gconstpointer user_data)
87 const mesh_local_node_t *u1 = data;
88 const uint8_t *u2 = user_data;
90 retv_if(NULL == u1, -1);
91 retv_if(NULL == u2, -1);
93 return memcmp(u1->node_uuid, u2, 16);
96 static gint __mesh_compare_app_network_uuid(
97 gconstpointer data, gconstpointer user_data)
99 const _bt_mesh_cdb_t *cdb = data;
100 const uint8_t *net_uuid = user_data;
102 retv_if(NULL == cdb, -1);
103 retv_if(NULL == net_uuid, -1);
105 return memcmp(net_uuid, cdb->uuid, 16);
108 static gint __mesh_compare_app_cdb_token(gconstpointer data,
109 gconstpointer user_data)
112 const _bt_mesh_cdb_t *cdb = data;
113 const char *token = user_data;
115 retv_if(NULL == cdb, -1);
116 retv_if(NULL == token, -1);
118 _bt_mesh_util_convert_hex_to_string((uint8_t *) cdb->token,
121 BT_INFO("Mesh: Match Token 1[%s] Token 2 [%s]",
123 return g_strcmp0(token_str, token);
126 static gint __mesh_compare_addr(
127 const void *a, const void *b)
129 const _bt_mesh_group_t *grp = a;
130 uint16_t addr = GPOINTER_TO_UINT(b);
131 BT_INFO("Mesh: Network group addr [0x%2.2x]", grp->grp_addr);
132 BT_INFO("Mesh: To be matched group addr [0x%2.2x]", addr);
134 return (grp->grp_addr - addr);
137 int _bt_mesh_load_app_networks(const char *app_cred)
139 int ret = BLUETOOTH_ERROR_NONE;
141 /* Load Mesh Networks for the current app */
142 if (!_bt_mesh_util_is_directory_exists(MESH_CDB_DEFAULT_DIR_PATH))
143 return BLUETOOTH_ERROR_INTERNAL;
148 static void __bt_mesh_free_temp_node(mesh_local_node_t *tmp)
152 temp_nodes = g_slist_remove(temp_nodes, tmp);
153 g_free((gpointer)tmp->app_cred);
154 g_free((gpointer)tmp->sender);
155 /* Do not free tmp->model_list here
156 This memory is also being used in HAL layer
157 This memory will be freed with the call of __mesh_client_disconnected */
158 tmp->model_list = NULL;
162 int _bt_mesh_network_request_provisioning_data_request(uint8_t net_uuid[],
166 int ret = OAL_STATUS_SUCCESS;
168 char uuid_string[33];
170 _bt_mesh_util_convert_hex_to_string(net_uuid, 16,
171 uuid_string, sizeof(uuid_string));
172 BT_INFO("Mesh: Provisioning Data requested for network [%s]",
175 unicast = _bt_mesh_node_get_next_unicast(net_uuid,
176 MESH_DEFAULT_START_ADDRESS,
177 MESH_DEFAULT_MAX_ADDRESS, count);
178 BT_INFO("Mesh: Network: Got unicast [%4.4x]", unicast);
180 memcpy(uuid.uuid, net_uuid, 16);
181 /* Register Mesh Node */
182 ret = mesh_network_send_provisioning_data(&uuid,
183 MESH_PRIMARY_NET_IDX, unicast);
184 if (ret != OAL_STATUS_SUCCESS) {
185 BT_ERR("ret: %d", ret);
186 return BLUETOOTH_ERROR_INTERNAL;
188 return BLUETOOTH_ERROR_NONE;
191 int _bt_mesh_network_remove_node_configuration(
192 bluetooth_mesh_node_info_t *node)
195 _bt_mesh_cdb_t *cdb_cfg = NULL;
196 uint8_t net_uuid[16];
197 BT_INFO("Mesh: Remove Node Configuration: Unicast [0x%2.2x]",
198 node->primary_unicast);
200 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
201 strlen(node->net_uuid), net_uuid, 16);
204 l = g_slist_find_custom(cdb_list, net_uuid,
205 __mesh_compare_app_network_uuid);
207 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
208 return BLUETOOTH_ERROR_INVALID_PARAM;
211 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
213 if (_bt_mesh_conf_delete_node(cdb_cfg, node->primary_unicast)) {
214 BT_INFO("Mesh: Node Entry deleted from Config DB");
215 if (!_bt_mesh_node_del_node(cdb_cfg, node->primary_unicast)) {
216 BT_ERR("Mesh: Node Entry could not be unloaded from memory");
217 return BLUETOOTH_ERROR_INTERNAL;
220 BT_ERR("Mesh: Node Entry could not be deleted from Config DB");
221 return BLUETOOTH_ERROR_INTERNAL;
223 return BLUETOOTH_ERROR_NONE;
226 void _bt_mesh_network_unload_net_configuration(_bt_mesh_cdb_t *cdb_cfg)
228 BT_INFO("Mesh: Unload Network Configuration");
230 /* Unload Network from Keys */
231 _bt_mesh_keys_unload_net(cdb_cfg->uuid);
233 /* Unload Network from Nodes */
234 _bt_mesh_node_unload_net(cdb_cfg->uuid);
236 cdb_list = g_slist_remove(cdb_list, cdb_cfg);
237 _bt_mesh_conf_free(cdb_cfg);
238 BT_INFO("Mesh: CDB freed from memory: Remaining Networks [%d]",
239 g_slist_length(cdb_list));
242 int _bt_mesh_network_remove_net_configuration(
243 bluetooth_mesh_network_t *net)
247 _bt_mesh_cdb_t *cdb_cfg = NULL;
248 uint8_t net_uuid[16];
249 BT_INFO("Mesh: Remove network Configuration");
251 _bt_mesh_util_convert_string_to_hex(net->uuid,
252 strlen(net->uuid), net_uuid, 16);
255 l = g_slist_find_custom(cdb_list, net_uuid,
256 __mesh_compare_app_network_uuid);
258 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
259 return BLUETOOTH_ERROR_INVALID_PARAM;
262 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
264 /* Create the CDB for the network */
265 file_path = g_strdup_printf("%s.bak", cdb_cfg->cfg_fname);
267 BT_INFO("Mesh: CDB File path[%s]", cdb_cfg->cfg_fname);
268 BT_INFO("Mesh: CDB Backup File path[%s]", file_path);
270 /* Remove the config */
271 if (!_bt_mesh_util_delete_file(cdb_cfg->cfg_fname))
272 return BLUETOOTH_ERROR_INTERNAL;
274 /* Remove the config backup */
275 if (!_bt_mesh_util_delete_file(file_path))
276 return BLUETOOTH_ERROR_INTERNAL;
277 BT_INFO("Mesh: Config DB file removed!!");
279 cdb_list = g_slist_remove(cdb_list, cdb_cfg);
280 _bt_mesh_conf_free(cdb_cfg);
281 BT_INFO("Mesh: CDB freed from memory");
286 /* Unload Network from Keys */
287 _bt_mesh_keys_unload_net(net_uuid);
289 /* Unload Network from Nodes */
290 _bt_mesh_node_unload_net(net_uuid);
292 BT_INFO("Mesh: Cleanup Done!");
293 return BLUETOOTH_ERROR_NONE;
296 int _bt_mesh_network_create_cdb(int result,
297 const char *sender, const char *app_creds,
298 uint8_t uuid[16], uint8_t token[8],
301 char *dir_path = NULL;
302 char *file_path = NULL;
304 mesh_local_node_t *tmp;
305 _bt_mesh_cdb_t *cdb_cfg = NULL;
306 char uuid_string[33];
307 char token_string[17];
309 _bt_mesh_util_convert_hex_to_string(uuid, 16,
310 uuid_string, sizeof(uuid_string));
311 _bt_mesh_util_convert_hex_to_string(token, 8,
312 token_string, sizeof(token_string));
313 BT_INFO("Mesh: Create CDB request for network UUID [%s] token [%s]",
314 uuid_string, token_string);
315 BT_INFO("Mesh: Temporary node count [%d]",
316 g_slist_length(temp_nodes));
319 l = g_slist_find_custom(temp_nodes, uuid,
320 __mesh_compare_hex_uuid);
322 BT_ERR("Mesh: Temp Node not found for the UUID [%s]",
324 return BLUETOOTH_ERROR_INTERNAL;
327 tmp = (mesh_local_node_t*)l->data;
329 if (result != BLUETOOTH_ERROR_NONE) {
330 /* Free the node structure */
331 __bt_mesh_free_temp_node(tmp);
335 BT_INFO("Mesh: Create CDB for the New Network [%s]",
337 /* Stat/Create directory for new CDB file */
338 dir_path = g_strdup_printf(MESH_CDB_DEFAULT_DIR_PATH"/%s/",
340 BT_INFO("Mesh: Directory path for new Network CDB [%s]",
342 if (!_bt_mesh_util_create_directory(dir_path)) {
344 __bt_mesh_free_temp_node(tmp);
345 return BLUETOOTH_ERROR_INTERNAL;
348 BT_INFO("Mesh: Directory Created successfully");
349 /* Create the CDB for the network */
350 file_path = g_strdup_printf("%s%s_config.json",
351 dir_path, uuid_string);
352 BT_INFO("Mesh: CDB File path[%s]", file_path);
353 BT_INFO("Mesh: CDB App Cred[%s]", app_creds);
354 cdb_cfg = _bt_mesh_conf_database_create(file_path, uuid,
355 token, network, app_creds);
362 /* Free the memory of temporary node
363 which was created during Network creation */
364 __bt_mesh_free_temp_node(tmp);
365 return BLUETOOTH_ERROR_INTERNAL;
367 BT_INFO("Mesh: CDB CFG file created successfully");
369 _bt_mesh_conf_set_unicast_address_range(cdb_cfg,
370 MESH_DEFAULT_START_ADDRESS,
371 MESH_DEFAULT_MAX_ADDRESS);
372 BT_INFO("Mesh: Address range Set for network");
374 /* Create new network for saving network specific Keys */
375 _bt_mesh_keys_load_net(cdb_cfg->uuid);
376 BT_INFO("Mesh: Address range Set for network");
377 _bt_mesh_keys_add_net_key(cdb_cfg->uuid,
378 MESH_PRIMARY_NET_IDX);
379 BT_INFO("Mesh: Primary net key added to network memeory");
380 _bt_mesh_conf_insert_network_key(cdb_cfg,
381 MESH_PRIMARY_NET_IDX,
382 MESH_KEY_REFRESH_PHASE_NONE);
383 BT_INFO("Mesh: Primary net key added to CDB");
385 /* Create new network for saving network specific nodes */
386 _bt_mesh_node_load_net(cdb_cfg->uuid);
387 BT_INFO("Mesh: local Node loaded to memory");
388 _bt_mesh_node_add_node(cdb_cfg->uuid, tmp->node_uuid,
389 tmp->prim_unicast, tmp->num_elems,
390 MESH_PRIMARY_NET_IDX);
391 BT_INFO("Mesh: Added basic info (num elems, UUID, primary NetIDx)");
393 /* Add Primary Node as 1st entry in CDB */
394 _bt_mesh_conf_insert_node_object(cdb_cfg, /* Dev UUID */uuid,
395 tmp->num_elems, tmp->prim_unicast,
396 MESH_PRIMARY_NET_IDX);
397 BT_INFO("Mesh: Added Local node's basic info in CDB");
399 cdb_list = g_slist_append(cdb_list, cdb_cfg);
400 BT_INFO("Mesh: CDB added to list");
402 __bt_mesh_free_temp_node(tmp);
403 BT_INFO("Mesh: temp node freed");
404 return BLUETOOTH_ERROR_NONE;
407 int _bt_mesh_network_unload(const char *app_cred,
408 const char *sender, bluetooth_mesh_network_t *network)
411 uint8_t net_uuid[16];
412 _bt_mesh_cdb_t *cdb_cfg;
414 BT_INFO("Mesh: Unload Network Configuration");
415 /* If Scanning is going on */
416 if (_bt_mesh_is_provisioning() ||
417 _bt_mesh_is_scanning()) {
418 BT_ERR("Device is buzy..");
419 return BLUETOOTH_ERROR_DEVICE_BUSY;
422 _bt_mesh_util_convert_string_to_hex(network->uuid,
423 strlen(network->uuid), net_uuid, 16);
426 l = g_slist_find_custom(cdb_list, net_uuid,
427 __mesh_compare_app_network_uuid);
429 BT_ERR("Mesh: Could not find Network Entry: unexpected!!");
430 return BLUETOOTH_ERROR_INVALID_PARAM;
433 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
435 _bt_mesh_network_unload_net_configuration(cdb_cfg);
436 return BLUETOOTH_ERROR_NONE;
439 int _bt_mesh_network_destroy(const char *app_cred,
440 const char *sender, bluetooth_mesh_network_t *network)
442 int ret = OAL_STATUS_SUCCESS;
445 /* If Scanning is going on */
446 if (_bt_mesh_is_provisioning() ||
447 _bt_mesh_is_scanning()) {
448 BT_ERR("Device is buzy..");
449 return BLUETOOTH_ERROR_DEVICE_BUSY;
451 _bt_mesh_util_convert_string_to_hex(network->uuid,
452 strlen(network->uuid), net_uuid.uuid, 16);
453 /* Destroy Mesh Network */
454 ret = mesh_network_destroy(&net_uuid);
455 if (ret != OAL_STATUS_SUCCESS) {
456 BT_ERR("ret: %d", ret);
457 return BLUETOOTH_ERROR_INTERNAL;
460 return BLUETOOTH_ERROR_NONE;
463 int _bt_mesh_network_scan(const char *app_cred,
465 bluetooth_mesh_network_t *network,
466 bluetooth_mesh_scan_param_t *param)
468 int ret = OAL_STATUS_SUCCESS;
471 /* If Scanning is going on */
472 if (_bt_mesh_is_provisioning() ||
473 _bt_mesh_is_scanning()) {
474 BT_ERR("Device is buzy..");
475 return BLUETOOTH_ERROR_DEVICE_BUSY;
477 _bt_mesh_util_convert_string_to_hex(network->uuid,
478 strlen(network->uuid), net_uuid.uuid, 16);
479 /* Register Mesh Node */
480 ret = mesh_network_start_scan(&net_uuid,
481 (oal_mesh_scan_params_t*) param);
482 if (ret != OAL_STATUS_SUCCESS) {
483 BT_ERR("ret: %d", ret);
484 return BLUETOOTH_ERROR_INTERNAL;
487 /* Set scanning state to true */
488 _bt_mesh_set_scanning_state(true);
489 return BLUETOOTH_ERROR_NONE;
492 int _bt_mesh_network_scan_cancel(const char *app_cred,
493 const char *sender, bluetooth_mesh_network_t *network)
495 int ret = OAL_STATUS_SUCCESS;
498 if (!_bt_mesh_is_scanning()) {
499 BT_INFO("Mesh: Scanning is not ongoing");
500 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
502 _bt_mesh_util_convert_string_to_hex(network->uuid,
503 strlen(network->uuid), net_uuid.uuid, 16);
505 /* Cancel unprovisioned device scan */
506 ret = mesh_network_scan_cancel(&net_uuid);
507 if (ret != OAL_STATUS_SUCCESS) {
508 BT_ERR("ret: %d", ret);
509 return BLUETOOTH_ERROR_INTERNAL;
512 return BLUETOOTH_ERROR_NONE;
515 int _bt_mesh_network_set_provisioner_caps(const char *app_cred,
516 const char *sender, bluetooth_mesh_network_t *network,
517 bluetooth_mesh_provisioner_caps_t *caps)
519 int ret = OAL_STATUS_SUCCESS;
522 _bt_mesh_util_convert_string_to_hex(network->uuid,
523 strlen(network->uuid), net_uuid.uuid, 16);
525 /* Set provisioning capabilities */
526 ret = mesh_network_set_provisioning_capabilities(&net_uuid,
527 (oal_mesh_capabilities_t*) caps);
528 if (ret != OAL_STATUS_SUCCESS) {
529 BT_ERR("ret: %d", ret);
530 return BLUETOOTH_ERROR_INTERNAL;
533 return BLUETOOTH_ERROR_NONE;
536 bool _bt_mesh_is_scanning(void)
538 return bt_mesh_is_scanning;
541 void _bt_mesh_set_scanning_state(bool state)
543 bt_mesh_is_scanning = state;
546 bool _bt_mesh_is_provisioning(void)
548 return bt_mesh_is_provisoning;
551 void _bt_mesh_set_provisioning_state(bool state)
553 bt_mesh_is_provisoning = state;
556 int _bt_mesh_network_provision_device(const char *app_cred,
558 bluetooth_mesh_provisioning_request_t *req)
560 int ret = OAL_STATUS_SUCCESS;
564 /* If Scanning is going on */
565 if (_bt_mesh_is_provisioning() ||
566 _bt_mesh_is_scanning()) {
567 BT_ERR("Device is buzy..");
568 return BLUETOOTH_ERROR_DEVICE_BUSY;
571 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
572 strlen(req->net_uuid), net_uuid.uuid, 16);
573 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
574 strlen(req->dev_uuid), dev_uuid.uuid, 16);
576 ret = mesh_network_provision_device(&net_uuid, &dev_uuid);
578 if (ret != OAL_STATUS_SUCCESS) {
579 BT_ERR("ret: %d", ret);
580 return BLUETOOTH_ERROR_INTERNAL;
583 /* Set Provisioning state */
584 _bt_mesh_set_provisioning_state(true);
586 return BLUETOOTH_ERROR_NONE;
589 int _bt_mesh_authentication_reply(int auth_type,
590 const char *auth_value, gboolean reply)
592 int ret = OAL_STATUS_SUCCESS;
594 if (!_bt_mesh_is_provisioning())
595 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
598 return BLUETOOTH_ERROR_INVALID_PARAM;
600 ret = mesh_authentication_reply(
601 (oal_mesh_variant_authentication_e)auth_type,
604 if (ret != OAL_STATUS_SUCCESS) {
605 BT_ERR("ret: %d", ret);
606 return BLUETOOTH_ERROR_INTERNAL;
609 BT_INFO("Mesh: Scheduled Prov Auth Reply!");
610 return BLUETOOTH_ERROR_NONE;
613 int _bt_mesh_network_get_netkeys(const char *app_cred, const char *sender,
614 bluetooth_mesh_network_t *network, GArray **out_param)
617 _bt_mesh_cdb_t *cdb_cfg = NULL;
620 l = g_slist_find_custom(cdb_list, network->token.token,
621 __mesh_compare_app_cdb_token);
623 return BLUETOOTH_ERROR_INVALID_PARAM;
625 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
627 if (_bt_mesh_keys_get_netkey_list(cdb_cfg->uuid, out_param))
628 return BLUETOOTH_ERROR_NONE;
630 return BLUETOOTH_ERROR_INTERNAL;
633 int _bt_mesh_network_get_appkeys(const char *app_cred, const char *sender,
634 bluetooth_mesh_network_t *network, uint16_t net_idx,
638 _bt_mesh_cdb_t *cdb_cfg = NULL;
641 l = g_slist_find_custom(cdb_list, network->token.token,
642 __mesh_compare_app_cdb_token);
644 return BLUETOOTH_ERROR_INVALID_PARAM;
646 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
648 if (_bt_mesh_keys_get_appkey_list(cdb_cfg->uuid,
650 return BLUETOOTH_ERROR_NONE;
652 return BLUETOOTH_ERROR_INTERNAL;
655 int _bt_mesh_network_node_get_netkeys(const char *app_cred,
656 bluetooth_mesh_node_discover_t *node,
660 _bt_mesh_cdb_t *cdb_cfg = NULL;
661 uint8_t net_uuid[16];
663 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
664 strlen(node->net_uuid), net_uuid, 16);
667 l = g_slist_find_custom(cdb_list, net_uuid,
668 __mesh_compare_app_network_uuid);
670 return BLUETOOTH_ERROR_INVALID_PARAM;
672 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
674 if (_bt_mesh_node_get_all_netkeys(cdb_cfg->uuid,
675 node->unicast, out_param))
676 return BLUETOOTH_ERROR_NONE;
678 return BLUETOOTH_ERROR_INTERNAL;
681 int _bt_mesh_network_node_get_appkeys(const char *app_cred,
682 const char *sender, bluetooth_mesh_node_discover_t *node,
686 _bt_mesh_cdb_t *cdb_cfg = NULL;
687 uint8_t net_uuid[16];
689 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
690 strlen(node->net_uuid), net_uuid, 16);
693 l = g_slist_find_custom(cdb_list, net_uuid,
694 __mesh_compare_app_network_uuid);
696 return BLUETOOTH_ERROR_INVALID_PARAM;
698 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
700 if (_bt_mesh_node_get_all_appkeys(cdb_cfg->uuid,
701 node->unicast, node->netkey_idx, out_param))
702 return BLUETOOTH_ERROR_NONE;
704 return BLUETOOTH_ERROR_INTERNAL;
707 int _bt_mesh_element_get_models(const char *app_cred, const char *sender,
708 bluetooth_mesh_network_t *network, uint16_t unicast,
709 int elem_idx, GArray **out_param)
712 _bt_mesh_cdb_t *cdb_cfg = NULL;
715 l = g_slist_find_custom(cdb_list, network->token.token,
716 __mesh_compare_app_cdb_token);
718 return BLUETOOTH_ERROR_INVALID_PARAM;
720 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
722 if (_bt_mesh_node_get_models(cdb_cfg->uuid, unicast,
723 elem_idx, out_param))
724 return BLUETOOTH_ERROR_NONE;
726 return BLUETOOTH_ERROR_INTERNAL;
729 int _bt_mesh_network_handle_netkey_added(
730 uint8_t net_uuid[], uint16_t netkey_idx)
733 _bt_mesh_cdb_t *cdb_cfg = NULL;
735 BT_INFO("Mesh: Handle Netkey Added");
737 l = g_slist_find_custom(cdb_list, net_uuid,
738 __mesh_compare_app_network_uuid);
740 return BLUETOOTH_ERROR_INVALID_PARAM;
742 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
744 _bt_mesh_keys_add_net_key(cdb_cfg->uuid, netkey_idx);
746 if (!_bt_mesh_conf_insert_network_key(cdb_cfg,
747 netkey_idx, MESH_KEY_REFRESH_PHASE_NONE))
748 return BLUETOOTH_ERROR_INTERNAL;
750 return BLUETOOTH_ERROR_NONE;
753 int _bt_mesh_network_handle_netkey_deleted(
754 uint8_t net_uuid[], uint16_t netkey_idx)
757 _bt_mesh_cdb_t *cdb_cfg = NULL;
759 BT_INFO("Mesh: Handle Netkey Deleted");
761 l = g_slist_find_custom(cdb_list, net_uuid,
762 __mesh_compare_app_network_uuid);
764 return BLUETOOTH_ERROR_INVALID_PARAM;
766 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
768 _bt_mesh_keys_del_net_key(cdb_cfg->uuid, netkey_idx, cdb_cfg);
770 if (!_bt_mesh_conf_delete_network_key(cdb_cfg, netkey_idx))
771 return BLUETOOTH_ERROR_INTERNAL;
773 return BLUETOOTH_ERROR_NONE;
776 int _bt_mesh_network_handle_netkey_updated(
777 uint8_t net_uuid[], uint16_t netkey_idx)
780 _bt_mesh_cdb_t *cdb_cfg = NULL;
782 BT_INFO("Mesh: Handle Netkey Updated");
784 l = g_slist_find_custom(cdb_list, net_uuid,
785 __mesh_compare_app_network_uuid);
787 return BLUETOOTH_ERROR_INVALID_PARAM;
789 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
791 _bt_mesh_keys_set_net_key_phase(cdb_cfg,
792 netkey_idx, MESH_KEY_REFRESH_PHASE_ONE, true);
794 return BLUETOOTH_ERROR_NONE;
797 int _bt_mesh_network_handle_appkey_added(uint8_t net_uuid[],
798 uint16_t netkey_idx, uint16_t appkey_idx)
801 _bt_mesh_cdb_t *cdb_cfg = NULL;
803 BT_INFO("Mesh: Handle Appkey Added");
805 l = g_slist_find_custom(cdb_list, net_uuid,
806 __mesh_compare_app_network_uuid);
808 return BLUETOOTH_ERROR_INVALID_PARAM;
810 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
812 _bt_mesh_keys_add_app_key(cdb_cfg->uuid,
813 netkey_idx, appkey_idx);
815 if (!_bt_mesh_conf_insert_application_key(cdb_cfg,
816 netkey_idx, appkey_idx))
817 return BLUETOOTH_ERROR_INTERNAL;
819 return BLUETOOTH_ERROR_NONE;
822 int _bt_mesh_network_handle_appkey_deleted(uint8_t net_uuid[],
823 uint16_t netkey_idx, uint16_t appkey_idx)
826 _bt_mesh_cdb_t *cdb_cfg = NULL;
828 BT_INFO("Mesh: Handle Appkey Deleted");
830 l = g_slist_find_custom(cdb_list, net_uuid,
831 __mesh_compare_app_network_uuid);
833 return BLUETOOTH_ERROR_INVALID_PARAM;
835 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
837 _bt_mesh_keys_del_app_key(cdb_cfg->uuid, appkey_idx);
839 if (!_bt_mesh_conf_delete_application_key(cdb_cfg, appkey_idx))
840 return BLUETOOTH_ERROR_INTERNAL;
842 return BLUETOOTH_ERROR_NONE;
845 int _bt_mesh_network_set_name(const char *app_cred, const char *sender,
846 bluetooth_mesh_network_t *network)
849 _bt_mesh_cdb_t *cdb_cfg = NULL;
850 BT_INFO("Mesh: Set network name, app_creds [%s]", app_cred);
851 BT_INFO("Mesh: UUID [%s]", network->uuid);
852 BT_INFO("Mesh: Token[%s]", network->token.token);
853 BT_INFO("Mesh: Name to be set[%s]", network->name.name);
856 l = g_slist_find_custom(cdb_list,
857 network->token.token, __mesh_compare_app_cdb_token);
859 return BLUETOOTH_ERROR_INVALID_PARAM;
861 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
862 if (_bt_mesh_conf_set_network_friendly_name(cdb_cfg, network->name.name))
863 return BLUETOOTH_ERROR_NONE;
865 return BLUETOOTH_ERROR_INTERNAL;
868 int _bt_mesh_network_add_netkey(const char *app_cred,
869 const char *sender, bluetooth_mesh_network_t *network)
871 int ret = OAL_STATUS_SUCCESS;
873 _bt_mesh_cdb_t *cdb_cfg = NULL;
875 oal_mesh_key_op_e op = OAL_MESH_KEY_ADD;
879 l = g_slist_find_custom(cdb_list,
880 network->token.token, __mesh_compare_app_cdb_token);
882 return BLUETOOTH_ERROR_INVALID_PARAM;
884 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
886 if (!_bt_mesh_keys_get_new_netkey_index(cdb_cfg->uuid, &idx))
887 return BLUETOOTH_ERROR_INVALID_PARAM;
889 _bt_mesh_util_convert_string_to_hex(network->uuid,
890 strlen(network->uuid), net_uuid.uuid, 16);
892 BT_INFO("Mesh: netkey index to be created [%u]", idx);
893 ret = mesh_network_subnet_execute(&net_uuid, op, idx);
895 if (ret != OAL_STATUS_SUCCESS) {
896 BT_ERR("ret: %d", ret);
897 return BLUETOOTH_ERROR_INTERNAL;
900 return BLUETOOTH_ERROR_NONE;
903 int _bt_mesh_network_delete_netkey(const char *app_cred,
904 const char *sender, bluetooth_mesh_network_t *network,
907 int ret = OAL_STATUS_SUCCESS;
909 _bt_mesh_cdb_t *cdb_cfg = NULL;
911 oal_mesh_key_op_e op = OAL_MESH_KEY_DELETE;
914 l = g_slist_find_custom(cdb_list, network->token.token,
915 __mesh_compare_app_cdb_token);
917 return BLUETOOTH_ERROR_INVALID_PARAM;
919 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
921 /* Check if NetKey entry is present in local Network */
922 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, index))
923 return BLUETOOTH_ERROR_INVALID_PARAM;
925 /* Check if NetKey is already added to one of the provisioned nodes */
926 if (_bt_mesh_node_is_netkey_added_in_network(cdb_cfg->uuid, index)) {
927 BT_INFO("Mesh: NetKey index [0x%2.2x] can not be deleted", index);
928 return BLUETOOTH_ERROR_INVALID_PARAM;
931 _bt_mesh_util_convert_string_to_hex(network->uuid,
932 strlen(network->uuid), net_uuid.uuid, 16);
934 BT_INFO("Mesh: netkey index to be deleted [%u]", index);
935 ret = mesh_network_subnet_execute(&net_uuid, op, index);
937 if (ret != OAL_STATUS_SUCCESS) {
938 BT_ERR("ret: %d", ret);
939 return BLUETOOTH_ERROR_INTERNAL;
942 return BLUETOOTH_ERROR_NONE;
945 int _bt_mesh_network_update_netkey(const char *app_cred,
946 const char *sender, bluetooth_mesh_network_t *network,
949 int ret = OAL_STATUS_SUCCESS;
951 _bt_mesh_cdb_t *cdb_cfg = NULL;
953 oal_mesh_key_op_e op = OAL_MESH_KEY_UPDATE;
956 l = g_slist_find_custom(cdb_list, network->token.token,
957 __mesh_compare_app_cdb_token);
959 return BLUETOOTH_ERROR_INVALID_PARAM;
961 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
963 /* Check if NetKey entry is present in local Network */
964 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, index))
965 return BLUETOOTH_ERROR_INVALID_PARAM;
967 _bt_mesh_util_convert_string_to_hex(network->uuid,
968 strlen(network->uuid), net_uuid.uuid, 16);
970 BT_INFO("Mesh: netkey index to be updated [%u]", index);
971 ret = mesh_network_subnet_execute(&net_uuid, op, index);
973 if (ret != OAL_STATUS_SUCCESS) {
974 BT_ERR("ret: %d", ret);
975 return BLUETOOTH_ERROR_INTERNAL;
978 return BLUETOOTH_ERROR_NONE;
981 int _bt_mesh_network_add_appkey(const char *app_cred,
982 const char *sender, bluetooth_mesh_network_t *network,
985 int ret = OAL_STATUS_SUCCESS;
987 _bt_mesh_cdb_t *cdb_cfg = NULL;
989 oal_mesh_key_op_e op = OAL_MESH_KEY_ADD;
993 l = g_slist_find_custom(cdb_list, network->token.token,
994 __mesh_compare_app_cdb_token);
996 return BLUETOOTH_ERROR_INVALID_PARAM;
998 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1000 /* Check if NetKey entry is present in local Network */
1001 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1002 return BLUETOOTH_ERROR_INVALID_PARAM;
1004 if (!_bt_mesh_keys_get_new_appkey_index(cdb_cfg->uuid, &idx))
1005 return BLUETOOTH_ERROR_INVALID_PARAM;
1007 _bt_mesh_util_convert_string_to_hex(network->uuid,
1008 strlen(network->uuid), net_uuid.uuid, 16);
1010 BT_INFO("Mesh: AppKey index to be created [%u]", idx);
1011 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, idx);
1013 if (ret != OAL_STATUS_SUCCESS) {
1014 BT_ERR("ret: %d", ret);
1015 return BLUETOOTH_ERROR_INTERNAL;
1018 return BLUETOOTH_ERROR_NONE;
1021 int _bt_mesh_network_update_appkey(const char *app_cred,
1022 const char *sender, bluetooth_mesh_network_t *network,
1023 uint16_t netkey_idx, uint16_t appkey_idx)
1025 int ret = OAL_STATUS_SUCCESS;
1027 _bt_mesh_cdb_t *cdb_cfg = NULL;
1028 oal_uuid_t net_uuid;
1029 oal_mesh_key_op_e op = OAL_MESH_KEY_UPDATE;
1032 l = g_slist_find_custom(cdb_list, network->token.token,
1033 __mesh_compare_app_cdb_token);
1035 return BLUETOOTH_ERROR_INVALID_PARAM;
1037 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1039 /* Check if NetKey entry is present in local Network */
1040 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1041 return BLUETOOTH_ERROR_INVALID_PARAM;
1043 _bt_mesh_util_convert_string_to_hex(network->uuid,
1044 strlen(network->uuid), net_uuid.uuid, 16);
1046 BT_INFO("Mesh: AppKey index to be updated [%u]", appkey_idx);
1047 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, appkey_idx);
1049 if (ret != OAL_STATUS_SUCCESS) {
1050 BT_ERR("ret: %d", ret);
1051 return BLUETOOTH_ERROR_INTERNAL;
1054 return BLUETOOTH_ERROR_NONE;
1057 int _bt_mesh_network_delete_appkey(const char *app_cred,
1058 const char *sender, bluetooth_mesh_network_t *network,
1059 uint16_t netkey_idx, uint16_t appkey_idx)
1061 int ret = OAL_STATUS_SUCCESS;
1063 _bt_mesh_cdb_t *cdb_cfg = NULL;
1064 oal_uuid_t net_uuid;
1065 oal_mesh_key_op_e op = OAL_MESH_KEY_DELETE;
1068 l = g_slist_find_custom(cdb_list, network->token.token,
1069 __mesh_compare_app_cdb_token);
1071 return BLUETOOTH_ERROR_INVALID_PARAM;
1073 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1075 /* Check if NetKey entry is present in local Network */
1076 if (!_bt_mesh_keys_is_netkey_present(cdb_cfg->uuid, netkey_idx))
1077 return BLUETOOTH_ERROR_INVALID_PARAM;
1079 /* Check if AppKey is already added to one of the provisioned nodes */
1080 if (_bt_mesh_node_is_appkey_added_in_network(cdb_cfg->uuid, appkey_idx))
1081 return BLUETOOTH_ERROR_INVALID_PARAM;
1083 _bt_mesh_util_convert_string_to_hex(network->uuid,
1084 strlen(network->uuid), net_uuid.uuid, 16);
1086 BT_INFO("Mesh: AppKey index to be deleted [%u]", appkey_idx);
1087 ret = mesh_network_appkey_execute(&net_uuid, op, netkey_idx, appkey_idx);
1089 if (ret != OAL_STATUS_SUCCESS) {
1090 BT_ERR("ret: %d", ret);
1091 return BLUETOOTH_ERROR_INTERNAL;
1094 return BLUETOOTH_ERROR_NONE;
1097 int _bt_mesh_network_create(const char *app_cred, const char *sender,
1098 const char *network_name, bluetooth_mesh_node_t *node,
1101 int ret = OAL_STATUS_SUCCESS;
1103 BT_INFO("Mesh: App Credential#### [%s] sender [%s] network [%s]",
1104 app_cred, sender, network_name);
1106 /* TODO Handle Buzy status */
1107 /* Sanity Check: CDB directory creation */
1108 if (!_bt_mesh_util_is_directory_exists(MESH_CDB_DEFAULT_DIR_PATH)) {
1109 BT_INFO("MESH: CDB directory does not exist");
1110 if (!_bt_mesh_util_create_directory(MESH_CDB_DEFAULT_DIR_PATH)) {
1111 BT_ERR("MESH: Fail to create Mesh CDB directory");
1112 return BLUETOOTH_ERROR_INTERNAL;
1116 BT_INFO("Mesh: Send Local Network Creation Request to OAL");
1118 /* Register Mesh Node */
1119 ret = mesh_register_node((oal_mesh_node_t*)node, model_list, true);
1120 if (ret != OAL_STATUS_SUCCESS) {
1121 BT_ERR("ret: %d", ret);
1122 return BLUETOOTH_ERROR_INTERNAL;
1125 BT_INFO("Mesh: Request Sent to Stack successfully");
1126 /* Create a temporary node & wait for Network Created event */
1127 mesh_local_node_t *temp = g_malloc0(sizeof(mesh_local_node_t));
1128 memcpy(temp->node_uuid, node->uuid, 16);
1129 temp->num_elems = node->num_elements;
1130 temp->prim_unicast = node->primary_unicast;
1131 temp->sender = g_strdup(sender);
1132 temp->app_cred = g_strdup(app_cred);
1133 temp->vendor_info = node->vendor_info;
1134 temp->model_list = model_list;
1135 temp_nodes = g_slist_append(temp_nodes, temp);
1137 return BLUETOOTH_ERROR_NONE;
1140 bool _bt_mesh_network_save_remote_node_appkey(
1141 uint8_t net_uuid[], uint16_t remote_unicast,
1142 uint16_t netkey_idx, uint16_t appkey_idx)
1145 _bt_mesh_cdb_t *cdb_cfg = NULL;
1147 BT_INFO("Mesh: Add Appkey [0x%2.2x] to node", appkey_idx);
1149 l = g_slist_find_custom(cdb_list, net_uuid,
1150 __mesh_compare_app_network_uuid);
1154 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1156 if (_bt_mesh_node_is_appkey_exists(net_uuid,
1157 remote_unicast, appkey_idx)) {
1158 BT_INFO("Mesh: AppKey is already added to Node");
1161 if (_bt_mesh_node_add_app_key(net_uuid,
1162 remote_unicast, appkey_idx)) {
1163 BT_INFO("Mesh: Add App Key Idx [0x%2.2x] in CDB", appkey_idx);
1164 return _bt_mesh_conf_node_insert_application_key(cdb_cfg,
1165 remote_unicast, appkey_idx);
1168 BT_INFO("Mesh: AppKey idx [0x%2.2x] Failed to add in Node", appkey_idx);
1172 bool _bt_mesh_network_delete_remote_node_appkey(
1173 uint8_t net_uuid[], uint16_t remote_unicast,
1174 uint16_t netkey_idx, uint16_t appkey_idx)
1177 _bt_mesh_cdb_t *cdb_cfg = NULL;
1178 bool is_deleted = false;
1180 BT_INFO("Mesh: Delete Appkey Idx[0x%2.2x]from Node Unicast [0x%2.2x]",
1181 appkey_idx, remote_unicast);
1183 l = g_slist_find_custom(cdb_list, net_uuid,
1184 __mesh_compare_app_network_uuid);
1188 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1190 if (_bt_mesh_node_del_app_key(net_uuid,
1191 remote_unicast, appkey_idx)) {
1192 BT_INFO("Mesh: Removed Appkey from node");
1193 is_deleted = _bt_mesh_conf_node_delete_application_key(cdb_cfg,
1194 remote_unicast, appkey_idx);
1196 BT_INFO("Mesh: AppKey removed from CDB Node Entry");
1198 BT_INFO("Mesh: AppKey could not be removed from CDB Node Entry");
1204 bool _bt_mesh_network_save_remote_node_ttl(
1205 uint8_t net_uuid[], uint16_t remote_unicast,
1209 _bt_mesh_cdb_t *cdb_cfg = NULL;
1212 l = g_slist_find_custom(cdb_list, net_uuid,
1213 __mesh_compare_app_network_uuid);
1217 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1219 return _bt_mesh_conf_node_set_timetolive_value(cdb_cfg,
1220 remote_unicast, ttl);
1223 bool _bt_mesh_network_save_remote_node_netkey(
1224 uint8_t net_uuid[], uint16_t remote_unicast,
1225 uint16_t netkey_idx)
1228 _bt_mesh_cdb_t *cdb_cfg = NULL;
1231 l = g_slist_find_custom(cdb_list, net_uuid,
1232 __mesh_compare_app_network_uuid);
1236 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1238 if (_bt_mesh_node_is_netkey_exists(net_uuid,
1239 remote_unicast, netkey_idx)) {
1240 BT_INFO("Mesh: NetKey is already added to Node");
1243 if (_bt_mesh_node_add_net_key(net_uuid,
1244 remote_unicast, netkey_idx))
1245 return _bt_mesh_conf_node_insert_network_key(cdb_cfg,
1246 remote_unicast, netkey_idx);
1251 bool _bt_mesh_network_delete_remote_node_netkey(
1252 uint8_t net_uuid[], uint16_t remote_unicast,
1253 uint16_t netkey_idx)
1256 _bt_mesh_cdb_t *cdb_cfg = NULL;
1259 l = g_slist_find_custom(cdb_list, net_uuid,
1260 __mesh_compare_app_network_uuid);
1264 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1266 if (_bt_mesh_node_del_net_key(cdb_cfg, net_uuid,
1267 remote_unicast, netkey_idx))
1268 return _bt_mesh_conf_node_delete_network_key(cdb_cfg,
1269 remote_unicast, netkey_idx);
1273 bool _bt_mesh_network_save_remote_node_composition(
1274 uint8_t net_uuid[], uint16_t remote_unicast,
1275 uint8_t *data, uint16_t data_len)
1278 _bt_mesh_cdb_t *cdb_cfg = NULL;
1281 l = g_slist_find_custom(cdb_list, net_uuid,
1282 __mesh_compare_app_network_uuid);
1286 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1288 return _bt_mesh_conf_set_node_comp_data(cdb_cfg,
1289 remote_unicast, data, data_len);
1292 int _bt_mesh_network_add_remote_node(uint8_t net_uuid[],
1293 uint8_t dev_uuid[], uint16_t unicast, uint8_t count)
1296 _bt_mesh_cdb_t *cdb_cfg = NULL;
1299 l = g_slist_find_custom(cdb_list, net_uuid,
1300 __mesh_compare_app_network_uuid);
1302 return BLUETOOTH_ERROR_INVALID_PARAM;
1304 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1306 _bt_mesh_node_add_node(net_uuid, dev_uuid,
1307 unicast, count, MESH_PRIMARY_NET_IDX);
1309 /* Add Remote Node entry in CDB */
1310 _bt_mesh_conf_insert_node_object(cdb_cfg, /* Dev UUID */ dev_uuid,
1311 count, unicast, MESH_PRIMARY_NET_IDX);
1313 return BLUETOOTH_ERROR_NONE;
1316 bool _bt_mesh_node_get_vendor_features(uint8_t net_uuid[],
1317 uint16_t unicast, bluetooth_mesh_node_features_t *feats)
1320 _bt_mesh_cdb_t *cdb_cfg = NULL;
1321 BT_INFO("Mesh: Attempt to get vendor features: unicast [0x%2.2x]", unicast);
1323 l = g_slist_find_custom(cdb_list, net_uuid,
1324 __mesh_compare_app_network_uuid);
1328 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1330 BT_INFO("Mesh: Read Vendor Features from CDB");
1331 return _bt_mesh_conf_fetch_vendor_specific_info(cdb_cfg, unicast,
1332 &feats->vendor_info.companyid, &feats->vendor_info.vendorid,
1333 &feats->vendor_info.versionid, &feats->vendor_info.crpl,
1334 &feats->features.relay, &feats->features.frnd,
1335 &feats->features.proxy, &feats->features.lpn);
1338 int _bt_mesh_network_load_cdb(int result, const char *sender,
1339 const char *app_creds,
1340 uint8_t uuid[16], uint8_t token[8],
1345 _bt_mesh_cdb_t *cdb_cfg = NULL;
1347 token_str = _bt_service_convert_hex_to_string(token, 8);
1350 l = g_slist_find_custom(cdb_list, token_str,
1351 __mesh_compare_app_cdb_token);
1353 return BLUETOOTH_ERROR_INTERNAL;
1356 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1358 if (result != BLUETOOTH_ERROR_NONE)
1361 /* Create new network for saving network specific Keys */
1362 _bt_mesh_keys_load_net(cdb_cfg->uuid);
1363 if (!_bt_mesh_conf_load_all_keys(cdb_cfg)) {
1364 _bt_mesh_keys_unload_net(cdb_cfg->uuid);
1368 /* Create new network for saving network specific nodes */
1369 _bt_mesh_node_load_net(cdb_cfg->uuid);
1370 if (!_bt_mesh_conf_load_all_nodes(cdb_cfg)) {
1371 _bt_mesh_node_unload_net(cdb_cfg->uuid);
1375 /* Load Groups Created */
1376 cdb_cfg->groups = _bt_mesh_conf_load_group_info(cdb_cfg);
1378 /* Fetch Network name */
1379 *network = (char*)_bt_mesh_conf_get_network_friendly_name(cdb_cfg);
1380 BT_INFO("Mesh: Attached Network [%s]", *network);
1381 return BLUETOOTH_ERROR_NONE;
1383 /* Free the CDB object */
1384 _bt_mesh_conf_free(cdb_cfg);
1385 return BLUETOOTH_ERROR_INTERNAL;
1388 int _bt_mesh_network_load(const char *app_cred,
1389 const char *sender, const char *token)
1391 int ret = OAL_STATUS_SUCCESS;
1393 char *dir_path = NULL;
1394 _bt_mesh_cdb_t *cdb_cfg = NULL;
1395 oal_mesh_node_t node;
1396 GSList *models = NULL;
1398 /* Check CDB directory exist or not */
1399 dir_path = g_strdup_printf(MESH_CDB_DEFAULT_DIR_PATH"/%s/", "default");
1400 if (!_bt_mesh_util_is_directory_exists(dir_path)) {
1401 BT_ERR("Mesh: CDB dir [%s]does not exist for app", dir_path);
1403 return BLUETOOTH_ERROR_INTERNAL;
1407 l = g_slist_find_custom(cdb_list, token,
1408 __mesh_compare_app_cdb_token);
1410 BT_INFO("Mesh: Already loaded");
1412 return BLUETOOTH_ERROR_NONE;
1414 BT_INFO("Mesh: Not loaded");
1415 /* Attempt to load CDB's present in app's
1417 const char *filename;
1418 GDir *dir = g_dir_open(dir_path, 0, NULL);
1420 BT_ERR("Mesh: Could not open directory");
1422 return BLUETOOTH_ERROR_INTERNAL;
1425 while ((filename = g_dir_read_name(dir)) != NULL) {
1427 if ((g_file_test(filename, G_FILE_TEST_IS_SYMLINK) == TRUE) ||
1428 (g_str_has_suffix(filename, ".json") == FALSE))
1431 BT_INFO("Mesh: File name [%s]", filename);
1432 cdb_cfg = _bt_mesh_conf_load(filename, token);
1439 BT_ERR("Mesh: Could not find CDB for the token!! Possibly not authorized!!");
1440 return BLUETOOTH_ERROR_ACCESS_DENIED;
1443 /* Fill the Mesh node info */
1444 memset(&node, 0x00, sizeof(oal_mesh_node_t));
1445 memcpy(node.uuid.uuid, cdb_cfg->uuid, sizeof(oal_uuid_t));
1446 _bt_mesh_util_convert_string_to_hex(token, strlen(token), node.token.u8, 8);
1447 if (!_bt_mesh_conf_get_element_count(cdb_cfg, &node.num_elements)) {
1448 _bt_mesh_conf_free(cdb_cfg);
1449 return BLUETOOTH_ERROR_INTERNAL;
1452 if (!_bt_mesh_conf_fetch_vendor_specific_info(cdb_cfg, 0x0001 /* Local Node Unicast */,
1453 &node.vendor_info.companyid, &node.vendor_info.vendorid,
1454 &node.vendor_info.versionid, &node.vendor_info.crpl,
1455 &node.vendor_info.relay, &node.vendor_info.frnd,
1456 &node.vendor_info.proxy, &node.vendor_info.lpn)) {
1458 _bt_mesh_conf_free(cdb_cfg);
1459 return BLUETOOTH_ERROR_INTERNAL;
1462 for (int i = 0; i < node.num_elements; i++) {
1464 uint16_t **model_array = _bt_mesh_conf_get_all_model_info(
1465 cdb_cfg, i, &num_models);
1467 _bt_mesh_conf_free(cdb_cfg);
1468 return BLUETOOTH_ERROR_INTERNAL;
1471 for (int j = 0; j < num_models; j++) {
1472 bluetooth_mesh_model_t *mod;
1473 mod = g_malloc0(sizeof(bluetooth_mesh_model_t));
1474 mod->elem_index = i;
1475 mod->model_id = *model_array[j];
1476 models = g_slist_append(models, mod);
1478 /* Free all model(s) */
1479 for (int j = 0; j < num_models; j++)
1480 g_free(model_array[j]);
1483 /* Register Mesh Node */
1484 ret = mesh_register_node((oal_mesh_node_t*)&node,
1488 g_slist_free_full(models, g_free);
1490 if (ret != OAL_STATUS_SUCCESS) {
1491 BT_ERR("Mesh: Load Network Failed ret: %d", ret);
1492 _bt_mesh_conf_free(cdb_cfg);
1493 return BLUETOOTH_ERROR_INTERNAL;
1496 /* Save till Network attached */
1497 cdb_list = g_slist_append(cdb_list, cdb_cfg);
1501 bool _bt_mesh_network_get_label_uuid_from_sub_addr(
1502 uint8_t net_uuid[], uint16_t sub_addr,
1506 _bt_mesh_cdb_t *cdb_cfg = NULL;
1509 l = g_slist_find_custom(cdb_list, net_uuid,
1510 __mesh_compare_app_network_uuid);
1514 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1516 l1 = g_slist_find_custom(cdb_cfg->groups,
1517 GUINT_TO_POINTER(sub_addr), __mesh_compare_addr);
1522 _bt_mesh_group_t *grp = l1->data;
1523 memcpy(label, grp->label_uuid, 16);
1528 int _bt_mesh_network_remove_group(const char *app_cred,
1529 const char *sender, bluetooth_mesh_network_t *net,
1530 bluetooth_mesh_network_group_info_t *req)
1533 _bt_mesh_cdb_t *cdb_cfg = NULL;
1534 _bt_mesh_group_t *grp;
1535 uint8_t net_uuid[16];
1537 _bt_mesh_util_convert_string_to_hex(net->uuid,
1538 strlen(net->uuid), net_uuid, 16);
1540 l = g_slist_find_custom(cdb_list, net_uuid,
1541 __mesh_compare_app_network_uuid);
1543 return BLUETOOTH_ERROR_INVALID_PARAM;
1545 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1546 BT_INFO("Mesh: Total groups stored in network [%d]",
1547 g_slist_length(cdb_cfg->groups));
1549 l1 = g_slist_find_custom(cdb_cfg->groups,
1550 GUINT_TO_POINTER(req->group_addr), __mesh_compare_addr);
1553 BT_ERR("Mesh: To be Removed Group: [0x%2.2x] Not Found!", req->group_addr);
1554 return BLUETOOTH_ERROR_INVALID_PARAM;
1556 grp = (_bt_mesh_group_t*)l1->data;
1558 BT_INFO("Mesh: To be Removed Group: [0x%2.2x]", req->group_addr);
1559 if (!_bt_mesh_conf_delete_group_entry(cdb_cfg, req->group_addr)) {
1560 BT_ERR("Mesh: Failed to remove Group: [0x%2.2x]", req->group_addr);
1561 return BLUETOOTH_ERROR_INTERNAL;
1564 BT_INFO("Mesh: Successfully removed Group: [0x%2.2x]", req->group_addr);
1565 cdb_cfg->groups = g_slist_remove(cdb_cfg->groups, grp);
1566 BT_INFO("Mesh: Total groups after update [%d]",
1567 g_slist_length(cdb_cfg->groups));
1569 return BLUETOOTH_ERROR_NONE;
1572 int _bt_mesh_network_create_group(const char *app_cred,
1573 const char *sender, bluetooth_mesh_network_t *net,
1574 bool is_virtual, uint16_t addr,
1575 bluetooth_mesh_network_group_info_t *req)
1577 GSList *l, *l1, *l2;
1578 _bt_mesh_cdb_t *cdb_cfg = NULL;
1579 uint8_t net_uuid[16];
1581 _bt_mesh_util_convert_string_to_hex(net->uuid,
1582 strlen(net->uuid), net_uuid, 16);
1584 l = g_slist_find_custom(cdb_list, net_uuid,
1585 __mesh_compare_app_network_uuid);
1587 return BLUETOOTH_ERROR_INVALID_PARAM;
1589 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1590 BT_INFO("Mesh: Total Groups present in Network already [%d]",
1591 g_slist_length(cdb_cfg->groups));
1594 uint8_t max_tries = 5;
1595 _bt_mesh_group_t *grp = NULL;
1596 grp = g_malloc0(sizeof(_bt_mesh_group_t));
1597 BT_INFO("Mesh: Network Create Virtual group");
1599 l_getrandom(grp->label_uuid, 16);
1600 _bt_mesh_util_crypto_create_virtual_address(
1601 grp->label_uuid, &grp->grp_addr);
1603 /* For simplicity sake, avoid labels that map to the same hash */
1604 l1 = g_slist_find_custom(cdb_cfg->groups,
1605 GUINT_TO_POINTER(grp->grp_addr), __mesh_compare_addr);
1608 if (!_bt_mesh_conf_insert_group_info(cdb_cfg, grp)) {
1609 BT_ERR("Mesh: unable to save group in Conf DB!!");
1611 return BLUETOOTH_ERROR_INTERNAL;
1613 req->is_virtual = true;
1614 req->group_addr = grp->grp_addr;
1615 _bt_mesh_util_convert_hex_to_string(
1616 (uint8_t *) grp->label_uuid, 16, req->label_uuid,
1617 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1619 cdb_cfg->groups = g_slist_append(cdb_cfg->groups, grp);
1620 BT_INFO("Mesh: Virtual Group[0x%2.2x] inserted in List",
1622 BT_INFO("Mesh: Total groups present in Network after update [%d]",
1623 g_slist_length(cdb_cfg->groups));
1624 return BLUETOOTH_ERROR_NONE;
1632 /* Failed to create a unique hash */
1633 return BLUETOOTH_ERROR_INTERNAL;
1635 BT_INFO("Mesh: Network Create group Addr: [0x%2.2x]", addr);
1637 if (!MESH_IS_GROUP(addr)) {
1638 BT_ERR("Mesh: Group Address [0x%2.2x] is not valid!", addr);
1639 return BLUETOOTH_ERROR_INVALID_PARAM;
1642 l2 = g_slist_find_custom(cdb_cfg->groups,
1643 GUINT_TO_POINTER(addr), __mesh_compare_addr);
1646 _bt_mesh_group_t *grp = l2->data;
1647 req->is_virtual = false;
1648 req->group_addr = grp->grp_addr;
1649 BT_INFO("Mesh: Group already found: addr [0x%2.2x]", grp->grp_addr);
1651 /* Group is not present */
1652 _bt_mesh_group_t *grp = g_malloc0(sizeof(_bt_mesh_group_t));
1653 grp->grp_addr = addr;
1654 if (!_bt_mesh_conf_insert_group_info(cdb_cfg, grp)) {
1655 BT_ERR("Mesh: unable to save group in Conf DB!!");
1657 return BLUETOOTH_ERROR_INTERNAL;
1659 cdb_cfg->groups = g_slist_append(cdb_cfg->groups, grp);
1660 req->is_virtual = false;
1661 req->group_addr = grp->grp_addr;
1662 BT_INFO("Mesh: Group[0x%2.2x] inserted in List",
1665 BT_INFO("Mesh: Total groups present in Network after update [%d]",
1666 g_slist_length(cdb_cfg->groups));
1669 return BLUETOOTH_ERROR_NONE;
1672 int _bt_mesh_network_get_groups(const char *app_cred, const char *sender,
1673 bluetooth_mesh_network_t *network,
1677 _bt_mesh_cdb_t *cdb_cfg = NULL;
1678 uint8_t net_uuid[16];
1680 BT_INFO("Mesh: Get All groups from Network [%s]", network->uuid);
1681 _bt_mesh_util_convert_string_to_hex(network->uuid,
1682 strlen(network->uuid), net_uuid, 16);
1684 l = g_slist_find_custom(cdb_list, net_uuid,
1685 __mesh_compare_app_network_uuid);
1687 return BLUETOOTH_ERROR_INVALID_PARAM;
1689 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1690 BT_INFO("Mesh: Got CDB");
1692 BT_INFO("Mesh: Total groups present in Network [%d]",
1693 g_slist_length(cdb_cfg->groups));
1695 for (l = cdb_cfg->groups; l; l = l->next) {
1696 bluetooth_mesh_network_group_info_t grp;
1697 _bt_mesh_group_t *group = l->data;
1698 memset(&grp, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
1699 g_strlcpy(grp.net_uuid, network->uuid, sizeof(grp.net_uuid));
1700 if (MESH_IS_GROUP(group->grp_addr) && !MESH_IS_VIRTUAL(group->grp_addr)) {
1701 grp.is_virtual = false;
1702 grp.group_addr = group->grp_addr;
1703 BT_INFO("Mesh: Found Non-Virtual group, addr[0x%2.2x]", group->grp_addr);
1704 } else if (MESH_IS_VIRTUAL(group->grp_addr)) {
1705 grp.is_virtual = true;
1706 grp.group_addr = group->grp_addr;
1707 _bt_mesh_util_convert_hex_to_string((uint8_t *) group->label_uuid,
1708 16, grp.label_uuid, sizeof(grp.label_uuid));
1709 BT_INFO("Mesh: Found Virtual group, addr[0x%2.2x]", group->grp_addr);
1710 BT_INFO("Mesh: Label UUID[%s]", grp.label_uuid);
1713 g_array_append_vals(*out_param,
1714 &grp, sizeof(bluetooth_mesh_network_group_info_t));
1717 return BLUETOOTH_ERROR_NONE;
1720 int _bt_mesh_network_get_nodes(const char *app_cred,
1721 const char *sender, bluetooth_mesh_network_t *network,
1725 _bt_mesh_cdb_t *cdb_cfg = NULL;
1728 l = g_slist_find_custom(cdb_list, network->token.token,
1729 __mesh_compare_app_cdb_token);
1731 return BLUETOOTH_ERROR_INVALID_PARAM;
1733 cdb_cfg = (_bt_mesh_cdb_t*)l->data;
1735 if (_bt_mesh_node_get_all(cdb_cfg->uuid, out_param))
1736 return BLUETOOTH_ERROR_NONE;
1738 return BLUETOOTH_ERROR_INTERNAL;