Fix tizen coding style errors and warnings
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-agent.c
index 92a36ab..92ee46f 100644 (file)
 #include <string.h>
 #include <malloc.h>
 #include <stacktrim.h>
-#include <syspopup_caller.h>
 #include <vconf.h>
 #include <bundle_internal.h>
 
-#ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
-#include <tethering.h>
-#endif
-
 #include "bt-internal-types.h"
 #include "bt-service-common.h"
 #include "bt-service-agent.h"
@@ -78,7 +73,7 @@
 
 extern guint nap_connected_device_count;
 
-static char *passkey_watcher = NULL;
+static char *passkey_watcher;
 
 #define G_VARIANT_UNREF(variant) \
        g_variant_unref(variant); \
@@ -97,137 +92,6 @@ static void __bt_agent_release_memory(void)
        /* Release Stack Memory*/
        stack_trim();
 }
-static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
-{
-       int ret;
-       static int retry_count;
-       bundle *b = (bundle *)user_data;
-       retv_if(user_data == NULL, FALSE);
-
-       ++retry_count;
-
-       ret = syspopup_launch("bt-syspopup", b);
-       if (ret < 0) {
-               BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
-                                                       ret, retry_count);
-               if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
-                       BT_ERR("Sorry!! Max retry %d reached", retry_count);
-                       bundle_free(b);
-                       retry_count = 0;
-                       return FALSE;
-               }
-       } else {
-               BT_DBG("Hurray!! Finally Popup launched");
-               retry_count = 0;
-               bundle_free(b);
-       }
-
-       return (ret < 0) ? TRUE : FALSE;
-}
-
-int _bt_launch_system_popup(bt_agent_event_type_t event_type,
-                                                       const char *device_name,
-                                                       const unsigned char *auth_info,
-                                                       char *passkey,
-                                                       const char *filename,
-                                                       const char *agent_path)
-{
-       int ret;
-       bundle *b;
-       char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
-
-       b = bundle_create();
-       if (!b) {
-               BT_ERR("Launching system popup failed");
-               return -1;
-       }
-
-       bundle_add(b, "device-name", device_name);
-       bundle_add(b, "auth-info", (const char *)auth_info);
-       bundle_add(b, "passkey", passkey);
-       bundle_add(b, "file", filename);
-       bundle_add(b, "agent-path", agent_path);
-
-       switch (event_type) {
-       case BT_AGENT_EVENT_PIN_REQUEST:
-               g_strlcpy(event_str, "pin-request", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
-               g_strlcpy(event_str, "passkey-confirm-request",
-                                               sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_PASSKEY_REQUEST:
-               g_strlcpy(event_str, "passkey-request", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
-               g_strlcpy(event_str, "passkey-display-request",
-                                               sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
-               g_strlcpy(event_str, "authorize-request",
-                                               sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
-               g_strlcpy(event_str, "confirm-mode-request",
-                                               sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_FILE_RECEIVED:
-               g_strlcpy(event_str, "file-received", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
-               g_strlcpy(event_str, "keyboard-passkey-request",
-                                               sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_TERMINATE:
-               g_strlcpy(event_str, "terminate", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_EXCHANGE_REQUEST:
-               g_strlcpy(event_str, "exchange-request", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_PBAP_REQUEST:
-               g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_MAP_REQUEST:
-               g_strlcpy(event_str, "message-request", sizeof(event_str));
-               break;
-
-       case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
-               g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
-               break;
-
-       default:
-               BT_ERR("Invalid event type");
-               bundle_free(b);
-               return -1;
-
-       }
-
-       bundle_add(b, "event-type", event_str);
-
-       ret = syspopup_launch("bt-syspopup", b);
-       if (0 > ret) {
-               BT_ERR("Popup launch failed...retry %d", ret);
-
-               g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
-                             (GSourceFunc)__bt_agent_system_popup_timer_cb, b);
-       } else {
-               bundle_free(b);
-       }
-
-       BT_INFO("_bt_agent_launch_system_popup");
-       return 0;
-}
 
 static GVariant *__bt_service_getall(GDBusProxy *device, const char *interface)
 {
@@ -261,11 +125,16 @@ static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
        GVariant *reply_temp = NULL;
        GVariant *tmp_value;
        char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
+       int ret = BLUETOOTH_ERROR_NONE;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int pairing_state = DPM_STATUS_ERROR;
+#endif
 
        BT_DBG("+");
 
 #ifdef TIZEN_FEATURE_BT_DPM
-       if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
+       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
+       if (pairing_state == DPM_RESTRICTED) {
                BT_ERR("Not allow to pair the device");
                gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
                __bt_agent_release_memory();
@@ -333,9 +202,14 @@ static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
                gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
                                                        str_passkey, NULL);
 
-               _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
-                                               name, auth_info, str_passkey, NULL,
-                                               _gap_agent_get_path(agent));
+               if (headed_plugin_info->plugin_headed_enabled) {
+                       headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
+                                       name, auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
+               } else {
+                       GVariant *param = NULL;
+                       param = g_variant_new("(isss)", ret, address, name, str_passkey);
+                       _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
+               }
        } else if (_bt_get_device_pin_code(address, pin_code)
                                == BLUETOOTH_ERROR_NONE) {
                BT_DBG("Use stored PIN code(%s)", pin_code);
@@ -343,9 +217,16 @@ static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
                                                                        NULL);
                goto done;
        } else {
-               BT_DBG("Show Pin entry");
-               _bt_launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, auth_info,
+               if (headed_plugin_info->plugin_headed_enabled) {
+                       BT_DBG("Show Pin entry");
+                       headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, auth_info,
                                        NULL, NULL, _gap_agent_get_path(agent));
+               } else {
+                       BT_INFO("Plugin Headed not Enabled");
+                       GVariant *param = NULL;
+                       param = g_variant_new("(iss)", ret, address, name);
+                       _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_PIN_REQUEST, param);
+               }
        }
 
 done:
@@ -367,10 +248,16 @@ static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
        GVariant *reply = NULL;
        GVariant *reply_temp = NULL;
        GVariant *tmp_value;
+       int ret = BLUETOOTH_ERROR_NONE;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int pairing_state = DPM_STATUS_ERROR;
+#endif
+
        BT_DBG("+");
 
 #ifdef TIZEN_FEATURE_BT_DPM
-       if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
+       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
+       if (pairing_state == DPM_RESTRICTED) {
                BT_ERR("Not allow to pair the device");
                gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
                __bt_agent_release_memory();
@@ -404,8 +291,16 @@ static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
 
        __bt_get_auth_info(reply, (char *)auth_info);
 
-       _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
-                                               NULL, NULL, _gap_agent_get_path(agent));
+       if (headed_plugin_info->plugin_headed_enabled) {
+               headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
+                               NULL, NULL, _gap_agent_get_path(agent));
+       } else {
+               BT_INFO("Plugin Headed not Enabled");
+               GVariant *param = NULL;
+               param = g_variant_new("(iss)", ret, address, name);
+               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
+       }
+
 
 done:
        g_variant_unref(reply);
@@ -428,6 +323,7 @@ static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
        GVariant *reply = NULL;
        GVariant *reply_temp = NULL;
        GVariant *tmp_value = NULL;
+       int ret = BLUETOOTH_ERROR_NONE;
 
        BT_DBG("+");
 
@@ -458,19 +354,24 @@ static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
 
        str_passkey = g_strdup_printf("%06d", passkey);
 
-       if (passkey_watcher) {
-               GVariant *param = NULL;
+       GVariant *param = NULL;
+       param = g_variant_new("(ss)", address, str_passkey);
 
+       if (passkey_watcher) {
                BT_INFO("Send passkey to %s", passkey_watcher);
-
-               param = g_variant_new("(ss)", address, str_passkey);
-
                _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
                                BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
        } else {
-               _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
-                                               auth_info, str_passkey, NULL,
-                                               _gap_agent_get_path(agent));
+               if (headed_plugin_info->plugin_headed_enabled) {
+                       BT_INFO("Plugin Headed Enabled");
+                       headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
+                                       auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
+               } else {
+                       BT_INFO("Plugin Headed not Enabled");
+                       GVariant *param = NULL;
+                       param = g_variant_new("(isss)", ret, address, name, str_passkey);
+                       _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
+               }
        }
 
        g_free(str_passkey);
@@ -496,10 +397,15 @@ static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
        GVariant *reply_temp = NULL;
        GVariant *reply = NULL;
        GVariant *tmp_value;
+       int ret = BLUETOOTH_ERROR_NONE;
+#ifdef TIZEN_FEATURE_BT_DPM
+               int pairing_state = DPM_STATUS_ERROR;
+#endif
        BT_DBG("+ passkey[%.6d]", passkey);
 
 #ifdef TIZEN_FEATURE_BT_DPM
-       if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
+       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
+       if (pairing_state == DPM_RESTRICTED) {
                BT_ERR("Not allow to pair the device");
                gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
                __bt_agent_release_memory();
@@ -535,10 +441,17 @@ static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
                name = g_strdup(address);
        __bt_get_auth_info(reply, (char *)auth_info);
 
-       BT_DBG("LAUNCH SYSPOPUP");
-       _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
-                                               auth_info, str_passkey, NULL,
-                                               _gap_agent_get_path(agent));
+       if (headed_plugin_info->plugin_headed_enabled) {
+               BT_DBG("LAUNCH SYSPOPUP");
+               headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
+                               auth_info, str_passkey, NULL,
+                               _gap_agent_get_path(agent));
+       } else {
+               BT_DBG("Headless Confirmation");
+               GVariant *param = NULL;
+               param = g_variant_new("(isss)", ret, address, name, str_passkey);
+               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
+       }
 
 done:
        g_variant_unref(reply);
@@ -555,7 +468,8 @@ static gboolean __pairing_cancel_request(GapAgentPrivate *agent, const char *add
 {
        BT_DBG("On Going Pairing is cancelled by remote\n");
 
-       syspopup_destroy_all();
+       if (headed_plugin_info->plugin_headed_enabled)
+               headed_plugin_info->headed_plugin->bt_destroy_popup_all();
 
        __bt_agent_release_memory();
 
@@ -568,6 +482,37 @@ static gboolean __a2dp_authorize_request_check(void)
        return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
 }
 
+static gboolean __bt_agent_is_hid_device_connectable(void)
+{
+       GDBusProxy *proxy = NULL;
+       GVariant *reply = NULL;
+       GError *err = NULL;
+       gboolean connectable = FALSE;
+
+       proxy = _bt_gdbus_get_hid_agent_proxy();
+       retv_if(proxy == NULL, FALSE);
+
+       reply = g_dbus_proxy_call_sync(proxy, "IsHidConnectable", NULL,
+                               G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &err);
+       if (reply == NULL) {
+               BT_ERR("Error returned in method call");
+               if (err != NULL) {
+                       BT_ERR("Error message = %s", err->message);
+                       g_error_free(err);
+               }
+               connectable = FALSE;
+       } else {
+               g_variant_get(reply, "(b)", &connectable);
+               g_variant_unref(reply);
+       }
+       g_object_unref(proxy);
+
+       BT_INFO("HID Device is %s",
+                       connectable ? "Connectable" : "Non-connectable");
+
+       return connectable;
+}
+
 static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
                                                        const char *uuid)
 {
@@ -578,10 +523,6 @@ static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
        GVariant *reply = NULL;
        GVariant *reply_temp = NULL;
        GVariant *tmp_value;
-#ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
-       bool enabled;
-       tethering_h tethering = NULL;
-#endif
        int result = BLUETOOTH_ERROR_NONE;
        int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
 
@@ -602,12 +543,22 @@ static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
        }
        /* Check completed */
 
+       if (!strcasecmp(uuid, HID_UUID)) {
+               gboolean is_connectable = __bt_agent_is_hid_device_connectable();
+               BT_DBG("Automatically %s authorization for HID",
+                       is_connectable ? "accept" : "reject");
+               if (is_connectable == TRUE)
+                       gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
+               else
+                       gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
+               goto done;
+       }
+
        if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
             !strcasecmp(uuid, HSP_AUDIO_GATEWAY_UUID) ||
             !strcasecmp(uuid, HFP_HS_UUID) ||
             !strcasecmp(uuid, HSP_HS_UUID) ||
             !strcasecmp(uuid, A2DP_UUID) ||
-            !strcasecmp(uuid, HID_UUID) ||
             !strcasecmp(uuid, HID_DEVICE_UUID) ||
             !strcasecmp(uuid, SAP_UUID_OLD) ||
             !strcasecmp(uuid, SAP_UUID_NEW) ||
@@ -625,43 +576,29 @@ static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
              !strcasecmp(uuid, BNEP_UUID)) {
 
                BT_DBG("Network connection request: %s", uuid);
-#ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
-               if (nap_connected_device_count >=
-                                       BT_PAN_MAX_CONNECTION) {
-                       BT_ERR("Max connection exceeded");
-                       goto fail;
-               }
-               int ret;
-               ret = tethering_create(&tethering);
-
-               if (ret != TETHERING_ERROR_NONE) {
-                       BT_ERR("Fail to create tethering: %d", ret);
-                       goto fail;
-               }
-
-               enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
-
-               ret = tethering_destroy(tethering);
-
-               if (ret != TETHERING_ERROR_NONE)
-                       BT_ERR("Fail to create tethering: %d", ret);
+               if (TIZEN_FEATURE_NETWORK_TETHERING_ENABLE) {
+                       if (nap_connected_device_count >=
+                                               BT_PAN_MAX_CONNECTION) {
+                               BT_ERR("Max connection exceeded");
+                               goto fail;
+                       }
 
-               if (enabled != true) {
-                       BT_ERR("BT tethering is not enabled");
-                       goto fail;
+                       if (headed_plugin_info->plugin_headed_enabled) {
+                               if (headed_plugin_info->headed_plugin->bt_is_tethering_enabled() == false) {
+                                       BT_ERR("BT tethering is not enabled");
+                                       goto fail;
+                               }
+                       }
                }
-#endif
 
                gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
                                              NULL);
                goto done;
-#ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
 fail:
                gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
                      NULL);
 
                goto done;
-#endif
        }
 
        reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
@@ -704,6 +641,7 @@ fail:
 
                goto done;
        }
