Mesh: Handle OAL events 53/237453/3
authorAnupam Roy <anupam.r@samsung.com>
Sat, 27 Jun 2020 19:05:12 +0000 (00:35 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Fri, 17 Jul 2020 16:31:46 +0000 (22:01 +0530)
Change-Id: I2a06ea2ecac60b4ae0c5b1f0d4182dfa02053b23
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
bt-service-adaptation/services/bt-service-event-receiver.c
bt-service-adaptation/services/include/bt-service-mesh-cdb.h
bt-service-adaptation/services/include/bt-service-mesh-network.h
bt-service-adaptation/services/mesh/bt-service-mesh-cdb.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

index 17383fa..d152e57 100644 (file)
@@ -51,6 +51,7 @@ _bt_service_event_handler_callback avrcp_cb;
 _bt_service_event_handler_callback adapter_le_cb;
 _bt_service_event_handler_callback adapter_gatt_cb;
 _bt_service_event_handler_callback hf_client_cb;
+_bt_service_event_handler_callback mesh_cb;
 
 void _bt_service_register_event_handler_callback(
                bt_service_module_t module, _bt_service_event_handler_callback cb)
@@ -116,6 +117,10 @@ void _bt_service_register_event_handler_callback(
                BT_INFO("Register BT_GATT_MODULE callback");
                adapter_gatt_cb = cb;
                break;
+       case BT_MESH_MODULE:
+               BT_INFO("Register BT_MESH_MODULE callback");
+               mesh_cb = cb;
+               break;
        default:
                BT_INFO("Unknown module");
        }
@@ -168,6 +173,10 @@ void _bt_service_unregister_event_handler_callback(bt_service_module_t module)
                BT_INFO("Un-Register BT_GATT_MODULE callback");
                adapter_gatt_cb = NULL;
                break;
+       case BT_MESH_MODULE:
+               BT_INFO("Un-Register BT_MESH_MODULE callback");
+               mesh_cb = NULL;
+               break;
        default:
                BT_INFO("Unknown module");
        }
@@ -399,6 +408,21 @@ static gboolean __bt_handle_oal_events(gpointer data)
                if (adapter_le_cb)
                        adapter_le_cb(event_type, event_data);
                break;
+       case OAL_EVENT_MESH_NETWORK_ATTACHED:
+       case OAL_EVENT_MESH_SCAN_STARTED:
+       case OAL_EVENT_MESH_SCAN_FINISHED:
+       case OAL_EVENT_MESH_SCAN_RESULT:
+       case OAL_EVENT_MESH_PROVISIONING_STARTED:
+       case OAL_EVENT_MESH_PROVISIONING_FAILED:
+       case OAL_EVENT_MESH_PROVISIONING_FINISHED:
+       case OAL_EVENT_MESH_PROVISIONING_DATA_REQUESTED:
+       case OAL_EVENT_MESH_AUTHENTICATION_REQUESTED:
+       case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT:
+       case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT:
+       case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED:
+               if (mesh_cb)
+                       mesh_cb(event_type, event_data);
+               break;
        default:
                BT_ERR("Unhandled Event: %d", event_type);
                break;
