Fix the coverity issues
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-dbus-handler.c
index 872dd23..04c1297 100644 (file)
@@ -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.
 #define BT_SERVICE_NAME                "org.projectx.bt"
 #define BT_SERVICE_PATH                "/org/projectx/bt_service"
 
+#ifdef TIZEN_FEATURE_BT_HPS
+#define BT_HPS_SERVICE_NAME "org.projectx.httpproxy"
+#define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
+#define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
+#endif
+
 static GDBusConnection *service_gconn;
 static GDBusProxy *service_gproxy;
+#ifdef TIZEN_FEATURE_BT_HPS
+static GDBusProxy *hps_gproxy;
+#endif
+static gboolean factory_test_mode;
 
 void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
 {
@@ -52,7 +56,7 @@ void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
        }
 }
 
-static GDBusConnection * _bt_core_get_gdbus_connection(void)
+GDBusConnection * _bt_core_get_gdbus_connection(void)
 {
        GError *err = NULL;
 
@@ -76,8 +80,6 @@ static GDBusProxy *__bt_core_gdbus_init_service_proxy(void)
        GError *err = NULL;
        GDBusConnection *conn;
 
-       g_type_init();
-
        conn = _bt_core_get_gdbus_connection();
        if (!conn)
                return NULL;
@@ -107,6 +109,96 @@ GDBusProxy *_bt_core_gdbus_get_service_proxy(void)
        return (service_gproxy) ? service_gproxy : __bt_core_gdbus_init_service_proxy();
 }
 
