Added NULL pointer guard check before g_ascii_strcasecmp.
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-network.c
old mode 100755 (executable)
new mode 100644 (file)
index 30e02fd..bd065b2
@@ -1,11 +1,5 @@
 /*
- * Bluetooth-frwk
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
- *              Girishashok Joshi <girish.joshi@samsung.com>
- *              Chanyeol Park <chanyeol.park@samsung.com>
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  */
 
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus.h>
 #include <glib.h>
+#include <gio/gio.h>
 #include <dlog.h>
 #include <string.h>
 #include <stdio.h>
-#if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
 #include <syspopup_caller.h>
-#endif
 #include <net_connection.h>
 
 #include "bluetooth-api.h"
@@ -109,7 +100,10 @@ static connection_profile_h __bt_get_net_profile(void *connection,
                        if (g_strv_length(split_string) < 3)
                                continue;
 
-                       if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
+                       if (split_string[2] == NULL) {
+                               BT_ERR("__bt_get_net_profile Error Parameter are NULL..\n");
+                               continue;
+                       } else if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
                                BT_DBG("matched profile");
                                g_strfreev(split_string);
                                return profile_h;
@@ -128,7 +122,7 @@ int _bt_is_network_connected(void *connection, unsigned char *address,
        handle = __bt_get_net_profile(connection,
                                CONNECTION_ITERATOR_TYPE_CONNECTED,
                                address);
-       if(handle)
+       if (handle)
                *is_connected = TRUE;
        else
                *is_connected = FALSE;
@@ -136,93 +130,98 @@ int _bt_is_network_connected(void *connection, unsigned char *address,
        return BLUETOOTH_ERROR_NONE;
 }
 
-static void __bt_network_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
-                                   gpointer user_data)
+static void __bt_network_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
+                                       gpointer user_data)
 {
        GError *g_error = NULL;
-       char *device = NULL;
-       GArray *out_param1 = NULL;
-       GArray *out_param2 = NULL;
+       GVariant *out_param1 = NULL;
+       GVariant *reply = NULL;
        bluetooth_device_address_t device_addr = { {0} };
        int result = BLUETOOTH_ERROR_NONE;
        bt_function_data_t *func_data;
        request_info_t *req_info;
 
-       dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_STRING, &device, G_TYPE_INVALID);
-
+       reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
        g_object_unref(proxy);
 
-       func_data = user_data;
+       if (reply == NULL) {
+               BT_ERR("Network Connect Dbus Call Error");
+               if (g_error) {
+                       BT_ERR("Error: %s\n", g_error->message);
+                       g_clear_error(&g_error);
+               }
+               result = BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_unref(reply);
+       }
 
+       func_data = user_data;
        if (func_data == NULL) {
                /* Send reply */
                BT_ERR("func_data == NULL");
                goto done;
        }
 
+       BT_ERR("func_data->req_id: %d", func_data->req_id);
        req_info = _bt_get_request_info(func_data->req_id);
        if (req_info == NULL) {
                BT_ERR("req_info == NULL");
                goto done;
        }
 
-       if (g_error != NULL) {
-               BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
-               result = BLUETOOTH_ERROR_INTERNAL;
-       }
-
        if (req_info->context == NULL)
                goto done;
 
-       out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-
        _bt_convert_addr_string_to_type(device_addr.addr,
                                        func_data->address);
 
-       g_array_append_vals(out_param1, &device_addr,
-                               sizeof(bluetooth_device_address_t));
-       g_array_append_vals(out_param2, &result, sizeof(int));
+       out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
+                                                       &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
 
-       dbus_g_method_return(req_info->context, out_param1, out_param2);
-
-       g_array_free(out_param1, TRUE);
-       g_array_free(out_param2, TRUE);
+       g_dbus_method_invocation_return_value(req_info->context,
+                       g_variant_new("iv", result, out_param1));
 
        _bt_delete_request_list(req_info->req_id);
-done:
-       if (g_error)
-               g_error_free(g_error);
 
+done:
        if (func_data) {
                g_free(func_data->address);
                g_free(func_data);
        }
 }
 
