[OTP] Handle Object Execute request
[platform/core/connectivity/bluetooth-frwk.git] / plugin / headed.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <syspopup_caller.h>
23 #include <bundle.h>
24 #include <bundle_internal.h>
25 #include <tethering.h>
26 #include <mime_type.h>
27 #include <app_control.h>
28
29 #include "plugin.h"
30 #include "bluetooth-api.h"
31
32 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
33 #define BT_MAX_EVENT_STR_LENGTH 50
34 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
35
36 #ifdef TIZEN_FEATURE_BT_DPM
37 int bt_launch_dpm_popup(char *mode)
38 {
39         int ret = 0;
40         bundle *b;
41
42         b = bundle_create();
43         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
44
45         bundle_add(b, "mode", mode);
46
47         ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
48
49         if (ret < 0)
50                 BT_ERR("Popup launch failed: %d\n", ret);
51
52         bundle_free(b);
53
54         return ret;
55 }
56 #endif
57
58 static gboolean _bt_syspopup_timer_cb(gpointer user_data)
59 {
60         int ret;
61         bundle *b;
62         retv_if(user_data == NULL, FALSE);
63
64         b = (bundle *)user_data;
65
66         ret = syspopup_launch("bt-syspopup", b);
67         if (ret < 0) {
68                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
69         } else {
70                 BT_DBG("Hurray!!! Finally Popup launched");
71                 bundle_free(b);
72         }
73         return (ret < 0) ? TRUE : FALSE;
74 }
75
76
77 static gboolean _bt_agent_system_popup_timer_cb(gpointer user_data)
78 {
79         int ret;
80         static int retry_count;
81         bundle *b = (bundle *)user_data;
82         retv_if(user_data == NULL, FALSE);
83
84         ++retry_count;
85
86         ret = syspopup_launch("bt-syspopup", b);
87         if (ret < 0) {
88                 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
89                                                         ret, retry_count);
90                 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
91                         BT_ERR("Sorry!! Max retry %d reached", retry_count);
92                         bundle_free(b);
93                         retry_count = 0;
94                         return FALSE;
95                 }
96         } else {
97                 BT_DBG("Hurray!! Finally Popup launched");
98                 retry_count = 0;
99                 bundle_free(b);
100         }
101
102         return (ret < 0) ? TRUE : FALSE;
103 }
104
105 int bt_launch_system_popup(bt_agent_event_type_t event_type,
106                                                         const char *device_name,
107                                                         const unsigned char *auth_info,
108                                                         char *passkey,
109                                                         const char *filename,
110                                                         const char *agent_path)
111 {
112         int ret;
113         bundle *b;
114         char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
115
116         b = bundle_create();
117         if (!b) {
118                 BT_ERR("Launching system popup failed");
119                 return -1;
120         }
121
122         bundle_add(b, "device-name", device_name);
123         bundle_add(b, "auth-info", (const char *)auth_info);
124         bundle_add(b, "passkey", passkey);
125         bundle_add(b, "file", filename);
126         bundle_add(b, "agent-path", agent_path);
127
128         switch (event_type) {
129         case BT_AGENT_EVENT_PIN_REQUEST:
130                 g_strlcpy(event_str, "pin-request", sizeof(event_str));
131                 break;
132
133         case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
134                 g_strlcpy(event_str, "passkey-confirm-request",
135                                                 sizeof(event_str));
136                 break;
137
138         case BT_AGENT_EVENT_PASSKEY_REQUEST:
139                 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
140                 break;
141
142         case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
143                 g_strlcpy(event_str, "passkey-display-request",
144                                                 sizeof(event_str));
145                 break;
146
147         case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
148                 g_strlcpy(event_str, "authorize-request",
149                                                 sizeof(event_str));
150                 break;
151
152         case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
153                 g_strlcpy(event_str, "confirm-mode-request",
154                                                 sizeof(event_str));
155                 break;
156
157         case BT_AGENT_EVENT_FILE_RECEIVED:
158                 g_strlcpy(event_str, "file-received", sizeof(event_str));
159                 break;
160
161         case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
162                 g_strlcpy(event_str, "keyboard-passkey-request",
163                                                 sizeof(event_str));
164                 break;
165
166         case BT_AGENT_EVENT_TERMINATE:
167                 g_strlcpy(event_str, "terminate", sizeof(event_str));
168                 break;
169
170         case BT_AGENT_EVENT_EXCHANGE_REQUEST:
171                 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
172                 break;
173
174         case BT_AGENT_EVENT_PBAP_REQUEST:
175                 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
176                 break;
177
178         case BT_AGENT_EVENT_MAP_REQUEST:
179                 g_strlcpy(event_str, "message-request", sizeof(event_str));
180                 break;
181
182         case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
183                 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
184                 break;
185
186         default:
187                 BT_ERR("Invalid event type");
188                 bundle_free(b);
189                 return -1;
190
191         }
192
193         bundle_add(b, "event-type", event_str);
194
195         ret = syspopup_launch("bt-syspopup", b);
196         if (0 > ret) {
197                 BT_ERR("Popup launch failed...retry %d", ret);
198
199                 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
200                               (GSourceFunc)_bt_agent_system_popup_timer_cb, b);
201         } else {
202                 bundle_free(b);
203         }
204
205         BT_INFO("_bt_agent_launch_system_popup");
206         return 0;
207 }
208
209 gboolean bt_launch_unable_to_pairing_syspopup(int result)
210 {
211         BT_DBG("+");
212         int ret = 0;
213         bundle *b = NULL;
214
215         b = bundle_create();
216         if (b == NULL)
217                 return FALSE;
218
219         bundle_add(b, "event-type", "unable-to-pairing");
220
221         if (result == BLUETOOTH_ERROR_TIMEOUT)
222                 bundle_add(b, "error", "timeout");
223         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
224                 bundle_add(b, "error", "authfailed");
225         else
226                 bundle_add(b, "error", "error");
227
228         ret = syspopup_launch("bt-syspopup", b);
229         if (0 > ret) {
230                 BT_ERR("Popup launch failed...retry %d \n", ret);
231                 g_timeout_add(200, (GSourceFunc) _bt_syspopup_timer_cb,
232                                 b);
233         } else {
234                 bundle_free(b);
235         }
236
237         BT_DBG("-");
238         return TRUE;
239 }
240
241
242 gboolean bt_is_tethering_enabled(void)
243 {
244         BT_DBG("+");
245
246         tethering_h tethering = NULL;
247         bool enabled = FALSE;
248         int ret;
249
250         ret = tethering_create(&tethering);
251
252         if (ret != TETHERING_ERROR_NONE) {
253                 BT_ERR("Fail to create tethering: %d", ret);
254                 return FALSE;
255         }
256
257         enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
258
259         if (enabled != true)
260                 BT_ERR("BT tethering is not enabled");
261
262         ret = tethering_destroy(tethering);
263
264         if (ret != TETHERING_ERROR_NONE)
265                 BT_ERR("Fail to destroy tethering: %d", ret);
266
267         return (gboolean)enabled;
268 }
269
270 int bt_get_mime_type(char *file_name, char **mime_type)
271 {
272         BT_DBG("+");
273
274         int ret = MIME_TYPE_ERROR_NONE;
275
276         if (file_name == NULL)
277                 return BLUETOOTH_ERROR_INVALID_PARAM;
278
279         ret = mime_type_get_mime_type(file_name, (gchar **)mime_type);
280
281         if (ret != MIME_TYPE_ERROR_NONE) {
282                 BT_ERR("Fail to get mime type: %d", ret);
283                 return BLUETOOTH_ERROR_INTERNAL;
284         }
285
286         return BLUETOOTH_ERROR_NONE;
287 }
288
289 void bt_destroy_popup_all(void)
290 {
291         syspopup_destroy_all();
292 }
293
294 int bt_app_control_send_launch_request(char *absolute_path)
295 {
296         BT_DBG("+");
297
298         app_control_h app_control;
299         int ret;
300
301         app_control_create(&app_control);
302         app_control_set_operation(app_control, APP_CONTROL_OPERATION_VIEW);
303         app_control_set_uri(app_control, absolute_path);
304         app_control_set_mime(app_control, "*/*");
305
306         if (app_control_send_launch_request(app_control, NULL, NULL) == APP_CONTROL_ERROR_NONE)
307                 ret = BLUETOOTH_ERROR_NONE;
308         else
309                 ret = BLUETOOTH_ERROR_INTERNAL;
310
311         app_control_destroy(app_control);
312
313         return ret;
314 }
315
316 extern struct bluetooth_headed_plugin_t headed_plugin
317 __attribute__ ((visibility("default")));
318 struct bluetooth_headed_plugin_t headed_plugin = {
319 #ifdef TIZEN_FEATURE_BT_DPM
320         bt_launch_dpm_popup,
321 #else
322                 NULL,
323 #endif
324         bt_launch_system_popup,
325         bt_destroy_popup_all,
326         bt_launch_unable_to_pairing_syspopup,
327         bt_is_tethering_enabled,
328         bt_get_mime_type
329 };
330