187bd7f22f9e2749adb74845e8a90014928b85d3
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / adapter / bt-service-core-adapter-le.c
1 /*
2  * Copyright (c) 2016 2017 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
27 #include "bt-internal-types.h"
28 #include "bt-service-common.h"
29 #include "bt-service-event.h"
30 #include "bt-service-core-adapter.h"
31 #include "bt-service-core-adapter-le.h"
32 #include "bt-service-event-receiver.h"
33 #include "bt-service-gatt.h"
34 #include "bt-service-util.h"
35 #include "bt-service-core-device.h"
36 #include "bt-service-oob.h"
37 #include "bt-service-battery-monitor.h"
38 #include "bt-service-mesh-common.h"
39
40 #include <oal-hardware.h>
41 #include <oal-manager.h>
42 #include <oal-event.h>
43 #include <oal-adapter-mgr.h>
44 #include <oal-device-mgr.h>
45 #include <oal-gatt.h>
46
47 #define BT_UUID_128 16
48 #define BT_ADV_DEFAULT_TIMEOUT 0
49 #define BT_ADV_DEFAULT_TX_POWER 4
50 #define BT_ADV_DEFAULT_CHANNEL_MAP 0
51 #define BT_SCAN_INTERVAL_SPLIT 0.625
52
53 static const char BASE_UUID_CONVERTED[BT_UUID_128] = {
54         0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
55         0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56 };
57
58 typedef struct {
59         int adv_inst_max;
60         int rpa_offloading;
61         int max_filter;
62         int le_2m_phy;
63         int le_coded_phy;
64 } bt_adapter_le_feature_info_t;
65
66 /* Set Default values */
67 static bt_adapter_le_feature_info_t le_feature_info = {1, 0, 0, 0, 0};
68
69 typedef struct {
70         int adv_handle;
71         char *sender;
72         int data_len;
73         unsigned char data[31];
74         gboolean is_adv;/* Adv or Scan Response: Only applicable if is_data_set_pending is TRUE */
75         gboolean is_data_set_pending; /* Data Set pending or Adv enable request at one time */
76         gboolean is_custom_adv; /* If Adv enable is custom adv enable request */
77         bluetooth_advertising_params_t params; /* Adv Parameters */
78 } bt_pending_adv_data_set_t;
79
80 static guint timer_id = 0;
81
82 static GSList *adv_data_pending_list = NULL;
83 static bt_le_status_t adapter_le_state = BT_LE_DEACTIVATED;
84 static bt_le_discovery_state_t adapter_le_discovery_state = LE_DISCOVERY_STOPPED;
85
86 static bool is_static_random_address = false;
87
88 /******************************************* LE Scan *********************************************/
89 #define BT_LE_SCAN_INTERVAL_MIN 2.5
90 #define BT_LE_SCAN_INTERVAL_MAX 10240
91 #define BT_LE_SCAN_WINDOW_MIN 2.5
92 #define BT_LE_SCAN_WINDOW_MAX 10240
93
94 #define BT_ADV_INTERVAL_SPLIT 0.625
95
96 typedef struct {
97         char *sender;
98         uid_t uid;
99         pid_t pid;
100         GSList *filter_list;
101         gboolean is_scanning;
102 } bt_adapter_le_scanner_t;
103
104 static GSList *scanner_list = NULL;
105 static gboolean is_le_set_scan_parameter = FALSE;
106 static gboolean is_le_scanning = FALSE;
107 static gboolean is_le_scan_hold = FALSE;
108 static gboolean scan_filter_enabled = FALSE;
109 static gboolean scan_stop_requested = FALSE;
110 static gboolean is_mesh_le_scan_stop_hold = FALSE;
111
112 static bluetooth_le_scan_params_t le_scan_params = { BT_LE_ACTIVE_SCAN, 0, 0 };
113
114 static int g_gatt_client_id = 0;
115 static void _bt_disable_all_scanner_status(void);
116
117 /******************************************* LE Scan *********************************************/
118
119 static void __bt_free_le_scanner(bt_adapter_le_scanner_t *scanner)
120 {
121         g_free(scanner->sender);
122         g_slist_free_full(scanner->filter_list, g_free);
123         g_free(scanner);
124 }
125
126 static void __bt_free_le_scanner_all(void)
127 {
128         g_slist_free_full(scanner_list, (GDestroyNotify)__bt_free_le_scanner);
129         scanner_list = NULL;
130
131         is_le_scanning = FALSE;
132         is_le_set_scan_parameter = FALSE;
133         le_scan_params.type = BT_LE_ACTIVE_SCAN;
134         le_scan_params.interval = 0;
135         le_scan_params.window = 0;
136 }
137
138 void _bt_adapter_set_le_status(bt_le_status_t status)
139 {
140         BT_INFO("adapter_le_state changed [%d] -> [%d]", adapter_le_state, status);
141         adapter_le_state = status;
142 }
143
144 bt_le_status_t _bt_adapter_get_le_status(void)
145 {
146         return adapter_le_state;
147 }
148
149 void _bt_set_le_scan_stop_requested(gboolean request)
150 {
151         scan_stop_requested = request;
152 }
153
154 gboolean _bt_is_le_scan_stop_requested(void)
155 {
156         return scan_stop_requested;
157 }
158
159 /* Internal functions of core adapter service */
160 static void __bt_le_handle_pending_requests(int service_function, void *user_data, unsigned int size)
161 {
162         GSList *l;
163         GArray *out_param;
164         invocation_info_t *req_info;
165         BT_INFO("+");
166
167         /* Get method invocation context */
168         for (l = _bt_get_invocation_list(); l != NULL; ) {
169                 req_info = l->data;
170                 l = g_slist_next(l);
171                 if (req_info == NULL || req_info->service_function != service_function)
172                         continue;
173
174                 /* Create out param */
175                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
176
177                 switch (service_function) {
178                 case BT_ENABLE_ADAPTER_LE:
179                 case BT_DISABLE_ADAPTER_LE: {
180                         gboolean done = TRUE;
181                         g_array_append_vals(out_param, &done, sizeof(gboolean));
182                         break;
183                 }
184                 default:
185                         BT_ERR("Unknown service function[%d]", service_function);
186                 }
187
188                 _bt_service_method_return(req_info->context, out_param, req_info->result);
189                 g_array_free(out_param, TRUE);
190                 /* Now free invocation info for this request*/
191                 _bt_free_info_from_invocation_list(req_info);
192         }
193 }
194
195
196 /* Request return handlings */
197 static gboolean __bt_le_post_set_enabled(gpointer user_data)
198 {
199         BT_INFO("__bt_adapter_post_set_enabled>>");
200
201         /* Add LE enabled post processing codes */
202
203         return FALSE;
204 }
205
206
207 static gboolean __bt_le_post_set_disabled(gpointer user_data)
208 {
209         BT_INFO("_bt_adapter_post_set_disabled>>");
210
211         /* Add LE disabled post processing codes */
212         _bt_le_oob_reset_local_cache_data();
213         is_static_random_address = false;
214
215         return FALSE;
216 }
217
218 static void __bt_le_update_bt_enabled(void)
219 {
220         int result = BLUETOOTH_ERROR_NONE;
221         BT_ERR("_bt_adapter_update_bt_enabled>>");
222         /* Update Bluetooth Status to notify other modules */
223         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
224                 BT_ERR("Set vconf failed\n");
225
226         /* TODO:Add timer function to handle any further post processing */
227         g_idle_add((GSourceFunc)__bt_le_post_set_enabled, NULL);
228
229         /*Return BT_ADAPTER_ENABLE Method invocation context */
230         __bt_le_handle_pending_requests(BT_ENABLE_ADAPTER_LE, NULL, 0);
231         /*Send BT Enabled event to application */
232         _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
233                         g_variant_new("(i)", result));
234 }
235
236 static void __bt_le_update_bt_disabled(void)
237 {
238         int result = BLUETOOTH_ERROR_NONE;
239         BT_INFO("_bt_adapter_update_bt_disabled>>");
240
241         int power_off_status = 0;
242         int ret;
243
244         /* Update the vconf LE status in normal Deactivation case only */
245         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
246         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
247
248         /* Update LE Status to notify other modules */
249         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
250                 BT_ERR("Set vconf failed");
251
252         /* TODO:Add timer function to handle any further post processing */
253         g_idle_add((GSourceFunc)__bt_le_post_set_disabled, NULL);
254
255         /* Return BT_ADAPTER_DISABLE Method invocation context */
256         __bt_le_handle_pending_requests(BT_DISABLE_ADAPTER_LE, NULL, 0);
257
258         /* Send BT Disabled event to application */
259         _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
260                         g_variant_new("(i)", result));
261 }
262
263 static void __bt_set_le_scan_status(gboolean mode)
264 {
265         BT_DBG("Set scanning status: %s", mode == TRUE ? "TRUE" : "FALSE");
266         is_le_scanning = mode;
267 }
268
269 gboolean _bt_is_le_scanning(void)
270 {
271         return is_le_scanning;
272 }
273
274 bt_adapter_le_scanner_t* __bt_find_scanner_from_list(const char *sender)
275 {
276         GSList *l;
277         bt_adapter_le_scanner_t *scanner;
278
279         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
280                 scanner = l->data;
281                 if (g_strcmp0(scanner->sender, sender) == 0)
282                         return scanner;
283         }
284
285         return NULL;
286 }
287
288 /* Event handlers */
289 static void __bt_adapter_le_handle_pending_request_info(int result,
290                 int service_function, void *param, unsigned int size)
291 {
292         GSList *l;
293         GArray *out_param;
294         invocation_info_t *req_info = NULL;
295
296         for (l = _bt_get_invocation_list(); l != NULL; ) {
297                 req_info = l->data;
298                 l = g_slist_next(l);
299                 if (req_info == NULL || req_info->service_function != service_function)
300                         continue;
301
302                 switch (service_function) {
303                 case BT_SET_ADVERTISING:
304                 case BT_SET_CUSTOM_ADVERTISING:
305                 case BT_SET_ADVERTISING_DATA:
306                 case BT_SET_SCAN_RESPONSE_DATA: {
307                         int *saved_handle;
308                         bt_pending_adv_data_set_t *data;
309
310                         ret_if(param == NULL);
311
312                         saved_handle = (int*)req_info->user_data;
313                         data = (bt_pending_adv_data_set_t*)param;
314                         BT_DBG("Current Sender [%s] Current Handle [%d]", data->sender, data->adv_handle);
315
316                         if (!g_strcmp0(req_info->sender, data->sender) && (*saved_handle == data->adv_handle)) {
317                                 BT_DBG("Requester found [%s] ADV Handle [%d]", req_info->sender, *saved_handle);
318                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
319                                 g_array_append_vals(out_param, &data->adv_handle, sizeof(int));
320                                 _bt_service_method_return(req_info->context, out_param, result);
321
322                                 /* Free data */
323                                 _bt_free_info_from_invocation_list(req_info);
324                                 g_array_free(out_param, TRUE);
325                         }
326                         break;
327                 }
328                 case BT_START_LE_DISCOVERY: {
329                         bt_adapter_le_scanner_t *scanner;
330
331                         BT_DBG("Request Sender: [%s]", req_info->sender);
332                         if (BLUETOOTH_ERROR_NONE != result) {
333                                 scanner = __bt_find_scanner_from_list(req_info->sender);
334                                 if (scanner && scanner->is_scanning)
335                                         scanner->is_scanning = FALSE;
336                         }
337
338                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
339                         _bt_service_method_return(req_info->context, out_param, result);
340                         _bt_free_info_from_invocation_list(req_info);
341                         g_array_free(out_param, TRUE);
342                         break;
343                 }
344                 case BT_STOP_LE_DISCOVERY: {
345                         BT_DBG("Request Sender: [%s]", req_info->sender);
346                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
347                         _bt_service_method_return(req_info->context, out_param, result);
348                         _bt_free_info_from_invocation_list(req_info);
349                         g_array_free(out_param, TRUE);
350                         break;
351                 }
352                 default:
353                         BT_DBG("Unknown function(%d)", service_function);
354                         break;
355                 }
356         }
357 }
358
359 static void __bt_le_handle_server_instance_registered(event_gatts_register_t *data)
360 {
361         int adv_handle;
362         GSList *l;
363         char uuid_string[BLUETOOTH_UUID_STRING_MAX];
364         bluetooth_advertising_data_t adv;
365         bluetooth_scan_resp_data_t scan_rsp;
366         int result = BLUETOOTH_ERROR_NONE;
367         bt_pending_adv_data_set_t *info = NULL;
368
369         memset(adv.data, 0, sizeof(adv.data));
370         memset(scan_rsp.data, 0, sizeof(scan_rsp.data));
371
372         _bt_uuid_to_string(&(data->server_uuid), uuid_string);
373         BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
374
375         /* Get sender and adv handle for the server instance */
376         _bt_get_adv_handle_from_instance(data->server_inst, &adv_handle);
377
378                 /* Check if pending Adv Data set or Scan response data set request is pending*/
379                 for (l = adv_data_pending_list; l != NULL; l = l->next) {
380                         info = l->data;
381
382                         if (info && (adv_handle == info->adv_handle)) {
383                                 if (info->is_data_set_pending) {
384                                         BT_INFO("LE Data set request is pending");
385                                         if (info->is_adv) {
386                                                 memcpy(&adv.data, info->data, info->data_len);
387                                                 result = _bt_set_advertising_data(info->sender,
388                                                         info->adv_handle, &adv, info->data_len, FALSE);
389                                                 if (result != BLUETOOTH_ERROR_NONE) {
390                                                         __bt_adapter_le_handle_pending_request_info(result,
391                                                                 BT_SET_ADVERTISING_DATA,
392                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
393                                                 }
394                                                 goto data_free;
395                                         } else {
396                                                 memcpy(&scan_rsp.data, info->data, info->data_len);
397                                                 result = _bt_set_scan_response_data(info->sender,
398                                                                 info->adv_handle, &scan_rsp, info->data_len, FALSE);
399                                                 if (result != BLUETOOTH_ERROR_NONE) {
400                                                         __bt_adapter_le_handle_pending_request_info(result,
401                                                                 BT_SET_SCAN_RESPONSE_DATA, (void*)info,
402                                                                 sizeof(bt_pending_adv_data_set_t));
403                                                 }
404                                                 goto data_free;
405                                         }
406                                 } else {
407                                         BT_INFO("LE Enable Adv request is pending");
408                                         if (info->is_custom_adv) {
409                                                 result = _bt_set_custom_advertising(info->sender, info->adv_handle,
410                                                                         TRUE, &info->params, FALSE/*Reserved Slot*/);
411                                                 if (result != BLUETOOTH_ERROR_NONE) {
412                                                         __bt_adapter_le_handle_pending_request_info(result,
413                                                                 BT_SET_CUSTOM_ADVERTISING,
414                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
415                                                 }
416                                                 goto data_free;
417                                         } else {
418                                                 result = _bt_set_advertising(info->sender, info->adv_handle,
419                                                                 TRUE, FALSE/*Reserved Slot*/);
420                                                 if (result != BLUETOOTH_ERROR_NONE) {
421                                                         __bt_adapter_le_handle_pending_request_info(result,
422                                                                 BT_SET_ADVERTISING,
423                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
424                                                 }
425                                                 goto data_free;
426                                 }
427                         }
428                 }
429         }
430         BT_DBG("-");
431         return;
432
433 data_free:
434         adv_data_pending_list = g_slist_remove(adv_data_pending_list, info);
435         g_free(info->sender);
436         g_free(info);
437 }
438
439 static void __bt_le_multi_advertising_enabled(event_ble_multiadv_status *event)
440 {
441         char *sender;
442         int adv_handle;
443         bt_pending_adv_data_set_t *info = NULL;
444         GVariant *param = NULL;
445         int result = BLUETOOTH_ERROR_NONE;
446
447         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
448         if (sender == NULL) {
449                 BT_ERR("Abnormal!!");
450         } else {
451                 if (event->status != OAL_STATUS_SUCCESS)
452                         result = BLUETOOTH_ERROR_INTERNAL;
453                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
454                 info->sender = sender;
455                 info->adv_handle = adv_handle;
456                 __bt_adapter_le_handle_pending_request_info(result,
457                                 BT_SET_CUSTOM_ADVERTISING,
458                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
459                 __bt_adapter_le_handle_pending_request_info(result,
460                                 BT_SET_ADVERTISING,
461                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
462
463                 /* Send event */
464                 param = g_variant_new("(ii)", result, info->adv_handle);
465                 _bt_send_event_to_dest(info->sender, BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ADVERTISING_STARTED, param);
466
467                 /* Free data */
468                 g_free(info->sender);
469                 g_free(info);
470
471                 BT_PERMANENT_LOG("Adv started %d", event->server_inst);
472         }
473 }
474
475 static void __bt_le_multi_advertising_disabled(event_ble_multiadv_status *event)
476 {
477         char *sender;
478         int adv_handle;
479         bt_pending_adv_data_set_t *info = NULL;
480         GVariant *param = NULL;
481         int result = BLUETOOTH_ERROR_NONE;
482         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
483
484         if (sender == NULL) {
485                 BT_INFO("Means application containing the adv info is already freed!!");
486         } else {
487                 if (event->status != OAL_STATUS_SUCCESS)
488                         result = BLUETOOTH_ERROR_INTERNAL;
489                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
490                 info->sender = sender;
491                 info->adv_handle = adv_handle;
492                 __bt_adapter_le_handle_pending_request_info(result,
493                                 BT_SET_CUSTOM_ADVERTISING,
494                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
495                 __bt_adapter_le_handle_pending_request_info(result,
496                                 BT_SET_ADVERTISING,
497                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
498                 /* Send event */
499                 param = g_variant_new("(ii)", result, info->adv_handle);
500                 _bt_send_event_to_dest(info->sender, BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ADVERTISING_STOPPED, param);
501
502                 /* Free allocated slot or server instance from stack to be used for other advertisng */
503                 result = _bt_unregister_server_instance(sender, adv_handle);
504
505                 /* Free data */
506                 g_free(info->sender);
507                 g_free(info);
508
509                 BT_PERMANENT_LOG("Adv stopped %d", event->server_inst);
510         }
511 }
512
513 static void __bt_le_multi_advertising_set_data(event_ble_multiadv_status *event)
514 {
515         char *sender = NULL;
516         int adv_handle;
517         bt_pending_adv_data_set_t *info = NULL;
518         int result = BLUETOOTH_ERROR_NONE;
519         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
520
521         if (sender == NULL) {
522                 BT_ERR("Abnormal!!");
523         } else {
524                 BT_INFO("Sender [%s], adv handle [%d]",  sender, adv_handle);
525                 if (event->status != OAL_STATUS_SUCCESS)
526                         result = BLUETOOTH_ERROR_INTERNAL;
527
528                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
529                 info->sender = sender;
530                 info->adv_handle = adv_handle;
531                 __bt_adapter_le_handle_pending_request_info(result,
532                                 BT_SET_SCAN_RESPONSE_DATA,
533                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
534                 __bt_adapter_le_handle_pending_request_info(result,
535                                 BT_SET_ADVERTISING_DATA,
536                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
537                 g_free(info->sender);
538                 g_free(info);
539         }
540 }
541
542 static int __bt_get_ad_data_by_type(const char *in_data, int in_len,
543                 char in_type, char **data, int *data_len)
544 {
545         if (in_data == NULL || data == NULL || data_len == NULL)
546                 return BLUETOOTH_ERROR_INTERNAL;
547
548         if (in_len <= 0)
549                 return BLUETOOTH_ERROR_INTERNAL;
550
551         int i;
552         unsigned char len = 0;
553         int type = 0;
554
555         for (i = 0; i < in_len && i < BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1; i++) {
556                 len = in_data[i];
557                 if (len <= 0 || i + 1 >= in_len) {
558                         BT_ERR("Invalid advertising data");
559                         return BLUETOOTH_ERROR_INTERNAL;
560                 }
561
562                 type = in_data[i + 1];
563                 if (type == in_type) {
564                         i = i + 2;
565                         len--;
566                         break;
567                 }
568
569                 i += len;
570                 len = 0;
571         }
572
573         if (i > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1
574                         || i + len > in_len
575                         || i + len > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX) {
576                 BT_ERR("Invalid advertising data");
577                 return BLUETOOTH_ERROR_INTERNAL;
578         } else if (len == 0) {
579                 BT_DBG("AD Type 0x%02x data is not set", in_type);
580                 *data = NULL;
581                 *data_len = 0;
582                 return BLUETOOTH_ERROR_NONE;
583         }
584
585         *data = g_memdup(&in_data[i], len);
586         if (*data == NULL)
587                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
588         *data_len = len;
589
590         return BLUETOOTH_ERROR_NONE;
591 }
592
593 static gboolean __bt_check_scan_result_uuid(const char *adv_data,
594                 int adv_data_len, const char *svc_uuid, int uuid_len,
595                 const char *uuid_mask, char ad_type)
596 {
597         char *data = NULL;
598         int data_len = 0;
599         int i;
600
601         __bt_get_ad_data_by_type(adv_data, adv_data_len,
602                         ad_type, &data, &data_len);
603         if (data != NULL) {
604                 for (i = 0; i < data_len; i += uuid_len) {
605                         if (uuid_len > (data_len - i))
606                                 break;
607
608                         if (_bt_byte_arr_cmp_with_mask(data + i,
609                                 svc_uuid, uuid_mask, uuid_len) == 0) {
610                                 g_free(data);
611                                 return TRUE;
612                         }
613                 }
614                 g_free(data);
615         }
616
617         return FALSE;
618 }
619
620 static gboolean __bt_check_scan_result_with_filter(const char *device_address,
621                 const char *adv_data, int adv_data_len,
622                 const char *scan_data, int scan_data_len,
623                 const bt_adapter_le_scanner_t *scanner)
624 {
625         GSList *l;
626         bluetooth_le_scan_filter_t *filter_data = NULL;
627         char *data = NULL;
628         int data_len = 0;
629         gboolean is_matched = FALSE;
630         int idx;
631
632         if (scanner->filter_list == NULL) {
633                 BT_DBG("This scanner is on Full Scan.");
634                 return TRUE;
635         }
636
637         for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
638                 filter_data = l->data;
639
640                 if (filter_data->added_features &
641                         BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS) {
642                         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
643
644                         _bt_convert_addr_type_to_string(address,
645                                         filter_data->device_address.addr);
646                         if (strncmp(address, device_address,
647                                         BT_ADDRESS_STRING_SIZE) != 0)
648                                 continue;
649                 }
650
651                 /* Check service uuid filter */
652                 if (filter_data->added_features &
653                                 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID) {
654                         is_matched = FALSE;
655                         const int ad_type_uuids[] = {
656                                 BT_LE_AD_TYPE_INCOMP_LIST_16_BIT_SERVICE_CLASS_UUIDS,
657                                 BT_LE_AD_TYPE_COMP_LIST_16_BIT_SERVICE_CLASS_UUIDS,
658                                 BT_LE_AD_TYPE_INCOMP_LIST_128_BIT_SERVICE_CLASS_UUIDS,
659                                 BT_LE_AD_TYPE_COMP_LIST_128_BIT_SERVICE_CLASS_UUIDS
660                         };
661
662                         for (idx = 0; idx < sizeof(ad_type_uuids) / sizeof(bt_le_ad_type_t); idx++) {
663                                 /* Check service uuid in advertising data */
664                                 if (__bt_check_scan_result_uuid(adv_data, adv_data_len,
665                                                 (const char *)filter_data->service_uuid.data.data,
666                                                 filter_data->service_uuid.data_len,
667                                                 (const char *)filter_data->service_uuid_mask.data.data,
668                                                 ad_type_uuids[idx]) == TRUE) {
669                                         BT_INFO("Service UUID is matched in adv data.");
670                                         is_matched = TRUE;
671                                         break;
672                                 }
673
674                                 /* Check service uuid in scan response data */
675                                 if (__bt_check_scan_result_uuid(scan_data, scan_data_len,
676                                                 (const char *)filter_data->service_uuid.data.data,
677                                                 filter_data->service_uuid.data_len,
678                                                 (const char *)filter_data->service_uuid_mask.data.data,
679                                                 ad_type_uuids[idx]) == TRUE) {
680                                         BT_INFO("Service UUID is matched in scan data.");
681                                         is_matched = TRUE;
682                                         break;
683                                 }
684                         }
685
686                         /* Service UUID is NOT matched. Continue to next filter */
687                         if (is_matched == FALSE)
688                                 continue;
689                 }
690
691                 /* Check solicitation uuid filter */
692                 if (filter_data->added_features &
693                                 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID) {
694                         is_matched = FALSE;
695                         const int ad_type_solicit_uuids[] = {
696                                 BT_LE_AD_TYPE_LIST_16_BIT_SERVICE_SOLICITATION_UUIDS,
697                                 BT_LE_AD_TYPE_LIST_128_BIT_SERVICE_SOLICITATION_UUIDS
698                         };
699
700                         for (idx = 0; idx < sizeof(ad_type_solicit_uuids) / sizeof(bt_le_ad_type_t); idx++) {
701                                 /* Check solicit uuid in advertising data */
702                                 if (__bt_check_scan_result_uuid(adv_data, adv_data_len,
703                                                 (const char *)filter_data->service_solicitation_uuid.data.data,
704                                                 filter_data->service_solicitation_uuid.data_len,
705                                                 (const char *)filter_data->service_solicitation_uuid_mask.data.data,
706                                                 ad_type_solicit_uuids[idx]) == TRUE) {
707                                         BT_INFO("Service Solicitation UUID is matched in adv data.");
708                                         is_matched = TRUE;
709                                         break;
710                                 }
711
712                                 /* Check solicit uuid in scan response data */
713                                 if (__bt_check_scan_result_uuid(scan_data, scan_data_len,
714                                                 (const char *)filter_data->service_solicitation_uuid.data.data,
715                                                 filter_data->service_solicitation_uuid.data_len,
716                                                 (const char *)filter_data->service_solicitation_uuid_mask.data.data,
717                                                 ad_type_solicit_uuids[idx]) == TRUE) {
718                                         BT_INFO("Service Solicitation UUID is matched in scan data.");
719                                         is_matched = TRUE;
720                                         break;
721                                 }
722                         }
723
724                         /* Service Solicitation UUID is NOT matched. Continue to next filter */
725                         if (is_matched == FALSE)
726                                 continue;
727                 }
728
729                 /* Check device name filter */
730                 if (filter_data->added_features &
731                                 BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME) {
732                         char name[BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX] = {0, };
733                         data = NULL;
734                         data_len = 0;
735                         is_matched = FALSE;
736
737                         /* Check device name in advertising data */
738                         __bt_get_ad_data_by_type(adv_data, adv_data_len,
739                                         BT_LE_AD_TYPE_COMPLETE_LOCAL_NAME,
740                                         &data, &data_len);
741                         if (data != NULL) {
742                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
743                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
744                                 memcpy(name, data, data_len);
745                                 name[data_len] = '\0';
746                                 g_free(data);
747                                 data = NULL;
748                                 if (g_strrstr(name, filter_data->device_name) != NULL) {
749                                         BT_INFO("Device Name is matched in adv data.");
750                                         is_matched = TRUE;
751                                 }
752                         }
753
754                         /* Check device name in scan response data */
755                         __bt_get_ad_data_by_type(scan_data, scan_data_len,
756                                         BT_LE_AD_TYPE_COMPLETE_LOCAL_NAME,
757                                         &data, &data_len);
758                         if (data != NULL) {
759                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
760                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
761                                 memcpy(name, data, data_len);
762                                 name[data_len] = '\0';
763                                 g_free(data);
764                                 data = NULL;
765                                 if (g_strrstr(name, filter_data->device_name) != NULL) {
766                                         BT_INFO("Device Name is matched in scan data.");
767                                         is_matched = TRUE;
768                                 }
769                         }
770
771                         /* Device Name is NOT matched. Continue to next filter */
772                         if (is_matched == FALSE)
773                                 continue;
774                 }
775
776                 /* Check manufacturer data filter */
777                 if (filter_data->added_features &
778                         BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA) {
779                         data = NULL;
780                         data_len = 0;
781                         is_matched = FALSE;
782
783                         /* Check manufacturer data in advertising data */
784                         __bt_get_ad_data_by_type(adv_data, adv_data_len,
785                                 BT_LE_AD_TYPE_MANUFACTURER_SPECIFIC_DATA,
786                                 &data, &data_len);
787                         if (data != NULL) {
788                                 int manufacturer_id;
789                                 manufacturer_id = (data[1] << 8) + data[0];
790
791                                 if (filter_data->manufacturer_id == manufacturer_id) {
792                                         if (filter_data->manufacturer_data.data_len == 0) {
793                                                 is_matched = TRUE;
794                                         } else {
795                                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
796                                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
797                                                 if (_bt_byte_arr_cmp_with_mask(data + 2,
798                                                                 (const char *)filter_data->manufacturer_data.data.data,
799                                                                 (const char *)filter_data->manufacturer_data_mask.data.data,
800                                                                 data_len - 2) == 0) {
801                                                         BT_INFO("Manufacturer Data is matched in adv data.");
802                                                         is_matched = TRUE;
803                                                 }
804                                         }
805                                 }
806                                 g_free(data);
807                                 data = NULL;
808                         }
809
810                         /* Check manufacturer data in scan response data */
811                         __bt_get_ad_data_by_type(scan_data, scan_data_len,
812                                 BT_LE_AD_TYPE_MANUFACTURER_SPECIFIC_DATA,
813                                 &data, &data_len);
814                         if (data != NULL) {
815                                 int manufacturer_id;
816                                 manufacturer_id = (data[1] << 8) + data[0];
817
818                                 if (filter_data->manufacturer_id == manufacturer_id) {
819                                         if (filter_data->manufacturer_data.data_len == 0) {
820                                                 is_matched = TRUE;
821                                         } else {
822                                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
823                                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
824                                                 if (_bt_byte_arr_cmp_with_mask(data + 2,
825                                                                 (const char *)filter_data->manufacturer_data.data.data,
826                                                                 (const char *)filter_data->manufacturer_data_mask.data.data,
827                                                                 data_len - 2) == 0) {
828                                                         BT_INFO("Manufacturer Data is matched in scan data.");
829                                                         is_matched = TRUE;
830                                                 }
831                                         }
832                                 }
833                                 g_free(data);
834                                 data = NULL;
835                         }
836
837                         /* Manufacturer Data is NOT matched. Continue to next filter */
838                         if (is_matched == FALSE)
839                                 continue;
840                 }
841
842                 /* Check service data filter */
843                 if (filter_data->added_features &
844                         BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA) {
845                         data = NULL;
846                         data_len = 0;
847                         is_matched = FALSE;
848
849                         /* Check service data in advertising data */
850                         __bt_get_ad_data_by_type(adv_data,
851                                 adv_data_len,
852                                 BT_LE_AD_TYPE_SERVICE_DATA,
853                                 &data, &data_len);
854                         if (data != NULL) {
855                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
856                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
857                                 if (_bt_byte_arr_cmp_with_mask(data,
858                                                 (const char *)filter_data->service_data.data.data,
859                                                 (const char *)filter_data->service_data_mask.data.data,
860                                                 data_len) == 0) {
861                                         BT_INFO("Service Data is matched in adv data.");
862                                         is_matched = TRUE;
863                                 }
864                                 g_free(data);
865                                 data = NULL;
866                         }
867
868                         /* Check service data in scan response data */
869                         __bt_get_ad_data_by_type(scan_data,
870                                 scan_data_len,
871                                 BT_LE_AD_TYPE_SERVICE_DATA,
872                                 &data, &data_len);
873                         if (data != NULL) {
874                                 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
875                                         data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
876                                 if (_bt_byte_arr_cmp_with_mask(data,
877                                                 (const char *)filter_data->service_data.data.data,
878                                                 (const char *)filter_data->service_data_mask.data.data,
879                                                 data_len) == 0) {
880                                         BT_INFO("Service Data is matched in scan data.");
881                                         is_matched = TRUE;
882                                 }
883                                 g_free(data);
884                                 data = NULL;
885                         }
886
887                         /* Service Data is NOT matched. Continue to next filter */
888                         if (is_matched == FALSE)
889                                 continue;
890                 }
891
892                 BT_INFO("The scan result is conformable.");
893                 return TRUE;
894         }
895
896         BT_INFO("The scan result is NOT conformable.");
897         return FALSE;
898 }
899
900 static void __bt_le_handle_device_found(event_ble_scan_result_info *scan_result)
901 {
902         int result = BLUETOOTH_ERROR_NONE;
903         bt_adapter_le_scanner_t *scanner = NULL;
904         char address[BT_ADDRESS_STRING_SIZE];
905         unsigned char adv_ind_data[BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX];
906         unsigned char scan_resp_data[BLUETOOTH_SCAN_RESP_DATA_LENGTH_MAX];
907         int adv_ind_len = 0;
908         int scan_resp_len = 0;
909         GVariant *scan_data_param;
910         GVariant *adv_data_param;
911         GVariant *param;
912         GSList *l;
913
914         ret_if(NULL == scan_result);
915
916         _bt_convert_addr_type_to_string(address, scan_result->address.addr);
917
918         memset(adv_ind_data, 0x00, sizeof(adv_ind_data));
919         adv_ind_len = scan_result->adv_data_len;
920         memcpy(adv_ind_data, scan_result->adv_data, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
921
922         memset(scan_resp_data, 0x00, sizeof(scan_resp_data));
923         scan_resp_len = scan_result->scan_rsp_data_len;
924         memcpy(scan_resp_data, scan_result->scan_rsp_data, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
925
926         BT_DBG("Address: %s, RSSI: %d, adv_ind_len: %d, scan_resp_len: %d",
927                         address, scan_result->rssi, adv_ind_len, scan_resp_len);
928
929         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
930                 scanner = l->data;
931                 if (scanner->is_scanning == FALSE)
932                         continue;
933
934                 if (__bt_check_scan_result_with_filter(address, (const char *)adv_ind_data,
935                                 adv_ind_len, (const char *)scan_resp_data, scan_resp_len, scanner) == FALSE)
936                         continue;
937
938                 adv_data_param = g_variant_new_from_data((const GVariantType *)"ay",
939                                 adv_ind_data, adv_ind_len, TRUE, NULL, NULL);
940                 scan_data_param = g_variant_new_from_data((const GVariantType *)"ay",
941                                 scan_resp_data, scan_resp_len, TRUE, NULL, NULL);
942
943                 param = g_variant_new("(isnnn@ayn@ay)",
944                                 result, address,
945                                 scan_result->addr_type, scan_result->rssi,
946                                 adv_ind_len, adv_data_param,
947                                 scan_resp_len, scan_data_param);
948
949                 _bt_send_event_to_dest(scanner->sender, BT_LE_ADAPTER_EVENT,
950                                 BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND, param);
951         }
952 }
953
954 static void __bt_le_state_change_callback(int bt_status)
955 {
956         BT_INFO("__bt_le_state_change_callback: status [%d]", bt_status);
957
958         switch (bt_status) {
959         case BT_LE_DEACTIVATED:
960                 _bt_adapter_set_le_status(bt_status);
961
962                 /* Add Adapter disabled post processing codes */
963                 __bt_le_update_bt_disabled();
964                 break;
965         case BT_LE_ACTIVATED:
966                 _bt_adapter_set_le_status(bt_status);
967
968                 /* Add Adapter enabled post processing codes */
969                 if (timer_id > 0) {
970                         BT_DBG("g_source is removed");
971                         g_source_remove(timer_id);
972                         timer_id = 0;
973                 }
974                 __bt_le_update_bt_enabled();
975                 break;
976         default:
977                 BT_ERR("Incorrect Bluetooth adapter state changed status");
978                 break;
979
980         }
981 }
982
983 static void __bt_le_event_handler(int event_type, gpointer event_data)
984 {
985         BT_INFO("OAL event = %d", event_type);
986
987         switch (event_type) {
988         case OAL_EVENT_BLE_ENABLED: {
989                 __bt_le_state_change_callback(BT_LE_ACTIVATED);
990                 break;
991         }
992         case OAL_EVENT_BLE_DISABLED: {
993                 __bt_le_state_change_callback(BT_LE_DEACTIVATED);
994                 break;
995         }
996         case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
997                 BT_INFO("OAL Event: Server Instance Registered");
998                 __bt_le_handle_server_instance_registered((event_gatts_register_t *)event_data);
999                 break;
1000         }
1001         case OAL_EVENT_BLE_ADVERTISING_STARTED: {
1002                 BT_INFO("OAL Event: Legacy Advertising Enabled: Not Supported!!");
1003                 break;
1004         }
1005         case OAL_EVENT_BLE_ADVERTISING_STOPPED: {
1006                 BT_INFO("OAL Event: Legacy Advertising Disabled: Not Supported!!");
1007                 break;
1008         }
1009         case OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE: {
1010                 BT_INFO("OAL Event: Advertising Enabled");
1011                 __bt_le_multi_advertising_enabled((event_ble_multiadv_status *)event_data);
1012                 break;
1013         }
1014         case OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE: {
1015                 BT_INFO("OAL Event: Advertising Disabled");
1016                 __bt_le_multi_advertising_disabled((event_ble_multiadv_status *)event_data);
1017                 break;
1018         }
1019         case OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA: {
1020                 BT_INFO("OAL Event: Advertising Data set successfully");
1021                 __bt_le_multi_advertising_set_data((event_ble_multiadv_status *)event_data);
1022                 break;
1023         }
1024         case OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE: {
1025                         BT_INFO("OAL Event: Advertising Params updated");
1026                 break;
1027         }
1028         case OAL_EVENT_BLE_DISCOVERY_STARTED: {
1029                 __bt_set_le_scan_status(TRUE);
1030                 __bt_adapter_le_handle_pending_request_info(
1031                                 BLUETOOTH_ERROR_NONE,
1032                                 BT_START_LE_DISCOVERY, NULL, 0);
1033                 break;
1034         }
1035         case OAL_EVENT_BLE_DISCOVERY_STOPPED: {
1036                 if (!_bt_is_le_scanning()) {
1037                         BT_ERR("LE discovery start failed");
1038                         __bt_adapter_le_handle_pending_request_info(
1039                                         BLUETOOTH_ERROR_INTERNAL,
1040                                         BT_START_LE_DISCOVERY, NULL, 0);
1041                         break;
1042                 }
1043
1044                 if (_bt_is_le_scan_stop_requested() == FALSE) {
1045                         int ret = gattc_start_le_discovery(g_gatt_client_id);
1046                         if (OAL_STATUS_SUCCESS != ret)
1047                                 BT_ERR("gattc_start_le_discovery failed");
1048                         break;
1049                 }
1050
1051                 _bt_set_le_scan_stop_requested(FALSE);
1052                 if (is_le_scan_hold == TRUE)
1053                         break;
1054                 __bt_set_le_scan_status(FALSE);
1055                 _bt_disable_all_scanner_status();
1056                 __bt_adapter_le_handle_pending_request_info(
1057                                 BLUETOOTH_ERROR_NONE,
1058                                 BT_STOP_LE_DISCOVERY, NULL, 0);
1059                 break;
1060         }
1061         case OAL_EVENT_BLE_REMOTE_DEVICE_FOUND: {
1062                 event_ble_scan_result_info *scan_result = event_data;
1063                 __bt_le_handle_device_found(scan_result);
1064                 break;
1065         }
1066         case OAL_EVENT_GATTC_REGISTRATION: {
1067                 event_gattc_register_t *gattc_event = event_data;
1068                 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
1069                 char *default_uuid_string;
1070
1071                 default_uuid_string = _bt_gatt_get_default_gatt_client_uuid();
1072                 _bt_uuid_to_string((service_uuid_t*)&(gattc_event->client_uuid), uuid_str);
1073                 BT_INFO("default UUID [%s] current registered uuid [%s]",
1074                                 default_uuid_string, uuid_str);
1075                 if (g_strcmp0(uuid_str, default_uuid_string)) {
1076                         BT_INFO("This is not the default GATT client that is registered");
1077
1078                         g_free(default_uuid_string);
1079                         break;
1080                 }
1081                 BT_INFO("GATT CLient instance registered is default client: ID [%d]",
1082                                 gattc_event->client_if);
1083                 g_free(default_uuid_string);
1084
1085                 g_gatt_client_id = gattc_event->client_if;
1086                 break;
1087         }
1088         case OAL_EVENT_BLE_LOCAL_FEATURES: {
1089                 event_adapter_le_features_t *le_features = event_data;
1090
1091                 le_feature_info.le_2m_phy = le_features->le_2m_phy_support;
1092                 le_feature_info.le_coded_phy = le_features->le_coded_phy_support;
1093                 le_feature_info.max_filter = le_features->max_adv_filter;
1094
1095                 BT_INFO("Adapter LE 2M PHY Support [%s]", le_feature_info.le_2m_phy ? "TRUE" : "FALSE");
1096                 BT_INFO("Adapter LE CODED PHY Support [%s]", le_feature_info.le_coded_phy ? "TRUE" : "FALSE");
1097
1098                 break;
1099         }
1100         default:
1101                 break;
1102         }
1103 }
1104
1105 int _bt_le_init(void)
1106 {
1107         /* Register LE event handler */
1108         _bt_service_register_event_handler_callback(BT_ADAPTER_LE_MODULE, __bt_le_event_handler);
1109         return BLUETOOTH_ERROR_NONE;
1110 }
1111
1112 void _bt_le_deinit(void)
1113 {
1114         /* Un-register LE event handler */
1115         _bt_service_unregister_event_handler_callback(BT_ADAPTER_LE_MODULE);
1116 }
1117
1118 static void __bt_le_update_discovery_status(bt_adapter_discovery_state_t status)
1119 {
1120         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_le_discovery_state, status);
1121         adapter_le_discovery_state = status;
1122 }
1123
1124 static int __bt_le_state_handle_request(gboolean enable)
1125 {
1126         int result = BLUETOOTH_ERROR_NONE;
1127
1128         switch (_bt_adapter_get_le_status()) {
1129         case BT_LE_ACTIVATING: {
1130                 BT_INFO("LE is currently in activating state, state [%d]",
1131                                 _bt_adapter_get_le_status());
1132                 if (enable) {
1133                         return BLUETOOTH_ERROR_IN_PROGRESS;
1134                 } else {
1135                         if (adapter_le_discovery_state == LE_DISCOVERY_STARTED ||
1136                                         adapter_le_discovery_state == LE_DISCOVERY_STARTING) {
1137                                 /*TODO Stop Discovery*/
1138                                 __bt_le_update_discovery_status(FALSE);
1139                         }
1140                         result = le_disable();  //change
1141                         if (result != OAL_STATUS_SUCCESS) {
1142                                 BT_ERR("LE_enable failed: [%d]", result);
1143                                 result = _bt_convert_oal_status_to_bt_error(result);
1144                                 /*TODO: perform if anything more needs to be done to handle failure */
1145                         } else {
1146                                 /* TODO: To be handled */
1147                                 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1148                                 result = BLUETOOTH_ERROR_NONE;
1149                         }
1150                 }
1151                 break;
1152         }
1153         case BT_LE_ACTIVATED: {
1154                 BT_INFO("LE is currently in activated state, state [%d]",
1155                                 _bt_adapter_get_le_status());
1156                 if (enable) {
1157                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1158                 } else {
1159                         if (adapter_le_discovery_state == LE_DISCOVERY_STARTED ||
1160                                         adapter_le_discovery_state == LE_DISCOVERY_STARTING) {
1161                                 /*TODO Stop Discovery*/
1162                                 __bt_le_update_discovery_status(FALSE);
1163                         }
1164                         result = le_disable();
1165                         if (result != OAL_STATUS_SUCCESS) {
1166                                 BT_ERR("LE_enable failed: [%d]", result);
1167                                 result = _bt_convert_oal_status_to_bt_error(result);
1168                                 /*TODO: perform if anything more needs to be done to handle failure */
1169                         } else {
1170                                 /* TODO: To be handled */
1171                                 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1172                                 result = BLUETOOTH_ERROR_NONE;
1173                         }
1174                 }
1175                 break;
1176         }
1177         case BT_LE_DEACTIVATING: {
1178                 BT_INFO("LE is currently in deactivating state, state [%d]",
1179                                 _bt_adapter_get_le_status());
1180                 if (!enable) {
1181                         return BLUETOOTH_ERROR_IN_PROGRESS;
1182
1183                 } else {
1184                         result = le_enable();
1185                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1186                                 BT_ERR("LE_enable failed: [%d]", result);
1187                                 le_disable();
1188                                 result = _bt_convert_oal_status_to_bt_error(result);
1189                                 /*TODO: perform if anything more needs to be done to handle failure */
1190                         } else {
1191                                 /* TODO: To be handled */
1192                                 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1193                                 result = BLUETOOTH_ERROR_NONE;
1194                         }
1195                 }
1196                 break;
1197         }
1198         case BT_LE_DEACTIVATED: {
1199                 BT_INFO("LE is currently in deactivated state, state [%d]",
1200                                 _bt_adapter_get_le_status());
1201                 if (!enable) {
1202                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1203                 } else {
1204                         result = le_enable();
1205                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1206                                 BT_ERR("LE_enable failed: [%d]", result);
1207                                 le_disable();
1208                                 result = _bt_convert_oal_status_to_bt_error(result);
1209                                 /*TODO: perform if anything more needs to be done to handle failure */
1210                         } else {
1211                                 /* TODO: To be handled */
1212                                 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1213                                 result = BLUETOOTH_ERROR_NONE;
1214                         }
1215                 }
1216                 break;
1217         }
1218         default:
1219                 BT_ERR("Unknown state: %d", _bt_adapter_get_le_status());
1220                 break;
1221         }
1222
1223         if (enable && result == BLUETOOTH_ERROR_NONE) {
1224                 /* Adapter enable request is successful, setup event handlers */
1225                 _bt_service_register_event_handler_callback(
1226                                 BT_ADAPTER_LE_MODULE, __bt_le_event_handler);
1227                 _bt_device_state_handle_callback_set_request();
1228         }
1229         return result;
1230 }
1231
1232 int _bt_enable_adapter_le(void)
1233 {
1234         return __bt_le_state_handle_request(TRUE);
1235 }
1236
1237 int _bt_disable_adapter_le(void)
1238 {
1239         return __bt_le_state_handle_request(FALSE);
1240 }
1241
1242
1243 static gboolean __bt_is_factory_test_mode(void)
1244 {
1245         int mode = 0;
1246
1247         if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
1248                 BT_ERR("Get the DUT Mode fail");
1249                 return TRUE;
1250         }
1251
1252         if (mode != FALSE) {
1253                 BT_INFO("DUT Test Mode !!");
1254                 return TRUE;
1255         }
1256
1257         return FALSE;
1258 }
1259
1260 int _bt_set_advertising(const char *sender, int adv_handle, gboolean enable, gboolean use_reserved_slot)
1261 {
1262         BT_CHECK_PARAMETER(sender, return);
1263         int server_instance;
1264         bt_pending_adv_data_set_t *info = NULL;
1265         int result = OAL_STATUS_SUCCESS;
1266         BT_DBG("+");
1267
1268         if (__bt_is_factory_test_mode()) {
1269                 BT_ERR("Unable to start advertising in factory binary !!");
1270                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1271         }
1272
1273         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1274
1275         if (server_instance == -1) {
1276                 BT_DBG("No available slot for the current sender and handle combination");
1277
1278                 if (enable == FALSE) {
1279                         BT_ERR("Advertising not even enabled on adv handle [%d] sender [%s]", adv_handle, sender);
1280                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1281                 }
1282
1283                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1284
1285                 if (server_instance == -1) {
1286                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
1287                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1288                                 return BLUETOOTH_ERROR_INTERNAL;
1289                         else {
1290                                 /* Allocate a pending structure and mark Adv data set pending */
1291                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1292                                 info->adv_handle = adv_handle;
1293                                 info->sender = g_strdup(sender);
1294                                 info->is_data_set_pending = FALSE;
1295                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1296                                 return BLUETOOTH_ERROR_NONE;
1297                         }
1298                 }
1299         }
1300
1301         if (enable)
1302                 result = adapter_ble_multi_adv_enable(server_instance);
1303         else
1304                 result = adapter_ble_multi_adv_disable(server_instance);
1305         if (result != OAL_STATUS_SUCCESS) {
1306                 BT_ERR("OAL API adapter_ble_multi_adv_enable Fail %d", result);
1307                 return _bt_convert_oal_status_to_bt_error(result);
1308         }
1309
1310         /* Update adv handle in table */
1311         _bt_update_adv_handle(sender, adv_handle);
1312
1313         return BLUETOOTH_ERROR_NONE;
1314 }
1315
1316 int _bt_set_custom_advertising(const char *sender, int adv_handle,
1317                 gboolean enable, bluetooth_advertising_params_t *params, gboolean use_reserved_slot)
1318 {
1319         BT_CHECK_PARAMETER(sender, return);
1320         int server_instance;
1321         bt_pending_adv_data_set_t *info = NULL;
1322         int result = OAL_STATUS_SUCCESS;
1323
1324         if (__bt_is_factory_test_mode()) {
1325                 BT_ERR("Unable to start advertising in factory binary !!");
1326                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1327         }
1328
1329         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1330
1331         if (server_instance == -1) {
1332                 BT_DBG("No available slot for the current sender and handle combination");
1333
1334                 if (enable == FALSE) {
1335                         BT_ERR("Advertising not even enabled on adv handle [%d] sender [%s]", adv_handle, sender);
1336                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1337                 }
1338                 /* Below logic is only valid only when enabling advertising */
1339                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1340
1341                 if (server_instance == -1) {
1342                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
1343                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1344                                 return BLUETOOTH_ERROR_INTERNAL;
1345                         else {
1346                                 /* Allocate a pending structure and mark Adv data set pending */
1347                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1348                                 info->adv_handle = adv_handle;
1349                                 info->sender = g_strdup(sender);
1350                                 info->is_custom_adv = TRUE;
1351                                 info->is_data_set_pending = FALSE;
1352                                 memcpy(&info->params, params, sizeof(bluetooth_advertising_params_t));
1353                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1354                                 return BLUETOOTH_ERROR_NONE;
1355                         }
1356                 }
1357         }
1358
1359         if (enable) {
1360                 /* Set the Advertising filter policy Parameter in HAL */
1361                 result = adapter_ble_set_filter_policy(params->filter_policy);
1362
1363                 if (result != OAL_STATUS_SUCCESS){
1364                         BT_ERR("OAL API adapter_ble_set_filter_policy Fail %d", result);
1365                         return _bt_convert_oal_status_to_bt_error(result);
1366                 }
1367                 /* Set Advertising parameters to Stack */
1368                 result = adapter_ble_multi_adv_update(server_instance, params->interval_min, params->interval_max,
1369                                 params->type, BT_ADV_DEFAULT_CHANNEL_MAP, params->tx_power_level, BT_ADV_DEFAULT_TIMEOUT);
1370                 if (result != OAL_STATUS_SUCCESS) {
1371                         BT_ERR("OAL API adapter_ble_multi_adv_update Fail %d", result);
1372                         return _bt_convert_oal_status_to_bt_error(result);
1373                 }
1374                 /* Start Advertising when Adv update event is received */
1375                 result = adapter_ble_multi_adv_enable(server_instance);
1376                 if (result != OAL_STATUS_SUCCESS) {
1377                         BT_ERR("OAL API adapter_ble_multi_adv_enable Fail %d", result);
1378                         return _bt_convert_oal_status_to_bt_error(result);
1379                 }
1380                 /* Update adv handle in table */
1381                 _bt_update_adv_handle(sender, adv_handle);
1382
1383                 return BLUETOOTH_ERROR_NONE;
1384         } else
1385                 result = adapter_ble_multi_adv_disable(server_instance);
1386
1387         if (result != OAL_STATUS_SUCCESS) {
1388                 BT_ERR("OAL API adapter_ble_multi_adv_disable Fail %d", result);
1389                 return _bt_convert_oal_status_to_bt_error(result);
1390         }
1391         return BLUETOOTH_ERROR_NONE;
1392 }
1393
1394 int _bt_get_advertising_data(char *sender, int adv_handle, bluetooth_advertising_data_t *adv, int *length)
1395 {
1396         BT_CHECK_PARAMETER(adv, return);
1397         BT_CHECK_PARAMETER(length, return);
1398         BT_CHECK_PARAMETER(sender, return);
1399
1400         int server_instance;
1401
1402         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
1403
1404         if (server_instance == -1) {
1405                 BT_DBG("No available slot for the current sender and handle combination");
1406                 return BLUETOOTH_ERROR_INTERNAL;
1407         }
1408
1409         _bt_get_previous_adv_data(adv, length, server_instance);
1410         BT_DBG("ADV Data length [%d] Server Instance [%d] Adv handle [%d]", *length, server_instance, adv_handle);
1411
1412         return BLUETOOTH_ERROR_NONE;
1413 }
1414
1415 int _bt_get_scan_response_data(char *sender, int adv_handle, bluetooth_scan_resp_data_t *response, int *length)
1416 {
1417         BT_CHECK_PARAMETER(response, return);
1418         BT_CHECK_PARAMETER(length, return);
1419         BT_CHECK_PARAMETER(sender, return);
1420
1421         int server_instance;
1422
1423         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
1424
1425         if (server_instance == -1) {
1426                 BT_DBG("No available slot for the current sender and handle combination");
1427                 return BLUETOOTH_ERROR_INTERNAL;
1428         }
1429
1430         _bt_get_previous_scan_rsp_data(response, length, server_instance);
1431         BT_DBG("SCAN RSP Data length [%d] Server Instance [%d] Adv handle [%d]", *length, server_instance, adv_handle);
1432
1433         return BLUETOOTH_ERROR_NONE;
1434 }
1435
1436 void print_adv_data(unsigned char * adv_data, int len)
1437 {
1438         char adv_data_str[(31 * 2) + 1];
1439
1440         for (int i = 0; i < len ; i++)
1441                 snprintf(&adv_data_str[i * 2], 3, "%02X", adv_data[i]);
1442
1443         BT_INFO("data: [%s]", adv_data_str);
1444 }
1445
1446
1447 static int __bt_set_multi_adv_param(oal_ble_multi_adv_param_setup_t *adv_setup,
1448                 bluetooth_advertising_data_t *adv_data, int length)
1449 {
1450         unsigned char *ptr;
1451         int num_uuids;
1452
1453         retv_if(NULL == adv_data, BLUETOOTH_ERROR_INVALID_PARAM);
1454         retv_if(NULL == adv_setup, BLUETOOTH_ERROR_INVALID_PARAM);
1455
1456         for (ptr = adv_data->data; NULL != ptr && length > 0;) {
1457                 int len = ptr[0];
1458                 int type = ptr[1];
1459
1460                 BT_DBG("len: %d, type: 0x%x", len, type);
1461
1462                 switch (type) {
1463                 case 0xFF: /* Manufacturer Data */
1464                         adv_setup->manufacturer_data = g_malloc0(sizeof(char) * (len - 1));
1465                         memcpy(adv_setup->manufacturer_data, (ptr + 2), (len - 1));
1466                         adv_setup->manufacturer_data_len = len - 1;
1467                         break;
1468                 case 0x15: /* 128 bit Solicit UUID */
1469                         adv_setup->solicit_uuid = g_malloc0(sizeof(char) * (len - 1));
1470                         memcpy((adv_setup->solicit_uuid), (ptr + 2), (len - 1));
1471                         adv_setup->solicit_uuid_len = len;
1472                         break;
1473                 case 0x06: /* 128 bit Service UUID */
1474                         adv_setup->service_uuid = g_malloc0(sizeof(char) * (len - 1));
1475                         memcpy((adv_setup->service_uuid), (ptr + 2), (len - 1));
1476                         adv_setup->service_uuid_len = len;
1477                         break;
1478                         case 0x14: {  /* 16 bit Solicit UUID */
1479                         int c;
1480                         num_uuids = (len -1)/2;
1481                         adv_setup->solicit_uuid = g_malloc0(sizeof(char) * 16 * num_uuids);
1482                         char *tmp = adv_setup->solicit_uuid;
1483                         adv_setup->solicit_uuid_len = 0;
1484
1485                         for (c = 1; c <= num_uuids; c++) {
1486                                 adv_setup->solicit_uuid_len += 16;;
1487                                 memcpy(tmp, BASE_UUID_CONVERTED, BT_UUID_128);
1488                                 memcpy(tmp+12, &ptr[c*2/* Byte Length*/], 2/* Byte Length */);
1489
1490                                 if (c < num_uuids)
1491                                         tmp += 16;
1492                         }
1493                         break;
1494                 }
1495                 case 0x02: { /* 16 bit Service UUID */
1496                         int c;
1497                         num_uuids = (len -1)/2;
1498                         adv_setup->service_uuid = g_malloc0(sizeof(char) * 16 * num_uuids);
1499                         char *tmp = adv_setup->service_uuid;
1500                         adv_setup->service_uuid_len = 0;
1501
1502                         for (c = 1; c <= num_uuids; c++) {
1503                                 adv_setup->service_uuid_len += 16;;
1504                                 memcpy(tmp, BASE_UUID_CONVERTED, BT_UUID_128);
1505                                 memcpy(tmp+12, &ptr[c*2/* Byte Length */], 2/* Byte Length */);
1506
1507                                 if (c < num_uuids)
1508                                         tmp += 16;
1509                         }
1510                         break;
1511                 }
1512                 case 0x16: { /* Service Data */
1513                         if (adv_setup->service_data == NULL) {
1514                                 /* first service data  */
1515                                 adv_setup->service_data = g_malloc0(sizeof(char) * (len));
1516                                 adv_setup->service_data[0] = len -1;  /*length1 + service_data1*/
1517                                 memcpy(adv_setup->service_data + 1, (ptr + 2), (len - 1));
1518                                 adv_setup->service_data_len = len;
1519                         } else {
1520                                 /* Next service data */
1521                                 char *prev_service_data = adv_setup->service_data;
1522                                 int prev_service_len = adv_setup->service_data_len;
1523
1524                                 /* create new memory */
1525                                 adv_setup->service_data = g_malloc0(sizeof(char) * (len + prev_service_len));
1526                                 memcpy(adv_setup->service_data, prev_service_data, prev_service_len);
1527                                 adv_setup->service_data[prev_service_len] = len -1;  /*length2 + service_data2*/
1528                                 memcpy(adv_setup->service_data + prev_service_len + 1, (ptr + 2), (len - 1));
1529
1530                                 adv_setup->service_data_len = prev_service_len + len;
1531
1532                                 /* remove the existing memory */
1533                                 g_free(prev_service_data);
1534                         }
1535                         BT_INFO("service data is packed:");
1536                         print_adv_data((unsigned char *)adv_setup->service_data, adv_setup->service_data_len);
1537                         break;
1538                 }
1539                 case 0x21: {
1540                         BT_INFO("128 Bit Service Data Not Supported!!");
1541                         break;
1542                 }
1543                 case 0x0A: {
1544                         adv_setup->include_txpower = 1;
1545                         break;
1546                 }
1547                 case 0x09:
1548                 case 0x08: {
1549                         adv_setup->include_name = 1;
1550                         break;
1551                 }
1552                 case 0x19: {
1553 #ifdef TIZEN_BT_HAL
1554                         adv_setup->include_appearance = 1;
1555 #endif
1556                         memcpy(&adv_setup->appearance, (ptr + 2), (len - 1));
1557                         break;
1558                 }
1559                 default:
1560                         BT_ERR("Unknown type: %x", type);
1561                         break;
1562         }
1563
1564                 length -= len + 1;
1565                 ptr += len + 1;
1566         }
1567
1568         return BLUETOOTH_ERROR_NONE;
1569 }
1570
1571 int _bt_set_advertising_data(const char *sender, int adv_handle,
1572                 bluetooth_advertising_data_t *adv, int length, gboolean use_reserved_slot)
1573 {
1574         BT_CHECK_PARAMETER(adv, return);
1575         BT_CHECK_PARAMETER(sender, return);
1576         bt_pending_adv_data_set_t *info = NULL;
1577         int server_instance;
1578         bluetooth_advertising_data_t adv_old;
1579         int adv_data_len;
1580         char *old_mdata = NULL;
1581         int old_len = 0;
1582         GVariant *ad_data, *param = NULL;
1583         oal_ble_multi_adv_param_setup_t adv_setup;
1584         int result = OAL_STATUS_SUCCESS;
1585         BT_DBG("+");
1586
1587         if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1588                 return BLUETOOTH_ERROR_INVALID_PARAM;
1589
1590         if (__bt_is_factory_test_mode()) {
1591                 BT_ERR("Unable to set advertising data in factory binary !!");
1592                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1593         }
1594
1595         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1596
1597         if (server_instance == -1) {
1598                 BT_DBG("No available slot for the current sender and handle combination");
1599
1600                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1601
1602                 if (server_instance == -1) {
1603                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
1604                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1605                                 return BLUETOOTH_ERROR_INTERNAL;
1606                         else {
1607                                 /* Allocate a pending structure and mark Adv data set pending */
1608                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1609                                 info->adv_handle = adv_handle;
1610                                 info->sender = g_strdup(sender);
1611                                 info->data_len = length;
1612                                 info->is_adv = TRUE;
1613                                 info->is_data_set_pending = TRUE;
1614                                 memcpy(&info->data, &(adv->data[0]), length);
1615                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1616                                 return BLUETOOTH_ERROR_NONE;
1617                         }
1618                 }
1619         }
1620
1621         /* Server Instance is already allocated, set Adv data to stack */
1622         /* First check if adv data is already present for slot server_instance*/
1623         memset(&adv_old.data, 0, sizeof(adv_old.data));
1624         _bt_get_previous_adv_data(&adv_old, &adv_data_len, server_instance);
1625
1626         /* Send Data to stack */
1627         memset(&adv_setup, 0, sizeof(oal_ble_multi_adv_param_setup_t));
1628
1629         if (BLUETOOTH_ERROR_NONE !=
1630                         __bt_set_multi_adv_param(&adv_setup, adv, length)) {
1631                 if (adv_setup.manufacturer_data)
1632                         g_free(adv_setup.manufacturer_data);
1633                 if (adv_setup.service_uuid)
1634                         g_free(adv_setup.service_uuid);
1635                 if (adv_setup.service_data)
1636                         g_free(adv_setup.service_data);
1637                 return BLUETOOTH_ERROR_INTERNAL;
1638         }
1639         /* Set Scan response false */
1640         adv_setup.set_scan_rsp = FALSE;
1641
1642         /* Set Server instance */
1643         adv_setup.server_if = server_instance;
1644
1645         /* Set Server instance[Product Requirement] */
1646         adv_setup.tx_power = 4;
1647
1648         BT_DBG("Service UUID len [%d], service data len [%d] Solicit UUID len [%d]",
1649                                 adv_setup.solicit_uuid_len, adv_setup.service_uuid_len, adv_setup.service_data_len);
1650         result = adapter_ble_multi_adv_set_inst_data(server_instance, &adv_setup);
1651         if (result != OAL_STATUS_SUCCESS) {
1652                 BT_ERR("OAL API adapter_ble_multi_adv_set_inst_data Fail %d", result);
1653                 /* Free the data */
1654                 if (adv_setup.manufacturer_data)
1655                         g_free(adv_setup.manufacturer_data);
1656                 if (adv_setup.service_data)
1657                         g_free(adv_setup.service_data);
1658                 if (adv_setup.service_uuid)
1659                         g_free(adv_setup.service_uuid);
1660                 return BLUETOOTH_ERROR_INTERNAL;
1661         }
1662
1663         /* Data sent to Stack successfully, send manuf data changed event if applicable */
1664         if (adv_setup.manufacturer_data_len != 0) {
1665                 if (adv_data_len > 0) {
1666                         _bt_get_ad_data_by_type((char *)adv_old.data, adv_data_len, 0xff,
1667                                         &old_mdata, &old_len);
1668                 }
1669
1670                 if (old_len != adv_setup.manufacturer_data_len ||
1671                                 (old_mdata && adv_setup.manufacturer_data &&
1672                                  memcmp(old_mdata, adv_setup.manufacturer_data, adv_setup.manufacturer_data_len))) {
1673
1674                         ad_data = g_variant_new_from_data((const GVariantType *)"ay",
1675                                         adv_setup.manufacturer_data, adv_setup.manufacturer_data_len, TRUE, NULL, NULL);
1676                         param = g_variant_new("(@ay)", ad_data);
1677                         _bt_send_event(BT_ADAPTER_EVENT,
1678                                         BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
1679                                         param);
1680                 }
1681
1682                 g_free(old_mdata);
1683         }
1684
1685         /* Time to update new ADV data completely in Table */
1686         _bt_set_new_adv_data(adv, length, server_instance);
1687
1688         /* Free the data */
1689         if (adv_setup.manufacturer_data)
1690                 g_free(adv_setup.manufacturer_data);
1691         if (adv_setup.service_data)
1692                 g_free(adv_setup.service_data);
1693         if (adv_setup.service_uuid)
1694                 g_free(adv_setup.service_uuid);
1695
1696         /* Update adv handle in table */
1697         _bt_update_adv_handle(sender, adv_handle);
1698
1699         return BLUETOOTH_ERROR_NONE;
1700 }
1701
1702 int _bt_set_scan_response_data(const char *sender, int adv_handle,
1703                                 bluetooth_scan_resp_data_t *response, int length, gboolean use_reserved_slot)
1704 {
1705         BT_CHECK_PARAMETER(response, return);
1706         BT_CHECK_PARAMETER(sender, return);
1707         bt_pending_adv_data_set_t *info = NULL;
1708         bluetooth_scan_resp_data_t scan_rsp_old;
1709         int scan_rsp_data_len;
1710         GVariant *ad_data, *param = NULL;
1711         oal_ble_multi_adv_param_setup_t adv_setup;
1712         char *old_mdata = NULL;
1713         int old_len = 0;
1714         int server_instance;
1715         int result = OAL_STATUS_SUCCESS;
1716         BT_DBG("+");
1717
1718         if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1719                 return BLUETOOTH_ERROR_INVALID_PARAM;
1720
1721         if (__bt_is_factory_test_mode()) {
1722                 BT_ERR("Unable to set Scan Response data in factory binary !!");
1723                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1724         }
1725
1726         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1727         if (server_instance == -1) {
1728                 BT_DBG("No available slot for the current sender and handle combination");
1729
1730                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1731
1732                 if (server_instance == -1) {
1733                         /* Internal Logic to register server instance if not initialized */
1734                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1735                                 return BLUETOOTH_ERROR_INTERNAL;
1736                         else {
1737                                 /* Allocate a pending structure and mark Adv data set pending */
1738                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1739                                 info->adv_handle = adv_handle;
1740                                 info->sender = g_strdup(sender);
1741                                 info->data_len = length;
1742                                 info->is_adv = FALSE;
1743                                 info->is_data_set_pending = TRUE;
1744                                 memcpy(&info->data, &(response->data[0]), length);
1745                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1746                                 return BLUETOOTH_ERROR_NONE;
1747                         }
1748                 }
1749         }
1750
1751         /* Server Instance is already allocated, set Adv data to stack */
1752         /* First check if adv data is already present for slot server_instance*/
1753         memset(&scan_rsp_old.data, 0, sizeof(scan_rsp_old.data));
1754         _bt_get_previous_scan_rsp_data(&scan_rsp_old, &scan_rsp_data_len, server_instance);
1755
1756         /* Send Data to stack */
1757         memset(&adv_setup, 0, sizeof(oal_ble_multi_adv_param_setup_t));
1758
1759         if (BLUETOOTH_ERROR_NONE !=
1760                         __bt_set_multi_adv_param(&adv_setup, (bluetooth_advertising_data_t*)response, length)) {
1761                 if (adv_setup.manufacturer_data)
1762                         g_free(adv_setup.manufacturer_data);
1763                 if (adv_setup.service_uuid)
1764                         g_free(adv_setup.service_uuid);
1765                 if (adv_setup.service_data)
1766                         g_free(adv_setup.service_data);
1767                 return BLUETOOTH_ERROR_INTERNAL;
1768         }
1769
1770         /* Set Scan response to TRUE */
1771         adv_setup.set_scan_rsp = TRUE;
1772
1773         /* Set Server instance */
1774         adv_setup.server_if = server_instance;
1775
1776         /* Set Server instance[Product Requirement] */
1777         adv_setup.tx_power = 4;
1778
1779         result = adapter_ble_multi_adv_set_inst_data(server_instance, &adv_setup);
1780         if (result != OAL_STATUS_SUCCESS) {
1781                 BT_ERR("OAL API adapter_ble_multi_adv_set_inst_data Fail %d", result);
1782                 /* Free the data */
1783                 if (adv_setup.manufacturer_data)
1784                         g_free(adv_setup.manufacturer_data);
1785                 if (adv_setup.service_data)
1786                         g_free(adv_setup.service_data);
1787                 if (adv_setup.service_uuid)
1788                         g_free(adv_setup.service_uuid);
1789                 return BLUETOOTH_ERROR_INTERNAL;
1790         }
1791
1792         /* Data sent to Stack successfully, send manuf data changed event if applicable */
1793         if (adv_setup.manufacturer_data_len != 0) {
1794                 if (scan_rsp_data_len > 0) {
1795                         _bt_get_ad_data_by_type((char *)scan_rsp_old.data, scan_rsp_data_len, 0xff,
1796                                         &old_mdata, &old_len);
1797                 }
1798
1799                 if (old_len != adv_setup.manufacturer_data_len ||
1800                                 (old_mdata && adv_setup.manufacturer_data &&
1801                                  memcmp(old_mdata, adv_setup.manufacturer_data, adv_setup.manufacturer_data_len))) {
1802
1803                         ad_data = g_variant_new_from_data((const GVariantType *)"ay",
1804                                         adv_setup.manufacturer_data, adv_setup.manufacturer_data_len, TRUE, NULL, NULL);
1805                         param = g_variant_new("(@ay)", ad_data);
1806                         _bt_send_event(BT_ADAPTER_EVENT,
1807                                         BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
1808                                         param);
1809                 }
1810
1811                 g_free(old_mdata);
1812         }
1813
1814         /* Time to update new Scan Response data completely in Table */
1815         _bt_set_new_scan_rsp_data(response, length, server_instance);
1816
1817         /* Free the data */
1818         if (adv_setup.manufacturer_data)
1819                 g_free(adv_setup.manufacturer_data);
1820         if (adv_setup.service_data)
1821                 g_free(adv_setup.service_data);
1822         if (adv_setup.service_uuid)
1823                 g_free(adv_setup.service_uuid);
1824
1825         /* Update adv handle in table */
1826         _bt_update_adv_handle(sender, adv_handle);
1827         return BLUETOOTH_ERROR_NONE;
1828 }
1829
1830 /*************************************** LE Scan APIs *********************************************/
1831 int _bt_set_scan_parameters(bluetooth_le_scan_params_t *params)
1832 {
1833         int itv = 0;
1834         int win = 0;
1835         int ret;
1836
1837         BT_CHECK_PARAMETER(params, return);
1838
1839         if (_bt_adapter_get_status() != BT_ACTIVATED &&
1840                         _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
1841                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1842         }
1843
1844         BT_DBG("inteval %f, win %f, type %d", params->interval, params->window, params->type);
1845
1846         if (params->interval < BT_LE_SCAN_INTERVAL_MIN ||
1847                         params->interval > BT_LE_SCAN_INTERVAL_MAX)
1848                 return BLUETOOTH_ERROR_INVALID_PARAM;
1849
1850         if (params->window < BT_LE_SCAN_WINDOW_MIN ||
1851                         params->window > BT_LE_SCAN_WINDOW_MAX)
1852                 return BLUETOOTH_ERROR_INVALID_PARAM;
1853
1854         if (params->window > params->interval)
1855                 return BLUETOOTH_ERROR_INVALID_PARAM;
1856
1857         itv = params->interval / BT_SCAN_INTERVAL_SPLIT;
1858         win = params->window / BT_SCAN_INTERVAL_SPLIT;
1859
1860         ret = gattc_set_le_scan_param(params->type, itv, win);
1861         if (OAL_STATUS_SUCCESS != ret) {
1862                 BT_ERR("gattc_set_le_scan_param failed");
1863                 return _bt_convert_oal_status_to_bt_error(ret);
1864         }
1865
1866         BT_INFO("Set scan parameters inteval %f, win %f, type %d",
1867                         itv * BT_SCAN_INTERVAL_SPLIT, win * BT_SCAN_INTERVAL_SPLIT, params->type);
1868
1869         return BLUETOOTH_ERROR_NONE;
1870 }
1871
1872 int _bt_prepare_scan_parameters(bluetooth_le_scan_params_t *params, int scan_type)
1873 {
1874         if (_bt_adapter_get_status() != BT_ACTIVATED &&
1875                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
1876                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1877         }
1878
1879         if (params)
1880                 BT_DBG("inteval %f, win %f", params->interval, params->window);
1881         else
1882                 BT_DBG("type %d", scan_type);
1883
1884         if (params) {
1885                 if (params->interval < BT_LE_SCAN_INTERVAL_MIN || params->interval > BT_LE_SCAN_INTERVAL_MAX)
1886                         return BLUETOOTH_ERROR_INVALID_PARAM;
1887
1888                 if (params->window < BT_LE_SCAN_WINDOW_MIN || params->window > BT_LE_SCAN_WINDOW_MAX)
1889                         return BLUETOOTH_ERROR_INVALID_PARAM;
1890
1891                 if (params->window > params->interval)
1892                         return BLUETOOTH_ERROR_INVALID_PARAM;
1893
1894                 le_scan_params.interval = params->interval;
1895                 le_scan_params.window = params->window;
1896         } else {
1897                 le_scan_params.type = scan_type;
1898                 if (le_scan_params.interval == 0) {
1899                         /* Set default scan interval same with BT_ADAPTER_LE_SCAN_MODE_LOW_ENERGY */
1900                         le_scan_params.interval = 5120;
1901                         le_scan_params.window = 512;
1902                 }
1903         }
1904
1905         is_le_set_scan_parameter = TRUE;
1906
1907         return BLUETOOTH_ERROR_NONE;
1908 }
1909
1910 gboolean __send_le_scan_reply(gpointer data)
1911 {
1912         int value = (intptr_t)data;
1913
1914         if (0 == value) {
1915                 BT_DBG("Send reply to BT_STOP_LE_DISCOVERY");
1916                 __bt_adapter_le_handle_pending_request_info(
1917                                 BLUETOOTH_ERROR_NONE,
1918                                 BT_STOP_LE_DISCOVERY, NULL, 0);
1919         } else {
1920                 BT_DBG("Send reply to BT_START_LE_DISCOVERY");
1921                 __bt_adapter_le_handle_pending_request_info(
1922                                 BLUETOOTH_ERROR_NONE,
1923                                 BT_START_LE_DISCOVERY, NULL, 0);
1924         }
1925
1926         return FALSE;
1927 }
1928
1929 static int __bt_get_available_scan_filter_slot_id(void)
1930 {
1931         GSList *l;
1932         bt_adapter_le_scanner_t *scanner;
1933         GSList *fl;
1934         bluetooth_le_scan_filter_t *filter_data;
1935         gboolean *slot_check_list = NULL;
1936         int i;
1937
1938         if (le_feature_info.max_filter == 0) {
1939                 BT_ERR("Scan filter is NOT Supported");
1940                 return -1;
1941         }
1942         slot_check_list = g_malloc0(sizeof(gboolean) * le_feature_info.max_filter);
1943
1944         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1945                 scanner = l->data;
1946                 for (fl = scanner->filter_list; fl != NULL; fl = g_slist_next(fl)) {
1947                         filter_data = fl->data;
1948                         if (filter_data->slot_id < le_feature_info.max_filter)
1949                                 slot_check_list[filter_data->slot_id] = TRUE;
1950                 }
1951         }
1952
1953         for (i = 0; i < le_feature_info.max_filter; i++) {
1954                 if (slot_check_list[i] == FALSE) {
1955                         g_free(slot_check_list);
1956                         return i;
1957                 }
1958         }
1959
1960         BT_ERR("There is NO available slot for scan filter.");
1961         g_free(slot_check_list);
1962         return -1;
1963 }
1964
1965 int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *filter)
1966 {
1967         int ret = BLUETOOTH_ERROR_NONE;
1968         bt_adapter_le_scanner_t *scanner = NULL;
1969         bluetooth_le_scan_filter_t *scan_filter_data = NULL;
1970         int slot_id;
1971
1972         slot_id = __bt_get_available_scan_filter_slot_id();
1973         if (slot_id == -1)
1974                 return BLUETOOTH_ERROR_NO_RESOURCES;
1975
1976         oal_ble_scan_filter_t filter_data = {.slot_id = slot_id, .device_address = (bt_address_t*)&filter->device_address, .device_name = filter->device_name,
1977                                                 .service_uuid = (oal_uuid_t*)&filter->service_uuid.data, .service_uuid_len = filter->service_uuid.data_len,
1978                                                 .service_uuid_mask = (oal_uuid_t*)&filter->service_uuid_mask.data, .service_uuid_mask_len = filter->service_uuid_mask.data_len,
1979                                                 .service_solicitation_uuid = (oal_uuid_t*)&filter->service_solicitation_uuid.data,
1980                                                 .service_solicitation_uuid_len = filter->service_solicitation_uuid.data_len,
1981                                                 .service_solicitation_uuid_mask = (oal_uuid_t*)&filter->service_solicitation_uuid_mask.data,
1982                                                 .service_solicitation_uuid_mask_len = filter->service_solicitation_uuid_mask.data_len,
1983                                                 .service_data = filter->service_data.data.data, .service_data_len = filter->service_data.data_len,
1984                                                 .service_data_mask = filter->service_data_mask.data.data, .service_data_mask_len = filter->service_data_mask.data_len,
1985                                                 .manufacturer_id = filter->manufacturer_id, .manufacturer_data = filter->manufacturer_data.data.data,
1986                                                 .manufacturer_data_len = filter->manufacturer_data.data_len, .manufacturer_data_mask = filter->manufacturer_data_mask.data.data,
1987                                                 .manufacturer_data_mask_len = filter->manufacturer_data_mask.data_len , .added_features = filter->added_features
1988                                         };
1989         ret = gattc_register_scan_filter(&filter_data);
1990
1991         if (OAL_STATUS_SUCCESS != ret) {
1992                 BT_ERR("gattc_register_scan_filter failed");
1993                 return _bt_convert_oal_status_to_bt_error(ret);
1994         }
1995
1996         scanner = __bt_find_scanner_from_list(sender);
1997
1998         if (scanner == NULL) {
1999                 scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
2000
2001                 if (scanner == NULL)
2002                         return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2003
2004                 scanner->sender = g_strdup(sender);
2005                 scanner_list = g_slist_append(scanner_list, scanner);
2006         }
2007
2008         scan_filter_data = g_malloc0(sizeof(bluetooth_le_scan_filter_t));
2009
2010         memcpy(scan_filter_data, filter, sizeof(bluetooth_le_scan_filter_t));
2011
2012         scan_filter_data->slot_id = slot_id;
2013         scanner->filter_list = g_slist_append(scanner->filter_list, scan_filter_data);
2014
2015         return ret;
2016 }
2017
2018 int _bt_unregister_all_scan_filters(const char* sender)
2019 {
2020         int ret;
2021         bt_adapter_le_scanner_t *scanner = NULL;
2022         bluetooth_le_scan_filter_t *filter_data = NULL;
2023         GSList *l;
2024
2025         scanner = __bt_find_scanner_from_list(sender);
2026
2027         if (scanner == NULL) {
2028                 BT_ERR("There is NO available scanner.");
2029                 return BLUETOOTH_ERROR_NOT_FOUND;
2030         }
2031         for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
2032                 filter_data = l->data;
2033
2034                 ret = gattc_unregister_scan_filter(filter_data->slot_id);
2035
2036                 if (OAL_STATUS_SUCCESS != ret)
2037                         BT_ERR("gattc_unregister_scan_filter failed for slot_id = [%d]", filter_data->slot_id);
2038         }
2039
2040         g_slist_free_full(scanner->filter_list, g_free);
2041         scanner->filter_list = NULL;
2042
2043         return BLUETOOTH_ERROR_NONE;
2044 }
2045
2046 int _bt_start_le_scan(const char *sender, uid_t uid, pid_t pid)
2047 {
2048         bt_adapter_le_scanner_t *scanner;
2049         int ret;
2050
2051         scanner = __bt_find_scanner_from_list(sender);
2052         if (!scanner) {
2053                 scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
2054                 retv_if(scanner == NULL, BLUETOOTH_ERROR_INTERNAL);
2055
2056                 scanner->sender = g_strdup(sender);
2057                 scanner->uid = uid;
2058                 scanner->pid = pid;
2059                 scanner_list = g_slist_append(scanner_list, scanner);
2060         }
2061
2062         BT_INFO("LE scan start request, Do not stop LE scan on mesh deinit");
2063         is_mesh_le_scan_stop_hold = false;
2064
2065         /* Check scanning is in progress or not */
2066         if (scanner->is_scanning) {
2067                 BT_ERR("BT is already in LE scanning");
2068                 return BLUETOOTH_ERROR_IN_PROGRESS;
2069         }
2070
2071         scanner->is_scanning = TRUE;
2072
2073         /* Check scanning is in progress or not by other users */
2074         if (_bt_is_le_scanning()) {
2075                 int value = 1;
2076                 BT_INFO("LE Full Scan is already on progress");
2077                 g_idle_add(__send_le_scan_reply, (void *)(intptr_t)value);
2078
2079                 /* Disable scan filter if filter is NULL */
2080                 if (scan_filter_enabled == TRUE) {
2081                         if (scanner->filter_list == NULL) {
2082                                 BT_INFO("Disable LE Scan Filter");
2083                                 ret = gattc_disable_scan_filter(0);
2084                                 if (ret != OAL_STATUS_SUCCESS)
2085                                         BT_ERR("gattc_disable_scan_filter failed");
2086                                 scan_filter_enabled = FALSE;
2087                         } else {
2088                                 BT_INFO("LE Filter Scan is continue");
2089                         }
2090                 } else {
2091                         BT_INFO("LE Full Scan is already on progress");
2092                 }
2093                 return BLUETOOTH_ERROR_NONE;
2094         } else {
2095                 if (is_le_set_scan_parameter == FALSE) {
2096                         /* Set default scan parameter same with BT_ADAPTER_LE_SCAN_MODE_LOW_ENERGY */
2097                         le_scan_params.type = BT_LE_ACTIVE_SCAN;
2098                         le_scan_params.interval = 5120;
2099                         le_scan_params.window = 512;
2100                         is_le_set_scan_parameter = TRUE;
2101                 }
2102                 _bt_set_scan_parameters(&le_scan_params);
2103
2104                 /* Enable scan filter if filter is exisiting */
2105                 if (scanner->filter_list == NULL) {
2106                         scan_filter_enabled = FALSE;
2107                 } else {
2108                         BT_INFO("Enable LE Scan Filter");
2109                         ret = gattc_enable_scan_filter(0);
2110                         if (ret != OAL_STATUS_SUCCESS)
2111                                 BT_ERR("gattc_enable_scan_filter failed");
2112                         scan_filter_enabled = TRUE;
2113                 }
2114         }
2115
2116         BT_INFO("Start LE Full Scan");
2117         ret = gattc_start_le_discovery(g_gatt_client_id);
2118
2119         if (OAL_STATUS_SUCCESS != ret) {
2120                 BT_ERR("gattc_start_le_discovery failed");
2121                 goto fail;
2122         }
2123
2124         return BLUETOOTH_ERROR_NONE;
2125
2126 fail:
2127         _bt_unregister_all_scan_filters(sender);
2128         scanner_list = g_slist_remove(scanner_list, scanner);
2129         __bt_free_le_scanner(scanner);
2130         return BLUETOOTH_ERROR_INTERNAL;
2131 }
2132
2133 int _bt_stop_le_scan(const char *sender)
2134 {
2135         int result = BLUETOOTH_ERROR_NONE;
2136         bt_adapter_le_scanner_t *scanner;
2137         gboolean next_scanning = FALSE;
2138         GSList *l;
2139         int ret;
2140
2141         /* Check scanning is in progress or not */
2142         scanner = __bt_find_scanner_from_list(sender);
2143         if (scanner == NULL)
2144                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2145
2146         if (scanner->is_scanning == FALSE)
2147                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2148
2149         scanner->is_scanning = FALSE;
2150
2151         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
2152                 bt_adapter_le_scanner_t *scanner_iter = l->data;
2153
2154                 if (scanner_iter->is_scanning == TRUE)
2155                         next_scanning = TRUE;
2156         }
2157
2158         if (next_scanning == TRUE) {
2159                 int value = 0;
2160
2161                 g_idle_add(__send_le_scan_reply, (void *)(intptr_t)value);
2162                 result = BLUETOOTH_ERROR_NONE;
2163         } else {
2164                 if (is_le_scan_hold == TRUE) {
2165                         BT_INFO("Just remove LE scan hold info");
2166                         _bt_set_le_scan_stop_requested(FALSE);
2167                         is_le_scan_hold = FALSE;
2168                         result = BLUETOOTH_ERROR_INTERNAL;
2169                 } else if (_bt_is_mesh_initialized()) {
2170                         /* Do not stop LE scan if BLE mesh running */
2171                         int value = 0;
2172                         g_idle_add(__send_le_scan_reply, (void *)(intptr_t)value);
2173
2174                         BT_INFO("Hold LE scan stop, sets to stop on mesh deinit");
2175                         _bt_set_le_scan_stop_requested(FALSE);
2176                         is_mesh_le_scan_stop_hold = true;
2177                         result = BLUETOOTH_ERROR_NONE;
2178                 } else {
2179                         BT_INFO("Just stop LE scan");
2180                         ret = gattc_stop_le_discovery(g_gatt_client_id);
2181                         if (OAL_STATUS_SUCCESS == ret) {
2182                                 _bt_set_le_scan_stop_requested(TRUE);
2183                         } else {
2184                                 BT_ERR("gattc_stop_le_discovery failed");
2185                                 result = BLUETOOTH_ERROR_INTERNAL;
2186                         }
2187                 }
2188
2189 // TODO: Disable scan filter
2190                 is_le_set_scan_parameter = FALSE;
2191
2192                 le_scan_params.type = BT_LE_ACTIVE_SCAN;
2193                 le_scan_params.interval = 0;
2194                 le_scan_params.window = 0;
2195         }
2196
2197         _bt_unregister_all_scan_filters(sender);
2198
2199         scanner_list = g_slist_remove(scanner_list, scanner);
2200         __bt_free_le_scanner(scanner);
2201
2202         return result;
2203 }
2204
2205 void _bt_hold_le_scan(void)
2206 {
2207         int ret;
2208
2209         if (_bt_is_le_scanning() == FALSE)
2210                 return;
2211
2212         BT_INFO("Hold le scan");
2213
2214         ret = gattc_stop_le_discovery(g_gatt_client_id);
2215         if (OAL_STATUS_SUCCESS == ret) {
2216                 _bt_set_le_scan_stop_requested(TRUE);
2217                 is_le_scan_hold = TRUE;
2218         } else
2219                 BT_ERR("gattc_stop_le_discovery failed");
2220
2221         if (scan_filter_enabled == TRUE) {
2222                 ret = gattc_disable_scan_filter(0);
2223                 if (ret != OAL_STATUS_SUCCESS)
2224                         BT_ERR("gattc_disable_scan_filter failed");
2225         }
2226 }
2227
2228 void _bt_restart_le_scan(void)
2229 {
2230         int ret;
2231
2232         if (is_le_scan_hold == FALSE)
2233                 return;
2234         is_le_scan_hold = FALSE;
2235         if (_bt_is_le_scanning() == FALSE)
2236                 return;
2237
2238         BT_INFO("Restart le scan");
2239
2240         if (is_le_set_scan_parameter == FALSE) {
2241                 /* Set default scan parameter same with BT_ADAPTER_LE_SCAN_MODE_LOW_ENERGY */
2242                 le_scan_params.type = BT_LE_ACTIVE_SCAN;
2243                 le_scan_params.interval = 5120;
2244                 le_scan_params.window = 512;
2245                 is_le_set_scan_parameter = TRUE;
2246         }
2247         _bt_set_scan_parameters(&le_scan_params);
2248
2249         if (scan_filter_enabled == TRUE) {
2250                 ret = gattc_enable_scan_filter(0);
2251                 if (ret != OAL_STATUS_SUCCESS)
2252                         BT_ERR("gattc_enable_scan_filter failed");
2253         }
2254
2255         ret = gattc_start_le_discovery(g_gatt_client_id);
2256         if (OAL_STATUS_SUCCESS != ret)
2257                 BT_ERR("gattc_start_le_discovery failed");
2258 }
2259
2260 void _bt_mesh_deinitialized() {
2261         int ret;
2262
2263         if (!is_mesh_le_scan_stop_hold) {
2264                 BT_INFO("Not required to stop scan");
2265                 return;
2266         }
2267
2268         BT_INFO("Gatt stop LE discovery was on hold for mesh");
2269         BT_INFO("stop LE discovery");
2270         ret = gattc_stop_le_discovery(g_gatt_client_id);
2271         if (OAL_STATUS_SUCCESS == ret) {
2272                 _bt_set_le_scan_stop_requested(TRUE);
2273         } else {
2274                 BT_ERR("gattc_stop_le_discovery failed");
2275         }
2276
2277         is_mesh_le_scan_stop_hold = false;
2278         return;
2279 }
2280
2281 static void _bt_disable_all_scanner_status(void)
2282 {
2283         GSList *l;
2284         bt_adapter_le_scanner_t *scanner;
2285
2286         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
2287                 scanner = l->data;
2288                 scanner->is_scanning = FALSE;
2289         }
2290 }
2291
2292 void _bt_check_le_scanner_app_termination(const char *sender)
2293 {
2294         bt_adapter_le_scanner_t *scanner;
2295
2296         scanner = __bt_find_scanner_from_list(sender);
2297         if (!scanner)
2298                 return;
2299
2300         _bt_bm_remove_scan_app(SCAN_LE, scanner->uid, scanner->pid);
2301
2302         if (scanner->is_scanning) {
2303                 /* Free 'scanner' into the function */
2304                 if (_bt_stop_le_scan(sender) != BLUETOOTH_ERROR_NOT_IN_OPERATION)
2305                         return;
2306         }
2307
2308         scanner_list = g_slist_remove(scanner_list, scanner);
2309         __bt_free_le_scanner(scanner);
2310 }
2311
2312 int _bt_service_le_init(void)
2313 {
2314         le_init();
2315
2316         return BLUETOOTH_ERROR_NONE;
2317 }
2318
2319 void _bt_service_le_deinit(void)
2320 {
2321         le_deinit();
2322         __bt_free_le_scanner_all();
2323 }
2324
2325 int _bt_is_advertising(void)
2326 {
2327         int ret = is_advertising();
2328
2329         if (ret == BLUETOOTH_ERROR_NONE)
2330                 return TRUE;
2331         else
2332                 return FALSE;
2333 }
2334
2335 gboolean _bt_is_le_2m_phy_supported(void)
2336 {
2337         if (le_feature_info.le_2m_phy)
2338                 return TRUE;
2339         else
2340                 return FALSE;
2341 }
2342
2343 gboolean _bt_is_le_coded_phy_supported(void)
2344 {
2345         if (le_feature_info.le_coded_phy)
2346                 return TRUE;
2347         else
2348                 return FALSE;
2349 }
2350
2351 gboolean _bt_is_scan_filter_supported(void)
2352 {
2353         if (le_feature_info.max_filter > 0)
2354                 return TRUE;
2355
2356         return FALSE;
2357 }
2358
2359 int _bt_set_le_privacy(gboolean set_privacy)
2360 {
2361         int result = BLUETOOTH_ERROR_NONE;
2362
2363         if (__bt_is_factory_test_mode()) {
2364                  BT_ERR("Unable to set le privacy in factory binary !!");
2365                  return BLUETOOTH_ERROR_NOT_SUPPORT;
2366         }
2367
2368         if (_bt_adapter_get_status() != BT_ACTIVATED &&
2369                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2370                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2371         }
2372
2373         result = adapter_ble_set_privacy(set_privacy);
2374         if (result != OAL_STATUS_SUCCESS) {
2375                 BT_ERR("adapter_set_le_privacy failed: %d", result);
2376                 result = _bt_convert_oal_status_to_bt_error(result);
2377         }
2378
2379         return result;
2380 }
2381
2382
2383 int _bt_set_white_list(bluetooth_device_address_t *device_address, int address_type, bool is_add)
2384 {
2385         int result = BLUETOOTH_ERROR_NONE;
2386         if (__bt_is_factory_test_mode()) {
2387                 if(is_add)
2388                         BT_ERR("Unable to add to white list in factory binary !!");
2389                 else
2390                         BT_ERR("Unable to remove white list in factory binary !!");
2391                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2392         }
2393
2394         BT_CHECK_PARAMETER(device_address, return);
2395
2396         if(_bt_adapter_get_status() != BT_ACTIVATED ){
2397                 BT_ERR("Bluetooth adapter is disabled");
2398                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2399         }
2400
2401         result = adapter_set_white_list((bt_address_t*)device_address, address_type, is_add);
2402         if (result != OAL_STATUS_SUCCESS) {
2403                 if (is_add)
2404                         BT_ERR("Add to White List Failed %d", result);
2405                 else
2406                         BT_ERR("Remove from white list Failed %d", result);
2407                 return _bt_convert_oal_status_to_bt_error(result);
2408         }
2409
2410         return result;
2411 }
2412
2413 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2414 {
2415         GVariant *manufac_data, *param = NULL;
2416         int result = BLUETOOTH_ERROR_NONE;
2417
2418         BT_CHECK_PARAMETER(m_data, return);
2419
2420         if (_bt_adapter_get_status() != BT_ACTIVATED &&
2421                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED)
2422                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2423
2424         result = adapter_set_manufacturer_data((oal_manufacturer_data_t*)m_data);
2425         if (result != OAL_STATUS_SUCCESS) {
2426                 BT_ERR("Set manufacturer data Failed %d", result);
2427                 return _bt_convert_oal_status_to_bt_error(result);
2428         }
2429         /*data has been successfully sent to the stack, inititate manufacturer data change event */
2430         manufac_data = g_variant_new_from_data((const GVariantType *)"ay",
2431                                         m_data->data, m_data->data_len, TRUE, NULL, NULL);
2432         param = g_variant_new("(@ay)", manufac_data);
2433         _bt_send_event(BT_ADAPTER_EVENT,
2434                         BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2435                         param);
2436
2437         return result;
2438 }
2439
2440 /*************************************** LE Scan APIs *********************************************/
2441
2442 int _bt_set_le_static_random_address(gboolean is_enable)
2443 {
2444         int result = BLUETOOTH_ERROR_NONE;
2445
2446         if (__bt_is_factory_test_mode()) {
2447                 BT_ERR("Unable to set le random address in factory binary !!");
2448                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2449         }
2450
2451         if (_bt_adapter_get_status() != BT_ACTIVATED &&
2452                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2453                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2454         }
2455
2456         result = adapter_set_le_static_random_address(is_enable);
2457         if (result != OAL_STATUS_SUCCESS) {
2458                 BT_ERR("adapter_set_le_static_random_address failed: %d", result);
2459                 result = _bt_convert_oal_status_to_bt_error(result);
2460         } else {
2461                 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2462                 result = BLUETOOTH_ERROR_NONE;
2463                 is_static_random_address = is_enable;
2464         }
2465
2466         return result;
2467 }
2468
2469 bool _bt_is_le_static_random_address_enabled(void)
2470 {
2471         return is_static_random_address;
2472 }