Resolve the LE advertising fail after slot exhaustion
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-adapter-le.c
index 3acf1cf..27b56fb 100644 (file)
@@ -188,22 +188,23 @@ gboolean _bt_hal_is_advertising_in_slot(int slot)
 
 void _bt_hal_set_advertising_status(int slot_id, gboolean mode)
 {
-       DBG("Advertising enabled [%s] server_slot [%d]",  mode ? "TRUE" : "FALSE", slot_id);
        bt_hal_adv_event_data_t *event;
-       int adv_slot_id = -1;
+       int adv_slot_id = slot_id;
+       int server_if = 0;
 
-       if (le_adv_slot == NULL)
-               return;
+       server_if = bt_hal_gatts_get_server_if(slot_id);
 
-       adv_slot_id = bt_hal_gatts_get_adv_slot_id(slot_id);
-       if (adv_slot_id < 0)
+       DBG("Advertising enabled [%s] server_slot [%d] server_if[%d]",  mode ? "TRUE" : "FALSE", slot_id, server_if);
+
+       if (le_adv_slot == NULL)
                return;
 
        le_adv_slot[adv_slot_id].is_advertising = mode;
 
        event = g_malloc0(sizeof(bt_hal_adv_event_data_t));
        event->event = mode ? BT_HAL_MULTI_ADV_ENB_EVT : BT_HAL_MULTI_ADV_DISABLE_EVT;
-       event->server_if = slot_id;
+
+       event->server_if = server_if;
        event->status = BT_STATUS_SUCCESS;
        /* To be removed later when we completely descope Legacy Adv concept */
 
@@ -213,7 +214,7 @@ void _bt_hal_set_advertising_status(int slot_id, gboolean mode)
 
        if (mode == false ) {
                DBG("release the adv_slot");
-               bt_hal_gatts_release_adv_slot(slot_id);
+               bt_hal_gatts_release_adv_slot(server_if);
        }
 
        __bt_hal_adv_event_cb((gpointer)event);
@@ -223,6 +224,9 @@ gboolean _bt_hal_is_advertising(void)
 {
        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)
                        return TRUE;
@@ -624,7 +628,7 @@ int _bt_hal_enable_advertising(int server_if, int adv_slot_id, bool enable, bool
        }
 
        ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
-                       g_variant_new("(bi)", enable, server_if),
+                       g_variant_new("(bi)", enable, adv_slot_id),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
@@ -657,6 +661,7 @@ int _bt_hal_set_advertising_params(int server_if, int min_interval,
        guint32 min = 0;
        guint32 max = 0;
        bt_hal_adv_event_data_t *event;
+       int slot_id = 0;
 
 
        proxy = _bt_hal_get_adapter_proxy();
@@ -678,10 +683,12 @@ int _bt_hal_set_advertising_params(int server_if, int min_interval,
        min = min_interval / BT_HAL_ADV_INTERVAL_SPLIT;
        max = max_interval / BT_HAL_ADV_INTERVAL_SPLIT;
 
+       slot_id = bt_hal_gatts_allocate_adv_slot_by_server_if(server_if);
+
        ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingParameters",
                        g_variant_new("(uuuuii)", min, max,
                        BT_HAL_ADV_FILTER_POLICY_DEFAULT, adv_type,
-                       tx_power, server_if), G_DBUS_CALL_FLAGS_NONE,
+                       tx_power, slot_id), G_DBUS_CALL_FLAGS_NONE,
                        -1, NULL, &error);
        if (error) {
                ERR("SetAdvertisingParameters Fail: %s", error->message);
@@ -720,6 +727,7 @@ int _bt_hal_set_advertising_data(btgatt_adv_param_setup_t adv_param_setup)
        bt_hal_adv_event_data_t *event;
        int length = 0;
        int i;
+       int slot_id = 0;
 
        /* Parse data according to Bluez Interface */
        if (__bt_hal_is_factory_test_mode()) {
@@ -732,6 +740,8 @@ int _bt_hal_set_advertising_data(btgatt_adv_param_setup_t adv_param_setup)
        if (proxy == NULL)
                return BT_STATUS_FAIL;
 
+       slot_id = bt_hal_gatts_allocate_adv_slot_by_server_if(adv_param_setup.server_if);
+
        memset(&adv_data, 0, 31);
 
        /* Service UUID */
@@ -817,13 +827,13 @@ int _bt_hal_set_advertising_data(btgatt_adv_param_setup_t adv_param_setup)
        if (adv_param_setup.set_scan_rsp == 0) {
                /* Set Advertising data to stack */
                ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingData",
-                               g_variant_new("(@ayi)", temp, adv_param_setup.server_if),
+                               g_variant_new("(@ayi)", temp, slot_id),
                                G_DBUS_CALL_FLAGS_NONE,
                                -1, NULL, &error);
        } else {
                /* Set Scan response data to stack */
                ret = g_dbus_proxy_call_sync(proxy, "SetScanRespData",
-                               g_variant_new("(@ayi)", temp, adv_param_setup.server_if),
+                               g_variant_new("(@ayi)", temp, slot_id),
                                G_DBUS_CALL_FLAGS_NONE,
                                -1, NULL, &error);
        }
@@ -922,6 +932,37 @@ int _bt_hal_adapter_le_stop_scan(void)
        return BT_STATUS_SUCCESS;
 }
 
+/*sets the privacy functionality of the adapter*/
+int _bt_hal_adapter_le_set_privacy(uint8_t set_privacy)
+{
+       GDBusProxy *proxy;
+       GError *error = NULL;
+       GVariant *result = NULL;
+       proxy = _bt_hal_get_adapter_proxy();
+       if (proxy == NULL)
+               return BT_STATUS_FAIL;
+
+       result = g_dbus_proxy_call_sync(proxy,
+                               "SetLePrivacy",
+                               g_variant_new("(b)", set_privacy),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               -1,
+                               NULL,
+                               &error);
+
+       if (!result) {
+               if (error != NULL) {
+                       ERR("Failed to SetLePrivacy (Error: %s)", error->message);
+                       g_clear_error(&error);
+               } else
+                       ERR("Failed to SetLePrivacy");
+               return BT_STATUS_FAIL;
+       }
+
+       g_variant_unref(result);
+       INFO("SetLePrivacy as %d", set_privacy);
+       return BT_STATUS_SUCCESS;
+}
 
 int _bt_hal_adapter_le_set_scan_parameters(
                int scan_type, int scan_interval, int scan_window)
@@ -966,6 +1007,54 @@ void _bt_hal_unregister_gatt_le_dbus_handler_cb(void)
        gatt_le_event_cb = NULL;
 }
 