-static void __bt_network_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
-                                   gpointer user_data)
+static void __bt_network_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
+                                       gpointer user_data)
 {
        GError *g_error = NULL;
-       GArray *out_param1 = NULL;
-       GArray *out_param2 = NULL;
+       GVariant *out_param1 = NULL;
+       GVariant *reply = NULL;
        bluetooth_device_address_t device_addr = { {0} };
        int result = BLUETOOTH_ERROR_NONE;
        bt_function_data_t *func_data;
        request_info_t *req_info;
 
-       dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
-
+       reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
        g_object_unref(proxy);
 
-       func_data = user_data;
+       if (reply == NULL) {
+               BT_ERR("Network Disconnect Dbus Call Error");
+               if (g_error) {
+                       BT_ERR("Error: %s\n", g_error->message);
+                       g_clear_error(&g_error);
+               }
+               result = BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_unref(reply);
+       }
 
+       func_data = user_data;
        if (func_data == NULL) {
                /* Send reply */
                BT_ERR("func_data == NULL");
                goto done;
        }
-
+       BT_ERR("func_data->req_id: %d", func_data->req_id);
        req_info = _bt_get_request_info(func_data->req_id);
        if (req_info == NULL) {
                BT_ERR("req_info == NULL");
@@ -237,26 +236,15 @@ static void __bt_network_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
        if (req_info->context == NULL)
                goto done;
 
-       out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
+       out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
+                                                       &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
 
-       _bt_convert_addr_string_to_type(device_addr.addr,
-                                       func_data->address);
-
-       g_array_append_vals(out_param1, &device_addr,
-                               sizeof(bluetooth_device_address_t));
-       g_array_append_vals(out_param2, &result, sizeof(int));
-
-       dbus_g_method_return(req_info->context, out_param1, out_param2);
-
-       g_array_free(out_param1, TRUE);
-       g_array_free(out_param2, TRUE);
+       g_dbus_method_invocation_return_value(req_info->context,
+                       g_variant_new("iv", result, out_param1));
 
        _bt_delete_request_list(req_info->req_id);
-done:
-       if (g_error)
-               g_error_free(g_error);
 
+done:
        if (func_data) {
                g_free(func_data->address);
                g_free(func_data);
@@ -267,19 +255,20 @@ int _bt_network_activate(void)
 {
        int ret = BLUETOOTH_ERROR_NONE;
        char *adapter_path;
+       GVariant *result = NULL;
        GError *err = NULL;
-       DBusGConnection *conn;
-       DBusGProxy *server_proxy;
+       GDBusConnection *conn;
+       GDBusProxy *server_proxy;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();
        retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
-                            adapter_path, BT_NETWORK_SERVER_INTERFACE);
-
+       server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_BLUEZ_NAME,
+                       adapter_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
        g_free(adapter_path);
 
        if (server_proxy == NULL) {
@@ -287,19 +276,25 @@ int _bt_network_activate(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       if (!dbus_g_proxy_call(server_proxy, "Register", &err,
-                       G_TYPE_STRING, NAP_UUID_NAME,
-                       G_TYPE_STRING, NET_BRIDGE_INTERFACE,
-                       G_TYPE_INVALID, G_TYPE_INVALID)) {
+       result = g_dbus_proxy_call_sync(server_proxy, "Register",
+                                g_variant_new("(ss)", NAP_UUID_NAME, NET_BRIDGE_INTERFACE),
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &err);
+       if (result == NULL) {
                if (err != NULL) {
+                       g_dbus_error_strip_remote_error(err);
                        BT_ERR("Network server register Error: %s\n", err->message);
-                       if (g_strcmp0(err->message, "Already Exists") == 0) {
+                       if (g_strcmp0(err->message, "Already Exists") == 0)
                                ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
-                       } else {
+                       else
                                ret = BLUETOOTH_ERROR_INTERNAL;
-                       }
+
                        g_error_free(err);
                }
+       } else {
+               g_variant_unref(result);
        }
 
        g_object_unref(server_proxy);
@@ -310,20 +305,21 @@ int _bt_network_activate(void)
 int _bt_network_deactivate(void)
 {
        char *adapter_path;
+       GVariant *result = NULL;
        GError *err = NULL;
-       DBusGConnection *conn;
-       DBusGProxy *server_proxy;
+       GDBusConnection *conn;
+       GDBusProxy *server_proxy;
        int ret = BLUETOOTH_ERROR_NONE;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();
        retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
-                            adapter_path, BT_NETWORK_SERVER_INTERFACE);
-
+       server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                                               NULL, BT_BLUEZ_NAME,
+                                                               adapter_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
        g_free(adapter_path);
 
        if (server_proxy == NULL) {
@@ -331,10 +327,15 @@ int _bt_network_deactivate(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       if (!dbus_g_proxy_call(server_proxy, "Unregister", &err,
-                       G_TYPE_STRING, NAP_UUID_NAME,
-                       G_TYPE_INVALID, G_TYPE_INVALID)) {
+       result = g_dbus_proxy_call_sync(server_proxy, "Unregister",
+                                g_variant_new("(s)", NAP_UUID_NAME),
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &err);
+       if (result == NULL) {
                if (err != NULL) {
+                       g_dbus_error_strip_remote_error(err);
                        BT_ERR("Network server unregister Error: %s\n", err->message);
                        if (g_strcmp0(err->message,
                                        "Operation currently not available") == 0) {
@@ -344,6 +345,8 @@ int _bt_network_deactivate(void)
                        }
                        g_error_free(err);
                }
+       } else {
+               g_variant_unref(result);
        }
 
        g_object_unref(server_proxy);
@@ -358,9 +361,9 @@ int _bt_network_connect(int request_id, int role,
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        char remote_role[BLUETOOTH_UUID_STRING_MAX] = { 0 };
        bt_function_data_t *func_data;
-       DBusGProxy *adapter_proxy;
-       DBusGProxy *profile_proxy;
-       DBusGConnection *conn;
+       GDBusProxy *adapter_proxy;
+       GDBusProxy *profile_proxy;
+       GDBusConnection *conn;
 
        BT_CHECK_PARAMETER(device_address, return);
 
@@ -384,38 +387,34 @@ int _bt_network_connect(int request_id, int role,
        adapter_proxy = _bt_get_adapter_proxy();
        retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
 
-       dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
-                         G_TYPE_STRING, address, G_TYPE_INVALID,
-                         DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
-
+       device_path = _bt_get_device_object_path(address);
        if (device_path == NULL) {
                BT_ERR("No paired device");
                return BLUETOOTH_ERROR_NOT_PAIRED;
        }
 
-       profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
-                                     device_path, BT_NETWORK_CLIENT_INTERFACE);
+       profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_BLUEZ_NAME,
+                       device_path, BT_NETWORK_CLIENT_INTERFACE,  NULL, NULL);
+
        g_free(device_path);
        retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        func_data = g_malloc0(sizeof(bt_function_data_t));
-
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
 
-       if (!dbus_g_proxy_begin_call(profile_proxy, "Connect",
-                       (DBusGProxyCallNotify)__bt_network_connect_cb,
-                       func_data, NULL,
-                       G_TYPE_STRING, remote_role,
-                       G_TYPE_INVALID)) {
-               BT_ERR("network connect Dbus Call Error");
-               g_object_unref(profile_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
+       g_dbus_proxy_call(profile_proxy, "Connect",
+                               g_variant_new("(s)", remote_role),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               BT_MAX_DBUS_TIMEOUT,
+                               NULL,
+                               (GAsyncReadyCallback)__bt_network_connect_cb,
+                               func_data);
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -426,45 +425,43 @@ int _bt_network_disconnect(int request_id,
        gchar *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_function_data_t *func_data;
-       DBusGProxy *adapter_proxy;
-       DBusGProxy *profile_proxy;
-       DBusGConnection *conn;
+       GDBusProxy *adapter_proxy;
+       GDBusProxy *profile_proxy;
+       GDBusConnection *conn;
 
        BT_CHECK_PARAMETER(device_address, return);
 
        adapter_proxy = _bt_get_adapter_proxy();
        retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
 
-       dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
-                         G_TYPE_STRING, address, G_TYPE_INVALID,
-                         DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
-
+       device_path = _bt_get_device_object_path(address);
        if (device_path == NULL) {
                BT_ERR("No paired device");
                return BLUETOOTH_ERROR_NOT_PAIRED;
        }
 
-       profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
-                                     device_path, BT_NETWORK_CLIENT_INTERFACE);
+       profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_BLUEZ_NAME,
+                       device_path, BT_NETWORK_CLIENT_INTERFACE,  NULL, NULL);
+
        g_free(device_path);
        retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        func_data = g_malloc0(sizeof(bt_function_data_t));
-
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
 
-       if (!dbus_g_proxy_begin_call(profile_proxy, "Disconnect",
-                       (DBusGProxyCallNotify)__bt_network_disconnect_cb,
-                       func_data, NULL, G_TYPE_INVALID)) {
-               BT_ERR("network disconnect Dbus Call Error");
-               g_object_unref(profile_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
+       g_dbus_proxy_call(profile_proxy, "Disconnect",
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       BT_MAX_DBUS_TIMEOUT,
+                       NULL,
+                       (GAsyncReadyCallback)__bt_network_disconnect_cb,
+                       func_data);
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -475,12 +472,12 @@ int _bt_network_server_disconnect(int request_id,
        gchar *adapter_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_function_data_t *func_data;
-       DBusGProxy *profile_proxy;
-       DBusGConnection *conn;
+       GDBusProxy *profile_proxy;
+       GDBusConnection *conn;
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();
@@ -491,8 +488,9 @@ int _bt_network_server_disconnect(int request_id,
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
 
-       profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
-                                     adapter_path, BT_NETWORK_SERVER_INTERFACE);
+       profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_BLUEZ_NAME,
+                       adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
        g_free(adapter_path);
        retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        func_data = g_malloc0(sizeof(bt_function_data_t));
@@ -500,16 +498,13 @@ int _bt_network_server_disconnect(int request_id,
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
 
-       if (!dbus_g_proxy_begin_call(profile_proxy, "Disconnect",
-                       (DBusGProxyCallNotify)__bt_network_disconnect_cb,
-                       func_data, NULL, G_TYPE_STRING, address,
-                       G_TYPE_INVALID)) {
-               BT_ERR("network server disconnect Dbus Call Error");
-               g_free(func_data->address);
-               g_free(func_data);
-               g_object_unref(profile_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
+       g_dbus_proxy_call(profile_proxy, "Disconnect",
+                                g_variant_new("(s)", address),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               BT_MAX_DBUS_TIMEOUT,
+                               NULL,
+                               (GAsyncReadyCallback)__bt_network_disconnect_cb,
+                               func_data);
 
        return BLUETOOTH_ERROR_NONE;
 }