index c36c9a1..3194f99 100644 (file)
@@ -61,12 +61,14 @@ _bt_mesh_cdb_t * _bt_mesh_conf_database_create(const char *file_name,
 bool _bt_mesh_conf_set_phase_network_key(_bt_mesh_cdb_t *cfg,
        uint16_t net_idx, uint8_t phase);
 
-_bt_mesh_cdb_t* _bt_mesh_cdb_load(const char *file_name,
+_bt_mesh_cdb_t* _bt_mesh_conf_load(const char *file_name,
                const char *token);
 
 bool _bt_mesh_conf_delete_application_key(_bt_mesh_cdb_t *cfg,
                uint16_t app_idx);
 
+bool _bt_mesh_conf_load_all_nodes(_bt_mesh_cdb_t *cfg);
+
 bool _bt_mesh_conf_insert_network_key(_bt_mesh_cdb_t *cfg,
                uint16_t idx, uint8_t key_refresh);
 
index 66f101c..6e86032 100644 (file)
@@ -43,6 +43,10 @@ int _bt_mesh_network_create_cdb(int result,
 int _bt_mesh_network_load(const char *app_cred,
                const char *sender, const char *token);
 
+int _bt_mesh_network_load_cdb(int result, const char *sender,
+               const char *app_creds, uint8_t uuid[16],
+                       uint8_t token[8], char **network);
+
 int _bt_mesh_network_request_provisioning_data_request(
                uint8_t net_uuid[], uint8_t count);
 
@@ -104,6 +108,21 @@ int _bt_mesh_network_get_appkeys(const char *app_cred, const char *sender,
                bluetooth_mesh_network_t *network,  uint16_t net_idx,
                        GArray **out_param);
 
+int _bt_mesh_network_handle_netkey_added(uint8_t net_uuid[],
+               uint16_t netkey_idx);
+
+int _bt_mesh_network_handle_netkey_deleted(uint8_t net_uuid[],
+               uint16_t netkey_idx);
+
+int _bt_mesh_network_handle_netkey_updated(uint8_t net_uuid[],
+               uint16_t netkey_idx);
+
+int _bt_mesh_network_handle_appkey_added(uint8_t net_uuid[],
+               uint16_t netkey_idx, uint16_t appkey_idx);
+
+int _bt_mesh_network_handle_appkey_deleted(uint8_t net_uuid[],
+               uint16_t netkey_idx, uint16_t appkey_idx);
+
 int _bt_mesh_network_get_nodes(const char *app_cred, const char *sender,
                bluetooth_mesh_network_t *network,  GArray **out_param);
 
index cb11a5b..00418a9 100644 (file)
@@ -1406,7 +1406,7 @@ fail:
        return false;
 }
 
-_bt_mesh_cdb_t* _bt_mesh_cdb_load(const char *file_name,
+_bt_mesh_cdb_t* _bt_mesh_conf_load(const char *file_name,
                const char *token)
 {
        char *token_str = NULL;
@@ -1475,7 +1475,7 @@ fail:
        return NULL;
 }
 
-bool _bt_mesh_cdb_load_nodes(_bt_mesh_cdb_t *cfg)
+bool _bt_mesh_conf_load_all_nodes(_bt_mesh_cdb_t *cfg)
 {
        json_object *jnodes;
        int i, sz, node_count = 0;
index 3ba340e..79f81ce 100644 (file)
@@ -40,7 +40,7 @@
 #include "bt-service-mesh-util.h"
 #include "bt-service-mesh-network.h"
 #include "bt-service-mesh-nodes.h"
-//#include "bt-service-mesh-config-client.h"
+#include "bt-service-mesh-config-client.h"
 
 #include <oal-mesh.h>
 
@@ -122,12 +122,27 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                        BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1);
 
                        if (!g_strcmp0(network->token.token, token_str)) {
-
+                               char *network_name = NULL;
                                BT_INFO("Mesh: BT_MESH_NETWORK_LOAD Request found Token [%s]",
                                        token_str);
 
-                               /* TODO: Handle Meh Network Load CDB */
                                /* Send request to mesh-network to load keys and Nodes for the network */
+                               if (BLUETOOTH_ERROR_NONE == _bt_mesh_network_load_cdb(
+                                               result, req_info->sender, network->app_cred,
+                                                       node->uuid, node->token.u8, &network_name)) {
+                                       g_strlcpy(network->name.name, network_name, strlen(network_name));
+                               } else
+                                       BT_ERR("!!Mesh: BT_MESH_NETWORK_LOAD Failed!!");
+
+                               _bt_mesh_util_convert_hex_to_string((uint8_t *) node->uuid, 16, network->uuid,
+                                               BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+
+                               out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+                               g_array_append_vals(out_param, network, sizeof(bluetooth_mesh_network_t));
+                               _bt_service_method_return(req_info->context, out_param, result);
+                               _bt_free_info_from_invocation_list(req_info);
+                               g_free(req_info->user_data);
+                               g_array_free(out_param, TRUE);
                        }
                        break;
                }
@@ -220,15 +235,13 @@ static void __bt_mesh_handle_pending_request_info(int result,
 
                        event = (event_mesh_appkey_operation_t*) param;
                        network = (bluetooth_mesh_network_t*)req_info->user_data;
-                       _bt_mesh_util_convert_hex_to_string(
-                               (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
-                                       BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
+                       _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
+                               BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
 
-                       BT_DBG("Request Sender: [%s]", req_info->sender);
+                        BT_DBG("Request Sender: [%s]", req_info->sender);
                        if (!g_strcmp0(network->uuid, net_uuid)) {
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
-                               g_array_append_vals(out_param,
-                                       &event->app_idx, sizeof(guint16));
+                               g_array_append_vals(out_param, &event->app_idx, sizeof(guint16));
                                _bt_service_method_return(req_info->context, out_param, result);
                                _bt_free_info_from_invocation_list(req_info);
                                g_free(req_info->user_data);
@@ -243,10 +256,68 @@ static void __bt_mesh_handle_pending_request_info(int result,
        }
 }
 
+static void __handle_mesh_network_subnet_operation_event(
+               event_mesh_netkey_operation_t *event)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+
+       if (event->status != OAL_STATUS_SUCCESS)
+               result = BLUETOOTH_ERROR_INTERNAL;
+
+       /* Handle DBUS Context return */
+       if (event->op == OAL_MESH_KEY_ADD) {
+               if (result == BLUETOOTH_ERROR_NONE)
+                       _bt_mesh_network_handle_netkey_added(event->net_uuid.uuid, event->key_idx);
+
+               __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_ADD_NETKEY,
+                               event, sizeof(event_mesh_netkey_operation_t));
+       } else if (event->op == OAL_MESH_KEY_DELETE) {
+               if (result == BLUETOOTH_ERROR_NONE)
+                       _bt_mesh_network_handle_netkey_deleted(event->net_uuid.uuid, event->key_idx);
+
+               __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_DELETE_NETKEY,
+                               event, sizeof(event_mesh_netkey_operation_t));
+       } else if (event->op == OAL_MESH_KEY_UPDATE) {
+               _bt_mesh_network_handle_netkey_updated(event->net_uuid.uuid, event->key_idx);
+
+               __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_UPDATE_NETKEY,
+                               event, sizeof(event_mesh_netkey_operation_t));
+       }
+}
+
+static void __handle_mesh_network_appkey_operation_event(
+                       event_mesh_appkey_operation_t *event)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+
+       if (event->status != OAL_STATUS_SUCCESS)
+               result = BLUETOOTH_ERROR_INTERNAL;
+
+       /* Handle DBUS Context return */
+       if (event->op == OAL_MESH_KEY_ADD) {
+               if (result == BLUETOOTH_ERROR_NONE)
+                       _bt_mesh_network_handle_appkey_added(
+                                       event->net_uuid.uuid, event->net_idx, event->app_idx);
+
+               __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) {
+               if (result == BLUETOOTH_ERROR_NONE)
+                       _bt_mesh_network_handle_appkey_deleted(
+                                       event->net_uuid.uuid, event->net_idx, event->app_idx);
+
+               __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_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_UPDATE_APPKEY,
+                               event, sizeof(event_mesh_netkey_operation_t));
+       }
+}
+
 static void  __handle_mesh_devkey_message_received_event(
                event_mesh_devkey_message_t *event)
 {
-       /* TODO Handle Devkey message Handler */
+       _bt_mesh_config_client_devkey_msg_handler(event);
 }
 
 static void __handle_mesh_network_attached_event(
@@ -410,8 +481,12 @@ static void __handle_mesh_network_provisioning_finished_event(
                        BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED,
                        param);
 
-       /* TODO Add newly provisioned Node info in Node Manager */
        /* Add Remote Node entry in Local CDB */
+       if (event->status == OAL_STATUS_SUCCESS) {
+               _bt_mesh_network_add_remote_node(
+                       event->net_uuid.uuid, event->dev_uuid.uuid,
+                               event->unicast, event->count);
+       }
 }
 
 static void __handle_mesh_network_provisioning_data_requested_event(
@@ -550,11 +625,13 @@ static void __handle_mesh_events(int event_type,
                BT_PERMANENT_LOG("Mesh: Network Authentication Requested");
                break;
        case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT:
-               /* TODO: Handle Netkey key DBUS Handler & event */
+               __handle_mesh_network_subnet_operation_event(
+                       (event_mesh_netkey_operation_t*)event_data);
                BT_PERMANENT_LOG("Mesh: Network Subnet operation event");
                break;
        case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT:
-               /* TODO: Handle Netkey key DBUS Handler & event */
+                __handle_mesh_network_appkey_operation_event(
+                       (event_mesh_appkey_operation_t*)event_data);
                BT_PERMANENT_LOG("Mesh: AppKey operation event");
                break;
        case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED:
index 5968fc5..befaf00 100644 (file)
@@ -558,6 +558,117 @@ int _bt_mesh_element_get_models(const char *app_cred, const char *sender,
                return BLUETOOTH_ERROR_INTERNAL;
 }
 
+int _bt_mesh_network_handle_netkey_added(
+               uint8_t net_uuid[], uint16_t netkey_idx)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, net_uuid,
+                       __mesh_compare_app_network_uuid);
+       if (!l)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       _bt_mesh_keys_add_net_key(cdb_cfg->uuid, netkey_idx);
+
+       if (!_bt_mesh_conf_insert_network_key(cdb_cfg,
+                       netkey_idx, MESH_KEY_REFRESH_PHASE_NONE))
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_mesh_network_handle_netkey_deleted(
+       uint8_t net_uuid[], uint16_t netkey_idx)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, net_uuid,
+               __mesh_compare_app_network_uuid);
+       if (!l)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       _bt_mesh_keys_del_net_key(cdb_cfg->uuid, netkey_idx, cdb_cfg);
+
+       if (!_bt_mesh_conf_delete_network_key(cdb_cfg, netkey_idx))
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_mesh_network_handle_netkey_updated(
+               uint8_t net_uuid[], uint16_t netkey_idx)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, net_uuid,
+                       __mesh_compare_app_network_uuid);
+       if (!l)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       _bt_mesh_keys_set_net_key_phase(cdb_cfg,
+               netkey_idx, MESH_KEY_REFRESH_PHASE_ONE, true);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_mesh_network_handle_appkey_added(uint8_t net_uuid[],
+               uint16_t netkey_idx, uint16_t appkey_idx)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, net_uuid,
+                       __mesh_compare_app_network_uuid);
+       if (!l)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       _bt_mesh_keys_add_app_key(cdb_cfg->uuid,
+                       netkey_idx, appkey_idx);
+
+       if (!_bt_mesh_conf_insert_application_key(cdb_cfg,
+                               netkey_idx, appkey_idx))
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_mesh_network_handle_appkey_deleted(uint8_t net_uuid[],
+               uint16_t netkey_idx, uint16_t appkey_idx)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, net_uuid,
+               __mesh_compare_app_network_uuid);
+       if (!l)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       _bt_mesh_keys_del_app_key(cdb_cfg->uuid, appkey_idx);
+
+       if (!_bt_mesh_conf_delete_application_key(cdb_cfg, appkey_idx))
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
 int _bt_mesh_network_set_name(const char *app_cred, const char *sender,
        bluetooth_mesh_network_t *network)
 {
@@ -1023,6 +1134,56 @@ bool _bt_mesh_node_get_vendor_features(uint8_t net_uuid[],
                        &feats->features.proxy, &feats->features.lpn);
 }
 
