4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include <bundle_internal.h>
30 #include <eventsystem.h>
44 #include "bt-hal-log.h"
45 #include "bt-hal-msg.h"
46 #include "bt-hal-internal.h"
47 #include "bt-hal-utils.h"
48 #include "bt-hal-event-receiver.h"
49 #include "bt-hal-dbus-common-utils.h"
51 #include "bt-hal-adapter-dbus-handler.h"
52 #include "bt-hal-rfcomm-dbus-handler.h"
53 #include "bt-hal-event-receiver.h"
55 #include <bt-hal-agent.h>
56 #include <bt-hal-gap-agent.h>
57 #include <bt-hal-dbus-common-utils.h>
59 /* TODO_40 : 4.0 merge */
60 #include "bt-internal-types.h"
62 #define BT_HAL_AGENT_AUTO_PAIR_BLACKLIST_FILE (APP_SYSCONFDIR"/auto-pair-blacklist")
63 #define BT_HAL_AGENT_NEW_LINE "\r\n"
65 #define PAGE_SIZE (1 << 12)
66 #define _ALIGN_UP(addr, size) (((addr)+((size)-1))&(~((size)-1)))
67 #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
68 #define PAGE_ALIGN(addr) _ALIGN_DOWN(addr, PAGE_SIZE)
69 #define BT_HAL_PIN_MAX_LENGTH 16
70 #define BT_HAL_PASSKEY_MAX_LENGTH 4
71 #define BT_HAL_LOWER_ADDRESS_LENGTH 9
72 #define BT_HAL_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
73 #define BT_HAL_AGENT_SYSPOPUP_MAX_ATTEMPT 3
75 #define G_VARIANT_UNREF(variant) \
76 g_variant_unref(variant); \
78 #define BT_HAL_MAX_EVENT_STR_LENGTH 50
80 static void *adapter_agent = NULL;
81 static gboolean passkey_display_notification = FALSE;
82 static gboolean passkey_confirm_notification = FALSE;
83 static gboolean passkey_notification = FALSE;
84 static gboolean pincode_notification = FALSE;
86 /* Forward delcaration */
87 static void __bt_hal_send_pin_request_event(const gchar *address, const gchar *name,
89 static gboolean __bt_hal_pincode_request(GapAgentPrivate *agent, GDBusProxy *device);
90 static gboolean __bt_hal_display_request(GapAgentPrivate *agent, GDBusProxy *device,
92 static gboolean __bt_hal_passkey_request(GapAgentPrivate *agent, GDBusProxy *device);
93 static gboolean __bt_hal_confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
95 static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
97 static gboolean __bt_hal_authorize_cancel_request(GapAgentPrivate *agent, const char *address);
98 static gboolean __bt_hal_pairing_cancel_request(GapAgentPrivate *agent, const char *address);
99 static GVariant *__bt_hal_service_getall(GDBusProxy *device, const char *interface);
100 static void __bt_hal_agent_release_memory(void);
101 static inline void stack_trim(void);
103 static gboolean __bt_hal_device_is_hid_keyboard(unsigned int dev_class);
104 static gboolean __bt_hal_is_hid_device_connectable(void);
105 static int __bt_hal_device_generate_passkey(char *passkey, int size);
106 static gboolean __bt_hal_device_is_auto_response(uint32_t dev_class,
107 const gchar *address, const gchar *name);
108 static gboolean __bt_hal_device_is_device_blacklisted(const char *address, const char *name);
109 static gboolean __bt_hal_find_device_by_address_exactname(char *buffer,
110 const char *address);
111 static gboolean __bt_hal_find_device_by_partial_name(char *buffer,
112 const char *partial_name);
113 static void __bt_hal_send_ssp_request_events(const gchar *address, const gchar *name,
114 guint passkey, uint32_t cod, unsigned char variant);
115 static void __bt_hal_send_authorize_request_event(const gchar *address, const char *uuid);
117 void* _bt_hal_create_agent(const char *path, gboolean adapter)
119 GAP_AGENT_FUNC_CB func_cb;
120 GDBusProxy *adapter_proxy;
121 GapAgentPrivate *agent;
124 adapter_proxy = _bt_hal_get_adapter_proxy();
128 func_cb.pincode_func = __bt_hal_pincode_request;
129 func_cb.display_func = __bt_hal_display_request;
130 func_cb.passkey_func = __bt_hal_passkey_request;
131 func_cb.confirm_func = __bt_hal_confirm_request;
132 func_cb.authorize_func = __bt_hal_authorize_request;
133 func_cb.pairing_cancel_func = __bt_hal_pairing_cancel_request;
134 func_cb.authorization_cancel_func = __bt_hal_authorize_cancel_request;
137 agent = g_new0(GapAgentPrivate, 1);
139 _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
142 if (!_gap_agent_register(agent)) {
143 ERR("gap agent registration failed!");
144 _bt_hal_destroy_agent(agent);
152 void _bt_hal_destroy_agent(void *agent)
158 _gap_agent_reset_dbus((GapAgentPrivate *)agent);
164 gboolean _bt_hal_agent_is_canceled(void)
166 void *agent = _bt_hal_get_adapter_agent();
170 return _gap_agent_is_canceled(agent);
173 int _bt_hal_agent_reply_cancellation(void)
175 void *agent = _bt_hal_get_adapter_agent();
177 return BT_STATUS_FAIL;
179 if (gap_agent_reply_confirmation(agent, GAP_AGENT_CANCEL, NULL) != TRUE) {
180 ERR("Fail to reply agent");
181 return BT_STATUS_FAIL;
183 DBG("gap agent cancellation done successfully!");
184 return BT_STATUS_SUCCESS;
188 void _bt_hal_agent_set_canceled(gboolean value)
190 void *agent = _bt_hal_get_adapter_agent();
194 return _gap_agent_set_canceled(agent, value);
197 void _bt_hal_initialize_adapter_agent(void)
200 ERR("Adapter agent already exist!");
204 adapter_agent = _bt_hal_create_agent(BT_HAL_ADAPTER_AGENT_PATH, TRUE);
205 if (!adapter_agent) {
206 ERR("Fail to register agent");
211 void _bt_hal_destroy_adapter_agent(void)
214 _bt_hal_destroy_agent(adapter_agent);
215 adapter_agent = NULL;
218 void* _bt_hal_get_adapter_agent(void)
220 return adapter_agent;
223 void _bt_hal_enable_gap_auth_notifications(unsigned int type, gboolean enable)
225 INFO("type: %d, enable: %d", type, enable);
228 case BT_PASSKEY_CONFIRMATION:
229 /* Note: Currently not used for notification sending, should be used when required */
230 passkey_confirm_notification = enable;
232 case BT_PASSKEY_DISPLAY:
233 passkey_display_notification = enable;
235 case BT_PASSKEY_ENTRY:
236 /* Note: Currently not used for notification sending, should be used when required */
237 passkey_notification = enable;
239 case BT_PINCODE_ENTRY:
240 pincode_notification = enable;
243 ERR("Unknown type: %d", type);
247 static void __bt_hal_send_authorize_request_event(const gchar *address, const char *uuid)
249 struct hal_ev_authorize_request ev;
250 memset(&ev, 0, sizeof(ev));
252 DBG("Remote Device address [%s]", address);
254 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
256 /* UUID received in authorization request (In case of HFP unit, UUID received from Bluez
257 would be of that of Audio Gateway UUID (0x111f) */
258 ev.service_id = _bt_convert_uuid_string_to_service_id(uuid);
260 handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
262 DBG("Sending AUTHORIZE REQUEST");
263 event_cb(HAL_EV_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
270 static void __bt_hal_send_rfcomm_authorize_request_event(const gchar *address, const char *uuid, const char *name, char *path, int fd)
272 struct hal_ev_sock_conn_auth ev;
274 DBG("Remote Device address [%s], uuid: %s", address, uuid);
276 memset(&ev, 0, sizeof(ev));
277 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
278 _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid);
280 memcpy(ev.name, name, strlen(name) - 1);
282 memcpy(ev.path, path, strlen(path) - 1);
285 handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
287 DBG("Sending Socket AUTHORIZE REQUEST");
288 event_cb(HAL_EV_SOCK_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
296 static void __bt_hal_send_pin_request_event(const gchar *address, const gchar *name,
299 struct hal_ev_pin_request ev;
300 memset(&ev, 0, sizeof(ev));
302 DBG("Remote Device address [%s]", address);
303 DBG("Remote Device Name [%s]", name);
304 DBG("Remote Device COD [%u]", cod);
306 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
308 memcpy(ev.name, name, strlen(name)+1);
309 ev.class_of_dev = cod;
311 handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
313 DBG("Sending PIN REQUEST");
314 event_cb(HAL_EV_PIN_REQUEST, (void*)&ev, sizeof(ev));
321 void __bt_hal_get_auth_info(GVariant *reply, char *auth_info)
325 char *manufacturer_data = NULL;
326 int manufacturer_data_len;
327 gboolean is_alias_set;
328 GVariantIter *value_iter;
332 tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
333 G_VARIANT_TYPE_BOOLEAN);
335 is_alias_set = g_variant_get_boolean(tmp_value);
336 g_variant_unref(tmp_value);
338 is_alias_set = FALSE;
340 if (is_alias_set == FALSE) {
341 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerDataLen",
342 G_VARIANT_TYPE_UINT16);
344 manufacturer_data_len = g_variant_get_uint16(tmp_value);
345 if (manufacturer_data_len >
346 MAX_MANUFACTURE_LEN) {
347 ERR("manufacturer_data_len is too long");
348 manufacturer_data_len = MAX_MANUFACTURE_LEN;
350 g_variant_unref(tmp_value);
352 manufacturer_data_len = 0;
354 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerData",
355 G_VARIANT_TYPE_ARRAY);
357 if ((manufacturer_data_len == 0) ||
358 manufacturer_data_len != g_variant_get_size(tmp_value)) {
359 ERR("manufacturer data length doesn't match");
360 manufacturer_data_len = 0;
361 manufacturer_data = NULL;
363 manufacturer_data = g_malloc0(manufacturer_data_len);
364 g_variant_get(tmp_value, "ay", &value_iter);
365 while (g_variant_iter_loop(value_iter, "y", &m_value))
366 manufacturer_data[i++] = m_value;
368 g_variant_iter_free(value_iter);
370 g_variant_unref(tmp_value);
372 INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
373 manufacturer_data_len = 0;
374 manufacturer_data = NULL;
376 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
377 if (manufacturer_data_len < 30) {
378 g_free(manufacturer_data);
381 if (manufacturer_data[0] != 0x00 || manufacturer_data[1] != 0x75) {
382 DBG("This is not a samsung specific manufaturer data");
383 g_free(manufacturer_data);
387 /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
388 1 (discovery version) + 1 (associated service ID)
389 2 (Proxamity and locality) + 2 (Device type and icon) */
393 memcpy(auth_info, &(manufacturer_data[cursor]), 5);
395 g_free(manufacturer_data);
398 static gboolean __bt_hal_pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
400 uint32_t device_class;
401 const gchar *address;
402 unsigned char auth_info[5] = {0, };
404 GVariant *reply = NULL;
405 GVariant *reply_temp = NULL;
409 reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
411 if (reply_temp == NULL) {
412 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
417 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
419 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
420 g_variant_get(tmp_value, "u", &device_class);
421 G_VARIANT_UNREF(tmp_value);
423 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
424 g_variant_get(tmp_value, "s", &address);
425 G_VARIANT_UNREF(tmp_value);
427 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
428 g_variant_get(tmp_value, "s", &name);
429 G_VARIANT_UNREF(tmp_value);
434 if (headed_plugin_info->plugin_headed_enabled) {
435 __bt_hal_get_auth_info(reply, (char *)auth_info);
436 if (__bt_hal_device_is_hid_keyboard(device_class)) {
437 char str_passkey[BT_HAL_PASSKEY_MAX_LENGTH + 1] = { 0 };
439 DBG("Device is HID Keyboard");
440 if (__bt_hal_device_generate_passkey(str_passkey,
441 BT_HAL_PASSKEY_MAX_LENGTH) != 0) {
442 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
447 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
450 DBG("Launch BT Syspopup");
451 headed_plugin_info->headed_plugin->bt_launch_system_popup(
452 BT_HAL_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
453 name, auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
454 } else if (__bt_hal_device_is_auto_response(device_class, address, name)) {
455 DBG("Device is of Type Auto response, send event to HAL");
456 __bt_hal_send_pin_request_event(address, name, device_class);
457 } else if (pincode_notification) {
458 DBG("pincode_notification is enabled, send event to HAL");
459 __bt_hal_send_pin_request_event(address, name, device_class);
461 DBG("Device is not of Auto response class, Show PIN Entry");
462 headed_plugin_info->headed_plugin->bt_launch_system_popup(
463 BT_HAL_AGENT_EVENT_PIN_REQUEST, name, auth_info,
464 NULL, NULL, _gap_agent_get_path(agent));
467 INFO("Plugin Headed not Enabled");
468 __bt_hal_send_pin_request_event(address, name, device_class);
472 g_variant_unref(reply);
473 g_variant_unref(reply_temp);
474 __bt_hal_agent_release_memory();
481 /* BT_SSP_VARIANT_PASSKEY_CONFIRMATION */
482 /* BT_SSP_VARIANT_PASSKEY_NOTIFICATION */
483 /* BT_SSP_VARIANT_PASSKEY_ENTRY */
484 /* BT_SSP_VARIANT_CONSENT */
486 static void __bt_hal_send_ssp_request_events(const gchar *address,
490 unsigned char variant)
492 struct hal_ev_ssp_request ev;
493 memset(&ev, 0, sizeof(ev));
494 DBG("sizeof ev [%zu]", sizeof(ev));
496 DBG("Remote Device address [%s]", address);
497 DBG("Remote Device Name [%s]", name);
498 DBG("Remote Device passkey [%d]", passkey);
499 DBG("Remote Device pairing variant [0x%x]", variant);
500 DBG("Remote Device cod [%d]", cod);
502 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
504 memcpy(ev.name, name, strlen(name)+1);
505 ev.class_of_dev = cod;
506 ev.pairing_variant = variant;
507 ev.passkey = passkey;
509 handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
511 DBG("Sending SSP type [%d]", variant);
512 event_cb(HAL_EV_SSP_REQUEST, (void*)&ev, sizeof(ev));
519 static gboolean __bt_hal_display_request(GapAgentPrivate *agent, GDBusProxy *device,
522 const gchar *address;
524 unsigned char auth_info[5] = {0, };
526 uint32_t device_class;
527 GVariant *reply = NULL;
528 GVariant *reply_temp = NULL;
529 GVariant *tmp_value = NULL;
532 reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
533 if (reply_temp == NULL) {
534 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
539 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
541 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
542 g_variant_get(tmp_value, "s", &address);
543 G_VARIANT_UNREF(tmp_value);
545 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
549 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
550 g_variant_get(tmp_value, "s", &name);
551 G_VARIANT_UNREF(tmp_value);
553 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
554 g_variant_get(tmp_value, "u", &device_class);
555 G_VARIANT_UNREF(tmp_value);
560 str_passkey = g_strdup_printf("%d", passkey);
562 __bt_hal_get_auth_info(reply, (char *)auth_info);
564 DBG("KEYBOARD_PASSKEY_REQUEST");
566 if (passkey_display_notification) {
567 DBG("passkey_display_notification is enabled, send event to HAL");
568 __bt_hal_send_ssp_request_events(address, name, passkey, device_class,
569 BT_SSP_VARIANT_PASSKEY_NOTIFICATION);
570 } else if (headed_plugin_info->plugin_headed_enabled) {
571 INFO("Plugin Headed Enabled");
572 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
573 auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
575 INFO("Plugin Headed not Enabled");
576 __bt_hal_send_ssp_request_events(address, name, passkey, device_class,
577 BT_SSP_VARIANT_PASSKEY_NOTIFICATION);
582 g_variant_unref(reply);
583 g_variant_unref(reply_temp);
584 __bt_hal_agent_release_memory();
591 static gboolean __bt_hal_passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
593 const gchar *address;
595 unsigned char auth_info[5] = {0, };
596 uint32_t device_class;
597 GVariant *reply = NULL;
598 GVariant *reply_temp = NULL;
602 reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
604 if (reply_temp == NULL) {
605 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
610 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
612 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
613 g_variant_get(tmp_value, "s", &address);
614 G_VARIANT_UNREF(tmp_value);
616 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
620 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
621 g_variant_get(tmp_value, "s", &name);
622 G_VARIANT_UNREF(tmp_value);
624 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
625 g_variant_get(tmp_value, "u", &device_class);
626 G_VARIANT_UNREF(tmp_value);
631 DBG("PASSKEY_REQUEST");
633 __bt_hal_get_auth_info(reply, (char *)auth_info);
635 if (headed_plugin_info->plugin_headed_enabled) {
636 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
637 NULL, NULL, _gap_agent_get_path(agent));
639 __bt_hal_send_ssp_request_events(address, name, 0, device_class,
640 BT_SSP_VARIANT_PASSKEY_ENTRY);
644 g_variant_unref(reply);
645 g_variant_unref(reply_temp);
646 __bt_hal_agent_release_memory();
653 static gboolean __bt_hal_confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
656 const gchar *address;
659 uint32_t device_class;
660 GVariant *reply_temp = NULL;
661 GVariant *reply = NULL;
663 DBG("+ passkey[%.6d]", passkey);
664 DBG("Agent Path [%s]", agent->path);
666 snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
668 reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
670 if (reply_temp == NULL) {
671 ERR("####Device doesn't exist####");
672 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
676 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
678 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
679 g_variant_get(tmp_value, "s", &address);
680 G_VARIANT_UNREF(tmp_value);
682 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
683 g_variant_get(tmp_value, "s", &name);
684 G_VARIANT_UNREF(tmp_value);
686 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
687 g_variant_get(tmp_value, "u", &device_class);
688 G_VARIANT_UNREF(tmp_value);
693 if (headed_plugin_info->plugin_headed_enabled) {
694 unsigned char auth_info[5] = {0, };
696 DBG("LAUNCH SYSPOPUP");
697 DBG("Name [%s]", name);
698 DBG("Passkey [%s]", str_passkey);
700 __bt_hal_get_auth_info(reply, (char *)auth_info);
701 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
702 auth_info, str_passkey, NULL,
703 _gap_agent_get_path(agent));
705 DBG("Headless Confirmation");
707 if (TIZEN_FEATURE_BLUEZ_SPEAKER_REFERENCE) {
708 /* Reply the ssp confirmation automaticaly */
709 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
712 __bt_hal_send_ssp_request_events(address, name, passkey,
713 device_class, BT_SSP_VARIANT_PASSKEY_CONFIRMATION);
717 g_variant_unref(reply);
718 g_variant_unref(reply_temp);
719 __bt_hal_agent_release_memory();
724 static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
727 const gchar *address;
731 GVariant *reply = NULL;
732 GVariant *reply_temp = NULL;
734 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
735 bt_hal_agent_osp_server_t *osp_server;
738 DBG("Authorize Request from Bluez Stack: UUID [%s]", uuid);
740 reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
741 if (reply_temp == NULL) {
742 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
746 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
748 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
749 g_variant_get(tmp_value, "s", &address);
750 G_VARIANT_UNREF(tmp_value);
752 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
756 tmp_value = g_variant_lookup_value(reply, "Alias", G_VARIANT_TYPE_STRING);
757 g_variant_get(tmp_value, "s", &name);
758 G_VARIANT_UNREF(tmp_value);
762 tmp_value = g_variant_lookup_value(reply, "Trusted", G_VARIANT_TYPE_BOOLEAN);
763 g_variant_get(tmp_value, "b", &trust);
764 G_VARIANT_UNREF(tmp_value);
766 tmp_value = g_variant_lookup_value(reply, "Paired", G_VARIANT_TYPE_BOOLEAN);
767 g_variant_get(tmp_value, "b", &paired);
768 G_VARIANT_UNREF(tmp_value);
769 if ((paired == FALSE) && (trust == FALSE)) {
770 ERR("No paired & No trusted device");
771 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
775 INFO("Authorization request for device [%s] Service:[%s]\n", address, uuid);
778 INFO("Trusted device, so authorize\n");
779 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
782 INFO("Device is not Trusted, so prompt user to accept or reject authorization \n");
785 if (!strcasecmp(uuid, BT_HAL_HID_UUID)) {
786 gboolean is_connectable = __bt_hal_is_hid_device_connectable();
787 DBG("Automatically %s authorization for HID",
788 is_connectable ? "accept" : "reject");
789 if (is_connectable == TRUE)
790 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
792 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
796 if (!strcasecmp(uuid, BT_HAL_HFP_AUDIO_GATEWAY_UUID) ||
797 !strcasecmp(uuid, BT_HAL_HSP_AUDIO_GATEWAY_UUID) ||
798 !strcasecmp(uuid, BT_HAL_HFP_HF_UUID) ||
799 !strcasecmp(uuid, BT_HAL_HSP_HS_UUID) ||
800 !strcasecmp(uuid, BT_HAL_A2DP_PROFILE_UUID) ||
801 !strcasecmp(uuid, BT_HAL_HID_DEVICE_UUID) ||
802 !strcasecmp(uuid, BT_HAL_SAP_UUID_OLD) ||
803 !strcasecmp(uuid, BT_HAL_SAP_UUID_NEW) ||
805 !strcasecmp(uuid, BT_HAL_IOTIVITY_UUID) ||
807 !strcasecmp(uuid, BT_HAL_AVRCP_TARGET_UUID)) {
808 DBG("Authorization for UUID: %s, send event", uuid);
809 __bt_hal_send_authorize_request_event(address, uuid);
814 if (!strcasecmp(uuid, BT_HAL_OPP_UUID)) {
815 if (NULL != _bt_hal_gap_agent_find_osp_server_by_type(
816 agent->osp_servers, BT_OSP_SERVER_OBEX, NULL)) {
817 INFO("OSP server for OPP found, send event");
818 __bt_hal_send_authorize_request_event(address, uuid);
821 INFO("Native OPP server, auto accept");
822 gap_agent_reply_authorize(agent,
823 GAP_AGENT_ACCEPT, NULL);
828 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
829 /* If user only listens(without accepts) to rfcomm server, occurs connection requested cb not popup */
830 osp_server = _bt_hal_gap_agent_find_osp_server_by_type(agent->osp_servers, BT_OSP_SERVER_RFCOMM, uuid);
832 INFO("Found rfcomm osp server. path: %s, fd: %d", osp_server->path, osp_server->fd);
833 __bt_hal_send_rfcomm_authorize_request_event(address, uuid, name, osp_server->path, osp_server->fd);
837 if (_is_rfcomm_server_uuid(uuid)) {
838 __bt_hal_send_rfcomm_authorize_request_event(address, uuid, NULL, NULL, -1);
844 if (trust || !headed_plugin_info->plugin_headed_enabled) {
845 INFO("Trusted or Headless device, so authorize\n");
846 gap_agent_reply_authorize(agent,
847 GAP_AGENT_ACCEPT, NULL);
852 if (headed_plugin_info->plugin_headed_enabled) {
853 unsigned char auth_info[5] = {0, };
854 int request_type = BT_HAL_AGENT_EVENT_AUTHORIZE_REQUEST;
856 __bt_hal_get_auth_info(reply, (char *)auth_info);
858 if (!strcasecmp(uuid, BT_HAL_OPP_UUID))
859 request_type = BT_HAL_AGENT_EVENT_EXCHANGE_REQUEST;
860 else if (!strcasecmp(uuid, BT_HAL_PBAP_UUID))
861 request_type = BT_HAL_AGENT_EVENT_PBAP_REQUEST;
862 else if (!strcasecmp(uuid, BT_HAL_MAP_UUID))
863 request_type = BT_HAL_AGENT_EVENT_MAP_REQUEST;
865 headed_plugin_info->headed_plugin->bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
866 _gap_agent_get_path(agent));
870 g_variant_unref(reply);
871 g_variant_unref(reply_temp);
872 __bt_hal_agent_release_memory();
878 static gboolean __bt_hal_authorize_cancel_request(GapAgentPrivate *agent,
881 DBG("On Going Authorization is cancelled by remote [%s]", address);
882 gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
884 if (headed_plugin_info->plugin_headed_enabled)
885 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
887 __bt_hal_agent_release_memory();
891 static gboolean __bt_hal_pairing_cancel_request(GapAgentPrivate *agent, const char *address)
893 DBG("On Going Pairing is cancelled by remote [%s]", address);
895 if (headed_plugin_info->plugin_headed_enabled)
896 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
898 __bt_hal_agent_release_memory();
902 static gboolean __bt_hal_device_is_hid_keyboard(unsigned int dev_class)
904 switch ((dev_class & 0x1f00) >> 8) {
906 switch ((dev_class & 0xc0) >> 6) {
908 /* input-keyboard" */
917 static gboolean __bt_hal_is_hid_device_connectable(void)
919 GDBusProxy *proxy = NULL;
920 GVariant *reply = NULL;
922 gboolean connectable = FALSE;
924 proxy = _bt_hal_get_hid_agent_proxy();
928 reply = g_dbus_proxy_call_sync(proxy, "IsHidConnectable", NULL,
929 G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &err);
931 ERR("Error returned in method call");
933 ERR("Error message = %s", err->message);
938 g_variant_get(reply, "(b)", &connectable);
939 g_variant_unref(reply);
941 g_object_unref(proxy);
943 INFO("HID Device is %s",
944 connectable ? "Connectable" : "Non-connectable");
949 static int __bt_hal_device_generate_passkey(char *passkey, int size)
954 unsigned int value = 0;
962 random_fd = open("/dev/urandom", O_RDONLY);
967 for (i = 0; i < size; i++) {
968 len = read(random_fd, &value, sizeof(value));
970 passkey[i] = '0' + (value % 10);
975 DBG("passkey: %s", passkey);
980 static gboolean __bt_hal_find_device_by_address_exactname(char *buffer,
986 pch = strtok_r(buffer, "= ,", &last);
991 while ((pch = strtok_r(NULL, ",", &last))) {
992 if (0 == g_strcmp0(pch, address)) {
993 DBG("Match found\n");
1000 static gboolean __bt_hal_find_device_by_partial_name(char *buffer,
1001 const char *partial_name)
1006 pch = strtok_r(buffer, "= ,", &last);
1011 while ((pch = strtok_r(NULL, ",", &last))) {
1012 if (g_str_has_prefix(partial_name, pch)) {
1013 DBG("Match found\n");
1020 static gboolean __bt_hal_device_is_device_blacklisted(const char *address,
1032 fp = fopen(BT_HAL_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
1035 ERR("Unable to open blacklist file");
1039 fseek(fp, 0, SEEK_END);
1042 DBG("size is not a positive number");
1049 buffer = g_malloc0(sizeof(char) * size);
1050 /* Fix : NULL_RETURNS */
1051 if (buffer == NULL) {
1052 ERR("Fail to allocate memory");
1056 result = fread((char *)buffer, 1, size, fp);
1058 if (result != size) {
1064 DBG("Buffer = %s", buffer);
1066 lines = g_strsplit_set(buffer, BT_HAL_AGENT_NEW_LINE, 0);
1069 if (lines == NULL) {
1070 ERR("No lines in the file");
1074 for (i = 0; lines[i] != NULL; i++) {
1075 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
1076 if (__bt_hal_find_device_by_address_exactname(
1079 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
1080 if (__bt_hal_find_device_by_address_exactname(
1083 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
1084 if (__bt_hal_find_device_by_partial_name(lines[i],
1087 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1088 if (__bt_hal_find_device_by_address_exactname(
1096 DBG("Found the device");
1101 static gboolean __bt_hal_device_is_auto_response(uint32_t dev_class,
1102 const gchar *address, const gchar *name)
1104 gboolean is_headset = FALSE;
1105 gboolean is_mouse = FALSE;
1106 unsigned int adapter_class = 0;
1107 char lap_address[BT_HAL_LOWER_ADDRESS_LENGTH];
1109 DBG("bt_agent_is_headset_class, %d +", dev_class);
1111 if (address == NULL)
1114 _bt_hal_dbus_get_adapter_class(&adapter_class);
1116 if (((adapter_class & 0x1f00) >> 8) == 0x05)
1118 DBG("Adapter is Peripheral, do not response automatically");
1122 switch ((dev_class & 0x1f00) >> 8) {
1124 switch ((dev_class & 0xfc) >> 2) {
1134 case 0x0b: /* VCR */
1135 case 0x0c: /* Video Camera */
1136 case 0x0d: /* Camcorder */
1139 /* Other audio device */
1145 switch (dev_class & 0xff) {
1146 case 0x80: /* 0x80: Pointing device(Mouse) */
1150 case 0x40: /* 0x40: input device (BT keyboard) */
1152 /* Get the LAP(Lower Address part) */
1153 g_strlcpy(lap_address, address, sizeof(lap_address));
1155 /* Need to Auto pair the blacklisted Keyboard */
1156 if (__bt_hal_device_is_device_blacklisted(lap_address, name) != TRUE) {
1157 DBG("Device is not black listed\n");
1160 ERR("Device is black listed\n");
1166 if ((!is_headset) && (!is_mouse))
1169 /* Get the LAP(Lower Address part) */
1170 g_strlcpy(lap_address, address, sizeof(lap_address));
1172 DBG("Device address = %s\n", address);
1173 DBG("Address 3 byte = %s\n", lap_address);
1175 if (__bt_hal_device_is_device_blacklisted(lap_address, name)) {
1176 ERR("Device is black listed\n");
1183 static GVariant *__bt_hal_service_getall(GDBusProxy *device, const char *interface)
1185 GError *error = NULL;
1188 reply = g_dbus_proxy_call_sync(device,
1189 "GetAll", g_variant_new("(s)", interface),
1190 G_DBUS_CALL_FLAGS_NONE, -1,
1192 if (reply == NULL) {
1193 ERR("GetAll dBUS-RPC failed");
1195 ERR("D-Bus API failure: errCode[%x], message[%s]",
1196 error->code, error->message);
1197 g_clear_error(&error);
1206 static void __bt_hal_agent_release_memory(void)
1208 /* Release Malloc Memory*/
1211 /* Release Stack Memory*/
1215 static inline void stack_trim(void)
1221 unsigned int stacktop;
1224 asm volatile ("mov %0,sp " : "=r"(sp));
1226 sprintf(buf, "/proc/%d/maps", getpid());
1227 file = fopen(buf, "r");
1228 while (fgets(buf, BUF_SIZE, file) != NULL) {
1229 if (strstr(buf, "[stack]")) {
1237 sscanf(buf, "%x-", &stacktop);
1238 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp) - stacktop,
1240 perror("stack madvise fail");