4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <stacktrim.h>
26 #include <tethering.h>
28 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-agent.h"
31 #include "bt-service-gap-agent.h"
32 #include "bt-service-adapter.h"
33 #include "bt-service-event.h"
34 #include "bt-service-rfcomm-server.h"
35 #include "bt-service-device.h"
36 #include "bt-service-audio.h"
38 #if defined(LIBNOTIFY_SUPPORT)
40 #elif defined(LIBNOTIFICATION_SUPPORT)
41 #include "bt-service-agent-notification.h"
43 #include <syspopup_caller.h>
46 #define BT_APP_AUTHENTICATION_TIMEOUT 35
47 #define BT_APP_AUTHORIZATION_TIMEOUT 15
49 #define HFP_AUDIO_GATEWAY_UUID "0000111f-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"
62 #define BT_AGENT_OBJECT "/org/bluez/agent/frwk_agent"
63 #define BT_AGENT_INTERFACE "org.bluez.Agent"
64 #define BT_AGENT_SIGNAL_RFCOMM_AUTHORIZE "RfcommAuthorize"
65 #define BT_AGENT_SIGNAL_OBEX_AUTHORIZE "ObexAuthorize"
67 #define BT_PIN_MAX_LENGTH 16
68 #define BT_PASSKEY_MAX_LENGTH 4
70 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
72 static int __bt_agent_is_auto_response(uint32_t dev_class, const gchar *address,
74 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class);
75 static int __bt_agent_generate_passkey(char *passkey, int size);
77 static void __bt_agent_release_memory(void)
79 /* Release Malloc Memory*/
82 /* Release Stack Memory*/
86 static int __syspopup_launch(gpointer user_data)
89 bundle *b = (bundle *) user_data;
90 #if defined(LIBNOTIFY_SUPPORT)
91 ret = notify_launch(b);
92 #elif defined(LIBNOTIFICATION_SUPPORT)
93 ret = notification_launch(b);
95 ret = syspopup_launch("bt-syspopup", b);
100 static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
103 bundle *b = (bundle *) user_data;
106 BT_DBG("There is some problem with the user data..popup can not be created\n");
109 ret = __syspopup_launch(b);
112 BT_DBG("Sorry Can not launch popup\n");
114 BT_DBG("Hurray Popup launched \n");
120 static int __launch_system_popup(bt_agent_event_type_t event_type,
121 const char *device_name,
123 const char *filename,
124 const char *agent_path)
128 char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
130 BT_DBG("_bt_agent_launch_system_popup +");
136 bundle_add(b, "device-name", device_name);
137 bundle_add(b, "passkey", passkey);
138 bundle_add(b, "file", filename);
139 bundle_add(b, "agent-path", agent_path);
141 switch (event_type) {
142 case BT_AGENT_EVENT_PIN_REQUEST:
143 g_strlcpy(event_str, "pin-request", sizeof(event_str));
146 case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
147 g_strlcpy(event_str, "passkey-confirm-request",
151 case BT_AGENT_EVENT_PASSKEY_REQUEST:
152 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
155 case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
156 g_strlcpy(event_str, "passkey-display-request",
160 case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
161 g_strlcpy(event_str, "authorize-request",
165 case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
166 g_strlcpy(event_str, "confirm-mode-request",
170 case BT_AGENT_EVENT_FILE_RECEIVED:
171 g_strlcpy(event_str, "file-received", sizeof(event_str));
174 case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
175 g_strlcpy(event_str, "keyboard-passkey-request",
179 case BT_AGENT_EVENT_TERMINATE:
180 g_strlcpy(event_str, "terminate", sizeof(event_str));
183 case BT_AGENT_EVENT_EXCHANGE_REQUEST:
184 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
187 case BT_AGENT_EVENT_PBAP_REQUEST:
188 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
191 case BT_AGENT_EVENT_MAP_REQUEST:
192 g_strlcpy(event_str, "message-request", sizeof(event_str));
201 bundle_add(b, "event-type", event_str);
203 ret = __syspopup_launch(b);
205 BT_DBG("Popup launch failed...retry %d\n", ret);
206 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
207 (GSourceFunc) __bt_agent_system_popup_timer_cb,
213 BT_DBG("_bt_agent_launch_system_popup -%d", ret);
217 static gboolean __pincode_request(GapAgent *agent, DBusGProxy *device)
219 uint32_t device_class;
220 GHashTable *hash = NULL;
222 const gchar *address;
224 GError *error = NULL;
228 dbus_g_proxy_call(device, "GetProperties", &error,
230 dbus_g_type_get_map("GHashTable",
231 G_TYPE_STRING, G_TYPE_VALUE),
232 &hash, G_TYPE_INVALID);
234 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
236 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
241 value = g_hash_table_lookup(hash, "Class");
242 device_class = value ? g_value_get_uint(value) : 0;
244 value = g_hash_table_lookup(hash, "Address");
245 address = value ? g_value_get_string(value) : NULL;
247 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
251 value = g_hash_table_lookup(hash, "Name");
252 name = value ? g_value_get_string(value) : NULL;
256 if (__bt_agent_is_auto_response(device_class, address, name)) {
257 /* Use Fixed PIN "0000" for basic pairing*/
258 _bt_set_autopair_status_in_bonding_info(TRUE);
259 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
261 } else if (__bt_agent_is_hid_keyboard(device_class)) {
262 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
264 if (__bt_agent_generate_passkey(str_passkey,
265 BT_PASSKEY_MAX_LENGTH) != 0) {
266 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
271 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
274 __launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
275 name, str_passkey, NULL,
276 _gap_agent_get_path(agent));
278 __launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, NULL,
279 NULL, _gap_agent_get_path(agent));
283 g_hash_table_destroy(hash);
284 __bt_agent_release_memory();
291 static gboolean __passkey_request(GapAgent *agent, DBusGProxy *device)
293 uint32_t device_class;
294 GHashTable *hash = NULL;
296 const gchar *address;
298 GError *error = NULL;
302 dbus_g_proxy_call(device, "GetProperties", &error,
304 dbus_g_type_get_map("GHashTable",
305 G_TYPE_STRING, G_TYPE_VALUE),
306 &hash, G_TYPE_INVALID);
308 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
310 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
315 value = g_hash_table_lookup(hash, "Class");
316 device_class = value ? g_value_get_uint(value) : 0;
318 value = g_hash_table_lookup(hash, "Address");
319 address = value ? g_value_get_string(value) : NULL;
321 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
325 value = g_hash_table_lookup(hash, "Name");
326 name = value ? g_value_get_string(value) : NULL;
330 __launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, NULL, NULL,
331 _gap_agent_get_path(agent));
334 __bt_agent_release_memory();
335 g_hash_table_destroy(hash);
341 static gboolean __display_request(GapAgent *agent, DBusGProxy *device,
344 GHashTable *hash = NULL;
346 const gchar *address;
348 GError *error = NULL;
353 dbus_g_proxy_call(device, "GetProperties", &error,
355 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
357 &hash, G_TYPE_INVALID);
359 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
361 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
366 value = g_hash_table_lookup(hash, "Address");
367 address = value ? g_value_get_string(value) : NULL;
369 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
373 value = g_hash_table_lookup(hash, "Name");
374 name = value ? g_value_get_string(value) : NULL;
378 str_passkey = g_strdup_printf("%d", passkey);
380 __launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
382 _gap_agent_get_path(agent));
387 __bt_agent_release_memory();
388 g_hash_table_destroy(hash);
394 static gboolean __confirm_request(GapAgent *agent, DBusGProxy *device,
397 uint32_t device_class;
398 GHashTable *hash = NULL;
400 const gchar *address;
402 GError *error = NULL;
405 BT_DBG("+ passkey[%.6d]\n", passkey);
407 snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
409 dbus_g_proxy_call(device, "GetProperties", &error,
411 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
413 &hash, G_TYPE_INVALID);
415 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
417 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
422 value = g_hash_table_lookup(hash, "Class");
423 device_class = value ? g_value_get_uint(value) : 0;
425 value = g_hash_table_lookup(hash, "Address");
426 address = value ? g_value_get_string(value) : NULL;
428 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
432 value = g_hash_table_lookup(hash, "Name");
433 name = value ? g_value_get_string(value) : NULL;
437 __launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
439 _gap_agent_get_path(agent));
442 __bt_agent_release_memory();
443 g_hash_table_destroy(hash);
450 static gboolean __pairing_cancel_request(GapAgent *agent, const char *address)
452 BT_DBG("On Going Pairing is cancelled by remote\n");
454 gap_agent_reply_pin_code(agent, GAP_AGENT_CANCEL, "", NULL);
456 #ifndef LIBNOTIFY_SUPPORT
457 syspopup_destroy_all();
460 __bt_agent_release_memory();
465 static gboolean __a2dp_authorize_request_check(void)
467 /* Check for existing Media device to disconnect */
468 return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
471 static gboolean __authorize_request(GapAgent *agent, DBusGProxy *device,
474 GHashTable *hash = NULL;
476 const gchar *address;
481 tethering_h tethering = NULL;
482 GError *error = NULL;
484 int result = BLUETOOTH_ERROR_NONE;
485 int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
491 /* Check if already Media connection exsist */
492 if (!strcasecmp(uuid, A2DP_UUID)) {
493 gboolean ret = FALSE;
495 ret = __a2dp_authorize_request_check();
498 BT_DBG("Already one A2DP device connected \n");
499 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
504 /* Check completed */
506 if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
507 !strcasecmp(uuid, A2DP_UUID) ||
508 !strcasecmp(uuid, HID_UUID) ||
509 !strcasecmp(uuid, AVRCP_TARGET_UUID)) {
510 BT_DBG("Auto accept authorization for audio device (HFP, A2DP, AVRCP) [%s]", uuid);
511 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
517 if (!strcasecmp(uuid, NAP_UUID) ||
518 !strcasecmp(uuid, GN_UUID) ||
519 !strcasecmp(uuid, BNEP_UUID)) {
521 BT_DBG("Network connection request: %s", uuid);
522 ret = tethering_create(&tethering);
524 if (ret != TETHERING_ERROR_NONE) {
525 BT_ERR("Fail to create tethering: %d", ret);
529 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
531 ret = tethering_destroy(tethering);
533 if (ret != TETHERING_ERROR_NONE) {
534 BT_ERR("Fail to create tethering: %d", ret);
537 if (enabled != true) {
538 BT_ERR("BT tethering is not enabled");
542 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
546 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
553 dbus_g_proxy_call(device, "GetProperties", &error, G_TYPE_INVALID,
554 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
556 &hash, G_TYPE_INVALID);
558 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
560 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
565 value = g_hash_table_lookup(hash, "Address");
566 address = value ? g_value_get_string(value) : NULL;
568 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
572 value = g_hash_table_lookup(hash, "Alias");
573 name = value ? g_value_get_string(value) : NULL;
577 value = g_hash_table_lookup(hash, "Trusted");
578 trust = value ? g_value_get_boolean(value) : 0;
580 value = g_hash_table_lookup(hash, "Paired");
581 paired = value ? g_value_get_boolean(value) : 0;
582 if ((paired == FALSE) && (trust == FALSE)) {
583 BT_DBG("No paired & No trusted device");
584 gap_agent_reply_authorize(agent,
585 GAP_AGENT_REJECT, NULL);
589 BT_DBG("Authorization request for device [%s] Service:[%s]\n", address,
592 if (strcasecmp(uuid, OPP_UUID) == 0 &&
593 _gap_agent_exist_osp_server(agent, BT_OBEX_SERVER,
595 _bt_send_event(BT_OPP_SERVER_EVENT,
596 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
597 DBUS_TYPE_INT32, &result,
598 DBUS_TYPE_STRING, &address,
599 DBUS_TYPE_STRING, &name,
605 if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
606 (char *)uuid) == TRUE) {
607 bt_rfcomm_server_info_t *server_info;
609 server_info = _bt_rfcomm_get_server_info_using_uuid((char *)uuid);
610 retv_if(server_info == NULL, TRUE);
611 retv_if(server_info->server_type != BT_CUSTOM_SERVER, TRUE);
613 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
614 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
615 DBUS_TYPE_INT32, &result,
616 DBUS_TYPE_STRING, &address,
617 DBUS_TYPE_STRING, &uuid,
618 DBUS_TYPE_STRING, &name,
619 DBUS_TYPE_INT16, &server_info->control_fd,
625 if (!strcasecmp(uuid, OPP_UUID))
626 request_type = BT_AGENT_EVENT_EXCHANGE_REQUEST;
627 else if (!strcasecmp(uuid, PBAP_UUID))
628 request_type = BT_AGENT_EVENT_PBAP_REQUEST;
629 else if (!strcasecmp(uuid, MAP_UUID))
630 request_type = BT_AGENT_EVENT_MAP_REQUEST;
633 BT_DBG("Trusted device, so authorize\n");
634 gap_agent_reply_authorize(agent,
635 GAP_AGENT_ACCEPT, NULL);
637 __launch_system_popup(request_type, name, NULL, NULL,
638 _gap_agent_get_path(agent));
642 __bt_agent_release_memory();
643 g_hash_table_destroy(hash);
650 static gboolean __authorization_cancel_request(GapAgent *agent,
653 BT_DBG("On Going Authorization is cancelled by remote\n");
655 gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
657 #ifndef LIBNOTIFY_SUPPORT
658 syspopup_destroy_all();
661 __bt_agent_release_memory();
666 void _bt_destroy_agent(void *agent)
671 _gap_agent_reset_dbus(agent);
673 g_object_unref(agent);
676 void* _bt_create_agent(const char *path, gboolean adapter)
678 GAP_AGENT_FUNC_CB func_cb;
679 DBusGProxy *adapter_proxy;
682 adapter_proxy = _bt_get_adapter_proxy();
686 func_cb.pincode_func = __pincode_request;
687 func_cb.display_func = __display_request;
688 func_cb.passkey_func = __passkey_request;
689 func_cb.confirm_func = __confirm_request;
690 func_cb.authorize_func = __authorize_request;
691 func_cb.pairing_cancel_func = __pairing_cancel_request;
692 func_cb.authorization_cancel_func = __authorization_cancel_request;
694 agent = _gap_agent_new();
696 _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
699 if (!_gap_agent_register(agent)) {
700 _bt_destroy_agent(agent);
708 gboolean _bt_agent_register_osp_server(const gint type, const char *uuid)
710 void *agent = _bt_get_adapter_agent();
714 return _gap_agent_register_osp_server(agent, type, uuid);
718 gboolean _bt_agent_unregister_osp_server(const gint type, const char *uuid)
720 void *agent = _bt_get_adapter_agent();
724 return _gap_agent_unregister_osp_server(agent, type, uuid);
727 gboolean _bt_agent_reply_authorize(gboolean accept)
731 void *agent = _bt_get_adapter_agent();
735 accept_val = accept ? GAP_AGENT_ACCEPT : GAP_AGENT_REJECT;
737 return gap_agent_reply_authorize(agent, accept_val, NULL);
740 gboolean _bt_agent_is_canceled(void *agent)
742 return _gap_agent_is_canceled(agent);
745 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class)
747 switch ((dev_class & 0x1f00) >> 8) {
749 switch ((dev_class & 0xc0) >> 6) {
751 /* input-keyboard" */
760 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
766 pch = strtok_r(buffer, "= ,", &last);
771 while ((pch = strtok_r(NULL, ",", &last))) {
772 if (0 == g_strcmp0(pch, address)) {
773 BT_DBG("Match found\n");
780 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
781 const char *partial_name)
786 pch = strtok_r(buffer, "= ,", &last);
791 while ((pch = strtok_r(NULL, ",", &last))) {
792 if (g_str_has_prefix(partial_name, pch)) {
793 BT_DBG("Match found\n");
800 static gboolean __bt_agent_is_device_blacklist(const char *address,
812 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
815 BT_DBG("fopen failed \n");
819 fseek(fp, 0, SEEK_END);
822 BT_DBG("size is not a positive number");
829 buffer = g_malloc0(sizeof(char) * size);
830 result = fread((char *)buffer, 1, size, fp);
832 if (result != size) {
833 BT_DBG("Read Error\n");
838 BT_DBG("Buffer = %s\n", buffer);
840 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
844 BT_DBG("No lines in the file \n");
848 for (i = 0; lines[i] != NULL; i++) {
849 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
850 if (__bt_agent_find_device_by_address_exactname(
853 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
854 if (__bt_agent_find_device_by_address_exactname(
857 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
858 if (__bt_agent_find_device_by_partial_name(lines[i],
866 BT_DBG("Found the device\n");
871 static gboolean __bt_agent_is_auto_response(uint32_t dev_class,
872 const gchar *address, const gchar *name)
874 gboolean is_headset = FALSE;
875 char lap_address[BT_LOWER_ADDRESS_LENGTH];
877 BT_DBG("bt_agent_is_headset_class, %d +", dev_class);
882 switch ((dev_class & 0x1f00) >> 8) {
884 switch ((dev_class & 0xfc) >> 2) {
895 case 0x0c: /* Video Camera */
896 case 0x0d: /* Camcorder */
899 /* Other audio device */
909 /* Get the LAP(Lower Address part) */
910 g_strlcpy(lap_address, address, sizeof(lap_address));
912 BT_DBG("Device address = %s\n", address);
913 BT_DBG("Address 3 byte = %s\n", lap_address);
915 if (__bt_agent_is_device_blacklist(lap_address, name)) {
916 BT_DBG("Device is black listed\n");
923 static int __bt_agent_generate_passkey(char *passkey, int size)
928 unsigned int value = 0;
936 random_fd = open("/dev/urandom", O_RDONLY);
941 for (i = 0; i < size; i++) {
942 len = read(random_fd, &value, sizeof(value));
943 passkey[i] = '0' + (value % 10);
948 BT_DBG("passkey: %s", passkey);