+       /* TODO: MAP? see above */
 
        if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
                                        (char *)uuid) == TRUE) {
@@ -727,14 +665,19 @@ fail:
                request_type = BT_AGENT_EVENT_PBAP_REQUEST;
        else if (!strcasecmp(uuid, MAP_UUID))
                request_type = BT_AGENT_EVENT_MAP_REQUEST;
+       /* TODO: MAP is already here */
 
-       if (trust) {
-               BT_INFO("Trusted device, so authorize\n");
+       if (trust || !headed_plugin_info->plugin_headed_enabled) {
+               BT_INFO("Trusted or Headless device, so authorize\n");
                gap_agent_reply_authorize(agent,
-                                             GAP_AGENT_ACCEPT, NULL);
-       } else {
-               _bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
-                                               _gap_agent_get_path(agent));
+                               GAP_AGENT_ACCEPT, NULL);
+
+               goto done;
+       }
+
+       if (headed_plugin_info->plugin_headed_enabled) {
+               headed_plugin_info->headed_plugin->bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
+                                       _gap_agent_get_path(agent));
        }
 
 done:
@@ -759,7 +702,8 @@ static gboolean __authorization_cancel_request(GapAgentPrivate *agent,
 
        gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
 
-       syspopup_destroy_all();
+       if (headed_plugin_info->plugin_headed_enabled)
+               headed_plugin_info->headed_plugin->bt_destroy_popup_all();
 
        __bt_agent_release_memory();
 
@@ -961,12 +905,6 @@ static gboolean __bt_agent_is_device_blacklist(const char *address,
        rewind(fp);
 
        buffer = g_malloc0(sizeof(char) * size);
-       /* Fix : NULL_RETURNS */
-       if (buffer == NULL) {
-               BT_ERR("Fail to allocate memory");
-               fclose(fp);
-               return FALSE;
-       }
        result = fread((char *)buffer, 1, size, fp);
        fclose(fp);
        if (result != size) {