Mesh: Apply stabilization fixes in FRWK 08/239508/2
authorAnupam Roy <anupam.r@samsung.com>
Mon, 27 Jul 2020 07:50:06 +0000 (13:20 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Mon, 27 Jul 2020 08:00:07 +0000 (08:00 +0000)
This patch applies few fixes for bugs/issues
found during MESH API testing & stabilziation

Some of the notable changes applied are following-
1. Add missing event handling for Model AppKey Configure command
2. Add exception handling for Key Configuration command
  [Send failure event to app, in case of failure]
3. Fix bug in Saving AppKey in Mesh node structure
  [Instead of AppKey, netkey was used, resulting in error]
4. Bug in fetching ALL AppKey(s) bound to a node
  [Instead of retruning appkeys bound to a Netkey, FRWK
   was returning all appkeys bound to a node, resulting in
   unexpected behavior of the API]
5. Added few DEBUG Prints.

Change-Id: I3b704b2eccd9ea79f59bae5deddf4b50275b365f
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
bt-api/bt-event-handler.c
bt-api/bt-request-sender.c
bt-service/services/bt-service-event-sender.c
bt-service/services/include/bt-service-mesh-nodes.h
bt-service/services/mesh/bt-service-mesh-config-client.c
bt-service/services/mesh/bt-service-mesh-network.c
bt-service/services/mesh/bt-service-mesh-nodes.c
include/bluetooth-api.h

index c08f6ec..abdecd3 100644 (file)
@@ -559,6 +559,23 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
                                result, info,
                                event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_APPKEY_CONFIGURED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_model_configure_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar));
