Apply send_destination_prefix policy#2 28/198128/4
authorWootak Jung <wootak.jung@samsung.com>
Mon, 21 Jan 2019 07:39:49 +0000 (16:39 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Tue, 22 Jan 2019 01:49:47 +0000 (10:49 +0900)
gdbus closing logic is not needed anymore

Change-Id: If529a348bee88dfe413fd936fcf07296eb6ae8d0

bt-api/bt-gatt-service.c
bt-oal/bluez_hal/bluetooth-frwk-gap-agent.conf
bt-oal/bluez_hal/src/bt-hal-gatt-server.c

index 1d30065..f7d73ec 100644 (file)
@@ -430,34 +430,6 @@ static int bluetooth_gatt_write_characteristics_value_to_fd_(
                return att_result;
 }
 
-static void __bt_gatt_close_gdbus_connection(void)
-{
-       GError *err = NULL;
-
-       BT_DBG("+");
-
-       ret_if(g_conn == NULL);
-
-       if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
-               BT_ERR("Fail to flush the connection: %s", err->message);
-               g_error_free(err);
-               err = NULL;
-       }
-
-       if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
-               if (err) {
-                       BT_ERR("Fail to close the dbus connection: %s", err->message);
-                       g_error_free(err);
-               }
-       }
-
-       g_object_unref(g_conn);
-
-       g_conn = NULL;
-
-       BT_DBG("-");
-}
-
 #ifdef TIZEN_FEATURE_BT_HPS
 static int __bt_send_event_to_hps(int event, GVariant *var)
 {
@@ -1427,15 +1399,9 @@ static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
        GDBusProxy *proxy;
        GError *err = NULL;
 
-       if (g_conn == NULL)
-               g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
-                                                       NULL, &err);
-
+       g_conn = _bt_init_system_gdbus_conn();
        if (!g_conn) {
-               if (err) {
-                       BT_ERR("Unable to connect to gdbus: %s", err->message);
-                       g_clear_error(&err);
-               }
+               BT_ERR("Unable to get connection");
                return NULL;
        }
 
