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