Remove handling for device found event in obex
[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 = g_strdup(address); /* TODO: Retrieve and send correct name in OBEX conn auth req event */
603
604                 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
605                 _bt_obex_server_set_pending_conn_auth_device_addr(address);
606                 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
607                 _bt_send_event(BT_OPP_SERVER_EVENT,
608                                 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
609                 g_free(name);
610                 return;
611         }
612 #endif
613         case HSP_SERVICE_ID:
614                 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
615                 break;
616         case HFP_SERVICE_ID:
617                 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
618                 break;
619         case SAP_SERVICE_ID:
620                 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
621                 break;
622         case HSP_HS_SERVICE_ID:
623                 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
624                 break;
625         case HFP_HS_SERVICE_ID:
626                 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
627                 break;
628 #ifdef TIZEN_BT_HAL
629         case IOTIVITY_SERVICE_ID:
630                 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
631                 break;
632 #endif
633         default:
634                 /* For now, reject authorization for any service apart from above switch cases */
635                 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
636                 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
637                 if (res != OAL_STATUS_SUCCESS)
638                         BT_ERR("authorize_response: %d", res);
639                 return;
640         }
641
642         /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
643         BT_INFO("Auto Accept authorization");
644         res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
645         if (res != OAL_STATUS_SUCCESS)
646                 BT_ERR("authorize_response: %d", res);
647         BT_INFO("-");
648 }
649
650 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
651 {
652         GVariant *param = NULL;
653         BT_DBG("+");
654
655         if ((remote_dev_info->name || remote_dev_info->alias)
656                         && remote_dev_info->address
657                         && remote_dev_info->uuids) {
658                 BT_INFO("All properties updated,  time to send bonding finished event");
659                 GVariant *uuids = NULL;
660                 GVariantBuilder *builder = NULL;
661                 GVariant *manufacturer_data;
662                 unsigned int i = 0;
663                 char *name = NULL;
664
665                 if (remote_dev_info->alias)
666                         name = remote_dev_info->alias;
667                 else
668                         name = remote_dev_info->name;
669
670                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
671                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
672                         g_variant_builder_add(builder, "s",
673                                         remote_dev_info->uuids[i]);
674                 }
675                 uuids = g_variant_new("as", builder);
676                 g_variant_builder_unref(builder);
677                 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
678                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
679                                 TRUE, NULL, NULL);
680
681                 param = g_variant_new("(isunsbub@asn@ay)",
682                                 BLUETOOTH_ERROR_NONE,
683                                 remote_dev_info->address,
684                                 remote_dev_info->class,
685                                 remote_dev_info->rssi,
686                                 name,
687                                 remote_dev_info->paired,
688                                 remote_dev_info->connected,
689                                 remote_dev_info->trust,
690                                 uuids,
691                                 remote_dev_info->manufacturer_data_len,
692                                 manufacturer_data);
693                 /* Send the event to application */
694                 _bt_send_event(BT_ADAPTER_EVENT,
695                                 BLUETOOTH_EVENT_BONDING_FINISHED,
696                                 param);
697                 if (incoming_bond) {
698                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
699                 } else {
700                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
701                         __bt_free_pairing_info(&trigger_pairing_info);
702                 }
703         } else {
704                 BT_INFO("Lets wait for more remote device properties");
705         }
706 }
707
708 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
709 {
710         char address[BT_ADDRESS_STRING_SIZE];
711         bluetooth_device_address_t dev_addr;
712
713         BT_INFO("+");
714
715         /*
716          * BlueZ sends paired signal for each paired device, during activation,
717          * We should ignore this, otherwise application thinks that a new device
718          * got paired
719          */
720         if (_bt_adapter_get_status() != BT_ACTIVATED) {
721                 BT_DBG("BT is not activated, so ignore this");
722                 return;
723         }
724
725         _bt_convert_addr_type_to_string(address, bd_addr->addr);
726         if (!incoming_bond_info) {
727                 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
728         } else {
729                 if (g_strcmp0(incoming_bond_info->addr, address)) {
730                         BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
731                                         address, incoming_bond_info->addr);
732                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
733                         incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
734                 }
735         }
736
737         BT_INFO("Incoming bond successfully completed");
738         g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
739         incoming_bond_info->dev_info = NULL;
740
741         _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
742         if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
743                 BT_DBG("Bonded device info query posted to stack successfully");
744         } else {
745                 BT_ERR("Bonded device info query failed");
746                 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
747         }
748
749         BT_INFO("-");
750 }
751
752 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
753 {
754         gchar address[BT_ADDRESS_STR_LEN];
755         bluetooth_device_address_t dev_addr;
756
757         BT_INFO("+");
758
759         if (trigger_bond_info == NULL) {
760                 /* Send reply */
761                 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
762                 __handle_incoming_bond_created_event(bd_addr);
763                 return;
764         }
765
766         _bt_convert_addr_type_to_string(address, bd_addr->addr);
767         if (g_strcmp0(trigger_bond_info->addr, address)) {
768                 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
769                                 address, trigger_bond_info->addr);
770                 __handle_incoming_bond_created_event(bd_addr);
771                 return;
772         }
773
774         BT_INFO("Bonding successfully completed");
775         /* Bonding state will be cleaned up & BONDING FINISHED EVENT
776            will be sent only when Properties are fetched from stack
777            Till that time lets not free trigger_bond_info.
778            However it is possible that while fetching device properties, internal
779            stack error can occur which can lead to no valid properties or
780            no properties at all. So in such cases, we must not wait for properties,
781            otherwise, it can lead to infinite wait  */
782         _bt_convert_addr_string_to_type(dev_addr.addr,
783                         trigger_bond_info->addr);
784
785         if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
786                 BT_DBG("BOnded device info query posted to stack successfully");
787                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
788                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
789         } else {
790                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
791                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
792                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
793                 /* Destroy if at all device got bonded at stack level */
794                 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
795
796                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
797                 __bt_free_pairing_info(&trigger_pairing_info);
798         }
799
800         BT_INFO("-");
801 }
802
803 /**********************************************************************************************
804 *  Bond removal event can be triggered for following reasons -
805 *  a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
806 *  b. If Application requests for explicitly removing the bond
807 *  c. When application attempt to create bond,bond is removed first which triggers this event
808 *     c. is in-line with Bluedroid bond create\emoval architecture
809 *********************************************************************************************/
810 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
811 {
812         char address[BT_ADDRESS_STRING_SIZE];
813
814         BT_INFO("+");
815
816         _bt_convert_addr_type_to_string(address, bd_addr->addr);
817         _bt_service_remove_device_from_bonded_list(address);
818
819         if (trigger_unbond_info) {
820                 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
821                 GVariant *param = NULL;
822                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
823                                 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
824                 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
825                 _bt_send_event(BT_ADAPTER_EVENT,
826                                 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
827                                 param);
828                 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
829                 __bt_free_pairing_info(&trigger_pairing_info);
830         } else if (trigger_bond_info) {
831                 BT_ERR("Bonding was removed");
832                 __bt_device_handle_bond_state();
833         }
834         BT_INFO("-");
835 }
836
837 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
838 {
839         BT_INFO("+");
840         oal_status_t status = bond_fail_event->status;
841         BT_INFO("Bonding failed, reason: %d", status);
842
843         switch (status) {
844         case OAL_STATUS_RMT_DEVICE_DOWN:
845         {
846                 if (trigger_bond_info) {
847                         BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
848                                         bond_retry_count);
849                         int ret = OAL_STATUS_SUCCESS;
850                         if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
851                                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
852                                 if (ret != OAL_STATUS_SUCCESS) {
853                                         BT_ERR("Create Bond procedure could not suceed");
854                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
855                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
856                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
857                                         __bt_free_pairing_info(&trigger_pairing_info);
858                                         bond_retry_count = 0;
859                                 } else
860                                         bond_retry_count++;
861                         } else {
862                                 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
863                                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
864                                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
865                                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
866                                 __bt_free_pairing_info(&trigger_pairing_info);
867                                 bond_retry_count = 0;
868                         }
869                 }
870                 break;
871         }
872         case OAL_STATUS_AUTH_FAILED:
873         {
874                 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
875                   To be considered later*/
876                 int result = BLUETOOTH_ERROR_INTERNAL;
877                 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
878                 if (trigger_bond_info) {
879                         BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
880                         if (trigger_bond_info->is_cancelled_by_user) {
881                                 BT_ERR("Bonding is cancelled by user");
882                                 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
883                         }
884                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
885                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
886                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
887                 }
888
889                 __bt_free_pairing_info(&trigger_pairing_info);
890                 break;
891         }
892         case OAL_STATUS_INTERNAL_ERROR:
893         {
894                 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
895                 if (trigger_unbond_info) {
896                         BT_INFO("Bond removal request failed, return DBUS and send event");
897                         GVariant *param = NULL;
898                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
899                                         trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
900                         param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
901                         _bt_send_event(BT_ADAPTER_EVENT,
902                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
903                                         param);
904                         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
905                 } else if (trigger_bond_info) {
906                         if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
907                                 if (trigger_bond_info) {
908                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
909                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
910                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
911                                 }
912                         }
913                 }
914
915                 __bt_free_pairing_info(&trigger_pairing_info);
916                 break;
917         }
918         default:
919         {
920                 BT_ERR("Unknown status of Bond failed event status [%d]", status);
921                 break;
922         }
923
924         }
925         BT_INFO("-");
926 }
927
928 static void __bt_device_event_handler(int event_type, gpointer event_data)
929 {
930         int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
931         BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
932
933         switch (event_type) {
934         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
935                 BT_INFO("BREDR Device Found");
936                 __bt_device_remote_device_found_callback(event_data, FALSE);
937                 break;
938         }
939         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
940                 BT_INFO("Dual Device Found");
941                 __bt_device_remote_device_found_callback(event_data, FALSE);
942                 break;
943         }
944         case OAL_EVENT_DEVICE_PROPERTIES: {
945                 BT_INFO("Remote Device properties Received");
946                 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
947                 break;
948         }
949         case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
950                 BT_INFO("Bonding Success event Received");
951                 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
952                 break;
953         }
954         case OAL_EVENT_DEVICE_BONDING_REMOVED: {
955                 BT_INFO("Bonding Removed event Received");
956                 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
957                 break;
958         }
959         case OAL_EVENT_DEVICE_BONDING_FAILED: {
960                 BT_INFO("Bonding Failed event Received");
961                 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
962                 break;
963         }
964         case OAL_EVENT_DEVICE_ACL_CONNECTED: {
965                 BT_INFO("ACL Connected event Received");
966                 event_dev_conn_status_t* param = event_data;
967                 __bt_device_conn_state_changed_callback(param, TRUE, 0);
968                 break;
969         }
970         case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
971                 BT_INFO("ACL Disconnected event Received");
972                 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
973                 break;
974         }
975         case OAL_EVENT_DEVICE_LE_CONNECTED: {
976                 BT_INFO("LE Connected event Received");
977                 event_dev_conn_status_t* param = event_data;
978                 __bt_device_conn_state_changed_callback(param, TRUE, 1);
979                 break;
980         }
981         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
982                 BT_INFO("LE Disconnected event Received");
983                 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
984                 break;
985         }
986         case OAL_EVENT_DEVICE_PIN_REQUEST: {
987                 BT_INFO("PIN Request Received");
988                 __bt_device_pin_request_callback((remote_device_t*)event_data);
989                 break;
990         }
991         case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
992                 BT_INFO("Passkey Entry request Received");
993                 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
994                 break;
995         }
996         case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
997                 BT_INFO("Passkey Confirmation Request Received");
998                 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
999                 break;
1000         }
1001         case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1002                 BT_INFO("Passkey Display Request Received");
1003                 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1004                 break;
1005         }
1006         case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1007                 BT_INFO("SSP Consent Request Received");
1008                  __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1009                 break;
1010         }
1011         case OAL_EVENT_DEVICE_SERVICES: {
1012                 BT_INFO("Remote Device Services Received");
1013                 __bt_device_services_callback((event_dev_services_t*)event_data);
1014                 break;
1015         }
1016         case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1017                 BT_INFO("Remote Device Authorization Request");
1018                 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1019                 break;
1020         }
1021         case OAL_EVENT_DEVICE_TRUSTED: {
1022                 BT_INFO("Remote Device Trusted");
1023                 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1024                 break;
1025         }
1026         case OAL_EVENT_DEVICE_UNTRUSTED: {
1027                 BT_INFO("Remote Device UnTrusted");
1028                 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1029                 break;
1030         }
1031         case OAL_EVENT_DEVICE_NAME: {
1032                 remote_device_t *rem_dev = event_data;
1033                 gchar address[BT_ADDRESS_STR_LEN];
1034
1035                 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1036                 BT_INFO("Remote Device name Received");
1037                 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1038
1039                 /* Update local cache */
1040                 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1041                 break;
1042         }
1043         case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1044                 event_device_trusted_profiles_t *ev = event_data;
1045                 char address[BT_ADDRESS_STRING_SIZE];
1046
1047                 ret_if(NULL == ev);
1048
1049                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1050                 _bt_send_event(BT_DEVICE_EVENT,
1051                                 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1052                                 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1053                                         address, ev->trust_val));
1054                 break;
1055         }
1056         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1057                 event_dev_rssi_info_t *ev = event_data;
1058                 char address[BT_ADDRESS_STRING_SIZE];
1059                 GVariant *param;
1060
1061                 ret_if(NULL == ev);
1062
1063                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1064                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1065                                 address, ev->link_type, TRUE);
1066                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1067                 break;
1068         }
1069         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1070                 event_dev_rssi_info_t *ev = event_data;
1071                 char address[BT_ADDRESS_STRING_SIZE];
1072                 GVariant *param;
1073
1074                 ret_if(NULL == ev);
1075
1076                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1077                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1078                                 address, ev->link_type, FALSE);
1079                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1080                 break;
1081         }
1082         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1083                 event_dev_rssi_info_t *ev = event_data;
1084                 char address[BT_ADDRESS_STRING_SIZE];
1085                 GVariant *param;
1086
1087                 ret_if(NULL == ev);
1088
1089                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1090                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1091                                 address, ev->link_type, ev->alert_type, ev->rssi);
1092                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1093                 break;
1094         }
1095         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1096                 event_dev_rssi_info_t *ev = event_data;
1097                 char address[BT_ADDRESS_STRING_SIZE];
1098                 GVariant *param;
1099
1100                 ret_if(NULL == ev);
1101
1102                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1103                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1104                                 address, ev->link_type, ev->rssi);
1105                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1106                 break;
1107         }
1108         default:
1109                 BT_INFO("Unhandled event..");
1110         }
1111 }
1112
1113 /* Legacy Pairing event handler */
1114 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1115 {
1116         GVariant *param;
1117         char address[BT_ADDRESS_STRING_SIZE];
1118         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1119         BT_DBG("+");
1120
1121         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1122
1123         BT_INFO("Address[%s]", address);
1124         BT_INFO("Name[%s]", pin_req_event->name);
1125         BT_INFO("COD[%d]", pin_req_event->cod);
1126
1127         if (trigger_pairing_info) {
1128                 /* BTAPI support only one pairing at a time */
1129                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1130                 BT_ERR("New PIN request address [%s]", address);
1131                 device_reject_pin_request(&pin_req_event->address);
1132                 return;
1133         }
1134
1135         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1136         if (_bt_is_bonding_device_address(address) == TRUE &&
1137                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1138                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1139                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1140                    to be checked and fixed apropriately */
1141                 _bt_set_autopair_status_in_bonding_info(TRUE);
1142                 device_accept_pin_request(&pin_req_event->address, "0000");
1143         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1144                 BT_DBG("Remote Device is HID keyboard Type..");
1145                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1146
1147                 if (_bt_agent_generate_passkey(str_passkey,
1148                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1149                         device_reject_pin_request(&pin_req_event->address);
1150                         goto done;
1151                 }
1152                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1153
1154                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1155                 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1156                 _bt_send_event(BT_ADAPTER_EVENT,
1157                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1158                 BT_DBG("Key board pairing in process");
1159         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1160                 BT_DBG("Use stored PIN code [%s]", pin_code);
1161                 device_accept_pin_request(&pin_req_event->address, pin_code);
1162         } else {
1163                 if (_bt_is_bonding_device_address(address) == TRUE) {
1164                         BT_DBG("Show Pin entry");
1165                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1166                         trigger_pairing_info->addr = g_strdup(address);
1167                         trigger_pairing_info->is_ssp = FALSE;
1168
1169                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1170                         param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1171                         _bt_send_event(BT_ADAPTER_EVENT,
1172                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1173                 }
1174         }
1175
1176 done:
1177         _bt_agent_release_memory();
1178         BT_DBG("-");
1179 }
1180
1181 /* SSP Pairing event handler */
1182 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1183 {
1184         GVariant *param;
1185         gchar address[BT_ADDRESS_STR_LEN];
1186         char *p_addr;
1187         gchar *name;
1188         int result = BLUETOOTH_ERROR_NONE;
1189         BT_DBG("+");
1190
1191         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1192         p_addr = address;
1193         name = dev_info->name;
1194
1195         BT_INFO("Address[%s]", address);
1196         BT_INFO("Name[%s]", name);
1197         BT_INFO("COD[%d]", dev_info->cod);
1198
1199         if (trigger_pairing_info) {
1200                 /* BTAPI support only one pairing at a time */
1201                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1202                 BT_ERR("New PIN request address [%s]", address);
1203                 device_reject_pin_request(&dev_info->address);
1204                 BT_DBG("-");
1205                 return;
1206         }
1207
1208         /* Set pairing data */
1209         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1210         trigger_pairing_info->addr = g_strdup(address);
1211         trigger_pairing_info->is_ssp = TRUE;
1212
1213         param = g_variant_new("(iss)", result, p_addr, name);
1214         _bt_send_event(BT_ADAPTER_EVENT,
1215                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1216         BT_DBG("-");
1217 }
1218
1219 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1220 {
1221         GVariant *param;
1222         gchar address[BT_ADDRESS_STR_LEN];
1223         char *p_addr;
1224         gchar *name;
1225         char str_passkey[7];
1226         int result = BLUETOOTH_ERROR_NONE;
1227         BT_DBG("+");
1228
1229         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1230         p_addr = address;
1231         name = dev_info->device_info.name;
1232
1233         BT_INFO("Address[%s]", address);
1234         BT_INFO("Name[%s]", name);
1235         BT_INFO("COD[%d]", dev_info->device_info.cod);
1236
1237         if (trigger_pairing_info) {
1238                 /* BTAPI support only one pairing at a time */
1239                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1240                 BT_ERR("New PIN request address [%s]", address);
1241                 device_reject_pin_request(&dev_info->device_info.address);
1242                 BT_DBG("-");
1243                 return;
1244         }
1245
1246         /* Set pairing data */
1247         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1248         trigger_pairing_info->addr = g_strdup(address);
1249         trigger_pairing_info->is_ssp = TRUE;
1250
1251         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1252         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1253
1254         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1255         _bt_send_event(BT_ADAPTER_EVENT,
1256                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1257         BT_DBG("-");
1258 }
1259
1260 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1261 {
1262         GVariant *param;
1263         gchar address[BT_ADDRESS_STR_LEN];
1264         char *p_addr;
1265         gchar *name;
1266         char str_passkey[7];
1267         int result = BLUETOOTH_ERROR_NONE;
1268         BT_DBG("+");
1269
1270         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1271         p_addr = address;
1272         name = dev_info->device_info.name;
1273
1274         BT_INFO("Address[%s]", address);
1275         BT_INFO("Name[%s]", name);
1276         BT_INFO("COD[%d]", dev_info->device_info.cod);
1277
1278         if (trigger_pairing_info) {
1279                 /* BTAPI support only one pairing at a time */
1280                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1281                 BT_ERR("New PIN request address [%s]", address);
1282                 device_reject_pin_request(&dev_info->device_info.address);
1283                 BT_DBG("-");
1284                 return;
1285         }
1286
1287         /* Set pairing data */
1288         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1289         trigger_pairing_info->addr = g_strdup(address);
1290         trigger_pairing_info->is_ssp = TRUE;
1291
1292         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1293         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1294
1295         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1296         if (passkey_watcher) {
1297                 BT_INFO("Send passkey to %s", passkey_watcher);
1298                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1299                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1300         } else {
1301                 _bt_send_event(BT_ADAPTER_EVENT,
1302                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1303         }
1304         BT_DBG("-");
1305 }
1306
1307 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1308 {
1309         gchar address[BT_ADDRESS_STR_LEN];
1310         gchar *name;
1311         int local_major;
1312         int local_minor;
1313         int cod;
1314         BT_DBG("+");
1315
1316         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1317         name = dev_info->name;
1318         cod = dev_info->cod;
1319
1320         BT_INFO("Address[%s]", address);
1321         BT_INFO("Name[%s]", name);
1322         BT_INFO("COD[%d]", cod);
1323
1324         if (trigger_pairing_info) {
1325                 /* BTAPI support only one pairing at a time */
1326                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1327                 BT_ERR("New PIN request address [%s]", address);
1328                 device_reject_pin_request(&dev_info->address);
1329                 BT_DBG("-");
1330                 return;
1331         }
1332
1333         /* Set pairing data */
1334         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1335         trigger_pairing_info->addr = g_strdup(address);
1336         trigger_pairing_info->is_ssp = TRUE;
1337
1338         local_major = ((cod >> 8) & 0x001f);
1339         local_minor = (cod & 0x00fc);
1340         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1341
1342         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1343         BT_DBG("-");
1344 }
1345
1346 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1347                 gboolean connected, unsigned char type)
1348 {
1349         gchar address[BT_ADDRESS_STR_LEN];
1350         int result = BLUETOOTH_ERROR_NONE;
1351         GVariant *param = NULL;
1352         bt_device_conn_info_t conn_info;
1353
1354         BT_DBG("+");
1355         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1356
1357         _bt_logging_connection(connected, type);
1358
1359         if (connected) {
1360                 param = g_variant_new("(isy)", result, address, type);
1361                 _bt_send_event(BT_DEVICE_EVENT,
1362                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1363                                 param);
1364         } else {
1365                 param = g_variant_new("(isy)", result, address, type);
1366                 _bt_send_event(BT_DEVICE_EVENT,
1367                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1368                                 param);
1369         }
1370
1371         conn_info.connected = connected;
1372         conn_info.type = type;
1373         /* Update local cache */
1374         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1375
1376 #ifdef TIZEN_GATT_CLIENT
1377         /*handle LE connected device info*/
1378         if (type) {
1379                 BT_DBG("handle LE connected device info");
1380                 _bt_handle_le_connected_dev_info(address, connected);
1381         }
1382 #endif
1383
1384         BT_DBG("-");
1385 }
1386
1387 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1388 {
1389         bt_remote_dev_info_t *dev_info = NULL;
1390         int result = BLUETOOTH_ERROR_NONE;
1391         GVariant *param = NULL;
1392         GVariant *uuids = NULL;
1393         GVariant *manufacturer_data = NULL;
1394         GVariantBuilder *builder = NULL;
1395         unsigned int i;
1396
1397         BT_INFO("+");
1398         ret_if(_bt_is_discovering() == FALSE);
1399         ret_if(event_data == NULL);
1400
1401         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1402
1403         if (is_ble) {
1404                 event_ble_dev_found_t * oal_ble_dev = event_data;
1405                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1406
1407                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1408
1409                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1410                 if (dev_info->manufacturer_data_len)
1411                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1412                                         dev_info->manufacturer_data_len);
1413                 else
1414                         dev_info->manufacturer_data = NULL;
1415                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1416         } else {
1417                 event_dev_found_t * oal_dev = event_data;
1418                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1419         }
1420
1421         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1422         if (dev_info->name == NULL)
1423                 dev_info->name = g_strdup(dev_info->address);
1424         BT_DBG("Name %s", dev_info->name);
1425
1426         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1427         for (i = 0; i < dev_info->uuid_count; i++)
1428                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1429
1430         uuids = g_variant_new("as", builder);
1431         g_variant_builder_unref(builder);
1432
1433         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1434                         dev_info->manufacturer_data,
1435                         dev_info->manufacturer_data_len,
1436                         TRUE,
1437                         NULL, NULL);
1438
1439         param = g_variant_new("(isunsbub@asn@ay)", result,
1440                         dev_info->address,
1441                         dev_info->class,
1442                         dev_info->rssi,
1443                         dev_info->name,
1444                         dev_info->paired,
1445                         dev_info->connected,
1446                         dev_info->trust,
1447                         uuids,
1448                         dev_info->manufacturer_data_len,
1449                         manufacturer_data);
1450
1451         _bt_send_event(BT_ADAPTER_EVENT,
1452                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1453                         param);
1454
1455         _bt_free_remote_dev(dev_info);
1456         BT_DBG("-");
1457 }
1458
1459 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1460 {
1461         gchar address[BT_ADDRESS_STR_LEN];
1462         int result = BLUETOOTH_ERROR_NONE;
1463         GVariant *param = NULL;
1464         int event;
1465         BT_DBG("+");
1466
1467         _bt_convert_addr_type_to_string(address, info->address.addr);
1468
1469         /* Update local cache */
1470         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1471
1472         param = g_variant_new("(is)", result, address);
1473         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1474                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1475         /* Send event to application */
1476         _bt_send_event(BT_DEVICE_EVENT,
1477                         event,
1478                         param);
1479
1480         BT_DBG("-");
1481 }
1482
1483 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1484 {
1485         BT_DBG("+");
1486         bt_pairing_data_t * info = *p_info;
1487         if (info) {
1488                 if (info->addr)
1489                         g_free(info->addr);
1490
1491                 g_free(info);
1492         }
1493         *p_info = NULL;
1494         BT_DBG("-");
1495 }
1496
1497 static void __bt_free_bond_info(uint8_t type)
1498 {
1499         BT_INFO("+");
1500
1501         switch (type) {
1502         case BT_DEVICE_BOND_INFO:
1503                 if (!trigger_bond_info)
1504                         break;
1505
1506                 if (trigger_bond_info->addr)
1507                         g_free(trigger_bond_info->addr);
1508                 if (trigger_bond_info->dev_addr)
1509                         g_free(trigger_bond_info->dev_addr);
1510                 if (trigger_bond_info->dev_info)
1511                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1512                 g_free(trigger_bond_info);
1513                 trigger_bond_info = NULL;
1514                 break;
1515         case BT_DEVICE_INCOMING_BOND_INFO:
1516                 if (!incoming_bond_info)
1517                         break;
1518
1519                 if (incoming_bond_info->dev_info)
1520                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1521                 g_free(incoming_bond_info);
1522                 incoming_bond_info = NULL;
1523                 break;
1524         case BT_DEVICE_UNBOND_INFO:
1525                 if (!trigger_unbond_info)
1526                         break;
1527
1528                 if (trigger_unbond_info->addr)
1529                         g_free(trigger_unbond_info->addr);
1530                 if (trigger_unbond_info->dev_addr)
1531                         g_free(trigger_unbond_info->dev_addr);
1532                 if (trigger_unbond_info->dev_info)
1533                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1534                 g_free(trigger_unbond_info);
1535                 trigger_unbond_info = NULL;
1536                 break;
1537         }
1538         BT_INFO("-");
1539 }
1540
1541 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1542 {
1543         bt_service_search_info_data_t * info = *p_info;
1544         if (info) {
1545                 if (info->addr) {
1546                         g_free(info->addr);
1547                         info->addr = NULL;
1548                 }
1549
1550                 if (info->dev_addr) {
1551                         g_free(info->dev_addr);
1552                         info->dev_addr = NULL;
1553                 }
1554
1555                 if (info->dev_info) {
1556                         _bt_free_remote_dev(info->dev_info);
1557                         info->dev_info = NULL;
1558                 }
1559
1560                 g_free(info);
1561         }
1562         *p_info = NULL;
1563 }
1564
1565 static int __bt_device_handle_bond_state(void)
1566 {
1567         BT_INFO("Current Bond state: %d", bt_device_bond_state);
1568         int ret = OAL_STATUS_INTERNAL_ERROR;
1569
1570         switch (bt_device_bond_state) {
1571         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1572                 /*TODO:Bonding during discovery: Unhandled!!*/
1573                 BT_INFO("Bonding during discovery: Unhandled!!");
1574                 break;
1575         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1576                 /*TODO:Bonding during discovery: Unhandled!!*/
1577                 BT_INFO("Bonding during discovery: Unhandled!!");
1578                 break;
1579         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1580                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1581                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1582                 if (ret != OAL_STATUS_SUCCESS)
1583                         ret = __bt_device_handle_bond_state();
1584                 break;
1585         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1586                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1587                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1588                 /* Bonding procedure was started but unfortunately could not complete.
1589                    Basically removed bonding was success, but create bond request could not proceed
1590                    So lets cleanup the context */
1591                 if (ret != OAL_STATUS_SUCCESS) {
1592                         BT_ERR("Create Bond procedure could not suceed");
1593                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1594                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1595                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1596                         __bt_free_pairing_info(&trigger_pairing_info);
1597                 }
1598                 break;
1599         case BT_DEVICE_BOND_STATE_NONE:
1600                 BT_INFO("Create Bond failed!!");
1601                 if (trigger_bond_info) {
1602                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1603                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1604                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1605                         __bt_free_pairing_info(&trigger_pairing_info);
1606                 }
1607                 break;
1608         default:
1609                 break;
1610         }
1611
1612         if (ret != OAL_STATUS_SUCCESS)
1613                 return BLUETOOTH_ERROR_INTERNAL;
1614         else
1615                 return BLUETOOTH_ERROR_NONE;
1616 }
1617
1618 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1619 {
1620         int result;
1621         bt_address_t bd_addr;
1622
1623         BT_DBG("+");
1624
1625         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1626
1627         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1628         result = device_query_attributes(&bd_addr);
1629         if (result != OAL_STATUS_SUCCESS) {
1630                 BT_ERR("device_query_attributes error: [%d]", result);
1631                 return BLUETOOTH_ERROR_INTERNAL;
1632         }
1633
1634         BT_DBG("-");
1635         return BLUETOOTH_ERROR_NONE;
1636 }
1637
1638 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1639 {
1640         int ret;
1641         char address[BT_ADDRESS_STRING_SIZE];
1642
1643         BT_DBG("+");
1644         BT_CHECK_PARAMETER(alias, return);
1645
1646         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1647         if (ret != OAL_STATUS_SUCCESS) {
1648                 BT_ERR("device_set_alias: %d", ret);
1649                 return BLUETOOTH_ERROR_INTERNAL;
1650         }
1651
1652         /* Update local cache */
1653         _bt_convert_addr_type_to_string(address, device_address->addr);
1654         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1655
1656         BT_DBG("-");
1657         return BLUETOOTH_ERROR_NONE;
1658 }
1659
1660 int _bt_bond_device(bluetooth_device_address_t *device_address,
1661                 unsigned short conn_type, GArray **out_param1)
1662 {
1663         int result = BLUETOOTH_ERROR_NONE;
1664         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1665         bluetooth_device_info_t dev_info;
1666         BT_DBG("+");
1667
1668         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1669
1670         /* If bonding or discovery already going on */
1671         if (trigger_bond_info || _bt_is_discovering()) {
1672                 BT_ERR("Device is buzy, bonding can not proceed now..");
1673                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1674                 goto fail;
1675         }
1676
1677         /*TODO: If unbonding with same device going on */
1678         _bt_convert_addr_type_to_string(address, device_address->addr);
1679
1680         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1681         trigger_bond_info->addr = g_strdup(address);
1682         trigger_bond_info->conn_type = conn_type;
1683         trigger_bond_info->is_device_creating = TRUE;
1684         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1685         trigger_bond_info->dev_info = NULL;
1686
1687         /* Ready to initiate bonding */
1688
1689         /* In Tizen, we will first remove bond and then attempt to create bond to keep
1690            consistency with bluedroid. Even if remove bond fails due to device not already
1691            bonded, then straight away create bond is triggered. This is because, remove bond
1692            is handled differently in bluedroid and bluez. In Bluez, if device is
1693            already removed, remove bond call fails.
1694            However in bluedroid, remove bond on already removed device returns success. So we will
1695            handle the cases transparently*/
1696         bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1697         bond_retry_count = 0;
1698         result = __bt_device_handle_bond_state();
1699
1700         if (result != BLUETOOTH_ERROR_NONE)
1701                 goto fail;
1702
1703         BT_DBG("-");
1704         return result;
1705
1706 fail:
1707         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1708         memcpy(dev_info.device_address.addr, device_address->addr,
1709                         BLUETOOTH_ADDRESS_LENGTH);
1710
1711         g_array_append_vals(*out_param1, &dev_info,
1712                         sizeof(bluetooth_device_info_t));
1713         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1714
1715         BT_DBG("-");
1716         return result;
1717 }
1718
1719 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1720                 GArray **out_param1)
1721 {
1722         int result = OAL_STATUS_SUCCESS;
1723         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1724         bluetooth_device_info_t dev_info;
1725         BT_INFO("+");
1726
1727         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1728
1729         _bt_convert_addr_type_to_string(address, device_address->addr);
1730
1731         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1732         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1733         trigger_unbond_info->addr = g_strdup(address);
1734         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1735
1736         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1737         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1738                 BT_ERR("Bonding with same device already ongoing");
1739                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1740                 goto fail;
1741         }
1742
1743         result = device_destroy_bond((bt_address_t *)device_address);
1744         if (result != OAL_STATUS_SUCCESS)
1745                 goto fail;
1746
1747         return result;
1748
1749 fail:
1750         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1751         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1752                         trigger_unbond_info->addr);
1753
1754         g_array_append_vals(*out_param1, &dev_info,
1755                         sizeof(bluetooth_device_info_t));
1756         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1757
1758         return result;
1759 }
1760
1761 int _bt_cancel_bonding(void)
1762 {
1763         int result = OAL_STATUS_SUCCESS;
1764         BT_INFO("+");
1765
1766         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1767
1768         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1769
1770         if (result == OAL_STATUS_SUCCESS)
1771                 trigger_bond_info->is_cancelled_by_user = TRUE;
1772
1773         return result;
1774 }
1775
1776 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1777 {
1778         bluetooth_device_address_t device_address;
1779         int ret = OAL_STATUS_SUCCESS;
1780         BT_INFO("reply: %d", cnfm_reply);
1781
1782         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1783         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1784
1785         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1786
1787         if (trigger_pairing_info->is_ssp) {
1788                 if (cnfm_reply)
1789                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1790                 else
1791                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1792                 trigger_pairing_info->is_ssp = FALSE;
1793         } else {
1794                 if (cnfm_reply)
1795                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1796                 else
1797                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1798         }
1799
1800         __bt_free_pairing_info(&trigger_pairing_info);
1801
1802         if (ret != OAL_STATUS_SUCCESS) {
1803                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1804                 return BLUETOOTH_ERROR_INTERNAL;
1805         }
1806
1807         BT_INFO("-");
1808         return BLUETOOTH_ERROR_NONE;
1809 }
1810
1811 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1812 {
1813         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1814         bluetooth_device_address_t device_address;
1815         int ret = OAL_STATUS_SUCCESS;
1816         BT_INFO("reply: %d", cnfm_reply);
1817
1818         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1819         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1820
1821         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1822
1823         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1824
1825         __bt_free_pairing_info(&trigger_pairing_info);
1826         if (ret != OAL_STATUS_SUCCESS) {
1827                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1828                 return BLUETOOTH_ERROR_INTERNAL;
1829         }
1830
1831         BT_INFO("-");
1832         return BLUETOOTH_ERROR_NONE;
1833 }
1834
1835 gboolean _bt_device_is_pairing(void)
1836 {
1837         return (trigger_pairing_info) ? TRUE : FALSE;
1838 }
1839
1840 gboolean _bt_device_is_bonding(void)
1841 {
1842         return (trigger_bond_info) ? TRUE : FALSE;
1843 }
1844
1845 gboolean _bt_is_bonding_device_address(const char *address)
1846 {
1847         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1848                 return FALSE;
1849
1850         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1851                 BT_DBG("[%s]  is bonding device", address);
1852                 return TRUE;
1853         }
1854
1855         BT_DBG("[%s]  is NOT bonding device", address);
1856         return FALSE;
1857 }
1858
1859 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1860 {
1861         ret_if(trigger_bond_info == NULL);
1862         trigger_bond_info->is_autopair = is_autopair;
1863 }
1864
1865 int _bt_search_device(bluetooth_device_address_t *device_address)
1866 {
1867         int result = OAL_STATUS_SUCCESS;
1868         BT_DBG("+");
1869
1870         BT_CHECK_PARAMETER(device_address, return);
1871
1872         if (trigger_bond_info) {
1873                 BT_ERR("Bonding in progress");
1874                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1875         }
1876
1877         if (service_search_info) {
1878                 BT_ERR("Service searching in progress");
1879                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1880         }
1881
1882         /* allocate user data so that it can be retrieved in callback */
1883         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1884         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1885         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1886
1887         _bt_convert_addr_type_to_string(service_search_info->addr,
1888                         device_address->addr);
1889
1890         result = device_query_services((bt_address_t *)device_address);
1891
1892         if (result != OAL_STATUS_SUCCESS) {
1893                 BT_ERR("Device Service Search Failed..: %d", result);
1894                 __bt_free_service_search_info(&service_search_info);
1895                 return BLUETOOTH_ERROR_INTERNAL;
1896         }
1897         return BLUETOOTH_ERROR_NONE;
1898 }
1899
1900 int _bt_cancel_search_device(void)
1901 {
1902         int ret = OAL_STATUS_SUCCESS;
1903         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1904
1905         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1906
1907         if (ret != OAL_STATUS_SUCCESS) {
1908                 BT_ERR("SDP Cancel request failed [%d]", ret);
1909                 return BLUETOOTH_ERROR_INTERNAL;
1910         }
1911
1912         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1913                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1914
1915         __bt_free_service_search_info(&service_search_info);
1916
1917         return BLUETOOTH_ERROR_NONE;
1918         BT_DBG("-");
1919 }
1920
1921 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1922                 gboolean authorize)
1923 {
1924         int ret = OAL_STATUS_SUCCESS;
1925         BT_DBG("+");
1926
1927         BT_CHECK_PARAMETER(device_address, return);
1928         BT_INFO("Device to be Trusted? [%d]", authorize);
1929
1930         ret = device_set_authorized((bt_address_t*)device_address, authorize);
1931         if (ret != OAL_STATUS_SUCCESS) {
1932                 BT_ERR("device_set_authorized: %d", ret);
1933                 return BLUETOOTH_ERROR_INTERNAL;
1934         }
1935
1936         return BLUETOOTH_ERROR_NONE;
1937 }
1938
1939 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1940 {
1941         gboolean is_connected;
1942         oal_service_t svc_id;
1943
1944         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1945
1946         /*
1947          * TODO: While adding support for new profiles, need to add more
1948          * <svc_type, svc_id> mapping here.
1949          */
1950         switch (svc_type) {
1951         case BT_PROFILE_CONN_HID:
1952                 svc_id = HID_SERVICE_ID;
1953                 break;
1954         case BT_PROFILE_CONN_A2DP:
1955                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1956                 break;
1957         case BT_PROFILE_CONN_A2DP_SINK:
1958                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1959                 break;
1960         case BT_PROFILE_CONN_HSP:
1961                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1962                 break;
1963 #ifdef TIZEN_GATT_CLIENT
1964         case BT_PROFILE_CONN_GATT:
1965                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1966 #endif
1967         default:
1968                 BT_DBG("Unknown svc_type: %d", svc_type);
1969                 return FALSE;
1970         }
1971
1972         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1973
1974         BT_DBG("svc_type: %d, is_connected: %s",
1975                         svc_type, is_connected ? "TRUE" : "FALSE");
1976
1977         return is_connected;
1978 }
1979
1980 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1981 {
1982         bt_address_t bd_addr;
1983         int res;
1984
1985         BT_DBG("+");
1986
1987         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1988         _bt_convert_addr_string_to_type(bd_addr.addr, address);
1989         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1990         if (res != OAL_STATUS_SUCCESS) {
1991                 BT_ERR("authorize_response: %d", res);
1992                 return BLUETOOTH_ERROR_INTERNAL;
1993         }
1994
1995         BT_DBG("-");
1996         return BLUETOOTH_ERROR_NONE;
1997 }
1998
1999 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2000                 int low_threshold, int in_range_threshold, int high_threshold)
2001 {
2002         int result;
2003         bt_address_t bd_addr;
2004
2005         BT_DBG("+");
2006
2007         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2008
2009         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2010         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2011                         low_threshold, in_range_threshold, high_threshold);
2012         if (result != OAL_STATUS_SUCCESS) {
2013                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2014                 return BLUETOOTH_ERROR_INTERNAL;
2015         }
2016
2017         BT_DBG("-");
2018         return BLUETOOTH_ERROR_NONE;
2019 }
2020
2021 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2022 {
2023         int result;
2024         bt_address_t bd_addr;
2025
2026         BT_DBG("+");
2027
2028         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2029
2030         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2031         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2032         if (result != OAL_STATUS_SUCCESS) {
2033                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2034                 return BLUETOOTH_ERROR_INTERNAL;
2035         }
2036
2037         BT_DBG("-");
2038         return BLUETOOTH_ERROR_NONE;
2039 }
2040
2041 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2042 {
2043         int result;
2044
2045         BT_INFO("Set passkey notification(sender:%s, %s)",
2046                         sender, enable ? "Enable" : "Disable");
2047
2048         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2049         if (OAL_STATUS_SUCCESS != result) {
2050                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2051                 return BLUETOOTH_ERROR_INTERNAL;
2052         }
2053
2054         g_free(passkey_watcher);
2055         if (enable == TRUE)
2056                 passkey_watcher = g_strdup(sender);
2057         else
2058                 passkey_watcher = NULL;
2059
2060         return BLUETOOTH_ERROR_NONE;
2061 }
2062
2063 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2064 {
2065         GSList *l = NULL;
2066
2067         BT_CHECK_PARAMETER(address, return);
2068         BT_CHECK_PARAMETER(pin_code, return);
2069
2070         for (l = pin_info_list; l != NULL; l = l->next) {
2071                 bt_pin_code_info_t *pin_info = l->data;
2072
2073                 if (!pin_info || !pin_info->address)
2074                         continue;
2075
2076                 if (g_strcmp0(pin_info->address, address) == 0) {
2077                         g_strlcpy(pin_code, pin_info->pin_code,
2078                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2079                         return BLUETOOTH_ERROR_NONE;
2080                 }
2081         }
2082
2083         return BLUETOOTH_ERROR_NOT_FOUND;
2084 }
2085
2086 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2087                 bluetooth_device_pin_code_t *pin_code)
2088 {
2089         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2090         bt_pin_code_info_t *pin_info = NULL;
2091         GSList *l = NULL;
2092
2093         BT_CHECK_PARAMETER(device_address, return);
2094         BT_CHECK_PARAMETER(pin_code, return);
2095         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2096                         BLUETOOTH_ERROR_NO_RESOURCES);
2097
2098         _bt_convert_addr_type_to_string(address, device_address->addr);
2099
2100         for (l = pin_info_list; l != NULL; l = l->next) {
2101                 pin_info = l->data;
2102
2103                 if (!pin_info || !pin_info->address)
2104                         continue;
2105
2106                 if (g_strcmp0(pin_info->address, address) == 0) {
2107                         g_free(pin_info->pin_code);
2108                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2109                         return BLUETOOTH_ERROR_NONE;
2110                 }
2111         }
2112
2113         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2114         pin_info->address = g_strdup(address);
2115         pin_info->pin_code = g_strdup(pin_code->pin_code);
2116         pin_info_list = g_slist_append(pin_info_list, pin_info);
2117
2118         return BLUETOOTH_ERROR_NONE;
2119 }
2120
2121 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2122 {
2123         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2124         bt_pin_code_info_t *pin_info = NULL;
2125         GSList *l = NULL;
2126
2127         BT_DBG("+");
2128
2129         BT_CHECK_PARAMETER(device_address, return);
2130
2131         _bt_convert_addr_type_to_string(address, device_address->addr);
2132
2133         for (l = pin_info_list; l != NULL; l = l->next) {
2134                 pin_info = l->data;
2135
2136                 if (!pin_info || !pin_info->address)
2137                         continue;
2138
2139                 if (g_strcmp0(pin_info->address, address) == 0) {
2140                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2141                         g_free(pin_info->address);
2142                         g_free(pin_info->pin_code);
2143                         g_free(pin_info);
2144                         break;
2145                 }
2146         }
2147
2148         BT_DBG("-");
2149         return BLUETOOTH_ERROR_NONE;
2150 }
2151
2152 #ifdef TIZEN_GATT_CLIENT
2153 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2154 {
2155         GSList *l = NULL;
2156         bt_connected_le_dev_t *dev;
2157
2158         if (!address)
2159                 return NULL;
2160
2161         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2162                 dev = l->data;
2163
2164                 if (g_strcmp0(dev->address, address) == 0)
2165                         return dev;
2166         }
2167         return NULL;
2168 }
2169
2170 static void __bt_le_conn_param_free(void *data)
2171 {
2172         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2173
2174         BT_DBG("%s", param->sender);
2175         g_free(param->sender);
2176         g_free(param);
2177 }
2178
2179 static void _bt_add_le_connected_dev_info(const char *address)
2180 {
2181         bt_connected_le_dev_t *dev = NULL;
2182
2183         if (!address)
2184                 return;
2185
2186         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2187         dev->address = g_strdup(address);
2188
2189         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2190
2191         return;
2192 }
2193
2194 static void _bt_remove_le_connected_dev_info(const char *address)
2195 {
2196         bt_connected_le_dev_t *dev = NULL;
2197
2198         if (!address)
2199                 return;
2200
2201         dev = __bt_get_le_connected_dev_info(address);
2202         if (!dev)
2203                 return;
2204
2205         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2206         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2207         g_free(dev->address);
2208         g_free(dev);
2209
2210         return;
2211 }
2212
2213
2214 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2215 {
2216         BT_DBG("+");
2217
2218         if (connected)
2219                 _bt_add_le_connected_dev_info(address);
2220         else
2221                 _bt_remove_le_connected_dev_info(address);
2222 }
2223
2224 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2225 {
2226         GSList *l = NULL;
2227         bt_le_conn_param_t *param = NULL;
2228
2229         if (!dev || !sender)
2230                 return NULL;
2231
2232         for (l = dev->senders; l; l = g_slist_next(l)) {
2233                 param = l->data;
2234                 if (g_strcmp0(param->sender, sender) == 0)
2235                         return param;
2236         }
2237
2238         return NULL;
2239 }
2240
2241 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2242 {
2243         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2244         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2245
2246         return parama->key > paramb->key;
2247 }
2248
2249
2250 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2251                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2252 {
2253         bt_connected_le_dev_t *dev = NULL;
2254         bt_le_conn_param_t *param = NULL;
2255         bt_le_conn_param_t *data = NULL;
2256
2257         BT_DBG("+");
2258
2259         if (!address || !sender)
2260                 return BLUETOOTH_ERROR_INVALID_PARAM;
2261
2262         dev = __bt_get_le_connected_dev_info(address);
2263         if (!dev)
2264                 return BLUETOOTH_ERROR_INTERNAL;
2265
2266         param = __bt_get_le_conn_param_info(dev, sender);
2267
2268         data = g_malloc0(sizeof(bt_le_conn_param_t));
2269         data->sender = g_strdup(sender);
2270         data->interval_min = interval_min;
2271         data->interval_max = interval_max;
2272         data->latency = latency;
2273         data->time_out = time_out;
2274         data->key = interval_min + (interval_max - interval_min)/2;
2275
2276         if (param == NULL) {
2277                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2278                 dev->senders = g_slist_append(dev->senders, data);
2279         } else {
2280                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2281                 dev->senders = g_slist_remove(dev->senders, param);
2282                 g_free(param->sender);
2283                 g_free(param);
2284                 dev->senders = g_slist_append(dev->senders, data);
2285         }
2286
2287         /* Sorting. First element have the minimum interval */
2288         dev->senders = g_slist_sort(dev->senders,
2289                         (GCompareFunc)__bt_compare_le_conn_param_key);
2290
2291         return BLUETOOTH_ERROR_NONE;
2292 }
2293
2294
2295 static int __bt_le_set_conn_parameter(const char *address,
2296                 float interval_min, float interval_max,
2297                 guint16 latency, guint16 time_out)
2298 {
2299         bt_address_t dev_addr = { {0} };
2300         guint32 min, max, to;
2301
2302         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2303                         interval_min, interval_max, latency, time_out);
2304
2305         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2306         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2307         to = time_out / BT_LE_CONN_TO_SPLIT;
2308
2309         BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2310                         min, max, latency, to);
2311
2312         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2313
2314         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2315 }
2316
2317 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2318 {
2319         bt_connected_le_dev_t *dev = NULL;
2320         bt_le_conn_param_t *param = NULL;
2321
2322         if (!address || !sender)
2323                 return BLUETOOTH_ERROR_INVALID_PARAM;
2324
2325         dev = __bt_get_le_connected_dev_info(address);
2326         if (!dev)
2327                 return BLUETOOTH_ERROR_INTERNAL;
2328
2329         param = __bt_get_le_conn_param_info(dev, sender);
2330         if (param) {
2331                 BT_DBG("Remove param %s %s ", address, sender);
2332                 dev->senders = g_slist_remove(dev->senders, param);
2333                 g_free(param->sender);
2334                 g_free(param);
2335         }
2336
2337         return BLUETOOTH_ERROR_NONE;
2338 }
2339
2340
2341 int _bt_le_connection_update(const char *sender,
2342                 unsigned char *device_address,
2343                 float interval_min, float interval_max,
2344                 guint16 latency, guint16 time_out)
2345 {
2346         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2347         guint32 min_supervision_to;
2348         bt_connected_le_dev_t *dev = NULL;
2349         bt_le_conn_param_t *param = NULL;
2350         int ret = BLUETOOTH_ERROR_NONE;
2351
2352         BT_CHECK_PARAMETER(device_address, return);
2353
2354         BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2355                         sender, interval_min, interval_max, latency, time_out);
2356
2357         if (interval_min > interval_max ||
2358                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2359                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2360                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2361                 goto fail;
2362         }
2363
2364         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2365                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2366                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2367                 goto fail;
2368         }
2369
2370         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2371                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2372                 goto fail;
2373         }
2374
2375         /*
2376          * The Supervision_Timeout in milliseconds shall be larger than
2377          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2378          * where Conn_Interval_Max is given in milliseconds.
2379          */
2380
2381         min_supervision_to = (1 + latency) * interval_max * 2;
2382         if (time_out <= min_supervision_to) {
2383                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2384                 goto fail;
2385         }
2386
2387         _bt_convert_addr_type_to_string(address, device_address);
2388         BT_DBG("Remote device address: %s", address);
2389
2390         _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2391
2392         dev = __bt_get_le_connected_dev_info(address);
2393         if (dev == NULL) {
2394                 BT_DBG("device not found in the list");
2395                 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2396                 goto fail;
2397         }
2398
2399         if (g_slist_length(dev->senders) == 1)
2400                 goto update;
2401         else {
2402                 param = dev->senders->data;
2403
2404                 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2405
2406                 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2407                         BT_DBG("Skip due to same interval");
2408                         return ret;
2409                 }
2410
2411                 interval_min = param->interval_min;
2412                 interval_max = param->interval_max;
2413         }
2414
2415 update:
2416         ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2417
2418         if (ret != OAL_STATUS_SUCCESS) {
2419                 _bt_remove_le_conn_param_info(address, sender);
2420                 BT_DBG("fail to update the LE connection parameter");
2421                 ret = BLUETOOTH_ERROR_INTERNAL;
2422                 goto fail;
2423         }
2424
2425         BT_DBG("updated LE connection parameter");
2426         dev->interval_min = interval_min;
2427         dev->interval_max = interval_max;
2428
2429         return BLUETOOTH_ERROR_NONE;
2430 fail:
2431         return ret;
2432 }
2433
2434 #endif