Use vendor adv command only when multipled adv is supported
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-server.c
index b2313dd..447da05 100644 (file)
@@ -322,7 +322,7 @@ static const gchar manager_introspection_xml[] =
 "  </interface>"
 "</node>";
 
-GSList *_bt_get_service_list_from_server(int instance)
+static GSList *_bt_get_service_list_from_server(int instance)
 {
        GSList *l;
        INFO("Number of GATT Server apps [%d]", g_slist_length(gatt_server_apps));
@@ -340,7 +340,7 @@ GSList *_bt_get_service_list_from_server(int instance)
        return NULL;
 }
 
-void _bt_remote_service_from_gatt_server(int instance, int service_handle)
+static void _bt_remote_service_from_gatt_server(int instance, int service_handle)
 {
        GSList *l;
        GSList *l1;
@@ -360,7 +360,7 @@ void _bt_remote_service_from_gatt_server(int instance, int service_handle)
        }
 }
 
-void _bt_hal_update_gatt_service_in_gatt_server(int slot, struct gatt_service_info *serv_info)
+static void _bt_hal_update_gatt_service_in_gatt_server(int slot, struct gatt_service_info *serv_info)
 {
        GSList *l;
        for (l = gatt_server_apps; l; l = g_slist_next(l)) {
@@ -535,7 +535,7 @@ static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const cha
        return NULL;
 }
 
-char *__bt_gatt_find_char_path_from_handle(int char_hdl)
+static char *__bt_gatt_find_char_path_from_handle(int char_hdl)
 {
        GSList *l1, *l2;
 
@@ -553,7 +553,7 @@ char *__bt_gatt_find_char_path_from_handle(int char_hdl)
        return NULL;
 }
 
-struct gatt_char_info *__bt_gatt_find_char_info_from_handle(int char_hdl)
+static struct gatt_char_info *__bt_gatt_find_char_info_from_handle(int char_hdl)
 {
        GSList *l1, *l2;
 
@@ -737,11 +737,16 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                for (i = 0; i < char_info->flags_length; i++) {
                                        g_variant_builder_add(builder2, "s",
                                                        char_info->char_flags[i]);
-                                       if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0)
+
+                                       if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0) {
+                                               DBG("setting WriteAcquired property");
                                                writeAcquired = TRUE;
+                                       }
 
-                                       if (strncmp(char_info->char_flags[i], "notify", 4) == 0)
+                                       if (strncmp(char_info->char_flags[i], "notify", 4) == 0) {
+                                               DBG("setting NotifyAcquired property");
                                                notifyAcquired = TRUE;
+                                       }
                                }
 
                                flags_val = g_variant_new("as", builder2);
@@ -752,13 +757,19 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                g_variant_builder_add(inner_builder, "{sv}", "Notifying",
                                                g_variant_new("b", notify));
 
-                               /* Unicast */
-                               g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
+                               /* WriteAcquired */
+                               if (writeAcquired == TRUE) {
+                                       DBG("adding WriteAcquired property");
+                                       g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
                                                                        g_variant_new("b", writeAcquired));
+                               }
 
                                /* NotifyAcquired */
-                                                       g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
-                                                                       g_variant_new("b", notifyAcquired));
+                               if (notifyAcquired == TRUE) {
+                                       DBG("adding NotifyAcquired property");
+                                       g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
+                                                       g_variant_new("b", notifyAcquired));
+                               }
 
                                /* Unicast */
                                unicast = g_strdup("00:00:00:00:00:00");
@@ -951,6 +962,7 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
                req_info->attr_path = g_strdup(object_path);
                req_info->svc_path = g_strdup(svc_info->serv_path);
                req_info->request_id = req_id;
+               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_READ;
                req_info->offset = offset;
                req_info->context = invocation;
 
@@ -1022,6 +1034,7 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
                        req_info->attr_path = g_strdup(object_path);
                        req_info->svc_path = g_strdup(svc_info->serv_path);
                        req_info->request_id = req_id;
+                       req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_WRITE;
                        req_info->offset = offset;
                        req_info->context = invocation;
 
@@ -1426,7 +1439,7 @@ done:
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
-gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
+static gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
 {
        gboolean ret;
        GError *error = NULL;
@@ -1713,7 +1726,7 @@ static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
        hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
 
        /* Check if slot available */
-       server_if = _bt_hal_get_available_adv_slot_id(uuid);
+       server_if = _bt_hal_get_available_adv_slot_id(uuid, FALSE);
 
        if (server_if == -1) {
                ERR("Allocation of server instance failed");
@@ -1735,7 +1748,7 @@ static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
        return BT_STATUS_SUCCESS;
 }
 
-void _bt_hal_remove_gatt_server_from_list(int server_if)
+static void _bt_hal_remove_gatt_server_from_list(int server_if)
 {
        GSList *l;
        struct gatt_server_app *info = NULL;
@@ -1880,89 +1893,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];
@@ -1981,7 +1911,7 @@ static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
                return strdup(uuid);
 }
 
-int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
+static int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
 {
        gboolean ret;
        /* For GATT service specific */
@@ -2142,43 +2072,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) {
@@ -2196,7 +2128,7 @@ int __bt_hal_gatt_init(void)
        return BT_STATUS_SUCCESS;
 }
 
-void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
+static void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
 {
        GSList *l;
 
@@ -2213,7 +2145,7 @@ void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
        *app_path = NULL;
 }
 
-void _bt_hal_update_gatt_server_path(int slot, char *app_path)
+static void _bt_hal_update_gatt_server_path(int slot, char *app_path)
 {
        if (app_path == NULL)
                return;
@@ -2234,7 +2166,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 +2200,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);
@@ -2965,7 +2897,8 @@ static bt_status_t gatt_server_send_indication(int server_if, int attribute_hand
        g_variant_builder_add(outer_builder, "{sv}", "Notifying",
                        g_variant_new("b", notify));
 
-       _bt_hal_convert_addr_type_to_string(addr, (unsigned char *)conn_info->addr);
+       memcpy(addr, conn_info->addr, BT_HAL_ADDRESS_STRING_SIZE);
+       DBG("Send Indication to Unicast addr [%s]", addr);
 
        g_variant_builder_add(outer_builder, "{sv}", "Unicast",
                        g_variant_new("s", addr));
@@ -3104,7 +3037,7 @@ static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
 
        /* DEBUG */
        for (i = 0; i < response->attr_value.len; i++)
-               DBG("Resonse [%d] = [0x%x]", response->attr_value.value[i]);
+               DBG("Resonse [%d] = [0x%x]", i, response->attr_value.value[i]);
 
        if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
                GVariantBuilder *inner_builder = NULL;