Mesh: Apply Fixes & code refactoring 75/238775/3 ble_mesh
authorAnupam Roy <anupam.r@samsung.com>
Fri, 17 Jul 2020 07:42:56 +0000 (13:12 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Fri, 17 Jul 2020 19:58:29 +0000 (01:28 +0530)
This patch handles initial set of fixes
across all layers in BLE Mesh FRWK & some
code refactoring

Some of the notable fixes applied are following:-
1. Fix bt-service crash during cleanup of mesh framework resources
   [Resolution: removing double free of Mesh model resources]
2. Fix crash in unit test app during Scan stop operation
   [Resolution: Dont allow Stop scan if scanning is not in progress]
3. Fix Node Browse event fail issue
   [Resolution: Handle wrong usage of event pointer]
4. Fix "Timer expired even if response is received within timeout" issue.
   [Resolution: Remove timer once event is received from stack]
5. Fix "TTL configured callback(CAPI callback)" is not invoked in case of success
   Resolution: Handle wrong usage of event pointer in service layer]
6. Fix NetKey configuration API failed due to mishandling of Key in Network
   [Resolution: Network Key handling is corrected]

Change-Id: I107a1d26df134d54a7347e4cae71a38e106b6a12
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
16 files changed:
bt-api/bt-event-handler.c
bt-api/bt-mesh.c
bt-oal/bluez_hal/src/bt-hal-mesh-dbus-handler.c
bt-oal/bluez_hal/src/bt-hal-mesh.c
bt-oal/common/oal-event-dispatcher.c
bt-service-adaptation/services/bt-request-handler.c
bt-service-adaptation/services/bt-service-event-receiver.c
bt-service-adaptation/services/bt-service-main.c
bt-service-adaptation/services/include/bt-service-mesh-nodes.h
bt-service-adaptation/services/mesh/bt-service-mesh-config-client.c
bt-service-adaptation/services/mesh/bt-service-mesh-keys.c
bt-service-adaptation/services/mesh/bt-service-mesh-main.c
bt-service-adaptation/services/mesh/bt-service-mesh-network.c
bt-service-adaptation/services/mesh/bt-service-mesh-nodes.c
bt-service-adaptation/services/mesh/bt-service-mesh-util.c
include/bluetooth-mesh-api.h

index dab9e03..1d472b8 100644 (file)
@@ -456,6 +456,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                GVariant *param = NULL;
                GArray *dbus_data = NULL;
                bluetooth_mesh_scan_result_t *info = NULL;
+               BT_INFO("Mesh: BT_MESH_SCAN_RESULT");
 
                g_variant_get(parameters, "(iv)", &result, &param);
                dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar));
@@ -473,6 +474,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                GVariant *param = NULL;
                GArray *dbus_data = NULL;
                bluetooth_mesh_authentication_request_t *info = NULL;
+               BT_INFO("Mesh: BT_MESH_AUTHENTICATION_REQ");
 
                g_variant_get(parameters, "(iv)", &result, &param);
                dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar));
@@ -481,6 +483,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                g_variant_unref(param);
 
                info = &g_array_index(dbus_data, bluetooth_mesh_authentication_request_t, 0);
+               BT_INFO("Mesh: Authentication Type [%d]", info->auth_type);
 
                _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_AUTHENTICATION_REQUEST,
                                result, info,
@@ -490,6 +493,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                GVariant *param = NULL;
                GArray *dbus_data = NULL;
                bluetooth_mesh_provisioning_result_t *info = NULL;
+               BT_INFO("Mesh: BT_MESH_PROVISIONING_RESULT");
 
                g_variant_get(parameters, "(iv)", &result, &param);
                dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar));
@@ -507,7 +511,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                GVariant *param = NULL;
                GArray *dbus_data = NULL;
                bluetooth_mesh_node_discover_t *info = NULL;
-
+               BT_INFO("Mesh: Node browsed event");
                g_variant_get(parameters, "(iv)", &result, &param);
                dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar));
 
@@ -516,6 +520,8 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
 
                info = &g_array_index(dbus_data, bluetooth_mesh_node_discover_t, 0);
 
+               BT_INFO("Mesh: Send Node Browse event: Net UID [%s], Node UUID [%s]",
+                       info->net_uuid, info->dev_uuid);
                _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_BROWSED,
                                result, info,
                                event_info->cb, event_info->user_data);