@@ -1605,67 +1571,8 @@ BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
-static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
-{
-       GDBusConnection *local_system_gconn = NULL;
-       char *address;
-       GError *err = NULL;
-
-       if (g_conn == NULL) {
-               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-               if (address == NULL) {
-                       if (err) {
-                               BT_ERR("Failed to get bus address: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-
-               g_conn = g_dbus_connection_new_for_address_sync(address,
-                                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                                       NULL, /* GDBusAuthObserver */
-                                       NULL,
-                                       &err);
-               g_free(address);
-               if (!g_conn) {
-                       if (err) {
-                               BT_ERR("Unable to connect to dbus: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-       } else if (g_dbus_connection_is_closed(g_conn)) {
-               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-               if (address == NULL) {
-                       if (err) {
-                               BT_ERR("Failed to get bus address: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-
-               local_system_gconn = g_dbus_connection_new_for_address_sync(address,
-                                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                                       NULL, /* GDBusAuthObserver */
-                                       NULL,
-                                       &err);
-               g_free(address);
-               if (!local_system_gconn) {
-                       BT_ERR("Unable to connect to dbus: %s", err->message);
-                       g_clear_error(&err);
-               }
-
-               g_conn = local_system_gconn;
-       }
-
-       return g_conn;
-}
-
 BT_EXPORT_API int bluetooth_gatt_init(void)
 {
-       GDBusConnection *conn;
        GError *error = NULL;
        GDBusNodeInfo *node_info = NULL;
 
@@ -1674,24 +1581,27 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
                return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
        }
 
+       g_conn = _bt_init_system_gdbus_conn();
+       if (!g_conn) {
+               BT_ERR("Unable to get connection");
+               goto failed;
+       }
+
        if (owner_id == 0) {
-               owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-                                       BT_GATT_SERVICE_NAME,
+               gchar *name = g_strdup_printf("%s.p%d", BT_GATT_SERVICE_NAME, getpid());
+               BT_DBG("well-known name: %s", name);
+
+               owner_id = g_bus_own_name_on_connection(g_conn, name,
                                        G_BUS_NAME_OWNER_FLAGS_NONE,
-                                       NULL, NULL, NULL, NULL, NULL);
+                                       NULL, NULL, NULL, NULL);
+               g_free(name);
        }
-
        BT_DBG("owner_id is [%d]", owner_id);
+
        app_path = g_strdup_printf("/com/%d", getpid());
 
        serv_id = 1;
 
-       conn = __bt_gatt_get_gdbus_connection();
-       if (!conn) {
-               BT_ERR("Unable to get connection");
-               goto failed;
-       }
-
        /* Register ObjectManager interface */
        node_info = __bt_gatt_create_method_node_info(
                                        manager_introspection_xml);
@@ -1726,8 +1636,6 @@ failed:
        app_path = NULL;
        owner_id = 0;
 
-       __bt_gatt_close_gdbus_connection();
-
        return BLUETOOTH_ERROR_INTERNAL;
 }
 
@@ -1743,7 +1651,6 @@ BT_EXPORT_API int bluetooth_gatt_deinit()
 
        if (owner_id == 0) {
                BT_ERR("owner_id is zero");
-               __bt_gatt_close_gdbus_connection();
                return BLUETOOTH_ERROR_NOT_FOUND;
        }
 
@@ -1772,8 +1679,6 @@ BT_EXPORT_API int bluetooth_gatt_deinit()
                manager_gproxy = NULL;
        }
 
-       __bt_gatt_close_gdbus_connection();
-
        BT_DBG("-");
        return ret;
 }
index aedbd95..29f6a17 100644 (file)
@@ -6,24 +6,28 @@
         <allow own_prefix="org.bt.frwk"/>
         <allow send_interface="org.bluez.Profile1" send_member="NewConnection"/>
         <allow send_interface="org.bluez.Profile1" send_member="RequestDisconnection"/>
-        <!-- No send_destination because private connection does not have well-known service name -->
-        <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
-        <allow send_interface="org.bluez.GattCharacteristic1"/>
-        <allow send_interface="org.bluez.GattDescriptor1"/>
+        <allow own_prefix="org.frwk.gatt_service"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.freedesktop.DBus.Properties"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.freedesktop.DBus.ObjectManager"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.bluez.GattCharacteristic1"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.bluez.GattDescriptor1"/>
     </policy>
     <policy group="network_fw">
         <!-- service name(org.bt.frwk.p<pid>) is changeable by pid -->
         <allow own_prefix="org.bt.frwk"/>
         <allow send_interface="org.bluez.Profile1" send_member="NewConnection"/>
         <allow send_interface="org.bluez.Profile1" send_member="RequestDisconnection"/>
-        <!-- No send_destination because private connection does not have well-known service name -->
-        <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
-        <allow send_interface="org.bluez.GattCharacteristic1"/>
-        <allow send_interface="org.bluez.GattDescriptor1"/>
+        <allow own_prefix="org.frwk.gatt_service"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.freedesktop.DBus.Properties"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.freedesktop.DBus.ObjectManager"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.bluez.GattCharacteristic1"/>
+        <allow send_destination_prefix="org.frwk.gatt_service" send_interface="org.bluez.GattDescriptor1"/>
     </policy>
     <policy context="default">
         <deny own_prefix="org.bt.frwk"/>
         <deny send_interface="org.bluez.Profile1" send_member="NewConnection"/>
         <deny send_interface="org.bluez.Profile1" send_member="RequestDisconnection"/>
+        <deny own_prefix="org.frwk.gatt_service"/>
+        <deny send_destination_prefix="org.frwk.gatt_service"/>
     </policy>
 </busconfig>
index a45aad0..f35bc0c 100644 (file)
@@ -1880,89 +1880,6 @@ static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr,
        return BT_STATUS_SUCCESS;
 }
 
-static void __bt_gatt_close_gdbus_connection(void)
-{
-       GError *err = NULL;
-       DBG("+");
-
-       if (g_conn == NULL)
-               return;
-
-       if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
-               ERR("Fail to flush the connection: %s", err->message);
-               g_error_free(err);
-               err = NULL;
-       }
-
-       if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
-               if (err) {
-                       ERR("Fail to close the dbus connection: %s", err->message);
-                       g_error_free(err);
-               }
-       }
-
-       g_object_unref(g_conn);
-       g_conn = NULL;
-       DBG("-");
-}
-
-static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
-{
-       GDBusConnection *local_system_gconn = NULL;
-       char *address;
-       GError *err = NULL;
-
-       if (g_conn == NULL) {
-               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-               if (address == NULL) {
-                       if (err) {
-                               ERR("Failed to get bus address: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-
-               g_conn = g_dbus_connection_new_for_address_sync(address,
-                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                               G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                               NULL, /* GDBusAuthObserver */
-                               NULL,
-                               &err);
-               if (!g_conn) {
-                       if (err) {
-                               ERR("Unable to connect to dbus: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-       } else if (g_dbus_connection_is_closed(g_conn)) {
-               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-               if (address == NULL) {
-                       if (err) {
-                               ERR("Failed to get bus address: %s", err->message);
-                               g_clear_error(&err);
-                       }
-                       return NULL;
-               }
-
-               local_system_gconn = g_dbus_connection_new_for_address_sync(address,
-                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                               G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                               NULL, /* GDBusAuthObserver */
-                               NULL,
-                               &err);
-
-               if (!local_system_gconn) {
-                       ERR("Unable to connect to dbus: %s", err->message);
-                       g_clear_error(&err);
-               }
-
-               g_conn = local_system_gconn;
-       }
-
-       return g_conn;
-}
-
 static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
 {
        char uuid_buf[5];
@@ -2142,43 +2059,45 @@ static void __bt_hal_gatt_deinit(char *app_path)
        if (gatt_server_apps == NULL) {
                INFO("All GATT servers are removed, clean all DBUS resources");
                if (owner_id) {
-                       /* unregister the exported interface for object manager
-                          g_conn and manager_id are common for all GATT servers */
-                       g_dbus_connection_unregister_object(g_conn,
-                                       manager_id);
+                       /* unregister the exported interface for object manager */
+                       g_dbus_connection_unregister_object(g_conn, manager_id);
                        manager_id = 0;
                        g_bus_unown_name(owner_id);
                        owner_id = 0;
 
                        g_object_unref(manager_gproxy);
                        manager_gproxy = NULL;
-
-                       /* Close the GDBUS connection */
-                       __bt_gatt_close_gdbus_connection();
                }
        }
        INFO("-");
 }
 
-int __bt_hal_gatt_init(void)
+static int __bt_hal_gatt_init(void)
 {
-       GDBusConnection *conn = NULL;
        DBG("+");
+
        /* Only once for ALL GATT Servers */
        if (owner_id == 0) {
-               owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-                               BT_GATT_SERVICE_NAME,
-                               G_BUS_NAME_OWNER_FLAGS_NONE,
-                               NULL, NULL, NULL, NULL, NULL);
-       }
-       INFO("Owner ID [%d]", owner_id);
+               GError *err = NULL;
+               gchar *name = NULL;
 
-       /* Only once for ALL GATT Servers conn = g_conn(global)*/
-       conn = __bt_gatt_get_gdbus_connection();
-       if (!conn) {
-               ERR("Unable to get connection");
-               return BT_STATUS_FAIL;
+               g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               if (!g_conn) {
+                       if (err) {
+                               ERR("Unable to connect to gdbus: %s", err->message);
+                               g_clear_error(&err);
+                       }
+                       return BT_STATUS_FAIL;
+               }
+
+               name = g_strdup_printf("%s.p%d", BT_GATT_SERVICE_NAME, getpid());
+               INFO("well-known name: %s", name);
+
+               owner_id = g_bus_own_name_on_connection(g_conn, name,
+                                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                                       NULL, NULL, NULL, NULL);
        }
+       INFO("Owner ID [%d]", owner_id);
 
        /* Only once for ALL GATT Servers */
        if (manager_node_info == NULL) {
@@ -2234,7 +2153,6 @@ static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc
        char *app_path = NULL;
        GError *error = NULL;
        int *app_id = NULL;
-       guint manager_id;
 
        int result = BT_STATUS_SUCCESS;
 
@@ -2269,6 +2187,7 @@ static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc
                        g_error_free(error);
                        goto failed;
                }
+               INFO("manager_id [%d]", manager_id);
 
                /* For current GATT Server, app_path is created, save it in Table */
                _bt_hal_update_gatt_server_path(server_if, app_path);