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>
23 #include <syspopup_caller.h>
25 #include <bundle_internal.h>
27 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
28 #include <tethering.h>
31 #include "bt-internal-types.h"
32 #include "bt-service-common.h"
33 #include "bt-service-agent.h"
34 #include "bt-service-gap-agent.h"
35 #include "bt-service-adapter.h"
36 #include "bt-service-event.h"
37 #include "bt-service-rfcomm-server.h"
38 #include "bt-service-device.h"
39 #include "bt-service-audio.h"
41 #ifdef TIZEN_FEATURE_BT_DPM
42 #include "bt-service-dpm.h"
45 #define BT_APP_AUTHENTICATION_TIMEOUT 35
46 #define BT_APP_AUTHORIZATION_TIMEOUT 15
48 #define HFP_AUDIO_GATEWAY_UUID "0000111f-0000-1000-8000-00805f9b34fb"
49 #define HSP_AUDIO_GATEWAY_UUID "00001112-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"
61 #define HID_DEVICE_UUID "00001124-0000-1000-8000-00805f9b43bf"
62 #define SAP_UUID_OLD "a49eb41e-cb06-495c-9f4f-bb80a90cdf00"
63 #define SAP_UUID_NEW "a49eb41e-cb06-495c-9f4f-aa80a90cdf4a"
64 #define IOTIVITY_UUID "12341234-1C25-481F-9DFB-59193D238280"
66 #define BT_AGENT_OBJECT "/org/bluez/agent/frwk_agent"
68 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
70 #define BT_AGENT_SIGNAL_RFCOMM_AUTHORIZE "RfcommAuthorize"
71 #define BT_AGENT_SIGNAL_OBEX_AUTHORIZE "ObexAuthorize"
73 #define BT_PASSKEY_MAX_LENGTH 4
75 #define BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
76 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
77 #define BT_PAN_MAX_CONNECTION 4
79 extern guint nap_connected_device_count;
81 static char *passkey_watcher = NULL;
83 #define G_VARIANT_UNREF(variant) \
84 g_variant_unref(variant); \
87 static int __bt_agent_is_auto_response(uint32_t dev_class, const gchar *address,
89 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class);
90 static int __bt_agent_generate_passkey(char *passkey, int size);
92 static void __bt_agent_release_memory(void)
94 /* Release Malloc Memory*/
97 /* Release Stack Memory*/
100 static gboolean __bt_agent_system_popup_timer_cb(gpointer user_data)
103 static int retry_count;
104 bundle *b = (bundle *)user_data;
105 retv_if(user_data == NULL, FALSE);
109 ret = syspopup_launch("bt-syspopup", b);
111 BT_ERR("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
113 if (retry_count >= BT_AGENT_SYSPOPUP_MAX_ATTEMPT) {
114 BT_ERR("Sorry!! Max retry %d reached", retry_count);
120 BT_DBG("Hurray!! Finally Popup launched");
125 return (ret < 0) ? TRUE : FALSE;
128 int _bt_launch_system_popup(bt_agent_event_type_t event_type,
129 const char *device_name,
130 const unsigned char *auth_info,
132 const char *filename,
133 const char *agent_path)
137 char event_str[BT_MAX_EVENT_STR_LENGTH + 1];
141 BT_ERR("Launching system popup failed");
145 bundle_add(b, "device-name", device_name);
146 bundle_add(b, "auth-info", (const char *)auth_info);
147 bundle_add(b, "passkey", passkey);
148 bundle_add(b, "file", filename);
149 bundle_add(b, "agent-path", agent_path);
151 switch (event_type) {
152 case BT_AGENT_EVENT_PIN_REQUEST:
153 g_strlcpy(event_str, "pin-request", sizeof(event_str));
156 case BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST:
157 g_strlcpy(event_str, "passkey-confirm-request",
161 case BT_AGENT_EVENT_PASSKEY_REQUEST:
162 g_strlcpy(event_str, "passkey-request", sizeof(event_str));
165 case BT_AGENT_EVENT_PASSKEY_DISPLAY_REQUEST:
166 g_strlcpy(event_str, "passkey-display-request",
170 case BT_AGENT_EVENT_AUTHORIZE_REQUEST:
171 g_strlcpy(event_str, "authorize-request",
175 case BT_AGENT_EVENT_CONFIRM_MODE_REQUEST:
176 g_strlcpy(event_str, "confirm-mode-request",
180 case BT_AGENT_EVENT_FILE_RECEIVED:
181 g_strlcpy(event_str, "file-received", sizeof(event_str));
184 case BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST:
185 g_strlcpy(event_str, "keyboard-passkey-request",
189 case BT_AGENT_EVENT_TERMINATE:
190 g_strlcpy(event_str, "terminate", sizeof(event_str));
193 case BT_AGENT_EVENT_EXCHANGE_REQUEST:
194 g_strlcpy(event_str, "exchange-request", sizeof(event_str));
197 case BT_AGENT_EVENT_PBAP_REQUEST:
198 g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
201 case BT_AGENT_EVENT_MAP_REQUEST:
202 g_strlcpy(event_str, "message-request", sizeof(event_str));
205 case BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE:
206 g_strlcpy(event_str, "remote-legacy-pair-failed", sizeof(event_str));
210 BT_ERR("Invalid event type");
216 bundle_add(b, "event-type", event_str);
218 ret = syspopup_launch("bt-syspopup", b);
220 BT_ERR("Popup launch failed...retry %d", ret);
222 g_timeout_add(BT_AGENT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
223 (GSourceFunc)__bt_agent_system_popup_timer_cb, b);
228 BT_INFO("_bt_agent_launch_system_popup");
232 static GVariant *__bt_service_getall(GDBusProxy *device, const char *interface)
234 GError *error = NULL;
237 reply = g_dbus_proxy_call_sync(device,
238 "GetAll", g_variant_new("(s)", interface),
239 G_DBUS_CALL_FLAGS_NONE, -1,
242 ERR("GetAll dBUS-RPC failed");
244 ERR("D-Bus API failure: errCode[%x], message[%s]",
245 error->code, error->message);
246 g_clear_error(&error);
254 static gboolean __pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
256 uint32_t device_class;
257 gchar *address = NULL;
258 unsigned char auth_info[5] = {0, };
260 GVariant *reply = NULL;
261 GVariant *reply_temp = NULL;
263 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
267 #ifdef TIZEN_FEATURE_BT_DPM
268 if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
269 BT_ERR("Not allow to pair the device");
270 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
271 __bt_agent_release_memory();
276 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
278 if (reply_temp == NULL) {
279 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
284 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
286 tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
287 g_variant_get(tmp_value, "u", &device_class);
288 G_VARIANT_UNREF(tmp_value);
290 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
291 g_variant_get(tmp_value, "s", &address);
292 G_VARIANT_UNREF(tmp_value);
294 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
298 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
299 g_variant_get(tmp_value, "s", &name);
300 G_VARIANT_UNREF(tmp_value);
302 BT_DBG("Replacing the name with address");
303 name = g_strdup(address);
305 BT_INFO("Name = %s, Address = %s, Class = 0x%x", name, address, device_class);
306 if (name[0] == '\0') {
308 BT_DBG("Name[0]=NULL, Replacing the name with address");
309 name = g_strdup(address);
313 __bt_get_auth_info(reply, (char *)auth_info);
314 if (_bt_is_device_creating() == TRUE &&
315 _bt_is_bonding_device_address(address) == TRUE &&
316 __bt_agent_is_auto_response(device_class, address, name)) {
317 BT_DBG("0000 Auto Pair");
318 /* Use Fixed PIN "0000" for basic pairing */
319 _bt_set_autopair_status_in_bonding_info(TRUE);
320 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, "0000",
322 } else if (__bt_agent_is_hid_keyboard(device_class)) {
323 BT_DBG("HID Keyboard");
324 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
326 if (__bt_agent_generate_passkey(str_passkey,
327 BT_PASSKEY_MAX_LENGTH) != 0) {
328 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
333 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
336 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
337 name, auth_info, str_passkey, NULL,
338 _gap_agent_get_path(agent));
339 } else if (_bt_get_device_pin_code(address, pin_code)
340 == BLUETOOTH_ERROR_NONE) {
341 BT_DBG("Use stored PIN code(%s)", pin_code);
342 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, pin_code,
346 BT_DBG("Show Pin entry");
347 _bt_launch_system_popup(BT_AGENT_EVENT_PIN_REQUEST, name, auth_info,
348 NULL, NULL, _gap_agent_get_path(agent));
352 g_variant_unref(reply);
353 g_variant_unref(reply_temp);
356 __bt_agent_release_memory();
362 static gboolean __passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
364 gchar *address = NULL;
366 unsigned char auth_info[5] = {0, };
367 GVariant *reply = NULL;
368 GVariant *reply_temp = NULL;
372 #ifdef TIZEN_FEATURE_BT_DPM
373 if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
374 BT_ERR("Not allow to pair the device");
375 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
376 __bt_agent_release_memory();
381 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
383 if (reply_temp == NULL) {
384 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
389 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
391 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
392 g_variant_get(tmp_value, "s", &address);
393 G_VARIANT_UNREF(tmp_value);
395 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
399 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
400 g_variant_get(tmp_value, "s", &name);
401 G_VARIANT_UNREF(tmp_value);
403 name = g_strdup(address);
405 __bt_get_auth_info(reply, (char *)auth_info);
407 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_REQUEST, name, auth_info,
408 NULL, NULL, _gap_agent_get_path(agent));
411 g_variant_unref(reply);
412 g_variant_unref(reply_temp);
415 __bt_agent_release_memory();
421 static gboolean __display_request(GapAgentPrivate *agent, GDBusProxy *device,
424 gchar *address = NULL;
426 unsigned char auth_info[5] = {0, };
428 GVariant *reply = NULL;
429 GVariant *reply_temp = NULL;
430 GVariant *tmp_value = NULL;
434 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
435 if (reply_temp == NULL) {
436 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
441 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
443 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
444 g_variant_get(tmp_value, "s", &address);
445 G_VARIANT_UNREF(tmp_value);
447 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
451 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
452 g_variant_get(tmp_value, "s", &name);
453 G_VARIANT_UNREF(tmp_value);
455 name = g_strdup(address);
457 __bt_get_auth_info(reply, (char *)auth_info);
459 str_passkey = g_strdup_printf("%06d", passkey);
461 if (passkey_watcher) {
462 GVariant *param = NULL;
464 BT_INFO("Send passkey to %s", passkey_watcher);
466 param = g_variant_new("(ss)", address, str_passkey);
468 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
469 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
471 _bt_launch_system_popup(BT_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
472 auth_info, str_passkey, NULL,
473 _gap_agent_get_path(agent));
479 g_variant_unref(reply);
480 g_variant_unref(reply_temp);
483 __bt_agent_release_memory();
489 static gboolean __confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
492 gchar *address = NULL;
494 unsigned char auth_info[5] = {0, };
496 GVariant *reply_temp = NULL;
497 GVariant *reply = NULL;
499 BT_DBG("+ passkey[%.6d]", passkey);
501 #ifdef TIZEN_FEATURE_BT_DPM
502 if (_bt_dpm_get_bluetooth_pairing_state() == DPM_RESTRICTED) {
503 BT_ERR("Not allow to pair the device");
504 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
505 __bt_agent_release_memory();
510 snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
512 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
514 if (reply_temp == NULL) {
515 BT_ERR("Device doesn't exist");
516 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
521 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
523 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
524 g_variant_get(tmp_value, "s", &address);
525 G_VARIANT_UNREF(tmp_value);
527 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
531 tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
532 g_variant_get(tmp_value, "s", &name);
533 G_VARIANT_UNREF(tmp_value);
535 name = g_strdup(address);
536 __bt_get_auth_info(reply, (char *)auth_info);
538 BT_DBG("LAUNCH SYSPOPUP");
539 _bt_launch_system_popup(BT_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST, name,
540 auth_info, str_passkey, NULL,
541 _gap_agent_get_path(agent));
544 g_variant_unref(reply);
545 g_variant_unref(reply_temp);
548 __bt_agent_release_memory();
554 static gboolean __pairing_cancel_request(GapAgentPrivate *agent, const char *address)
556 BT_DBG("On Going Pairing is cancelled by remote\n");
558 syspopup_destroy_all();
560 __bt_agent_release_memory();
565 static gboolean __a2dp_authorize_request_check(void)
567 /* Check for existing Media device to disconnect */
568 return _bt_is_headset_type_connected(BT_AUDIO_A2DP, NULL);
571 static gboolean __authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
574 gchar *address = NULL;
576 unsigned char auth_info[5] = {0, };
578 GVariant *reply = NULL;
579 GVariant *reply_temp = NULL;
581 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
583 tethering_h tethering = NULL;
585 int result = BLUETOOTH_ERROR_NONE;
586 int request_type = BT_AGENT_EVENT_AUTHORIZE_REQUEST;
590 /* Check if already Media connection exsist */
591 if (!strcasecmp(uuid, A2DP_UUID)) {
592 gboolean ret = FALSE;
594 ret = __a2dp_authorize_request_check();
597 BT_ERR("Already one A2DP device connected \n");
598 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
603 /* Check completed */
605 if (!strcasecmp(uuid, HFP_AUDIO_GATEWAY_UUID) ||
606 !strcasecmp(uuid, HSP_AUDIO_GATEWAY_UUID) ||
607 !strcasecmp(uuid, HFP_HS_UUID) ||
608 !strcasecmp(uuid, HSP_HS_UUID) ||
609 !strcasecmp(uuid, A2DP_UUID) ||
610 !strcasecmp(uuid, HID_UUID) ||
611 !strcasecmp(uuid, HID_DEVICE_UUID) ||
612 !strcasecmp(uuid, SAP_UUID_OLD) ||
613 !strcasecmp(uuid, SAP_UUID_NEW) ||
614 !strcasecmp(uuid, IOTIVITY_UUID) ||
615 !strcasecmp(uuid, AVRCP_TARGET_UUID)) {
616 BT_DBG("Auto accept authorization for audio device (HFP, A2DP, AVRCP) [%s]", uuid);
617 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
623 if (!strcasecmp(uuid, NAP_UUID) ||
624 !strcasecmp(uuid, GN_UUID) ||
625 !strcasecmp(uuid, BNEP_UUID)) {
627 BT_DBG("Network connection request: %s", uuid);
628 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
629 if (nap_connected_device_count >=
630 BT_PAN_MAX_CONNECTION) {
631 BT_ERR("Max connection exceeded");
635 ret = tethering_create(&tethering);
637 if (ret != TETHERING_ERROR_NONE) {
638 BT_ERR("Fail to create tethering: %d", ret);
642 enabled = tethering_is_enabled(tethering, TETHERING_TYPE_BT);
644 ret = tethering_destroy(tethering);
646 if (ret != TETHERING_ERROR_NONE)
647 BT_ERR("Fail to create tethering: %d", ret);
649 if (enabled != true) {
650 BT_ERR("BT tethering is not enabled");
655 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT,
658 #ifdef TIZEN_FEATURE_NETWORK_TETHERING_ENABLE
660 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT,
667 reply_temp = __bt_service_getall(device, BT_DEVICE_INTERFACE);
668 if (reply_temp == NULL) {
669 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
674 g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
676 tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
677 g_variant_get(tmp_value, "s", &address);
678 G_VARIANT_UNREF(tmp_value);
680 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
684 tmp_value = g_variant_lookup_value(reply, "Alias", G_VARIANT_TYPE_STRING);
685 g_variant_get(tmp_value, "s", &name);
686 G_VARIANT_UNREF(tmp_value);
688 name = g_strdup(address);
690 tmp_value = g_variant_lookup_value(reply, "Trusted", G_VARIANT_TYPE_BOOLEAN);
691 g_variant_get(tmp_value, "b", &trust);
692 G_VARIANT_UNREF(tmp_value);
694 __bt_get_auth_info(reply, (char *)auth_info);
696 BT_INFO("Authorization request for device [%s] Service:[%s]\n", address, uuid);
698 if (strcasecmp(uuid, OPP_UUID) == 0 &&
699 _gap_agent_exist_osp_server(agent, BT_OBEX_SERVER,
701 _bt_send_event(BT_OPP_SERVER_EVENT,
702 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
703 g_variant_new("(iss)", result, address, name));
708 if (_gap_agent_exist_osp_server(agent, BT_RFCOMM_SERVER,
709 (char *)uuid) == TRUE) {
710 bt_agent_osp_server_t *osp_serv;
711 osp_serv = _gap_agent_get_osp_server(agent,
712 BT_RFCOMM_SERVER, (char *)uuid);
715 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
716 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
717 g_variant_new("(issssn)", result, address, uuid,
718 name, osp_serv->path, osp_serv->fd));
724 if (!strcasecmp(uuid, OPP_UUID))
725 request_type = BT_AGENT_EVENT_EXCHANGE_REQUEST;
726 else if (!strcasecmp(uuid, PBAP_UUID))
727 request_type = BT_AGENT_EVENT_PBAP_REQUEST;
728 else if (!strcasecmp(uuid, MAP_UUID))
729 request_type = BT_AGENT_EVENT_MAP_REQUEST;
732 BT_INFO("Trusted device, so authorize\n");
733 gap_agent_reply_authorize(agent,
734 GAP_AGENT_ACCEPT, NULL);
736 _bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
737 _gap_agent_get_path(agent));
742 g_variant_unref(reply);
745 g_variant_unref(reply_temp);
749 __bt_agent_release_memory();
755 static gboolean __authorization_cancel_request(GapAgentPrivate *agent,
758 BT_DBG("On Going Authorization is cancelled by remote\n");
760 gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
762 syspopup_destroy_all();
764 __bt_agent_release_memory();
769 void _bt_destroy_agent(void *agent)
774 _gap_agent_reset_dbus((GapAgentPrivate *)agent);
779 void* _bt_create_agent(const char *path, gboolean adapter)
781 GAP_AGENT_FUNC_CB func_cb;
782 GDBusProxy *adapter_proxy;
783 GapAgentPrivate *agent;
785 adapter_proxy = _bt_get_adapter_proxy();
789 func_cb.pincode_func = __pincode_request;
790 func_cb.display_func = __display_request;
791 func_cb.passkey_func = __passkey_request;
792 func_cb.confirm_func = __confirm_request;
793 func_cb.authorize_func = __authorize_request;
794 func_cb.pairing_cancel_func = __pairing_cancel_request;
795 func_cb.authorization_cancel_func = __authorization_cancel_request;
798 agent = g_new0(GapAgentPrivate, 1);
800 _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
803 if (!_gap_agent_register(agent)) {
804 _bt_destroy_agent(agent);
812 gboolean _bt_agent_register_osp_server(const gint type,
813 const char *uuid, char *path, int fd)
815 void *agent = _bt_get_adapter_agent();
819 return _gap_agent_register_osp_server(agent, type, uuid, path, fd);
823 gboolean _bt_agent_unregister_osp_server(const gint type, const char *uuid)
825 void *agent = _bt_get_adapter_agent();
830 return _gap_agent_unregister_osp_server(agent, type, uuid);
833 gboolean _bt_agent_reply_authorize(gboolean accept)
837 void *agent = _bt_get_adapter_agent();
841 accept_val = accept ? GAP_AGENT_ACCEPT : GAP_AGENT_REJECT;
843 return gap_agent_reply_authorize(agent, accept_val, NULL);
846 gboolean _bt_agent_is_canceled(void)
848 void *agent = _bt_get_adapter_agent();
852 return _gap_agent_is_canceled(agent);
855 void _bt_agent_set_canceled(gboolean value)
857 void *agent = _bt_get_adapter_agent();
861 return _gap_agent_set_canceled(agent, value);
864 int _bt_agent_reply_cancellation(void)
866 void *agent = _bt_get_adapter_agent();
869 return BLUETOOTH_ERROR_INTERNAL;
871 if (gap_agent_reply_confirmation(agent, GAP_AGENT_CANCEL, NULL) != TRUE) {
872 BT_ERR("Fail to reply agent");
873 return BLUETOOTH_ERROR_INTERNAL;
876 return BLUETOOTH_ERROR_NONE;
879 static gboolean __bt_agent_is_hid_keyboard(uint32_t dev_class)
881 switch ((dev_class & 0x1f00) >> 8) {
883 switch ((dev_class & 0xc0) >> 6) {
885 /* input-keyboard" */
894 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
900 pch = strtok_r(buffer, "= ,", &last);
905 while ((pch = strtok_r(NULL, ",", &last))) {
906 if (0 == g_strcmp0(pch, address)) {
907 BT_DBG("Match found\n");
914 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
915 const char *partial_name)
920 pch = strtok_r(buffer, "= ,", &last);
925 while ((pch = strtok_r(NULL, ",", &last))) {
926 if (g_str_has_prefix(partial_name, pch)) {
927 BT_DBG("Match found\n");
934 static gboolean __bt_agent_is_device_blacklist(const char *address,
946 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
949 BT_ERR("Unable to open blacklist file");
953 fseek(fp, 0, SEEK_END);
956 BT_DBG("size is not a positive number");
963 buffer = g_malloc0(sizeof(char) * size);
964 /* Fix : NULL_RETURNS */
965 if (buffer == NULL) {
966 BT_ERR("Fail to allocate memory");
970 result = fread((char *)buffer, 1, size, fp);
972 if (result != size) {
973 BT_ERR("Read Error");
978 BT_DBG("Buffer = %s", buffer);
980 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
984 BT_ERR("No lines in the file");
988 for (i = 0; lines[i] != NULL; i++) {
989 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
990 if (__bt_agent_find_device_by_address_exactname(
993 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
994 if (__bt_agent_find_device_by_address_exactname(
997 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
998 if (__bt_agent_find_device_by_partial_name(lines[i],
1001 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1002 if (__bt_agent_find_device_by_address_exactname(
1010 BT_DBG("Found the device");
1015 static gboolean __bt_agent_is_auto_response(uint32_t dev_class,
1016 const gchar *address, const gchar *name)
1018 gboolean is_headset = FALSE;
1019 gboolean is_mouse = FALSE;
1020 char lap_address[BT_LOWER_ADDRESS_LENGTH];
1022 BT_DBG("bt_agent_is_headset_class, %d +", dev_class);
1024 if (address == NULL)
1027 switch ((dev_class & 0x1f00) >> 8) {
1029 switch ((dev_class & 0xfc) >> 2) {
1039 case 0x0b: /* VCR */
1040 case 0x0c: /* Video Camera */
1041 case 0x0d: /* Camcorder */
1044 /* Other audio device */
1050 switch (dev_class & 0xff) {
1051 case 0x80: /* 0x80: Pointing device(Mouse) */
1055 case 0x40: /* 0x40: input device (BT keyboard) */
1057 /* Get the LAP(Lower Address part) */
1058 g_strlcpy(lap_address, address, sizeof(lap_address));
1060 /* Need to Auto pair the blacklisted Keyboard */
1061 if (__bt_agent_is_device_blacklist(lap_address, name) != TRUE) {
1062 BT_DBG("Device is not black listed\n");
1065 BT_ERR("Device is black listed\n");
1071 if ((!is_headset) && (!is_mouse))
1074 /* Get the LAP(Lower Address part) */
1075 g_strlcpy(lap_address, address, sizeof(lap_address));
1077 BT_DBG("Device address = %s\n", address);
1078 BT_DBG("Address 3 byte = %s\n", lap_address);
1080 if (__bt_agent_is_device_blacklist(lap_address, name)) {
1081 BT_ERR("Device is black listed\n");
1088 static int __bt_agent_generate_passkey(char *passkey, int size)
1093 unsigned int value = 0;
1095 if (passkey == NULL)
1101 random_fd = open("/dev/urandom", O_RDONLY);
1106 for (i = 0; i < size; i++) {
1107 len = read(random_fd, &value, sizeof(value));
1109 passkey[i] = '0' + (value % 10);
1114 BT_DBG("passkey: %s", passkey);
1119 int _bt_set_passkey_notification(const char *sender, gboolean enable)
1121 BT_INFO("Set passkey notification(sender:%s, %s)",
1122 sender, enable ? "Enable" : "Disable");
1124 g_free(passkey_watcher);
1126 passkey_watcher = g_strdup(sender);
1128 passkey_watcher = NULL;
1130 return BLUETOOTH_ERROR_NONE;