+
+               __bt_fill_garray_from_variant(param, dbus_data);
+               g_variant_unref(param);
+
+               info = &g_array_index(dbus_data, bluetooth_mesh_model_configure_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
+                               result, info,
+                               event_info->cb, event_info->user_data);
        } else if (strcasecmp(signal_name, BT_MESH_MODEL_APPKEY_LIST) == 0) {
                int result;
                GVariantIter *iter = NULL;
@@ -571,7 +588,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                uint16_t appkey_idx;
                int i;
 
-               g_variant_get(parameters, "(i&sqiui(ay))", &result, &net_uuid,
+               g_variant_get(parameters, "(i&sqiui(aq))", &result, &net_uuid,
                                &node_unicast, &elem_idx, &model, &total, &iter);
 
                memset(&info, 0x00, sizeof(bluetooth_mesh_model_configure_t));
@@ -580,7 +597,7 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                info.elem_index = elem_idx;
                info.model = model;
                info.appkeylist_count = total;
-
+               BT_INFO("Mesh: Total appkeys bound to model [%d]", total);
                if (total) {
                        info.appkey_list = (uint16_t**)g_malloc0(sizeof(uint16_t*) * total);
 
@@ -594,11 +611,13 @@ static void __bt_mesh_event_filter(GDBusConnection *connection,
                                result, &info,
                                event_info->cb, event_info->user_data);
 
-               if (total) {
-                       for (i = 0; i < total; i++)
-                               g_free(info.appkey_list[i]);
+               for (i = 0; i < total; i++)
+                       g_free(info.appkey_list[i]);
+               if (info.appkey_list) {
+                       g_free(info.appkey_list);
                        g_variant_iter_free(iter);
                }
+
        } else if (strcasecmp(signal_name, BT_MESH_MODEL_SUB_LIST) == 0) {
                int result;
                GVariantIter *iter = NULL;
index a8ce6d9..c530c89 100644 (file)
@@ -368,7 +368,7 @@ static void __bt_get_event_info(int service_function, GArray *output,
                break;
        case BT_MESH_MODEL_CONFIGURE_APPKEY:
                *event_type = BT_MESH_EVENT;
-               *event = BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND;
                ret_if(output == NULL);
                *param_data = &g_array_index(output,
                                bluetooth_mesh_model_configure_t, 0);
index a38230d..4168567 100644 (file)
@@ -572,7 +572,7 @@ int _bt_send_event(int event_type, int event, GVariant *param)
        case BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED:
                signal = BT_MESH_NODE_TTL_CONFIGURED;
                break;
-       case BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND:
+       case BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND:
                signal = BT_MESH_MODEL_APPKEY_CONFIGURED;
                break;
        case BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST:
index fea1a47..4833ef3 100644 (file)
@@ -101,7 +101,7 @@ bool _bt_mesh_node_get_all_netkeys(uint8_t net_uuid[],
                uint16_t unicast, GArray **out);
 
 bool _bt_mesh_node_get_all_appkeys(uint8_t net_uuid[],
-               uint16_t unicast, GArray **out);
+               uint16_t unicast, uint16_t netkey_idx, GArray **out);
 
 #ifdef __cplusplus
 }
index a9bfac4..211e46d 100644 (file)
@@ -522,21 +522,22 @@ static void __bt_mesh_send_model_get_appkey_list_event(int result,
 {
        GVariant  *param = NULL;
        GVariantBuilder *builder = NULL;
+       char *net_uuid;
        int i;
 
        if (BLUETOOTH_ERROR_NONE == result) {
-
-               if (evt->appkeylist_count) {
-                       builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
-                       for (i = 0; i < evt->appkeylist_count; i++)
-                               g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
-
-                       param = g_variant_new("(isqiui(aq))", result, evt->net_uuid,
-                                       evt->primary_unicast, evt->elem_index, evt->model,
-                                       evt->appkeylist_count, builder);
-
-                       g_variant_builder_unref(builder);
-               }
+               builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
+               BT_INFO("Mesh: Total AppKeys bound to model [%d]",
+                               evt->appkeylist_count);
+               BT_INFO("Mesh: Network [%s]", evt->net_uuid);
+               for (i = 0; i < evt->appkeylist_count; i++)
+                       g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
+               net_uuid = g_strdup(evt->net_uuid);
+               param = g_variant_new("(isqiui(aq))", result, net_uuid,
+                               evt->primary_unicast, evt->elem_index, evt->model,
+                               evt->appkeylist_count, builder);
+
+               g_variant_builder_unref(builder);
 
                /* Send event */
                _bt_send_event(BT_MESH_EVENT,
@@ -544,11 +545,13 @@ static void __bt_mesh_send_model_get_appkey_list_event(int result,
                                param);
 
                if (evt->appkeylist_count) {
-                       /* Free List data */
                        for (int i = 0; i < evt->appkeylist_count; i++)
                                g_free(evt->appkey_list[i]);
+
                        g_free(evt->appkey_list);
                }
+               /* Free List data */
+               g_free(net_uuid);
        }
 }
 
@@ -569,7 +572,7 @@ static void __bt_mesh_send_model_configure_appkey_event(int result,
 
                param = g_variant_new("(iv)", result, out_var);
                _bt_send_event(BT_MESH_EVENT,
-                               BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND,
+                               BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
                                param);
        }
 }
@@ -1189,6 +1192,10 @@ void _bt_mesh_config_client_devkey_msg_handler(
                                BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
                BT_INFO("Node %4.4x: Model App status %s\n", event->source,
                        __mesh_status_to_string(data[0]));
+
+               if (data[0] != MESH_STATUS_SUCCESS)
+                       result = BLUETOOTH_ERROR_INTERNAL;
+
                elem_addr = l_get_le16(data + 1);
                app_idx = l_get_le16(data + 3);
 
@@ -1410,7 +1417,7 @@ void _bt_mesh_config_client_devkey_msg_handler(
                BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
 
                if (data[0] != 0)
-                       break;
+                       result = BLUETOOTH_ERROR_INTERNAL;
 
                if (!cmd)
                        break;
@@ -1422,19 +1429,25 @@ void _bt_mesh_config_client_devkey_msg_handler(
                param.netkey_idx = net_idx;
                param.is_netkey = true;
 
+               BT_INFO("Mesh: Resp recvd from node unicast [0x%4.4x]", event->source);
                if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
                        BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
-                       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!");
+                       if (result == BLUETOOTH_ERROR_NONE) {
+                               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(
-                                       event->net_uuid.uuid, event->source, net_idx)) {
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                               BT_INFO("Failed to delete node Netkey!");
+                       BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_DELETE");
+                       if (result == BLUETOOTH_ERROR_NONE) {
+                               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!");
+                               }
                        }
                        param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
                } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
@@ -1463,7 +1476,7 @@ void _bt_mesh_config_client_devkey_msg_handler(
                BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
 
                if (data[0] != MESH_STATUS_SUCCESS)
-                       break;
+                       result = BLUETOOTH_ERROR_INTERNAL;
 
                if (!cmd)
                        break;
@@ -1479,20 +1492,24 @@ 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(
-                               event->net_uuid.uuid, event->source,
-                                       net_idx, app_idx)) {
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                               BT_INFO("Failed to save node Appkey!");
+                       if (result == BLUETOOTH_ERROR_NONE) {
+                               if (!_bt_mesh_network_save_remote_node_appkey(
+                                               event->net_uuid.uuid, event->source,
+                                               net_idx, app_idx)) {
+                                       result = BLUETOOTH_ERROR_INTERNAL;
+                                       BT_INFO("Failed to save node Appkey!");
+                               }
                        }
                        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(
-                               event->net_uuid.uuid, event->source,
-                                       net_idx, app_idx)) {
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                               BT_INFO("Failed to delete node Appkey!");
+                       if (result == BLUETOOTH_ERROR_NONE) {
+                               if (!_bt_mesh_network_delete_remote_node_appkey(
+                                               event->net_uuid.uuid, event->source,
+                                               net_idx, app_idx)) {
+                                       result = BLUETOOTH_ERROR_INTERNAL;
+                                       BT_INFO("Failed to delete node Appkey!");
+                               }
                        }
                        param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
                } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
index 81bc2c9..fd3f2f2 100644 (file)
@@ -550,7 +550,7 @@ int _bt_mesh_network_node_get_appkeys(const char *app_cred,
        cdb_cfg = (_bt_mesh_cdb_t*)l->data;
 
        if (_bt_mesh_node_get_all_appkeys(cdb_cfg->uuid,
-                               node->unicast, out_param))
+                               node->unicast, node->netkey_idx, out_param))
                return BLUETOOTH_ERROR_NONE;
        else
                return BLUETOOTH_ERROR_INTERNAL;
@@ -996,6 +996,7 @@ bool _bt_mesh_network_save_remote_node_appkey(
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
 
+       BT_INFO("Mesh: Add Appkey [0x%2.2x] to node", appkey_idx);
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                        __mesh_compare_app_network_uuid);
@@ -1005,15 +1006,18 @@ 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)) {
+                       remote_unicast, appkey_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))
+                       remote_unicast, appkey_idx)) {
+               BT_INFO("Mesh: Add App Key Idx [0x%2.2x] in CDB", appkey_idx);
                return _bt_mesh_conf_node_insert_application_key(cdb_cfg,
                                        remote_unicast, appkey_idx);
+       }
 
