bc72fdfb88be1b96038eb6f99412846ac82de80c
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-agent.c
1 /*
2  * BLUETOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@samsung.com>
7  *
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
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <malloc.h>
26
27 #include <vconf.h>
28 #include <bundle.h>
29 #include <bundle_internal.h>
30 #include <eventsystem.h>
31
32 #include <glib.h>
33 #include <gio/gio.h>
34 #include <dlog.h>
35 #include <vconf.h>
36
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <sys/mman.h>
41
42 /* BT HAL Headers */
43 #include "bt-hal.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"
50
51 #include "bt-hal-adapter-dbus-handler.h"
52 #include "bt-hal-rfcomm-dbus-handler.h"
53 #include "bt-hal-event-receiver.h"
54
55 #include <bt-hal-agent.h>
56 #include <bt-hal-gap-agent.h>
57 #include <bt-hal-dbus-common-utils.h>
58
59 /* TODO_40 : 4.0 merge  */
60 #include "bt-internal-types.h"
61
62 #define BT_HAL_AGENT_AUTO_PAIR_BLACKLIST_FILE (APP_SYSCONFDIR"/auto-pair-blacklist")
63 #define BT_HAL_AGENT_NEW_LINE "\r\n"
64 #define BUF_SIZE                256
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
74
75 #define G_VARIANT_UNREF(variant) \
76         g_variant_unref(variant); \
77         variant = NULL
78 #define BT_HAL_MAX_EVENT_STR_LENGTH 50
79
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;
85
86 /* Forward delcaration */
87 static void __bt_hal_send_pin_request_event(const gchar *address, const gchar *name,
88                 uint32_t cod);
89 static gboolean __bt_hal_pincode_request(GapAgentPrivate *agent, GDBusProxy *device);
90 static gboolean __bt_hal_display_request(GapAgentPrivate *agent, GDBusProxy *device,
91                 guint passkey);
92 static gboolean __bt_hal_passkey_request(GapAgentPrivate *agent, GDBusProxy *device);
93 static gboolean __bt_hal_confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
94                 guint passkey);
95 static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
96                                                                 const char *uuid);
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);
102
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);
116
117 void* _bt_hal_create_agent(const char *path, gboolean adapter)
118 {
119         GAP_AGENT_FUNC_CB func_cb;
120         GDBusProxy *adapter_proxy;
121         GapAgentPrivate *agent;
122
123         DBG("+");
124         adapter_proxy = _bt_hal_get_adapter_proxy();
125         if (!adapter_proxy)
126                 return NULL;
127
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;
135
136         /* Allocate memory*/
137         agent = g_new0(GapAgentPrivate, 1);
138
139         _gap_agent_setup_dbus(agent, &func_cb, path, adapter_proxy);
140
141         if (adapter) {
142                 if (!_gap_agent_register(agent)) {
143                         ERR("gap agent registration failed!");
144                         _bt_hal_destroy_agent(agent);
145                         agent = NULL;
146                 }
147         }
148         DBG("-");
149         return agent;
150 }
151
152 void _bt_hal_destroy_agent(void *agent)
153 {
154         DBG("+");
155         if (!agent)
156                 return;
157
158         _gap_agent_reset_dbus((GapAgentPrivate *)agent);
159
160         g_free(agent);
161         DBG("-");
162 }
163
164 gboolean _bt_hal_agent_is_canceled(void)
165 {
166         void *agent = _bt_hal_get_adapter_agent();
167         if (!agent)
168                 return FALSE;
169
170         return _gap_agent_is_canceled(agent);
171 }
172
173 int _bt_hal_agent_reply_cancellation(void)
174 {
175         void *agent = _bt_hal_get_adapter_agent();
176         if (!agent)
177                 return BT_STATUS_FAIL;
178
179         if (gap_agent_reply_confirmation(agent, GAP_AGENT_CANCEL, NULL) != TRUE) {
180                 ERR("Fail to reply agent");
181                 return BT_STATUS_FAIL;
182         }
183         DBG("gap agent cancellation done successfully!");
184         return BT_STATUS_SUCCESS;
185
186 }
187
188 void _bt_hal_agent_set_canceled(gboolean value)
189 {
190         void *agent = _bt_hal_get_adapter_agent();
191         if (!agent)
192                 return;
193
194         return _gap_agent_set_canceled(agent, value);
195 }
196
197 void _bt_hal_initialize_adapter_agent(void)
198 {
199         if (adapter_agent) {
200                 ERR("Adapter agent already exist!");
201                 return;
202         }
203
204         adapter_agent = _bt_hal_create_agent(BT_HAL_ADAPTER_AGENT_PATH, TRUE);
205         if (!adapter_agent) {
206                 ERR("Fail to register agent");
207                 return;
208         }
209 }
210
211 void _bt_hal_destroy_adapter_agent(void)
212 {
213         if (adapter_agent)
214                 _bt_hal_destroy_agent(adapter_agent);
215         adapter_agent = NULL;
216 }
217
218 void* _bt_hal_get_adapter_agent(void)
219 {
220         return adapter_agent;
221 }
222
223 void _bt_hal_enable_gap_auth_notifications(unsigned int type, gboolean enable)
224 {
225         INFO("type: %d, enable: %d", type, enable);
226
227         switch (type) {
228         case BT_PASSKEY_CONFIRMATION:
229                 /* Note: Currently not used for notification sending, should be used when required */
230                 passkey_confirm_notification = enable;
231                 break;
232         case BT_PASSKEY_DISPLAY:
233                 passkey_display_notification = enable;
234                 break;
235         case BT_PASSKEY_ENTRY:
236                 /* Note: Currently not used for notification sending, should be used when required */
237                 passkey_notification = enable;
238                 break;
239         case BT_PINCODE_ENTRY:
240                 pincode_notification = enable;
241                 break;
242         default:
243                 ERR("Unknown type: %d", type);
244         }
245 }
246
247 static void __bt_hal_send_authorize_request_event(const gchar *address, const char *uuid)
248 {
249         struct hal_ev_authorize_request ev;
250         memset(&ev, 0, sizeof(ev));
251
252         DBG("Remote Device address [%s]", address);
253
254         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
255
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);
259
260         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
261         if (event_cb) {
262                 DBG("Sending AUTHORIZE REQUEST");
263                 event_cb(HAL_EV_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
264         }
265
266         DBG("-");
267 }
268
269 #ifdef TIZEN_BT_HAL
270 static void __bt_hal_send_rfcomm_authorize_request_event(const gchar *address, const char *uuid, const char *name, char *path, int fd)
271 {
272         struct hal_ev_sock_conn_auth ev;
273
274         DBG("Remote Device address [%s], uuid: %s", address, uuid);
275
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);
279         if (name)
280                 memcpy(ev.name, name, strlen(name) - 1);
281         if (path)
282                 memcpy(ev.path, path, strlen(path) - 1);
283         ev.fd = fd;
284
285         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
286         if (event_cb) {
287                 DBG("Sending Socket AUTHORIZE REQUEST");
288                 event_cb(HAL_EV_SOCK_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
289         }
290
291         DBG("-");
292 }
293 #endif
294
295 /* Legacy Pairing */
296 static void __bt_hal_send_pin_request_event(const gchar *address, const gchar *name,
297                 uint32_t cod)
298 {
299         struct hal_ev_pin_request ev;
300         memset(&ev, 0, sizeof(ev));
301
302         DBG("Remote Device address [%s]", address);
303         DBG("Remote Device Name [%s]", name);
304         DBG("Remote Device COD [%u]", cod);
305
306         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
307
308         memcpy(ev.name, name, strlen(name)+1);
309         ev.class_of_dev = cod;
310
311         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
312         if (event_cb) {
313                 DBG("Sending PIN REQUEST");
314                 event_cb(HAL_EV_PIN_REQUEST, (void*)&ev, sizeof(ev));
315         }
316
317         DBG("-");
318 }
319
320
321 void __bt_hal_get_auth_info(GVariant *reply, char *auth_info)
322 {
323         int cursor;
324         GVariant *tmp_value;
325         char *manufacturer_data = NULL;
326         int manufacturer_data_len;
327         gboolean is_alias_set;
328         GVariantIter *value_iter;
329         guint8 m_value;
330         int i = 0;
331
332         tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
333                                                                 G_VARIANT_TYPE_BOOLEAN);
334         if (tmp_value) {
335                 is_alias_set = g_variant_get_boolean(tmp_value);
336                 g_variant_unref(tmp_value);
337         } else {
338                 is_alias_set = FALSE;
339         }
340         if (is_alias_set == FALSE) {
341                 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerDataLen",
342                                                                 G_VARIANT_TYPE_UINT16);
343                 if (tmp_value) {
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;
349                         }
350                         g_variant_unref(tmp_value);
351                 } else
352                         manufacturer_data_len = 0;
353
354                 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerData",
355                                                                 G_VARIANT_TYPE_ARRAY);
356                 if (tmp_value) {
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;
362                         } else {
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;
367
368                                 g_variant_iter_free(value_iter);
369                         }
370                         g_variant_unref(tmp_value);
371                 } else {
372                         INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
373                         manufacturer_data_len = 0;
374                         manufacturer_data = NULL;
375                 }
376                 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
377                 if (manufacturer_data_len < 30) {
378                         g_free(manufacturer_data);
379                         return;
380                 }
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);
384                         return;
385                 }
386
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) */
390
391                 cursor = 10;
392
393                 memcpy(auth_info, &(manufacturer_data[cursor]), 5);
394         }
395          g_free(manufacturer_data);
396 }
397
398 static gboolean __bt_hal_pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
399 {
400         uint32_t device_class;
401         const gchar *address;
402         unsigned char auth_info[5] = {0, };
403         const gchar *name;
404         GVariant *reply = NULL;
405         GVariant *reply_temp = NULL;
406         GVariant *tmp_value;
407         DBG("+");
408
409         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
410
411         if (reply_temp == NULL) {
412                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
413                                 NULL);
414                 goto done;
415         }
416
417         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
418
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);
422
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);
426
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);
430
431         if (!name)
432                 name = address;
433
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 };
438
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,
443                                                 "", NULL);
444                                 goto done;
445                         }
446
447                         gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
448                                         str_passkey, NULL);
449
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);
460                 } else {
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));
465                 }
466         } else {
467                 INFO("Plugin Headed not Enabled");
468                 __bt_hal_send_pin_request_event(address, name, device_class);
469         }
470
471 done:
472         g_variant_unref(reply);
473         g_variant_unref(reply_temp);
474         __bt_hal_agent_release_memory();
475         DBG("-");
476
477         return TRUE;
478 }
479
480
481 /* BT_SSP_VARIANT_PASSKEY_CONFIRMATION */
482 /* BT_SSP_VARIANT_PASSKEY_NOTIFICATION */
483 /* BT_SSP_VARIANT_PASSKEY_ENTRY */
484 /* BT_SSP_VARIANT_CONSENT */
485
486 static void __bt_hal_send_ssp_request_events(const gchar *address,
487                 const gchar *name,
488                 guint passkey,
489                 uint32_t cod,
490                 unsigned char variant)
491 {
492         struct hal_ev_ssp_request ev;
493         memset(&ev, 0, sizeof(ev));
494         DBG("sizeof ev [%zu]", sizeof(ev));
495
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);
501
502         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
503
504         memcpy(ev.name, name, strlen(name)+1);
505         ev.class_of_dev = cod;
506         ev.pairing_variant = variant;
507         ev.passkey = passkey;
508
509         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
510         if (event_cb) {
511                 DBG("Sending SSP type [%d]", variant);
512                 event_cb(HAL_EV_SSP_REQUEST, (void*)&ev, sizeof(ev));
513         }
514
515         DBG("-");
516 }
517
518 /* SSP */
519 static gboolean __bt_hal_display_request(GapAgentPrivate *agent, GDBusProxy *device,
520                 guint passkey)
521 {
522         const gchar *address;
523         const gchar *name;
524         unsigned char auth_info[5] = {0, };
525         char *str_passkey;
526         uint32_t device_class;
527         GVariant *reply = NULL;
528         GVariant *reply_temp = NULL;
529         GVariant *tmp_value = NULL;
530         DBG("+");
531
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, "",
535                                 NULL);
536                 goto done;
537         }
538
539         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
540
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);
544         if (!address) {
545                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
546                 goto done;
547         }
548
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);
552
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);
556
557         if (!name)
558                 name = address;
559
560         str_passkey = g_strdup_printf("%d", passkey);
561
562         __bt_hal_get_auth_info(reply, (char *)auth_info);
563
564         DBG("KEYBOARD_PASSKEY_REQUEST");
565
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));
574         } else {
575                 INFO("Plugin Headed not Enabled");
576                 __bt_hal_send_ssp_request_events(address, name, passkey, device_class,
577                                 BT_SSP_VARIANT_PASSKEY_NOTIFICATION);
578         }
579
580         g_free(str_passkey);
581 done:
582         g_variant_unref(reply);
583         g_variant_unref(reply_temp);
584         __bt_hal_agent_release_memory();
585
586         DBG("-");
587         return TRUE;
588 }
589
590 /* SSP */
591 static gboolean __bt_hal_passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
592 {
593         const gchar *address;
594         const gchar *name;
595         unsigned char auth_info[5] = {0, };
596         uint32_t device_class;
597         GVariant *reply = NULL;
598         GVariant *reply_temp = NULL;
599         GVariant *tmp_value;
600         DBG("+");
601
602         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
603
604         if (reply_temp == NULL) {
605                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
606                                 NULL);
607                 goto done;
608         }
609
610         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
611
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);
615         if (!address) {
616                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
617                 goto done;
618         }
619
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);
623
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);
627
628         if (!name)
629                 name = address;
630
631         DBG("PASSKEY_REQUEST");
632
633         __bt_hal_get_auth_info(reply, (char *)auth_info);
634
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));
638         } else {
639                 __bt_hal_send_ssp_request_events(address, name, 0, device_class,
640                                 BT_SSP_VARIANT_PASSKEY_ENTRY);
641         }
642
643 done:
644         g_variant_unref(reply);
645         g_variant_unref(reply_temp);
646         __bt_hal_agent_release_memory();
647
648         DBG("-");
649         return TRUE;
650 }
651
652 /*ssp*/
653 static gboolean __bt_hal_confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
654                 guint passkey)
655 {
656         const gchar *address;
657         const gchar *name;
658         char str_passkey[7];
659         uint32_t device_class;
660         GVariant *reply_temp = NULL;
661         GVariant *reply = NULL;
662         GVariant *tmp_value;
663         DBG("+ passkey[%.6d]", passkey);
664         DBG("Agent Path [%s]", agent->path);
665
666         snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
667
668         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
669
670         if (reply_temp == NULL) {
671                 ERR("####Device doesn't exist####");
672                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
673                                 NULL);
674                 goto done;
675         }
676         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
677
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);
681
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);
685
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);
689
690         if (!name)
691                 name = address;
692
693         if (headed_plugin_info->plugin_headed_enabled) {
694                 unsigned char auth_info[5] = {0, };
695
696                 DBG("LAUNCH SYSPOPUP");
697                 DBG("Name [%s]", name);
698                 DBG("Passkey [%s]", str_passkey);
699
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));
704         } else {
705                 DBG("Headless Confirmation");
706
707                 if (TIZEN_FEATURE_BLUEZ_SPEAKER_REFERENCE) {
708                         /* Reply the ssp confirmation automaticaly */
709                         gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
710                 }
711
712                 __bt_hal_send_ssp_request_events(address, name, passkey,
713                                 device_class, BT_SSP_VARIANT_PASSKEY_CONFIRMATION);
714         }
715
716 done:
717         g_variant_unref(reply);
718         g_variant_unref(reply_temp);
719         __bt_hal_agent_release_memory();
720         DBG("-");
721         return TRUE;
722 }
723
724 static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
725                 const char *uuid)
726 {
727         const gchar *address;
728         const gchar *name;
729         gboolean trust;
730         gboolean paired;
731         GVariant *reply = NULL;
732         GVariant *reply_temp = NULL;
733         GVariant *tmp_value;
734 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
735         bt_hal_agent_osp_server_t *osp_server;
736 #endif
737
738         DBG("Authorize Request from Bluez Stack: UUID [%s]", uuid);
739
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);
743                 goto done;
744         }
745
746         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
747
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);
751         if (!address) {
752                 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
753                 goto done;
754         }
755
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);
759         if (!name)
760                 name = address;
761
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);
765
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);
772                 goto done;
773         }
774
775         INFO("Authorization request for device [%s] Service:[%s]\n", address, uuid);
776
777         if (trust) {
778                 INFO("Trusted device, so authorize\n");
779                 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
780                 goto done;
781         } else {
782                 INFO("Device is not Trusted, so prompt user to accept or reject authorization \n");
783         }
784
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);
791                 else
792                         gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
793                 goto done;
794         }
795
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) ||
804 #ifdef TIZEN_BT_HAL
805                         !strcasecmp(uuid, BT_HAL_IOTIVITY_UUID) ||
806 #endif
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);
810                 goto done;
811         }
812
813 #ifdef TIZEN_BT_HAL
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);
819                         goto done;
820                 } else {
821                         INFO("Native OPP server, auto accept");
822                         gap_agent_reply_authorize(agent,
823                                         GAP_AGENT_ACCEPT, NULL);
824                         goto done;
825                 }
826         }
827
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);
831         if (osp_server) {
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);
834                 goto done;
835         }
836 #else
837         if (_is_rfcomm_server_uuid(uuid)) {
838                 __bt_hal_send_rfcomm_authorize_request_event(address, uuid, NULL, NULL, -1);
839                 goto done;
840         }
841 #endif
842 #endif
843
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);
848
849                 goto done;
850         }
851
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;
855
856                 __bt_hal_get_auth_info(reply, (char *)auth_info);
857
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;
864
865                 headed_plugin_info->headed_plugin->bt_launch_system_popup(request_type, name, auth_info, NULL, NULL,
866                                         _gap_agent_get_path(agent));
867         }
868
869 done:
870         g_variant_unref(reply);
871         g_variant_unref(reply_temp);
872         __bt_hal_agent_release_memory();
873
874         DBG("-");
875         return TRUE;
876 }
877
878 static gboolean __bt_hal_authorize_cancel_request(GapAgentPrivate *agent,
879                                                 const char *address)
880 {
881         DBG("On Going Authorization is cancelled by remote [%s]", address);
882         gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
883
884         if (headed_plugin_info->plugin_headed_enabled)
885                 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
886
887         __bt_hal_agent_release_memory();
888         return TRUE;
889 }
890
891 static gboolean __bt_hal_pairing_cancel_request(GapAgentPrivate *agent, const char *address)
892 {
893         DBG("On Going Pairing is cancelled by remote [%s]", address);
894
895         if (headed_plugin_info->plugin_headed_enabled)
896                 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
897
898         __bt_hal_agent_release_memory();
899         return TRUE;
900 }
901
902 static gboolean __bt_hal_device_is_hid_keyboard(unsigned int dev_class)
903 {
904         switch ((dev_class & 0x1f00) >> 8) {
905         case 0x05:
906                 switch ((dev_class & 0xc0) >> 6) {
907                 case 0x01:
908                         /* input-keyboard" */
909                         return TRUE;
910                 }
911                 break;
912         }
913
914         return FALSE;
915 }
916
917 static gboolean __bt_hal_is_hid_device_connectable(void)
918 {
919         GDBusProxy *proxy = NULL;
920         GVariant *reply = NULL;
921         GError *err = NULL;
922         gboolean connectable = FALSE;
923
924         proxy = _bt_hal_get_hid_agent_proxy();
925         if(proxy == NULL)
926                 return FALSE;
927
928         reply = g_dbus_proxy_call_sync(proxy, "IsHidConnectable", NULL,
929                                 G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &err);
930         if (reply == NULL) {
931                 ERR("Error returned in method call");
932                 if (err != NULL) {
933                         ERR("Error message = %s", err->message);
934                         g_error_free(err);
935                 }
936                 connectable = FALSE;
937         } else {
938                 g_variant_get(reply, "(b)", &connectable);
939                 g_variant_unref(reply);
940         }
941         g_object_unref(proxy);
942
943         INFO("HID Device is %s",
944                         connectable ? "Connectable" : "Non-connectable");
945
946         return connectable;
947 }
948
949 static int __bt_hal_device_generate_passkey(char *passkey, int size)
950 {
951         int i;
952         ssize_t len;
953         int random_fd;
954         unsigned int value = 0;
955
956         if (passkey == NULL)
957                 return -1;
958
959         if (size <= 0)
960                 return -1;
961
962         random_fd = open("/dev/urandom", O_RDONLY);
963
964         if (random_fd < 0)
965                 return -1;
966
967         for (i = 0; i < size; i++) {
968                 len = read(random_fd, &value, sizeof(value));
969                 if (len > 0)
970                         passkey[i] = '0' + (value % 10);
971         }
972
973         close(random_fd);
974
975         DBG("passkey: %s", passkey);
976
977         return 0;
978 }
979
980 static gboolean __bt_hal_find_device_by_address_exactname(char *buffer,
981                 const char *address)
982 {
983         char *pch;
984         char *last;
985
986         pch = strtok_r(buffer, "= ,", &last);
987
988         if (pch == NULL)
989                 return FALSE;
990
991         while ((pch = strtok_r(NULL, ",", &last))) {
992                 if (0 == g_strcmp0(pch, address)) {
993                         DBG("Match found\n");
994                         return TRUE;
995                 }
996         }
997         return FALSE;
998 }
999
1000 static gboolean __bt_hal_find_device_by_partial_name(char *buffer,
1001                 const char *partial_name)
1002 {
1003         char *pch;
1004         char *last;
1005
1006         pch = strtok_r(buffer, "= ,", &last);
1007
1008         if (pch == NULL)
1009                 return FALSE;
1010
1011         while ((pch = strtok_r(NULL, ",", &last))) {
1012                 if (g_str_has_prefix(partial_name, pch)) {
1013                         DBG("Match found\n");
1014                         return TRUE;
1015                 }
1016         }
1017         return FALSE;
1018 }
1019
1020 static gboolean __bt_hal_device_is_device_blacklisted(const char *address,
1021                 const char *name)
1022 {
1023         char *buffer;
1024         char **lines;
1025         int i;
1026         FILE *fp;
1027         long size;
1028         size_t result;
1029
1030         DBG("+");
1031
1032         fp = fopen(BT_HAL_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
1033
1034         if (fp == NULL) {
1035                 ERR("Unable to open blacklist file");
1036                 return FALSE;
1037         }
1038
1039         fseek(fp, 0, SEEK_END);
1040         size = ftell(fp);
1041         if (size <= 0) {
1042                 DBG("size is not a positive number");
1043                 fclose(fp);
1044                 return FALSE;
1045         }
1046
1047         rewind(fp);
1048
1049         buffer = g_malloc0(sizeof(char) * size);
1050         /* Fix : NULL_RETURNS */
1051         if (buffer == NULL) {
1052                 ERR("Fail to allocate memory");
1053                 fclose(fp);
1054                 return FALSE;
1055         }
1056         result = fread((char *)buffer, 1, size, fp);
1057         fclose(fp);
1058         if (result != size) {
1059                 ERR("Read Error");
1060                 g_free(buffer);
1061                 return FALSE;
1062         }
1063
1064         DBG("Buffer = %s", buffer);
1065
1066         lines = g_strsplit_set(buffer, BT_HAL_AGENT_NEW_LINE, 0);
1067         g_free(buffer);
1068
1069         if (lines == NULL) {
1070                 ERR("No lines in the file");
1071                 return FALSE;
1072         }
1073
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(
1077                                                 lines[i], address))
1078                                 goto done;
1079                 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
1080                         if (__bt_hal_find_device_by_address_exactname(
1081                                                 lines[i], name))
1082                                 goto done;
1083                 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
1084                         if (__bt_hal_find_device_by_partial_name(lines[i],
1085                                                 name))
1086                                 goto done;
1087                 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1088                         if (__bt_hal_find_device_by_address_exactname(
1089                                                 lines[i], address))
1090                                 goto done;
1091         }
1092         g_strfreev(lines);
1093         DBG("-");
1094         return FALSE;
1095 done:
1096         DBG("Found the device");
1097         g_strfreev(lines);
1098         return TRUE;
1099 }
1100
1101 static gboolean __bt_hal_device_is_auto_response(uint32_t dev_class,
1102                 const gchar *address, const gchar *name)
1103 {
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];
1108
1109         DBG("bt_agent_is_headset_class, %d +", dev_class);
1110
1111         if (address == NULL)
1112                 return FALSE;
1113
1114         _bt_hal_dbus_get_adapter_class(&adapter_class);
1115
1116         if (((adapter_class & 0x1f00) >> 8) == 0x05)
1117         {
1118                 DBG("Adapter is Peripheral, do not response automatically");
1119                 return FALSE;
1120         }
1121
1122         switch ((dev_class & 0x1f00) >> 8) {
1123         case 0x04:
1124                 switch ((dev_class & 0xfc) >> 2) {
1125                 case 0x01:
1126                 case 0x02:
1127                         /* Headset */
1128                         is_headset = TRUE;
1129                         break;
1130                 case 0x06:
1131                         /* Headphone */
1132                         is_headset = TRUE;
1133                         break;
1134                 case 0x0b:      /* VCR */
1135                 case 0x0c:      /* Video Camera */
1136                 case 0x0d:      /* Camcorder */
1137                         break;
1138                 default:
1139                         /* Other audio device */
1140                         is_headset = TRUE;
1141                         break;
1142                 }
1143                 break;
1144         case 0x05:
1145                 switch (dev_class & 0xff) {
1146                 case 0x80:  /* 0x80: Pointing device(Mouse) */
1147                         is_mouse = TRUE;
1148                         break;
1149
1150                 case 0x40: /* 0x40: input device (BT keyboard) */
1151
1152                         /* Get the LAP(Lower Address part) */
1153                         g_strlcpy(lap_address, address, sizeof(lap_address));
1154
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");
1158                                 return FALSE;
1159                         } else {
1160                                 ERR("Device is black listed\n");
1161                                 return TRUE;
1162                         }
1163                 }
1164         }
1165
1166         if ((!is_headset) && (!is_mouse))
1167                 return FALSE;
1168
1169         /* Get the LAP(Lower Address part) */
1170         g_strlcpy(lap_address, address, sizeof(lap_address));
1171
1172         DBG("Device address = %s\n", address);
1173         DBG("Address 3 byte = %s\n", lap_address);
1174
1175         if (__bt_hal_device_is_device_blacklisted(lap_address, name)) {
1176                 ERR("Device is black listed\n");
1177                 return FALSE;
1178         }
1179
1180         return TRUE;
1181 }
1182
1183 static GVariant *__bt_hal_service_getall(GDBusProxy *device, const char *interface)
1184 {
1185         GError *error = NULL;
1186         GVariant *reply;
1187         DBG("+");
1188         reply = g_dbus_proxy_call_sync(device,
1189                         "GetAll", g_variant_new("(s)", interface),
1190                         G_DBUS_CALL_FLAGS_NONE, -1,
1191                         NULL, &error);
1192         if (reply == NULL) {
1193                 ERR("GetAll dBUS-RPC failed");
1194                 if (error) {
1195                         ERR("D-Bus API failure: errCode[%x], message[%s]",
1196                                         error->code, error->message);
1197                         g_clear_error(&error);
1198                 }
1199                 return NULL;
1200         }
1201
1202         DBG("-");
1203         return reply;
1204 }
1205
1206 static void __bt_hal_agent_release_memory(void)
1207 {
1208         /* Release Malloc Memory*/
1209         malloc_trim(0);
1210
1211         /* Release Stack Memory*/
1212         stack_trim();
1213 }
1214
1215 static inline void stack_trim(void)
1216 {
1217 #ifdef STACK_FLUSH
1218         unsigned int sp;
1219         char buf[BUF_SIZE];
1220         FILE *file;
1221         unsigned int stacktop;
1222         int found = 0;
1223
1224         asm volatile ("mov %0,sp " : "=r"(sp));
1225
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]")) {
1230                         found = 1;
1231                         break;
1232                 }
1233         }
1234         fclose(file);
1235
1236         if (found) {
1237                 sscanf(buf, "%x-", &stacktop);
1238                 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp) - stacktop,
1239                                         MADV_DONTNEED) < 0)
1240                         perror("stack madvise fail");
1241         }
1242 #endif
1243 }