Handle bluetooth-meshd life cycle
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-main.c
index 79f81ce..167b3ba 100644 (file)
@@ -29,6 +29,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <ell/ell.h>
+#include <actd/unit_control.h>
 
 #include "bluetooth-api.h"
 #include "bt-internal-types.h"
@@ -44,6 +45,8 @@
 
 #include <oal-mesh.h>
 
+#define MESH_SYSTEMD_SERVICE_NAME "bluetooth-mesh.service"
+
 /* Event handlers */
 static void __bt_mesh_handle_pending_request_info(int result,
                int service_function, void *param,
@@ -101,7 +104,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                _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;
@@ -141,7 +143,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                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;
@@ -165,7 +166,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                _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;
@@ -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)) {
@@ -195,7 +202,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                        sizeof(bluetooth_mesh_provisioning_request_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,7 +226,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                g_array_append_vals(out_param, &event->key_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);
                                g_array_free(out_param, TRUE);
                        }
                        break;
@@ -244,7 +249,6 @@ static void __bt_mesh_handle_pending_request_info(int result,
                                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);
                                g_array_free(out_param, TRUE);
                        }
                        break;
@@ -292,9 +296,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 +306,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 +314,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 +395,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 +429,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 +442,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 +456,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 +493,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 +531,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 +597,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 +623,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");
@@ -646,8 +671,18 @@ static void __handle_mesh_events(int event_type,
 
 int _bt_mesh_init(void)
 {
+       int ret = UNIT_CONTROL_OK;
        oal_status_t status = OAL_STATUS_SUCCESS;
 
+       /* Launch bluetooth-meshd */
+       ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
+                               MESH_SYSTEMD_SERVICE_NAME, 5000);
+
+       if (ret != UNIT_CONTROL_OK) {
+               BT_ERR("Failed to call systemact service: %d", ret);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
        status = mesh_enable();
        if (OAL_STATUS_SUCCESS != status) {
                BT_ERR("Mesh: Failed to initialize Mesh profile, status: %d",
@@ -666,6 +701,14 @@ int _bt_mesh_init(void)
 int _bt_mesh_deinit(void)
 {
        oal_status_t status = OAL_STATUS_SUCCESS;
+       int ret = UNIT_CONTROL_OK;
+
+       /* Terminate bluetooth-meshd */
+       ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
+                       MESH_SYSTEMD_SERVICE_NAME, 5000);
+
+       if (ret != UNIT_CONTROL_OK)
+               BT_ERR("Failed to call systemact service: %d", ret);
 
        status = mesh_disable();
        if (OAL_STATUS_SUCCESS != status) {