index 5704417..69c1c0b 100644 (file)
@@ -774,7 +774,6 @@ BT_EXPORT_API int bluetooth_mesh_netkey_get_all_appkey(
 
                        if (size == 0) {
                                BT_ERR("Mesh: No Appkeys created for the NetKey in the network");
-                               result = BLUETOOTH_ERROR_NOT_FOUND;
                        }
 
                        for (i = 0; i < size; i++) {
index db099aa..f27af9b 100644 (file)
@@ -56,7 +56,7 @@ static const char *dbus_err_fail = "org.freedesktop.DBus.Error.Failed";
 static struct l_dbus *dbus = NULL;
 static struct l_dbus_client *client = NULL;
 
-static struct l_dbus_proxy *net_proxy;
+static struct l_dbus_proxy *net_proxy = NULL;
 static struct l_dbus_message *agent_msg;
 
 static handle_stack_msg mesh_event_cb = NULL;
@@ -203,7 +203,7 @@ struct meshcfg_app {
 typedef struct meshcfg_app meshcfg_app;
 
 /* Will contain critical data related to local Mesh Network */
-GSList *mesh_apps;
+static GSList *mesh_apps = NULL;
 
 
 struct meshcfg_node {
@@ -267,7 +267,7 @@ static unsigned char* __mesh_get_net_uuid_from_path(
                return l_util_from_hexstring(uuid, &sz);
        }
        case MESH_AGENT_IFACE: {
-               memcpy(uuid, is_prov ? (void*) &dbus_path[22] : (void*) &dbus_path[23], 32);
+               memcpy(uuid, is_prov ? (void*) &dbus_path[16] : (void*) &dbus_path[23], 32);
                uuid[32] = '\0';
                return l_util_from_hexstring(uuid, &sz);
        }
@@ -284,12 +284,44 @@ static unsigned char* __mesh_get_net_uuid_from_path(
 static void __mesh_hal_free_elements(gpointer data)
 {
        meshcfg_el *element = (meshcfg_el*) data;
+       if (!element)
+               return;
        g_free(element->path);
        if (element->models)
                g_slist_free_full(element->models, g_free);
+
+       element->models = NULL;
        g_free(element);
 }
 
+static bool __bt_mesh_proxy_check(meshcfg_app *app)
+{
+       /* Check meshd stack Vis up or not */
+       if (!dbus) {
+               ERR("Mesh: DBUS is not UP!, possibly stack not Up!");
+               return false;
+       }
+       /* Check Network Proxy is added or not */
+       if (!net_proxy) {
+               ERR("Mesh: Network proxy is not attached yet!");
+               return false;
+       }
+
+       if (app) {
+               /* Check App management proxyis added or not */
+               if (!app->mgmt_proxy) {
+                       ERR("Mesh: Network proxy is not attached yet!");
+                       return false;
+               }
+               /* Check App  Node Proxy is added or not */
+               if (!app->proxy) {
+                       ERR("Mesh: Node proxy is not attached yet!");
+                       return false;
+               }
+       }
+       return true;
+}
+
 static void __bt_hal_mesh_destroy_app_object(gpointer data)
 {
 
@@ -298,27 +330,52 @@ static void __bt_hal_mesh_destroy_app_object(gpointer data)
                return;
 
        mesh_apps = g_slist_remove(mesh_apps, app);
-       g_free(app->path);
-       g_free(app->agent_path);
 
-       if (app->elements)
+       if (app->path) {
+               INFO("Mesh: App path [%s] ", app->path);
+               g_free(app->path);
+       }
+       if (app->agent_path) {
+               INFO("Mesh: Agent Path [%s]", app->agent_path);
+               g_free(app->agent_path);
+       }
+
+       if (app->elements) {
+               INFO("Mesh: Total elements present in app [%d]",
+                               g_slist_length(app->elements));
                g_slist_free_full(app->elements, __mesh_hal_free_elements);
+       }
        g_free(app);
 }
 
-static void __mesh_client_connected(struct l_dbus *dbus, void *user_data)
+static void __mesh_client_connected(struct l_dbus *dbus_obj, void *user_data)
 {
        ERR("MESH: D-Bus client connected\n");
+       dbus = dbus_obj;
+       if (dbus)
+               INFO("Mesh: MeshD connected: dbus [%p]", dbus);
 }
 
-static void __mesh_client_disconnected(struct l_dbus *dbus, void *user_data)
+static void __mesh_client_disconnected(struct l_dbus *dbus_obj, void *user_data)
 {
        ERR("MESH: D-Bus client disconnected, possibly meshd exited \n");
        /* TODO: Send event to app about meshd termination  & then remove all mesh apps */
+       INFO("Mesh: Total number of networks present [%d]",
+                       g_slist_length(mesh_apps));
+
        if (mesh_apps) {
                g_slist_free_full(mesh_apps, __bt_hal_mesh_destroy_app_object);
                mesh_apps = NULL;
        }
+       /* Set DBUS to NULL */
+       if (dbus)
+               INFO("Mesh: dbus [%p]", dbus);
+       if (net_proxy)
+               INFO("Mesh: net proxy [%p]", net_proxy);
+
+       dbus = NULL;
+       net_proxy = NULL;
+       INFO("Mesh: All apps cleaned up after meshd exited");
 }
 
 static gint __compare_proxy_path(gconstpointer data, gconstpointer user_data)
@@ -363,15 +420,18 @@ static void __mesh_proxy_added(struct l_dbus_proxy *proxy, void *user_data)
        INFO("MESH: Proxy added: %s (%s)\n", interface, path);
 
        if (!strcmp(interface, BT_HAL_MESH_NETWORK_INTERFACE)) {
-
+               INFO("Mesh: Network Proxy added");
                /* Save Global proxy */
                net_proxy = proxy;
+               if (net_proxy)
+                       INFO("Mesh: Net Proxy [%p]", net_proxy);
                return;
        }
 
        if (!strcmp(interface, BT_HAL_MESH_MANAGEMENT_INTERFACE)) {
                GSList *l;
                meshcfg_app *app;
+               INFO("Mesh: Mgmt Proxy added");
                INFO("Mesh: Number of mesh app present in list [%d]",
                        g_slist_length(mesh_apps));
                l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
@@ -385,13 +445,13 @@ static void __mesh_proxy_added(struct l_dbus_proxy *proxy, void *user_data)
        }
 
        if (!strcmp(interface, BT_HAL_MESH_NODE_INTERFACE)) {
-
+               INFO("Mesh: Node Proxy added");
                GSList *l;
                meshcfg_app *app;
                l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
                if (l) {
                        app = l->data;
-                       app->mgmt_proxy = proxy;
+                       app->proxy = proxy;
                } else {
                        ERR("Mesh: app not found for Node proxy");
                }
@@ -444,11 +504,11 @@ static void __mesh_proxy_removed(struct l_dbus_proxy *proxy, void *user_data)
        }
 }
 
-static void __mesh_dbus_client_ready(struct l_dbus_client *client,
+static void __mesh_dbus_client_ready(struct l_dbus_client *client_obj,
                void *user_data)
 {
        INFO("Mesh: D-Bus client ready: bluetooth-meshd connected \n");
-       /* TODO: Book keeping */
+       client = client_obj;
 }
 
 static void __mesh_ready_callback(void *user_data)
@@ -466,6 +526,8 @@ bool _bt_hal_mesh_stack_init(void)
        if (!dbus)
                return false;
 
+       INFO("Mesh: Got dbus [%p]", dbus);
+
        if (!l_dbus_set_ready_handler(dbus, __mesh_ready_callback, NULL, NULL))
                return false;
 
@@ -578,6 +640,7 @@ static void __bt_hal_mesh_attach_node_reply(struct l_dbus_proxy *proxy,
                struct l_dbus_message *msg, void *user_data)
 {
        struct l_dbus_message_iter iter_cfg;
+       char *path;
        meshcfg_app *app = (meshcfg_app*) user_data;
        INFO("Mesh: Attach Node Reply: App path [%s] Agent Path [%s]",
                app->path, app->agent_path);
@@ -591,7 +654,7 @@ static void __bt_hal_mesh_attach_node_reply(struct l_dbus_proxy *proxy,
        }
 
        if (!l_dbus_message_get_arguments(msg, "oa(ya(qa{sv}))",
-                               &app->path, &iter_cfg))
+                               &path, &iter_cfg))
                goto failed;
 
        INFO("Mesh: Attached with path %s\n", app->path);
@@ -749,9 +812,12 @@ static struct l_dbus_message *__mesh_device_message_received(struct l_dbus *dbus
        ev->is_remote_devkey = rmt;
        ev->netkey_idx = idx;
        ev->data_len = n;
-       memcpy(ev->data, buf, n);
+       memcpy(ev->data, data, n);
        size += n;
 
+       INFO("Mesh: Src [0x%2.2x]", src);
+       INFO("Mesh: Is Remote [%s]", rmt ? "YES" : "NO");
+       INFO("Mesh: NetKey Idx [0x%2.2x]", idx);
        /* Send DevKeyMessage Received event */
        if (mesh_event_cb) {
                mesh_event_cb(HAL_EV_MESH_DEVKEY_MESSAGE_EVENT, (void*)buf, size);
@@ -863,12 +929,19 @@ static struct l_dbus_message *__mesh_request_provisioner_call(
        uint8_t cnt;
        struct hal_ev_mesh_provision_finished ev;
        struct hal_ev_mesh_provision_data_request req;
+       char *uuid_string;
        meshcfg_app *app = user_data;
 
+       INFO("Mesh: provisioning data requested app path [%s]",
+               app->path);
+       uuid_string = l_util_hexstring(app->uuid, 16);
+       INFO("Mesh: Network UUID [%s]", uuid_string);
+
        memset(&ev, 0, sizeof(ev));
        memset(&req, 0, sizeof(req));
        memcpy(ev.net_uuid, app->uuid, 16);
        memcpy(req.net_uuid, app->uuid, 16);
+       l_free(uuid_string);
 
        if (!l_dbus_message_get_arguments(msg, "y", &cnt)) {
                ERR("Mesh: Cannot parse request for prov data");
@@ -885,7 +958,7 @@ static struct l_dbus_message *__mesh_request_provisioner_call(
        req.count = cnt;
        if (mesh_event_cb)
                mesh_event_cb(HAL_EV_MESH_PROVISIONING_DATA_REQUEST,
-                       (void*)&ev, sizeof(ev));
+                       (void*)&req, sizeof(req));
 
        l_dbus_message_ref(msg);
        return NULL;
@@ -1360,7 +1433,7 @@ bool __bt_hal_mesh_register_agent(meshcfg_app *ptr)
        if (!l_dbus_register_interface(dbus, BT_HAL_MESH_PROVISION_AGENT_INTERFACE,
                                __bt_hal_mesh_setup_agent_iface, NULL, false)) {
                ERR("Mesh: Unable to register agent interface");
-               return false;
+               //return false;
        }
 
        INFO("Mesh: Register Agent path [%s]",  ptr->agent_path);
@@ -1385,19 +1458,21 @@ bool __bt_hal_mesh_register_application(meshcfg_app *ptr)
 
        if (!ptr)
                return false;
+       if (!dbus)
+               return false;
 
        if (!l_dbus_register_interface(dbus, BT_HAL_MESH_APPLICATION_INTERFACE,
                                __bt_hal_mesh_setup_app_iface, NULL, false)) {
                ERR("Mesh: Failed to register interface %s",
                                BT_HAL_MESH_APPLICATION_INTERFACE);
-               return false;
+               //return false;
        }
 
        if (!l_dbus_register_interface(dbus, BT_HAL_MESH_PROVISIONER_INTERFACE,
                                __bt_hal_mesh_setup_prov_iface, NULL, false)) {
                ERR("Mesh: Failed to register interface %s",
                                BT_HAL_MESH_PROVISIONER_INTERFACE);
-               return false;
+               //return false;
        }
 
        if (!l_dbus_register_object(dbus, ptr->path, NULL, NULL,
@@ -1415,21 +1490,21 @@ bool __bt_hal_mesh_register_application(meshcfg_app *ptr)
                                __bt_hal_mesh_setup_ele_iface, NULL, false)) {
                ERR("Mesh: Failed to register interface %s",
                                BT_HAL_MESH_ELEMENT_INTERFACE);
-               return false;
+               //return false;
        }
 
        INFO("Mesh: Number of elements to be registsred [%d]",
-               g_slist_length(ptr->elements));
+                       g_slist_length(ptr->elements));
 
        g_slist_foreach(ptr->elements, __bt_hal_mesh_register_element_obj, ptr);
 
        INFO("Mesh: Add Object manager Interface: app path [%s]", ptr->path);
        if (!l_dbus_object_add_interface(dbus, ptr->path,
                                L_DBUS_INTERFACE_OBJECT_MANAGER, NULL)) {
-                       ERR("Mesh: Failed to add interface %s",
-                                       L_DBUS_INTERFACE_OBJECT_MANAGER);
-                       return false;
-               }
+               ERR("Mesh: Failed to add interface %s",
+                               L_DBUS_INTERFACE_OBJECT_MANAGER);
+               return false;
+       }
        INFO("Mesh: Application Register completed");
 
        return true;
@@ -1454,9 +1529,11 @@ static void __bt_mesh_hal_create_element_object(gpointer data, gpointer user_dat
                INFO("Mesh: Created element index [%d] path [%s]",
                        elem->index, elem->path);
        }
+       INFO("Mesh: This is model [0x%4.4x] for Element [0x%2.2x]",
+               model_info->model, elem->index);
        /* Add Model in the element */
        elem->models = g_slist_append(elem->models, model_info);
-       INFO("Mesh: total models of the element with index [%d] is [%d]",
+       INFO("Mesh: Model added in element: total Model count in elem [%d] is [%d]",
                        elem->index, g_slist_length(elem->models));
 }
 
@@ -1479,11 +1556,11 @@ meshcfg_app *__bt_hal_mesh_create_app(bt_hal_mesh_node_t *node,
 
        if (is_prov) {
                app->path = g_strdup_printf("/tizen/mesh/cfg/%s", uuid_str);
-               app->agent_path = g_strdup_printf("/tizen/mesh/cfg/agent/%s", uuid_str);
+               app->agent_path = g_strdup_printf("%s/agent", app->path);
 
        } else {
                app->path = g_strdup_printf("/tizen/mesh/node/%s", uuid_str);
-               app->agent_path = g_strdup_printf("/tizen/mesh/node/agent/%s", uuid_str);
+               app->agent_path = g_strdup_printf("%s/agent", app->path);
        }
        g_slist_foreach(models, __bt_mesh_hal_create_element_object, app);
 
@@ -1695,13 +1772,17 @@ static void __bt_hal_mesh_add_node_reply(
        if (mesh_event_cb)
                mesh_event_cb(HAL_EV_MESH_PROVISIONING_STATUS,
                        (void*)&ev, sizeof(ev));
+       INFO("Mesh: Provisioning status sent");
 }
 
 static void __bt_hal_mesh_add_node_setup(struct l_dbus_message *msg,
                void *user_data)
 {
+       char *uuid;
        bt_uuid_t *dev = user_data;
        struct l_dbus_message_builder *builder;
+       uuid =  l_util_hexstring(dev->uu, 16);
+       INFO("Mesh: Add Node Setup UUID [%s]", uuid);
 
        builder = l_dbus_message_builder_new(msg);
        __mesh_append_byte_array(builder, dev->uu, 16);
@@ -1709,8 +1790,6 @@ static void __bt_hal_mesh_add_node_setup(struct l_dbus_message *msg,
        l_dbus_message_builder_leave_array(builder);
        l_dbus_message_builder_finalize(builder);
        l_dbus_message_builder_destroy(builder);
-
-       l_free(dev);
 }
 
 bt_status_t _bt_hal_mesh_provision_device(
@@ -1723,7 +1802,7 @@ bt_status_t _bt_hal_mesh_provision_device(
        if (l) {
                app = l->data;
                dev = g_memdup((gpointer)dev_uuid, 16);
-
+               INFO("Mesh: Schedule Add Node request to meshd");
                if (!l_dbus_proxy_method_call(app->mgmt_proxy, "AddNode",
                                        __bt_hal_mesh_add_node_setup,
                                        __bt_hal_mesh_add_node_reply,
@@ -1776,10 +1855,13 @@ static void __bt_hal_mesh_subnet_key_reply(struct l_dbus_proxy *proxy,
        ev.status = BT_STATUS_SUCCESS;
 
        if (!strcmp("CreateSubnet", method)) {
+               INFO("Mesh: Reply for CreateSubnet");
                ev.key_event = HAL_MESH_KEY_ADD;
        } else if (!strcmp("DeleteSubnet", method)) {
+               INFO("Mesh: Reply for DeleteSubnet");
                ev.key_event = HAL_MESH_KEY_DELETE;
        } else if (!strcmp("UpdateSubnet", method)) {
+               INFO("Mesh: Reply for UpdateSubnet");
                ev.key_event = HAL_MESH_KEY_UPDATE;
        }
 
@@ -1813,7 +1895,10 @@ static void __bt_hal_mesh_app_key_setup(struct l_dbus_message *msg,
        uint16_t net_idx = (uint16_t) req->net_idx;
        uint16_t app_idx = (uint16_t) req->app_idx;
 
-        l_dbus_message_set_arguments(msg, "qq", net_idx, app_idx);
+       if (g_strcmp0(req->str,"CreateAppKey") == 0)
+               l_dbus_message_set_arguments(msg, "qq", net_idx, app_idx);
+       else
+               l_dbus_message_set_arguments(msg, "q", app_idx);
 }
 
 static void __bt_hal_mesh_app_key_reply(struct l_dbus_proxy *proxy,
@@ -1842,15 +1927,17 @@ static void __bt_hal_mesh_app_key_reply(struct l_dbus_proxy *proxy,
                l_dbus_message_get_error(msg, &name, NULL);
                ERR("Mesh: AppKey execute [%s] failed: error: [%s]", method, name);
                ev.status = BT_STATUS_FAIL;
-       }
-
-       ev.status = BT_STATUS_SUCCESS;
+       } else
+               ev.status = BT_STATUS_SUCCESS;
 
        if (!strcmp("CreateAppKey", method)) {
+               INFO("Mesh: AppKey Create Reply");
                ev.key_event = HAL_MESH_KEY_ADD;
        } else if (!strcmp("DeleteAppKey", method)) {
+               INFO("Mesh: AppKey Delete Reply");
                ev.key_event = HAL_MESH_KEY_DELETE;
        } else if (!strcmp("UpdateAppKey", method)) {
+               INFO("Mesh: AppKey Update Reply");
                ev.key_event = HAL_MESH_KEY_UPDATE;
        }
 
@@ -1924,9 +2011,12 @@ bt_status_t _bt_hal_mesh_network_appkey_execute(bt_uuid_t *net_uuid,
                else if (op == BT_MESH_KEY_DELETE)
                        status = __mesh_subnet_appkey_command_execute(app,
                                netkey_idx, appkey_idx, "DeleteAppKey");
-               else if (op == BT_MESH_KEY_UPDATE)
+               else if (op == BT_MESH_KEY_UPDATE) {
+                       INFO("Mesh: Update ApKey command NK Idx [0x%2.2x] AK Idx [0x%2.2x]",
+                                       netkey_idx, appkey_idx);
                        status = __mesh_subnet_appkey_command_execute(app,
                                netkey_idx, appkey_idx, "UpdateAppKey");
+               }
                if (!status)
                        return BT_STATUS_FAIL;
 
@@ -1969,6 +2059,10 @@ bt_status_t _bt_hal_mesh_network_scan_cancel(bt_uuid_t *net_uuid)
                net_uuid->uu, __mesh_compare_network_uuid);
        if (l) {
                app = l->data;
+               if (!__bt_mesh_proxy_check(app)) {
+                       ERR("Mesh: Proxy check failed!!");
+                       return BT_STATUS_FAIL;
+               }
                if (!l_dbus_proxy_method_call(app->mgmt_proxy,
                                "UnprovisionedScanCancel",
                                        NULL, NULL, NULL, NULL))
@@ -1996,27 +2090,46 @@ bt_status_t _bt_hal_mesh_auth_reply(bt_hal_mesh_auth_variant_e auth_type,
        uint32_t val_u32;
        struct l_dbus_message *reply = NULL;
        struct l_dbus_message_builder *builder;
-       uint8_t alpha[16];
+       uint8_t *alpha;
+       bt_status_t ret = BT_STATUS_SUCCESS;
+       size_t sz = 0;
 
+       /* Proxy Check */
+       if (!__bt_mesh_proxy_check(0)) {
+               ERR("Mesh: Proxy check failed!!");
+               return BT_STATUS_FAIL;
+       }
+       INFO("Mesh: Authentication Reply: auth type [%d]", auth_type);
+       INFO("Mesh: Authentication Reply: auth value [%s]", auth_value);
        /* For Numeric Type Inputs: Numeric, Blink, Beep & Vibrate */
        if (auth_type >= BT_HAL_MESH_AUTH_REQ_NUMERIC_INPUT &&
                        auth_type <= BT_HAL_MESH_AUTH_REQ_VIBRATE_COUNT_INPUT) {
-               INFO("Mesh: Authentication reply: Numeric ype");
+               INFO("Mesh: Authentication reply: Numeric Type");
                val_u32 = atoi(auth_value);
                reply = l_dbus_message_new_method_return(agent_msg);
                l_dbus_message_set_arguments(reply, "u", val_u32);
-               /* For Alpha-Numeric */
-       } else if (auth_type == BT_HAL_MESH_AUTH_REQ_ALPHANUMERIC_INPUT) {
-               INFO("Mesh: Authentication reply: Alpha-Numeric ype");
-               memset(alpha, 0x00, 16);
-               memcpy(alpha, auth_value, strlen(auth_value));
+               if (!reply)
+                       reply = l_dbus_message_new_error(agent_msg, dbus_err_fail, NULL);
+               l_dbus_send(dbus, reply);
+               ret = BT_STATUS_SUCCESS;
+       /* For Alpha-Numeric */
+       } else if (auth_type == BT_HAL_MESH_AUTH_REQ_ALPHANUMERIC_INPUT ||
+                       auth_type == BT_HAL_MESH_AUTH_REQ_OOB_STATIC_KEY_INPUT ||
+                       auth_type == BT_HAL_MESH_AUTH_REQ_OOB_PUBLIC_KEY_INPUT ) {
+               INFO("Mesh: Authentication reply: Alpha-Numeric Type");
+               alpha = l_util_from_hexstring(auth_value, &sz);
                reply = l_dbus_message_new_method_return(agent_msg);
                builder = l_dbus_message_builder_new(reply);
                __mesh_append_byte_array(builder, alpha, 16);
                l_dbus_message_builder_finalize(builder);
                l_dbus_message_builder_destroy(builder);
+               l_free(alpha);
+               if (!reply)
+                       reply = l_dbus_message_new_error(agent_msg, dbus_err_fail, NULL);
+               l_dbus_send(dbus, reply);
+               ret = BT_STATUS_SUCCESS;
        }
-       return BT_STATUS_SUCCESS;
+       return ret;
 }
 
 bt_status_t _bt_hal_mesh_network_scan(bt_uuid_t *net_uuid,
@@ -2027,6 +2140,10 @@ bt_status_t _bt_hal_mesh_network_scan(bt_uuid_t *net_uuid,
        l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
        if (l) {
                app = l->data;
+               if (!__bt_mesh_proxy_check(app)) {
+                       ERR("Mesh: Proxy check failed!!");
+                       return BT_STATUS_FAIL;
+               }
                if (!l_dbus_proxy_method_call(app->mgmt_proxy, "UnprovisionedScan",
                                        __mesh_scan_setup, __mesh_scan_reply,
                                        L_UINT_TO_PTR(param->scan_time), NULL))
@@ -2044,6 +2161,19 @@ bt_status_t _bt_hal_mesh_create_network(
        meshcfg_app *app;
 
        INFO("Mesh: Create Network Request");
+
+       if (!__bt_mesh_proxy_check(0)) {
+               ERR("Mesh: Proxy check failed!!");
+               return BT_STATUS_FAIL;
+       }
+
+       INFO("Mesh: Node Element count [%d]", node->num_elements);
+       INFO("Mesh: Node Primary Unicast[0x%2.2x]", node->primary_unicast);
+       INFO("Mesh: Node Vendor Info: CID[0x%2.2x]", node->vendor_info.companyid);
+       INFO("Mesh: Node Vendor Info: VID[0x%2.2x]", node->vendor_info.vendorid);
+       INFO("Mesh: Node Vendor Info: VSID[0x%2.2x]", node->vendor_info.versionid);
+       INFO("Mesh: Node Vendor Info: CRPL[0x%2.2x]", node->vendor_info.crpl);
+       INFO("Mesh: Node Total Number of Models in the node[%d]", g_slist_length(models));
        /* Create DBUS APP */
        app = __bt_hal_mesh_create_app(node, models, is_prov);
        if (!app)
@@ -2140,6 +2270,10 @@ bt_status_t _bt_hal_mesh_send_key_config_message(
        l = g_slist_find_custom(mesh_apps, network->uu, __mesh_compare_network_uuid);
        if (l) {
                app = l->data;
+               if (!__bt_mesh_proxy_check(app)) {
+                       ERR("Mesh: Proxy check failed!!");
+                       return BT_STATUS_FAIL;
+               }
                l1 = g_slist_find_custom(app->elements,
                        GUINT_TO_POINTER(src_elem_idx), __compare_element_index);
                elem = l1->data;
@@ -2151,7 +2285,7 @@ bt_status_t _bt_hal_mesh_send_key_config_message(
                req->idx = netkey_idx; /* Encryption Key index */
                req->update_req = is_update;
 
-               if (!l_dbus_proxy_method_call(app->mgmt_proxy, key_method,
+               if (!l_dbus_proxy_method_call(app->proxy, key_method,
                                        __bt_hal_mesh_key_config_send, NULL,
                                        (void*)req, l_free))
                        return BT_STATUS_FAIL;
@@ -2178,6 +2312,10 @@ bt_status_t _bt_hal_mesh_send_configuration_message(
                        __mesh_compare_network_uuid);
        if (l) {
                app = l->data;
+               if (!__bt_mesh_proxy_check(app)) {
+                       ERR("Mesh: Proxy check failed!!");
+                       return BT_STATUS_FAIL;
+               }
                l1 = g_slist_find_custom(app->elements,
                                GUINT_TO_POINTER(src_elem_idx),
                                __compare_element_index);
@@ -2192,7 +2330,7 @@ bt_status_t _bt_hal_mesh_send_configuration_message(
                req->rmt = true;
                req->is_dev_key = is_dev_key;
 
-               if (!l_dbus_proxy_method_call(app->mgmt_proxy, "DevKeySend",
+               if (!l_dbus_proxy_method_call(app->proxy, "DevKeySend",
                                        __bt_hal_mesh_config_send, NULL,
                                        (void*)req, l_free))
                        return BT_STATUS_FAIL;
index 8711b15..3c1bd13 100644 (file)
@@ -81,10 +81,12 @@ static void __bt_hal_handle_network_provisioning_status(void *buf, uint16_t len)
 {
        struct hal_ev_mesh_provision_status *ev = buf;
 
+       INFO("Mesh: Send provstatus callback");
        if (bt_hal_mesh_cbacks->provisioning_status_cb)
                bt_hal_mesh_cbacks->provisioning_status_cb(ev->status,
                                (bt_uuid_t*)&ev->net_uuid,
                                        (bt_uuid_t*)&ev->dev_uuid);
+       INFO("Mesh: Done Sending  provstatus callback");
 }
 
 static void __bt_hal_handle_network_provisioning_finished(void *buf, uint16_t len)
@@ -168,7 +170,7 @@ static void __bt_hal_handle_mesh_events(int message, void *buf, uint16_t len)
                __bt_hal_handle_network_scan_result(buf, len);
                break;
        case HAL_EV_MESH_PROVISIONING_STATUS:
-               DBG("Mesh Event: HAL_EV_MESH_SCAN_RESULT");
+               DBG("Mesh Event: HAL_EV_MESH_PROVISIONING_STATUS");
                __bt_hal_handle_network_provisioning_status(buf, len);
                break;
        case HAL_EV_MESH_PROVISIONING_FINISHED:
index 3eb9989..33a240e 100644 (file)
@@ -107,7 +107,7 @@ static gboolean need_same_context(oal_event_t event)
 
        switch (event) {
        default:
-               ret = FALSE;
+               ret = TRUE;
                break;
        }
        return ret;
index 076c27a..0fcb091 100644 (file)
@@ -3434,6 +3434,7 @@ normal:
                                        result, sender,
                                        function_name, (gpointer)network);
                } else {
+                       g_array_append_vals(*out_param1, &node, sizeof(bluetooth_mesh_node_t));
                        BT_ERR("Mesh: Mesh Network creation schedule failed");
                        g_slist_free_full(model_list, g_free);
                }
@@ -3531,6 +3532,9 @@ normal:
                __bt_service_get_parameters(in_param1,
                                &req, sizeof(bluetooth_mesh_provisioning_request_t));
 
+               BT_INFO("Mesh: Provision Device UUID [%s]", req.dev_uuid);
+               BT_INFO("Mesh: Provision Device in network[%s]", req.net_uuid);
+
                result = _bt_mesh_network_provision_device(
                                requester_unique_creds, sender, &req);
 
@@ -3549,6 +3553,7 @@ normal:
                break;
        }
        case BT_MESH_AUTHENTICATION_REPLY: {
+               BT_INFO("Mesh: BT_MESH_AUTHENTICATION_REPLY");
                const char *auth_value = NULL;
                gboolean authentication_reply = FALSE;
                int auth_type;
@@ -3557,6 +3562,9 @@ normal:
                                &authentication_reply, sizeof(gboolean));
                __bt_service_get_parameters(in_param1,
                                &auth_type, sizeof(int));
+               BT_INFO("Mesh: Auth Reply Type [%d]", auth_type);
+               BT_INFO("Mesh: Auth Value [%s]", auth_value);
+               BT_INFO("Mesh: Auth Accept [%d]", authentication_reply);
                result = _bt_mesh_authentication_reply(auth_type,
                                auth_value, authentication_reply);
                break;
index d152e57..81ba33a 100644 (file)
@@ -420,6 +420,7 @@ static gboolean __bt_handle_oal_events(gpointer data)
        case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT:
        case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT:
        case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED:
+               BT_INFO("Mesh: Event");
                if (mesh_cb)
                        mesh_cb(event_type, event_data);
                break;
index d3bf5f7..7607a3b 100644 (file)
@@ -57,7 +57,6 @@ static gboolean event_prepare(GSource *source, gint *timeout)
 {
         int r = l_main_prepare();
         *timeout = r;
-
         return FALSE;
 }
 
index 32b2973..fea1a47 100644 (file)
@@ -52,12 +52,18 @@ uint16_t _bt_mesh_node_get_next_unicast(uint8_t net_uuid[],
                uint16_t low, uint16_t high,
                        uint8_t ele_cnt);
 
-bool _bt_mesh_node_is_netkey_added(uint8_t net_uuid[],
+bool _bt_mesh_node_is_netkey_added_in_network(uint8_t net_uuid[],
                uint16_t net_idx);
 
-bool _bt_mesh_node_is_appkey_added(uint8_t net_uuid[],
+bool _bt_mesh_node_is_appkey_added_in_network(uint8_t net_uuid[],
        uint16_t app_idx);
 
+bool _bt_mesh_node_is_netkey_exists(uint8_t net_uuid[],
+               uint16_t addr, uint16_t net_idx);
+
+bool _bt_mesh_node_is_appkey_exists(uint8_t net_uuid[],
+               uint16_t addr, uint16_t app_idx);
+
 bool _bt_mesh_node_add_net_key(uint8_t net_uuid[],
                uint16_t addr, uint16_t net_idx);
 
index 370ca14..94b270a 100644 (file)
@@ -269,6 +269,8 @@ static void __bt_mesh_wait_response_timeout(
        default:
                break;
        }
+       BT_INFO("Mesh: Number of pending requests [%u] Remove the req",
+               l_queue_length(pending_requests));
        l_queue_remove(pending_requests, req);
        __mesh_request_remove(req);
 }
@@ -278,11 +280,17 @@ static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
 {
        struct mesh_pending_request *req;
        const struct mesh_config_cmd *cmd;
+       char uuid_str[33];
 
        cmd = __mesh_get_command(opcode);
        if (!cmd)
                return;
+       _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
+               BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+       BT_INFO("Mesh: Net UUID[%s]",uuid_str);
 
+       BT_INFO("Mesh: Adding command opcode [0x%2.2x] response [0x%2.2x]",
+                       cmd->opcode, cmd->response);
        req = l_new(struct mesh_pending_request, 1);
        req->cmd = cmd;
        req->addr = dest;
@@ -290,7 +298,11 @@ static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
        memcpy(req->net_uuid, net_uuid, 16);
        req->timer = l_timeout_create(MESH_DEFAULT_RESPONSE_TIMEOUT,
                        __bt_mesh_wait_response_timeout, req, NULL);
+
+       if (!pending_requests)
+               pending_requests = l_queue_new();
        l_queue_push_tail(pending_requests, req);
+       BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
 }
 
 static struct mesh_pending_request *__bt_mesh_get_request_by_response(
@@ -298,12 +310,25 @@ static struct mesh_pending_request *__bt_mesh_get_request_by_response(
                        uint32_t response)
 {
        const struct l_queue_entry *entry;
+       char uuid_str[33];
+       char uuid_str1[33];
 
+       BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
        entry = l_queue_get_entries(pending_requests);
 
        for (; entry; entry = entry->next) {
                struct mesh_pending_request *req = entry->data;
 
+               /* Test */
+               BT_INFO("Mesh: Req addr [0x%2.2x] req opcode [0x%2.2x] res [0x%2.2x]", req->addr, req->cmd->opcode, req->cmd->response);
+               BT_INFO("Mesh: Current req addr [0x%2.2x] res [0x%2.2x]", addr, response);
+               _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
+                               BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+               BT_INFO("Mesh: Net UUID[%s]",uuid_str);
+
+               _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16, uuid_str1,
+                               BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+               BT_INFO("Mesh: Net UUID1[%s]",uuid_str1);
                if (!memcmp(net_uuid, req->net_uuid, 16) &&
                                req->addr == addr &&
                                req->cmd->response == response)
@@ -423,7 +448,7 @@ static void __bt_mesh_send_model_publication_status_event(
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &evt,
+               g_array_append_vals(info, evt,
                                sizeof(bluetooth_mesh_model_configure_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -446,7 +471,7 @@ static void __bt_mesh_send_model_subscription_configure_event(
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &evt,
+               g_array_append_vals(info, evt,
                                sizeof(bluetooth_mesh_model_configure_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -536,7 +561,7 @@ static void __bt_mesh_send_model_configure_appkey_event(int result,
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &evt, sizeof(bluetooth_mesh_model_configure_t));
+               g_array_append_vals(info, evt, sizeof(bluetooth_mesh_model_configure_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
                                info->data, info->len,
@@ -558,7 +583,7 @@ static void __bt_mesh_send_node_ttl_configuration_event(int result,
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &ttl_evt,
+               g_array_append_vals(info, ttl_evt,
                        sizeof(bluetooth_mesh_node_ttl_info_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -581,7 +606,7 @@ static void __bt_mesh_send_node_key_configuration_event(int result,
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &key_evt,
+               g_array_append_vals(info, key_evt,
                        sizeof(bluetooth_mesh_key_configure_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -604,7 +629,7 @@ static void __bt_mesh_send_node_get_vendor_features_event(int result,
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &features_evt,
+               g_array_append_vals(info, features_evt,
                                sizeof(bluetooth_mesh_node_features_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -627,7 +652,7 @@ static void __bt_mesh_send_node_browsed_event(int result,
        if (BLUETOOTH_ERROR_NONE == result) {
                /* Send event */
                info = g_array_new(FALSE, FALSE, sizeof(gchar));
-               g_array_append_vals(info, &browse_evt,
+               g_array_append_vals(info, browse_evt,
                                sizeof(bluetooth_mesh_node_discover_t));
 
                out_var = g_variant_new_from_data((const GVariantType *)"ay",
@@ -669,6 +694,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                event->unicast = req->unicast;
                                event->elem_count = req->elem_count;
 
+                               /* Send Event */
+                               __bt_mesh_send_node_get_vendor_features_event(result, event);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_node_features_t));
 
@@ -677,9 +705,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_node_get_vendor_features_event(result, event);
                        }
                        break;
                }
@@ -693,6 +718,8 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        event = (event_mesh_devkey_message_t*) param;
                        node = (bluetooth_mesh_node_discover_t*)req_info->user_data;
+                       BT_INFO("Mesh: Request Node UUID [%s]", node->dev_uuid);
+                       BT_INFO("Mesh: Request Network UUID [%s]", node->net_uuid);
 
                        _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
                                        BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
@@ -705,12 +732,14 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                BT_DBG("Request Sender: [%s]", req_info->sender);
                                /* Match Network and Remote Node unicast*/
                                if (!g_strcmp0(node->net_uuid, net_uuid) && remote_addr == event->source) {
-                                       out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
-                                       g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_t));
-
                                        _bt_mesh_node_get_element_count(event->net_uuid.uuid, remote_addr, &elem_count);
                                        node->unicast = event->source;
                                        node->count = elem_count;
+                                       /* Send Event */
+                                       __bt_mesh_send_node_browsed_event(result, node);
+
+                                       out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+                                       g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_t));
 
                                        /* Return DBUS Invocation*/
                                        _bt_service_method_return(req_info->context, out_param, result);
@@ -718,8 +747,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                        g_free(req_info->user_data);
                                        g_array_free(out_param, TRUE);
 
-                                       /* Send Event */
-                                       __bt_mesh_send_node_browsed_event(result, node);
                                }
                        }
                        break;
@@ -734,6 +761,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast &&
                                req->is_netkey == event->is_netkey) {
+                               /* Send Event */
+                               __bt_mesh_send_node_key_configuration_event(result, event);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_key_configure_t));
 
@@ -742,10 +772,7 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_node_key_configuration_event(result, event);
-                               }
+                       }
                        break;
                }
                case BT_MESH_NODE_TTL_EXECUTE: {
@@ -758,6 +785,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->unicast == event->unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_node_ttl_configuration_event(result, req);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_node_ttl_info_t));
 
@@ -766,9 +796,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_node_ttl_configuration_event(result, req);
                        }
                        break;
                }
@@ -781,6 +808,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_configure_appkey_event(result, req);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -789,9 +819,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_configure_appkey_event(result, req);
                        }
 
                        break;
@@ -805,6 +832,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_get_appkey_list_event(result, event);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -813,9 +843,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_get_appkey_list_event(result, event);
                        }
                        break;
                }
@@ -828,6 +855,9 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_get_subscription_list_event(result, event);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -836,9 +866,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_get_subscription_list_event(result, event);
                        }
                        break;
                }
@@ -853,6 +880,11 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                req->primary_unicast == event->primary_unicast) {
 
                                req->sub_addr = event->sub_addr;
+                               /* Send Event */
+                               __bt_mesh_send_model_subscription_configure_event(
+                                       BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
+                                               result, req);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -861,11 +893,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_subscription_configure_event( \
-                                       BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED, \
-                                               result, req);
                        }
                        break;
                }
@@ -878,6 +905,11 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_subscription_configure_event( \
+                                       BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
+                                               result, req);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -887,10 +919,6 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
 
-                               /* Send Event */
-                               __bt_mesh_send_model_subscription_configure_event( \
-                                       BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
-                                               result, req);
                        }
                        break;
                }
@@ -902,7 +930,12 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                        req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
-                               req->primary_unicast == event->primary_unicast) {
+                                       req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_publication_status_event( \
+                                               BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
+                                               result, event);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -911,12 +944,7 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_publication_status_event( \
-                                       BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
-                                               result, event);
-                               }
+                       }
                        break;
                }
                case BT_MESH_MODEL_SET_PUBLICATION: {
@@ -928,6 +956,11 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
+                               /* Send Event */
+                               __bt_mesh_send_model_publication_status_event( \
+                                       BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
+                                               result, req);
+
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                                g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
 
@@ -936,12 +969,7 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
                                g_array_free(out_param, TRUE);
-
-                               /* Send Event */
-                               __bt_mesh_send_model_publication_status_event( \
-                                       BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
-                                               result, req);
-                               }
+                       }
                        break;
                }
                default:
@@ -1022,17 +1050,19 @@ void _bt_mesh_config_client_devkey_msg_handler(
        struct mesh_pending_request *req;
 
        if (_bt_mesh_util_opcode_get(data, data_len, &opcode, &n)) {
+               BT_INFO("Mesh: Opcode of response data [0x%2.2x], actual data len [%d]", opcode, n);
                data_len -= n;
                data += n;
        } else
                return;
 
-       BT_INFO("Mesh: Received %s (len %u)",
-               __mesh_get_opcode_string(opcode), data_len);
+       BT_INFO("Mesh: Received %s (len %u) opcode [0x%2.2x]",
+               __mesh_get_opcode_string(opcode), data_len, opcode);
 
        req = __bt_mesh_get_request_by_response(event->source,
                event->net_uuid.uuid, (opcode & ~MESH_OPCODE_UNRELIABLE));
        if (req) {
+               BT_INFO("Mesh: Got Config Request");
                cmd = req->cmd;
                __mesh_request_remove(req);
                l_queue_remove(pending_requests, req);
@@ -1350,7 +1380,7 @@ void _bt_mesh_config_client_devkey_msg_handler(
                        break;
                bluetooth_mesh_node_features_t features;
                memset(&features, 0x00, sizeof(bluetooth_mesh_node_features_t));
-
+               BT_INFO("Mesh: Got Response for Device Composition Data");
                __bt_mesh_print_device_composition_data(data, data_len);
 
                if (!_bt_mesh_network_save_remote_node_composition(
@@ -1403,14 +1433,14 @@ void _bt_mesh_config_client_devkey_msg_handler(
 
                if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
                        BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
-                       if (_bt_mesh_network_save_remote_node_netkey(
+                       if (!_bt_mesh_network_save_remote_node_netkey(
                                        event->net_uuid.uuid, event->source, net_idx)) {
                                result = BLUETOOTH_ERROR_INTERNAL;
                                BT_INFO("Failed to save node Netkey!");
                        }
                        param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
                } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
-                       if (_bt_mesh_network_delete_remote_node_netkey(
+                       if (!_bt_mesh_network_delete_remote_node_netkey(
                                        event->net_uuid.uuid, event->source, net_idx)) {
                                result = BLUETOOTH_ERROR_INTERNAL;
                                BT_INFO("Failed to delete node Netkey!");
@@ -1458,7 +1488,7 @@ void _bt_mesh_config_client_devkey_msg_handler(
 
                if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
                        BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
-                       if (_bt_mesh_network_save_remote_node_appkey(
+                       if (!_bt_mesh_network_save_remote_node_appkey(
                                event->net_uuid.uuid, event->source,
                                        net_idx, app_idx)) {
                                result = BLUETOOTH_ERROR_INTERNAL;
@@ -1467,7 +1497,7 @@ void _bt_mesh_config_client_devkey_msg_handler(
                        param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
                } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
                        BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
-                       if (_bt_mesh_network_delete_remote_node_appkey(
+                       if (!_bt_mesh_network_delete_remote_node_appkey(
                                event->net_uuid.uuid, event->source,
                                        net_idx, app_idx)) {
                                result = BLUETOOTH_ERROR_INTERNAL;
@@ -1587,6 +1617,8 @@ int _bt_mesh_browse_remote_node(const char *app_cred,
        _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
                strlen(req->dev_uuid), dev_uuid.uuid, 16);
 
+       BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
+
        /* Get Remote Node unicast address from Dev UUID */
        if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
                        dev_uuid.uuid, &dest))
@@ -2358,7 +2390,7 @@ int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
 
        _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
 
-       if (req->is_netkey && _bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
+       if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
                BT_ERR("Local Subnet not found..");
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
index e0980bd..05bc6e3 100644 (file)
@@ -94,7 +94,7 @@ static bool __mesh_delete_bound_appkey(void *a, void *b)
        if (!_bt_mesh_conf_delete_application_key(cfg, idx))
                return false;
 
-       l_free(a);
+       BT_INFO("Mesh: freed Appkey index [%u]", idx);
        return true;
 }
 static bool __mesh_remove_netkey_entry(void *a, void *b)
@@ -247,25 +247,33 @@ bool _bt_mesh_keys_get_new_netkey_index(uint8_t net_uuid[],
 bool _bt_mesh_keys_get_new_appkey_index(uint8_t net_uuid[],
                uint16_t *app_idx)
 {
-       const struct l_queue_entry *l;
+       const struct l_queue_entry *l, *l1;
        struct mesh_network_t *network;
        int maxkey = -1;
-
+       BT_INFO("Mesh: Find New appkey index");
        network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid);
        if (!network)
                return false;
 
+       BT_INFO("Mesh: Found network, number of netkeys present [%d]",
+                       l_queue_length(network->net_keys));
+
        if (!network->net_keys)
                return false;
 
+       /* TODO: Save max appkey index in Network to avoid looping */
        for (l = l_queue_get_entries(network->net_keys); l; l = l->next) {
                struct mesh_net_key_t  *key = l->data;
 
+               BT_INFO("Mesh: Number of Appkeys in current netkey is [%d]",
+                       l_queue_length(key->app_keys));
+
                if (!key->app_keys)
                        continue;
 
-               for (l = l_queue_get_entries(key->app_keys); l; l = l->next) {
-                       uint16_t app_idx = L_PTR_TO_UINT(l->data);
+               for (l1 = l_queue_get_entries(key->app_keys); l1; l1 = l1->next) {
+                       uint16_t app_idx = L_PTR_TO_UINT(l1->data);
+                       BT_INFO("Mesh: Current AppKey Index [0x%2.2x]", app_idx);
                        if (app_idx > maxkey)
                                maxkey = app_idx;
                }
@@ -346,6 +354,7 @@ void _bt_mesh_keys_del_net_key(uint8_t net_uuid[],
        l_queue_foreach_remove(key->app_keys,
                        __mesh_delete_bound_appkey, cfg);
        l_free(key);
+       BT_INFO("Mesh: NetKey deleted");
 }
 
 void _bt_mesh_keys_add_app_key(uint8_t net_uuid[],
@@ -354,6 +363,7 @@ void _bt_mesh_keys_add_app_key(uint8_t net_uuid[],
        struct mesh_net_key_t  *key;
        struct mesh_network_t *network;
 
+       BT_INFO("Mesh: Add Appkey in Netkey");
        network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid);
        if (!network)
                return;
@@ -373,6 +383,8 @@ void _bt_mesh_keys_add_app_key(uint8_t net_uuid[],
                return;
 
        l_queue_push_tail(key->app_keys, L_UINT_TO_PTR(app_idx));
+       BT_INFO("Mesh: Appkey with index [0x%2.2x] added to Netkey [0x%2.2x]",
+               app_idx, key->idx);
 }
 
 void _bt_mesh_keys_del_app_key(uint8_t net_uuid[], uint16_t app_idx)
@@ -392,7 +404,7 @@ void _bt_mesh_keys_del_app_key(uint8_t net_uuid[], uint16_t app_idx)
 
                if (!key->app_keys)
                        continue;
-
+               BT_INFO("Mesh: Delete AppKey index [%u]", app_idx);
                if (l_queue_remove(key->app_keys, L_UINT_TO_PTR(app_idx)))
                        return;
        }
index 79f81ce..bcc284c 100644 (file)
@@ -187,6 +187,13 @@ static void __bt_mesh_handle_pending_request_info(int result,
                        _bt_mesh_util_convert_hex_to_string(
                                (uint8_t *) event->dev_uuid.uuid, 16, status_data.dev_uuid,
                                        BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+                       BT_INFO("Mesh: Provision Status: Device UUID [%s]", status_data.dev_uuid);
+                       BT_INFO("Mesh: Provision Status: Net UUID [%s]", status_data.net_uuid);
+                       BT_INFO("Mesh: Provision Status: Result [%d]", event->status);
+                       if (event->status == OAL_STATUS_SUCCESS)
+                               BT_INFO("Mesh: Provisioning status : SUCCESS");
+                       else
+                               BT_INFO("Mesh: Provisioning status : FAIL");
 
                        BT_DBG("Request Sender: [%s]", req_info->sender);
                        if (!g_strcmp0(req_data->net_uuid, status_data.net_uuid)) {
@@ -292,9 +299,9 @@ static void __handle_mesh_network_appkey_operation_event(
 
        if (event->status != OAL_STATUS_SUCCESS)
                result = BLUETOOTH_ERROR_INTERNAL;
-
        /* Handle DBUS Context return */
        if (event->op == OAL_MESH_KEY_ADD) {
+               BT_INFO("Mesh: Appkey Add event");
                if (result == BLUETOOTH_ERROR_NONE)
                        _bt_mesh_network_handle_appkey_added(
                                        event->net_uuid.uuid, event->net_idx, event->app_idx);
@@ -302,6 +309,7 @@ static void __handle_mesh_network_appkey_operation_event(
                __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_ADD_APPKEY,
                                event, sizeof(event_mesh_netkey_operation_t));
        } else if (event->op == OAL_MESH_KEY_DELETE) {
+               BT_INFO("Mesh: Appkey Delete event");
                if (result == BLUETOOTH_ERROR_NONE)
                        _bt_mesh_network_handle_appkey_deleted(
                                        event->net_uuid.uuid, event->net_idx, event->app_idx);
@@ -309,6 +317,7 @@ static void __handle_mesh_network_appkey_operation_event(
                __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_DELETE_APPKEY,
                                event, sizeof(event_mesh_netkey_operation_t));
        } else if (event->op == OAL_MESH_KEY_UPDATE) {
+               BT_INFO("Mesh: Appkey Update event");
                __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_UPDATE_APPKEY,
                                event, sizeof(event_mesh_netkey_operation_t));
        }
@@ -389,11 +398,12 @@ static void __handle_mesh_network_scan_finished_event(
        GVariant *out_var = NULL, *param = NULL;
        GArray *info = NULL;
        bluetooth_mesh_network_t network;
-
        int result = BLUETOOTH_ERROR_NONE;
-       if (event->status != OAL_STATUS_SUCCESS)
+       if (event->status != OAL_STATUS_SUCCESS) {
+               BT_INFO("Mesh: Scan Finished: status:: FAILED!");
                result = BLUETOOTH_ERROR_INTERNAL;
-
+       } else
+               BT_INFO("Mesh: Scan Finished: status:: SUCCESS!");
 
        /* Handle Scan finsihed event */
        if (result == BLUETOOTH_ERROR_NONE) {
@@ -422,10 +432,11 @@ static void __handle_mesh_network_provisioning_started_event(
                event_mesh_provisioning_status_t *status)
 {
        int result = BLUETOOTH_ERROR_NONE;
+       BT_INFO("Mesh: Provisioning started");
 
        __bt_mesh_handle_pending_request_info(result,
                        BT_MESH_NETWORK_PROVISION_DEVICE,
-                       status, sizeof(event_mesh_scan_status_t));
+                       status, sizeof(event_mesh_provisioning_status_t));
 
        _bt_mesh_set_provisioning_state(true);
 }
@@ -434,10 +445,10 @@ static void __handle_mesh_network_provisioning_failed_event(
                event_mesh_provisioning_status_t *status)
 {
        int result = BLUETOOTH_ERROR_INTERNAL;
-
+       BT_INFO("Mesh: Provisioning failed!!");
        __bt_mesh_handle_pending_request_info(result,
                        BT_MESH_NETWORK_PROVISION_DEVICE,
-                       status, sizeof(event_mesh_scan_status_t));
+                       status, sizeof(event_mesh_provisioning_status_t));
 
        _bt_mesh_set_provisioning_state(false);
 }
@@ -448,13 +459,15 @@ static void __handle_mesh_network_provisioning_finished_event(
        GVariant *out_var = NULL, *param = NULL;
        GArray *info = NULL;
        bluetooth_mesh_provisioning_result_t prov_result;
+       BT_INFO("Mesh: Provisioning Finished!");
 
        memset(&prov_result, 0x00,
                        sizeof(bluetooth_mesh_provisioning_result_t));
 
        if (event->status != OAL_STATUS_SUCCESS)
                prov_result.result = BLUETOOTH_ERROR_INTERNAL;
-       prov_result.result = BLUETOOTH_ERROR_NONE;
+       else
+               prov_result.result = BLUETOOTH_ERROR_NONE;
 
        prov_result.reason = event->reason;
        prov_result.unicast = event->unicast;
@@ -483,10 +496,17 @@ static void __handle_mesh_network_provisioning_finished_event(
 
        /* Add Remote Node entry in Local CDB */
        if (event->status == OAL_STATUS_SUCCESS) {
+               BT_INFO("Mesh: Provisioning done, add node to Network");
+               BT_INFO("Mesh: Node UUID [%s]", prov_result.dev_uuid);
+               BT_INFO("Mesh: Node Unicast[0x%2.2x] Element Count [%d]",
+                       event->unicast, event->count);
+
                _bt_mesh_network_add_remote_node(
                        event->net_uuid.uuid, event->dev_uuid.uuid,
                                event->unicast, event->count);
        }
+       /* Unset provisioning state */
+       _bt_mesh_set_provisioning_state(false);
 }
 
 static void __handle_mesh_network_provisioning_data_requested_event(
@@ -514,6 +534,8 @@ static void __handle_mesh_network_authentication_requested_event(
                        (uint8_t *) event->net_uuid.uuid, 16, auth_req.net_uuid,
                        BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
 
+       BT_INFO("Mesh: Authentication Requested by Device: Network [%s]",
+               auth_req.net_uuid);
        info = g_array_new(FALSE, FALSE, sizeof(gchar));
        g_array_append_vals(info, &auth_req,
                        sizeof(bluetooth_mesh_authentication_request_t));
@@ -578,6 +600,10 @@ static void __handle_mesh_network_scan_result_event(
 static void __handle_mesh_events(int event_type,
                gpointer event_data)
 {
+       BT_INFO("Mesh: Got Mesh event!!! event type [%d]", event_type);
+       if (event_type == OAL_EVENT_MESH_PROVISIONING_STARTED)
+               BT_INFO("Mesh: Provisioning started event");
+
        switch (event_type) {
        case OAL_EVENT_MESH_NETWORK_ATTACHED:
                __handle_mesh_network_attached_event(
@@ -600,11 +626,13 @@ static void __handle_mesh_events(int event_type,
                BT_PERMANENT_LOG("Mesh: Network Scan Result!!");
                break;
        case OAL_EVENT_MESH_PROVISIONING_STARTED:
+               BT_INFO("Mesh: Network Provisioning Started");
                __handle_mesh_network_provisioning_started_event(
                        (event_mesh_provisioning_status_t*)event_data);
                BT_PERMANENT_LOG("Mesh: Network Provisioning Started");
-       break;
+               break;
        case OAL_EVENT_MESH_PROVISIONING_FAILED:
+               BT_INFO("Mesh: Network Provisioning Failed!!!!");
                __handle_mesh_network_provisioning_failed_event(
                        (event_mesh_provisioning_status_t*)event_data);
                BT_PERMANENT_LOG("Mesh: Network Provisioning Failed");
index befaf00..690be51 100644 (file)
@@ -108,20 +108,19 @@ static gint __mesh_compare_app_network_uuid(
 static gint __mesh_compare_app_cdb_token(gconstpointer data,
                gconstpointer user_data)
 {
-       char *token_str = NULL;
+       char token_str[17];
        const _bt_mesh_cdb_t *cdb = data;
        const char *token = user_data;
-       int ret = 0;
 
        retv_if(NULL == cdb, -1);
        retv_if(NULL == token, -1);
 
-       token_str = _bt_service_convert_hex_to_string(
-               (unsigned char*)cdb->token, 8);
+       _bt_mesh_util_convert_hex_to_string((uint8_t *) cdb->token,
+                       8, token_str, 17);
 
-       ret = g_strcmp0(token_str, token);
-       g_free(token_str);
-       return ret;
+       BT_INFO("Mesh: Match Token 1[%s] Toekn 2 [%s]",
+                       token_str, token);
+       return g_strcmp0(token_str, token);
 }
 
 static gint __mesh_compare_addr(
@@ -162,10 +161,15 @@ int _bt_mesh_load_app_networks(const char *app_cred)
 
 static void __bt_mesh_free_temp_node(mesh_local_node_t *tmp)
 {
+       if (!tmp)
+               return;
        temp_nodes = g_slist_remove(temp_nodes, tmp);
        g_free((gpointer)tmp->app_cred);
        g_free((gpointer)tmp->sender);
-       g_slist_free_full(tmp->model_list, g_free);
+       /* Do not free tmp->model_list here
+         This memory is also being used in HAL layer
+         This memory will be freed with the call of __mesh_client_disconnected */
+       tmp->model_list = NULL;
        g_free(tmp);
 }
 
@@ -175,10 +179,17 @@ int _bt_mesh_network_request_provisioning_data_request(uint8_t net_uuid[],
        uint16_t unicast;
        int ret = OAL_STATUS_SUCCESS;
        oal_uuid_t uuid;
+       char uuid_string[33];
+
+       _bt_mesh_util_convert_hex_to_string(net_uuid, 16,
+                       uuid_string, sizeof(uuid_string));
+       BT_INFO("Mesh: Provisioning Data requested for network [%s]",
+                       uuid_string);
+
        unicast = _bt_mesh_node_get_next_unicast(net_uuid,
                        MESH_DEFAULT_START_ADDRESS,
                        MESH_DEFAULT_START_ADDRESS, count);
-       BT_INFO("Mesh: Network: Got unicast [0x%2x]", unicast);
+       BT_INFO("Mesh: Network: Got unicast [%4.4x]", unicast);
 
        memcpy(uuid.uuid, net_uuid, 16);
        /* Register Mesh Node */
@@ -202,11 +213,14 @@ int _bt_mesh_network_create_cdb(int result,
        mesh_local_node_t *tmp;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
        char uuid_string[33];
+       char token_string[17];
 
        _bt_mesh_util_convert_hex_to_string(uuid, 16,
                        uuid_string, sizeof(uuid_string));
-       BT_INFO("Mesh: Create CDB request for network UUID [%s]",
-                       uuid_string);
+       _bt_mesh_util_convert_hex_to_string(token, 8,
+                       token_string, sizeof(token_string));
+       BT_INFO("Mesh: Create CDB request for network UUID [%s] token [%s]",
+                       uuid_string, token_string);
        BT_INFO("Mesh: Temporary node count [%d]",
                        g_slist_length(temp_nodes));
 
@@ -245,6 +259,7 @@ int _bt_mesh_network_create_cdb(int result,
        file_path = g_strdup_printf("%s%s_config.json",
                        dir_path, uuid_string);
        BT_INFO("Mesh: CDB File path[%s]", file_path);
+       BT_INFO("Mesh: CDB App Cred[%s]", app_creds);
        cdb_cfg = _bt_mesh_conf_database_create(file_path, uuid,
                        token, network, app_creds);
 
@@ -309,7 +324,7 @@ int _bt_mesh_network_scan(const char *app_cred,
        /* If Scanning is going on */
        if (_bt_mesh_is_provisioning() ||
                        _bt_mesh_is_scanning()) {
-               BT_ERR("Device is already scanning..");
+               BT_ERR("Device is buzy..");
                return BLUETOOTH_ERROR_DEVICE_BUSY;
        }
        _bt_mesh_util_convert_string_to_hex(network->uuid,
@@ -333,6 +348,10 @@ int _bt_mesh_network_scan_cancel(const char *app_cred,
        int ret = OAL_STATUS_SUCCESS;
        oal_uuid_t net_uuid;
 
+       if (!_bt_mesh_is_scanning()) {
+               BT_INFO("Mesh: Scanning is not ongoing");
+               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+       }
        _bt_mesh_util_convert_string_to_hex(network->uuid,
                strlen(network->uuid), net_uuid.uuid, 16);
        /* Register Mesh Node */
@@ -378,7 +397,7 @@ void _bt_mesh_set_scanning_state(bool state)
 
 bool _bt_mesh_is_provisioning(void)
 {
-       return bt_mesh_is_scanning;
+       return bt_mesh_is_provisoning;
 }
 
 void _bt_mesh_set_provisioning_state(bool state)
@@ -439,6 +458,7 @@ int _bt_mesh_authentication_reply(int auth_type,
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+       BT_INFO("Mesh: Scheduled Prov Auth Reply!");
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -564,6 +584,7 @@ int _bt_mesh_network_handle_netkey_added(
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Handle Netkey Added");
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                        __mesh_compare_app_network_uuid);
@@ -587,6 +608,7 @@ int _bt_mesh_network_handle_netkey_deleted(
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Handle Netkey Deleted");
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                __mesh_compare_app_network_uuid);
@@ -609,6 +631,7 @@ int _bt_mesh_network_handle_netkey_updated(
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Handle Netkey Updated");
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                        __mesh_compare_app_network_uuid);
@@ -629,6 +652,7 @@ int _bt_mesh_network_handle_appkey_added(uint8_t net_uuid[],
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Handle Appkey Added");
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                        __mesh_compare_app_network_uuid);
@@ -653,6 +677,7 @@ int _bt_mesh_network_handle_appkey_deleted(uint8_t net_uuid[],
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Handle Appkey Deleted");
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                __mesh_compare_app_network_uuid);
@@ -674,6 +699,11 @@ int _bt_mesh_network_set_name(const char *app_cred, const char *sender,
 {
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
+       BT_INFO("Mesh: Set network name, app_creds [%s]", app_cred);
+       BT_INFO("Mesh: UUID [%s]", network->uuid);
+       BT_INFO("Mesh: Token[%s]", network->token.token);
+       BT_INFO("Mesh: Name to be set[%s]", network->name.name);
+
        /* Find CDB */
        l = g_slist_find_custom(cdb_list,
                network->token.token, __mesh_compare_app_cdb_token);
@@ -745,8 +775,10 @@ int _bt_mesh_network_delete_netkey(const char *app_cred,
                return BLUETOOTH_ERROR_INVALID_PARAM;
 
        /* Check if NetKey is already added to one of the provisioned nodes */
-       if (_bt_mesh_node_is_netkey_added(cdb_cfg->uuid, index))
+       if (_bt_mesh_node_is_netkey_added_in_network(cdb_cfg->uuid, index)) {
+               BT_INFO("Mesh: NetKey index [0x%2.2x] can not be deleted", index);
                return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
 
        _bt_mesh_util_convert_string_to_hex(network->uuid,
                        strlen(network->uuid), net_uuid.uuid, 16);
@@ -897,7 +929,7 @@ int _bt_mesh_network_delete_appkey(const char *app_cred,
                return BLUETOOTH_ERROR_INVALID_PARAM;
 
        /* Check if AppKey is already added to one of the provisioned nodes */
-       if (_bt_mesh_node_is_appkey_added(cdb_cfg->uuid, appkey_idx))
+       if (_bt_mesh_node_is_appkey_added_in_network(cdb_cfg->uuid, appkey_idx))
                return BLUETOOTH_ERROR_INVALID_PARAM;
 
        _bt_mesh_util_convert_string_to_hex(network->uuid,
@@ -920,7 +952,7 @@ int _bt_mesh_network_create(const char *app_cred, const char *sender,
 {
        int ret = OAL_STATUS_SUCCESS;
 
-       BT_INFO("Mesh: App Cred [%s] sender [%s] network [%s]",
+       BT_INFO("Mesh: App Credential [%s] sender [%s] network [%s]",
                        app_cred, sender, network_name);
 
        /* TODO Handle Buzy status */
@@ -972,6 +1004,11 @@ bool _bt_mesh_network_save_remote_node_appkey(
 
        cdb_cfg = (_bt_mesh_cdb_t*)l->data;
 
+       if (_bt_mesh_node_is_appkey_exists(net_uuid,
+                       remote_unicast, netkey_idx)) {
+               BT_INFO("Mesh: AppKey is already added to Node");
+               return true;
+       }
        if (_bt_mesh_node_add_app_key(net_uuid,
                        remote_unicast, appkey_idx))
                return _bt_mesh_conf_node_insert_application_key(cdb_cfg,
@@ -1037,6 +1074,11 @@ bool _bt_mesh_network_save_remote_node_netkey(
 
        cdb_cfg = (_bt_mesh_cdb_t*)l->data;
 
+       if (_bt_mesh_node_is_netkey_exists(net_uuid,
+                       remote_unicast, netkey_idx)) {
+               BT_INFO("Mesh: NetKey is already added to Node");
+               return true;
+       }
        if (_bt_mesh_node_add_net_key(net_uuid,
                        remote_unicast, netkey_idx))
                return _bt_mesh_conf_node_insert_network_key(cdb_cfg,
index 489cdb4..ffbfe07 100644 (file)
@@ -227,7 +227,7 @@ bool _bt_mesh_node_get_unicast_from_dev_uuid(uint8_t net_uuid[],
 
        rmt = l_queue_find(network->nodes,
                        __mesh_match_node_dev_uuid, dev_uuid);
-       if (rmt)
+       if (!rmt)
                return false;
 
        *unicast = rmt->unicast;
@@ -246,7 +246,7 @@ bool _bt_mesh_node_get_element_count(uint8_t net_uuid[],
 
        rmt = l_queue_find(network->nodes, __mesh_match_node_addr,
                        L_UINT_TO_PTR(unicast));
-       if (rmt)
+       if (!rmt)
                return false;
 
        *count = rmt->num_ele;
@@ -283,7 +283,10 @@ bool _bt_mesh_node_add_node(uint8_t net_uuid[],
        if (!network->nodes)
                network->nodes = l_queue_new();
 
-       l_queue_insert(network->nodes, rmt, __mesh_compare_unicast, NULL);
+       if (l_queue_insert(network->nodes, rmt, __mesh_compare_unicast, NULL))
+               BT_INFO("Mesh: node added to Network");
+       else
+               BT_INFO("Mesh: node could not be added to Network");
        return true;
 }
 
@@ -458,7 +461,9 @@ bool _bt_mesh_node_get_all(uint8_t net_uuid[], GArray **out)
                struct _bt_mesh_node_t *node = entry->data;
 
                n.primary_unicast = node->unicast;
-               n.num_elements = node->num_ele;
+               _bt_mesh_util_convert_hex_to_string(node->uuid, 16,
+                       n.dev_uuid, sizeof(n.dev_uuid));
+               BT_INFO("Mesh: Dev UUID [%s]", n.dev_uuid);
 
                g_array_append_vals(*out, &n,
                                sizeof(bluetooth_mesh_node_info_t));
@@ -466,7 +471,7 @@ bool _bt_mesh_node_get_all(uint8_t net_uuid[], GArray **out)
        return true;
 }
 
-bool _bt_mesh_node_is_netkey_added(uint8_t net_uuid[],
+bool _bt_mesh_node_is_netkey_added_in_network(uint8_t net_uuid[],
                uint16_t net_idx)
 {
        struct mesh_network_t *network;
@@ -487,7 +492,7 @@ bool _bt_mesh_node_is_netkey_added(uint8_t net_uuid[],
        return false;
 }
 
-bool _bt_mesh_node_is_appkey_added(uint8_t net_uuid[],
+bool _bt_mesh_node_is_appkey_added_in_network(uint8_t net_uuid[],
                uint16_t app_idx)
 {
        struct mesh_network_t *network;
@@ -508,7 +513,28 @@ bool _bt_mesh_node_is_appkey_added(uint8_t net_uuid[],
        return false;
 }
 
-bool _bt_mesh_node_add_net_key(uint8_t net_uuid[],
+bool _bt_mesh_node_is_appkey_exists(uint8_t net_uuid[],
+               uint16_t addr, uint16_t app_idx)
+{
+       struct _bt_mesh_node_t *rmt;
+       struct mesh_network_t *network;
+
+       network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid);
+       if (!network)
+               return false;
+
+       rmt = l_queue_find(network->nodes,
+                       __mesh_match_node_addr, L_UINT_TO_PTR(addr));
+       if (!rmt)
+               return false;
+
+       if (__mesh_key_present(rmt->app_keys, app_idx))
+               return true;
+
+       return false;
+}
+
+bool _bt_mesh_node_is_netkey_exists(uint8_t net_uuid[],
                uint16_t addr, uint16_t net_idx)
 {
        struct _bt_mesh_node_t *rmt;
@@ -524,8 +550,29 @@ bool _bt_mesh_node_add_net_key(uint8_t net_uuid[],
                return false;
 
        if (__mesh_key_present(rmt->net_keys, net_idx))
+               return true;
+
+       return false;
+}
+
+bool _bt_mesh_node_add_net_key(uint8_t net_uuid[],
+               uint16_t addr, uint16_t net_idx)
+{
+       struct _bt_mesh_node_t *rmt;
+       struct mesh_network_t *network;
+
+       network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid);
+       if (!network)
+               return false;
+
+       rmt = l_queue_find(network->nodes,
+                       __mesh_match_node_addr, L_UINT_TO_PTR(addr));
+       if (!rmt)
                return false;
 
+       if (__mesh_key_present(rmt->net_keys, net_idx))
+               return true;
+
        l_queue_push_tail(rmt->net_keys, L_UINT_TO_PTR(net_idx));
        return true;
 }
@@ -608,8 +655,10 @@ uint16_t _bt_mesh_node_get_next_unicast(uint8_t net_uuid[],
 
        network = l_queue_find(networks,
                        __mesh_net_uuid_match, net_uuid);
-       if (!network)
+       if (!network) {
+               BT_ERR("Mesh: Net UUID did not match");
                return 0;
+       }
 
        /* Note: the address space includes both
           low and high terminal values */
index 9307c0e..08fe99a 100644 (file)
@@ -131,6 +131,8 @@ uint16_t _bt_mesh_util_opcode_set(uint32_t opcode,
 bool _bt_mesh_util_opcode_get(const uint8_t *buf,
                uint16_t sz, uint32_t *opcode, int *n)
 {
+       BT_INFO("Mesh: Opcode Get DatLen [%d] Buf0 [0x%x]",
+                       sz, buf[0]);
        if (!n || !opcode || sz < 1) return false;
 
        switch (buf[0] & 0xc0) {
index c83a99e..e054b52 100644 (file)
@@ -218,7 +218,7 @@ typedef struct {
 
 typedef struct {
        uint16_t primary_unicast;
-       int num_elements;
+       char dev_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
 } bluetooth_mesh_node_info_t;
 
 /**