DPM: Sync API return value with other bt-service modules.
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-agent.c
1 /*
2  * Copyright (c) 2011 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 <unistd.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <malloc.h>
22 #include <stacktrim.h>
23 #include <syspopup_caller.h>
24 #include <vconf.h>
25 #include <bundle_internal.h>
26
27 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
28 #include <tethering.h>
29 #endif
30
31 #include "bt-internal-types.h"
32 #include "bt-service-common.h"
33 #include "bt-service-agent.h"
34 #include "bt-service-gap-agent.h"
35 #include "bt-service-adapter.h"
36 #include "bt-service-event.h"
37 #include "bt-service-rfcomm-server.h"
38 #include "bt-service-device.h"
39 #include "bt-service-audio.h"
40
41 #ifdef TIZEN_FEATURE_BT_DPM
42 #include "bt-service-dpm.h"
43 #endif
44
45 #define BT_APP_AUTHENTICATION_TIMEOUT           35
46 #define BT_APP_AUTHORIZATION_TIMEOUT            15
47
48 #define HFP_AUDIO_GATEWAY_UUID "0000111f-0000-1000-8000-00805f9b34fb"
49 #define HSP_AUDIO_GATEWAY_UUID "00001112-0000-1000-8000-00805f9b34fb"
50 #define A2DP_UUID "0000110D-0000-1000-8000-00805F9B34FB"
51 #define AVRCP_TARGET_UUID "0000110c-0000-1000-8000-00805f9b34fb"
52 #define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
53 #define FTP_UUID "00001106-0000-1000-8000-00805f9b34fb"
54 #define SPP_UUID "00001101-0000-1000-8000-00805f9b34fb"
55 #define PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb"
56 #define MAP_UUID "00001132-0000-1000-8000-00805f9b34fb"
57 #define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
58 #define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
59 #define BNEP_UUID "0000000f-0000-1000-8000-00805f9b34fb"
60 #define HID_UUID "00001124-0000-1000-8000-00805f9b34fb"
61 #define HID_DEVICE_UUID         "00001124-0000-1000-8000-00805f9b43bf"
62 #define SAP_UUID_OLD "a49eb41e-cb06-495c-9f4f-bb80a90cdf00"
63 #define SAP_UUID_NEW "a49eb41e-cb06-495c-9f4f-aa80a90cdf4a"
64 #define IOTIVITY_UUID "12341234-1C25-481F-9DFB-59193D238280"
65
66 #define BT_AGENT_OBJECT "/org/bluez/agent/frwk_agent"
67
68 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
69
70 #define BT_AGENT_SIGNAL_RFCOMM_AUTHORIZE "RfcommAuthorize"
71 #define BT_AGENT_SIGNAL_OBEX_AUTHORIZE "ObexAuthorize"
72
73 #define BT_PASSKEY_MAX_LENGTH 4
74
75 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
76 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
77 #define BT_PAN_MAX_CONNECTION 4
78
79 extern guint nap_connected_device_count;
80
81 static char *passkey_watcher = NULL;
82
83 #define G_VARIANT_UNREF(variant) \
84         g_variant_unref(variant); \
85         variant = NULL
86
87 static int __bt_agent_is_auto_response(uint32_t dev_class, const gchar *address,
88                                                         const gchar *name);
89 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class);
90 static int __bt_agent_generate_passkey(char *passkey, int size);
91
92 static void __bt_agent_release_memory(void)
93 {
94         /* Release Malloc Memory*/
95         malloc_trim(0);
96
97         /* Release Stack Memory*/
98         stack_trim();
99 }
100 static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
101 {
102         int ret;
103         static int retry_count;
104         bundle *b = (bundle *)user_data;
105         retv_if(user_data == NULL, FALSE);
106
107         ++retry_count;
108
109         ret = syspopup_launch("bt-syspopup", b);
110         if (ret < 0) {
111                 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
112                                                         ret, retry_count);
113                 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
114                         BT_ERR("Sorry!! Max retry %d reached", retry_count);
115                         bundle_free(b);
116                         retry_count = 0;
117                         return FALSE;
118                 }
119         } else {
120                 BT_DBG("Hurray!! Finally Popup launched");
121                 retry_count = 0;
122                 bundle_free(b);
123         }
124
125         return (ret < 0) ? TRUE : FALSE;
126 }
127
128 int _bt_launch_system_popup(bt_agent_event_type_t event_type,
129                                                         const char *device_name,
130                                                         const unsigned char *auth_info,
131                                                         char *passkey,
132                                                         const char *filename,
133                                                         const char *agent_path)
134 {
135         int ret;
136         bundle *b;
137         char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
138
139         b = bundle_create();
140         if (!b) {
141                 BT_ERR("Launching system popup failed");
142                 return -1;
143         }
144
145         bundle_add(b, "device-name", device_name);
146         bundle_add(b, "auth-info", (const char *)auth_info);
147         bundle_add(b, "passkey", passkey);
148         bundle_add(b, "file", filename);
149         bundle_add(b, "agent-path", agent_path);
150
151         switch (event_type) {
152         case BT_AGENT_EVENT_PIN_REQUEST:
153                 g_strlcpy(event_str, "pin-request", sizeof(event_str));
154                 break;
155
156         case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
157                 g_strlcpy(event_str, "passkey-confirm-request",
158                                                 sizeof(event_str));
159                 break;
160
161         case BT_AGENT_EVENT_PASSKEY_REQUEST:
162                 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
163                 break;
164
165         case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
166                 g_strlcpy(event_str, "passkey-display-request",
167                                                 sizeof(event_str));
168                 break;
169
170         case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
171                 g_strlcpy(event_str, "authorize-request",
172                                                 sizeof(event_str));
173                 break;
174
175         case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
176                 g_strlcpy(event_str, "confirm-mode-request",
177                                                 sizeof(event_str));
178                 break;
179
180         case BT_AGENT_EVENT_FILE_RECEIVED:
181                 g_strlcpy(event_str, "file-received", sizeof(event_str));
182                 break;
183
184         case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
185                 g_strlcpy(event_str, "keyboard-passkey-request",
186                                                 sizeof(event_str));
187                 break;
188
189         case BT_AGENT_EVENT_TERMINATE:
190                 g_strlcpy(event_str, "terminate", sizeof(event_str));
191                 break;
192
193         case BT_AGENT_EVENT_EXCHANGE_REQUEST:
194                 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
195                 break;
196
197         case BT_AGENT_EVENT_PBAP_REQUEST:
198                 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
199                 break;
200
201         case BT_AGENT_EVENT_MAP_REQUEST:
202                 g_strlcpy(event_str, "message-request", sizeof(event_str));
203                 break;
204
205         case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
206                 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
207                 break;
208
209         default:
210                 BT_ERR("Invalid event type");
211                 bundle_free(b);
212                 return -1;
213
214         }
215
216         bundle_add(b, "event-type", event_str);
217
218         ret = syspopup_launch("bt-syspopup", b);
219         if (0 > ret) {
220                 BT_ERR("Popup launch failed...retry %d", ret);
221
222                 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
223                               (GSourceFunc)__bt_agent_system_popup_timer_cb, b);
224         } else {
225                 bundle_free(b);
226         }
227
228         BT_INFO("_bt_agent_launch_system_popup");
229         return 0;
230 }
231
232 static GVariant *__bt_service_getall(GDBusProxy *device, const char *interface)
233 {
234         GError *error = NULL;
235         GVariant *reply;
236
237         reply = g_dbus_proxy_call_sync(device,
238                         "GetAll", g_variant_new("(s)", interface),
239                         G_DBUS_CALL_FLAGS_NONE, -1,
240                         NULL, &error);
241         if (reply == NULL) {
242                 ERR("GetAll dBUS-RPC failed");
243                 if (error) {
244                         ERR("D-Bus API failure: errCode[%x], message[%s]",
245                                 error->code, error->message);
246                         g_clear_error(&error);
247                 }
248                 return NULL;
249         }
250
251         return reply;
252 }
253
254 static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
255 {
256         uint32_t device_class;
257         gchar *address = NULL;
258         unsigned char auth_info[5] = {0, };
259         gchar *name = NULL;
260         GVariant *reply = NULL;
261         GVariant *reply_temp = NULL;
262         GVariant *tmp_value;
263         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
264 #ifdef TIZEN_FEATURE_BT_DPM
265         int pairing_state = DPM_STATUS_ERROR;
266 #endif
267
268         BT_DBG("+");
269
270 #ifdef TIZEN_FEATURE_BT_DPM
271         _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
272         if (pairing_state == DPM_RESTRICTED) {
273                 BT_ERR("Not allow to pair the device");
274                 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
275                 __bt_agent_release_memory();
276                 return TRUE;
277         }
278 #endif
279
280         reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
281
282         if (reply_temp == NULL) {
283                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
284                                 NULL);
285                 goto done;
286         }
287
288         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
289
290         tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
291         g_variant_get(tmp_value, "u", &device_class);
292         G_VARIANT_UNREF(tmp_value);
293
294         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
295         g_variant_get(tmp_value, "s", &address);
296         G_VARIANT_UNREF(tmp_value);
297         if (!address) {
298                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
299                 goto done;
300         }
301
302         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
303         g_variant_get(tmp_value, "s", &name);
304         G_VARIANT_UNREF(tmp_value);
305         if (!name) {
306                 BT_DBG("Replacing the name with address");
307                 name = g_strdup(address);
308         } else {
309                 BT_INFO("Name = %s, Address = %s, Class = 0x%x", name, address, device_class);
310                 if (name[0] == '\0') {
311                         g_free(name);
312                         BT_DBG("Name[0]=NULL, Replacing the name with address");
313                         name = g_strdup(address);
314                 }
315         }
316
317         __bt_get_auth_info(reply, (char *)auth_info);
318         if (_bt_is_device_creating() == TRUE &&
319                 _bt_is_bonding_device_address(address) == TRUE &&
320                 __bt_agent_is_auto_response(device_class, address, name)) {
321                 BT_DBG("0000 Auto Pair");
322                 /* Use Fixed PIN "0000" for basic pairing */
323                 _bt_set_autopair_status_in_bonding_info(TRUE);
324                 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
325                                                                         NULL);
326         } else if (__bt_agent_is_hid_keyboard(device_class)) {
327                 BT_DBG("HID Keyboard");
328                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
329
330                 if (__bt_agent_generate_passkey(str_passkey,
331                                         BT_PASSKEY_MAX_LENGTH) != 0) {
332                         gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
333                                                 "", NULL);
334                         goto done;
335                 }
336
337                 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
338                                                         str_passkey, NULL);
339
340                 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
341                                                 name, auth_info, str_passkey, NULL,
342                                                 _gap_agent_get_path(agent));
343         } else if (_bt_get_device_pin_code(address, pin_code)
344                                 == BLUETOOTH_ERROR_NONE) {
345                 BT_DBG("Use stored PIN code(%s)", pin_code);
346                 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, pin_code,
347                                                                         NULL);
348                 goto done;
349         } else {
350                 BT_DBG("Show Pin entry");
351                 _bt_launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, auth_info,
352                                         NULL, NULL, _gap_agent_get_path(agent));
353         }
354
355 done:
356         g_variant_unref(reply);
357         g_variant_unref(reply_temp);
358         g_free(address);
359         g_free(name);
360         __bt_agent_release_memory();
361         BT_DBG("-");
362
363         return TRUE;
364 }
365
366 static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
367 {
368         gchar *address = NULL;
369         gchar *name = NULL;
370         unsigned char auth_info[5] = {0, };
371         GVariant *reply = NULL;
372         GVariant *reply_temp = NULL;
373         GVariant *tmp_value;
374 #ifdef TIZEN_FEATURE_BT_DPM
375         int pairing_state = DPM_STATUS_ERROR;
376 #endif
377
378         BT_DBG("+");
379
380 #ifdef TIZEN_FEATURE_BT_DPM
381         _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
382         if (pairing_state == DPM_RESTRICTED) {
383                 BT_ERR("Not allow to pair the device");
384                 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
385                 __bt_agent_release_memory();
386                 return TRUE;
387         }
388 #endif
389
390         reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
391
392         if (reply_temp == NULL) {
393                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
394                                              NULL);
395                 goto done;
396         }
397
398         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
399
400         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
401         g_variant_get(tmp_value, "s", &address);
402         G_VARIANT_UNREF(tmp_value);
403         if (!address) {
404                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
405                 goto done;
406         }
407
408         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
409         g_variant_get(tmp_value, "s", &name);
410         G_VARIANT_UNREF(tmp_value);
411         if (!name)
412                 name = g_strdup(address);
413
414         __bt_get_auth_info(reply, (char *)auth_info);
415
416         _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
417                                                 NULL, NULL, _gap_agent_get_path(agent));
418
419 done:
420         g_variant_unref(reply);
421         g_variant_unref(reply_temp);
422         g_free(address);
423         g_free(name);
424         __bt_agent_release_memory();
425
426         BT_DBG("-");
427         return TRUE;
428 }
429
430 static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
431                                                                 guint passkey)
432 {
433         gchar *address = NULL;
434         gchar *name = NULL;
435         unsigned char auth_info[5] = {0, };
436         char *str_passkey;
437         GVariant *reply = NULL;
438         GVariant *reply_temp = NULL;
439         GVariant *tmp_value = NULL;
440
441         BT_DBG("+");
442
443         reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
444         if (reply_temp == NULL) {
445                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
446                                              NULL);
447                 goto done;
448         }
449
450         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
451
452         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
453         g_variant_get(tmp_value, "s", &address);
454         G_VARIANT_UNREF(tmp_value);
455         if (!address) {
456                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
457                 goto done;
458         }
459
460         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
461         g_variant_get(tmp_value, "s", &name);
462         G_VARIANT_UNREF(tmp_value);
463         if (!name)
464                 name = g_strdup(address);
465
466         __bt_get_auth_info(reply, (char *)auth_info);
467
468         str_passkey = g_strdup_printf("%06d", passkey);
469
470         if (passkey_watcher) {
471                 GVariant *param = NULL;
472
473                 BT_INFO("Send passkey to %s", passkey_watcher);
474
475                 param = g_variant_new("(ss)", address, str_passkey);
476
477                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
478                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
479         } else {
480                 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
481                                                 auth_info, str_passkey, NULL,
482                                                 _gap_agent_get_path(agent));
483         }
484
485         g_free(str_passkey);
486
487 done:
488         g_variant_unref(reply);
489         g_variant_unref(reply_temp);
490         g_free(address);
491         g_free(name);
492         __bt_agent_release_memory();
493
494         BT_DBG("-");
495         return TRUE;
496 }
497
498 static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
499                                                                 guint passkey)
500 {
501         gchar *address = NULL;
502         gchar *name = NULL;
503         unsigned char auth_info[5] = {0, };
504         char str_passkey[7];
505         GVariant *reply_temp = NULL;
506         GVariant *reply = NULL;
507         GVariant *tmp_value;
508 #ifdef TIZEN_FEATURE_BT_DPM
509                 int pairing_state = DPM_STATUS_ERROR;
510 #endif
511         BT_DBG("+ passkey[%.6d]", passkey);
512
513 #ifdef TIZEN_FEATURE_BT_DPM
514         _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
515         if (pairing_state == DPM_RESTRICTED) {
516                 BT_ERR("Not allow to pair the device");
517                 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
518                 __bt_agent_release_memory();
519                 return TRUE;
520         }
521 #endif
522
523         snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
524
525         reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
526
527         if (reply_temp == NULL) {
528                 BT_ERR("Device doesn't exist");
529                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
530                                              NULL);
531                 goto done;
532         }
533
534         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
535
536         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
537         g_variant_get(tmp_value, "s", &address);
538         G_VARIANT_UNREF(tmp_value);
539         if (!address) {
540                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
541                 goto done;
542         }
543
544         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
545         g_variant_get(tmp_value, "s", &name);
546         G_VARIANT_UNREF(tmp_value);
547         if (!name)
548                 name = g_strdup(address);
549         __bt_get_auth_info(reply, (char *)auth_info);
550
551         BT_DBG("LAUNCH SYSPOPUP");
552         _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
553                                                 auth_info, str_passkey, NULL,
554                                                 _gap_agent_get_path(agent));
555
556 done:
557         g_variant_unref(reply);
558         g_variant_unref(reply_temp);
559         g_free(address);
560         g_free(name);
561         __bt_agent_release_memory();
562         BT_DBG("-");
563
564         return TRUE;
565 }
566
567 static gboolean __pairing_cancel_request(GapAgentPrivate *agent, const char *address)
568 {
569         BT_DBG("On Going Pairing is cancelled by remote\n");
570
571         syspopup_destroy_all();
572
573         __bt_agent_release_memory();
574
575         return TRUE;
576 }
577
578 static gboolean __a2dp_authorize_request_check(void)
579 {
580         /* Check for existing Media device to disconnect */
581         return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
582 }
583
584 static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
585                                                         const char *uuid)
586 {
587         gchar *address = NULL;
588         gchar *name = NULL;
589         unsigned char auth_info[5] = {0, };
590         gboolean trust;
591         GVariant *reply = NULL;
592         GVariant *reply_temp = NULL;
593         GVariant *tmp_value;
594 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
595         bool enabled;
596         tethering_h tethering = NULL;
597 #endif
598         int result = BLUETOOTH_ERROR_NONE;
599         int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
600
601         BT_DBG("+");
602
603         /* Check if already Media connection exsist */
604         if (!strcasecmp(uuid, A2DP_UUID)) {
605                 gboolean ret = FALSE;
606
607                 ret = __a2dp_authorize_request_check();
608
609                 if (ret) {
610                         BT_ERR("Already one A2DP device connected \n");
611                         gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
612                                               NULL);
613                         goto done;
614                 }
615         }
616         /* Check completed */
617
618         if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
619              !strcasecmp(uuid, HSP_AUDIO_GATEWAY_UUID) ||
620              !strcasecmp(uuid, HFP_HS_UUID) ||
621              !strcasecmp(uuid, HSP_HS_UUID) ||
622              !strcasecmp(uuid, A2DP_UUID) ||
623              !strcasecmp(uuid, HID_UUID) ||
624              !strcasecmp(uuid, HID_DEVICE_UUID) ||
625              !strcasecmp(uuid, SAP_UUID_OLD) ||
626              !strcasecmp(uuid, SAP_UUID_NEW) ||
627              !strcasecmp(uuid, IOTIVITY_UUID) ||
628              !strcasecmp(uuid, AVRCP_TARGET_UUID)) {
629                 BT_DBG("Auto accept authorization for audio device (HFP, A2DP, AVRCP) [%s]", uuid);
630                 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
631                                               NULL);
632
633                 goto done;
634         }
635
636         if (!strcasecmp(uuid, NAP_UUID) ||
637              !strcasecmp(uuid, GN_UUID) ||
638               !strcasecmp(uuid, BNEP_UUID)) {
639
640                 BT_DBG("Network connection request: %s", uuid);
641 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
642                 if (nap_connected_device_count >=
643                                         BT_PAN_MAX_CONNECTION) {
644                         BT_ERR("Max connection exceeded");
645                         goto fail;
646                 }
647                 int ret;
648                 ret = tethering_create(&tethering);
649
650                 if (ret != TETHERING_ERROR_NONE) {
651                         BT_ERR("Fail to create tethering: %d", ret);
652                         goto fail;
653                 }
654
655                 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
656
657                 ret = tethering_destroy(tethering);
658
659                 if (ret != TETHERING_ERROR_NONE)
660                         BT_ERR("Fail to create tethering: %d", ret);
661
662                 if (enabled != true) {
663                         BT_ERR("BT tethering is not enabled");
664                         goto fail;
665                 }
666 #endif
667
668                 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
669                                               NULL);
670                 goto done;
671 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
672 fail:
673                 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
674                       NULL);
675
676                 goto done;
677 #endif
678         }
679
680         reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
681         if (reply_temp == NULL) {
682                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
683                                              NULL);
684                 goto done;
685         }
686
687         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
688
689         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
690         g_variant_get(tmp_value, "s", &address);
691         G_VARIANT_UNREF(tmp_value);
692         if (!address) {
693                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
694                 goto done;
695         }
696
697         tmp_value = g_variant_lookup_value(reply, "Alias", G_VARIANT_TYPE_STRING);
698         g_variant_get(tmp_value, "s", &name);
699         G_VARIANT_UNREF(tmp_value);
700         if (!name)
701                 name = g_strdup(address);
702
703         tmp_value = g_variant_lookup_value(reply, "Trusted", G_VARIANT_TYPE_BOOLEAN);
704         g_variant_get(tmp_value, "b", &trust);
705         G_VARIANT_UNREF(tmp_value);
706
707         __bt_get_auth_info(reply, (char *)auth_info);
708
709         BT_INFO("Authorization request for device [%s] Service:[%s]\n", address, uuid);
710
711         if (strcasecmp(uuid, OPP_UUID) == 0 &&
712              _gap_agent_exist_osp_server(agent, BT_OBEX_SERVER,
713                                         NULL) == TRUE) {
714                 _bt_send_event(BT_OPP_SERVER_EVENT,
715                                 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
716                                 g_variant_new("(iss)", result, address, name));
717
718                 goto done;
719         }
720
721         if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
722                                         (char *)uuid) == TRUE) {
723                 bt_agent_osp_server_t *osp_serv;
724                 osp_serv = _gap_agent_get_osp_server(agent,
725                                                 BT_RFCOMM_SERVER, (char *)uuid);
726
727                 if (osp_serv) {
728                         _bt_send_event(BT_RFCOMM_SERVER_EVENT,
729                                 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
730                                 g_variant_new("(issssn)", result, address, uuid,
731                                                 name, osp_serv->path, osp_serv->fd));
732                 }
733
734                 goto done;
735         }
736
737         if (!strcasecmp(uuid, OPP_UUID))
738                 request_type = BT_AGENT_EVENT_EXCHANGE_REQUEST;
739         else if (!strcasecmp(uuid, PBAP_UUID))
740                 request_type = BT_AGENT_EVENT_PBAP_REQUEST;
741         else if (!strcasecmp(uuid, MAP_UUID))
742                 request_type = BT_AGENT_EVENT_MAP_REQUEST;
743
744         if (trust) {
745                 BT_INFO("Trusted device, so authorize\n");
746                 gap_agent_reply_authorize(agent,
747                                               GAP_AGENT_ACCEPT, NULL);
748         } else {
749                 _bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
750                                                 _gap_agent_get_path(agent));
751         }
752
753 done:
754         if (reply)
755                 g_variant_unref(reply);
756
757         if (reply_temp)
758                 g_variant_unref(reply_temp);
759
760         g_free(name);
761         g_free(address);
762         __bt_agent_release_memory();
763         BT_DBG("-");
764
765         return TRUE;
766 }
767
768 static gboolean __authorization_cancel_request(GapAgentPrivate *agent,
769                                                         const char *address)
770 {
771         BT_DBG("On Going Authorization is cancelled by remote\n");
772
773         gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
774
775         syspopup_destroy_all();
776
777         __bt_agent_release_memory();
778
779         return TRUE;
780 }
781
782 void _bt_destroy_agent(void *agent)
783 {
784         if (!agent)
785                 return;
786
787         _gap_agent_reset_dbus((GapAgentPrivate *)agent);
788
789         g_free(agent);
790 }
791
792 void* _bt_create_agent(const char *path, gboolean adapter)
793 {
794         GAP_AGENT_FUNC_CB func_cb;
795         GDBusProxy *adapter_proxy;
796         GapAgentPrivate *agent;
797
798         adapter_proxy = _bt_get_adapter_proxy();
799         if (!adapter_proxy)
800                 return NULL;
801
802         func_cb.pincode_func = __pincode_request;
803         func_cb.display_func = __display_request;
804         func_cb.passkey_func = __passkey_request;
805         func_cb.confirm_func = __confirm_request;
806         func_cb.authorize_func = __authorize_request;
807         func_cb.pairing_cancel_func = __pairing_cancel_request;
808         func_cb.authorization_cancel_func = __authorization_cancel_request;
809
810         /* Allocate memory*/
811         agent = g_new0(GapAgentPrivate, 1);
812
813         _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
814
815         if (adapter) {
816                 if (!_gap_agent_register(agent)) {
817                         _bt_destroy_agent(agent);
818                         agent = NULL;
819                 }
820         }
821
822         return agent;
823 }
824
825 gboolean _bt_agent_register_osp_server(const gint type,
826                 const char *uuid, char *path, int fd)
827 {
828         void *agent = _bt_get_adapter_agent();
829         if (!agent)
830                 return FALSE;
831
832         return _gap_agent_register_osp_server(agent, type, uuid, path, fd);
833
834 }
835
836 gboolean _bt_agent_unregister_osp_server(const gint type, const char *uuid)
837 {
838         void *agent = _bt_get_adapter_agent();
839
840         if (!agent)
841                 return FALSE;
842
843         return _gap_agent_unregister_osp_server(agent, type, uuid);
844 }
845
846 gboolean _bt_agent_reply_authorize(gboolean accept)
847 {
848         guint accept_val;
849
850         void *agent = _bt_get_adapter_agent();
851         if (!agent)
852                 return FALSE;
853
854         accept_val = accept ? GAP_AGENT_ACCEPT : GAP_AGENT_REJECT;
855
856         return gap_agent_reply_authorize(agent, accept_val, NULL);
857 }
858
859 gboolean _bt_agent_is_canceled(void)
860 {
861         void *agent = _bt_get_adapter_agent();
862         if (!agent)
863                 return FALSE;
864
865         return _gap_agent_is_canceled(agent);
866 }
867
868 void _bt_agent_set_canceled(gboolean value)
869 {
870         void *agent = _bt_get_adapter_agent();
871         if (!agent)
872                 return;
873
874         return _gap_agent_set_canceled(agent, value);
875 }
876
877 int _bt_agent_reply_cancellation(void)
878 {
879         void *agent = _bt_get_adapter_agent();
880
881         if (!agent)
882                 return BLUETOOTH_ERROR_INTERNAL;
883
884         if (gap_agent_reply_confirmation(agent, GAP_AGENT_CANCEL, NULL) != TRUE) {
885                 BT_ERR("Fail to reply agent");
886                 return BLUETOOTH_ERROR_INTERNAL;
887         }
888
889         return BLUETOOTH_ERROR_NONE;
890 }
891
892 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class)
893 {
894         switch ((dev_class & 0x1f00) >> 8) {
895         case 0x05:
896                 switch ((dev_class & 0xc0) >> 6) {
897                 case 0x01:
898                         /* input-keyboard" */
899                         return TRUE;
900                 }
901                 break;
902         }
903
904         return FALSE;
905 }
906
907 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
908                                                         const char *address)
909 {
910         char *pch;
911         char *last;
912
913         pch = strtok_r(buffer, "= ,", &last);
914
915         if (pch == NULL)
916                 return FALSE;
917
918         while ((pch = strtok_r(NULL, ",", &last))) {
919                 if (0 == g_strcmp0(pch, address)) {
920                         BT_DBG("Match found\n");
921                         return TRUE;
922                 }
923         }
924         return FALSE;
925 }
926
927 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
928                                                 const char *partial_name)
929 {
930         char *pch;
931         char *last;
932
933         pch = strtok_r(buffer, "= ,", &last);
934
935         if (pch == NULL)
936                 return FALSE;
937
938         while ((pch = strtok_r(NULL, ",", &last))) {
939                 if (g_str_has_prefix(partial_name, pch)) {
940                         BT_DBG("Match found\n");
941                         return TRUE;
942                 }
943         }
944         return FALSE;
945 }
946
947 static gboolean __bt_agent_is_device_blacklist(const char *address,
948                                                         const char *name)
949 {
950         char *buffer;
951         char **lines;
952         int i;
953         FILE *fp;
954         long size;
955         size_t result;
956
957         BT_DBG("+");
958
959         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
960
961         if (fp == NULL) {
962                 BT_ERR("Unable to open blacklist file");
963                 return FALSE;
964         }
965
966         fseek(fp, 0, SEEK_END);
967         size = ftell(fp);
968         if (size <= 0) {
969                 BT_DBG("size is not a positive number");
970                 fclose(fp);
971                 return FALSE;
972         }
973
974         rewind(fp);
975
976         buffer = g_malloc0(sizeof(char) * size);
977         result = fread((char *)buffer, 1, size, fp);
978         fclose(fp);
979         if (result != size) {
980                 BT_ERR("Read Error");
981                 g_free(buffer);
982                 return FALSE;
983         }
984
985         BT_DBG("Buffer = %s", buffer);
986
987         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
988         g_free(buffer);
989
990         if (lines == NULL) {
991                 BT_ERR("No lines in the file");
992                 return FALSE;
993         }
994
995         for (i = 0; lines[i] != NULL; i++) {
996                 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
997                         if (__bt_agent_find_device_by_address_exactname(
998                                                 lines[i], address))
999                                 goto done;
1000                 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
1001                         if (__bt_agent_find_device_by_address_exactname(
1002                                                 lines[i], name))
1003                                 goto done;
1004                 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
1005                         if (__bt_agent_find_device_by_partial_name(lines[i],
1006                                                                 name))
1007                                 goto done;
1008                 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1009                         if (__bt_agent_find_device_by_address_exactname(
1010                                                 lines[i], address))
1011                                 goto done;
1012         }
1013         g_strfreev(lines);
1014         BT_DBG("-");
1015         return FALSE;
1016 done:
1017         BT_DBG("Found the device");
1018         g_strfreev(lines);
1019         return TRUE;
1020 }
1021
1022 static gboolean __bt_agent_is_auto_response(uint32_t dev_class,
1023                                 const gchar *address, const gchar *name)
1024 {
1025         gboolean is_headset = FALSE;
1026         gboolean is_mouse = FALSE;
1027         char lap_address[BT_LOWER_ADDRESS_LENGTH];
1028
1029         BT_DBG("bt_agent_is_headset_class, %d +", dev_class);
1030
1031         if (address == NULL)
1032                 return FALSE;
1033
1034         switch ((dev_class & 0x1f00) >> 8) {
1035         case 0x04:
1036                 switch ((dev_class & 0xfc) >> 2) {
1037                 case 0x01:
1038                 case 0x02:
1039                         /* Headset */
1040                         is_headset = TRUE;
1041                         break;
1042                 case 0x06:
1043                         /* Headphone */
1044                         is_headset = TRUE;
1045                         break;
1046                 case 0x0b:      /* VCR */
1047                 case 0x0c:      /* Video Camera */
1048                 case 0x0d:      /* Camcorder */
1049                         break;
1050                 default:
1051                         /* Other audio device */
1052                         is_headset = TRUE;
1053                         break;
1054                 }
1055                 break;
1056         case 0x05:
1057                 switch (dev_class & 0xff) {
1058                 case 0x80:  /* 0x80: Pointing device(Mouse) */
1059                         is_mouse = TRUE;
1060                         break;
1061
1062                 case 0x40: /* 0x40: input device (BT keyboard) */
1063
1064                         /* Get the LAP(Lower Address part) */
1065                         g_strlcpy(lap_address, address, sizeof(lap_address));
1066
1067                         /* Need to Auto pair the blacklisted Keyboard */
1068                         if (__bt_agent_is_device_blacklist(lap_address, name) != TRUE) {
1069                                 BT_DBG("Device is not black listed\n");
1070                                 return FALSE;
1071                         } else {
1072                                 BT_ERR("Device is black listed\n");
1073                                 return TRUE;
1074                         }
1075                 }
1076         }
1077
1078         if ((!is_headset) && (!is_mouse))
1079                 return FALSE;
1080
1081         /* Get the LAP(Lower Address part) */
1082         g_strlcpy(lap_address, address, sizeof(lap_address));
1083
1084         BT_DBG("Device address = %s\n", address);
1085         BT_DBG("Address 3 byte = %s\n", lap_address);
1086
1087         if (__bt_agent_is_device_blacklist(lap_address, name)) {
1088                 BT_ERR("Device is black listed\n");
1089                 return FALSE;
1090         }
1091
1092         return TRUE;
1093 }
1094
1095 static int __bt_agent_generate_passkey(char *passkey, int size)
1096 {
1097         int i;
1098         ssize_t len;
1099         int random_fd;
1100         unsigned int value = 0;
1101
1102         if (passkey == NULL)
1103                 return -1;
1104
1105         if (size <= 0)
1106                 return -1;
1107
1108         random_fd = open("/dev/urandom", O_RDONLY);
1109
1110         if (random_fd < 0)
1111                 return -1;
1112
1113         for (i = 0; i < size; i++) {
1114                 len = read(random_fd, &value, sizeof(value));
1115                 if (len > 0)
1116                         passkey[i] = '0' + (value % 10);
1117         }
1118
1119         close(random_fd);
1120
1121         BT_DBG("passkey: %s", passkey);
1122
1123         return 0;
1124 }
1125
1126 int _bt_set_passkey_notification(const char *sender, gboolean enable)
1127 {
1128         BT_INFO("Set passkey notification(sender:%s, %s)",
1129                         sender, enable ? "Enable" : "Disable");
1130
1131         g_free(passkey_watcher);
1132         if (enable == TRUE)
1133                 passkey_watcher = g_strdup(sender);
1134         else
1135                 passkey_watcher = NULL;
1136
1137         return BLUETOOTH_ERROR_NONE;
1138 }