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