Ignore vconfkey value change for AVC mode on
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-agent.c
index 88db75a..d231908 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"
@@ -33,7 +38,7 @@
 #include "bt-service-device.h"
 #include "bt-service-audio.h"
 
-#ifdef TIZEN_FEATURE_BT_DPM
+#ifdef TIZEN_DPM_ENABLE
 #include "bt-service-dpm.h"
 #endif
 
@@ -92,6 +97,137 @@ 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)
 {
@@ -125,16 +261,11 @@ 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
-       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
-       if (pairing_state == DPM_RESTRICTED) {
+#ifdef TIZEN_DPM_ENABLE
+       if (_bt_dpm_get_bluetooth_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();
@@ -202,14 +333,9 @@ static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
                gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
                                                        str_passkey, NULL);
 
-               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);
-               }
+               _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
+                                               name, auth_info, str_passkey, NULL,
+                                               _gap_agent_get_path(agent));
        } else if (_bt_get_device_pin_code(address, pin_code)
                                == BLUETOOTH_ERROR_NONE) {
                BT_DBG("Use stored PIN code(%s)", pin_code);
@@ -217,16 +343,9 @@ static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
                                                                        NULL);
                goto done;
        } else {
-               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,
+               BT_DBG("Show Pin entry");
+               _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:
@@ -248,16 +367,10 @@ 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
-       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
-       if (pairing_state == DPM_RESTRICTED) {
+#ifdef TIZEN_DPM_ENABLE
+       if (_bt_dpm_get_bluetooth_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();
@@ -291,16 +404,8 @@ static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
 
        __bt_get_auth_info(reply, (char *)auth_info);
 
-       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);
-       }
-
+       _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
+                                               NULL, NULL, _gap_agent_get_path(agent));
 
 done:
        g_variant_unref(reply);
@@ -323,7 +428,6 @@ 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("+");
 
@@ -354,24 +458,19 @@ static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
 
        str_passkey = g_strdup_printf("%06d", passkey);
 
-       GVariant *param = NULL;
-       param = g_variant_new("(ss)", address, str_passkey);
-
        if (passkey_watcher) {
+               GVariant *param = NULL;
+
                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 {
-               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);
-               }
+               _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
+                                               auth_info, str_passkey, NULL,
+                                               _gap_agent_get_path(agent));
        }
 
        g_free(str_passkey);
@@ -397,15 +496,10 @@ 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
-       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
-       if (pairing_state == DPM_RESTRICTED) {
+#ifdef TIZEN_DPM_ENABLE
+       if (_bt_dpm_get_bluetooth_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();
@@ -441,17 +535,10 @@ static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
                name = g_strdup(address);
        __bt_get_auth_info(reply, (char *)auth_info);
 
-       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);
-       }
+       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));
 
 done:
        g_variant_unref(reply);
@@ -468,8 +555,7 @@ static gboolean __pairing_cancel_request(GapAgentPrivate *agent, const char *add
 {
        BT_DBG("On Going Pairing is cancelled by remote\n");
 
-       if (headed_plugin_info->plugin_headed_enabled)
-               headed_plugin_info->headed_plugin->bt_destroy_popup_all();
+       syspopup_destroy_all();
 
        __bt_agent_release_memory();
 
@@ -492,6 +578,10 @@ 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;
 
@@ -535,29 +625,43 @@ static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
              !strcasecmp(uuid, BNEP_UUID)) {
 
                BT_DBG("Network connection request: %s", uuid);
-               if (TIZEN_FEATURE_NETWORK_TETHERING_ENABLE) {
-                       if (nap_connected_device_count >=
-                                               BT_PAN_MAX_CONNECTION) {
-                               BT_ERR("Max connection exceeded");
-                               goto fail;
-                       }
+#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 (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;
-                               }
-                       }
+               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 (enabled != true) {
+                       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);
@@ -600,7 +704,6 @@ fail:
 
                goto done;
        }
-       /* TODO: MAP? see above */
 
        if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
                                        (char *)uuid) == TRUE) {
@@ -624,19 +727,14 @@ 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 || !headed_plugin_info->plugin_headed_enabled) {
-               BT_INFO("Trusted or Headless device, so authorize\n");
+       if (trust) {
+               BT_INFO("Trusted device, so authorize\n");
                gap_agent_reply_authorize(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));
+                                             GAP_AGENT_ACCEPT, NULL);
+       } else {
+               _bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
+                                               _gap_agent_get_path(agent));
        }
 
 done:
@@ -661,8 +759,7 @@ static gboolean __authorization_cancel_request(GapAgentPrivate *agent,
 
        gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
 
-       if (headed_plugin_info->plugin_headed_enabled)
-               headed_plugin_info->headed_plugin->bt_destroy_popup_all();
+       syspopup_destroy_all();
 
        __bt_agent_release_memory();