+int _bt_mesh_network_load_cdb(int result, const char *sender,
+               const char *app_creds,
+                       uint8_t uuid[16], uint8_t token[8],
+                                        char **network)
+{
+       GSList *l;
+       char *token_str;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+
+       token_str = _bt_service_convert_hex_to_string(token, 8);
+
+       /* Find CDB */
+       l = g_slist_find_custom(cdb_list, token_str,
+                       __mesh_compare_app_cdb_token);
+       if (!l)
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       g_free(token_str);
+       cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+
+       if (result != BLUETOOTH_ERROR_NONE)
+               goto failed;
+
+       /* Create new network for saving network specific Keys */
+       _bt_mesh_keys_load_net(cdb_cfg->uuid);
+       if (!_bt_mesh_conf_load_all_keys(cdb_cfg)) {
+               _bt_mesh_keys_unload_net(cdb_cfg);
+               goto failed;
+       }
+
+       /* Create new network for saving network specific nodes */
+       _bt_mesh_node_load_net(cdb_cfg->uuid);
+       if (!_bt_mesh_conf_load_all_nodes(cdb_cfg)) {
+               _bt_mesh_node_unload_net(cdb_cfg);
+               goto failed;
+       }
+
+       /* Load Groups Created */
+       cdb_cfg->groups = _bt_mesh_conf_load_group_info(cdb_cfg);
+
+       /* Fetch Network name */
+       *network = (char*)_bt_mesh_conf_get_network_friendly_name(cdb_cfg);
+       BT_INFO("Mesh: Attached Network [%s]", *network);
+       return BLUETOOTH_ERROR_NONE;
+failed:
+       /* Free the CDB object */
+       _bt_mesh_conf_free(cdb_cfg);
+       return BLUETOOTH_ERROR_INTERNAL;
+}
+
 int _bt_mesh_network_load(const char *app_cred,
        const char *sender, const char *token)
 {
@@ -1067,7 +1228,7 @@ int _bt_mesh_network_load(const char *app_cred,
                                continue;
 
                        BT_INFO("Mesh: File name [%s]", filename);
-                       cdb_cfg = _bt_mesh_cdb_load(filename, token);
+                       cdb_cfg = _bt_mesh_conf_load(filename, token);
                        if (cdb_cfg)
                                break;
                }
index f0d6099..489cdb4 100644 (file)
@@ -99,6 +99,25 @@ static bool __mesh_net_uuid_match(const void *a, const void *b)
        return false;
 }
 
