Fix SVACE issues in Tizen New BT Architecture
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / 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
37 #include <oal-hardware.h>
38 #include <oal-manager.h>
39 #include <oal-event.h>
40 #include <oal-adapter-mgr.h>
41 #include <oal-device-mgr.h>
42 #include <oal-gatt.h>
43
44 #define BT_UUID_128 16
45 #define BT_ADV_DEFAULT_TIMEOUT 0
46 #define BT_ADV_DEFAULT_TX_POWER 4
47 #define BT_ADV_DEFAULT_CHANNEL_MAP 0
48
49 static const char BASE_UUID_CONVERTED[BT_UUID_128] = {
50         0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
51         0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52 };
53
54 typedef struct {
55         int adv_handle;
56         char *sender;
57         int data_len;
58         unsigned char data[31];
59         gboolean is_adv;/* Adv or Scan Response: Only applicable if is_data_set_pending is TRUE */
60         gboolean is_data_set_pending; /* Data Set pending or Adv enable request at one time */
61         gboolean is_custom_adv; /* If Adv enable is custom adv enable request */
62         bluetooth_advertising_params_t params; /* Adv Parameters */
63 } bt_pending_adv_data_set_t;
64
65 static guint timer_id = 0;
66
67 static GSList *adv_data_pending_list = NULL;
68 static bt_le_status_t adapter_le_state = BT_LE_DEACTIVATED;
69 static bt_le_discovery_state_t adapter_le_discovery_state = LE_DISCOVERY_STOPPED;
70
71 /******************************************* LE Scan *********************************************/
72 #define BT_LE_SCAN_INTERVAL_MIN 2.5
73 #define BT_LE_SCAN_INTERVAL_MAX 10240
74 #define BT_LE_SCAN_WINDOW_MIN 2.5
75 #define BT_LE_SCAN_WINDOW_MAX 10240
76
77 #define BT_ADV_INTERVAL_SPLIT 0.625
78
79 typedef struct {
80         char *sender;
81         GSList *filter_list;
82         gboolean is_scanning;
83 } bt_adapter_le_scanner_t;
84
85 GSList *scanner_list = NULL;
86 static gboolean is_le_set_scan_parameter = FALSE;
87 static gboolean is_le_scanning = FALSE;
88 /******************************************* LE Scan *********************************************/
89
90 void _bt_adapter_set_le_status(bt_le_status_t status)
91 {
92         BT_INFO("adapter_le_state changed [%d] -> [%d]", adapter_le_state, status);
93         adapter_le_state = status;
94 }
95
96 bt_le_status_t _bt_adapter_get_le_status(void)
97 {
98         return adapter_le_state;
99 }
100
101
102 /* Internal functions of core adapter service */
103 static void __bt_le_handle_pending_requests(int service_function, void *user_data, unsigned int size)
104 {
105         GSList *l;
106         GArray *out_param;
107         invocation_info_t *req_info;
108         BT_INFO("+");
109
110         /* Get method invocation context */
111         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
112                 req_info = l->data;
113                 if (req_info == NULL || req_info->service_function != service_function)
114                         continue;
115
116                 /* Create out param */
117                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
118
119                 switch (service_function) {
120                 case BT_ENABLE_ADAPTER_LE:
121                 case BT_DISABLE_ADAPTER_LE: {
122                         gboolean done = TRUE;
123                         g_array_append_vals(out_param, &done, sizeof(gboolean));
124                         break;
125                 }
126                 default:
127                         BT_ERR("Unknown service function[%d]", service_function);
128                 }
129
130                 _bt_service_method_return(req_info->context, out_param, req_info->result);
131                 g_array_free(out_param, TRUE);
132                 /* Now free invocation info for this request*/
133                 _bt_free_info_from_invocation_list(req_info);
134         }
135 }
136
137
138 /* Request return handlings */
139 static gboolean __bt_le_post_set_enabled(gpointer user_data)
140 {
141         BT_INFO("__bt_adapter_post_set_enabled>>");
142
143         /* Add LE enabled post processing codes */
144
145         return FALSE;
146 }
147
148
149 static gboolean __bt_le_post_set_disabled(gpointer user_data)
150 {
151         BT_INFO("_bt_adapter_post_set_disabled>>");
152
153         /* Add LE disabled post processing codes */
154
155         return FALSE;
156 }
157
158 static void __bt_le_update_bt_enabled(void)
159 {
160         int result = BLUETOOTH_ERROR_NONE;
161         BT_ERR("_bt_adapter_update_bt_enabled>>");
162         /* Update Bluetooth Status to notify other modules */
163         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
164                 BT_ERR("Set vconf failed\n");
165
166         /* TODO:Add timer function to handle any further post processing */
167         g_idle_add((GSourceFunc)__bt_le_post_set_enabled, NULL);
168
169         /*Return BT_ADAPTER_ENABLE Method invocation context */
170         __bt_le_handle_pending_requests(BT_ENABLE_ADAPTER_LE, NULL, 0);
171         /*Send BT Enabled event to application */
172         _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
173                         g_variant_new("(i)", result));
174 }
175
176 static void __bt_le_update_bt_disabled(void)
177 {
178         int result = BLUETOOTH_ERROR_NONE;
179         BT_INFO("_bt_adapter_update_bt_disabled>>");
180
181         int power_off_status = 0;
182         int ret;
183
184         /* Update the vconf LE status in normal Deactivation case only */
185         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
186         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
187
188         /* Update LE Status to notify other modules */
189         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
190                 BT_ERR("Set vconf failed");
191
192         /* TODO:Add timer function to handle any further post processing */
193         g_idle_add((GSourceFunc)__bt_le_post_set_disabled, NULL);
194
195         /* Return BT_ADAPTER_DISABLE Method invocation context */
196         __bt_le_handle_pending_requests(BT_DISABLE_ADAPTER_LE, NULL, 0);
197
198         /* Send BT Disabled event to application */
199         _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
200                         g_variant_new("(i)", result));
201 }
202
203
204 static void __bt_set_le_scan_status(gboolean mode)
205 {
206         is_le_scanning = mode;
207 }
208
209 gboolean _bt_is_le_scanning(void)
210 {
211         return is_le_scanning;
212 }
213
214 bt_adapter_le_scanner_t* __bt_find_scanner_from_list(const char *sender)
215 {
216         GSList *l;
217         bt_adapter_le_scanner_t *scanner;
218
219         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
220                 scanner = l->data;
221                 if (g_strcmp0(scanner->sender, sender) == 0)
222                         return scanner;
223         }
224
225         return NULL;
226 }
227
228 /* Event handlers */
229 static void __bt_adapter_le_handle_pending_request_info(int result,
230                 int service_function, void *param, unsigned int size)
231 {
232         GSList *l;
233         GArray *out_param;
234         invocation_info_t *req_info = NULL;
235         BT_DBG("+");
236
237         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
238                 req_info = l->data;
239                 if (req_info == NULL || req_info->service_function != service_function)
240                         continue;
241
242                 switch (service_function) {
243                 case BT_SET_ADVERTISING:
244                 case BT_SET_CUSTOM_ADVERTISING:
245                 case BT_SET_ADVERTISING_DATA:
246                 case BT_SET_SCAN_RESPONSE_DATA: {
247                         int *saved_handle;
248                         bt_pending_adv_data_set_t *data;
249
250                         BT_DBG("Service function: [%d]", service_function);
251                         ret_if(param == NULL);
252
253                         saved_handle = (int*)req_info->user_data;
254                         data = (bt_pending_adv_data_set_t*)param;
255                         BT_DBG("Current Sender [%s] Current Handle [%d]", data->sender, data->adv_handle);
256
257                         if (!g_strcmp0(req_info->sender, data->sender) && (*saved_handle == data->adv_handle)) {
258                                 BT_DBG("Requester found [%s] ADV Handle [%d]", req_info->sender, *saved_handle);
259                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
260                                 g_array_append_vals(out_param, &data->adv_handle, sizeof(int));
261                                 _bt_service_method_return(req_info->context, out_param, result);
262
263                                 /* Free data */
264                                 g_free(req_info->user_data);
265                                 _bt_free_info_from_invocation_list(req_info);
266                                 g_array_free(out_param, TRUE);
267                         }
268                         break;
269                 }
270                 case BT_START_LE_DISCOVERY: {
271                         bt_adapter_le_scanner_t *scanner;
272
273                         BT_DBG("Request Sender: [%s]", req_info->sender);
274                         if (BLUETOOTH_ERROR_NONE != result) {
275                                 scanner = __bt_find_scanner_from_list(req_info->sender);
276                                 if (scanner && scanner->is_scanning)
277                                         scanner->is_scanning = FALSE;
278                         }
279
280                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
281                         _bt_service_method_return(req_info->context, out_param, result);
282                         _bt_free_info_from_invocation_list(req_info);
283                         g_array_free(out_param, TRUE);
284                         break;
285                 }
286                 case BT_STOP_LE_DISCOVERY: {
287                         BT_DBG("Request Sender: [%s]", req_info->sender);
288                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
289                         _bt_service_method_return(req_info->context, out_param, result);
290                         _bt_free_info_from_invocation_list(req_info);
291                         g_array_free(out_param, TRUE);
292                         break;
293                 }
294                 default:
295                         break;
296                 }
297         }
298         BT_DBG("-");
299 }
300
301 static void __bt_le_handle_server_instance_registered(event_gatts_register_t *data)
302 {
303         int adv_handle;
304         GSList *l;
305         char uuid_string[BLUETOOTH_UUID_STRING_MAX];
306         bluetooth_advertising_data_t adv;
307         bluetooth_scan_resp_data_t scan_rsp;
308         int result = BLUETOOTH_ERROR_NONE;
309         bt_pending_adv_data_set_t *info = NULL;
310
311         memset(adv.data, 0, sizeof(adv.data));
312         memset(scan_rsp.data, 0, sizeof(scan_rsp.data));
313
314         _bt_uuid_to_string(&(data->server_uuid), uuid_string);
315         BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
316
317         /* Get sender and adv handle for the server instance */
318         _bt_get_adv_handle_from_instance(data->server_inst, &adv_handle);
319
320         if (adv_handle > 0) {
321                 /* Check if pending Adv Data set or Scan response data set request is pending*/
322                 for (l = adv_data_pending_list; l != NULL; l = l->next) {
323                         info = l->data;
324
325                         if (info && (adv_handle == info->adv_handle)) {
326                                 if (info->is_data_set_pending) {
327                                         BT_INFO("LE Data set request is pending");
328                                         if (info->is_adv) {
329                                                 memcpy(&adv.data, info->data, info->data_len);
330                                                 result = _bt_set_advertising_data(info->sender,
331                                                         info->adv_handle, &adv, info->data_len, FALSE);
332                                                 if (result != BLUETOOTH_ERROR_NONE) {
333                                                         __bt_adapter_le_handle_pending_request_info(result,
334                                                                 BT_SET_ADVERTISING_DATA,
335                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
336                                                 }
337                                                 goto data_free;
338                                         } else {
339                                                 memcpy(&scan_rsp.data, info->data, info->data_len);
340                                                 result = _bt_set_scan_response_data(info->sender,
341                                                                 info->adv_handle, &scan_rsp, info->data_len, FALSE);
342                                                 if (result != BLUETOOTH_ERROR_NONE) {
343                                                         __bt_adapter_le_handle_pending_request_info(result,
344                                                                 BT_SET_SCAN_RESPONSE_DATA, (void*)info,
345                                                                 sizeof(bt_pending_adv_data_set_t));
346                                                 }
347                                                 goto data_free;
348                                         }
349                                 } else {
350                                         BT_INFO("LE Enable Adv request is pending");
351                                         if (info->is_custom_adv) {
352                                                 result = _bt_set_custom_advertising(info->sender, info->adv_handle,
353                                                                         TRUE, &info->params, FALSE/*Reserved Slot*/);
354                                                 if (result != BLUETOOTH_ERROR_NONE) {
355                                                         __bt_adapter_le_handle_pending_request_info(result,
356                                                                 BT_SET_CUSTOM_ADVERTISING,
357                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
358                                                 }
359                                                 goto data_free;
360                                         } else {
361                                                 result = _bt_set_advertising(info->sender, info->adv_handle,
362                                                                 TRUE, FALSE/*Reserved Slot*/);
363                                                 if (result != BLUETOOTH_ERROR_NONE) {
364                                                         __bt_adapter_le_handle_pending_request_info(result,
365                                                                 BT_SET_ADVERTISING,
366                                                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
367                                                 }
368                                                 goto data_free;
369                                         }
370                                 }
371                         }
372                 }
373         }
374         BT_DBG("-");
375         return;
376
377 data_free:
378         adv_data_pending_list = g_slist_remove(adv_data_pending_list, info);
379         g_free(info->sender);
380         g_free(info);
381 }
382
383 static void __bt_le_multi_advertising_enabled(event_ble_multiadv_status *event)
384 {
385         char *sender;
386         int adv_handle;
387         bt_pending_adv_data_set_t *info = NULL;
388         GVariant *param = NULL;
389         int result = BLUETOOTH_ERROR_NONE;
390
391         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
392         if (sender == NULL) {
393                 BT_ERR("Abnormal!!");
394         } else {
395                 if (event->status != OAL_STATUS_SUCCESS)
396                         result = BLUETOOTH_ERROR_INTERNAL;
397                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
398                 info->sender = sender;
399                 info->adv_handle = adv_handle;
400                 __bt_adapter_le_handle_pending_request_info(result,
401                                 BT_SET_CUSTOM_ADVERTISING,
402                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
403                 __bt_adapter_le_handle_pending_request_info(result,
404                                 BT_SET_ADVERTISING,
405                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
406
407                 /* Send event */
408                 param = g_variant_new("(ii)", result, info->adv_handle);
409                 _bt_send_event_to_dest(info->sender, BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ADVERTISING_STARTED, param);
410
411                 /* Free data */
412                 g_free(info->sender);
413                 g_free(info);
414         }
415 }
416
417 static void __bt_le_multi_advertising_disabled(event_ble_multiadv_status *event)
418 {
419         char *sender;
420         int adv_handle;
421         bt_pending_adv_data_set_t *info = NULL;
422         GVariant *param = NULL;
423         int result = BLUETOOTH_ERROR_NONE;
424         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
425
426         if (sender == NULL) {
427                 BT_INFO("Means application containing the adv info is already freed!!");
428         } else {
429                 if (event->status != OAL_STATUS_SUCCESS)
430                         result = BLUETOOTH_ERROR_INTERNAL;
431                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
432                 info->sender = sender;
433                 info->adv_handle = adv_handle;
434                 __bt_adapter_le_handle_pending_request_info(result,
435                                 BT_SET_CUSTOM_ADVERTISING,
436                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
437                 __bt_adapter_le_handle_pending_request_info(result,
438                                 BT_SET_ADVERTISING,
439                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
440                 /* Send event */
441                 param = g_variant_new("(ii)", result, info->adv_handle);
442                 _bt_send_event_to_dest(info->sender, BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ADVERTISING_STOPPED, param);
443
444                 /* Free allocated slot or server instance from stack to be used for other advertisng */
445                 result = _bt_unregister_server_instance(sender, adv_handle);
446
447                 /* Free data */
448                 g_free(info->sender);
449                 g_free(info);
450         }
451 }
452
453 static void __bt_le_multi_advertising_set_data(event_ble_multiadv_status *event)
454 {
455         char *sender = NULL;
456         int adv_handle;
457         bt_pending_adv_data_set_t *info = NULL;
458         int result = BLUETOOTH_ERROR_NONE;
459         sender = _bt_get_sender_and_handle(event->server_inst, &adv_handle);
460
461         if (sender == NULL) {
462                 BT_ERR("Abnormal!!");
463         } else {
464                 BT_DBG("Sender [%s], adv handle [%d]",  sender, adv_handle);
465                 if (event->status != OAL_STATUS_SUCCESS)
466                         result = BLUETOOTH_ERROR_INTERNAL;
467
468                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
469                 info->sender = sender;
470                 info->adv_handle = adv_handle;
471                 BT_DBG("Sender [%s], adv handle [%d]",  info->sender, info->adv_handle);
472                 __bt_adapter_le_handle_pending_request_info(result,
473                                 BT_SET_SCAN_RESPONSE_DATA,
474                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
475                 __bt_adapter_le_handle_pending_request_info(result,
476                                 BT_SET_ADVERTISING_DATA,
477                                 (void*)info, sizeof(bt_pending_adv_data_set_t));
478                 g_free(info->sender);
479                 g_free(info);
480         }
481 }
482
483 static int __get_advertisement_data(unsigned char *dest, unsigned char *src)
484 {
485         int length = 0;
486         int i = 0;
487
488         while (i <= 31) {
489                 int len = src[i];
490
491                 if ((len == 0) || (31 < (i + len))) {
492                         BT_INFO("length: %d", length);
493                         memcpy(dest, src, length);
494                         break;
495                 }
496
497                 length += (len + 1);
498                 i += (len + 1);
499         }
500
501         return length;
502 }
503
504 static void __bt_le_handle_device_found(event_ble_scan_result_info *scan_result)
505 {
506         int result = BLUETOOTH_ERROR_NONE;
507         bt_adapter_le_scanner_t *scanner = NULL;
508         char address[BT_ADDRESS_STRING_SIZE];
509         int addr_type = 0x01; /* TODO: Need to get correct address type */
510         unsigned char adv_ind_data[31];
511         unsigned char scan_resp_data[31];
512         int adv_ind_len = 0;
513         int scan_resp_len = 0;
514         char *tmp_str = NULL;
515         GVariant *scan_data_param;
516         GVariant *adv_data_param;
517         GVariant *param;
518         GSList *l;
519
520         BT_DBG("+");
521         ret_if(NULL == scan_result);
522
523         _bt_convert_addr_type_to_string(address, scan_result->address.addr);
524
525         memset(adv_ind_data, 0x00, sizeof(adv_ind_data));
526         adv_ind_len = __get_advertisement_data(
527                         adv_ind_data, &(scan_result->adv_data[0]));
528
529         memset(scan_resp_data, 0x00, sizeof(scan_resp_data));
530         scan_resp_len = __get_advertisement_data(
531                         scan_resp_data, &(scan_result->adv_data[adv_ind_len]));
532
533         BT_INFO("Address: %s, RSSI: %d, adv_ind_len: %d, scan_resp_len: %d",
534                         address, scan_result->rssi, adv_ind_len, scan_resp_len);
535
536         tmp_str = _bt_service_convert_hex_to_string(scan_result->adv_data, 62);
537         BT_INFO("Complete Data: [%s]", tmp_str);
538         g_free(tmp_str);
539         tmp_str = NULL;
540
541         tmp_str = _bt_service_convert_hex_to_string(adv_ind_data, adv_ind_len);
542         BT_INFO("Adv Ind Data: [%s]", tmp_str);
543         g_free(tmp_str);
544         tmp_str = NULL;
545
546         tmp_str = _bt_service_convert_hex_to_string(scan_resp_data, scan_resp_len);
547         BT_INFO("Scan resp Data: [%s]", tmp_str);
548         g_free(tmp_str);
549         tmp_str = NULL;
550
551         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
552                 scanner = l->data;
553                 if (scanner->is_scanning == FALSE)
554                         continue;
555
556                 adv_data_param = g_variant_new_from_data((const GVariantType *)"ay",
557                                 adv_ind_data, adv_ind_len, TRUE, NULL, NULL);
558                 scan_data_param = g_variant_new_from_data((const GVariantType *)"ay",
559                                 scan_resp_data, scan_resp_len, TRUE, NULL, NULL);
560
561                 param = g_variant_new("(isnnn@ayn@ay)",
562                                 result, address,
563                                 addr_type, scan_result->rssi,
564                                 adv_ind_len, adv_data_param,
565                                 scan_resp_len, scan_data_param);
566
567                 _bt_send_event_to_dest(scanner->sender, BT_LE_ADAPTER_EVENT,
568                                 BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND, param);
569         }
570
571         BT_DBG("-");
572 }
573
574 static void __bt_le_state_change_callback(int bt_status)
575 {
576         BT_INFO("__bt_le_state_change_callback: status [%d]", bt_status);
577
578         switch (bt_status) {
579         case BT_LE_DEACTIVATED:
580                 _bt_adapter_set_le_status(bt_status);
581
582                 /* Add Adapter disabled post processing codes */
583                 __bt_le_update_bt_disabled();
584                 break;
585         case BT_LE_ACTIVATED:
586                 _bt_adapter_set_le_status(bt_status);
587
588                 /* Add Adapter enabled post processing codes */
589                 if (timer_id > 0) {
590                         BT_DBG("g_source is removed");
591                         g_source_remove(timer_id);
592                         timer_id = 0;
593                 }
594                 __bt_le_update_bt_enabled();
595                 break;
596         default:
597                 BT_ERR("Incorrect Bluetooth adapter state changed status");
598                 break;
599
600         }
601 }
602
603 static void __bt_le_event_handler(int event_type, gpointer event_data)
604 {
605         BT_INFO("OAL event = 0x%x, \n", event_type);
606
607         switch (event_type) {
608         case OAL_EVENT_BLE_ENABLED: {
609                 __bt_le_state_change_callback(BT_LE_ACTIVATED);
610                 break;
611         }
612         case OAL_EVENT_BLE_DISABLED: {
613                 __bt_le_state_change_callback(BT_LE_DEACTIVATED);
614                 break;
615         }
616         case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
617                 BT_INFO("OAL Event: Server Instance Registered");
618                 event_gatts_register_t* event = g_memdup(event_data, sizeof(event_gatts_register_t));
619                 /* GATT Server Registered event is handled in MAIN thread context */
620                 __bt_le_handle_server_instance_registered(event);
621                 break;
622         }
623         case OAL_EVENT_BLE_ADVERTISING_STARTED: {
624                 BT_INFO("OAL Event: Legacy Advertising Enabled: Not Supported!!");
625                 break;
626         }
627         case OAL_EVENT_BLE_ADVERTISING_STOPPED: {
628                 BT_INFO("OAL Event: Legacy Advertising Disabled: Not Supported!!");
629                 break;
630         }
631         case OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE: {
632                 BT_INFO("OAL Event: Advertising Enabled");
633                 event_ble_multiadv_status* event = (event_ble_multiadv_status*)g_memdup(event_data,
634                                                                 sizeof(event_ble_multiadv_status));
635                 __bt_le_multi_advertising_enabled(event);
636                 break;
637         }
638         case OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE: {
639                 BT_INFO("OAL Event: Advertising Disabled");
640                 event_ble_multiadv_status* event = (event_ble_multiadv_status*)g_memdup(event_data,
641                                                                 sizeof(event_ble_multiadv_status));
642                 __bt_le_multi_advertising_disabled(event);
643                 break;
644         }
645         case OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA: {
646                 BT_INFO("OAL Event: Advertising Data set successfully");
647                 event_ble_multiadv_status* event = (event_ble_multiadv_status*)g_memdup(event_data,
648                                                                 sizeof(event_ble_multiadv_status));
649                 __bt_le_multi_advertising_set_data(event);
650                 break;
651         }
652         case OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE: {
653                         BT_INFO("OAL Event: Advertising Params updated");
654                 break;
655         }
656         case OAL_EVENT_BLE_DISCOVERY_STARTED: {
657                 __bt_set_le_scan_status(TRUE);
658
659                 __bt_adapter_le_handle_pending_request_info(
660                                 BLUETOOTH_ERROR_NONE,
661                                 BT_START_LE_DISCOVERY, NULL, 0);
662                 break;
663         }
664         case OAL_EVENT_BLE_DISCOVERY_STOPPED: {
665                 if (!_bt_is_le_scanning()) {
666                         BT_ERR("LE discovery start failed");
667                         __bt_adapter_le_handle_pending_request_info(
668                                         BLUETOOTH_ERROR_INTERNAL,
669                                         BT_START_LE_DISCOVERY, NULL, 0);
670                         break;
671                 }
672
673                 __bt_set_le_scan_status(FALSE);
674                 __bt_adapter_le_handle_pending_request_info(
675                                 BLUETOOTH_ERROR_NONE,
676                                 BT_STOP_LE_DISCOVERY, NULL, 0);
677                 break;
678         }
679         case OAL_EVENT_BLE_REMOTE_DEVICE_FOUND: {
680                 event_ble_scan_result_info *scan_result = event_data;
681                 BT_INFO("OAL Event: OAL_EVENT_BLE_REMOTE_DEVICE_FOUND");
682                 __bt_le_handle_device_found(scan_result);
683                 break;
684         }
685         default:
686                 break;
687         }
688 }
689
690 int _bt_le_init(void)
691 {
692         BT_DBG("+");
693         /* Register LE event handler */
694         _bt_service_register_event_handler_callback(BT_ADAPTER_LE_MODULE, __bt_le_event_handler);
695         BT_DBG("-");
696         return BLUETOOTH_ERROR_NONE;
697 }
698
699 void _bt_le_deinit(void)
700 {
701         BT_DBG("+");
702         /* Un-register LE event handler */
703         _bt_service_unregister_event_handler_callback(BT_ADAPTER_LE_MODULE);
704         BT_DBG("-");
705 }
706
707 static void __bt_le_update_discovery_status(bt_adapter_discovery_state_t status)
708 {
709         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_le_discovery_state, status);
710         adapter_le_discovery_state = status;
711 }
712
713 static int __bt_le_state_handle_request(gboolean enable)
714 {
715         int result = BLUETOOTH_ERROR_NONE;
716         BT_DBG("");
717
718         switch (_bt_adapter_get_le_status()) {
719         case BT_LE_ACTIVATING: {
720                 BT_INFO("LE is currently in activating state, state [%d]",
721                                 _bt_adapter_get_le_status());
722                 if (enable) {
723                         return BLUETOOTH_ERROR_IN_PROGRESS;
724                 } else {
725                         if (adapter_le_discovery_state == LE_DISCOVERY_STARTED ||
726                                         adapter_le_discovery_state == LE_DISCOVERY_STARTING) {
727                                 /*TODO Stop Discovery*/
728                                 __bt_le_update_discovery_status(FALSE);
729                         }
730                         result = le_disable();  //change
731                         if (result != OAL_STATUS_SUCCESS) {
732                                 BT_ERR("LE_enable failed: [%d]", result);
733                                 result = BLUETOOTH_ERROR_INTERNAL;
734                                 /*TODO: perform if anything more needs to be done to handle failure */
735                         } else {
736                                 /* TODO: To be handled */
737                                 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
738                                 result = BLUETOOTH_ERROR_NONE;
739                         }
740                 }
741                 break;
742         }
743         case BT_LE_ACTIVATED: {
744                 BT_INFO("LE is currently in activated state, state [%d]",
745                                 _bt_adapter_get_le_status());
746                 if (enable) {
747                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
748                 } else {
749                         if (adapter_le_discovery_state == LE_DISCOVERY_STARTED ||
750                                         adapter_le_discovery_state == LE_DISCOVERY_STARTING) {
751                                 /*TODO Stop Discovery*/
752                                 __bt_le_update_discovery_status(FALSE);
753                         }
754                         result = le_disable();
755                         if (result != OAL_STATUS_SUCCESS) {
756                                 BT_ERR("LE_enable failed: [%d]", result);
757                                 result = BLUETOOTH_ERROR_INTERNAL;
758                                 /*TODO: perform if anything more needs to be done to handle failure */
759                         } else {
760                                 /* TODO: To be handled */
761                                 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
762                                 result = BLUETOOTH_ERROR_NONE;
763                         }
764                 }
765                 break;
766         }
767         case BT_LE_DEACTIVATING: {
768                 BT_INFO("LE is currently in deactivating state, state [%d]",
769                                 _bt_adapter_get_le_status());
770                 if (!enable) {
771                         return BLUETOOTH_ERROR_IN_PROGRESS;
772
773                 } else {
774                         result = le_enable();
775                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
776                                 BT_ERR("LE_enable failed: [%d]", result);
777                                 le_disable();
778                                 result = BLUETOOTH_ERROR_INTERNAL;
779                                 /*TODO: perform if anything more needs to be done to handle failure */
780                         } else {
781                                 /* TODO: To be handled */
782                                 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
783                                 result = BLUETOOTH_ERROR_NONE;
784                         }
785                 }
786                 break;
787         }
788         case BT_LE_DEACTIVATED: {
789                 BT_INFO("LE is currently in deactivated state, state [%d]",
790                                 _bt_adapter_get_le_status());
791                 if (!enable) {
792                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
793                 } else {
794                         result = le_enable();
795                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
796                                 BT_ERR("LE_enable failed: [%d]", result);
797                                 le_disable();
798                                 result = BLUETOOTH_ERROR_INTERNAL;
799                                 /*TODO: perform if anything more needs to be done to handle failure */
800                         } else {
801                                 /* TODO: To be handled */
802                                 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
803                                 result = BLUETOOTH_ERROR_NONE;
804                         }
805                 }
806                 break;
807         }
808         default:
809                 BT_ERR("Unknown state: %d", _bt_adapter_get_le_status);
810                 break;
811         }
812
813         if (enable && result == BLUETOOTH_ERROR_NONE) {
814                 /* Adapter enable request is successful, setup event handlers */
815                 _bt_service_register_event_handler_callback(
816                                 BT_ADAPTER_LE_MODULE, __bt_le_event_handler);
817                 _bt_device_state_handle_callback_set_request();
818         }
819         return result;
820 }
821
822 int _bt_enable_adapter_le(void)
823 {
824         return __bt_le_state_handle_request(TRUE);
825 }
826
827 int _bt_disable_adapter_le(void)
828 {
829         return __bt_le_state_handle_request(FALSE);
830 }
831
832
833 static gboolean __bt_is_factory_test_mode(void)
834 {
835         int mode = 0;
836
837         if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
838                 BT_ERR("Get the DUT Mode fail");
839                 return TRUE;
840         }
841
842         if (mode != FALSE) {
843                 BT_INFO("DUT Test Mode !!");
844                 return TRUE;
845         }
846
847         return FALSE;
848 }
849
850 int _bt_set_advertising(const char *sender, int adv_handle, gboolean enable, gboolean use_reserved_slot)
851 {
852         BT_CHECK_PARAMETER(sender, return);
853         int server_instance;
854         bt_pending_adv_data_set_t *info = NULL;
855         int result = OAL_STATUS_SUCCESS;
856         BT_DBG("+");
857
858         if (__bt_is_factory_test_mode()) {
859                 BT_ERR("Unable to start advertising in factory binary !!");
860                 return BLUETOOTH_ERROR_NOT_SUPPORT;
861         }
862
863         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
864
865         if (server_instance == -1) {
866                 BT_DBG("No available slot for the current sender and handle combination");
867
868                 if (enable == FALSE) {
869                         BT_ERR("Advertising not even enabled on adv handle [%d] sender [%s]", adv_handle, sender);
870                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
871                 }
872
873                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
874
875                 if (server_instance == -1) {
876                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
877                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
878                                 return BLUETOOTH_ERROR_INTERNAL;
879                         else {
880                                 /* Allocate a pending structure and mark Adv data set pending */
881                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
882                                 info->adv_handle = adv_handle;
883                                 info->sender = g_strdup(sender);
884                                 info->is_data_set_pending = FALSE;
885                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
886                                 return BLUETOOTH_ERROR_NONE;
887                         }
888                 }
889         }
890
891         if (enable)
892                 result = adapter_ble_multi_adv_enable(server_instance);
893         else
894                 result = adapter_ble_multi_adv_disable(server_instance);
895         if (result != OAL_STATUS_SUCCESS) {
896                 BT_ERR("OAL API adapter_ble_multi_adv_enable Fail %d", result);
897                 return BLUETOOTH_ERROR_INTERNAL;
898         }
899
900         /* Update adv handle in table */
901         _bt_update_adv_handle(sender, adv_handle);
902
903         return BLUETOOTH_ERROR_NONE;
904 }
905
906 int _bt_set_custom_advertising(const char *sender, int adv_handle,
907                 gboolean enable, bluetooth_advertising_params_t *params, gboolean use_reserved_slot)
908 {
909         BT_CHECK_PARAMETER(sender, return);
910         int server_instance;
911         bt_pending_adv_data_set_t *info = NULL;
912         int result = OAL_STATUS_SUCCESS;
913         BT_DBG("+");
914
915         if (__bt_is_factory_test_mode()) {
916                 BT_ERR("Unable to start advertising in factory binary !!");
917                 return BLUETOOTH_ERROR_NOT_SUPPORT;
918         }
919
920         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
921
922         if (server_instance == -1) {
923                 BT_DBG("No available slot for the current sender and handle combination");
924
925                 if (enable == FALSE) {
926                         BT_ERR("Advertising not even enabled on adv handle [%d] sender [%s]", adv_handle, sender);
927                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
928                 }
929                 /* Below logic is only valid only when enabling advertising */
930                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
931
932                 if (server_instance == -1) {
933                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
934                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
935                                 return BLUETOOTH_ERROR_INTERNAL;
936                         else {
937                                 /* Allocate a pending structure and mark Adv data set pending */
938                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
939                                 info->adv_handle = adv_handle;
940                                 info->sender = g_strdup(sender);
941                                 info->is_custom_adv = TRUE;
942                                 info->is_data_set_pending = FALSE;
943                                 memcpy(&info->params, params, sizeof(bluetooth_advertising_params_t));
944                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
945                                 return BLUETOOTH_ERROR_NONE;
946                         }
947                 }
948         }
949
950         if (enable) {
951                 /* Set Advertising parameters to Stack */
952                 result = adapter_ble_multi_adv_update(server_instance, params->interval_min, params->interval_max,
953                                 params->type, BT_ADV_DEFAULT_CHANNEL_MAP, BT_ADV_DEFAULT_TX_POWER, BT_ADV_DEFAULT_TIMEOUT);
954                 if (result != OAL_STATUS_SUCCESS) {
955                         BT_ERR("OAL API adapter_ble_multi_adv_update Fail %d", result);
956                         return BLUETOOTH_ERROR_INTERNAL;
957                 }
958                 /* Start Advertising when Adv update event is received */
959                 result = adapter_ble_multi_adv_enable(server_instance);
960                 if (result != OAL_STATUS_SUCCESS) {
961                         BT_ERR("OAL API adapter_ble_multi_adv_enable Fail %d", result);
962                         return BLUETOOTH_ERROR_INTERNAL;
963                 }
964                 /* Update adv handle in table */
965                 _bt_update_adv_handle(sender, adv_handle);
966
967                 return BLUETOOTH_ERROR_NONE;
968         } else
969                 result = adapter_ble_multi_adv_disable(server_instance);
970
971         if (result != OAL_STATUS_SUCCESS) {
972                 BT_ERR("OAL API adapter_ble_multi_adv_disable Fail %d", result);
973                 return BLUETOOTH_ERROR_INTERNAL;
974         }
975         return BLUETOOTH_ERROR_NONE;
976 }
977
978 int _bt_get_advertising_data(char *sender, int adv_handle, bluetooth_advertising_data_t *adv, int *length)
979 {
980         BT_CHECK_PARAMETER(adv, return);
981         BT_CHECK_PARAMETER(length, return);
982         BT_CHECK_PARAMETER(sender, return);
983
984         int server_instance;
985
986         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
987
988         if (server_instance == -1) {
989                 BT_DBG("No available slot for the current sender and handle combination");
990                 return BLUETOOTH_ERROR_INTERNAL;
991         }
992
993         _bt_get_previous_adv_data(adv, length, server_instance);
994         BT_DBG("ADV Data length [%d] Server Instance [%d] Adv handle [%d]", length, server_instance, adv_handle);
995
996         return BLUETOOTH_ERROR_NONE;
997 }
998
999 int _bt_get_scan_response_data(char *sender, int adv_handle, bluetooth_scan_resp_data_t *response, int *length)
1000 {
1001         BT_CHECK_PARAMETER(response, return);
1002         BT_CHECK_PARAMETER(length, return);
1003         BT_CHECK_PARAMETER(sender, return);
1004
1005         int server_instance;
1006
1007         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
1008
1009         if (server_instance == -1) {
1010                 BT_DBG("No available slot for the current sender and handle combination");
1011                 return BLUETOOTH_ERROR_INTERNAL;
1012         }
1013
1014         _bt_get_previous_scan_rsp_data(response, length, server_instance);
1015         BT_DBG("SCAN RSP Data length [%d] Server Instance [%d] Adv handle [%d]", length, server_instance, adv_handle);
1016
1017         return BLUETOOTH_ERROR_NONE;
1018 }
1019
1020 static int __bt_set_multi_adv_param(oal_ble_multi_adv_param_setup_t *adv_setup,
1021                 bluetooth_advertising_data_t *adv_data, int length)
1022 {
1023         unsigned char *ptr;
1024         int num_uuids;
1025
1026         BT_INFO("+");
1027         retv_if(NULL == adv_data, BLUETOOTH_ERROR_INVALID_PARAM);
1028         retv_if(NULL == adv_setup, BLUETOOTH_ERROR_INVALID_PARAM);
1029
1030         for (ptr = adv_data->data; NULL != ptr && length > 0;) {
1031                 int len = ptr[0];
1032                 int type = ptr[1];
1033
1034                 BT_INFO("len: %d, type: %x", len, type);
1035
1036                 switch (type) {
1037                 case 0xFF: /* Manufacturer Data */
1038                         adv_setup->manufacturer_data = g_malloc0(sizeof(char) * (len - 1));
1039                         memcpy(adv_setup->manufacturer_data, (ptr + 2), (len - 1));
1040                         adv_setup->manufacturer_data_len = len - 1;
1041                         break;
1042                 case 0x15: /* 128 bit Solicit UUID */
1043                         adv_setup->solicit_uuid = g_malloc0(sizeof(char) * (len - 1));
1044                         memcpy((adv_setup->solicit_uuid), (ptr + 2), (len - 1));
1045                         adv_setup->solicit_uuid_len = len;
1046                         break;
1047                 case 0x06: /* 128 bit Service UUID */
1048                         adv_setup->service_uuid = g_malloc0(sizeof(char) * (len - 1));
1049                         memcpy((adv_setup->service_uuid), (ptr + 2), (len - 1));
1050                         adv_setup->service_uuid_len = len;
1051                         break;
1052                         case 0x14: {  /* 16 bit Solicit UUID */
1053                         int c;
1054                         int k;
1055                         num_uuids = (len -1)/2;
1056                         adv_setup->solicit_uuid = g_malloc0(sizeof(char) * 16 * num_uuids);
1057                         char *tmp = adv_setup->solicit_uuid;
1058                         adv_setup->solicit_uuid_len = 0;
1059
1060                         for (c = 1; c <= num_uuids; c++) {
1061                                 adv_setup->solicit_uuid_len += 16;;
1062                                 memcpy(tmp, BASE_UUID_CONVERTED, BT_UUID_128);
1063                                 memcpy(tmp+12, &ptr[c*2/* Byte Length*/], 2/* Byte Length */);
1064
1065                                 if (c < num_uuids)
1066                                         tmp += 16;
1067                         }
1068                         /* DEBUG: Test*/
1069                         for (k = 0; k < 16 * num_uuids; k++)
1070                                 BT_DBG("%x", adv_setup->solicit_uuid[k]);
1071                         break;
1072                 }
1073                 case 0x02: { /* 16 bit Service UUID */
1074                         int c;
1075                         int k;
1076                         num_uuids = (len -1)/2;
1077                         adv_setup->service_uuid = g_malloc0(sizeof(char) * 16 * num_uuids);
1078                         char *tmp = adv_setup->service_uuid;
1079                         adv_setup->service_uuid_len = 0;
1080
1081                         for (c = 1; c <= num_uuids; c++) {
1082                                 adv_setup->service_uuid_len += 16;;
1083                                 memcpy(tmp, BASE_UUID_CONVERTED, BT_UUID_128);
1084                                 memcpy(tmp+12, &ptr[c*2/* Byte Length */], 2/* Byte Length */);
1085
1086                                 if (c < num_uuids)
1087                                         tmp += 16;
1088                         }
1089                         /* DEBUG: Test*/
1090                         for (k = 0; k < 16 * num_uuids; k++)
1091                                 BT_DBG("%x", adv_setup->service_uuid[k]);
1092                         break;
1093                 }
1094                 case 0x16: { /* Service Data */
1095                         adv_setup->service_data = g_malloc0(sizeof(char) * (len - 1));
1096                         memcpy(adv_setup->service_data, (ptr + 2), (len - 1));
1097                         adv_setup->service_data_len = len - 1;
1098                         break;
1099                 }
1100                 case 0x21: {
1101                         BT_INFO("128 Bit Service Data Not Supported!!");
1102                         break;
1103                 }
1104                 case 0x0A: {
1105                         adv_setup->include_txpower = 1;
1106                         break;
1107                 }
1108                 case 0x09:
1109                 case 0x08: {
1110                         adv_setup->include_name = 1;
1111                         break;
1112                 }
1113                 case 0x19: {
1114                         memcpy(&adv_setup->appearance, (ptr + 2), (len - 1));
1115                         break;
1116                 }
1117                 default:
1118                         BT_ERR("Unknown type: %x", type);
1119                         break;
1120         }
1121
1122                 length -= len + 1;
1123                 ptr += len + 1;
1124         }
1125
1126         BT_INFO("-");
1127         return BLUETOOTH_ERROR_NONE;
1128 }
1129
1130 int _bt_set_advertising_data(const char *sender, int adv_handle,
1131                 bluetooth_advertising_data_t *adv, int length, gboolean use_reserved_slot)
1132 {
1133         BT_CHECK_PARAMETER(adv, return);
1134         BT_CHECK_PARAMETER(sender, return);
1135         bt_pending_adv_data_set_t *info = NULL;
1136         int server_instance;
1137         bluetooth_advertising_data_t adv_old;
1138         int adv_data_len;
1139         char *old_mdata = NULL;
1140         int old_len = 0;
1141         GVariant *ad_data, *param = NULL;
1142         oal_ble_multi_adv_param_setup_t adv_setup;
1143         int result = OAL_STATUS_SUCCESS;
1144         BT_DBG("+");
1145
1146         if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1147                 return BLUETOOTH_ERROR_INVALID_PARAM;
1148
1149         if (__bt_is_factory_test_mode()) {
1150                 BT_ERR("Unable to set advertising data in factory binary !!");
1151                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1152         }
1153
1154         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1155
1156         if (server_instance == -1) {
1157                 BT_DBG("No available slot for the current sender and handle combination");
1158
1159                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1160
1161                 if (server_instance == -1) {
1162                         /* Internal Logic to register server instance if not initialized, store adv handle for future use */
1163                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1164                                 return BLUETOOTH_ERROR_INTERNAL;
1165                         else {
1166                                 /* Allocate a pending structure and mark Adv data set pending */
1167                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1168                                 info->adv_handle = adv_handle;
1169                                 info->sender = g_strdup(sender);
1170                                 info->data_len = length;
1171                                 info->is_adv = TRUE;
1172                                 info->is_data_set_pending = TRUE;
1173                                 memcpy(&info->data, &(adv->data[0]), length);
1174                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1175                                 return BLUETOOTH_ERROR_NONE;
1176                         }
1177                 }
1178         }
1179
1180         /* Server Instance is already allocated, set Adv data to stack */
1181         /* First check if adv data is already present for slot server_instance*/
1182         memset(&adv_old.data, 0, sizeof(adv_old.data));
1183         _bt_get_previous_adv_data(&adv_old, &adv_data_len, server_instance);
1184
1185         /* Send Data to stack */
1186         memset(&adv_setup, 0, sizeof(oal_ble_multi_adv_param_setup_t));
1187
1188         if (BLUETOOTH_ERROR_NONE !=
1189                         __bt_set_multi_adv_param(&adv_setup, adv, length)) {
1190                 if (adv_setup.manufacturer_data)
1191                         g_free(adv_setup.manufacturer_data);
1192                 if (adv_setup.service_uuid)
1193                         g_free(adv_setup.service_uuid);
1194                 if (adv_setup.service_data)
1195                         g_free(adv_setup.service_data);
1196                 return BLUETOOTH_ERROR_INTERNAL;
1197         }
1198         /* Set Scan response false */
1199         adv_setup.set_scan_rsp = FALSE;
1200
1201         /* Set Server instance */
1202         adv_setup.server_if = server_instance;
1203
1204         /* Set Server instance[Product Requirement] */
1205         adv_setup.tx_power = 4;
1206
1207         BT_DBG("####Service UUID len [%d], service data len [%d] Solicit UUID len [%d]",
1208                                 adv_setup.solicit_uuid_len, adv_setup.service_uuid_len, adv_setup.service_data_len);
1209         result = adapter_ble_multi_adv_set_inst_data(server_instance, &adv_setup);
1210         if (result != OAL_STATUS_SUCCESS) {
1211                 BT_ERR("OAL API adapter_ble_multi_adv_set_inst_data Fail %d", result);
1212                 /* Free the data */
1213                 if (adv_setup.manufacturer_data)
1214                         g_free(adv_setup.manufacturer_data);
1215                 if (adv_setup.service_data)
1216                         g_free(adv_setup.service_data);
1217                 if (adv_setup.service_uuid)
1218                         g_free(adv_setup.service_uuid);
1219                 return BLUETOOTH_ERROR_INTERNAL;
1220         }
1221
1222         /* Data sent to Stack successfully, send manuf data changed event if applicable */
1223         if (adv_setup.manufacturer_data_len != 0) {
1224                 if (adv_data_len > 0) {
1225                         _bt_get_ad_data_by_type((char *)adv_old.data, adv_data_len, 0xff,
1226                                         &old_mdata, &old_len);
1227                 }
1228
1229                 if (old_len != adv_setup.manufacturer_data_len ||
1230                                 (old_mdata && adv_setup.manufacturer_data &&
1231                                  memcmp(old_mdata, adv_setup.manufacturer_data, adv_setup.manufacturer_data_len))) {
1232
1233                         ad_data = g_variant_new_from_data((const GVariantType *)"ay",
1234                                         adv_setup.manufacturer_data, adv_setup.manufacturer_data_len, TRUE, NULL, NULL);
1235                         param = g_variant_new("(@ay)", ad_data);
1236                         _bt_send_event(BT_ADAPTER_EVENT,
1237                                         BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
1238                                         param);
1239                 }
1240         }
1241
1242         /* Time to update new ADV data completely in Table */
1243         _bt_set_new_adv_data(adv, length, server_instance);
1244
1245         /* Free the data */
1246         if (adv_setup.manufacturer_data)
1247                 g_free(adv_setup.manufacturer_data);
1248         if (adv_setup.service_data)
1249                 g_free(adv_setup.service_data);
1250         if (adv_setup.service_uuid)
1251                 g_free(adv_setup.service_uuid);
1252
1253         /* Update adv handle in table */
1254         _bt_update_adv_handle(sender, adv_handle);
1255
1256         return BLUETOOTH_ERROR_NONE;
1257 }
1258
1259 int _bt_set_scan_response_data(const char *sender, int adv_handle,
1260                                 bluetooth_scan_resp_data_t *response, int length, gboolean use_reserved_slot)
1261 {
1262         BT_CHECK_PARAMETER(response, return);
1263         BT_CHECK_PARAMETER(sender, return);
1264         bt_pending_adv_data_set_t *info = NULL;
1265         bluetooth_scan_resp_data_t scan_rsp_old;
1266         int scan_rsp_data_len;
1267         GVariant *ad_data, *param = NULL;
1268         oal_ble_multi_adv_param_setup_t adv_setup;
1269         char *old_mdata = NULL;
1270         int old_len = 0;
1271         int server_instance;
1272         int result = OAL_STATUS_SUCCESS;
1273         BT_DBG("+");
1274
1275         if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1276                 return BLUETOOTH_ERROR_INVALID_PARAM;
1277
1278         if (__bt_is_factory_test_mode()) {
1279                 BT_ERR("Unable to set Scan Response data in factory binary !!");
1280                 return BLUETOOTH_ERROR_NOT_SUPPORT;
1281         }
1282
1283         server_instance = _bt_get_allocated_server_instance(sender, adv_handle, use_reserved_slot);
1284         if (server_instance == -1) {
1285                 BT_DBG("No available slot for the current sender and handle combination");
1286
1287                 server_instance = _bt_is_sender_gatt_server_with_no_adv(sender, adv_handle);
1288
1289                 if (server_instance == -1) {
1290                         /* Internal Logic to register server instance if not initialized */
1291                         if (_bt_register_server_instance(sender, adv_handle) != BLUETOOTH_ERROR_NONE)
1292                                 return BLUETOOTH_ERROR_INTERNAL;
1293                         else {
1294                                 /* Allocate a pending structure and mark Adv data set pending */
1295                                 info = g_malloc0(sizeof(bt_pending_adv_data_set_t));
1296                                 info->adv_handle = adv_handle;
1297                                 info->sender = g_strdup(sender);
1298                                 info->data_len = length;
1299                                 info->is_adv = FALSE;
1300                                 info->is_data_set_pending = TRUE;
1301                                 memcpy(&info->data, &(response->data[0]), length);
1302                                 adv_data_pending_list = g_slist_append(adv_data_pending_list, info);
1303                                 return BLUETOOTH_ERROR_NONE;
1304                         }
1305                 }
1306         }
1307
1308         /* Server Instance is already allocated, set Adv data to stack */
1309         /* First check if adv data is already present for slot server_instance*/
1310         memset(&scan_rsp_old.data, 0, sizeof(scan_rsp_old.data));
1311         _bt_get_previous_scan_rsp_data(&scan_rsp_old, &scan_rsp_data_len, server_instance);
1312
1313         /* Send Data to stack */
1314         memset(&adv_setup, 0, sizeof(oal_ble_multi_adv_param_setup_t));
1315
1316         if (BLUETOOTH_ERROR_NONE !=
1317                         __bt_set_multi_adv_param(&adv_setup, (bluetooth_advertising_data_t*)response, length)) {
1318                 if (adv_setup.manufacturer_data)
1319                         g_free(adv_setup.manufacturer_data);
1320                 if (adv_setup.service_uuid)
1321                         g_free(adv_setup.service_uuid);
1322                 if (adv_setup.service_data)
1323                         g_free(adv_setup.service_data);
1324                 return BLUETOOTH_ERROR_INTERNAL;
1325         }
1326
1327         /* Set Scan response to TRUE */
1328         adv_setup.set_scan_rsp = TRUE;
1329
1330         /* Set Server instance */
1331         adv_setup.server_if = server_instance;
1332
1333         /* Set Server instance[Product Requirement] */
1334         adv_setup.tx_power = 4;
1335
1336         result = adapter_ble_multi_adv_set_inst_data(server_instance, &adv_setup);
1337         if (result != OAL_STATUS_SUCCESS) {
1338                 BT_ERR("OAL API adapter_ble_multi_adv_set_inst_data Fail %d", result);
1339                 /* Free the data */
1340                 if (adv_setup.manufacturer_data)
1341                         g_free(adv_setup.manufacturer_data);
1342                 if (adv_setup.service_data)
1343                         g_free(adv_setup.service_data);
1344                 if (adv_setup.service_uuid)
1345                         g_free(adv_setup.service_uuid);
1346                 return BLUETOOTH_ERROR_INTERNAL;
1347         }
1348
1349         /* Data sent to Stack successfully, send manuf data changed event if applicable */
1350         if (adv_setup.manufacturer_data_len != 0) {
1351                 if (scan_rsp_data_len > 0) {
1352                         _bt_get_ad_data_by_type((char *)scan_rsp_old.data, scan_rsp_data_len, 0xff,
1353                                         &old_mdata, &old_len);
1354                 }
1355
1356                 if (old_len != adv_setup.manufacturer_data_len ||
1357                                 (old_mdata && adv_setup.manufacturer_data &&
1358                                  memcmp(old_mdata, adv_setup.manufacturer_data, adv_setup.manufacturer_data_len))) {
1359
1360                         ad_data = g_variant_new_from_data((const GVariantType *)"ay",
1361                                         adv_setup.manufacturer_data, adv_setup.manufacturer_data_len, TRUE, NULL, NULL);
1362                         param = g_variant_new("(@ay)", ad_data);
1363                         _bt_send_event(BT_ADAPTER_EVENT,
1364                                         BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
1365                                         param);
1366                 }
1367         }
1368
1369         /* Time to update new Scan Response data completely in Table */
1370         _bt_set_new_scan_rsp_data(response, length, server_instance);
1371
1372         /* Free the data */
1373         if (adv_setup.manufacturer_data)
1374                 g_free(adv_setup.manufacturer_data);
1375         if (adv_setup.service_data)
1376                 g_free(adv_setup.service_data);
1377         if (adv_setup.service_uuid)
1378                 g_free(adv_setup.service_uuid);
1379
1380         /* Update adv handle in table */
1381         _bt_update_adv_handle(sender, adv_handle);
1382         return BLUETOOTH_ERROR_NONE;
1383 }
1384
1385 /*************************************** LE Scan APIs *********************************************/
1386 int _bt_set_scan_parameters(bluetooth_le_scan_params_t *params)
1387 {
1388         int itv = 0;
1389         int win = 0;
1390         int ret;
1391
1392         BT_CHECK_PARAMETER(params, return);
1393
1394         if (_bt_adapter_get_status() != BT_ACTIVATED &&
1395                         _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
1396                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1397         }
1398
1399         if (params->interval < BT_LE_SCAN_INTERVAL_MIN ||
1400                         params->interval > BT_LE_SCAN_INTERVAL_MAX)
1401                 return BLUETOOTH_ERROR_INVALID_PARAM;
1402
1403         if (params->window < BT_LE_SCAN_WINDOW_MIN ||
1404                         params->window > BT_LE_SCAN_WINDOW_MAX)
1405                 return BLUETOOTH_ERROR_INVALID_PARAM;
1406
1407         if (params->window > params->interval)
1408                 return BLUETOOTH_ERROR_INVALID_PARAM;
1409
1410         itv = params->interval / BT_ADV_INTERVAL_SPLIT;
1411         win = params->window / BT_ADV_INTERVAL_SPLIT;
1412
1413         ret = gattc_set_le_scan_param(params->type, itv, win);
1414         if (OAL_STATUS_SUCCESS != ret) {
1415                 BT_ERR("gattc_set_le_scan_param failed");
1416                 return BLUETOOTH_ERROR_INTERNAL;
1417         }
1418
1419         is_le_set_scan_parameter = TRUE;
1420         BT_INFO("Set scan parameters");
1421
1422         return BLUETOOTH_ERROR_NONE;
1423 }
1424
1425 gboolean __send_le_scan_reply(gpointer data)
1426 {
1427         int value = (int)data;
1428
1429         if (0 == value) {
1430                 BT_DBG("Send reply to BT_STOP_LE_DISCOVERY");
1431                 __bt_adapter_le_handle_pending_request_info(
1432                                 BLUETOOTH_ERROR_NONE,
1433                                 BT_STOP_LE_DISCOVERY, NULL, 0);
1434         } else {
1435                 BT_DBG("Send reply to BT_START_LE_DISCOVERY");
1436                 __bt_adapter_le_handle_pending_request_info(
1437                                 BLUETOOTH_ERROR_NONE,
1438                                 BT_START_LE_DISCOVERY, NULL, 0);
1439         }
1440
1441         return FALSE;
1442 }
1443
1444 int _bt_start_le_scan(const char *sender)
1445 {
1446         bt_adapter_le_scanner_t *scanner;
1447         int ret;
1448
1449         scanner = __bt_find_scanner_from_list(sender);
1450         if (!scanner) {
1451                 scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
1452                 retv_if(scanner == NULL, BLUETOOTH_ERROR_INTERNAL);
1453
1454                 scanner->sender = g_strdup(sender);
1455                 scanner_list = g_slist_append(scanner_list, scanner);
1456         }
1457
1458         if (scanner->is_scanning) {
1459                 BT_ERR("BT is already in LE scanning");
1460                 return BLUETOOTH_ERROR_IN_PROGRESS;
1461         }
1462
1463         if (_bt_is_le_scanning()) {
1464                 int value = 1;
1465
1466                 BT_INFO("LE Full Scan is already on progress");
1467                 g_idle_add(__send_le_scan_reply, (void *)value);
1468                 goto done;
1469         }
1470
1471         if (is_le_set_scan_parameter == FALSE) {
1472                 bluetooth_le_scan_params_t scan_params;
1473
1474                 /* Set default scan parameter same with BT_ADAPTER_LE_SCAN_MODE_LOW_ENERGY */
1475                 scan_params.type = 0x01;
1476                 scan_params.interval = 5000;
1477                 scan_params.window = 500;
1478                 _bt_set_scan_parameters(&scan_params);
1479         }
1480
1481         BT_INFO("Start LE Full Scan");
1482         ret = gattc_start_le_discovery();
1483         if (OAL_STATUS_SUCCESS != ret) {
1484                 BT_ERR("gattc_start_le_discovery failed");
1485                 return BLUETOOTH_ERROR_INTERNAL;
1486         }
1487
1488 done:
1489         scanner->is_scanning = TRUE;
1490         return BLUETOOTH_ERROR_NONE;
1491 }
1492
1493 int _bt_stop_le_scan(const char *sender)
1494 {
1495         bt_adapter_le_scanner_t *scanner;
1496         gboolean next_scanning = FALSE;
1497         GSList *l;
1498         int ret;
1499
1500         scanner = __bt_find_scanner_from_list(sender);
1501         if (scanner == NULL || scanner->is_scanning == FALSE)
1502                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1503
1504         scanner->is_scanning = FALSE;
1505
1506         for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1507                 scanner = l->data;
1508                 if (scanner->is_scanning == TRUE)
1509                         next_scanning = TRUE;
1510         }
1511
1512         if (next_scanning == TRUE) {
1513                 int value = 0;
1514
1515                 g_idle_add(__send_le_scan_reply, (void *)value);
1516                 return BLUETOOTH_ERROR_NONE;
1517         } else {
1518                 BT_INFO("Just stop LE scan");
1519         }
1520
1521         ret = gattc_stop_le_discovery();
1522         if (OAL_STATUS_SUCCESS != ret) {
1523                 BT_ERR("gattc_start_le_discovery failed");
1524                 return BLUETOOTH_ERROR_INTERNAL;
1525         }
1526
1527         is_le_set_scan_parameter = FALSE;
1528         return BLUETOOTH_ERROR_NONE;
1529 }
1530
1531 void _bt_check_le_scanner_app_termination(const char *sender)
1532 {
1533         bt_adapter_le_scanner_t *scanner;
1534
1535         scanner = __bt_find_scanner_from_list(sender);
1536         if (!scanner)
1537                 return;
1538
1539         if (scanner->is_scanning)
1540                 _bt_stop_le_scan(sender);
1541
1542         scanner_list = g_slist_remove(scanner_list, scanner);
1543         g_free(scanner->sender);
1544         g_free(scanner);
1545 }
1546
1547 int _bt_service_le_init(void)
1548 {
1549         le_init();
1550
1551         return BLUETOOTH_ERROR_NONE;
1552 }
1553
1554 void _bt_service_le_deinit(void)
1555 {
1556         le_deinit();
1557 }
1558
1559
1560 /*************************************** LE Scan APIs *********************************************/