+#ifdef TIZEN_FEATURE_BT_HPS
+int _bt_core_start_httpproxy(void)
+{
+       GVariant *variant = NULL;
+       unsigned char enabled;
+       GError *err = NULL;
+       BT_DBG(" ");
+
+       hps_gproxy = _bt_core_gdbus_get_hps_proxy();
+       if (!hps_gproxy) {
+               BT_DBG("Couldn't get service proxy");
+               return -1;
+       }
+
+       variant = g_dbus_proxy_call_sync(hps_gproxy, "enable",
+                               NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+       if (err) {
+               BT_ERR("Error : %s" , err->message);
+               g_clear_error(&err);
+       }
+       if (variant) {
+               g_variant_get(variant, "(y)", &enabled);
+               BT_ERR("HPS enabled status 0x%x", enabled);
+       }
+       return 0;
+}
+
+int _bt_core_stop_httpproxy(void)
+{
+       GVariant *variant = NULL;
+       unsigned char enabled;
+       GError *err = NULL;
+       BT_DBG(" ");
+
+       hps_gproxy = _bt_core_gdbus_get_hps_proxy();
+       if (!hps_gproxy) {
+               BT_DBG("Couldn't get service proxy");
+               return -1;
+       }
+
+       variant = g_dbus_proxy_call_sync(hps_gproxy, "disable",
+                               NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+       if (err) {
+               BT_ERR("Error : %s" , err->message);
+               g_clear_error(&err);
+       }
+       if (variant) {
+               g_variant_get(variant, "(y)", &enabled);
+               BT_ERR("HPS disabled status 0x%x", enabled);
+       }
+       return 0;
+}
+
+static GDBusProxy *_bt_core_gdbus_init_hps_proxy(void)
+{
+       GDBusProxy *proxy;
+       GError *err = NULL;
+       GDBusConnection *conn;
+
+       BT_DBG(" ");
+
+       conn = _bt_core_get_gdbus_connection();
+       if (!conn)
+               return NULL;
+
+       proxy =  g_dbus_proxy_new_sync(conn,
+                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       BT_HPS_SERVICE_NAME,
+                       BT_HPS_OBJECT_PATH,
+                       BT_HPS_INTERFACE_NAME,
+                       NULL, &err);
+       if (proxy == NULL) {
+               if (err) {
+                        BT_ERR("Unable to create proxy: %s", err->message);
+                        g_clear_error(&err);
+               }
+               return NULL;
+       }
+
+       hps_gproxy = proxy;
+
+       return proxy;
+}
+
+GDBusProxy *_bt_core_gdbus_get_hps_proxy(void)
+{
+       return (hps_gproxy) ? hps_gproxy : _bt_core_gdbus_init_hps_proxy();
+}
+#endif
+
 void _bt_core_gdbus_deinit_proxys(void)
 {
        BT_DBG("");
@@ -116,6 +208,13 @@ void _bt_core_gdbus_deinit_proxys(void)
                service_gproxy = NULL;
        }
 
+#ifdef TIZEN_FEATURE_BT_HPS
+       if (hps_gproxy) {
+               g_object_unref(hps_gproxy);
+               hps_gproxy = NULL;
+       }
+#endif
+
        if (service_gconn) {
                g_object_unref(service_gconn);
                service_gconn = NULL;
@@ -128,7 +227,7 @@ int _bt_core_service_request(int service_type, int service_function,
                        GArray **out_param1)
 {
        GDBusProxy  *proxy;
-       GVariant *ret;
+       GVariant *ret = NULL;
        GVariant *param1;
        GVariant *param2;
        GVariant *param3;
@@ -138,7 +237,6 @@ int _bt_core_service_request(int service_type, int service_function,
        int result = BLUETOOTH_ERROR_NONE;
        GError *error = NULL;
        GArray *in_param5 = NULL;
-       GArray *out_param2 = NULL;
 
        proxy = _bt_core_gdbus_get_service_proxy();
        if (!proxy)
@@ -167,9 +265,8 @@ int _bt_core_service_request(int service_type, int service_function,
                                        BT_SYNC_REQ, param1,
                                        param2, param3,
                                        param4, param5),
-                               G_DBUS_CALL_FLAGS_NONE, -1,
+                               G_DBUS_CALL_FLAGS_NONE, 10000,
                                NULL, &error);
-
        g_array_free(in_param5, TRUE);
 
        if (ret == NULL) {
@@ -191,9 +288,8 @@ int _bt_core_service_request(int service_type, int service_function,
        }
 
        param1 = NULL;
-       param2 = NULL;
 
-       g_variant_get(ret, "(@ay@ay)", &param1, &param2);
+       g_variant_get(ret, "(iv)", &result, &param1);
 
        if (param1) {
                *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
@@ -201,16 +297,6 @@ int _bt_core_service_request(int service_type, int service_function,
                g_variant_unref(param1);
        }
 
-       if (param2) {
-               out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
-               _bt_core_fill_garray_from_variant(param2, out_param2);
-               result = g_array_index(out_param2, int, 0);
-               g_variant_unref(param2);
-               g_array_free(out_param2, TRUE);
-       } else {
-               result = BLUETOOTH_ERROR_INTERNAL;
-       }
-
        g_variant_unref(ret);
 
        return result;
@@ -233,6 +319,9 @@ static const gchar bt_core_introspection_xml[] =
 "     </method>"
 "     <method name='EnableCore'>"
 "     </method>"
+"        <method name='SetTransferValue'>"
+"          <arg type='b' name='value' direction='in'/>"
+"        </method>"
 "     <method name='FactoryTestMode'>"
 "          <arg type='s' name='type' direction='in'/>"
 "          <arg type='s' name='arg' direction='in'/>"
@@ -270,14 +359,27 @@ static void __bt_core_dbus_method(GDBusConnection *connection,
                ret = _bt_core_disable_adapter_le();
        } else if (g_strcmp0(method_name, "EnableCore") == 0) {
                ret = _bt_core_enable_core();
+       } else if (g_strcmp0(method_name, "SetTransferValue") == 0) {
+               gboolean value = FALSE;
+
+               g_variant_get(parameters, "(b)", &value);
+               BT_DBG("Transfer value: %d", value);
+
+               ret = _bt_core_set_transfer_value(value);
        } else if (g_strcmp0(method_name, "FactoryTestMode") == 0) {
                const char *type = NULL;
                const char *arg = NULL;
 
                g_variant_get(parameters, "(&s&s)", &type, &arg);
                ret = _bt_core_factory_test_mode(type, arg);
-               g_dbus_method_invocation_return_value(invocation,
+               factory_test_mode = TRUE;
+
+               if (ret == TRUE)
+                       g_dbus_method_invocation_return_value(invocation,
                                g_variant_new("(i)", ret));
+               else
+                       _bt_core_terminate();
+
                 return;
        } else {
                ret = FALSE;
@@ -338,8 +440,6 @@ static void __handle_name_owner_changed(const char *name)
 {
        gboolean flight_mode_status;
 
-       BT_DBG("");
-
        flight_mode_status = _bt_core_is_flight_mode_enabled();
 
        if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
@@ -349,8 +449,8 @@ static void __handle_name_owner_changed(const char *name)
 
        if ((g_strcmp0(name, "org.bluez") == 0) ||
                (g_strcmp0(name, "org.projectx.bt") == 0)) {
-               BT_DBG("%s is terminated", name);
-               if (_bt_check_terminating_condition() == TRUE) {
+               BT_INFO("%s is terminated", name);
+               if (_bt_check_terminating_condition() == TRUE && factory_test_mode == FALSE) {
                        _bt_disable_adapter();
                        _bt_disable_adapter_le();
                        _bt_core_terminate();
@@ -376,9 +476,9 @@ static void __bt_core_event_filter(GDBusConnection *connection,
                g_variant_get(parameters, "(&o@a{sa{sv}})",
                                                &obj_path, &optional_param);
 
-               if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0) {
+               if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
                        _bt_core_adapter_added_cb();
-               }
+
        } else if (!g_strcmp0(signal_name, "InterfacesRemoved")) {
                char *obj_path = NULL;
                GVariant *optional_param;
@@ -386,9 +486,9 @@ static void __bt_core_event_filter(GDBusConnection *connection,
                g_variant_get(parameters, "(&o@as)", &obj_path,
                                                        &optional_param);
 
-               if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0) {
+               if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
                        _bt_core_adapter_removed_cb();
-               }
+
        } else { /* NameOwnerChanged */
                const char *name = NULL;
                const char *old_owner = NULL;
@@ -433,6 +533,7 @@ gboolean _bt_core_register_dbus(void)
                                        node_info->interfaces[0],
                                        &method_table,
                                        NULL, NULL, &error);
+       g_dbus_node_info_unref(node_info);
        if (obj_id == 0) {
                BT_ERR("Failed to register: %s", error->message);
                g_error_free(error);
@@ -450,7 +551,7 @@ gboolean _bt_core_register_dbus(void)
                                NULL, "org.freedesktop.DBus.ObjectManager",
                                "InterfacesAdded", NULL, NULL,
                                0, __bt_core_event_filter, NULL, NULL);
-       sig_id2 = g_dbus_connection_signal_subscribe(conn,
+       sig_id3 = g_dbus_connection_signal_subscribe(conn,
                                NULL, "org.freedesktop.DBus.ObjectManager",
                                "InterfacesRemoved", NULL,
                                NULL, 0, __bt_core_event_filter, NULL, NULL);