+static bool __mesh_remove_node_entry(void *a, void *b)
+{
+       uint8_t num_ele, i;
+       struct _bt_mesh_node_t *rmt = (struct _bt_mesh_node_t*) a;
+
+       num_ele = rmt->num_ele;
+
+       for (i = 0; i < num_ele; ++i)
+               l_queue_destroy(rmt->els[i], NULL);
+
+       l_free(rmt->els);
+
+       l_queue_destroy(rmt->net_keys, l_free);
+       l_queue_destroy(rmt->app_keys, l_free);
+
+       l_free(rmt);
+       return true;
+}
+
 static bool __mesh_key_present(struct l_queue *keys, uint16_t app_idx)
 {
        const struct l_queue_entry *l;
@@ -178,6 +197,24 @@ void _bt_mesh_node_load_net(uint8_t net_uuid[])
        l_queue_push_tail(networks, network);
 }
 
+void _bt_mesh_node_unload_net(_bt_mesh_cdb_t *cfg)
+{
+       struct mesh_network_t *network;
+       int numnodes;
+       BT_INFO("Mesh:Nodes: Unload network with all nodes");
+
+       network = l_queue_find(networks, __mesh_net_uuid_match, cfg->uuid);
+       if (!network || !network->nodes)
+               return;
+
+       /* Only remove the ndoe from the Local Network object:
+          DONT touch configuration file */
+       numnodes = l_queue_foreach_remove(network->nodes,
+                       __mesh_remove_node_entry, cfg);
+       BT_INFO("Mesh:Nodes: Unloadeded [%d] Nodes from the network", numnodes);
+       l_free(network);
+}
+
 bool _bt_mesh_node_get_unicast_from_dev_uuid(uint8_t net_uuid[],
                const uint8_t dev_uuid[], uint16_t *unicast)
 {