Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter-le.c
index efd72e3..d6567b1 100644 (file)
@@ -94,23 +94,14 @@ static void __bt_free_le_adv_slot(void)
        le_adv_slot = NULL;
 }
 
-int _bt_service_adapter_le_init(void)
-{
-       le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
-
-       return BLUETOOTH_ERROR_NONE;
-}
-
-void _bt_service_adapter_le_deinit(void)
-{
-       __bt_free_le_adv_slot();
-}
-
 gboolean _bt_update_le_feature_support(const char *item, const char *value)
 {
        if (item == NULL || value == NULL)
                return FALSE;
 
+       if (!le_adv_slot)
+               _bt_service_adapter_le_init();
+
        if (g_strcmp0(item, "adv_inst_max") == 0) {
                int slot_num;
 
@@ -204,6 +195,11 @@ void _bt_unregister_adv_slot_owner(int slot_id)
        if (le_adv_slot == NULL)
                return;
 
+       if (le_adv_slot[slot_id].hold_timer_id > 0) {
+               BT_INFO("Hold state adv is not unregistered");
+               return;
+       }
+
        g_free(le_adv_slot[slot_id].sender);
        le_adv_slot[slot_id].sender = NULL;
        le_adv_slot[slot_id].adv_handle = 0;
@@ -304,14 +300,15 @@ int _bt_set_advertising(const char *sender, int adv_handle, gboolean enable, gbo
        if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
                return BLUETOOTH_ERROR_IN_PROGRESS;
 
-       if (le_adv_slot[slot_id].sender != NULL && le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
-               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
-
        if (le_adv_slot[slot_id].hold_timer_id > 0) {
                g_source_remove(le_adv_slot[slot_id].hold_timer_id);
                le_adv_slot[slot_id].hold_timer_id = 0;
+               _bt_unregister_adv_slot_owner(slot_id);
        }
 
+       if (le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
+               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+
        proxy = _bt_get_adapter_proxy();
        retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
@@ -371,14 +368,15 @@ int _bt_set_custom_advertising(const char *sender, int adv_handle,
        if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
                return BLUETOOTH_ERROR_IN_PROGRESS;
 
-       if (le_adv_slot[slot_id].sender != NULL && le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
-               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
-
        if (le_adv_slot[slot_id].hold_timer_id > 0) {
                g_source_remove(le_adv_slot[slot_id].hold_timer_id);
                le_adv_slot[slot_id].hold_timer_id = 0;
+               _bt_unregister_adv_slot_owner(slot_id);
        }
 
+       if (le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
+               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+
        proxy = _bt_get_adapter_proxy();
        retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
@@ -433,8 +431,6 @@ int _bt_set_custom_advertising(const char *sender, int adv_handle,
 
        if (enable == TRUE)
                __bt_register_adv_slot_owner(sender, adv_handle, slot_id);
-       else
-               _bt_unregister_adv_slot_owner(slot_id);
 
        le_adv_slot[slot_id].is_advertising = enable;
        BT_INFO_C("### Set advertising [%d]", enable);
@@ -507,7 +503,7 @@ int _bt_hold_current_advertising(void)
                le_adv_slot[0].hold_timer_id = g_timeout_add(2000,
                                __bt_hold_current_advertising_timeout_cb, NULL);
        } else {
-               BT_ERR("It's NOT advertising");
+               BT_INFO("It's NOT advertising");
                return BLUETOOTH_ERROR_NOT_IN_OPERATION;
        }
 
@@ -1177,16 +1173,18 @@ int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *fil
        scanner = __bt_find_scanner_from_list(sender);
        if (scanner == NULL) {
                scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
-               scanner->sender = strdup(sender);
+               scanner->sender = g_strdup(sender);
                scanner_list = g_slist_append(scanner_list, scanner);
        }
 
-       filter_data = g_malloc0(sizeof(bluetooth_le_scan_filter_t));
-       memcpy(filter_data, filter, sizeof(bluetooth_le_scan_filter_t));
-       filter_data->slot_id = *slot_id;
 
-       if (scanner)
+       if (scanner) {
+               filter_data = g_malloc0(sizeof(bluetooth_le_scan_filter_t));
+               memcpy(filter_data, filter, sizeof(bluetooth_le_scan_filter_t));
+               filter_data->slot_id = *slot_id;
+
                scanner->filter_list = g_slist_append(scanner->filter_list, filter_data);
+       }
 
        if (ret)
                g_variant_unref(ret);
@@ -1290,7 +1288,7 @@ int _bt_start_le_scan(const char *sender)
 
        if (scanner == NULL) {
                scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
-               scanner->sender = strdup(sender);
+               scanner->sender = g_strdup(sender);
                scanner_list = g_slist_append(scanner_list, scanner);
        }
 
@@ -1462,6 +1460,26 @@ void _bt_disable_all_scanner_status(void)
        }
 }
 
+static void __bt_free_le_scanner(void)
+{
+       GSList *l;
+       bt_adapter_le_scanner_t *scanner;
+
+       for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
+               scanner = l->data;
+               g_free(scanner->sender);
+               g_slist_free_full(scanner->filter_list, g_free);
+               g_free(scanner);
+       }
+
+       g_slist_free(scanner_list);
+       scanner_list = NULL;
+
+       scan_filter_enabled = FALSE;
+       is_le_scanning = FALSE;
+       is_le_set_scan_parameter = FALSE;
+}
+
 void _bt_set_le_scan_status(gboolean mode)
 {
        is_le_scanning = mode;
@@ -2194,3 +2212,16 @@ int _bt_le_set_data_length(bluetooth_device_address_t *device_address,
 
        return BLUETOOTH_ERROR_NONE;
 }
+
+int _bt_service_adapter_le_init(void)
+{
+       le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+void _bt_service_adapter_le_deinit(void)
+{
+       __bt_free_le_adv_slot();
+       __bt_free_le_scanner();
+}