2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
25 #include <tethering.h>
26 #include <mime_type.h>
29 #include "bluetooth-api.h"
31 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
32 #define BT_MAX_EVENT_STR_LENGTH 50
33 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
35 #ifdef TIZEN_FEATURE_BT_DPM
36 int bt_launch_dpm_popup(char *mode)
42 retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
44 bundle_add(b, "mode", mode);
46 ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
49 BT_ERR("Popup launch failed: %d\n", ret);
57 static gboolean _bt_syspopup_timer_cb(gpointer user_data)
61 retv_if(user_data == NULL, FALSE);
63 b = (bundle *)user_data;
65 ret = syspopup_launch("bt-syspopup", b);
67 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
69 BT_DBG("Hurray!!! Finally Popup launched");
72 return (ret < 0) ? TRUE : FALSE;
76 static gboolean _bt_agent_system_popup_timer_cb(gpointer user_data)
79 static int retry_count;
80 bundle *b = (bundle *)user_data;
81 retv_if(user_data == NULL, FALSE);
85 ret = syspopup_launch("bt-syspopup", b);
87 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
89 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
90 BT_ERR("Sorry!! Max retry %d reached", retry_count);
96 BT_DBG("Hurray!! Finally Popup launched");
101 return (ret < 0) ? TRUE : FALSE;
104 int bt_launch_system_popup(bt_agent_event_type_t event_type,
105 const char *device_name,
106 const unsigned char *auth_info,
108 const char *filename,
109 const char *agent_path)
113 char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
117 BT_ERR("Launching system popup failed");
121 bundle_add(b, "device-name", device_name);
122 bundle_add(b, "auth-info", (const char *)auth_info);
123 bundle_add(b, "passkey", passkey);
124 bundle_add(b, "file", filename);
125 bundle_add(b, "agent-path", agent_path);
127 switch (event_type) {
128 case BT_AGENT_EVENT_PIN_REQUEST:
129 g_strlcpy(event_str, "pin-request", sizeof(event_str));
132 case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
133 g_strlcpy(event_str, "passkey-confirm-request",
137 case BT_AGENT_EVENT_PASSKEY_REQUEST:
138 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
141 case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
142 g_strlcpy(event_str, "passkey-display-request",
146 case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
147 g_strlcpy(event_str, "authorize-request",
151 case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
152 g_strlcpy(event_str, "confirm-mode-request",
156 case BT_AGENT_EVENT_FILE_RECEIVED:
157 g_strlcpy(event_str, "file-received", sizeof(event_str));
160 case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
161 g_strlcpy(event_str, "keyboard-passkey-request",
165 case BT_AGENT_EVENT_TERMINATE:
166 g_strlcpy(event_str, "terminate", sizeof(event_str));
169 case BT_AGENT_EVENT_EXCHANGE_REQUEST:
170 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
173 case BT_AGENT_EVENT_PBAP_REQUEST:
174 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
177 case BT_AGENT_EVENT_MAP_REQUEST:
178 g_strlcpy(event_str, "message-request", sizeof(event_str));
181 case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
182 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
186 BT_ERR("Invalid event type");
192 bundle_add(b, "event-type", event_str);
194 ret = syspopup_launch("bt-syspopup", b);
196 BT_ERR("Popup launch failed...retry %d", ret);
198 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
199 (GSourceFunc)_bt_agent_system_popup_timer_cb, b);
204 BT_INFO("_bt_agent_launch_system_popup");
208 gboolean bt_launch_unable_to_pairing_syspopup(int result)
218 bundle_add(b, "event-type", "unable-to-pairing");
220 if (result == BLUETOOTH_ERROR_TIMEOUT)
221 bundle_add(b, "error", "timeout");
222 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
223 bundle_add(b, "error", "authfailed");
225 bundle_add(b, "error", "error");
227 ret = syspopup_launch("bt-syspopup", b);
229 BT_ERR("Popup launch failed...retry %d \n", ret);
230 g_timeout_add(200, (GSourceFunc) _bt_syspopup_timer_cb,
241 gboolean bt_is_tethering_enabled(void)
245 tethering_h tethering = NULL;
246 bool enabled = FALSE;
249 ret = tethering_create(&tethering);
251 if (ret != TETHERING_ERROR_NONE) {
252 BT_ERR("Fail to create tethering: %d", ret);
256 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
259 BT_ERR("BT tethering is not enabled");
261 ret = tethering_destroy(tethering);
263 if (ret != TETHERING_ERROR_NONE)
264 BT_ERR("Fail to destroy tethering: %d", ret);
266 return (gboolean)enabled;
269 int bt_get_mime_type(char *file_name, char **mime_type)
273 int ret = MIME_TYPE_ERROR_NONE;
275 if (file_name == NULL)
276 return BLUETOOTH_ERROR_INVALID_PARAM;
278 ret = mime_type_get_mime_type(file_name, (gchar **)mime_type);
280 if (ret != MIME_TYPE_ERROR_NONE) {
281 BT_ERR("Fail to get mime type: %d", ret);
282 return BLUETOOTH_ERROR_INTERNAL;
285 return BLUETOOTH_ERROR_NONE;
288 void bt_destroy_popup_all(void)
290 syspopup_destroy_all();
294 extern struct bluetooth_headed_plugin_t headed_plugin
295 __attribute__ ((visibility("default")));
296 struct bluetooth_headed_plugin_t headed_plugin = {
297 #ifdef TIZEN_FEATURE_BT_DPM
302 bt_launch_system_popup,
303 bt_destroy_popup_all,
304 bt_launch_unable_to_pairing_syspopup,
305 bt_is_tethering_enabled,