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