Enable the connection request event for custom obex server
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / device / bt-service-core-device.c
1 /*
2  * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Anupam Roy <anupam.r@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <gio/gio.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #include <vconf.h>
26 #include <vconf-internal-keys.h>
27 #include <bundle.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
30
31 /*bt-service headers */
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-util.h"
35 #include "bt-service-main.h"
36 #include "bt-service-core-device.h"
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-event-receiver.h"
39 #include "bt-request-handler.h"
40 #include "bt-service-event.h"
41 #include "bt-service-agent-util.h"
42 #include "bt-service-a2dp-src.h"
43 #include "bt-service-a2dp-sink.h"
44 #ifdef TIZEN_FEATURE_BT_OBEX
45 #include "bt-service-obex-server.h"
46 #endif
47 #include "bt-service-device-internal.h"
48
49 #ifdef TIZEN_GATT_CLIENT
50 #include "bt-service-gatt.h"
51 #endif
52
53 /* OAL headers */
54 #include <oal-event.h>
55 #include <oal-manager.h>
56 #include <oal-adapter-mgr.h>
57 #include <oal-device-mgr.h>
58
59 #define MAX_BOND_RETRY_COUNT 3
60 #define BT_PASSKEY_MAX_LENGTH 4
61
62 /* Bonding Info structure */
63 typedef struct {
64         int result;
65         char *addr;
66         gboolean is_autopair;
67         unsigned short conn_type;
68         gboolean is_cancelled_by_user;
69         gboolean is_device_creating;
70         bluetooth_device_address_t *dev_addr;
71         bt_remote_dev_info_t *dev_info;
72 } bt_bond_data_t;
73
74 /* Searching Info structure */
75 typedef struct {
76         int result;
77         char *addr;
78         gboolean is_cancelled_by_user;
79         bluetooth_device_address_t *dev_addr;
80         bt_remote_dev_info_t *dev_info;
81 } bt_service_search_info_data_t;
82
83 /* Pairing Info structure */
84 typedef struct {
85         char *addr;
86         gboolean is_autopair;
87         int is_ssp;
88 } bt_pairing_data_t;
89
90 typedef struct {
91         char addr[BT_ADDRESS_STRING_SIZE];
92         bt_remote_dev_info_t *dev_info;
93 } bt_incoming_bond_data_t;
94
95 /* Bonding and Pairing Informations */
96 bt_bond_data_t *trigger_bond_info;
97 bt_bond_data_t *trigger_unbond_info;
98 bt_pairing_data_t *trigger_pairing_info;
99 bt_service_search_info_data_t *service_search_info;
100
101 bt_incoming_bond_data_t *incoming_bond_info;
102
103 typedef enum {
104         BT_DEVICE_BOND_STATE_NONE,
105         BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
106         BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
107         BT_DEVICE_BOND_STATE_REMOVE_BONDING,
108         BT_DEVICE_BOND_STATE_REMOVED_BONDING,
109         BT_DEVICE_BOND_STATE_STARTED,
110         BT_DEVICE_BOND_STATE_WAIT_PROP,
111         BT_DEVICE_BOND_STATE_WAIT_DID
112 } bt_bond_state_e;
113
114 typedef enum {
115         BT_DEVICE_BOND_INFO,
116         BT_DEVICE_INCOMING_BOND_INFO,
117         BT_DEVICE_UNBOND_INFO
118 } bt_bond_info_e;
119
120 /* BT device bond state variable */
121 static bt_bond_state_e bt_device_bond_state;
122 static int bond_retry_count;
123
124 static char *passkey_watcher;
125 static GSList *pin_info_list = NULL;
126
127 #ifdef TIZEN_GATT_CLIENT
128 typedef struct {
129         char *address;
130         float interval_min;
131         float interval_max;
132         GSList *senders;
133 } bt_connected_le_dev_t;
134
135 typedef struct {
136         char *sender;
137         float interval_min;
138         float interval_max;
139         guint16 latency;
140         guint16 time_out;
141         float key;
142 } bt_le_conn_param_t;
143
144 static GSList *le_connected_dev_list = NULL;
145
146 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
147 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
148 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
149 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
150 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
151 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
152 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
153
154 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
155 #endif
156
157 /* Forward declaration */
158 static void __bt_device_event_handler(int event_type, gpointer event_data);
159 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
160
161
162 static int __bt_device_handle_bond_state(void);
163 static void __bt_free_bond_info(uint8_t type);
164 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
165 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
166 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
167 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
168 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
169 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
170                 gboolean connected, unsigned char type);
171 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
172
173 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
174 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
175 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
176 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
177 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
178 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
179
180 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
181 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
182
183 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
184
185 static int __bt_get_device_pin_code(const char *address, char *pin_code);
186
187 gboolean _bt_is_device_creating(void)
188 {
189         if (!trigger_bond_info)
190                 return FALSE;
191         return trigger_bond_info->is_device_creating;
192 }
193
194 void _bt_device_state_handle_callback_set_request(void)
195 {
196         _bt_service_register_event_handler_callback(
197                         BT_DEVICE_MODULE, __bt_device_event_handler);
198 }
199
200 void __bt_device_handle_pending_requests(int result, int service_function,
201                 void *user_data, unsigned int size)
202 {
203         GSList *l;
204         GArray *out_param;
205         invocation_info_t *req_info = NULL;
206
207         BT_DBG("+");
208
209         /* Get method invocation context */
210         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
211                 req_info = l->data;
212                 if (req_info == NULL || req_info->service_function != service_function)
213                         continue;
214
215                 switch (service_function) {
216                 case BT_SEARCH_SERVICE: {
217                         char *address = (char *)user_data;
218                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
219                                 BT_ERR("Unexpected: Info request pending for a different address!!");
220                                 return;
221                         } else {
222                                 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
223                                 bt_sdp_info_t sdp_info;
224
225                                 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
226                                 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
227
228                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
229                                 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
230                                 _bt_service_method_return(req_info->context, out_param, result);
231
232                                 g_free(req_info->user_data);
233                                 _bt_free_info_from_invocation_list(req_info);
234                                 g_array_free(out_param, TRUE);
235                         }
236                         break;
237                 }
238                 case BT_BOND_DEVICE: {
239                         char *address = (char *)user_data;
240                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
241                                 BT_ERR("Unexpected: Info request pending for a different address!!");
242                                 return;
243                         } else {
244                                 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
245                                 bluetooth_device_info_t dev_info;
246                                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
247                                 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
248                                                 address);
249                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
250                                 g_array_append_vals(out_param, &dev_info,
251                                                 sizeof(bluetooth_device_info_t));
252                                 _bt_service_method_return(req_info->context, out_param, result);
253
254                                 g_free(req_info->user_data);
255                                 _bt_free_info_from_invocation_list(req_info);
256                                 g_array_free(out_param, TRUE);
257                         }
258                         break;
259                 }
260                 case BT_UNBOND_DEVICE: {
261                         char *address = (char *)user_data;
262                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
263                                 BT_ERR("Unexpected: Info request pending for a different address!!");
264                                 return;
265                         } else {
266                                 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
267                                 bluetooth_device_address_t dev_addr;
268                                 _bt_convert_addr_string_to_type(dev_addr.addr, address);
269                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
270                                 g_array_append_vals(out_param, &dev_addr,
271                                                 sizeof(bluetooth_device_address_t));
272                                 _bt_service_method_return(req_info->context, out_param, result);
273
274                                 g_free(req_info->user_data);
275                                 _bt_free_info_from_invocation_list(req_info);
276                                 g_array_free(out_param, TRUE);
277                         }
278                         break;
279                 }
280                 default:
281                         BT_ERR("Unhandled case");
282                         break;
283                 }
284         }
285         BT_INFO("-");
286 }
287
288 /*
289  * Remote device properties are received on all following conditions
290  * a. When Bonding in on-going
291  * b. When device properties are updated\changed for a connected device
292  *    (due to SDP or any other reason)
293  */
294 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
295 {
296         bt_remote_dev_info_t *rem_info = NULL;
297
298         BT_DBG("+");
299         rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
300         _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
301
302         if (oal_dev_props->adv_len > 0) {
303                 int k;
304
305                 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
306                 rem_info->manufacturer_data =
307                         g_memdup(oal_dev_props->adv_data,
308                                         oal_dev_props->adv_len);
309                 BT_DBG("----Advertising Data Length: %d",
310                                 rem_info->manufacturer_data_len);
311
312                 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
313                         BT_INFO("Check data[%d] = [[0x%x]",
314                                         k, oal_dev_props->adv_data[k]);
315                 }
316         } else {
317                 rem_info->manufacturer_data = NULL;
318                 rem_info->manufacturer_data_len = 0;
319         }
320
321         /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
322         if (trigger_bond_info  && !strcmp(trigger_bond_info->addr, rem_info->address)) {
323                 BT_INFO("Bonding is ongoing, try update properties");
324                 if (!trigger_bond_info->dev_info ||
325                                 (!trigger_bond_info->dev_info->name &&
326                                  !trigger_bond_info->dev_info->alias) ||
327                                         !trigger_bond_info->dev_info->address ||
328                                                 trigger_bond_info->dev_info->uuid_count == 0) {
329                         BT_INFO("Complete data is not present, Assigning rem_info");
330                         if (!trigger_bond_info->dev_info)
331                                 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
332                         _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
333                 }
334
335                 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
336                 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
337         } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
338                 BT_INFO("Incoming Bond is ongoing, try update properties");
339                 if (!incoming_bond_info->dev_info ||
340                                 (!incoming_bond_info->dev_info->name &&
341                                  !incoming_bond_info->dev_info->alias) ||
342                                         !incoming_bond_info->dev_info->address ||
343                                                 incoming_bond_info->dev_info->uuid_count == 0) {
344                         BT_INFO("Complete data is not present, Assigning rem_info");
345                         if (!incoming_bond_info->dev_info)
346                                 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
347                         _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
348                 }
349
350                 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
351                 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
352         }
353
354         /* Add device to bonded list */
355         _bt_service_add_device_to_bonded_list(rem_info);
356
357         /* Handle SDP Device properties update */
358         if (service_search_info && service_search_info->dev_info) {
359                 if (!strcmp(service_search_info->addr, rem_info->address)) {
360                         BT_DBG("Properties received and SDP request pending, fill device properties and send event");
361                         service_search_info->dev_info->class = rem_info->class;
362                         service_search_info->dev_info->paired = rem_info->paired;
363                         service_search_info->dev_info->connected = rem_info->connected;
364                         service_search_info->dev_info->rssi = rem_info->rssi;
365                         service_search_info->dev_info->addr_type = rem_info->addr_type;
366                         service_search_info->dev_info->trust = rem_info->trust;
367
368                         /* TODO*/
369                         service_search_info->dev_info->manufacturer_data = NULL;
370                         service_search_info->dev_info->manufacturer_data_len = 0;
371
372                         __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
373                 }
374         }
375
376         _bt_free_remote_dev(rem_info);
377         BT_DBG("-");
378 }
379
380 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
381 {
382         switch (profile) {
383         case TRUSTED_PROFILE_PBAP:
384                 return OAL_TRUSTED_PROFILE_PBAP;
385         case TRUSTED_PROFILE_MAP:
386                 return OAL_TRUSTED_PROFILE_MAP;
387         case TRUSTED_PROFILE_SAP:
388                 return OAL_TRUSTED_PROFILE_SAP;
389         case TRUSTED_PROFILE_HFP_HF:
390                 return OAL_TRUSTED_PROFILE_HFP_HF;
391         case TRUSTED_PROFILE_A2DP:
392                 return OAL_TRUSTED_PROFILE_A2DP;
393         case TRUSTED_PROFILE_ALL:
394                 return OAL_TRUSTED_PROFILE_ALL;
395         default:
396                 return 0;
397         }
398 }
399
400 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
401                 bluetooth_trusted_profile_t profile, gboolean trust)
402 {
403         int result;
404         bt_address_t bd_addr;
405         oal_trusted_profile_e oal_profile;
406
407         BT_DBG("+");
408
409         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
410
411         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
412         oal_profile = __get_oal_trusted_profile(profile);
413         retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
414
415         result = device_set_trust_profile(&bd_addr, oal_profile, trust);
416         if (result != OAL_STATUS_SUCCESS) {
417                 BT_ERR("device_set_trust_profile error: [%d]", result);
418                 return BLUETOOTH_ERROR_INTERNAL;
419         }
420
421         BT_DBG("-");
422         return BLUETOOTH_ERROR_NONE;
423 }
424
425 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
426                 bluetooth_trusted_profile_t profile, guint *trust)
427 {
428         int result;
429         bt_address_t bd_addr;
430         oal_trusted_profile_e oal_profile;
431
432         BT_DBG("+");
433
434         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
435
436         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
437         oal_profile = __get_oal_trusted_profile(profile);
438         retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
439
440         result = device_get_trust_profile(&bd_addr, oal_profile, trust);
441         if (result != OAL_STATUS_SUCCESS) {
442                 BT_ERR("device_set_trust_profile error: [%d]", result);
443                 return BLUETOOTH_ERROR_INTERNAL;
444         }
445
446         BT_DBG("-");
447         return BLUETOOTH_ERROR_NONE;
448 }
449
450 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
451 {
452         GVariant *param = NULL;
453         GVariant *uuids = NULL;
454         GVariantBuilder *builder = NULL;
455         GVariant *manufacturer_data;
456         unsigned int i = 0;
457         char *name = NULL;
458
459         BT_INFO("Send Service Search request event");
460
461         if (remote_dev_info->alias)
462                 name = remote_dev_info->alias;
463         else
464                 name = remote_dev_info->name;
465
466         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
467         for (i = 0; i < remote_dev_info->uuid_count; i++) {
468                 g_variant_builder_add(builder, "s",
469                                 remote_dev_info->uuids[i]);
470         }
471         uuids = g_variant_new("as", builder);
472         g_variant_builder_unref(builder);
473         manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
474                         remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
475                         TRUE, NULL, NULL);
476
477         param = g_variant_new("(isunsbub@asn@ay)",
478                         BLUETOOTH_ERROR_NONE,
479                         remote_dev_info->address,
480                         remote_dev_info->class,
481                         remote_dev_info->rssi,
482                         name,
483                         remote_dev_info->paired,
484                         remote_dev_info->connected,
485                         remote_dev_info->trust,
486                         uuids,
487                         remote_dev_info->manufacturer_data_len,
488                         manufacturer_data);
489         /* Send the event to application */
490         _bt_send_event(BT_ADAPTER_EVENT,
491                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
492                         param);
493
494         __bt_free_service_search_info(&service_search_info);
495         BT_DBG("-");
496 }
497
498 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
499 {
500         bt_remote_dev_info_t *rem_info = NULL;
501         unsigned int i;
502         BT_DBG("+");
503
504         if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
505                                 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
506                 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
507
508                 BT_DBG("Bonding dev addr has matched");
509                 /* Bonding ongoing, Query device properties again */
510                 if (BLUETOOTH_ERROR_NONE ==
511                         _bt_device_get_bonded_device_info(dev_addr))
512                         BT_DBG("_bt_device_get_bonded_device_info success");
513                 else
514                         BT_ERR("_bt_device_get_bonded_device_info failed");
515         }
516
517         if (service_search_info == NULL) {
518                 /* Send reply */
519                 BT_DBG("searching_info == NULL");
520                 return;
521         }
522
523         if (_bt_compare_adddress(service_search_info->dev_addr,
524                                 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
525                 BT_DBG("This device is not queried");
526                 return;
527         }
528
529         rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
530         memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
531
532         rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
533         _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
534
535         rem_info->uuid_count = uuid_list->num;
536
537         BT_INFO("Address [%s]", rem_info->address);
538         BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
539         if (rem_info->uuid_count > 0)
540                 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
541
542         /* Fill Remote Device Service List list */
543         for (i = 0; i < rem_info->uuid_count; i++) {
544                 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
545                 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
546                 BT_DBG("UUID value=%s", rem_info->uuids[i]);
547         }
548
549         /* Update local cache */
550         _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
551
552         BT_DBG("DBUS return");
553         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
554                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
555
556         /* Save UUID List of remote devices */
557         if (service_search_info->dev_info)
558                 _bt_free_remote_dev(service_search_info->dev_info);
559         service_search_info->dev_info = rem_info;
560
561         /* Query Other device properties */
562         if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
563                 BT_DBG("Bonded device info query posted to stack successfully");
564         } else {
565                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
566                 __bt_free_service_search_info(&service_search_info);
567         }
568         BT_DBG("-");
569 }
570
571 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
572 {
573         oal_service_t service_d = auth_event->service_id;
574         gchar address[BT_ADDRESS_STR_LEN];
575         int res;
576
577         _bt_convert_addr_type_to_string(address, auth_event->address.addr);
578
579         BT_INFO("service_d: %d", service_d);
580
581         switch (service_d) {
582         case HID_SERVICE_ID:
583                 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
584                 break;
585         case A2DP_SERVICE_ID:
586                 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
587                 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
588                 return;
589         case A2DP_SRC_SERVICE_ID:
590                 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
591                 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
592                 break;
593         case AVRCP_SERVICE_ID:
594                 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
595                 break;
596         case AVRCP_CT_SERVICE_ID:
597                 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
598                 break;
599 #ifdef TIZEN_FEATURE_BT_OBEX
600         case OPP_SERVICE_ID: {
601                 GVariant *param = NULL;
602                 char *name = NULL;
603
604                 if (_bt_obex_server_is_custom() == false) {
605                         /* Allow the connection for native OPP server */
606                         break;
607                 }
608
609                 name = g_strdup(address);
610
611                 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
612                 _bt_obex_server_set_pending_conn_auth_device_addr(address);
613                 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
614                 _bt_send_event(BT_OPP_SERVER_EVENT,
615                                 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
616                 g_free(name);
617                 return;
618         }
619 #endif
620         case HSP_SERVICE_ID:
621                 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
622                 break;
623         case HFP_SERVICE_ID:
624                 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
625                 break;
626         case SAP_SERVICE_ID:
627                 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
628                 break;
629         case HSP_HS_SERVICE_ID:
630                 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
631                 break;
632         case HFP_HS_SERVICE_ID:
633                 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
634                 break;
635 #ifdef TIZEN_BT_HAL
636         case IOTIVITY_SERVICE_ID:
637                 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
638                 break;
639 #endif
640         default:
641                 /* For now, reject authorization for any service apart from above switch cases */
642                 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
643                 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
644                 if (res != OAL_STATUS_SUCCESS)
645                         BT_ERR("authorize_response: %d", res);
646                 return;
647         }
648
649         /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
650         BT_INFO("Auto Accept authorization");
651         res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
652         if (res != OAL_STATUS_SUCCESS)
653                 BT_ERR("authorize_response: %d", res);
654         BT_INFO("-");
655 }
656
657 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
658 {
659         GVariant *param = NULL;
660         BT_DBG("+");
661
662         if ((remote_dev_info->name || remote_dev_info->alias)
663                         && remote_dev_info->address
664                         && remote_dev_info->uuids) {
665                 BT_INFO("All properties updated,  time to send bonding finished event");
666                 GVariant *uuids = NULL;
667                 GVariantBuilder *builder = NULL;
668                 GVariant *manufacturer_data;
669                 unsigned int i = 0;
670                 char *name = NULL;
671
672                 if (remote_dev_info->alias)
673                         name = remote_dev_info->alias;
674                 else
675                         name = remote_dev_info->name;
676
677                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
678                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
679                         g_variant_builder_add(builder, "s",
680                                         remote_dev_info->uuids[i]);
681                 }
682                 uuids = g_variant_new("as", builder);
683                 g_variant_builder_unref(builder);
684                 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
685                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
686                                 TRUE, NULL, NULL);
687
688                 param = g_variant_new("(isunsbub@asn@ay)",
689                                 BLUETOOTH_ERROR_NONE,
690                                 remote_dev_info->address,
691                                 remote_dev_info->class,
692                                 remote_dev_info->rssi,
693                                 name,
694                                 remote_dev_info->paired,
695                                 remote_dev_info->connected,
696                                 remote_dev_info->trust,
697                                 uuids,
698                                 remote_dev_info->manufacturer_data_len,
699                                 manufacturer_data);
700                 /* Send the event to application */
701                 _bt_send_event(BT_ADAPTER_EVENT,
702                                 BLUETOOTH_EVENT_BONDING_FINISHED,
703                                 param);
704                 if (incoming_bond) {
705                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
706                 } else {
707                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
708                         __bt_free_pairing_info(&trigger_pairing_info);
709                 }
710         } else {
711                 BT_INFO("Lets wait for more remote device properties");
712         }
713 }
714
715 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
716 {
717         char address[BT_ADDRESS_STRING_SIZE];
718         bluetooth_device_address_t dev_addr;
719
720         BT_INFO("+");
721
722         /*
723          * BlueZ sends paired signal for each paired device, during activation,
724          * We should ignore this, otherwise application thinks that a new device
725          * got paired
726          */
727         if (_bt_adapter_get_status() != BT_ACTIVATED) {
728                 BT_DBG("BT is not activated, so ignore this");
729                 return;
730         }
731
732         _bt_convert_addr_type_to_string(address, bd_addr->addr);
733         if (!incoming_bond_info) {
734                 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
735         } else {
736                 if (g_strcmp0(incoming_bond_info->addr, address)) {
737                         BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
738                                         address, incoming_bond_info->addr);
739                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
740                         incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
741                 }
742         }
743
744         BT_INFO("Incoming bond successfully completed");
745         g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
746         incoming_bond_info->dev_info = NULL;
747
748         _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
749         if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
750                 BT_DBG("Bonded device info query posted to stack successfully");
751         } else {
752                 BT_ERR("Bonded device info query failed");
753                 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
754         }
755
756         BT_INFO("-");
757 }
758
759 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
760 {
761         gchar address[BT_ADDRESS_STR_LEN];
762         bluetooth_device_address_t dev_addr;
763
764         BT_INFO("+");
765
766         if (trigger_bond_info == NULL) {
767                 /* Send reply */
768                 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
769                 __handle_incoming_bond_created_event(bd_addr);
770                 return;
771         }
772
773         _bt_convert_addr_type_to_string(address, bd_addr->addr);
774         if (g_strcmp0(trigger_bond_info->addr, address)) {
775                 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
776                                 address, trigger_bond_info->addr);
777                 __handle_incoming_bond_created_event(bd_addr);
778                 return;
779         }
780
781         BT_INFO("Bonding successfully completed");
782         /* Bonding state will be cleaned up & BONDING FINISHED EVENT
783            will be sent only when Properties are fetched from stack
784            Till that time lets not free trigger_bond_info.
785            However it is possible that while fetching device properties, internal
786            stack error can occur which can lead to no valid properties or
787            no properties at all. So in such cases, we must not wait for properties,
788            otherwise, it can lead to infinite wait  */
789         _bt_convert_addr_string_to_type(dev_addr.addr,
790                         trigger_bond_info->addr);
791
792         if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
793                 BT_DBG("BOnded device info query posted to stack successfully");
794                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
795                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
796         } else {
797                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
798                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
799                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
800                 /* Destroy if at all device got bonded at stack level */
801                 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
802
803                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
804                 __bt_free_pairing_info(&trigger_pairing_info);
805         }
806
807         BT_INFO("-");
808 }
809
810 /**********************************************************************************************
811 *  Bond removal event can be triggered for following reasons -
812 *  a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
813 *  b. If Application requests for explicitly removing the bond
814 *  c. When application attempt to create bond,bond is removed first which triggers this event
815 *     c. is in-line with Bluedroid bond create\emoval architecture
816 *********************************************************************************************/
817 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
818 {
819         char address[BT_ADDRESS_STRING_SIZE];
820
821         BT_INFO("+");
822
823         _bt_convert_addr_type_to_string(address, bd_addr->addr);
824         _bt_service_remove_device_from_bonded_list(address);
825
826         if (trigger_unbond_info) {
827                 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
828                 GVariant *param = NULL;
829                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
830                                 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
831                 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
832                 _bt_send_event(BT_ADAPTER_EVENT,
833                                 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
834                                 param);
835                 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
836                 __bt_free_pairing_info(&trigger_pairing_info);
837         } else if (trigger_bond_info) {
838                 BT_ERR("Bonding was removed");
839                 __bt_device_handle_bond_state();
840         }
841         BT_INFO("-");
842 }
843
844 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
845 {
846         BT_INFO("+");
847         oal_status_t status = bond_fail_event->status;
848         BT_INFO("Bonding failed, reason: %d", status);
849
850         switch (status) {
851         case OAL_STATUS_RMT_DEVICE_DOWN:
852         {
853                 if (trigger_bond_info) {
854                         BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
855                                         bond_retry_count);
856                         int ret = OAL_STATUS_SUCCESS;
857                         if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
858                                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
859                                 if (ret != OAL_STATUS_SUCCESS) {
860                                         BT_ERR("Create Bond procedure could not suceed");
861                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
862                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
863                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
864                                         __bt_free_pairing_info(&trigger_pairing_info);
865                                         bond_retry_count = 0;
866                                 } else
867                                         bond_retry_count++;
868                         } else {
869                                 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
870                                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
871                                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
872                                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
873                                 __bt_free_pairing_info(&trigger_pairing_info);
874                                 bond_retry_count = 0;
875                         }
876                 }
877                 break;
878         }
879         case OAL_STATUS_AUTH_FAILED:
880         {
881                 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
882                   To be considered later*/
883                 int result = BLUETOOTH_ERROR_INTERNAL;
884                 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
885                 if (trigger_bond_info) {
886                         BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
887                         if (trigger_bond_info->is_cancelled_by_user) {
888                                 BT_ERR("Bonding is cancelled by user");
889                                 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
890                         }
891                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
892                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
893                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
894                 }
895
896                 __bt_free_pairing_info(&trigger_pairing_info);
897                 break;
898         }
899         case OAL_STATUS_INTERNAL_ERROR:
900         {
901                 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
902                 if (trigger_unbond_info) {
903                         BT_INFO("Bond removal request failed, return DBUS and send event");
904                         GVariant *param = NULL;
905                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
906                                         trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
907                         param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
908                         _bt_send_event(BT_ADAPTER_EVENT,
909                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
910                                         param);
911                         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
912                 } else if (trigger_bond_info) {
913                         if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
914                                 if (trigger_bond_info) {
915                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
916                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
917                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
918                                 }
919                         }
920                 }
921
922                 __bt_free_pairing_info(&trigger_pairing_info);
923                 break;
924         }
925         default:
926         {
927                 BT_ERR("Unknown status of Bond failed event status [%d]", status);
928                 break;
929         }
930
931         }
932         BT_INFO("-");
933 }
934
935 static void __bt_device_event_handler(int event_type, gpointer event_data)
936 {
937         int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
938         BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
939
940         switch (event_type) {
941         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
942                 BT_INFO("BREDR Device Found");
943                 __bt_device_remote_device_found_callback(event_data, FALSE);
944                 break;
945         }
946         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
947                 BT_INFO("Dual Device Found");
948                 __bt_device_remote_device_found_callback(event_data, FALSE);
949                 break;
950         }
951         case OAL_EVENT_DEVICE_PROPERTIES: {
952                 BT_INFO("Remote Device properties Received");
953                 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
954                 break;
955         }
956         case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
957                 BT_INFO("Bonding Success event Received");
958                 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
959                 break;
960         }
961         case OAL_EVENT_DEVICE_BONDING_REMOVED: {
962                 BT_INFO("Bonding Removed event Received");
963                 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
964                 break;
965         }
966         case OAL_EVENT_DEVICE_BONDING_FAILED: {
967                 BT_INFO("Bonding Failed event Received");
968                 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
969                 break;
970         }
971         case OAL_EVENT_DEVICE_ACL_CONNECTED: {
972                 BT_INFO("ACL Connected event Received");
973                 event_dev_conn_status_t* param = event_data;
974                 __bt_device_conn_state_changed_callback(param, TRUE, 0);
975                 break;
976         }
977         case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
978                 BT_INFO("ACL Disconnected event Received");
979                 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
980                 break;
981         }
982         case OAL_EVENT_DEVICE_LE_CONNECTED: {
983                 BT_INFO("LE Connected event Received");
984                 event_dev_conn_status_t* param = event_data;
985                 __bt_device_conn_state_changed_callback(param, TRUE, 1);
986                 break;
987         }
988         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
989                 BT_INFO("LE Disconnected event Received");
990                 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
991                 break;
992         }
993         case OAL_EVENT_DEVICE_PIN_REQUEST: {
994                 BT_INFO("PIN Request Received");
995                 __bt_device_pin_request_callback((remote_device_t*)event_data);
996                 break;
997         }
998         case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
999                 BT_INFO("Passkey Entry request Received");
1000                 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1001                 break;
1002         }
1003         case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1004                 BT_INFO("Passkey Confirmation Request Received");
1005                 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1006                 break;
1007         }
1008         case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1009                 BT_INFO("Passkey Display Request Received");
1010                 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1011                 break;
1012         }
1013         case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1014                 BT_INFO("SSP Consent Request Received");
1015                  __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1016                 break;
1017         }
1018         case OAL_EVENT_DEVICE_SERVICES: {
1019                 BT_INFO("Remote Device Services Received");
1020                 __bt_device_services_callback((event_dev_services_t*)event_data);
1021                 break;
1022         }
1023         case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1024                 BT_INFO("Remote Device Authorization Request");
1025                 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1026                 break;
1027         }
1028         case OAL_EVENT_DEVICE_TRUSTED: {
1029                 BT_INFO("Remote Device Trusted");
1030                 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1031                 break;
1032         }
1033         case OAL_EVENT_DEVICE_UNTRUSTED: {
1034                 BT_INFO("Remote Device UnTrusted");
1035                 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1036                 break;
1037         }
1038         case OAL_EVENT_DEVICE_NAME: {
1039                 remote_device_t *rem_dev = event_data;
1040                 gchar address[BT_ADDRESS_STR_LEN];
1041
1042                 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1043                 BT_INFO("Remote Device name Received");
1044                 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1045
1046                 /* Update local cache */
1047                 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1048                 break;
1049         }
1050         case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1051                 event_device_trusted_profiles_t *ev = event_data;
1052                 char address[BT_ADDRESS_STRING_SIZE];
1053
1054                 ret_if(NULL == ev);
1055
1056                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1057                 _bt_send_event(BT_DEVICE_EVENT,
1058                                 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1059                                 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1060                                         address, ev->trust_val));
1061                 break;
1062         }
1063         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1064                 event_dev_rssi_info_t *ev = event_data;
1065                 char address[BT_ADDRESS_STRING_SIZE];
1066                 GVariant *param;
1067
1068                 ret_if(NULL == ev);
1069
1070                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1071                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1072                                 address, ev->link_type, TRUE);
1073                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1074                 break;
1075         }
1076         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1077                 event_dev_rssi_info_t *ev = event_data;
1078                 char address[BT_ADDRESS_STRING_SIZE];
1079                 GVariant *param;
1080
1081                 ret_if(NULL == ev);
1082
1083                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1084                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1085                                 address, ev->link_type, FALSE);
1086                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1087                 break;
1088         }
1089         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1090                 event_dev_rssi_info_t *ev = event_data;
1091                 char address[BT_ADDRESS_STRING_SIZE];
1092                 GVariant *param;
1093
1094                 ret_if(NULL == ev);
1095
1096                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1097                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1098                                 address, ev->link_type, ev->alert_type, ev->rssi);
1099                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1100                 break;
1101         }
1102         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1103                 event_dev_rssi_info_t *ev = event_data;
1104                 char address[BT_ADDRESS_STRING_SIZE];
1105                 GVariant *param;
1106
1107                 ret_if(NULL == ev);
1108
1109                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1110                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1111                                 address, ev->link_type, ev->rssi);
1112                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1113                 break;
1114         }
1115         default:
1116                 BT_INFO("Unhandled event..");
1117         }
1118 }
1119
1120 /* Legacy Pairing event handler */
1121 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1122 {
1123         GVariant *param;
1124         char address[BT_ADDRESS_STRING_SIZE];
1125         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1126         BT_DBG("+");
1127
1128         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1129
1130         BT_INFO("Address[%s]", address);
1131         BT_INFO("Name[%s]", pin_req_event->name);
1132         BT_INFO("COD[%d]", pin_req_event->cod);
1133
1134         if (trigger_pairing_info) {
1135                 /* BTAPI support only one pairing at a time */
1136                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1137                 BT_ERR("New PIN request address [%s]", address);
1138                 device_reject_pin_request(&pin_req_event->address);
1139                 return;
1140         }
1141
1142         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1143         if (_bt_is_bonding_device_address(address) == TRUE &&
1144                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1145                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1146                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1147                    to be checked and fixed apropriately */
1148                 _bt_set_autopair_status_in_bonding_info(TRUE);
1149                 device_accept_pin_request(&pin_req_event->address, "0000");
1150         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1151                 BT_DBG("Remote Device is HID keyboard Type..");
1152                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1153
1154                 if (_bt_agent_generate_passkey(str_passkey,
1155                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1156                         device_reject_pin_request(&pin_req_event->address);
1157                         goto done;
1158                 }
1159                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1160
1161                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1162                 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1163                 _bt_send_event(BT_ADAPTER_EVENT,
1164                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1165                 BT_DBG("Key board pairing in process");
1166         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1167                 BT_DBG("Use stored PIN code [%s]", pin_code);
1168                 device_accept_pin_request(&pin_req_event->address, pin_code);
1169         } else {
1170                 if (_bt_is_bonding_device_address(address) == TRUE) {
1171                         BT_DBG("Show Pin entry");
1172                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1173                         trigger_pairing_info->addr = g_strdup(address);
1174                         trigger_pairing_info->is_ssp = FALSE;
1175
1176                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1177                         param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1178                         _bt_send_event(BT_ADAPTER_EVENT,
1179                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1180                 }
1181         }
1182
1183 done:
1184         _bt_agent_release_memory();
1185         BT_DBG("-");
1186 }
1187
1188 /* SSP Pairing event handler */
1189 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1190 {
1191         GVariant *param;
1192         gchar address[BT_ADDRESS_STR_LEN];
1193         char *p_addr;
1194         gchar *name;
1195         int result = BLUETOOTH_ERROR_NONE;
1196         BT_DBG("+");
1197
1198         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1199         p_addr = address;
1200         name = dev_info->name;
1201
1202         BT_INFO("Address[%s]", address);
1203         BT_INFO("Name[%s]", name);
1204         BT_INFO("COD[%d]", dev_info->cod);
1205
1206         if (trigger_pairing_info) {
1207                 /* BTAPI support only one pairing at a time */
1208                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1209                 BT_ERR("New PIN request address [%s]", address);
1210                 device_reject_pin_request(&dev_info->address);
1211                 BT_DBG("-");
1212                 return;
1213         }
1214
1215         /* Set pairing data */
1216         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1217         trigger_pairing_info->addr = g_strdup(address);
1218         trigger_pairing_info->is_ssp = TRUE;
1219
1220         param = g_variant_new("(iss)", result, p_addr, name);
1221         _bt_send_event(BT_ADAPTER_EVENT,
1222                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1223         BT_DBG("-");
1224 }
1225
1226 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1227 {
1228         GVariant *param;
1229         gchar address[BT_ADDRESS_STR_LEN];
1230         char *p_addr;
1231         gchar *name;
1232         char str_passkey[7];
1233         int result = BLUETOOTH_ERROR_NONE;
1234         BT_DBG("+");
1235
1236         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1237         p_addr = address;
1238         name = dev_info->device_info.name;
1239
1240         BT_INFO("Address[%s]", address);
1241         BT_INFO("Name[%s]", name);
1242         BT_INFO("COD[%d]", dev_info->device_info.cod);
1243
1244         if (trigger_pairing_info) {
1245                 /* BTAPI support only one pairing at a time */
1246                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1247                 BT_ERR("New PIN request address [%s]", address);
1248                 device_reject_pin_request(&dev_info->device_info.address);
1249                 BT_DBG("-");
1250                 return;
1251         }
1252
1253         /* Set pairing data */
1254         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1255         trigger_pairing_info->addr = g_strdup(address);
1256         trigger_pairing_info->is_ssp = TRUE;
1257
1258         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1259         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1260
1261         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1262         _bt_send_event(BT_ADAPTER_EVENT,
1263                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1264         BT_DBG("-");
1265 }
1266
1267 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1268 {
1269         GVariant *param;
1270         gchar address[BT_ADDRESS_STR_LEN];
1271         char *p_addr;
1272         gchar *name;
1273         char str_passkey[7];
1274         int result = BLUETOOTH_ERROR_NONE;
1275         BT_DBG("+");
1276
1277         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1278         p_addr = address;
1279         name = dev_info->device_info.name;
1280
1281         BT_INFO("Address[%s]", address);
1282         BT_INFO("Name[%s]", name);
1283         BT_INFO("COD[%d]", dev_info->device_info.cod);
1284
1285         if (trigger_pairing_info) {
1286                 /* BTAPI support only one pairing at a time */
1287                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1288                 BT_ERR("New PIN request address [%s]", address);
1289                 device_reject_pin_request(&dev_info->device_info.address);
1290                 BT_DBG("-");
1291                 return;
1292         }
1293
1294         /* Set pairing data */
1295         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1296         trigger_pairing_info->addr = g_strdup(address);
1297         trigger_pairing_info->is_ssp = TRUE;
1298
1299         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1300         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1301
1302         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1303         if (passkey_watcher) {
1304                 BT_INFO("Send passkey to %s", passkey_watcher);
1305                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1306                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1307         } else {
1308                 _bt_send_event(BT_ADAPTER_EVENT,
1309                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1310         }
1311         BT_DBG("-");
1312 }
1313
1314 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1315 {
1316         gchar address[BT_ADDRESS_STR_LEN];
1317         gchar *name;
1318         int local_major;
1319         int local_minor;
1320         int cod;
1321         BT_DBG("+");
1322
1323         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1324         name = dev_info->name;
1325         cod = dev_info->cod;
1326
1327         BT_INFO("Address[%s]", address);
1328         BT_INFO("Name[%s]", name);
1329         BT_INFO("COD[%d]", cod);
1330
1331         if (trigger_pairing_info) {
1332                 /* BTAPI support only one pairing at a time */
1333                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1334                 BT_ERR("New PIN request address [%s]", address);
1335                 device_reject_pin_request(&dev_info->address);
1336                 BT_DBG("-");
1337                 return;
1338         }
1339
1340         /* Set pairing data */
1341         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1342         trigger_pairing_info->addr = g_strdup(address);
1343         trigger_pairing_info->is_ssp = TRUE;
1344
1345         local_major = ((cod >> 8) & 0x001f);
1346         local_minor = (cod & 0x00fc);
1347         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1348
1349         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1350         BT_DBG("-");
1351 }
1352
1353 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1354                 gboolean connected, unsigned char type)
1355 {
1356         gchar address[BT_ADDRESS_STR_LEN];
1357         int result = BLUETOOTH_ERROR_NONE;
1358         GVariant *param = NULL;
1359         bt_device_conn_info_t conn_info;
1360
1361         BT_DBG("+");
1362         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1363
1364         _bt_logging_connection(connected, type);
1365
1366         if (connected) {
1367                 param = g_variant_new("(isy)", result, address, type);
1368                 _bt_send_event(BT_DEVICE_EVENT,
1369                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1370                                 param);
1371         } else {
1372                 param = g_variant_new("(isy)", result, address, type);
1373                 _bt_send_event(BT_DEVICE_EVENT,
1374                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1375                                 param);
1376         }
1377
1378         conn_info.connected = connected;
1379         conn_info.type = type;
1380         /* Update local cache */
1381         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1382
1383 #ifdef TIZEN_GATT_CLIENT
1384         /*handle LE connected device info*/
1385         if (type) {
1386                 BT_DBG("handle LE connected device info");
1387                 _bt_handle_le_connected_dev_info(address, connected);
1388         }
1389 #endif
1390
1391         BT_DBG("-");
1392 }
1393
1394 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1395 {
1396         bt_remote_dev_info_t *dev_info = NULL;
1397         int result = BLUETOOTH_ERROR_NONE;
1398         GVariant *param = NULL;
1399         GVariant *uuids = NULL;
1400         GVariant *manufacturer_data = NULL;
1401         GVariantBuilder *builder = NULL;
1402         unsigned int i;
1403
1404         BT_INFO("+");
1405         ret_if(_bt_is_discovering() == FALSE);
1406         ret_if(event_data == NULL);
1407
1408         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1409
1410         if (is_ble) {
1411                 event_ble_dev_found_t * oal_ble_dev = event_data;
1412                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1413
1414                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1415
1416                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1417                 if (dev_info->manufacturer_data_len)
1418                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1419                                         dev_info->manufacturer_data_len);
1420                 else
1421                         dev_info->manufacturer_data = NULL;
1422                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1423         } else {
1424                 event_dev_found_t * oal_dev = event_data;
1425                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1426         }
1427
1428         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1429         if (dev_info->name == NULL)
1430                 dev_info->name = g_strdup(dev_info->address);
1431         BT_DBG("Name %s", dev_info->name);
1432
1433         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1434         for (i = 0; i < dev_info->uuid_count; i++)
1435                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1436
1437         uuids = g_variant_new("as", builder);
1438         g_variant_builder_unref(builder);
1439
1440         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1441                         dev_info->manufacturer_data,
1442                         dev_info->manufacturer_data_len,
1443                         TRUE,
1444                         NULL, NULL);
1445
1446         param = g_variant_new("(isunsbub@asn@ay)", result,
1447                         dev_info->address,
1448                         dev_info->class,
1449                         dev_info->rssi,
1450                         dev_info->name,
1451                         dev_info->paired,
1452                         dev_info->connected,
1453                         dev_info->trust,
1454                         uuids,
1455                         dev_info->manufacturer_data_len,
1456                         manufacturer_data);
1457
1458         _bt_send_event(BT_ADAPTER_EVENT,
1459                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1460                         param);
1461
1462         _bt_free_remote_dev(dev_info);
1463         BT_DBG("-");
1464 }
1465
1466 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1467 {
1468         gchar address[BT_ADDRESS_STR_LEN];
1469         int result = BLUETOOTH_ERROR_NONE;
1470         GVariant *param = NULL;
1471         int event;
1472         BT_DBG("+");
1473
1474         _bt_convert_addr_type_to_string(address, info->address.addr);
1475
1476         /* Update local cache */
1477         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1478
1479         param = g_variant_new("(is)", result, address);
1480         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1481                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1482         /* Send event to application */
1483         _bt_send_event(BT_DEVICE_EVENT,
1484                         event,
1485                         param);
1486
1487         BT_DBG("-");
1488 }
1489
1490 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1491 {
1492         BT_DBG("+");
1493         bt_pairing_data_t * info = *p_info;
1494         if (info) {
1495                 if (info->addr)
1496                         g_free(info->addr);
1497
1498                 g_free(info);
1499         }
1500         *p_info = NULL;
1501         BT_DBG("-");
1502 }
1503
1504 static void __bt_free_bond_info(uint8_t type)
1505 {
1506         BT_INFO("+");
1507
1508         switch (type) {
1509         case BT_DEVICE_BOND_INFO:
1510                 if (!trigger_bond_info)
1511                         break;
1512
1513                 if (trigger_bond_info->addr)
1514                         g_free(trigger_bond_info->addr);
1515                 if (trigger_bond_info->dev_addr)
1516                         g_free(trigger_bond_info->dev_addr);
1517                 if (trigger_bond_info->dev_info)
1518                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1519                 g_free(trigger_bond_info);
1520                 trigger_bond_info = NULL;
1521                 break;
1522         case BT_DEVICE_INCOMING_BOND_INFO:
1523                 if (!incoming_bond_info)
1524                         break;
1525
1526                 if (incoming_bond_info->dev_info)
1527                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1528                 g_free(incoming_bond_info);
1529                 incoming_bond_info = NULL;
1530                 break;
1531         case BT_DEVICE_UNBOND_INFO:
1532                 if (!trigger_unbond_info)
1533                         break;
1534
1535                 if (trigger_unbond_info->addr)
1536                         g_free(trigger_unbond_info->addr);
1537                 if (trigger_unbond_info->dev_addr)
1538                         g_free(trigger_unbond_info->dev_addr);
1539                 if (trigger_unbond_info->dev_info)
1540                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1541                 g_free(trigger_unbond_info);
1542                 trigger_unbond_info = NULL;
1543                 break;
1544         }
1545         BT_INFO("-");
1546 }
1547
1548 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1549 {
1550         bt_service_search_info_data_t * info = *p_info;
1551         if (info) {
1552                 if (info->addr) {
1553                         g_free(info->addr);
1554                         info->addr = NULL;
1555                 }
1556
1557                 if (info->dev_addr) {
1558                         g_free(info->dev_addr);
1559                         info->dev_addr = NULL;
1560                 }
1561
1562                 if (info->dev_info) {
1563                         _bt_free_remote_dev(info->dev_info);
1564                         info->dev_info = NULL;
1565                 }
1566
1567                 g_free(info);
1568         }
1569         *p_info = NULL;
1570 }
1571
1572 static int __bt_device_handle_bond_state(void)
1573 {
1574         BT_INFO("Current Bond state: %d", bt_device_bond_state);
1575         int ret = OAL_STATUS_INTERNAL_ERROR;
1576
1577         switch (bt_device_bond_state) {
1578         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1579                 /*TODO:Bonding during discovery: Unhandled!!*/
1580                 BT_INFO("Bonding during discovery: Unhandled!!");
1581                 break;
1582         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1583                 /*TODO:Bonding during discovery: Unhandled!!*/
1584                 BT_INFO("Bonding during discovery: Unhandled!!");
1585                 break;
1586         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1587                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1588                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1589                 if (ret != OAL_STATUS_SUCCESS)
1590                         ret = __bt_device_handle_bond_state();
1591                 break;
1592         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1593                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1594                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1595                 /* Bonding procedure was started but unfortunately could not complete.
1596                    Basically removed bonding was success, but create bond request could not proceed
1597                    So lets cleanup the context */
1598                 if (ret != OAL_STATUS_SUCCESS) {
1599                         BT_ERR("Create Bond procedure could not suceed");
1600                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1601                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1602                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1603                         __bt_free_pairing_info(&trigger_pairing_info);
1604                 }
1605                 break;
1606         case BT_DEVICE_BOND_STATE_NONE:
1607                 BT_INFO("Create Bond failed!!");
1608                 if (trigger_bond_info) {
1609                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1610                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1611                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1612                         __bt_free_pairing_info(&trigger_pairing_info);
1613                 }
1614                 break;
1615         default:
1616                 break;
1617         }
1618
1619         if (ret != OAL_STATUS_SUCCESS)
1620                 return BLUETOOTH_ERROR_INTERNAL;
1621         else
1622                 return BLUETOOTH_ERROR_NONE;
1623 }
1624
1625 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1626 {
1627         int result;
1628         bt_address_t bd_addr;
1629
1630         BT_DBG("+");
1631
1632         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1633
1634         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1635         result = device_query_attributes(&bd_addr);
1636         if (result != OAL_STATUS_SUCCESS) {
1637                 BT_ERR("device_query_attributes error: [%d]", result);
1638                 return BLUETOOTH_ERROR_INTERNAL;
1639         }
1640
1641         BT_DBG("-");
1642         return BLUETOOTH_ERROR_NONE;
1643 }
1644
1645 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1646 {
1647         int ret;
1648         char address[BT_ADDRESS_STRING_SIZE];
1649
1650         BT_DBG("+");
1651         BT_CHECK_PARAMETER(alias, return);
1652
1653         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1654         if (ret != OAL_STATUS_SUCCESS) {
1655                 BT_ERR("device_set_alias: %d", ret);
1656                 return BLUETOOTH_ERROR_INTERNAL;
1657         }
1658
1659         /* Update local cache */
1660         _bt_convert_addr_type_to_string(address, device_address->addr);
1661         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1662
1663         BT_DBG("-");
1664         return BLUETOOTH_ERROR_NONE;
1665 }
1666
1667 int _bt_bond_device(bluetooth_device_address_t *device_address,
1668                 unsigned short conn_type, GArray **out_param1)
1669 {
1670         int result = BLUETOOTH_ERROR_NONE;
1671         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1672         bluetooth_device_info_t dev_info;
1673         BT_DBG("+");
1674
1675         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1676
1677         /* If bonding or discovery already going on */
1678         if (trigger_bond_info || _bt_is_discovering()) {
1679                 BT_ERR("Device is buzy, bonding can not proceed now..");
1680                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1681                 goto fail;
1682         }
1683
1684         /*TODO: If unbonding with same device going on */
1685         _bt_convert_addr_type_to_string(address, device_address->addr);
1686
1687         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1688         trigger_bond_info->addr = g_strdup(address);
1689         trigger_bond_info->conn_type = conn_type;
1690         trigger_bond_info->is_device_creating = TRUE;
1691         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1692         trigger_bond_info->dev_info = NULL;
1693
1694         /* Ready to initiate bonding */
1695
1696         /* In Tizen, we will first remove bond and then attempt to create bond to keep
1697            consistency with bluedroid. Even if remove bond fails due to device not already
1698            bonded, then straight away create bond is triggered. This is because, remove bond
1699            is handled differently in bluedroid and bluez. In Bluez, if device is
1700            already removed, remove bond call fails.
1701            However in bluedroid, remove bond on already removed device returns success. So we will
1702            handle the cases transparently*/
1703         bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1704         bond_retry_count = 0;
1705         result = __bt_device_handle_bond_state();
1706
1707         if (result != BLUETOOTH_ERROR_NONE)
1708                 goto fail;
1709
1710         BT_DBG("-");
1711         return result;
1712
1713 fail:
1714         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1715         memcpy(dev_info.device_address.addr, device_address->addr,
1716                         BLUETOOTH_ADDRESS_LENGTH);
1717
1718         g_array_append_vals(*out_param1, &dev_info,
1719                         sizeof(bluetooth_device_info_t));
1720         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1721
1722         BT_DBG("-");
1723         return result;
1724 }
1725
1726 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1727                 GArray **out_param1)
1728 {
1729         int result = OAL_STATUS_SUCCESS;
1730         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1731         bluetooth_device_info_t dev_info;
1732         BT_INFO("+");
1733
1734         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1735
1736         _bt_convert_addr_type_to_string(address, device_address->addr);
1737
1738         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1739         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1740         trigger_unbond_info->addr = g_strdup(address);
1741         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1742
1743         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1744         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1745                 BT_ERR("Bonding with same device already ongoing");
1746                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1747                 goto fail;
1748         }
1749
1750         result = device_destroy_bond((bt_address_t *)device_address);
1751         if (result != OAL_STATUS_SUCCESS)
1752                 goto fail;
1753
1754         return result;
1755
1756 fail:
1757         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1758         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1759                         trigger_unbond_info->addr);
1760
1761         g_array_append_vals(*out_param1, &dev_info,
1762                         sizeof(bluetooth_device_info_t));
1763         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1764
1765         return result;
1766 }
1767
1768 int _bt_cancel_bonding(void)
1769 {
1770         int result = OAL_STATUS_SUCCESS;
1771         BT_INFO("+");
1772
1773         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1774
1775         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1776
1777         if (result == OAL_STATUS_SUCCESS)
1778                 trigger_bond_info->is_cancelled_by_user = TRUE;
1779
1780         return result;
1781 }
1782
1783 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1784 {
1785         bluetooth_device_address_t device_address;
1786         int ret = OAL_STATUS_SUCCESS;
1787         BT_INFO("reply: %d", cnfm_reply);
1788
1789         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1790         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1791
1792         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1793
1794         if (trigger_pairing_info->is_ssp) {
1795                 if (cnfm_reply)
1796                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1797                 else
1798                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1799                 trigger_pairing_info->is_ssp = FALSE;
1800         } else {
1801                 if (cnfm_reply)
1802                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1803                 else
1804                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1805         }
1806
1807         __bt_free_pairing_info(&trigger_pairing_info);
1808
1809         if (ret != OAL_STATUS_SUCCESS) {
1810                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1811                 return BLUETOOTH_ERROR_INTERNAL;
1812         }
1813
1814         BT_INFO("-");
1815         return BLUETOOTH_ERROR_NONE;
1816 }
1817
1818 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1819 {
1820         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1821         bluetooth_device_address_t device_address;
1822         int ret = OAL_STATUS_SUCCESS;
1823         BT_INFO("reply: %d", cnfm_reply);
1824
1825         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1826         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1827
1828         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1829
1830         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1831
1832         __bt_free_pairing_info(&trigger_pairing_info);
1833         if (ret != OAL_STATUS_SUCCESS) {
1834                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1835                 return BLUETOOTH_ERROR_INTERNAL;
1836         }
1837
1838         BT_INFO("-");
1839         return BLUETOOTH_ERROR_NONE;
1840 }
1841
1842 gboolean _bt_device_is_pairing(void)
1843 {
1844         return (trigger_pairing_info) ? TRUE : FALSE;
1845 }
1846
1847 gboolean _bt_device_is_bonding(void)
1848 {
1849         return (trigger_bond_info) ? TRUE : FALSE;
1850 }
1851
1852 gboolean _bt_is_bonding_device_address(const char *address)
1853 {
1854         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1855                 return FALSE;
1856
1857         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1858                 BT_DBG("[%s]  is bonding device", address);
1859                 return TRUE;
1860         }
1861
1862         BT_DBG("[%s]  is NOT bonding device", address);
1863         return FALSE;
1864 }
1865
1866 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1867 {
1868         ret_if(trigger_bond_info == NULL);
1869         trigger_bond_info->is_autopair = is_autopair;
1870 }
1871
1872 int _bt_search_device(bluetooth_device_address_t *device_address)
1873 {
1874         int result = OAL_STATUS_SUCCESS;
1875         BT_DBG("+");
1876
1877         BT_CHECK_PARAMETER(device_address, return);
1878
1879         if (trigger_bond_info) {
1880                 BT_ERR("Bonding in progress");
1881                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1882         }
1883
1884         if (service_search_info) {
1885                 BT_ERR("Service searching in progress");
1886                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1887         }
1888
1889         /* allocate user data so that it can be retrieved in callback */
1890         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1891         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1892         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1893
1894         _bt_convert_addr_type_to_string(service_search_info->addr,
1895                         device_address->addr);
1896
1897         result = device_query_services((bt_address_t *)device_address);
1898
1899         if (result != OAL_STATUS_SUCCESS) {
1900                 BT_ERR("Device Service Search Failed..: %d", result);
1901                 __bt_free_service_search_info(&service_search_info);
1902                 return BLUETOOTH_ERROR_INTERNAL;
1903         }
1904         return BLUETOOTH_ERROR_NONE;
1905 }
1906
1907 int _bt_cancel_search_device(void)
1908 {
1909         int ret = OAL_STATUS_SUCCESS;
1910         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1911
1912         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1913
1914         if (ret != OAL_STATUS_SUCCESS) {
1915                 BT_ERR("SDP Cancel request failed [%d]", ret);
1916                 return BLUETOOTH_ERROR_INTERNAL;
1917         }
1918
1919         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1920                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1921
1922         __bt_free_service_search_info(&service_search_info);
1923
1924         return BLUETOOTH_ERROR_NONE;
1925         BT_DBG("-");
1926 }
1927
1928 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1929                 gboolean authorize)
1930 {
1931         int ret = OAL_STATUS_SUCCESS;
1932         BT_DBG("+");
1933
1934         BT_CHECK_PARAMETER(device_address, return);
1935         BT_INFO("Device to be Trusted? [%d]", authorize);
1936
1937         ret = device_set_authorized((bt_address_t*)device_address, authorize);
1938         if (ret != OAL_STATUS_SUCCESS) {
1939                 BT_ERR("device_set_authorized: %d", ret);
1940                 return BLUETOOTH_ERROR_INTERNAL;
1941         }
1942
1943         return BLUETOOTH_ERROR_NONE;
1944 }
1945
1946 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1947 {
1948         gboolean is_connected;
1949         oal_service_t svc_id;
1950
1951         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1952
1953         /*
1954          * TODO: While adding support for new profiles, need to add more
1955          * <svc_type, svc_id> mapping here.
1956          */
1957         switch (svc_type) {
1958         case BT_PROFILE_CONN_HID:
1959                 svc_id = HID_SERVICE_ID;
1960                 break;
1961         case BT_PROFILE_CONN_A2DP:
1962                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1963                 break;
1964         case BT_PROFILE_CONN_A2DP_SINK:
1965                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1966                 break;
1967         case BT_PROFILE_CONN_HSP:
1968                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1969                 break;
1970 #ifdef TIZEN_GATT_CLIENT
1971         case BT_PROFILE_CONN_GATT:
1972                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1973 #endif
1974         default:
1975                 BT_DBG("Unknown svc_type: %d", svc_type);
1976                 return FALSE;
1977         }
1978
1979         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1980
1981         BT_DBG("svc_type: %d, is_connected: %s",
1982                         svc_type, is_connected ? "TRUE" : "FALSE");
1983
1984         return is_connected;
1985 }
1986
1987 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1988 {
1989         bt_address_t bd_addr;
1990         int res;
1991
1992         BT_DBG("+");
1993
1994         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1995         _bt_convert_addr_string_to_type(bd_addr.addr, address);
1996         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1997         if (res != OAL_STATUS_SUCCESS) {
1998                 BT_ERR("authorize_response: %d", res);
1999                 return BLUETOOTH_ERROR_INTERNAL;
2000         }
2001
2002         BT_DBG("-");
2003         return BLUETOOTH_ERROR_NONE;
2004 }
2005
2006 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2007                 int low_threshold, int in_range_threshold, int high_threshold)
2008 {
2009         int result;
2010         bt_address_t bd_addr;
2011
2012         BT_DBG("+");
2013
2014         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2015
2016         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2017         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2018                         low_threshold, in_range_threshold, high_threshold);
2019         if (result != OAL_STATUS_SUCCESS) {
2020                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2021                 return BLUETOOTH_ERROR_INTERNAL;
2022         }
2023
2024         BT_DBG("-");
2025         return BLUETOOTH_ERROR_NONE;
2026 }
2027
2028 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2029 {
2030         int result;
2031         bt_address_t bd_addr;
2032
2033         BT_DBG("+");
2034
2035         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2036
2037         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2038         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2039         if (result != OAL_STATUS_SUCCESS) {
2040                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2041                 return BLUETOOTH_ERROR_INTERNAL;
2042         }
2043
2044         BT_DBG("-");
2045         return BLUETOOTH_ERROR_NONE;
2046 }
2047
2048 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2049 {
2050         int result;
2051
2052         BT_INFO("Set passkey notification(sender:%s, %s)",
2053                         sender, enable ? "Enable" : "Disable");
2054
2055         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2056         if (OAL_STATUS_SUCCESS != result) {
2057                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2058                 return BLUETOOTH_ERROR_INTERNAL;
2059         }
2060
2061         g_free(passkey_watcher);
2062         if (enable == TRUE)
2063                 passkey_watcher = g_strdup(sender);
2064         else
2065                 passkey_watcher = NULL;
2066
2067         return BLUETOOTH_ERROR_NONE;
2068 }
2069
2070 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2071 {
2072         GSList *l = NULL;
2073
2074         BT_CHECK_PARAMETER(address, return);
2075         BT_CHECK_PARAMETER(pin_code, return);
2076
2077         for (l = pin_info_list; l != NULL; l = l->next) {
2078                 bt_pin_code_info_t *pin_info = l->data;
2079
2080                 if (!pin_info || !pin_info->address)
2081                         continue;
2082
2083                 if (g_strcmp0(pin_info->address, address) == 0) {
2084                         g_strlcpy(pin_code, pin_info->pin_code,
2085                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2086                         return BLUETOOTH_ERROR_NONE;
2087                 }
2088         }
2089
2090         return BLUETOOTH_ERROR_NOT_FOUND;
2091 }
2092
2093 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2094                 bluetooth_device_pin_code_t *pin_code)
2095 {
2096         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2097         bt_pin_code_info_t *pin_info = NULL;
2098         GSList *l = NULL;
2099
2100         BT_CHECK_PARAMETER(device_address, return);
2101         BT_CHECK_PARAMETER(pin_code, return);
2102         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2103                         BLUETOOTH_ERROR_NO_RESOURCES);
2104
2105         _bt_convert_addr_type_to_string(address, device_address->addr);
2106
2107         for (l = pin_info_list; l != NULL; l = l->next) {
2108                 pin_info = l->data;
2109
2110                 if (!pin_info || !pin_info->address)
2111                         continue;
2112
2113                 if (g_strcmp0(pin_info->address, address) == 0) {
2114                         g_free(pin_info->pin_code);
2115                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2116                         return BLUETOOTH_ERROR_NONE;
2117                 }
2118         }
2119
2120         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2121         pin_info->address = g_strdup(address);
2122         pin_info->pin_code = g_strdup(pin_code->pin_code);
2123         pin_info_list = g_slist_append(pin_info_list, pin_info);
2124
2125         return BLUETOOTH_ERROR_NONE;
2126 }
2127
2128 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2129 {
2130         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2131         bt_pin_code_info_t *pin_info = NULL;
2132         GSList *l = NULL;
2133
2134         BT_DBG("+");
2135
2136         BT_CHECK_PARAMETER(device_address, return);
2137
2138         _bt_convert_addr_type_to_string(address, device_address->addr);
2139
2140         for (l = pin_info_list; l != NULL; l = l->next) {
2141                 pin_info = l->data;
2142
2143                 if (!pin_info || !pin_info->address)
2144                         continue;
2145
2146                 if (g_strcmp0(pin_info->address, address) == 0) {
2147                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2148                         g_free(pin_info->address);
2149                         g_free(pin_info->pin_code);
2150                         g_free(pin_info);
2151                         break;
2152                 }
2153         }
2154
2155         BT_DBG("-");
2156         return BLUETOOTH_ERROR_NONE;
2157 }
2158
2159 #ifdef TIZEN_GATT_CLIENT
2160 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2161 {
2162         GSList *l = NULL;
2163         bt_connected_le_dev_t *dev;
2164
2165         if (!address)
2166                 return NULL;
2167
2168         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2169                 dev = l->data;
2170
2171                 if (g_strcmp0(dev->address, address) == 0)
2172                         return dev;
2173         }
2174         return NULL;
2175 }
2176
2177 static void __bt_le_conn_param_free(void *data)
2178 {
2179         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2180
2181         BT_DBG("%s", param->sender);
2182         g_free(param->sender);
2183         g_free(param);
2184 }
2185
2186 static void _bt_add_le_connected_dev_info(const char *address)
2187 {
2188         bt_connected_le_dev_t *dev = NULL;
2189
2190         if (!address)
2191                 return;
2192
2193         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2194         dev->address = g_strdup(address);
2195
2196         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2197
2198         return;
2199 }
2200
2201 static void _bt_remove_le_connected_dev_info(const char *address)
2202 {
2203         bt_connected_le_dev_t *dev = NULL;
2204
2205         if (!address)
2206                 return;
2207
2208         dev = __bt_get_le_connected_dev_info(address);
2209         if (!dev)
2210                 return;
2211
2212         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2213         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2214         g_free(dev->address);
2215         g_free(dev);
2216
2217         return;
2218 }
2219
2220
2221 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2222 {
2223         BT_DBG("+");
2224
2225         if (connected)
2226                 _bt_add_le_connected_dev_info(address);
2227         else
2228                 _bt_remove_le_connected_dev_info(address);
2229 }
2230
2231 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2232 {
2233         GSList *l = NULL;
2234         bt_le_conn_param_t *param = NULL;
2235
2236         if (!dev || !sender)
2237                 return NULL;
2238
2239         for (l = dev->senders; l; l = g_slist_next(l)) {
2240                 param = l->data;
2241                 if (g_strcmp0(param->sender, sender) == 0)
2242                         return param;
2243         }
2244
2245         return NULL;
2246 }
2247
2248 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2249 {
2250         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2251         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2252
2253         return parama->key > paramb->key;
2254 }
2255
2256
2257 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2258                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2259 {
2260         bt_connected_le_dev_t *dev = NULL;
2261         bt_le_conn_param_t *param = NULL;
2262         bt_le_conn_param_t *data = NULL;
2263
2264         BT_DBG("+");
2265
2266         if (!address || !sender)
2267                 return BLUETOOTH_ERROR_INVALID_PARAM;
2268
2269         dev = __bt_get_le_connected_dev_info(address);
2270         if (!dev)
2271                 return BLUETOOTH_ERROR_INTERNAL;
2272
2273         param = __bt_get_le_conn_param_info(dev, sender);
2274
2275         data = g_malloc0(sizeof(bt_le_conn_param_t));
2276         data->sender = g_strdup(sender);
2277         data->interval_min = interval_min;
2278         data->interval_max = interval_max;
2279         data->latency = latency;
2280         data->time_out = time_out;
2281         data->key = interval_min + (interval_max - interval_min)/2;
2282
2283         if (param == NULL) {
2284                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2285                 dev->senders = g_slist_append(dev->senders, data);
2286         } else {
2287                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2288                 dev->senders = g_slist_remove(dev->senders, param);
2289                 g_free(param->sender);
2290                 g_free(param);
2291                 dev->senders = g_slist_append(dev->senders, data);
2292         }
2293
2294         /* Sorting. First element have the minimum interval */
2295         dev->senders = g_slist_sort(dev->senders,
2296                         (GCompareFunc)__bt_compare_le_conn_param_key);
2297
2298         return BLUETOOTH_ERROR_NONE;
2299 }
2300
2301
2302 static int __bt_le_set_conn_parameter(const char *address,
2303                 float interval_min, float interval_max,
2304                 guint16 latency, guint16 time_out)
2305 {
2306         bt_address_t dev_addr = { {0} };
2307         guint32 min, max, to;
2308
2309         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2310                         interval_min, interval_max, latency, time_out);
2311
2312         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2313         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2314         to = time_out / BT_LE_CONN_TO_SPLIT;
2315
2316         BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2317                         min, max, latency, to);
2318
2319         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2320
2321         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2322 }
2323
2324 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2325 {
2326         bt_connected_le_dev_t *dev = NULL;
2327         bt_le_conn_param_t *param = NULL;
2328
2329         if (!address || !sender)
2330                 return BLUETOOTH_ERROR_INVALID_PARAM;
2331
2332         dev = __bt_get_le_connected_dev_info(address);
2333         if (!dev)
2334                 return BLUETOOTH_ERROR_INTERNAL;
2335
2336         param = __bt_get_le_conn_param_info(dev, sender);
2337         if (param) {
2338                 BT_DBG("Remove param %s %s ", address, sender);
2339                 dev->senders = g_slist_remove(dev->senders, param);
2340                 g_free(param->sender);
2341                 g_free(param);
2342         }
2343
2344         return BLUETOOTH_ERROR_NONE;
2345 }
2346
2347
2348 int _bt_le_connection_update(const char *sender,
2349                 unsigned char *device_address,
2350                 float interval_min, float interval_max,
2351                 guint16 latency, guint16 time_out)
2352 {
2353         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2354         guint32 min_supervision_to;
2355         bt_connected_le_dev_t *dev = NULL;
2356         bt_le_conn_param_t *param = NULL;
2357         int ret = BLUETOOTH_ERROR_NONE;
2358
2359         BT_CHECK_PARAMETER(device_address, return);
2360
2361         BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2362                         sender, interval_min, interval_max, latency, time_out);
2363
2364         if (interval_min > interval_max ||
2365                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2366                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2367                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2368                 goto fail;
2369         }
2370
2371         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2372                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2373                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2374                 goto fail;
2375         }
2376
2377         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2378                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2379                 goto fail;
2380         }
2381
2382         /*
2383          * The Supervision_Timeout in milliseconds shall be larger than
2384          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2385          * where Conn_Interval_Max is given in milliseconds.
2386          */
2387
2388         min_supervision_to = (1 + latency) * interval_max * 2;
2389         if (time_out <= min_supervision_to) {
2390                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2391                 goto fail;
2392         }
2393
2394         _bt_convert_addr_type_to_string(address, device_address);
2395         BT_DBG("Remote device address: %s", address);
2396
2397         _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2398
2399         dev = __bt_get_le_connected_dev_info(address);
2400         if (dev == NULL) {
2401                 BT_DBG("device not found in the list");
2402                 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2403                 goto fail;
2404         }
2405
2406         if (g_slist_length(dev->senders) == 1)
2407                 goto update;
2408         else {
2409                 param = dev->senders->data;
2410
2411                 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2412
2413                 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2414                         BT_DBG("Skip due to same interval");
2415                         return ret;
2416                 }
2417
2418                 interval_min = param->interval_min;
2419                 interval_max = param->interval_max;
2420         }
2421
2422 update:
2423         ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2424
2425         if (ret != OAL_STATUS_SUCCESS) {
2426                 _bt_remove_le_conn_param_info(address, sender);
2427                 BT_DBG("fail to update the LE connection parameter");
2428                 ret = BLUETOOTH_ERROR_INTERNAL;
2429                 goto fail;
2430         }
2431
2432         BT_DBG("updated LE connection parameter");
2433         dev->interval_min = interval_min;
2434         dev->interval_max = interval_max;
2435
2436         return BLUETOOTH_ERROR_NONE;
2437 fail:
2438         return ret;
2439 }
2440
2441 #endif