Remove wrong dependency in the systemd service file
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter-le.c
index 214312b..d85f6bb 100644 (file)
@@ -177,16 +177,27 @@ void __bt_free_le_adv_slot(void)
        le_adv_slot = NULL;
 }
 
-int _bt_service_adapter_le_init(void)
+int _bt_le_set_max_packet_len(void)
 {
-       le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
+       int result = BLUETOOTH_ERROR_NONE;
+       int tx_octets, tx_time;
+       bluetooth_le_read_maximum_data_length_t max_len = {0};
 
-       return BLUETOOTH_ERROR_NONE;
-}
+       if (BLUETOOTH_ERROR_NONE != _bt_le_read_maximum_data_length(&max_len))
+               return BLUETOOTH_ERROR_INTERNAL;
 
-void _bt_service_adapter_le_deinit(void)
-{
-       __bt_free_le_adv_slot();
+       if (max_len.max_tx_octets > BT_LE_TX_LEN_DEFAULT) {
+               tx_octets =  max_len.max_tx_octets > BT_LE_TX_LEN_MAX ?
+                               BT_LE_TX_LEN_MAX : max_len.max_tx_octets;
+               tx_time = BT_LE_TX_TIME_MAX;
+
+               result = _bt_le_write_host_suggested_default_data_length(tx_octets, tx_time);
+
+               BT_DBG("Wrote max packet size : result[%d], MAX[%d], set[%d]",
+                               result, max_len.max_tx_octets, tx_octets);
+       }
+
+       return result;
 }
 
 gboolean _bt_update_le_feature_support(const char *item, const char *value)
@@ -194,6 +205,9 @@ 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;
 
@@ -280,6 +294,11 @@ static void __bt_register_adv_slot_owner(const char *sender, int adv_handle, int
 
 void _bt_unregister_adv_slot_owner(int slot_id)
 {
+       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;
@@ -311,6 +330,9 @@ gboolean _bt_is_advertising(void)
        gboolean status = FALSE;
        int i;
 
+       if (le_adv_slot == NULL)
+               return FALSE;
+
        for (i = 0; i < le_feature_info.adv_inst_max; i++) {
                if (le_adv_slot[i].is_advertising == TRUE)
                        status = TRUE;
@@ -372,14 +394,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);
 
@@ -439,14 +462,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);
 
@@ -501,8 +525,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], Slot id [%d]", enable, slot_id);
@@ -1530,6 +1552,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;
@@ -2155,8 +2197,6 @@ int _bt_le_read_maximum_data_length(
        reply = g_dbus_proxy_call_sync(proxy, "LEReadMaximumDataLength",
                        NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
 
-       g_object_unref(proxy);
-
        if (reply == NULL) {
                BT_ERR("LEReadMaximumDataLength dBUS-RPC failed");
                if (error != NULL) {
@@ -2197,8 +2237,6 @@ int _bt_le_write_host_suggested_default_data_length(
                        NULL,
                        &error);
 
-       g_object_unref(proxy);
-
        if (reply == NULL) {
                BT_ERR("_bt_le_write_host_suggested_default_data_length dBUS-RPC failed");
                if (error != NULL) {
@@ -2300,3 +2338,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();
+}