Merge "Add the plugin for Headed/Headless device" 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
26 #include "plugin.h"
27 #include "bluetooth-api.h"
28
29 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
30 #define BT_MAX_EVENT_STR_LENGTH 50
31 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
32
33 #ifdef TIZEN_FEATURE_BT_DPM
34 int bt_launch_dpm_popup(char *mode)
35 {
36         int ret = 0;
37         bundle *b;
38
39         b = bundle_create();
40         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
41
42         bundle_add(b, "mode", mode);
43
44         ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
45
46         if (ret < 0)
47                 BT_ERR("Popup launch failed: %d\n", ret);
48
49         bundle_free(b);
50
51         return ret;
52 }
53 #endif
54
55 static gboolean _bt_syspopup_timer_cb(gpointer user_data)
56 {
57         int ret;
58         bundle *b;
59         retv_if(user_data == NULL, FALSE);
60
61         b = (bundle *)user_data;
62
63         ret = syspopup_launch("bt-syspopup", b);
64         if (ret < 0) {
65                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
66         } else {
67                 BT_DBG("Hurray!!! Finally Popup launched");
68                 bundle_free(b);
69         }
70         return (ret < 0) ? TRUE : FALSE;
71 }
72
73
74 static gboolean _bt_agent_system_popup_timer_cb(gpointer user_data)
75 {
76         int ret;
77         static int retry_count;
78         bundle *b = (bundle *)user_data;
79         retv_if(user_data == NULL, FALSE);
80
81         ++retry_count;
82
83         ret = syspopup_launch("bt-syspopup", b);
84         if (ret < 0) {
85                 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
86                                                         ret, retry_count);
87                 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
88                         BT_ERR("Sorry!! Max retry %d reached", retry_count);
89                         bundle_free(b);
90                         retry_count = 0;
91                         return FALSE;
92                 }
93         } else {
94                 BT_DBG("Hurray!! Finally Popup launched");
95                 retry_count = 0;
96                 bundle_free(b);
97         }
98
99         return (ret < 0) ? TRUE : FALSE;
100 }
101
102 int bt_launch_system_popup(bt_agent_event_type_t event_type,
103                                                         const char *device_name,
104                                                         const unsigned char *auth_info,
105                                                         char *passkey,
106                                                         const char *filename,
107                                                         const char *agent_path)
108 {
109         int ret;
110         bundle *b;
111         char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
112
113         b = bundle_create();
114         if (!b) {
115                 BT_ERR("Launching system popup failed");
116                 return -1;
117         }
118
119         bundle_add(b, "device-name", device_name);
120         bundle_add(b, "auth-info", (const char *)auth_info);
121         bundle_add(b, "passkey", passkey);
122         bundle_add(b, "file", filename);
123         bundle_add(b, "agent-path", agent_path);
124
125         switch (event_type) {
126         case BT_AGENT_EVENT_PIN_REQUEST:
127                 g_strlcpy(event_str, "pin-request", sizeof(event_str));
128                 break;
129
130         case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
131                 g_strlcpy(event_str, "passkey-confirm-request",
132                                                 sizeof(event_str));
133                 break;
134
135         case BT_AGENT_EVENT_PASSKEY_REQUEST:
136                 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
137                 break;
138
139         case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
140                 g_strlcpy(event_str, "passkey-display-request",
141                                                 sizeof(event_str));
142                 break;
143
144         case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
145                 g_strlcpy(event_str, "authorize-request",
146                                                 sizeof(event_str));
147                 break;
148
149         case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
150                 g_strlcpy(event_str, "confirm-mode-request",
151                                                 sizeof(event_str));
152                 break;
153
154         case BT_AGENT_EVENT_FILE_RECEIVED:
155                 g_strlcpy(event_str, "file-received", sizeof(event_str));
156                 break;
157
158         case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
159                 g_strlcpy(event_str, "keyboard-passkey-request",
160                                                 sizeof(event_str));
161                 break;
162
163         case BT_AGENT_EVENT_TERMINATE:
164                 g_strlcpy(event_str, "terminate", sizeof(event_str));
165                 break;
166
167         case BT_AGENT_EVENT_EXCHANGE_REQUEST:
168                 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
169                 break;
170
171         case BT_AGENT_EVENT_PBAP_REQUEST:
172                 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
173                 break;
174
175         case BT_AGENT_EVENT_MAP_REQUEST:
176                 g_strlcpy(event_str, "message-request", sizeof(event_str));
177                 break;
178
179         case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
180                 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
181                 break;
182
183         default:
184                 BT_ERR("Invalid event type");
185                 bundle_free(b);
186                 return -1;
187
188         }
189
190         bundle_add(b, "event-type", event_str);
191
192         ret = syspopup_launch("bt-syspopup", b);
193         if (0 > ret) {
194                 BT_ERR("Popup launch failed...retry %d", ret);
195
196                 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
197                               (GSourceFunc)_bt_agent_system_popup_timer_cb, b);
198         } else {
199                 bundle_free(b);
200         }
201
202         BT_INFO("_bt_agent_launch_system_popup");
203         return 0;
204 }
205
206 gboolean bt_launch_unable_to_pairing_syspopup(int result)
207 {
208         BT_DBG("+");
209         int ret = 0;
210         bundle *b = NULL;
211
212         b = bundle_create();
213         if (b == NULL)
214                 return FALSE;
215
216         bundle_add(b, "event-type", "unable-to-pairing");
217
218         if (result == BLUETOOTH_ERROR_TIMEOUT)
219                 bundle_add(b, "error", "timeout");
220         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
221                 bundle_add(b, "error", "authfailed");
222         else
223                 bundle_add(b, "error", "error");
224
225         ret = syspopup_launch("bt-syspopup", b);
226         if (0 > ret) {
227                 BT_ERR("Popup launch failed...retry %d \n", ret);
228                 g_timeout_add(200, (GSourceFunc) _bt_syspopup_timer_cb,
229                                 b);
230         } else {
231                 bundle_free(b);
232         }
233
234         BT_DBG("-");
235         return TRUE;
236 }
237
238
239 void bt_destroy_popup_all(void)
240 {
241         syspopup_destroy_all();
242 }
243
244
245 extern struct bluetooth_headed_plugin_t headed_plugin
246 __attribute__ ((visibility("default")));
247 struct bluetooth_headed_plugin_t headed_plugin = {
248 #ifdef TIZEN_FEATURE_BT_DPM
249         bt_launch_dpm_popup,
250 #else
251                 NULL,
252 #endif
253         bt_launch_system_popup,
254         bt_destroy_popup_all,
255         bt_launch_unable_to_pairing_syspopup
256 };
257
258
259
260
261
262
263
264
265
266
267
268
269