CAResult_t CABleGattSetCallbacks()
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    int ret = bt_gatt_set_connection_state_changed_cb(CABleGattConnectionStateChangedCb, NULL);
    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                  "bt_gatt_set_connection_state_changed_cb Failed with return as [%s ]",
                  CABTGetErrorMsg(ret));
        return CA_STATUS_FAILED;
    }

    ret = bt_adapter_le_set_device_discovery_state_changed_cb(
              CABtAdapterLeDeviceDiscoveryStateChangedCb, NULL);
    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                  "bt_adapter_le_set_device_discovery_state_changed_cb Failed with return as [%s ]",
                  CABTGetErrorMsg(ret));;
        return CA_STATUS_FAILED;
    }

    ret = bt_gatt_set_characteristic_changed_cb(CABleGattCharacteristicChangedCb, NULL);
    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG, "bt_gatt_set_characteristic_changed_cb Failed as [%s ]",
                  CABTGetErrorMsg(ret));
        return CA_STATUS_FAILED;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
CAResult_t CABleGattConnect(const char *remoteAddress)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    VERIFY_NON_NULL_RET(remoteAddress, TZ_BLE_CLIENT_TAG,
                        "remote address is NULL", CA_STATUS_FAILED);

    //Because of the platform issue, we added. Once platform is stablized, then it will be removed
    sleep(1);

    ca_mutex_lock(g_bleClientConnectMutex);

    int ret = bt_gatt_connect(remoteAddress, true);

    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG, "bt_gatt_connect Failed with ret value [%s] ",
                  CABTGetErrorMsg(ret));
        ca_mutex_unlock(g_bleClientConnectMutex);
        return CA_STATUS_FAILED;
    }
    ca_mutex_unlock(g_bleClientConnectMutex);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
CAResult_t CABleGattWatchCharacteristicChanges(bt_gatt_attribute_h service)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    int ret = bt_gatt_watch_characteristic_changes(service);
    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                  "bt_gatt_watch_characteristic_changes failed  with [%s]",
                  CABTGetErrorMsg(ret));

        return CA_STATUS_FAILED;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
CAResult_t CASetCharacteristicDescriptorValue(stGattCharDescriptor_t *stGattCharDescInfo)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    unsigned char noti[4] = {0,};

    char *strUUID = (char *)OICCalloc(5, sizeof(char));

    VERIFY_NON_NULL_RET(strUUID, TZ_BLE_CLIENT_TAG, "calloc failed", CA_STATUS_FAILED);

    snprintf(strUUID, 4, "%x%x", stGattCharDescInfo->desc[3], stGattCharDescInfo->desc[2]);
    noti[0] = stGattCharDescInfo->desc[0];
    noti[1] = stGattCharDescInfo->desc[1];
    noti[2] = 0x01;
    noti[3] = 0x00;

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "desc x0 [%x]", stGattCharDescInfo->desc[0]);
    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "desc x1 [%x]", stGattCharDescInfo->desc[1]);
    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "desc x2 [%x]", stGattCharDescInfo->desc[2]);
    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "desc x3 [%x]", stGattCharDescInfo->desc[3]);


    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "CA_GATT_CONFIGURATION_DESC_UUID strUUID is [%s]",
              strUUID);
    //if (!strncmp(strUUID, CA_GATT_CONFIGURATION_DESC_UUID, 2))
    {
        OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "setting notification/indication for descriptor");

        int ret =  bt_gatt_set_characteristic_desc_value_request(
                           stGattCharDescInfo->characteristic,
                           noti,  4, CABleGattCharacteristicWriteCb);
        if (BT_ERROR_NONE != ret)
        {
            OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                      "bt_gatt_set_characteristic_desc_value_request failed with return[%s]",
                      CABTGetErrorMsg(ret));
            OICFree(strUUID);
            return CA_STATUS_FAILED;
        }
    }
    OICFree(strUUID);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
CAResult_t CABleGattDiscoverDescriptor(bt_gatt_attribute_h service, const char *remoteAddress)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    VERIFY_NON_NULL_RET(service, TZ_BLE_CLIENT_TAG, "service is NULL", CA_STATUS_FAILED);

    int ret = bt_gatt_discover_characteristic_descriptor(service,
                  CABleGattDescriptorDiscoveredCb, NULL);
    if (BT_ERROR_NONE != ret)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                  "bt_gatt_discover_characteristic_descriptor failed with returns[%s]",
                  CABTGetErrorMsg(ret));
        return CA_STATUS_FAILED;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
Exemple #6
0
void CAStartBleGattServerThread(void *data)
{
    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
    ca_mutex_lock(g_bleServerStateMutex);
    if (true == g_isBleGattServerStarted)
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is already running");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    CAResult_t ret  =  CAInitBleGattService();
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_init_service failed");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    sleep(5); // Sleep is must because of the platform issue.

    char *serviceUUID = OIC_BLE_SERVICE_ID;

    ret  = CAAddNewBleServiceInGattServer(serviceUUID);
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewBleServiceInGattServer failed");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    char *charReadUUID = CA_BLE_READ_CHAR_UUID;
    char charReadValue[] = {33, 44, 55, 66}; // These are initial random values

    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
            CA_BLE_INITIAL_BUF_SIZE, true); // For Read Characteristics.
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    char *charWriteUUID = CA_BLE_WRITE_CHAR_UUID;
    char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values


    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
            CA_BLE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    ret = CARegisterBleServicewithGattServer(g_gattSvcPath);
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CARegisterBleServicewithGattServer failed");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    int res = bt_gatt_set_connection_state_changed_cb(CABleGattServerConnectionStateChangedCb,
                                                          NULL);
    if (BT_ERROR_NONE != res)
    {
        OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
                  "bt_gatt_set_connection_state_changed_cb Failed with return as [%s]",
                  CABTGetErrorMsg(res));
        return;
    }

    bt_adapter_le_create_advertiser(&g_hAdvertiser);
    if (NULL == g_hAdvertiser)
    {
        OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_hAdvertiser is NULL");
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    res = bt_adapter_le_start_advertising(g_hAdvertiser, NULL, NULL, NULL);
    if (BT_ERROR_NONE != res)
    {
        OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "bt_adapter_le_start_advertising failed with ret [%d] ",
                  res);
        ca_mutex_unlock(g_bleServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    g_isBleGattServerStarted = true;

    ca_mutex_unlock(g_bleServerStateMutex);

    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG,
            "LE Server initialization complete.");

    GMainContext *thread_context = NULL;

    thread_context = g_main_context_new();

    g_eventLoop = g_main_loop_new(thread_context, FALSE);

    g_main_context_push_thread_default(thread_context);

    g_main_loop_run(g_eventLoop);

    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
}