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