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>
25 #include <syspopup_caller.h>
27 #include <tethering.h>
29 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-agent.h"
32 #include "bt-service-gap-agent.h"
33 #include "bt-service-adapter.h"
34 #include "bt-service-event.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-device.h"
37 #include "bt-service-audio.h"
39 #define BT_APP_AUTHENTICATION_TIMEOUT 35
40 #define BT_APP_AUTHORIZATION_TIMEOUT 15
42 #define HFP_AUDIO_GATEWAY_UUID "0000111f-0000-1000-8000-00805f9b34fb"
43 #define A2DP_UUID "0000110D-0000-1000-8000-00805F9B34FB"
44 #define AVRCP_TARGET_UUID "0000110c-0000-1000-8000-00805f9b34fb"
45 #define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
46 #define FTP_UUID "00001106-0000-1000-8000-00805f9b34fb"
47 #define SPP_UUID "00001101-0000-1000-8000-00805f9b34fb"
48 #define PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb"
49 #define MAP_UUID "00001132-0000-1000-8000-00805f9b34fb"
50 #define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
51 #define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
52 #define BNEP_UUID "0000000f-0000-1000-8000-00805f9b34fb"
53 #define HID_UUID "00001124-0000-1000-8000-00805f9b34fb"
55 #define BT_AGENT_OBJECT "/org/bluez/agent/frwk_agent"
56 #define BT_AGENT_INTERFACE "org.bluez.Agent"
57 #define BT_AGENT_SIGNAL_RFCOMM_AUTHORIZE "RfcommAuthorize"
58 #define BT_AGENT_SIGNAL_OBEX_AUTHORIZE "ObexAuthorize"
60 #define BT_PIN_MAX_LENGTH 16
61 #define BT_PASSKEY_MAX_LENGTH 4
63 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
65 static int __bt_agent_is_auto_response(uint32_t dev_class, const gchar *address,
67 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class);
68 static int __bt_agent_generate_passkey(char *passkey, int size);
70 static void __bt_agent_release_memory(void)
72 /* Release Malloc Memory*/
75 /* Release Stack Memory*/
79 static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
82 bundle *b = (bundle *) user_data;
85 BT_DBG("There is some problem with the user data..popup can not be created\n");
88 ret = syspopup_launch("bt-syspopup", b);
91 BT_DBG("Sorry Can not launch popup\n");
93 BT_DBG("Hurray Popup launched \n");
99 static int __launch_system_popup(bt_agent_event_type_t event_type,
100 const char *device_name,
102 const char *filename,
103 const char *agent_path)
107 char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
109 BT_DBG("_bt_agent_launch_system_popup +");
115 bundle_add(b, "device-name", device_name);
116 bundle_add(b, "passkey", passkey);
117 bundle_add(b, "file", filename);
118 bundle_add(b, "agent-path", agent_path);
120 switch (event_type) {
121 case BT_AGENT_EVENT_PIN_REQUEST:
122 g_strlcpy(event_str, "pin-request", sizeof(event_str));
125 case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
126 g_strlcpy(event_str, "passkey-confirm-request",
130 case BT_AGENT_EVENT_PASSKEY_REQUEST:
131 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
134 case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
135 g_strlcpy(event_str, "passkey-display-request",
139 case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
140 g_strlcpy(event_str, "authorize-request",
144 case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
145 g_strlcpy(event_str, "confirm-mode-request",
149 case BT_AGENT_EVENT_FILE_RECEIVED:
150 g_strlcpy(event_str, "file-received", sizeof(event_str));
153 case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
154 g_strlcpy(event_str, "keyboard-passkey-request",
158 case BT_AGENT_EVENT_TERMINATE:
159 g_strlcpy(event_str, "terminate", sizeof(event_str));
162 case BT_AGENT_EVENT_EXCHANGE_REQUEST:
163 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
166 case BT_AGENT_EVENT_PBAP_REQUEST:
167 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
170 case BT_AGENT_EVENT_MAP_REQUEST:
171 g_strlcpy(event_str, "message-request", sizeof(event_str));
180 bundle_add(b, "event-type", event_str);
182 ret = syspopup_launch("bt-syspopup", b);
184 BT_DBG("Popup launch failed...retry %d\n", ret);
185 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
186 (GSourceFunc) __bt_agent_system_popup_timer_cb,
192 BT_DBG("_bt_agent_launch_system_popup -%d", ret);
196 static gboolean __pincode_request(GapAgent *agent, DBusGProxy *device)
198 uint32_t device_class;
199 GHashTable *hash = NULL;
201 const gchar *address;
203 GError *error = NULL;
207 dbus_g_proxy_call(device, "GetProperties", &error,
209 dbus_g_type_get_map("GHashTable",
210 G_TYPE_STRING, G_TYPE_VALUE),
211 &hash, G_TYPE_INVALID);
213 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
215 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
220 value = g_hash_table_lookup(hash, "Class");
221 device_class = value ? g_value_get_uint(value) : 0;
223 value = g_hash_table_lookup(hash, "Address");
224 address = value ? g_value_get_string(value) : NULL;
226 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
230 value = g_hash_table_lookup(hash, "Name");
231 name = value ? g_value_get_string(value) : NULL;
235 if (__bt_agent_is_auto_response(device_class, address, name)) {
236 /* Use Fixed PIN "0000" for basic pairing*/
237 _bt_set_autopair_status_in_bonding_info(TRUE);
238 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
240 } else if (__bt_agent_is_hid_keyboard(device_class)) {
241 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
243 if (__bt_agent_generate_passkey(str_passkey,
244 BT_PASSKEY_MAX_LENGTH) != 0) {
245 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
250 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
253 __launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
254 name, str_passkey, NULL,
255 _gap_agent_get_path(agent));
257 __launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, NULL,
258 NULL, _gap_agent_get_path(agent));
262 g_hash_table_destroy(hash);
263 __bt_agent_release_memory();
270 static gboolean __passkey_request(GapAgent *agent, DBusGProxy *device)
272 uint32_t device_class;
273 GHashTable *hash = NULL;
275 const gchar *address;
277 GError *error = NULL;
281 dbus_g_proxy_call(device, "GetProperties", &error,
283 dbus_g_type_get_map("GHashTable",
284 G_TYPE_STRING, G_TYPE_VALUE),
285 &hash, G_TYPE_INVALID);
287 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
289 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
294 value = g_hash_table_lookup(hash, "Class");
295 device_class = value ? g_value_get_uint(value) : 0;
297 value = g_hash_table_lookup(hash, "Address");
298 address = value ? g_value_get_string(value) : NULL;
300 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
304 value = g_hash_table_lookup(hash, "Name");
305 name = value ? g_value_get_string(value) : NULL;
309 __launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, NULL, NULL,
310 _gap_agent_get_path(agent));
313 __bt_agent_release_memory();
314 g_hash_table_destroy(hash);
320 static gboolean __display_request(GapAgent *agent, DBusGProxy *device,
323 GHashTable *hash = NULL;
325 const gchar *address;
327 GError *error = NULL;
332 dbus_g_proxy_call(device, "GetProperties", &error,
334 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
336 &hash, G_TYPE_INVALID);
338 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
340 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
345 value = g_hash_table_lookup(hash, "Address");
346 address = value ? g_value_get_string(value) : NULL;
348 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
352 value = g_hash_table_lookup(hash, "Name");
353 name = value ? g_value_get_string(value) : NULL;
357 str_passkey = g_strdup_printf("%d", passkey);
359 __launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
361 _gap_agent_get_path(agent));
366 __bt_agent_release_memory();
367 g_hash_table_destroy(hash);
373 static gboolean __confirm_request(GapAgent *agent, DBusGProxy *device,
376 uint32_t device_class;
377 GHashTable *hash = NULL;
379 const gchar *address;
381 GError *error = NULL;
384 BT_DBG("+ passkey[%.6d]\n", passkey);
386 snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
388 dbus_g_proxy_call(device, "GetProperties", &error,
390 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
392 &hash, G_TYPE_INVALID);
394 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
396 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
401 value = g_hash_table_lookup(hash, "Class");
402 device_class = value ? g_value_get_uint(value) : 0;
404 value = g_hash_table_lookup(hash, "Address");
405 address = value ? g_value_get_string(value) : NULL;
407 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
411 value = g_hash_table_lookup(hash, "Name");
412 name = value ? g_value_get_string(value) : NULL;
416 __launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
418 _gap_agent_get_path(agent));
421 __bt_agent_release_memory();
422 g_hash_table_destroy(hash);
429 static gboolean __pairing_cancel_request(GapAgent *agent, const char *address)
431 BT_DBG("On Going Pairing is cancelled by remote\n");
433 gap_agent_reply_pin_code(agent, GAP_AGENT_CANCEL, "", NULL);
435 syspopup_destroy_all();
437 __bt_agent_release_memory();
442 static gboolean __a2dp_authorize_request_check(void)
444 /* Check for existing Media device to disconnect */
445 return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
448 static gboolean __authorize_request(GapAgent *agent, DBusGProxy *device,
451 GHashTable *hash = NULL;
453 const gchar *address;
458 tethering_h tethering = NULL;
459 GError *error = NULL;
461 int result = BLUETOOTH_ERROR_NONE;
462 int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
468 /* Check if already Media connection exsist */
469 if (!strcasecmp(uuid, A2DP_UUID)) {
470 gboolean ret = FALSE;
472 ret = __a2dp_authorize_request_check();
475 BT_DBG("Already one A2DP device connected \n");
476 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
481 /* Check completed */
483 if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
484 !strcasecmp(uuid, A2DP_UUID) ||
485 !strcasecmp(uuid, HID_UUID) ||
486 !strcasecmp(uuid, AVRCP_TARGET_UUID)) {
487 BT_DBG("Auto accept authorization for audio device (HFP, A2DP, AVRCP) [%s]", uuid);
488 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
494 if (!strcasecmp(uuid, NAP_UUID) ||
495 !strcasecmp(uuid, GN_UUID) ||
496 !strcasecmp(uuid, BNEP_UUID)) {
498 BT_DBG("Network connection request: %s", uuid);
499 ret = tethering_create(&tethering);
501 if (ret != TETHERING_ERROR_NONE) {
502 BT_ERR("Fail to create tethering: %d", ret);
506 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
508 ret = tethering_destroy(tethering);
510 if (ret != TETHERING_ERROR_NONE) {
511 BT_ERR("Fail to create tethering: %d", ret);
514 if (enabled != true) {
515 BT_ERR("BT tethering is not enabled");
519 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
523 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
530 dbus_g_proxy_call(device, "GetProperties", &error, G_TYPE_INVALID,
531 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
533 &hash, G_TYPE_INVALID);
535 BT_DBG("error in GetBasicProperties [%s]\n", error->message);
537 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
542 value = g_hash_table_lookup(hash, "Address");
543 address = value ? g_value_get_string(value) : NULL;
545 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
549 value = g_hash_table_lookup(hash, "Alias");
550 name = value ? g_value_get_string(value) : NULL;
554 value = g_hash_table_lookup(hash, "Trusted");
555 trust = value ? g_value_get_boolean(value) : 0;
557 value = g_hash_table_lookup(hash, "Paired");
558 paired = value ? g_value_get_boolean(value) : 0;
559 if ((paired == FALSE) && (trust == FALSE)) {
560 BT_DBG("No paired & No trusted device");
561 gap_agent_reply_authorize(agent,
562 GAP_AGENT_REJECT, NULL);
566 BT_DBG("Authorization request for device [%s] Service:[%s]\n", address,
569 if (strcasecmp(uuid, OPP_UUID) == 0 &&
570 _gap_agent_exist_osp_server(agent, BT_OBEX_SERVER,
572 _bt_send_event(BT_OPP_SERVER_EVENT,
573 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
574 DBUS_TYPE_INT32, &result,
575 DBUS_TYPE_STRING, &address,
576 DBUS_TYPE_STRING, &name,
582 if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
583 (char *)uuid) == TRUE) {
584 bt_rfcomm_server_info_t *server_info;
586 server_info = _bt_rfcomm_get_server_info_using_uuid((char *)uuid);
587 retv_if(server_info == NULL, TRUE);
588 retv_if(server_info->server_type != BT_CUSTOM_SERVER, TRUE);
590 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
591 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
592 DBUS_TYPE_INT32, &result,
593 DBUS_TYPE_STRING, &address,
594 DBUS_TYPE_STRING, &uuid,
595 DBUS_TYPE_STRING, &name,
596 DBUS_TYPE_INT16, &server_info->control_fd,
602 if (!strcasecmp(uuid, OPP_UUID))
603 request_type = BT_AGENT_EVENT_EXCHANGE_REQUEST;
604 else if (!strcasecmp(uuid, PBAP_UUID))
605 request_type = BT_AGENT_EVENT_PBAP_REQUEST;
606 else if (!strcasecmp(uuid, MAP_UUID))
607 request_type = BT_AGENT_EVENT_MAP_REQUEST;
610 BT_DBG("Trusted device, so authorize\n");
611 gap_agent_reply_authorize(agent,
612 GAP_AGENT_ACCEPT, NULL);
614 __launch_system_popup(request_type, name, NULL, NULL,
615 _gap_agent_get_path(agent));
619 __bt_agent_release_memory();
620 g_hash_table_destroy(hash);
627 static gboolean __authorization_cancel_request(GapAgent *agent,
630 BT_DBG("On Going Authorization is cancelled by remote\n");
632 gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
634 syspopup_destroy_all();
636 __bt_agent_release_memory();
641 void _bt_destroy_agent(void *agent)
646 _gap_agent_reset_dbus(agent);
648 g_object_unref(agent);
651 void* _bt_create_agent(const char *path, gboolean adapter)
653 GAP_AGENT_FUNC_CB func_cb;
654 DBusGProxy *adapter_proxy;
657 adapter_proxy = _bt_get_adapter_proxy();
661 func_cb.pincode_func = __pincode_request;
662 func_cb.display_func = __display_request;
663 func_cb.passkey_func = __passkey_request;
664 func_cb.confirm_func = __confirm_request;
665 func_cb.authorize_func = __authorize_request;
666 func_cb.pairing_cancel_func = __pairing_cancel_request;
667 func_cb.authorization_cancel_func = __authorization_cancel_request;
669 agent = _gap_agent_new();
671 _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
674 if (!_gap_agent_register(agent)) {
675 _bt_destroy_agent(agent);
683 gboolean _bt_agent_register_osp_server(const gint type, const char *uuid)
685 void *agent = _bt_get_adapter_agent();
689 return _gap_agent_register_osp_server(agent, type, uuid);
693 gboolean _bt_agent_unregister_osp_server(const gint type, const char *uuid)
695 void *agent = _bt_get_adapter_agent();
699 return _gap_agent_unregister_osp_server(agent, type, uuid);
702 gboolean _bt_agent_reply_authorize(gboolean accept)
706 void *agent = _bt_get_adapter_agent();
710 accept_val = accept ? GAP_AGENT_ACCEPT : GAP_AGENT_REJECT;
712 return gap_agent_reply_authorize(agent, accept_val, NULL);
715 gboolean _bt_agent_is_canceled(void *agent)
717 return _gap_agent_is_canceled(agent);
720 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class)
722 switch ((dev_class & 0x1f00) >> 8) {
724 switch ((dev_class & 0xc0) >> 6) {
726 /* input-keyboard" */
735 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
741 pch = strtok_r(buffer, "= ,", &last);
746 while ((pch = strtok_r(NULL, ",", &last))) {
747 if (0 == g_strcmp0(pch, address)) {
748 BT_DBG("Match found\n");
755 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
756 const char *partial_name)
761 pch = strtok_r(buffer, "= ,", &last);
766 while ((pch = strtok_r(NULL, ",", &last))) {
767 if (g_str_has_prefix(partial_name, pch)) {
768 BT_DBG("Match found\n");
775 static gboolean __bt_agent_is_device_blacklist(const char *address,
787 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
790 BT_DBG("fopen failed \n");
794 fseek(fp, 0, SEEK_END);
797 BT_DBG("size is not a positive number");
804 buffer = g_malloc0(sizeof(char) * size);
805 result = fread((char *)buffer, 1, size, fp);
807 if (result != size) {
808 BT_DBG("Read Error\n");
813 BT_DBG("Buffer = %s\n", buffer);
815 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
819 BT_DBG("No lines in the file \n");
823 for (i = 0; lines[i] != NULL; i++) {
824 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
825 if (__bt_agent_find_device_by_address_exactname(
828 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
829 if (__bt_agent_find_device_by_address_exactname(
832 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
833 if (__bt_agent_find_device_by_partial_name(lines[i],
841 BT_DBG("Found the device\n");
846 static gboolean __bt_agent_is_auto_response(uint32_t dev_class,
847 const gchar *address, const gchar *name)
849 gboolean is_headset = FALSE;
850 char lap_address[BT_LOWER_ADDRESS_LENGTH];
852 BT_DBG("bt_agent_is_headset_class, %d +", dev_class);
857 switch ((dev_class & 0x1f00) >> 8) {
859 switch ((dev_class & 0xfc) >> 2) {
870 case 0x0c: /* Video Camera */
871 case 0x0d: /* Camcorder */
874 /* Other audio device */
884 /* Get the LAP(Lower Address part) */
885 g_strlcpy(lap_address, address, sizeof(lap_address));
887 BT_DBG("Device address = %s\n", address);
888 BT_DBG("Address 3 byte = %s\n", lap_address);
890 if (__bt_agent_is_device_blacklist(lap_address, name)) {
891 BT_DBG("Device is black listed\n");
898 static int __bt_agent_generate_passkey(char *passkey, int size)
903 unsigned int value = 0;
911 random_fd = open("/dev/urandom", O_RDONLY);
916 for (i = 0; i < size; i++) {
917 len = read(random_fd, &value, sizeof(value));
918 passkey[i] = '0' + (value % 10);
923 BT_DBG("passkey: %s", passkey);