0390f8791f2e1fe945fb8b55f96b86bb35eeec0d
[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                 event_cb(HAL_EV_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
263         }
264 }
265
266 #ifdef TIZEN_BT_HAL
267 static void __bt_hal_send_rfcomm_authorize_request_event(const gchar *address, const char *uuid, const char *name, char *path, int fd)
268 {
269         struct hal_ev_sock_conn_auth ev;
270
271         DBG("Remote Device address [%s], uuid: %s", address, uuid);
272
273         memset(&ev, 0, sizeof(ev));
274         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
275         _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid);
276
277         if (name)
278                 g_strlcpy((char *)ev.name, name, BT_HAL_DEVICE_NAME_LENGTH_MAX);
279
280         if (path)
281                 g_strlcpy((char *)ev.path, path, BT_HAL_PATH_NAME_LENGTH_MAX);
282
283         ev.fd = fd;
284
285         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
286         if (event_cb) {
287                 event_cb(HAL_EV_SOCK_AUTHORIZE_REQUEST, (void*)&ev, sizeof(ev));
288         }
289 }
290 #endif
291
292 /* Legacy Pairing */
293 static void __bt_hal_send_pin_request_event(const gchar *address, const gchar *name,
294                 uint32_t cod)
295 {
296         struct hal_ev_pin_request ev;
297         memset(&ev, 0, sizeof(ev));
298
299         DBG("Remote Device address [%s]", address);
300         DBG("Remote Device Name [%s]", name);
301         DBG("Remote Device COD [%u]", cod);
302
303         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
304
305         memcpy(ev.name, name, strlen(name)+1);
306         ev.class_of_dev = cod;
307
308         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
309         if (event_cb) {
310                 DBG("Sending PIN REQUEST");
311                 event_cb(HAL_EV_PIN_REQUEST, (void*)&ev, sizeof(ev));
312         }
313
314         DBG("-");
315 }
316
317
318 void __bt_hal_get_auth_info(GVariant *reply, char *auth_info)
319 {
320         int cursor;
321         GVariant *tmp_value;
322         char *manufacturer_data = NULL;
323         int manufacturer_data_len;
324         gboolean is_alias_set;
325         GVariantIter *value_iter;
326         guint8 m_value;
327         int i = 0;
328
329         tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
330                                                                 G_VARIANT_TYPE_BOOLEAN);
331         if (tmp_value) {
332                 is_alias_set = g_variant_get_boolean(tmp_value);
333                 g_variant_unref(tmp_value);
334         } else {
335                 is_alias_set = FALSE;
336         }
337         if (is_alias_set == FALSE) {
338                 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerDataLen",
339                                                                 G_VARIANT_TYPE_UINT16);
340                 if (tmp_value) {
341                         manufacturer_data_len = g_variant_get_uint16(tmp_value);
342                         if (manufacturer_data_len >
343                                         MAX_MANUFACTURE_LEN) {
344                                 ERR("manufacturer_data_len is too long");
345                                 manufacturer_data_len = MAX_MANUFACTURE_LEN;
346                         }
347                         g_variant_unref(tmp_value);
348                 } else
349                         manufacturer_data_len = 0;
350
351                 tmp_value = g_variant_lookup_value(reply, "LagacyManufacturerData",
352                                                                 G_VARIANT_TYPE_ARRAY);
353                 if (tmp_value) {
354                         if ((manufacturer_data_len == 0) ||
355                                         manufacturer_data_len != g_variant_get_size(tmp_value)) {
356                                 ERR("manufacturer data length doesn't match");
357                                 manufacturer_data_len = 0;
358                                 manufacturer_data = NULL;
359                         } else {
360                                 manufacturer_data = g_malloc0(manufacturer_data_len);
361                                 g_variant_get(tmp_value, "ay", &value_iter);
362                                 while (g_variant_iter_loop(value_iter, "y", &m_value))
363                                         manufacturer_data[i++] = m_value;
364
365                                 g_variant_iter_free(value_iter);
366                         }
367                         g_variant_unref(tmp_value);
368                 } else {
369                         INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
370                         manufacturer_data_len = 0;
371                         manufacturer_data = NULL;
372                 }
373                 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
374                 if (manufacturer_data_len < 30) {
375                         g_free(manufacturer_data);
376                         return;
377                 }
378                 if (manufacturer_data[0] != 0x00 || manufacturer_data[1] != 0x75) {
379                         DBG("This is not a samsung specific manufaturer data");
380                         g_free(manufacturer_data);
381                         return;
382                 }
383
384                 /* 2  samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
385                 1 (discovery version) + 1 (associated service ID)
386                 2 (Proxamity and locality) + 2 (Device type and icon) */
387
388                 cursor = 10;
389
390                 memcpy(auth_info, &(manufacturer_data[cursor]), 5);
391         }
392          g_free(manufacturer_data);
393 }
394
395 static gboolean __bt_hal_pincode_request(GapAgentPrivate *agent, GDBusProxy *device)
396 {
397         uint32_t device_class;
398         gchar *address = NULL;
399         unsigned char auth_info[5] = {0, };
400         gchar *name = NULL;
401         GVariant *reply = NULL;
402         GVariant *reply_temp = NULL;
403         GVariant *tmp_value = NULL;
404         DBG("+");
405
406         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
407
408         if (reply_temp == NULL) {
409                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
410                                 NULL);
411                 goto done;
412         }
413
414         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
415
416         tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
417         g_variant_get(tmp_value, "u", &device_class);
418         G_VARIANT_UNREF(tmp_value);
419
420         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
421         g_variant_get(tmp_value, "s", &address);
422         G_VARIANT_UNREF(tmp_value);
423
424         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
425         g_variant_get(tmp_value, "s", &name);
426         G_VARIANT_UNREF(tmp_value);
427
428         if (!name)
429                 name = g_strdup(address);
430
431         if (headed_plugin_info->plugin_headed_enabled) {
432                 __bt_hal_get_auth_info(reply, (char *)auth_info);
433                 if (__bt_hal_device_is_hid_keyboard(device_class)) {
434                         char str_passkey[BT_HAL_PASSKEY_MAX_LENGTH + 1] = { 0 };
435
436                         DBG("Device is HID Keyboard");
437                         if (__bt_hal_device_generate_passkey(str_passkey,
438                                                 BT_HAL_PASSKEY_MAX_LENGTH) != 0) {
439                                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT,
440                                                 "", NULL);
441                                 goto done;
442                         }
443
444                         gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT,
445                                         str_passkey, NULL);
446
447                         DBG("Launch BT Syspopup");
448                         headed_plugin_info->headed_plugin->bt_launch_system_popup(
449                                         BT_HAL_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST, name,
450                                         address, auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
451                 } else if (__bt_hal_device_is_auto_response(device_class, address, name)) {
452                         DBG("Device is of Type Auto response, send event to HAL");
453                         __bt_hal_send_pin_request_event(address, name, device_class);
454                 } else if (pincode_notification) {
455                         DBG("pincode_notification is enabled, send event to HAL");
456                         __bt_hal_send_pin_request_event(address, name, device_class);
457                 } else {
458                         DBG("Device is not of Auto response class, Show PIN Entry");
459                         headed_plugin_info->headed_plugin->bt_launch_system_popup(
460                                         BT_HAL_AGENT_EVENT_PIN_REQUEST, name, address,
461                                         auth_info, NULL, NULL, _gap_agent_get_path(agent));
462                 }
463         } else {
464                 INFO("Plugin Headed not Enabled");
465                 __bt_hal_send_pin_request_event(address, name, device_class);
466         }
467
468 done:
469         g_free(name);
470         g_free(address);
471         g_variant_unref(reply);
472         g_variant_unref(reply_temp);
473         __bt_hal_agent_release_memory();
474         DBG("-");
475
476         return TRUE;
477 }
478
479
480 /* BT_SSP_VARIANT_PASSKEY_CONFIRMATION */
481 /* BT_SSP_VARIANT_PASSKEY_NOTIFICATION */
482 /* BT_SSP_VARIANT_PASSKEY_ENTRY */
483 /* BT_SSP_VARIANT_CONSENT */
484
485 static void __bt_hal_send_ssp_request_events(const gchar *address,
486                 const gchar *name,
487                 guint passkey,
488                 uint32_t cod,
489                 unsigned char variant)
490 {
491         struct hal_ev_ssp_request ev;
492         memset(&ev, 0, sizeof(ev));
493         DBG("sizeof ev [%zu]", sizeof(ev));
494
495         DBG("Remote Device address [%s]", address);
496         DBG("Remote Device Name [%s]", name);
497         DBG("Remote Device passkey [%d]", passkey);
498         DBG("Remote Device pairing variant [0x%x]", variant);
499         DBG("Remote Device cod [%d]", cod);
500
501         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
502
503         memcpy(ev.name, name, strlen(name)+1);
504         ev.class_of_dev = cod;
505         ev.pairing_variant = variant;
506         ev.passkey = passkey;
507
508         handle_stack_msg event_cb = _bt_hal_get_stack_message_handler();
509         if (event_cb) {
510                 DBG("Sending SSP type [%d]", variant);
511                 event_cb(HAL_EV_SSP_REQUEST, (void*)&ev, sizeof(ev));
512         }
513
514         DBG("-");
515 }
516
517 /* SSP */
518 static gboolean __bt_hal_display_request(GapAgentPrivate *agent, GDBusProxy *device,
519                 guint passkey)
520 {
521         gchar *address = NULL;
522         gchar *name = NULL;
523         unsigned char auth_info[5] = {0, };
524         char *str_passkey = NULL;
525         uint32_t device_class;
526         GVariant *reply = NULL;
527         GVariant *reply_temp = NULL;
528         GVariant *tmp_value = NULL;
529         DBG("+");
530
531         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
532         if (reply_temp == NULL) {
533                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
534                                 NULL);
535                 goto done;
536         }
537
538         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
539
540         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
541         g_variant_get(tmp_value, "s", &address);
542         G_VARIANT_UNREF(tmp_value);
543         if (!address) {
544                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
545                 goto done;
546         }
547
548         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
549         g_variant_get(tmp_value, "s", &name);
550         G_VARIANT_UNREF(tmp_value);
551
552         tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
553         g_variant_get(tmp_value, "u", &device_class);
554         G_VARIANT_UNREF(tmp_value);
555
556         if (!name)
557                 name = g_strdup(address);
558
559         str_passkey = g_strdup_printf("%d", passkey);
560
561         __bt_hal_get_auth_info(reply, (char *)auth_info);
562
563         DBG("KEYBOARD_PASSKEY_REQUEST");
564
565         if (passkey_display_notification) {
566                 DBG("passkey_display_notification is enabled, send event to HAL");
567                 __bt_hal_send_ssp_request_events(address, name, passkey, device_class,
568                                 BT_SSP_VARIANT_PASSKEY_NOTIFICATION);
569         } else if (headed_plugin_info->plugin_headed_enabled) {
570                 INFO("Plugin Headed Enabled");
571                 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_KEYBOARD_PASSKEY_REQUEST,
572                                 name, address, auth_info, str_passkey, NULL, _gap_agent_get_path(agent));
573         } else {
574                 INFO("Plugin Headed not Enabled");
575                 __bt_hal_send_ssp_request_events(address, name, passkey, device_class,
576                                 BT_SSP_VARIANT_PASSKEY_NOTIFICATION);
577         }
578
579         g_free(str_passkey);
580 done:
581         g_free(name);
582         g_free(address);
583         g_variant_unref(reply);
584         g_variant_unref(reply_temp);
585         __bt_hal_agent_release_memory();
586
587         DBG("-");
588         return TRUE;
589 }
590
591 /* SSP */
592 static gboolean __bt_hal_passkey_request(GapAgentPrivate *agent, GDBusProxy *device)
593 {
594         gchar *address = NULL;
595         gchar *name = NULL;
596         unsigned char auth_info[5] = {0, };
597         uint32_t device_class;
598         GVariant *reply = NULL;
599         GVariant *reply_temp = NULL;
600         GVariant *tmp_value = NULL;
601         DBG("+");
602
603         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
604
605         if (reply_temp == NULL) {
606                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
607                                 NULL);
608                 goto done;
609         }
610
611         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
612
613         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
614         g_variant_get(tmp_value, "s", &address);
615         G_VARIANT_UNREF(tmp_value);
616         if (!address) {
617                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "", NULL);
618                 goto done;
619         }
620
621         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
622         g_variant_get(tmp_value, "s", &name);
623         G_VARIANT_UNREF(tmp_value);
624
625         tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
626         g_variant_get(tmp_value, "u", &device_class);
627         G_VARIANT_UNREF(tmp_value);
628
629         if (!name)
630                 name = g_strdup(address);
631
632         DBG("PASSKEY_REQUEST");
633
634         __bt_hal_get_auth_info(reply, (char *)auth_info);
635
636         if (headed_plugin_info->plugin_headed_enabled) {
637                 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_PASSKEY_REQUEST,
638                                 name, address, auth_info, NULL, NULL, _gap_agent_get_path(agent));
639         } else {
640                 __bt_hal_send_ssp_request_events(address, name, 0, device_class,
641                                 BT_SSP_VARIANT_PASSKEY_ENTRY);
642         }
643
644 done:
645         g_free(name);
646         g_free(address);
647         g_variant_unref(reply);
648         g_variant_unref(reply_temp);
649         __bt_hal_agent_release_memory();
650
651         DBG("-");
652         return TRUE;
653 }
654
655 /*ssp*/
656 static gboolean __bt_hal_confirm_request(GapAgentPrivate *agent, GDBusProxy *device,
657                 guint passkey)
658 {
659         gchar *address = NULL;
660         gchar *name = NULL;
661         char str_passkey[7];
662         uint32_t device_class;
663         GVariant *reply_temp = NULL;
664         GVariant *reply = NULL;
665         GVariant *tmp_value = NULL;
666         DBG("+ passkey[%.6d]", passkey);
667         DBG("Agent Path [%s]", agent->path);
668
669         snprintf(str_passkey, sizeof(str_passkey), "%.6d", passkey);
670
671         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
672
673         if (reply_temp == NULL) {
674                 ERR("####Device doesn't exist####");
675                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, "",
676                                 NULL);
677                 goto done;
678         }
679         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
680
681         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
682         g_variant_get(tmp_value, "s", &address);
683         G_VARIANT_UNREF(tmp_value);
684
685         tmp_value = g_variant_lookup_value(reply, "Name", G_VARIANT_TYPE_STRING);
686         g_variant_get(tmp_value, "s", &name);
687         G_VARIANT_UNREF(tmp_value);
688
689         tmp_value = g_variant_lookup_value(reply, "Class", G_VARIANT_TYPE_UINT32);
690         g_variant_get(tmp_value, "u", &device_class);
691         G_VARIANT_UNREF(tmp_value);
692
693         if (!name)
694                 name = g_strdup(address);
695
696         if (headed_plugin_info->plugin_headed_enabled) {
697                 unsigned char auth_info[5] = {0, };
698
699                 DBG("LAUNCH SYSPOPUP");
700                 DBG("Name [%s]", name);
701                 DBG("Passkey [%s]", str_passkey);
702
703                 __bt_hal_get_auth_info(reply, (char *)auth_info);
704                 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_HAL_AGENT_EVENT_PASSKEY_CONFIRM_REQUEST,
705                                 name, address, auth_info, str_passkey, NULL,
706                                 _gap_agent_get_path(agent));
707         } else {
708                 DBG("Headless Confirmation");
709
710                 if (TIZEN_FEATURE_BLUEZ_SPEAKER_REFERENCE) {
711                         /* Reply the ssp confirmation automaticaly */
712                         gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
713                 }
714
715                 __bt_hal_send_ssp_request_events(address, name, passkey,
716                                 device_class, BT_SSP_VARIANT_PASSKEY_CONFIRMATION);
717         }
718
719 done:
720         g_free(name);
721         g_free(address);
722         g_variant_unref(reply);
723         g_variant_unref(reply_temp);
724         __bt_hal_agent_release_memory();
725         DBG("-");
726         return TRUE;
727 }
728
729 static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *device,
730                 const char *uuid)
731 {
732         gchar *address = NULL;
733         gchar *name = NULL;
734         gboolean trust;
735         gboolean paired;
736         GVariant *reply = NULL;
737         GVariant *reply_temp = NULL;
738         GVariant *tmp_value = NULL;
739 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
740         bt_hal_agent_osp_server_t *osp_server;
741 #endif
742
743         reply_temp = __bt_hal_service_getall(device, BT_HAL_DEVICE_INTERFACE);
744         if (reply_temp == NULL) {
745                 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
746                 goto done;
747         }
748
749         g_variant_get(reply_temp, "(@a{sv})", &reply); /* Format of reply a{sv}*/
750
751         tmp_value = g_variant_lookup_value(reply, "Address", G_VARIANT_TYPE_STRING);
752         g_variant_get(tmp_value, "s", &address);
753         G_VARIANT_UNREF(tmp_value);
754         if (!address) {
755                 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
756                 goto done;
757         }
758
759         tmp_value = g_variant_lookup_value(reply, "Alias", G_VARIANT_TYPE_STRING);
760         g_variant_get(tmp_value, "s", &name);
761         G_VARIANT_UNREF(tmp_value);
762         if (!name)
763                 name = g_strdup(address);
764
765         tmp_value = g_variant_lookup_value(reply, "Trusted", G_VARIANT_TYPE_BOOLEAN);
766         g_variant_get(tmp_value, "b", &trust);
767         G_VARIANT_UNREF(tmp_value);
768
769         tmp_value = g_variant_lookup_value(reply, "Paired", G_VARIANT_TYPE_BOOLEAN);
770         g_variant_get(tmp_value, "b", &paired);
771         G_VARIANT_UNREF(tmp_value);
772         if ((paired == FALSE) && (trust == FALSE)) {
773                 ERR("No paired & No trusted device");
774                 gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
775                 goto done;
776         }
777
778         INFO("Authorization request for device [%s] Service:[%s]\n", address + 12, uuid);
779
780         if (trust) {
781                 DBG("Trusted device, so authorize\n");
782                 gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
783                 goto done;
784         }
785
786         if (!strcasecmp(uuid, BT_HAL_HID_UUID)) {
787                 gboolean is_connectable = __bt_hal_is_hid_device_connectable();
788                 DBG("Automatically %s authorization for HID",
789                         is_connectable ? "accept" : "reject");
790                 if (is_connectable == TRUE)
791                         gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL);
792                 else
793                         gap_agent_reply_authorize(agent, GAP_AGENT_REJECT, NULL);
794                 goto done;
795         }
796
797         if (!strcasecmp(uuid, BT_HAL_HFP_AUDIO_GATEWAY_UUID) ||
798                         !strcasecmp(uuid, BT_HAL_HSP_AUDIO_GATEWAY_UUID) ||
799                         !strcasecmp(uuid, BT_HAL_HFP_HF_UUID) ||
800                         !strcasecmp(uuid, BT_HAL_HSP_HS_UUID) ||
801                         !strcasecmp(uuid, BT_HAL_A2DP_PROFILE_UUID) ||
802                         !strcasecmp(uuid, BT_HAL_HID_DEVICE_UUID) ||
803                         !strcasecmp(uuid, BT_HAL_SAP_UUID_OLD) ||
804                         !strcasecmp(uuid, BT_HAL_SAP_UUID_NEW) ||
805 #ifdef TIZEN_BT_HAL
806                         !strcasecmp(uuid, BT_HAL_IOTIVITY_UUID) ||
807 #endif
808                         !strcasecmp(uuid, BT_HAL_AVRCP_TARGET_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,
866                                         address, auth_info, NULL, NULL, _gap_agent_get_path(agent));
867         }
868
869 done:
870         g_free(name);
871         g_free(address);
872         g_variant_unref(reply);
873         g_variant_unref(reply_temp);
874         __bt_hal_agent_release_memory();
875
876         return TRUE;
877 }
878
879 static gboolean __bt_hal_authorize_cancel_request(GapAgentPrivate *agent,
880                                                 const char *address)
881 {
882         DBG("On Going Authorization is cancelled by remote [%s]", address);
883         gap_agent_reply_authorize(agent, GAP_AGENT_CANCEL, NULL);
884
885         if (headed_plugin_info->plugin_headed_enabled)
886                 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
887
888         __bt_hal_agent_release_memory();
889         return TRUE;
890 }
891
892 static gboolean __bt_hal_pairing_cancel_request(GapAgentPrivate *agent, const char *address)
893 {
894         DBG("On Going Pairing is cancelled by remote [%s]", address);
895
896         if (headed_plugin_info->plugin_headed_enabled)
897                 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
898
899         __bt_hal_agent_release_memory();
900         return TRUE;
901 }
902
903 static gboolean __bt_hal_device_is_hid_keyboard(unsigned int dev_class)
904 {
905         switch ((dev_class & 0x1f00) >> 8) {
906         case 0x05:
907                 switch ((dev_class & 0xc0) >> 6) {
908                 case 0x01:
909                         /* input-keyboard" */
910                         return TRUE;
911                 }
912                 break;
913         }
914
915         return FALSE;
916 }
917
918 static gboolean __bt_hal_is_hid_device_connectable(void)
919 {
920         GDBusProxy *proxy = NULL;
921         GVariant *reply = NULL;
922         GError *err = NULL;
923         gboolean connectable = FALSE;
924
925         proxy = _bt_hal_get_hid_agent_proxy();
926         if(proxy == NULL)
927                 return FALSE;
928
929         reply = g_dbus_proxy_call_sync(proxy, "IsHidConnectable", NULL,
930                                 G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &err);
931         if (reply == NULL) {
932                 ERR("Error returned in method call");
933                 if (err != NULL) {
934                         ERR("Error message = %s", err->message);
935                         g_error_free(err);
936                 }
937                 connectable = FALSE;
938         } else {
939                 g_variant_get(reply, "(b)", &connectable);
940                 g_variant_unref(reply);
941         }
942         g_object_unref(proxy);
943
944         INFO("HID Device is %s",
945                         connectable ? "Connectable" : "Non-connectable");
946
947         return connectable;
948 }
949
950 static int __bt_hal_device_generate_passkey(char *passkey, int size)
951 {
952         int i;
953         ssize_t len;
954         int random_fd;
955         unsigned int value = 0;
956
957         if (passkey == NULL)
958                 return -1;
959
960         if (size <= 0)
961                 return -1;
962
963         random_fd = open("/dev/urandom", O_RDONLY);
964
965         if (random_fd < 0)
966                 return -1;
967
968         for (i = 0; i < size; i++) {
969                 len = read(random_fd, &value, sizeof(value));
970                 if (len > 0)
971                         passkey[i] = '0' + (value % 10);
972         }
973
974         close(random_fd);
975
976         DBG("passkey: %s", passkey);
977
978         return 0;
979 }
980
981 static gboolean __bt_hal_find_device_by_address_exactname(char *buffer,
982                 const char *address)
983 {
984         char *pch;
985         char *last;
986
987         pch = strtok_r(buffer, "= ,", &last);
988
989         if (pch == NULL)
990                 return FALSE;
991
992         while ((pch = strtok_r(NULL, ",", &last))) {
993                 if (0 == g_strcmp0(pch, address)) {
994                         DBG("Match found\n");
995                         return TRUE;
996                 }
997         }
998         return FALSE;
999 }
1000
1001 static gboolean __bt_hal_find_device_by_partial_name(char *buffer,
1002                 const char *partial_name)
1003 {
1004         char *pch;
1005         char *last;
1006
1007         pch = strtok_r(buffer, "= ,", &last);
1008
1009         if (pch == NULL)
1010                 return FALSE;
1011
1012         while ((pch = strtok_r(NULL, ",", &last))) {
1013                 if (g_str_has_prefix(partial_name, pch)) {
1014                         DBG("Match found\n");
1015                         return TRUE;
1016                 }
1017         }
1018         return FALSE;
1019 }
1020
1021 static gboolean __bt_hal_device_is_device_blacklisted(const char *address,
1022                 const char *name)
1023 {
1024         char *buffer;
1025         char **lines;
1026         int i;
1027         FILE *fp;
1028         long size;
1029         size_t result;
1030
1031         DBG("+");
1032
1033         fp = fopen(BT_HAL_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
1034
1035         if (fp == NULL) {
1036                 ERR("Unable to open blacklist file");
1037                 return FALSE;
1038         }
1039
1040         fseek(fp, 0, SEEK_END);
1041         size = ftell(fp);
1042         if (size <= 0) {
1043                 DBG("size is not a positive number");
1044                 fclose(fp);
1045                 return FALSE;
1046         }
1047
1048         rewind(fp);
1049
1050         buffer = g_malloc0(sizeof(char) * size);
1051         /* Fix : NULL_RETURNS */
1052         if (buffer == NULL) {
1053                 ERR("Fail to allocate memory");
1054                 fclose(fp);
1055                 return FALSE;
1056         }
1057         result = fread((char *)buffer, 1, size, fp);
1058         fclose(fp);
1059         if (result != size) {
1060                 ERR("Read Error");
1061                 g_free(buffer);
1062                 return FALSE;
1063         }
1064
1065         lines = g_strsplit_set(buffer, BT_HAL_AGENT_NEW_LINE, 0);
1066         g_free(buffer);
1067
1068         if (lines == NULL) {
1069                 ERR("No lines in the file");
1070                 return FALSE;
1071         }
1072
1073         for (i = 0; lines[i] != NULL; i++) {
1074                 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
1075                         if (__bt_hal_find_device_by_address_exactname(
1076                                                 lines[i], address))
1077                                 goto done;
1078                 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
1079                         if (__bt_hal_find_device_by_address_exactname(
1080                                                 lines[i], name))
1081                                 goto done;
1082                 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
1083                         if (__bt_hal_find_device_by_partial_name(lines[i],
1084                                                 name))
1085                                 goto done;
1086                 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
1087                         if (__bt_hal_find_device_by_address_exactname(
1088                                                 lines[i], address))
1089                                 goto done;
1090         }
1091         g_strfreev(lines);
1092         DBG("-");
1093         return FALSE;
1094 done:
1095         DBG("Found the device");
1096         g_strfreev(lines);
1097         return TRUE;
1098 }
1099
1100 static gboolean __bt_hal_device_is_auto_response(uint32_t dev_class,
1101                 const gchar *address, const gchar *name)
1102 {
1103         gboolean is_headset = FALSE;
1104         gboolean is_mouse = FALSE;
1105         unsigned int adapter_class = 0;
1106         char lap_address[BT_HAL_LOWER_ADDRESS_LENGTH];
1107
1108         DBG("bt_agent_is_headset_class, %d +", dev_class);
1109
1110         if (address == NULL)
1111                 return FALSE;
1112
1113         _bt_hal_dbus_get_adapter_class(&adapter_class);
1114
1115         if (((adapter_class & 0x1f00) >> 8) == 0x05)
1116         {
1117                 DBG("Adapter is Peripheral, do not response automatically");
1118                 return FALSE;
1119         }
1120
1121         switch ((dev_class & 0x1f00) >> 8) {
1122         case 0x04:
1123                 switch ((dev_class & 0xfc) >> 2) {
1124                 case 0x01:
1125                 case 0x02:
1126                         /* Headset */
1127                         is_headset = TRUE;
1128                         break;
1129                 case 0x06:
1130                         /* Headphone */
1131                         is_headset = TRUE;
1132                         break;
1133                 case 0x0b:      /* VCR */
1134                 case 0x0c:      /* Video Camera */
1135                 case 0x0d:      /* Camcorder */
1136                         break;
1137                 default:
1138                         /* Other audio device */
1139                         is_headset = TRUE;
1140                         break;
1141                 }
1142                 break;
1143         case 0x05:
1144                 switch (dev_class & 0xff) {
1145                 case 0x80:  /* 0x80: Pointing device(Mouse) */
1146                         is_mouse = TRUE;
1147                         break;
1148
1149                 case 0x40: /* 0x40: input device (BT keyboard) */
1150
1151                         /* Get the LAP(Lower Address part) */
1152                         g_strlcpy(lap_address, address, sizeof(lap_address));
1153
1154                         /* Need to Auto pair the blacklisted Keyboard */
1155                         if (__bt_hal_device_is_device_blacklisted(lap_address, name) != TRUE) {
1156                                 DBG("Device is not black listed\n");
1157                                 return FALSE;
1158                         } else {
1159                                 ERR("Device is black listed\n");
1160                                 return TRUE;
1161                         }
1162                 }
1163         }
1164
1165         if ((!is_headset) && (!is_mouse))
1166                 return FALSE;
1167
1168         /* Get the LAP(Lower Address part) */
1169         g_strlcpy(lap_address, address, sizeof(lap_address));
1170
1171         DBG("Device address = %s\n", address);
1172         DBG("Address 3 byte = %s\n", lap_address);
1173
1174         if (__bt_hal_device_is_device_blacklisted(lap_address, name)) {
1175                 ERR("Device is black listed\n");
1176                 return FALSE;
1177         }
1178
1179         return TRUE;
1180 }
1181
1182 static GVariant *__bt_hal_service_getall(GDBusProxy *device, const char *interface)
1183 {
1184         GError *error = NULL;
1185         GVariant *reply;
1186
1187         reply = g_dbus_proxy_call_sync(device,
1188                         "GetAll", g_variant_new("(s)", interface),
1189                         G_DBUS_CALL_FLAGS_NONE, -1,
1190                         NULL, &error);
1191         if (reply == NULL) {
1192                 ERR("GetAll dBUS-RPC failed");
1193                 if (error) {
1194                         ERR("D-Bus API failure: errCode[%x], message[%s]",
1195                                         error->code, error->message);
1196                         g_clear_error(&error);
1197                 }
1198                 return NULL;
1199         }
1200
1201         return reply;
1202 }
1203
1204 static void __bt_hal_agent_release_memory(void)
1205 {
1206         /* Release Malloc Memory*/
1207         malloc_trim(0);
1208
1209         /* Release Stack Memory*/
1210         stack_trim();
1211 }
1212
1213 static inline void stack_trim(void)
1214 {
1215 #ifdef STACK_FLUSH
1216         unsigned int sp;
1217         char buf[BUF_SIZE];
1218         FILE *file;
1219         unsigned int stacktop;
1220         int found = 0;
1221
1222         asm volatile ("mov %0,sp " : "=r"(sp));
1223
1224         sprintf(buf, "/proc/%d/maps", getpid());
1225         file = fopen(buf, "r");
1226         while (fgets(buf, BUF_SIZE, file) != NULL) {
1227                 if (strstr(buf, "[stack]")) {
1228                         found = 1;
1229                         break;
1230                 }
1231         }
1232         fclose(file);
1233
1234         if (found) {
1235                 sscanf(buf, "%x-", &stacktop);
1236                 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp) - stacktop,
1237                                         MADV_DONTNEED) < 0)
1238                         perror("stack madvise fail");
1239         }
1240 #endif
1241 }