+int _bt_hal_adapter_le_set_manufacturer_data(bt_manufacturer_data_t *m_data)
+{
+       GDBusProxy *proxy;
+       GError *error = NULL;
+       int i;
+       GVariant *val;
+       GVariant *ret;
+       GVariantBuilder *builder;
+
+       proxy = _bt_hal_get_adapter_proxy();
+       if (proxy == NULL)
+               return BT_STATUS_FAIL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+
+       if (m_data->data_len + 2 >= BT_MANUFACTURER_DATA_LENGTH_MAX)
+               return BT_STATUS_PARM_INVALID;
+       for (i = 0; i < (m_data->data_len) + 2; i++)
+               g_variant_builder_add(builder, "y", m_data->data[i]);
+
+       val = g_variant_new("(ay)", builder);
+
+       ret = g_dbus_proxy_call_sync(proxy,
+                               "SetManufacturerData",
+                               val,
+                               G_DBUS_CALL_FLAGS_NONE,
+                               -1,
+                               NULL,
+                               &error);
+       g_variant_builder_unref(builder);
+       if (!ret) {
+               if (error != NULL) {
+                       ERR("Failed to SetManufacturerData (Error: %s)", error->message);
+                       g_clear_error(&error);
+               } else {
+                       ERR("Failed to SetManufacturerData");
+               }
+               return BT_STATUS_FAIL;
+       }
+
+       INFO("Set manufacturer data");
+       g_variant_unref(ret);
+
+       return BT_STATUS_SUCCESS;
+}
+
+
+
 /*add/remove remote device address from white list*/
 int _bt_hal_adapter_le_set_white_list(bt_bdaddr_t *device_address, bt_dev_addr_type_t address_type, bool is_add)
 {