+       BT_INFO("Mesh: AppKey idx [0x%2.2x] Failed to add in Node", appkey_idx);
        return false;
 }
 
@@ -1023,7 +1027,10 @@ bool _bt_mesh_network_delete_remote_node_appkey(
 {
        GSList *l;
        _bt_mesh_cdb_t *cdb_cfg = NULL;
+       bool is_deleted = false;
 
+       BT_INFO("Mesh: Delete Appkey Idx[0x%2.2x]from Node Unicast [0x%2.2x]",
+                               appkey_idx, remote_unicast);
        /* Find CDB */
        l = g_slist_find_custom(cdb_list, net_uuid,
                        __mesh_compare_app_network_uuid);
@@ -1032,12 +1039,18 @@ bool _bt_mesh_network_delete_remote_node_appkey(
 
        cdb_cfg = (_bt_mesh_cdb_t*)l->data;
 
-       if (_bt_mesh_node_del_net_key(cdb_cfg,
-                       net_uuid, remote_unicast, appkey_idx))
-               return _bt_mesh_conf_node_delete_application_key(cdb_cfg,
+       if (_bt_mesh_node_del_app_key(net_uuid,
+                       remote_unicast, appkey_idx)) {
+               BT_INFO("Mesh: Removed Appkey from node");
+               is_deleted =  _bt_mesh_conf_node_delete_application_key(cdb_cfg,
                                remote_unicast, appkey_idx);
+               if (is_deleted)
+                       BT_INFO("Mesh: AppKey removed from CDB Node Entry");
+               else
+                       BT_INFO("Mesh: AppKey could not be removed from CDB Node Entry");
+       }
 
-       return false;
+       return is_deleted;
 }
 
 bool _bt_mesh_network_save_remote_node_ttl(
index ffbfe07..5173bac 100644 (file)
@@ -390,7 +390,7 @@ bool _bt_mesh_node_del_net_key(_bt_mesh_cdb_t *cfg,
 }
 
 bool _bt_mesh_node_get_all_appkeys(uint8_t net_uuid[],
-               uint16_t unicast, GArray **out)
+               uint16_t unicast, uint16_t netkey, GArray **out)
 {
        struct mesh_network_t *network;
        const struct l_queue_entry *entry;
@@ -408,10 +408,11 @@ bool _bt_mesh_node_get_all_appkeys(uint8_t net_uuid[],
        entry = l_queue_get_entries(rmt->app_keys);
 
        for (; entry; entry = entry->next) {
-
+               uint16_t net_idx;
                uint16_t app_idx = L_PTR_TO_UINT(entry->data);
-
-               g_array_append_vals(*out, &app_idx, sizeof(uint16_t));
+               net_idx = _bt_mesh_keys_get_bound_key(net_uuid, app_idx);
+               if (net_idx == netkey)
+                       g_array_append_vals(*out, &app_idx, sizeof(uint16_t));
        }
        return true;
 }
@@ -600,6 +601,8 @@ bool _bt_mesh_node_add_app_key(uint8_t net_uuid[],
                return false;
 
        l_queue_push_tail(rmt->app_keys, L_UINT_TO_PTR(app_idx));
+       BT_INFO("Mesh: Total appkeys present in node [%d]",
+                       l_queue_length(rmt->app_keys));
        return true;
 }
 
@@ -611,14 +614,21 @@ bool _bt_mesh_node_del_app_key(uint8_t net_uuid[],
 
        network = l_queue_find(networks,
                        __mesh_net_uuid_match, net_uuid);
-       if (!network)
+       if (!network) {
+               BT_INFO("Mesh: Could not find Network");
                return false;
+       }
 
        rmt = l_queue_find(network->nodes,
                        __mesh_match_node_addr, L_UINT_TO_PTR(addr));
-       if (!rmt)
+       if (!rmt) {
+               BT_INFO("Mesh: Remote Node not found!");
                return false;
+       }
 
+       BT_INFO("Mesh: Total appkeys present in node [%d]",
+                       l_queue_length(rmt->app_keys));
+       BT_INFO("Mesh: To be deleted AppKey Idx [0x%2.2x]", app_idx);
        return l_queue_remove(rmt->app_keys, L_UINT_TO_PTR(app_idx));
 }
 
index 65216c7..9b3da39 100644 (file)
@@ -960,7 +960,7 @@ typedef enum {
        BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
        BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
        BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
-       BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND,
+       BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
        BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
        BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
        BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,