Merge "[OTP] Handle Object Create request" into tizen
[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
28 #include "plugin.h"
29 #include "bluetooth-api.h"
30
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
34
35 #ifdef TIZEN_FEATURE_BT_DPM
36 int bt_launch_dpm_popup(char *mode)
37 {
38         int ret = 0;
39         bundle *b;
40
41         b = bundle_create();
42         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
43
44         bundle_add(b, "mode", mode);
45
46         ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
47
48         if (ret < 0)
49                 BT_ERR("Popup launch failed: %d\n", ret);
50
51         bundle_free(b);
52
53         return ret;
54 }
55 #endif
56
57 static gboolean _bt_syspopup_timer_cb(gpointer user_data)
58 {
59         int ret;
60         bundle *b;
61         retv_if(user_data == NULL, FALSE);
62
63         b = (bundle *)user_data;
64
65         ret = syspopup_launch("bt-syspopup", b);
66         if (ret < 0) {
67                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
68         } else {
69                 BT_DBG("Hurray!!! Finally Popup launched");
70                 bundle_free(b);
71         }
72         return (ret < 0) ? TRUE : FALSE;
73 }
74
75
76 static gboolean _bt_agent_system_popup_timer_cb(gpointer user_data)
77 {
78         int ret;
79         static int retry_count;
80         bundle *b = (bundle *)user_data;
81         retv_if(user_data == NULL, FALSE);
82
83         ++retry_count;
84
85         ret = syspopup_launch("bt-syspopup", b);
86         if (ret < 0) {
87                 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
88                                                         ret, retry_count);
89                 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
90                         BT_ERR("Sorry!! Max retry %d reached", retry_count);
91                         bundle_free(b);
92                         retry_count = 0;
93                         return FALSE;
94                 }
95         } else {
96                 BT_DBG("Hurray!! Finally Popup launched");
97                 retry_count = 0;
98                 bundle_free(b);
99         }
100
101         return (ret < 0) ? TRUE : FALSE;
102 }
103
104 int bt_launch_system_popup(bt_agent_event_type_t event_type,
105                                                         const char *device_name,
106                                                         const unsigned char *auth_info,
107                                                         char *passkey,
108                                                         const char *filename,
109                                                         const char *agent_path)
110 {
111         int ret;
112         bundle *b;
113         char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
114
115         b = bundle_create();
116         if (!b) {
117                 BT_ERR("Launching system popup failed");
118                 return -1;
119         }
120
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);
126
127         switch (event_type) {
128         case BT_AGENT_EVENT_PIN_REQUEST:
129                 g_strlcpy(event_str, "pin-request", sizeof(event_str));
130                 break;
131
132         case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
133                 g_strlcpy(event_str, "passkey-confirm-request",
134                                                 sizeof(event_str));
135                 break;
136
137         case BT_AGENT_EVENT_PASSKEY_REQUEST:
138                 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
139                 break;
140
141         case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
142                 g_strlcpy(event_str, "passkey-display-request",
143                                                 sizeof(event_str));
144                 break;
145
146         case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
147                 g_strlcpy(event_str, "authorize-request",
148                                                 sizeof(event_str));
149                 break;
150
151         case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
152                 g_strlcpy(event_str, "confirm-mode-request",
153                                                 sizeof(event_str));
154                 break;
155
156         case BT_AGENT_EVENT_FILE_RECEIVED:
157                 g_strlcpy(event_str, "file-received", sizeof(event_str));
158                 break;
159
160         case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
161                 g_strlcpy(event_str, "keyboard-passkey-request",
162                                                 sizeof(event_str));
163                 break;
164
165         case BT_AGENT_EVENT_TERMINATE:
166                 g_strlcpy(event_str, "terminate", sizeof(event_str));
167                 break;
168
169         case BT_AGENT_EVENT_EXCHANGE_REQUEST:
170                 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
171                 break;
172
173         case BT_AGENT_EVENT_PBAP_REQUEST:
174                 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
175                 break;
176
177         case BT_AGENT_EVENT_MAP_REQUEST:
178                 g_strlcpy(event_str, "message-request", sizeof(event_str));
179                 break;
180
181         case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
182                 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
183                 break;
184
185         default:
186                 BT_ERR("Invalid event type");
187                 bundle_free(b);
188                 return -1;
189
190         }
191
192         bundle_add(b, "event-type", event_str);
193
194         ret = syspopup_launch("bt-syspopup", b);
195         if (0 > ret) {
196                 BT_ERR("Popup launch failed...retry %d", ret);
197
198                 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
199                               (GSourceFunc)_bt_agent_system_popup_timer_cb, b);
200         } else {
201                 bundle_free(b);
202         }
203
204         BT_INFO("_bt_agent_launch_system_popup");
205         return 0;
206 }
207
208 gboolean bt_launch_unable_to_pairing_syspopup(int result)
209 {
210         BT_DBG("+");
211         int ret = 0;
212         bundle *b = NULL;
213
214         b = bundle_create();
215         if (b == NULL)
216                 return FALSE;
217
218         bundle_add(b, "event-type", "unable-to-pairing");
219
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");
224         else
225                 bundle_add(b, "error", "error");
226
227         ret = syspopup_launch("bt-syspopup", b);
228         if (0 > ret) {
229                 BT_ERR("Popup launch failed...retry %d \n", ret);
230                 g_timeout_add(200, (GSourceFunc) _bt_syspopup_timer_cb,
231                                 b);
232         } else {
233                 bundle_free(b);
234         }
235
236         BT_DBG("-");
237         return TRUE;
238 }
239
240
241 gboolean bt_is_tethering_enabled(void)
242 {
243         BT_DBG("+");
244
245         tethering_h tethering = NULL;
246         bool enabled = FALSE;
247         int ret;
248
249         ret = tethering_create(&tethering);
250
251         if (ret != TETHERING_ERROR_NONE) {
252                 BT_ERR("Fail to create tethering: %d", ret);
253                 return FALSE;
254         }
255
256         enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
257
258         if (enabled != true)
259                 BT_ERR("BT tethering is not enabled");
260
261         ret = tethering_destroy(tethering);
262
263         if (ret != TETHERING_ERROR_NONE)
264                 BT_ERR("Fail to destroy tethering: %d", ret);
265
266         return (gboolean)enabled;
267 }
268
269 int bt_get_mime_type(char *file_name, char **mime_type)
270 {
271         BT_DBG("+");
272
273         int ret = MIME_TYPE_ERROR_NONE;
274
275         if (file_name == NULL)
276                 return BLUETOOTH_ERROR_INVALID_PARAM;
277
278         ret = mime_type_get_mime_type(file_name, (gchar **)mime_type);
279
280         if (ret != MIME_TYPE_ERROR_NONE) {
281                 BT_ERR("Fail to get mime type: %d", ret);
282                 return BLUETOOTH_ERROR_INTERNAL;
283         }
284
285         return BLUETOOTH_ERROR_NONE;
286 }
287
288 void bt_destroy_popup_all(void)
289 {
290         syspopup_destroy_all();
291 }
292
293
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
298         bt_launch_dpm_popup,
299 #else
300                 NULL,
301 #endif
302         bt_launch_system_popup,
303         bt_destroy_popup_all,
304         bt_launch_unable_to_pairing_syspopup,
305         bt_is_tethering_enabled,
306         bt_get_mime_type
307 };
308