#include "oic_string.h"
#include "oic_malloc.h"
#include "caleutil.h"
+#include "cautilinterface.h"
+#ifndef TIZEN_4
+#include <bluetooth_internal.h>
+#endif
/**
* Logging tag for module name
static bt_advertiser_h g_hAdvertiser = NULL;
/**
+ * Function called just before starting advertising
+ * provided data are used to setServerAdvertisementData
+ */
+static CAAdvertisementDataGetterCB g_leServerAdvertisementDataGetter = NULL;
+
+/**
+ * Control if advertise will start automatically
+ */
+static bool g_leServerAutoAdvertisement = true;
+
+/**
* Callback register with LE adapter. This callback is called on reception of any
* data from the remote device.
*/
static char *custom_scanrsp_data = NULL;
static int custom_scanrsp_data_length = 0;
+/**
+ * Maximum lenght of BLE advertisement packet data size is acctually 24 bytes,
+ * but for safety reason 1 additional byte is allocated.
+ */
+const int max_ble_advertisement_packet_data_size = 25;
+
+
CAResult_t CAsetServerAdvertisementData(const char *data, int length)
{
- int res = 0;
CAResult_t ret = CA_STATUS_OK;
OIC_LOG(DEBUG, TAG, "IN");
}
oc_mutex_lock(g_leServerStateMutex);
+ CAsetServerAdvertisementDataImpl(data, length);
+ oc_mutex_unlock(g_leServerStateMutex);
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return ret;
+}
+void CAsetServerAdvertisementDataImpl(const char *data, int length)
+{
cutom_adv_flag = true;
custom_adv_data = data;
custom_adv_data_length = length;
+ OIC_LOG_V(DEBUG, TAG, "Custom advertise value has set as [%s]", custom_adv_data);
+}
+
+void CAsetServerAdvertisementDataGetter(CAAdvertisementDataGetterCB getter)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+
+ oc_mutex_lock(g_leServerStateMutex);
+ g_leServerAdvertisementDataGetter = getter;
oc_mutex_unlock(g_leServerStateMutex);
- OIC_LOG_V(DEBUG, TAG, "Custom advertise value has set as [%s]", custom_adv_data);
OIC_LOG(DEBUG, TAG, "OUT");
+}
- return ret;
+void CAsetServerAutoAdvertisement(bool auto_advertisement)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+
+ oc_mutex_lock(g_leServerStateMutex);
+ g_leServerAutoAdvertisement = auto_advertisement;
+ oc_mutex_unlock(g_leServerStateMutex);
+
+ OIC_LOG(DEBUG, TAG, "OUT");
}
-CAResult_t CAsetServerSanResponseData(const char *data, int length)
+CAResult_t CAsetServerScanResponseData(const char *data, int length)
{
int res = 0;
CAResult_t ret = CA_STATUS_OK;
CARemoveLEClientInfoFromList(&g_LEClientList, remoteAddress);
oc_mutex_unlock(g_LEClientListMutex);
- res = CALEStartAdvertise();
- if (CA_STATUS_OK != res)
+ if(g_leServerAutoAdvertisement)
{
- OIC_LOG_V(ERROR, TAG, "Failed to start advertising [%d]", res);
- return;
+ res = CALEStartAdvertise();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to start advertising [%d]", res);
+ return;
+ }
+ }
+ else
+ {
+ OIC_LOG(WARNING, TAG, "Auto advertisement is DISABLED. Advertising is not started.");
}
}
}
CATerminateLEGattServer();
return CA_STATUS_FAILED;
}
-
- ret = CALEStartAdvertise();
- if (CA_STATUS_OK != ret)
+
+ if(g_leServerAutoAdvertisement)
{
- OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
- oc_mutex_unlock(g_leServerStateMutex);
- CATerminateLEGattServer();
- return CA_STATUS_FAILED;
+ ret = CALEStartAdvertise();
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
+ oc_mutex_unlock(g_leServerStateMutex);
+ CATerminateLEGattServer();
+ return CA_STATUS_FAILED;
+ }
}
-
+ else
+ {
+ OIC_LOG(WARNING, TAG, "Auto advertisement is DISABLED. Advertising is not started.");
+ }
+
g_isLEGattServerStarted = true;
oc_mutex_unlock(g_leServerStateMutex);
CAResult_t CALEStartAdvertise()
{
OIC_LOG(DEBUG, TAG, "IN");
+ char * data = NULL;
+
+ if(g_leServerAdvertisementDataGetter)
+ {
+ int length;
+ data = (char*) malloc(max_ble_advertisement_packet_data_size);
+ g_leServerAdvertisementDataGetter(max_ble_advertisement_packet_data_size, data, &length);
+ if(!data || length <= 0)
+ {
+ OIC_LOG(WARNING, TAG, "Invalid data. Custom advertisement data will not be set.");
+ }
+ else
+ {
+ CAsetServerAdvertisementDataImpl(data, length);
+ }
+ }
CAResult_t res = CALEStartAdvertiseImpl(CA_GATT_SERVICE_UUID);
if (CA_STATUS_OK != res)
{
OIC_LOG_V(ERROR, TAG, "CALEStartAdvertiseImpl failed[%d]", res);
}
-
+
+ //This array is freed here because it's used in CALEStartAdvertiseImpl() method (freeing earlier causes bluetooth to crash)
+ free(data);
OIC_LOG(DEBUG, TAG, "OUT");
return res;
}
OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
- unsigned int mtu;
- int ret = bt_device_get_att_mtu(address, &mtu);
+ unsigned int mtu = CA_DEFAULT_BLE_MTU_SIZE;
+ int ret = 0;
+
+#ifdef TIZEN_4
+ bt_gatt_client_h client = NULL;
+ ret = bt_gatt_client_create(address, &client);
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG,
+ "bt_gatt_client_create failed with return [%s]", CALEGetErrorMsg(ret));
+ return CA_DEFAULT_BLE_MTU_SIZE;
+ }
+
+ ret = bt_gatt_client_get_att_mtu(client, &mtu);
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG,
+ "bt_gatt_client_get_att_mtu failed with return [%s]", CALEGetErrorMsg(ret));
+ return CA_DEFAULT_BLE_MTU_SIZE;
+ }
+
+ ret = bt_gatt_client_destroy(client);
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG,
+ "bt_gatt_client_destroy failed with return [%s]", CALEGetErrorMsg(ret));
+ return CA_DEFAULT_BLE_MTU_SIZE;
+ }
+#else
+ ret = bt_device_get_att_mtu(address, &mtu);
if (0 != ret)
{
OIC_LOG_V(ERROR, TAG,
"bt_device_get_att_mtu failed with return [%s]", CALEGetErrorMsg(ret));
return CA_DEFAULT_BLE_MTU_SIZE;
}
+#endif // TIZEN_4
+
OIC_LOG_V(INFO, TAG, "mtu size(including header) from bt_device_get_att_mtu is %d", mtu);
OIC_LOG(DEBUG, TAG, "OUT");
return mtu - CA_BLE_MTU_HEADER_SIZE;