2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <stacktrim.h>
24 #if defined(LIBNOTIFY_SUPPORT)
26 #elif defined(LIBNOTIFICATION_SUPPORT)
27 #include "bt-service-agent-notification.h"
29 #include <syspopup_caller.h>
33 #include <bundle_internal.h>
35 #ifdef TIZEN_NETWORK_TETHERING_ENABLE
36 #include <tethering.h>
39 #include "bt-internal-types.h"
40 #include "bt-service-common.h"
41 #include "bt-service-agent.h"
42 #include "bt-service-gap-agent.h"
43 #include "bt-service-adapter.h"
44 #include "bt-service-event.h"
45 #include "bt-service-rfcomm-server.h"
46 #include "bt-service-device.h"
47 #include "bt-service-audio.h"
49 #define BT_APP_AUTHENTICATION_TIMEOUT 35
50 #define BT_APP_AUTHORIZATION_TIMEOUT 15
52 #define HFP_AUDIO_GATEWAY_UUID "0000111f-0000-1000-8000-00805f9b34fb"
53 #define HSP_AUDIO_GATEWAY_UUID "00001112-0000-1000-8000-00805f9b34fb"
54 #define A2DP_UUID "0000110D-0000-1000-8000-00805F9B34FB"
55 #define AVRCP_TARGET_UUID "0000110c-0000-1000-8000-00805f9b34fb"
56 #define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
57 #define FTP_UUID "00001106-0000-1000-8000-00805f9b34fb"
58 #define SPP_UUID "00001101-0000-1000-8000-00805f9b34fb"
59 #define PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb"
60 #define MAP_UUID "00001132-0000-1000-8000-00805f9b34fb"
61 #define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
62 #define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
63 #define BNEP_UUID "0000000f-0000-1000-8000-00805f9b34fb"
64 #define HID_UUID "00001124-0000-1000-8000-00805f9b34fb"
65 #define SAP_UUID_OLD "a49eb41e-cb06-495c-9f4f-bb80a90cdf00"
66 #define SAP_UUID_NEW "a49eb41e-cb06-495c-9f4f-aa80a90cdf4a"
68 #define BT_AGENT_OBJECT "/org/bluez/agent/frwk_agent"
70 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
72 #define BT_AGENT_SIGNAL_RFCOMM_AUTHORIZE "RfcommAuthorize"
73 #define BT_AGENT_SIGNAL_OBEX_AUTHORIZE "ObexAuthorize"
75 #define BT_PIN_MAX_LENGTH 16
76 #define BT_PASSKEY_MAX_LENGTH 4
78 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
79 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
80 #define BT_PAN_MAX_CONNECTION 4
81 extern guint nap_connected_device_count;
83 #define G_VARIANT_UNREF(variant) \
84 g_variant_unref(variant); \
87 static gboolean syspopup_mode = TRUE;
89 static int __bt_agent_is_auto_response(uint32_t dev_class, const gchar *address,
91 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class);
92 static int __bt_agent_generate_passkey(char *passkey, int size);
94 static void __bt_agent_release_memory(void)
96 /* Release Malloc Memory*/
99 /* Release Stack Memory*/
103 static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
106 static int retry_count;
107 bundle *b = (bundle *)user_data;
108 retv_if(user_data == NULL, FALSE);
111 #if defined(LIBNOTIFY_SUPPORT)
112 ret = notify_launch(b);
113 #elif defined(LIBNOTIFICATION_SUPPORT)
114 ret = notification_launch(b);
116 ret = syspopup_launch("bt-syspopup", b);
119 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
121 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
122 BT_ERR("Sorry!! Max retry %d reached", retry_count);
128 BT_DBG("Hurray!! Finally Popup launched");
133 return (ret < 0) ? TRUE : FALSE;
136 #ifdef TIZEN_WEARABLE
137 static void __bt_popup_event_filter(GDBusConnection *connection,
138 const gchar *sender_name,
139 const gchar *object_path,
140 const gchar *interface_name,
141 const gchar *signal_name,
142 GVariant *parameters,
145 BT_DBG("Sender Name[%s] Object Path[%s] Interface[%s] Signal[%s]",
146 sender_name, object_path, interface_name, signal_name);
148 if (g_strcmp0(interface_name, "User.Bluetooth.syspopup") == 0 &&
149 g_strcmp0(signal_name, "ResetResponse") == 0) {
152 g_variant_get(parameters, "(i)", &response);
153 BT_DBG("response = %d", response);
157 int __bt_service_subscribe_popup(GDBusConnection *conn,
160 static guint subs_interface_added_id = 0;
163 return BLUETOOTH_ERROR_INVALID_PARAM;
166 if (subs_interface_added_id == 0) {
167 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
168 NULL, "User.Bluetooth.syspopup", "ResetResponse", NULL, NULL, 0,
169 __bt_popup_event_filter, NULL, NULL);
172 if (subs_interface_added_id > 0) {
173 g_dbus_connection_signal_unsubscribe(conn,
174 subs_interface_added_id);
175 subs_interface_added_id = 0;
178 return BLUETOOTH_ERROR_NONE;
181 static void __bt_register_popup_event_signal(void)
183 GDBusConnection *conn;
187 conn = _bt_get_system_gconn();
191 __bt_service_subscribe_popup(conn, TRUE);
197 static gboolean __is_reset_required(const gchar *address)
200 uint32_t no_of_device;
202 bluetooth_device_info_t info;
203 gboolean is_required = FALSE;
205 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
206 if (device_list == NULL) {
207 BT_ERR("g_array_new is failed");
211 if (_bt_get_bonded_devices(&device_list) != BLUETOOTH_ERROR_NONE) {
212 BT_ERR("_bt_get_bonded_devices is failed");
213 g_array_free(device_list, TRUE);
217 no_of_device = device_list->len / sizeof(bluetooth_device_info_t);
218 for (i = 0; i < no_of_device; i++) {
219 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
221 info = g_array_index(device_list, bluetooth_device_info_t, i);
223 _bt_convert_addr_type_to_string(addr, info.device_address.addr);
224 if (g_strcmp0(address, addr) == 0) {
225 BT_DBG("This device is already in paired list");
230 if (info.device_class.major_class != BLUETOOTH_DEVICE_MAJOR_CLASS_AUDIO) {
235 g_array_free(device_list, TRUE);
241 int _bt_launch_system_popup(bt_agent_event_type_t event_type,
242 const char *device_name,
244 const char *filename,
245 const char *agent_path)
249 char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
253 BT_ERR("Launching system popup failed");
257 bundle_add(b, "device-name", device_name);
258 bundle_add(b, "passkey", passkey);
259 bundle_add(b, "file", filename);
260 bundle_add(b, "agent-path", agent_path);
262 switch (event_type) {
263 case BT_AGENT_EVENT_PIN_REQUEST:
264 g_strlcpy(event_str, "pin-request", sizeof(event_str));
267 case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
268 g_strlcpy(event_str, "passkey-confirm-request",
272 case BT_AGENT_EVENT_PASSKEY_AUTO_ACCEPTED:
273 g_strlcpy(event_str, "passkey-auto-accepted",
277 case BT_AGENT_EVENT_PASSKEY_REQUEST:
278 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
281 case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
282 g_strlcpy(event_str, "passkey-display-request",
286 case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
287 g_strlcpy(event_str, "authorize-request",
291 case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
292 g_strlcpy(event_str, "confirm-mode-request",
296 case BT_AGENT_EVENT_FILE_RECEIVED:
297 g_strlcpy(event_str, "file-received", sizeof(event_str));
300 case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
301 g_strlcpy(event_str, "keyboard-passkey-request",
305 case BT_AGENT_EVENT_TERMINATE:
306 g_strlcpy(event_str, "terminate", sizeof(event_str));
309 case BT_AGENT_EVENT_EXCHANGE_REQUEST:
310 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
313 case BT_AGENT_EVENT_PBAP_REQUEST:
314 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
317 case BT_AGENT_EVENT_MAP_REQUEST:
318 g_strlcpy(event_str, "message-request", sizeof(event_str));
321 #ifdef TIZEN_WEARABLE
322 case BT_AGENT_EVENT_SYSTEM_RESET_REQUEST:
323 __bt_register_popup_event_signal();
324 g_strlcpy(event_str, "system-reset-request", sizeof(event_str));
328 case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
329 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
333 BT_ERR("Invalid event type");
339 bundle_add(b, "event-type", event_str);
341 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
342 ret = syspopup_launch("bt-syspopup", b);
345 BT_ERR("Popup launch failed...retry %d", ret);
347 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
348 (GSourceFunc)__bt_agent_system_popup_timer_cb, b);
353 BT_INFO("_bt_agent_launch_system_popup");
357 static GVariant *__bt_service_getall(GDBusProxy *device, const char *interface)
359 GError *error = NULL;
362 reply = g_dbus_proxy_call_sync(device,
363 "GetAll", g_variant_new("(s)", interface),
364 G_DBUS_CALL_FLAGS_NONE, -1,
367 ERR("GetAll dBUS-RPC failed");
369 ERR("D-Bus API failure: errCode[%x], message[%s]",
370 error->code, error->message);
371 g_clear_error(&error);
379 static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
381 uint32_t device_class;
382 const gchar *address;
384 GVariant *reply = NULL;
385 GVariant *reply_temp = NULL;
388 int result = BLUETOOTH_ERROR_NONE;
392 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
394 if (reply_temp == NULL) {
395 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
400 g_variant_get(reply_temp,"(@a{sv})", &reply); /* Format of reply a{sv}*/
402 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
403 g_variant_get(tmp_value, "u", &device_class);
404 G_VARIANT_UNREF(tmp_value);
406 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
407 g_variant_get(tmp_value, "s", &address);
408 G_VARIANT_UNREF(tmp_value);
410 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
414 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
415 g_variant_get(tmp_value, "s", &name);
416 G_VARIANT_UNREF(tmp_value);
420 if (_bt_is_device_creating() == TRUE &&
421 _bt_is_bonding_device_address(address) == TRUE &&
422 __bt_agent_is_auto_response(device_class, address, name)) {
423 BT_DBG("0000 Auto Pair");
424 /* Use Fixed PIN "0000" for basic pairing */
425 _bt_set_autopair_status_in_bonding_info(TRUE);
426 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
428 } else if (__bt_agent_is_hid_keyboard(device_class)) {
429 BT_DBG("HID Keyboard");
430 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
432 if (__bt_agent_generate_passkey(str_passkey,
433 BT_PASSKEY_MAX_LENGTH) != 0) {
434 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
439 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
443 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
447 BT_DBG("LAUNCH SYSPOPUP");
448 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
449 name, str_passkey, NULL,
450 _gap_agent_get_path(agent));
452 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
453 param = g_variant_new("(isss)", result, address, name, str_passkey);
454 _bt_send_event(BT_ADAPTER_EVENT,
455 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
459 BT_DBG("Show Pin entry");
462 BT_DBG("LAUNCH SYSPOPUP");
463 _bt_launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, NULL,
464 NULL, _gap_agent_get_path(agent));
466 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
467 param = g_variant_new("(iss)", result, address, name);
468 _bt_send_event(BT_ADAPTER_EVENT,
469 BLUETOOTH_EVENT_PIN_REQUEST, param);
474 g_variant_unref(reply);
475 g_variant_unref(reply_temp);
476 __bt_agent_release_memory();
482 static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
484 const gchar *address;
486 GVariant *reply = NULL;
487 GVariant *reply_temp = NULL;
491 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
493 if (reply_temp == NULL) {
494 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
499 g_variant_get(reply_temp,"(@a{sv})", &reply); /* Format of reply a{sv}*/
501 tmp_value = g_variant_lookup_value (reply, "Address", G_VARIANT_TYPE_STRING);
502 g_variant_get(tmp_value, "s", &address);
503 G_VARIANT_UNREF(tmp_value);
505 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
509 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
510 g_variant_get(tmp_value, "s", &name);
511 G_VARIANT_UNREF(tmp_value);
516 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
520 BT_DBG("LAUNCH SYSPOPUP");
521 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, NULL, NULL,
522 _gap_agent_get_path(agent));
524 int result = BLUETOOTH_ERROR_NONE;
527 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_REQUEST");
528 param = g_variant_new("(iss)", result, address, name);
529 _bt_send_event(BT_ADAPTER_EVENT,
530 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
535 g_variant_unref(reply);
536 g_variant_unref(reply_temp);
537 __bt_agent_release_memory();
543 static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
546 const gchar *address;
549 GVariant *reply = NULL;
550 GVariant *reply_temp = NULL;
551 GVariant *tmp_value = NULL;
555 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
556 if (reply_temp == NULL) {
557 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
562 g_variant_get(reply_temp,"(@a{sv})", &reply); /* Format of reply a{sv}*/
564 tmp_value = g_variant_lookup_value (reply, "Address", G_VARIANT_TYPE_STRING);
565 g_variant_get(tmp_value, "s", &address);
566 G_VARIANT_UNREF(tmp_value);
568 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
572 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
573 g_variant_get(tmp_value, "s", &name);
574 G_VARIANT_UNREF(tmp_value);
578 str_passkey = g_strdup_printf("%d", passkey);
581 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, str_passkey,
585 BT_DBG("LAUNCH SYSPOPUP");
586 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
588 _gap_agent_get_path(agent));
590 int result = BLUETOOTH_ERROR_NONE;
593 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
594 param = g_variant_new("(isss)", result, address, name, str_passkey);
595 _bt_send_event(BT_ADAPTER_EVENT,
596 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
603 g_variant_unref(reply);
604 g_variant_unref(reply_temp);
605 __bt_agent_release_memory();
611 static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
614 const gchar *address;
617 GVariant *reply_temp = NULL;
618 GVariant *reply = NULL;
620 BT_DBG("+ passkey[%.6d]", passkey);
622 snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
624 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
626 if (reply_temp == NULL) {
627 BT_ERR("Device doesn't exist");
628 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
633 g_variant_get(reply_temp,"(@a{sv})", &reply); /* Format of reply a{sv}*/
635 tmp_value = g_variant_lookup_value (reply, "Address", G_VARIANT_TYPE_STRING);
636 g_variant_get(tmp_value, "s", &address);
637 G_VARIANT_UNREF(tmp_value);
639 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
643 tmp_value = g_variant_lookup_value (reply, "Name", G_VARIANT_TYPE_STRING);
644 g_variant_get(tmp_value, "s", &name);
645 G_VARIANT_UNREF(tmp_value);
649 #ifdef TIZEN_WEARABLE
650 uint32_t device_class = 0x00;
651 uint32_t major_class;
653 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
654 g_variant_get(tmp_value, "u", &device_class);
655 G_VARIANT_UNREF(tmp_value);
657 major_class = (device_class & 0x1f00) >> 8;
659 if (major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_AUDIO) {
660 BT_DBG("Audio device. Launch passkey pop-up");
661 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
662 str_passkey, NULL, _gap_agent_get_path(agent));
666 if (__is_reset_required(address)) {
667 BT_INFO("Launch system reset pop-up");
668 _bt_launch_system_popup(BT_AGENT_EVENT_SYSTEM_RESET_REQUEST, name,
669 NULL, NULL, _gap_agent_get_path(agent));
671 BT_INFO("Launch passkey pop-up");
672 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_AUTO_ACCEPTED, name,
673 str_passkey, NULL, _gap_agent_get_path(agent));
675 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
679 BT_DBG("Confirm reply");
680 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
683 BT_DBG("LAUNCH SYSPOPUP");
684 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
686 _gap_agent_get_path(agent));
688 int result = BLUETOOTH_ERROR_NONE;
691 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST");
692 param = g_variant_new("(isss)", result, address, name, str_passkey);
693 _bt_send_event(BT_ADAPTER_EVENT,
694 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
700 g_variant_unref(reply);
701 g_variant_unref(reply_temp);
702 __bt_agent_release_memory();
708 static gboolean __pairing_cancel_request(GapAgentPrivate *agent, const char *address)
710 BT_DBG("On Going Pairing is cancelled by remote\n");
712 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
713 syspopup_destroy_all();
716 __bt_agent_release_memory();
721 static gboolean __a2dp_authorize_request_check(void)
723 /* Check for existing Media device to disconnect */
724 return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
727 static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
730 const gchar *address;
734 GVariant *reply = NULL;
735 GVariant *reply_temp = NULL;
737 #ifdef TIZEN_NETWORK_TETHERING_ENABLE
739 tethering_h tethering = NULL;
741 int result = BLUETOOTH_ERROR_NONE;
743 int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
749 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
754 /* Check if already Media connection exsist */
755 if (!strcasecmp(uuid, A2DP_UUID)) {
756 gboolean ret = FALSE;
758 ret = __a2dp_authorize_request_check();
761 BT_ERR("Already one A2DP device connected \n");
762 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
767 /* Check completed */
769 if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
770 !strcasecmp(uuid, HSP_AUDIO_GATEWAY_UUID) ||
771 !strcasecmp(uuid, HFP_HS_UUID) ||
772 !strcasecmp(uuid, HSP_HS_UUID) ||
773 !strcasecmp(uuid, A2DP_UUID) ||
774 !strcasecmp(uuid, HID_UUID) ||
775 !strcasecmp(uuid, SAP_UUID_OLD) ||
776 !strcasecmp(uuid, SAP_UUID_NEW) ||
777 !strcasecmp(uuid, AVRCP_TARGET_UUID)) {
778 BT_DBG("Auto accept authorization for audio device (HFP, A2DP, AVRCP) [%s]", uuid);
779 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
785 if (!strcasecmp(uuid, NAP_UUID) ||
786 !strcasecmp(uuid, GN_UUID) ||
787 !strcasecmp(uuid, BNEP_UUID)) {
789 BT_DBG("Network connection request: %s", uuid);
790 #ifdef TIZEN_NETWORK_TETHERING_ENABLE
791 if (nap_connected_device_count >=
792 BT_PAN_MAX_CONNECTION) {
793 BT_ERR("Max connection exceeded");
797 ret = tethering_create(&tethering);
799 if (ret != TETHERING_ERROR_NONE) {
800 BT_ERR("Fail to create tethering: %d", ret);
804 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
806 ret = tethering_destroy(tethering);
808 if (ret != TETHERING_ERROR_NONE) {
809 BT_ERR("Fail to create tethering: %d", ret);
812 if (enabled != true) {
813 BT_ERR("BT tethering is not enabled");
818 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
821 #ifdef TIZEN_NETWORK_TETHERING_ENABLE
823 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
830 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
831 if (reply_temp == NULL) {
832 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
837 g_variant_get(reply_temp,"(@a{sv})", &reply); /* Format of reply a{sv}*/
839 tmp_value = g_variant_lookup_value (reply, "Address", G_VARIANT_TYPE_STRING);
840 g_variant_get(tmp_value, "s", &address);
841 G_VARIANT_UNREF(tmp_value);
843 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
847 tmp_value = g_variant_lookup_value(reply, "Alias", G_VARIANT_TYPE_STRING);
848 g_variant_get(tmp_value, "s", &name);
849 G_VARIANT_UNREF(tmp_value);
853 tmp_value = g_variant_lookup_value(reply, "Trusted", G_VARIANT_TYPE_BOOLEAN);
854 g_variant_get(tmp_value, "b", &trust);
855 G_VARIANT_UNREF(tmp_value);
857 tmp_value = g_variant_lookup_value(reply, "Paired", G_VARIANT_TYPE_BOOLEAN);
858 g_variant_get(tmp_value, "b", &paired);
859 G_VARIANT_UNREF(tmp_value);
860 if ((paired == FALSE) && (trust == FALSE)) {
861 BT_ERR("No paired & No trusted device");
862 gap_agent_reply_authorize(agent,
863 GAP_AGENT_REJECT, NULL);
867 BT_INFO("Authorization request for device [%s] Service:[%s]\n", address, uuid);
869 if (strcasecmp(uuid, OPP_UUID) == 0 &&
870 _gap_agent_exist_osp_server(agent, BT_OBEX_SERVER,
872 _bt_send_event(BT_OPP_SERVER_EVENT,
873 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
874 g_variant_new("(iss)", result, address, name));
879 if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
880 (char *)uuid) == TRUE) {
881 bt_agent_osp_server_t *osp_serv;
882 osp_serv = _gap_agent_get_osp_server(agent,
883 BT_RFCOMM_SERVER, (char *)uuid);
886 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
887 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
888 g_variant_new("(issssn)", result, address, uuid,
889 name, osp_serv->path, osp_serv->fd));
896 BT_INFO("Trusted device, so authorize\n");
897 gap_agent_reply_authorize(agent,
898 GAP_AGENT_ACCEPT, NULL);
901 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
903 if (!strcasecmp(uuid, OPP_UUID))
904 request_type = BT_AGENT_EVENT_EXCHANGE_REQUEST;
905 else if (!strcasecmp(uuid, PBAP_UUID))
906 request_type = BT_AGENT_EVENT_PBAP_REQUEST;
907 else if (!strcasecmp(uuid, MAP_UUID))
908 request_type = BT_AGENT_EVENT_MAP_REQUEST;
910 _bt_launch_system_popup(request_type, name, NULL, NULL,
911 _gap_agent_get_path(agent));
916 g_variant_unref(reply);
917 g_variant_unref(reply_temp);
918 __bt_agent_release_memory();
924 static gboolean __authorization_cancel_request(GapAgentPrivate *agent,
927 BT_DBG("On Going Authorization is cancelled by remote\n");
929 gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
931 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
932 syspopup_destroy_all();
935 __bt_agent_release_memory();
940 void _bt_destroy_agent(void *agent)
945 _gap_agent_reset_dbus((GapAgentPrivate *)agent);
950 void* _bt_create_agent(const char *path, gboolean adapter)
952 GAP_AGENT_FUNC_CB func_cb;
953 GDBusProxy *adapter_proxy;
954 GapAgentPrivate *agent;
956 adapter_proxy = _bt_get_adapter_proxy();
960 func_cb.pincode_func = __pincode_request;
961 func_cb.display_func = __display_request;
962 func_cb.passkey_func = __passkey_request;
963 func_cb.confirm_func = __confirm_request;
964 func_cb.authorize_func = __authorize_request;
965 func_cb.pairing_cancel_func = __pairing_cancel_request;
966 func_cb.authorization_cancel_func = __authorization_cancel_request;
969 agent = g_new0(GapAgentPrivate, 1);
971 _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
974 if (!_gap_agent_register(agent)) {
975 _bt_destroy_agent(agent);
983 gboolean _bt_agent_register_osp_server(const gint type,
984 const char *uuid, char *path, int fd)
986 void *agent = _bt_get_adapter_agent();
990 return _gap_agent_register_osp_server(agent, type, uuid, path, fd);
994 gboolean _bt_agent_unregister_osp_server(const gint type, const char *uuid)
996 void *agent = _bt_get_adapter_agent();
1001 return _gap_agent_unregister_osp_server(agent, type, uuid);
1004 gboolean _bt_agent_reply_authorize(gboolean accept)
1008 void *agent = _bt_get_adapter_agent();
1012 accept_val = accept ? GAP_AGENT_ACCEPT : GAP_AGENT_REJECT;
1014 return gap_agent_reply_authorize(agent, accept_val, NULL);
1017 gboolean _bt_agent_is_canceled(void)
1019 void *agent = _bt_get_adapter_agent();
1023 return _gap_agent_is_canceled(agent);
1026 void _bt_agent_set_canceled(gboolean value)
1028 void *agent = _bt_get_adapter_agent();
1032 return _gap_agent_set_canceled(agent, value);
1035 int _bt_agent_reply_cancellation(void)
1037 void *agent = _bt_get_adapter_agent();
1040 return BLUETOOTH_ERROR_INTERNAL;
1042 if (gap_agent_reply_confirmation(agent, GAP_AGENT_CANCEL, NULL) != TRUE) {
1043 BT_ERR("Fail to reply agent");
1044 return BLUETOOTH_ERROR_INTERNAL;
1047 return BLUETOOTH_ERROR_NONE;
1050 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class)
1052 switch ((dev_class & 0x1f00) >> 8) {
1054 switch ((dev_class & 0xc0) >> 6) {
1056 /* input-keyboard" */
1065 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
1066 const char *address)
1071 pch = strtok_r(buffer, "= ,", &last);
1076 while ((pch = strtok_r(NULL, ",", &last))) {
1077 if (0 == g_strcmp0(pch, address)) {
1078 BT_DBG("Match found\n");
1085 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
1086 const char *partial_name)
1091 pch = strtok_r(buffer, "= ,", &last);
1096 while ((pch = strtok_r(NULL, ",", &last))) {
1097 if (g_str_has_prefix(partial_name, pch)) {
1098 BT_DBG("Match found\n");
1105 static gboolean __bt_agent_is_device_blacklist(const char *address,
1117 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
1120 BT_ERR("Unable to open blacklist file");
1124 fseek(fp, 0, SEEK_END);
1127 BT_DBG("size is not a positive number");
1134 buffer = g_malloc0(sizeof(char) * size);
1135 /* Fix : NULL_RETURNS */
1136 if (buffer == NULL) {
1137 BT_ERR("Fail to allocate memory");
1141 result = fread((char *)buffer, 1, size, fp);
1143 if (result != size) {
1144 BT_ERR("Read Error");
1149 BT_DBG("Buffer = %s", buffer);
1151 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
1154 if (lines == NULL) {
1155 BT_ERR("No lines in the file");
1159 for (i = 0; lines[i] != NULL; i++) {
1160 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
1161 if (__bt_agent_find_device_by_address_exactname(
1164 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
1165 if (__bt_agent_find_device_by_address_exactname(
1168 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
1169 if (__bt_agent_find_device_by_partial_name(lines[i],
1172 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1173 if (__bt_agent_find_device_by_address_exactname(
1181 BT_DBG("Found the device");
1186 static gboolean __bt_agent_is_auto_response(uint32_t dev_class,
1187 const gchar *address, const gchar *name)
1189 gboolean is_headset = FALSE;
1190 gboolean is_mouse = FALSE;
1191 char lap_address[BT_LOWER_ADDRESS_LENGTH];
1193 BT_DBG("bt_agent_is_headset_class, %d +", dev_class);
1195 if (address == NULL)
1198 switch ((dev_class & 0x1f00) >> 8) {
1200 switch ((dev_class & 0xfc) >> 2) {
1210 case 0x0b: /* VCR */
1211 case 0x0c: /* Video Camera */
1212 case 0x0d: /* Camcorder */
1215 /* Other audio device */
1221 switch (dev_class & 0xff) {
1222 case 0x80: /* 0x80: Pointing device(Mouse) */
1226 case 0x40: /* 0x40: input device (BT keyboard) */
1228 /* Get the LAP(Lower Address part) */
1229 g_strlcpy(lap_address, address, sizeof(lap_address));
1231 /* Need to Auto pair the blacklisted Keyboard */
1232 if (__bt_agent_is_device_blacklist(lap_address, name) != TRUE) {
1233 BT_DBG("Device is not black listed\n");
1236 BT_ERR("Device is black listed\n");
1242 if ((!is_headset) && (!is_mouse))
1245 /* Get the LAP(Lower Address part) */
1246 g_strlcpy(lap_address, address, sizeof(lap_address));
1248 BT_DBG("Device address = %s\n", address);
1249 BT_DBG("Address 3 byte = %s\n", lap_address);
1251 if (__bt_agent_is_device_blacklist(lap_address, name)) {
1252 BT_ERR("Device is black listed\n");
1259 static int __bt_agent_generate_passkey(char *passkey, int size)
1264 unsigned int value = 0;
1266 if (passkey == NULL)
1272 random_fd = open("/dev/urandom", O_RDONLY);
1277 for (i = 0; i < size; i++) {
1278 len = read(random_fd, &value, sizeof(value));
1280 passkey[i] = '0' + (value % 10);
1285 BT_DBG("passkey: %s", passkey);