gatt client adaptation feature changes bt-api bt-service OAL
[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         if (connected) {
1358                 param = g_variant_new("(isy)", result, address, type);
1359                 _bt_send_event(BT_DEVICE_EVENT,
1360                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1361                                 param);
1362         } else {
1363                 param = g_variant_new("(isy)", result, address, type);
1364                 _bt_send_event(BT_DEVICE_EVENT,
1365                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1366                                 param);
1367         }
1368
1369         conn_info.connected = connected;
1370         conn_info.type = type;
1371         /* Update local cache */
1372         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1373
1374 #ifdef TIZEN_GATT_CLIENT
1375         /*handle LE connected device info*/
1376         if (type) {
1377                 BT_DBG("handle LE connected device info");
1378                 _bt_handle_le_connected_dev_info(address, connected);
1379         }
1380 #endif
1381
1382         BT_DBG("-");
1383 }
1384
1385 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1386 {
1387         bt_remote_dev_info_t *dev_info = NULL;
1388         int result = BLUETOOTH_ERROR_NONE;
1389         GVariant *param = NULL;
1390         GVariant *uuids = NULL;
1391         GVariant *manufacturer_data = NULL;
1392         GVariantBuilder *builder = NULL;
1393         unsigned int i;
1394
1395         BT_INFO("+");
1396         ret_if(_bt_is_discovering() == FALSE);
1397         ret_if(event_data == NULL);
1398
1399         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1400
1401         if (is_ble) {
1402                 event_ble_dev_found_t * oal_ble_dev = event_data;
1403                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1404
1405                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1406
1407                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1408                 if (dev_info->manufacturer_data_len)
1409                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1410                                         dev_info->manufacturer_data_len);
1411                 else
1412                         dev_info->manufacturer_data = NULL;
1413                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1414         } else {
1415                 event_dev_found_t * oal_dev = event_data;
1416                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1417         }
1418
1419         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1420         if (dev_info->name == NULL)
1421                 dev_info->name = g_strdup(dev_info->address);
1422         BT_DBG("Name %s", dev_info->name);
1423
1424         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1425         for (i = 0; i < dev_info->uuid_count; i++)
1426                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1427
1428         uuids = g_variant_new("as", builder);
1429         g_variant_builder_unref(builder);
1430
1431         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1432                         dev_info->manufacturer_data,
1433                         dev_info->manufacturer_data_len,
1434                         TRUE,
1435                         NULL, NULL);
1436
1437         param = g_variant_new("(isunsbub@asn@ay)", result,
1438                         dev_info->address,
1439                         dev_info->class,
1440                         dev_info->rssi,
1441                         dev_info->name,
1442                         dev_info->paired,
1443                         dev_info->connected,
1444                         dev_info->trust,
1445                         uuids,
1446                         dev_info->manufacturer_data_len,
1447                         manufacturer_data);
1448
1449         _bt_send_event(BT_ADAPTER_EVENT,
1450                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1451                         param);
1452
1453         _bt_free_remote_dev(dev_info);
1454         BT_DBG("-");
1455 }
1456
1457 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1458 {
1459         gchar address[BT_ADDRESS_STR_LEN];
1460         int result = BLUETOOTH_ERROR_NONE;
1461         GVariant *param = NULL;
1462         int event;
1463         BT_DBG("+");
1464
1465         _bt_convert_addr_type_to_string(address, info->address.addr);
1466
1467         /* Update local cache */
1468         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1469
1470         param = g_variant_new("(is)", result, address);
1471         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1472                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1473         /* Send event to application */
1474         _bt_send_event(BT_DEVICE_EVENT,
1475                         event,
1476                         param);
1477
1478         BT_DBG("-");
1479 }
1480
1481 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1482 {
1483         BT_DBG("+");
1484         bt_pairing_data_t * info = *p_info;
1485         if (info) {
1486                 if (info->addr)
1487                         g_free(info->addr);
1488
1489                 g_free(info);
1490         }
1491         *p_info = NULL;
1492         BT_DBG("-");
1493 }
1494
1495 static void __bt_free_bond_info(uint8_t type)
1496 {
1497         BT_INFO("+");
1498
1499         switch (type) {
1500         case BT_DEVICE_BOND_INFO:
1501                 if (!trigger_bond_info)
1502                         break;
1503
1504                 if (trigger_bond_info->addr)
1505                         g_free(trigger_bond_info->addr);
1506                 if (trigger_bond_info->dev_addr)
1507                         g_free(trigger_bond_info->dev_addr);
1508                 if (trigger_bond_info->dev_info)
1509                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1510                 g_free(trigger_bond_info);
1511                 trigger_bond_info = NULL;
1512                 break;
1513         case BT_DEVICE_INCOMING_BOND_INFO:
1514                 if (!incoming_bond_info)
1515                         break;
1516
1517                 if (incoming_bond_info->dev_info)
1518                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1519                 g_free(incoming_bond_info);
1520                 incoming_bond_info = NULL;
1521                 break;
1522         case BT_DEVICE_UNBOND_INFO:
1523                 if (!trigger_unbond_info)
1524                         break;
1525
1526                 if (trigger_unbond_info->addr)
1527                         g_free(trigger_unbond_info->addr);
1528                 if (trigger_unbond_info->dev_addr)
1529                         g_free(trigger_unbond_info->dev_addr);
1530                 if (trigger_unbond_info->dev_info)
1531                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1532                 g_free(trigger_unbond_info);
1533                 trigger_unbond_info = NULL;
1534                 break;
1535         }
1536         BT_INFO("-");
1537 }
1538
1539 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1540 {
1541         bt_service_search_info_data_t * info = *p_info;
1542         if (info) {
1543                 if (info->addr) {
1544                         g_free(info->addr);
1545                         info->addr = NULL;
1546                 }
1547
1548                 if (info->dev_addr) {
1549                         g_free(info->dev_addr);
1550                         info->dev_addr = NULL;
1551                 }
1552
1553                 if (info->dev_info) {
1554                         _bt_free_remote_dev(info->dev_info);
1555                         info->dev_info = NULL;
1556                 }
1557
1558                 g_free(info);
1559         }
1560         *p_info = NULL;
1561 }
1562
1563 static int __bt_device_handle_bond_state(void)
1564 {
1565         BT_INFO("Current Bond state: %d", bt_device_bond_state);
1566         int ret = OAL_STATUS_INTERNAL_ERROR;
1567
1568         switch (bt_device_bond_state) {
1569         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1570                 /*TODO:Bonding during discovery: Unhandled!!*/
1571                 BT_INFO("Bonding during discovery: Unhandled!!");
1572                 break;
1573         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1574                 /*TODO:Bonding during discovery: Unhandled!!*/
1575                 BT_INFO("Bonding during discovery: Unhandled!!");
1576                 break;
1577         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1578                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1579                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1580                 if (ret != OAL_STATUS_SUCCESS)
1581                         ret = __bt_device_handle_bond_state();
1582                 break;
1583         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1584                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1585                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1586                 /* Bonding procedure was started but unfortunately could not complete.
1587                    Basically removed bonding was success, but create bond request could not proceed
1588                    So lets cleanup the context */
1589                 if (ret != OAL_STATUS_SUCCESS) {
1590                         BT_ERR("Create Bond procedure could not suceed");
1591                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1592                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1593                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1594                         __bt_free_pairing_info(&trigger_pairing_info);
1595                 }
1596                 break;
1597         case BT_DEVICE_BOND_STATE_NONE:
1598                 BT_INFO("Create Bond failed!!");
1599                 if (trigger_bond_info) {
1600                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1601                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1602                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1603                         __bt_free_pairing_info(&trigger_pairing_info);
1604                 }
1605                 break;
1606         default:
1607                 break;
1608         }
1609
1610         if (ret != OAL_STATUS_SUCCESS)
1611                 return BLUETOOTH_ERROR_INTERNAL;
1612         else
1613                 return BLUETOOTH_ERROR_NONE;
1614 }
1615
1616 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1617 {
1618         int result;
1619         bt_address_t bd_addr;
1620
1621         BT_DBG("+");
1622
1623         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1624
1625         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1626         result = device_query_attributes(&bd_addr);
1627         if (result != OAL_STATUS_SUCCESS) {
1628                 BT_ERR("device_query_attributes error: [%d]", result);
1629                 return BLUETOOTH_ERROR_INTERNAL;
1630         }
1631
1632         BT_DBG("-");
1633         return BLUETOOTH_ERROR_NONE;
1634 }
1635
1636 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1637 {
1638         int ret;
1639         char address[BT_ADDRESS_STRING_SIZE];
1640
1641         BT_DBG("+");
1642         BT_CHECK_PARAMETER(alias, return);
1643
1644         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1645         if (ret != OAL_STATUS_SUCCESS) {
1646                 BT_ERR("device_set_alias: %d", ret);
1647                 return BLUETOOTH_ERROR_INTERNAL;
1648         }
1649
1650         /* Update local cache */
1651         _bt_convert_addr_type_to_string(address, device_address->addr);
1652         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1653
1654         BT_DBG("-");
1655         return BLUETOOTH_ERROR_NONE;
1656 }
1657
1658 int _bt_bond_device(bluetooth_device_address_t *device_address,
1659                 unsigned short conn_type, GArray **out_param1)
1660 {
1661         int result = BLUETOOTH_ERROR_NONE;
1662         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1663         bluetooth_device_info_t dev_info;
1664         BT_DBG("+");
1665
1666         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1667
1668         /* If bonding or discovery already going on */
1669         if (trigger_bond_info || _bt_is_discovering()) {
1670                 BT_ERR("Device is buzy, bonding can not proceed now..");
1671                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1672                 goto fail;
1673         }
1674
1675         /*TODO: If unbonding with same device going on */
1676         _bt_convert_addr_type_to_string(address, device_address->addr);
1677
1678         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1679         trigger_bond_info->addr = g_strdup(address);
1680         trigger_bond_info->conn_type = conn_type;
1681         trigger_bond_info->is_device_creating = TRUE;
1682         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1683         trigger_bond_info->dev_info = NULL;
1684
1685         /* Ready to initiate bonding */
1686
1687         /* In Tizen, we will first remove bond and then attempt to create bond to keep
1688            consistency with bluedroid. Even if remove bond fails due to device not already
1689            bonded, then straight away create bond is triggered. This is because, remove bond
1690            is handled differently in bluedroid and bluez. In Bluez, if device is
1691            already removed, remove bond call fails.
1692            However in bluedroid, remove bond on already removed device returns success. So we will
1693            handle the cases transparently*/
1694         bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1695         bond_retry_count = 0;
1696         result = __bt_device_handle_bond_state();
1697
1698         if (result != BLUETOOTH_ERROR_NONE)
1699                 goto fail;
1700
1701         BT_DBG("-");
1702         return result;
1703
1704 fail:
1705         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1706         memcpy(dev_info.device_address.addr, device_address->addr,
1707                         BLUETOOTH_ADDRESS_LENGTH);
1708
1709         g_array_append_vals(*out_param1, &dev_info,
1710                         sizeof(bluetooth_device_info_t));
1711         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1712
1713         BT_DBG("-");
1714         return result;
1715 }
1716
1717 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1718                 GArray **out_param1)
1719 {
1720         int result = OAL_STATUS_SUCCESS;
1721         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1722         bluetooth_device_info_t dev_info;
1723         BT_INFO("+");
1724
1725         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1726
1727         _bt_convert_addr_type_to_string(address, device_address->addr);
1728
1729         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1730         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1731         trigger_unbond_info->addr = g_strdup(address);
1732         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1733
1734         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1735         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1736                 BT_ERR("Bonding with same device already ongoing");
1737                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1738                 goto fail;
1739         }
1740
1741         result = device_destroy_bond((bt_address_t *)device_address);
1742         if (result != OAL_STATUS_SUCCESS)
1743                 goto fail;
1744
1745         return result;
1746
1747 fail:
1748         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1749         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1750                         trigger_unbond_info->addr);
1751
1752         g_array_append_vals(*out_param1, &dev_info,
1753                         sizeof(bluetooth_device_info_t));
1754         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1755
1756         return result;
1757 }
1758
1759 int _bt_cancel_bonding(void)
1760 {
1761         int result = OAL_STATUS_SUCCESS;
1762         BT_INFO("+");
1763
1764         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1765
1766         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1767
1768         if (result == OAL_STATUS_SUCCESS)
1769                 trigger_bond_info->is_cancelled_by_user = TRUE;
1770
1771         return result;
1772 }
1773
1774 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1775 {
1776         bluetooth_device_address_t device_address;
1777         int ret = OAL_STATUS_SUCCESS;
1778         BT_INFO("reply: %d", cnfm_reply);
1779
1780         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1781         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1782
1783         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1784
1785         if (trigger_pairing_info->is_ssp) {
1786                 if (cnfm_reply)
1787                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1788                 else
1789                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1790                 trigger_pairing_info->is_ssp = FALSE;
1791         } else {
1792                 if (cnfm_reply)
1793                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1794                 else
1795                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1796         }
1797
1798         __bt_free_pairing_info(&trigger_pairing_info);
1799
1800         if (ret != OAL_STATUS_SUCCESS) {
1801                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1802                 return BLUETOOTH_ERROR_INTERNAL;
1803         }
1804
1805         BT_INFO("-");
1806         return BLUETOOTH_ERROR_NONE;
1807 }
1808
1809 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1810 {
1811         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1812         bluetooth_device_address_t device_address;
1813         int ret = OAL_STATUS_SUCCESS;
1814         BT_INFO("reply: %d", cnfm_reply);
1815
1816         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1817         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1818
1819         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1820
1821         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1822
1823         __bt_free_pairing_info(&trigger_pairing_info);
1824         if (ret != OAL_STATUS_SUCCESS) {
1825                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1826                 return BLUETOOTH_ERROR_INTERNAL;
1827         }
1828
1829         BT_INFO("-");
1830         return BLUETOOTH_ERROR_NONE;
1831 }
1832
1833 gboolean _bt_device_is_pairing(void)
1834 {
1835         return (trigger_pairing_info) ? TRUE : FALSE;
1836 }
1837
1838 gboolean _bt_device_is_bonding(void)
1839 {
1840         return (trigger_bond_info) ? TRUE : FALSE;
1841 }
1842
1843 gboolean _bt_is_bonding_device_address(const char *address)
1844 {
1845         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1846                 return FALSE;
1847
1848         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1849                 BT_DBG("[%s]  is bonding device", address);
1850                 return TRUE;
1851         }
1852
1853         BT_DBG("[%s]  is NOT bonding device", address);
1854         return FALSE;
1855 }
1856
1857 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1858 {
1859         ret_if(trigger_bond_info == NULL);
1860         trigger_bond_info->is_autopair = is_autopair;
1861 }
1862
1863 int _bt_search_device(bluetooth_device_address_t *device_address)
1864 {
1865         int result = OAL_STATUS_SUCCESS;
1866         BT_DBG("+");
1867
1868         BT_CHECK_PARAMETER(device_address, return);
1869
1870         if (trigger_bond_info) {
1871                 BT_ERR("Bonding in progress");
1872                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1873         }
1874
1875         if (service_search_info) {
1876                 BT_ERR("Service searching in progress");
1877                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1878         }
1879
1880         /* allocate user data so that it can be retrieved in callback */
1881         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1882         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1883         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1884
1885         _bt_convert_addr_type_to_string(service_search_info->addr,
1886                         device_address->addr);
1887
1888         result = device_query_services((bt_address_t *)device_address);
1889
1890         if (result != OAL_STATUS_SUCCESS) {
1891                 BT_ERR("Device Service Search Failed..: %d", result);
1892                 __bt_free_service_search_info(&service_search_info);
1893                 return BLUETOOTH_ERROR_INTERNAL;
1894         }
1895         return BLUETOOTH_ERROR_NONE;
1896 }
1897
1898 int _bt_cancel_search_device(void)
1899 {
1900         int ret = OAL_STATUS_SUCCESS;
1901         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1902
1903         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1904
1905         if (ret != OAL_STATUS_SUCCESS) {
1906                 BT_ERR("SDP Cancel request failed [%d]", ret);
1907                 return BLUETOOTH_ERROR_INTERNAL;
1908         }
1909
1910         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1911                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1912
1913         __bt_free_service_search_info(&service_search_info);
1914
1915         return BLUETOOTH_ERROR_NONE;
1916         BT_DBG("-");
1917 }
1918
1919 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1920                 gboolean authorize)
1921 {
1922         int ret = OAL_STATUS_SUCCESS;
1923         BT_DBG("+");
1924
1925         BT_CHECK_PARAMETER(device_address, return);
1926         BT_INFO("Device to be Trusted? [%d]", authorize);
1927
1928         ret = device_set_authorized((bt_address_t*)device_address, authorize);
1929         if (ret != OAL_STATUS_SUCCESS) {
1930                 BT_ERR("device_set_authorized: %d", ret);
1931                 return BLUETOOTH_ERROR_INTERNAL;
1932         }
1933
1934         return BLUETOOTH_ERROR_NONE;
1935 }
1936
1937 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1938 {
1939         gboolean is_connected;
1940         oal_service_t svc_id;
1941
1942         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1943
1944         /*
1945          * TODO: While adding support for new profiles, need to add more
1946          * <svc_type, svc_id> mapping here.
1947          */
1948         switch (svc_type) {
1949         case BT_PROFILE_CONN_HID:
1950                 svc_id = HID_SERVICE_ID;
1951                 break;
1952         case BT_PROFILE_CONN_A2DP:
1953                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1954                 break;
1955         case BT_PROFILE_CONN_A2DP_SINK:
1956                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1957                 break;
1958         case BT_PROFILE_CONN_HSP:
1959                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1960                 break;
1961 #ifdef TIZEN_GATT_CLIENT
1962         case BT_PROFILE_CONN_GATT:
1963                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1964 #endif
1965         default:
1966                 BT_DBG("Unknown svc_type: %d", svc_type);
1967                 return FALSE;
1968         }
1969
1970         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1971
1972         BT_DBG("svc_type: %d, is_connected: %s",
1973                         svc_type, is_connected ? "TRUE" : "FALSE");
1974
1975         return is_connected;
1976 }
1977
1978 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1979 {
1980         bt_address_t bd_addr;
1981         int res;
1982
1983         BT_DBG("+");
1984
1985         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1986         _bt_convert_addr_string_to_type(bd_addr.addr, address);
1987         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1988         if (res != OAL_STATUS_SUCCESS) {
1989                 BT_ERR("authorize_response: %d", res);
1990                 return BLUETOOTH_ERROR_INTERNAL;
1991         }
1992
1993         BT_DBG("-");
1994         return BLUETOOTH_ERROR_NONE;
1995 }
1996
1997 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
1998                 int low_threshold, int in_range_threshold, int high_threshold)
1999 {
2000         int result;
2001         bt_address_t bd_addr;
2002
2003         BT_DBG("+");
2004
2005         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2006
2007         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2008         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2009                         low_threshold, in_range_threshold, high_threshold);
2010         if (result != OAL_STATUS_SUCCESS) {
2011                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2012                 return BLUETOOTH_ERROR_INTERNAL;
2013         }
2014
2015         BT_DBG("-");
2016         return BLUETOOTH_ERROR_NONE;
2017 }
2018
2019 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2020 {
2021         int result;
2022         bt_address_t bd_addr;
2023
2024         BT_DBG("+");
2025
2026         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2027
2028         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2029         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2030         if (result != OAL_STATUS_SUCCESS) {
2031                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2032                 return BLUETOOTH_ERROR_INTERNAL;
2033         }
2034
2035         BT_DBG("-");
2036         return BLUETOOTH_ERROR_NONE;
2037 }
2038
2039 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2040 {
2041         int result;
2042
2043         BT_INFO("Set passkey notification(sender:%s, %s)",
2044                         sender, enable ? "Enable" : "Disable");
2045
2046         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2047         if (OAL_STATUS_SUCCESS != result) {
2048                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2049                 return BLUETOOTH_ERROR_INTERNAL;
2050         }
2051
2052         g_free(passkey_watcher);
2053         if (enable == TRUE)
2054                 passkey_watcher = g_strdup(sender);
2055         else
2056                 passkey_watcher = NULL;
2057
2058         return BLUETOOTH_ERROR_NONE;
2059 }
2060
2061 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2062 {
2063         GSList *l = NULL;
2064
2065         BT_CHECK_PARAMETER(address, return);
2066         BT_CHECK_PARAMETER(pin_code, return);
2067
2068         for (l = pin_info_list; l != NULL; l = l->next) {
2069                 bt_pin_code_info_t *pin_info = l->data;
2070
2071                 if (!pin_info || !pin_info->address)
2072                         continue;
2073
2074                 if (g_strcmp0(pin_info->address, address) == 0) {
2075                         g_strlcpy(pin_code, pin_info->pin_code,
2076                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2077                         return BLUETOOTH_ERROR_NONE;
2078                 }
2079         }
2080
2081         return BLUETOOTH_ERROR_NOT_FOUND;
2082 }
2083
2084 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2085                 bluetooth_device_pin_code_t *pin_code)
2086 {
2087         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2088         bt_pin_code_info_t *pin_info = NULL;
2089         GSList *l = NULL;
2090
2091         BT_CHECK_PARAMETER(device_address, return);
2092         BT_CHECK_PARAMETER(pin_code, return);
2093         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2094                         BLUETOOTH_ERROR_NO_RESOURCES);
2095
2096         _bt_convert_addr_type_to_string(address, device_address->addr);
2097
2098         for (l = pin_info_list; l != NULL; l = l->next) {
2099                 pin_info = l->data;
2100
2101                 if (!pin_info || !pin_info->address)
2102                         continue;
2103
2104                 if (g_strcmp0(pin_info->address, address) == 0) {
2105                         g_free(pin_info->pin_code);
2106                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2107                         return BLUETOOTH_ERROR_NONE;
2108                 }
2109         }
2110
2111         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2112         pin_info->address = g_strdup(address);
2113         pin_info->pin_code = g_strdup(pin_code->pin_code);
2114         pin_info_list = g_slist_append(pin_info_list, pin_info);
2115
2116         return BLUETOOTH_ERROR_NONE;
2117 }
2118
2119 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2120 {
2121         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2122         bt_pin_code_info_t *pin_info = NULL;
2123         GSList *l = NULL;
2124
2125         BT_DBG("+");
2126
2127         BT_CHECK_PARAMETER(device_address, return);
2128
2129         _bt_convert_addr_type_to_string(address, device_address->addr);
2130
2131         for (l = pin_info_list; l != NULL; l = l->next) {
2132                 pin_info = l->data;
2133
2134                 if (!pin_info || !pin_info->address)
2135                         continue;
2136
2137                 if (g_strcmp0(pin_info->address, address) == 0) {
2138                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2139                         g_free(pin_info->address);
2140                         g_free(pin_info->pin_code);
2141                         g_free(pin_info);
2142                         break;
2143                 }
2144         }
2145
2146         BT_DBG("-");
2147         return BLUETOOTH_ERROR_NONE;
2148 }
2149
2150 #ifdef TIZEN_GATT_CLIENT
2151 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2152 {
2153         GSList *l = NULL;
2154         bt_connected_le_dev_t *dev;
2155
2156         if (!address)
2157                 return NULL;
2158
2159         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2160                 dev = l->data;
2161
2162                 if (g_strcmp0(dev->address, address) == 0)
2163                         return dev;
2164         }
2165         return NULL;
2166 }
2167
2168 static void __bt_le_conn_param_free(void *data)
2169 {
2170         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2171
2172         BT_DBG("%s", param->sender);
2173         g_free(param->sender);
2174         g_free(param);
2175 }
2176
2177 static void _bt_add_le_connected_dev_info(const char *address)
2178 {
2179         bt_connected_le_dev_t *dev = NULL;
2180
2181         if (!address)
2182                 return;
2183
2184         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2185         dev->address = g_strdup(address);
2186
2187         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2188
2189         return;
2190 }
2191
2192 static void _bt_remove_le_connected_dev_info(const char *address)
2193 {
2194         bt_connected_le_dev_t *dev = NULL;
2195
2196         if (!address)
2197                 return;
2198
2199         dev = __bt_get_le_connected_dev_info(address);
2200         if (!dev)
2201                 return;
2202
2203         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2204         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2205         g_free(dev->address);
2206         g_free(dev);
2207
2208         return;
2209 }
2210
2211
2212 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2213 {
2214         BT_DBG("+");
2215
2216         if (connected)
2217                 _bt_add_le_connected_dev_info(address);
2218         else
2219                 _bt_remove_le_connected_dev_info(address);
2220 }
2221
2222 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2223 {
2224         GSList *l = NULL;
2225         bt_le_conn_param_t *param = NULL;
2226
2227         if (!dev || !sender)
2228                 return NULL;
2229
2230         for (l = dev->senders; l; l = g_slist_next(l)) {
2231                 param = l->data;
2232                 if (g_strcmp0(param->sender, sender) == 0)
2233                         return param;
2234         }
2235
2236         return NULL;
2237 }
2238
2239 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2240 {
2241         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2242         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2243
2244         return parama->key > paramb->key;
2245 }
2246
2247
2248 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2249                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2250 {
2251         bt_connected_le_dev_t *dev = NULL;
2252         bt_le_conn_param_t *param = NULL;
2253         bt_le_conn_param_t *data = NULL;
2254
2255         BT_DBG("+");
2256
2257         if (!address || !sender)
2258                 return BLUETOOTH_ERROR_INVALID_PARAM;
2259
2260         dev = __bt_get_le_connected_dev_info(address);
2261         if (!dev)
2262                 return BLUETOOTH_ERROR_INTERNAL;
2263
2264         param = __bt_get_le_conn_param_info(dev, sender);
2265
2266         data = g_malloc0(sizeof(bt_le_conn_param_t));
2267         data->sender = g_strdup(sender);
2268         data->interval_min = interval_min;
2269         data->interval_max = interval_max;
2270         data->latency = latency;
2271         data->time_out = time_out;
2272         data->key = interval_min + (interval_max - interval_min)/2;
2273
2274         if (param == NULL) {
2275                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2276                 dev->senders = g_slist_append(dev->senders, data);
2277         } else {
2278                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2279                 dev->senders = g_slist_remove(dev->senders, param);
2280                 g_free(param->sender);
2281                 g_free(param);
2282                 dev->senders = g_slist_append(dev->senders, data);
2283         }
2284
2285         /* Sorting. First element have the minimum interval */
2286         dev->senders = g_slist_sort(dev->senders,
2287                         (GCompareFunc)__bt_compare_le_conn_param_key);
2288
2289         return BLUETOOTH_ERROR_NONE;
2290 }
2291
2292
2293 static int __bt_le_set_conn_parameter(const char *address,
2294                 float interval_min, float interval_max,
2295                 guint16 latency, guint16 time_out)
2296 {
2297         bt_address_t dev_addr = { {0} };
2298         guint32 min, max, to;
2299
2300         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2301                         interval_min, interval_max, latency, time_out);
2302
2303         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2304         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2305         to = time_out / BT_LE_CONN_TO_SPLIT;
2306
2307         BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2308                         min, max, latency, to);
2309
2310         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2311
2312         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2313 }
2314
2315 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2316 {
2317         bt_connected_le_dev_t *dev = NULL;
2318         bt_le_conn_param_t *param = NULL;
2319
2320         if (!address || !sender)
2321                 return BLUETOOTH_ERROR_INVALID_PARAM;
2322
2323         dev = __bt_get_le_connected_dev_info(address);
2324         if (!dev)
2325                 return BLUETOOTH_ERROR_INTERNAL;
2326
2327         param = __bt_get_le_conn_param_info(dev, sender);
2328         if (param) {
2329                 BT_DBG("Remove param %s %s ", address, sender);
2330                 dev->senders = g_slist_remove(dev->senders, param);
2331                 g_free(param->sender);
2332                 g_free(param);
2333         }
2334
2335         return BLUETOOTH_ERROR_NONE;
2336 }
2337
2338
2339 int _bt_le_connection_update(const char *sender,
2340                 unsigned char *device_address,
2341                 float interval_min, float interval_max,
2342                 guint16 latency, guint16 time_out)
2343 {
2344         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2345         guint32 min_supervision_to;
2346         bt_connected_le_dev_t *dev = NULL;
2347         bt_le_conn_param_t *param = NULL;
2348         int ret = BLUETOOTH_ERROR_NONE;
2349
2350         BT_CHECK_PARAMETER(device_address, return);
2351
2352         BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2353                         sender, interval_min, interval_max, latency, time_out);
2354
2355         if (interval_min > interval_max ||
2356                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2357                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2358                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2359                 goto fail;
2360         }
2361
2362         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2363                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2364                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2365                 goto fail;
2366         }
2367
2368         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2369                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2370                 goto fail;
2371         }
2372
2373         /*
2374          * The Supervision_Timeout in milliseconds shall be larger than
2375          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2376          * where Conn_Interval_Max is given in milliseconds.
2377          */
2378
2379         min_supervision_to = (1 + latency) * interval_max * 2;
2380         if (time_out <= min_supervision_to) {
2381                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2382                 goto fail;
2383         }
2384
2385         _bt_convert_addr_type_to_string(address, device_address);
2386         BT_DBG("Remote device address: %s", address);
2387
2388         _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2389
2390         dev = __bt_get_le_connected_dev_info(address);
2391         if (dev == NULL) {
2392                 BT_DBG("device not found in the list");
2393                 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2394                 goto fail;
2395         }
2396
2397         if (g_slist_length(dev->senders) == 1)
2398                 goto update;
2399         else {
2400                 param = dev->senders->data;
2401
2402                 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2403
2404                 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2405                         BT_DBG("Skip due to same interval");
2406                         return ret;
2407                 }
2408
2409                 interval_min = param->interval_min;
2410                 interval_max = param->interval_max;
2411         }
2412
2413 update:
2414         ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2415
2416         if (ret != OAL_STATUS_SUCCESS) {
2417                 _bt_remove_le_conn_param_info(address, sender);
2418                 BT_DBG("fail to update the LE connection parameter");
2419                 ret = BLUETOOTH_ERROR_INTERNAL;
2420                 goto fail;
2421         }
2422
2423         BT_DBG("updated LE connection parameter");
2424         dev->interval_min = interval_min;
2425         dev->interval_max = interval_max;
2426
2427         return BLUETOOTH_ERROR_NONE;
2428 fail:
2429         return ret;
2430 }
2431
2432 #endif