Example #1
0
CAResult_t CAVerifyOICServiceByServiceHandle(bt_gatt_attribute_h serviceHandle)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");

    VERIFY_NON_NULL(serviceHandle, NULL, "Param serviceHandle is NULL");

    char *uuid = NULL;
    int ret = bt_gatt_get_service_uuid(serviceHandle, &uuid);

    if (0 != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "bt_gatt_get_service_uuid failed !");
        return CA_STATUS_FAILED;
    }

    if (strcasecmp(uuid, OIC_BLE_SERVICE_ID) != 0)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "It is not OIC service!");
        return CA_STATUS_FAILED;
    }
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
    return CA_STATUS_OK;
}
bool CABleGattCharacteristicsDiscoveredCb(int result,
        int inputIndex, int total,
        bt_gatt_attribute_h characteristic, void *userData)
{

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    VERIFY_NON_NULL_RET(characteristic, TZ_BLE_CLIENT_TAG, "Param characteristic is NULL", false);

    VERIFY_NON_NULL_RET(userData, TZ_BLE_CLIENT_TAG, "Param userData is NULL", false);

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
              "result [%d] input_index [%d] total [%d]",
              result, inputIndex, total);

    BLEServiceInfo *bleServiceInfo = NULL;

    ca_mutex_lock(g_bleServiceListMutex);

    char *bdAddress = (char *) userData;
    CAGetBLEServiceInfo(g_bLEServiceList, bdAddress, &bleServiceInfo);

    ca_mutex_unlock(g_bleServiceListMutex);

    char *uuid = NULL;
    bt_gatt_get_service_uuid(characteristic, &uuid);

    VERIFY_NON_NULL_RET(uuid, TZ_BLE_CLIENT_TAG, "uuid is NULL", false);

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "New Characteristics[%s] of uuid[%s] is obtained",
              (char *)characteristic, uuid);

    if(0 == strcasecmp(uuid, CA_GATT_RESPONSE_CHRC_UUID)) // Server will read on this characterisctics
    {
        OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Read characteristics is obtained");
        OICFree(uuid);
        CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_READ_CHAR, bleServiceInfo);
        if (CA_STATUS_OK != retVal)
        {
            OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAAppendBLECharInfo failed! ");
            return false;
        }

        stGattServiceInfo_t *stTemp = (stGattServiceInfo_t *)OICCalloc(1,
                                                                      sizeof(stGattServiceInfo_t));

        VERIFY_NON_NULL_RET(stTemp, TZ_BLE_CLIENT_TAG, "calloc failed!", false);

        stTemp->address = OICStrdup(bdAddress);
        if (NULL == stTemp->address)
        {
            OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "Malloc failed!");
            OICFree(stTemp);
            return false;
        }

        bt_gatt_clone_attribute_handle(&(stTemp->serviceInfo), characteristic);

        ca_mutex_lock(g_bleClientThreadPoolMutex);
        if (NULL == g_bleClientThreadPool)
        {
            OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "g_bleClientThreadPool is NULL");
            bt_gatt_destroy_attribute_handle(stTemp->serviceInfo);
            OICFree(stTemp->address);
            OICFree(stTemp);
            ca_mutex_unlock(g_bleClientThreadPoolMutex);
            return false;
        }

        retVal = ca_thread_pool_add_task(g_bleClientThreadPool,
                                        CADiscoverDescriptorThread,
                                        stTemp);
        if (CA_STATUS_OK != retVal)
        {
            OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                      "ca_thread_pool_add_task failed with ret [%d]", retVal);
            bt_gatt_destroy_attribute_handle(stTemp->serviceInfo);
            OICFree(stTemp->address);
            OICFree(stTemp);
            ca_mutex_unlock(g_bleClientThreadPoolMutex);
            return false;
        }
        ca_mutex_unlock(g_bleClientThreadPoolMutex);
    }
    else if (0 == strcasecmp(uuid, CA_GATT_REQUEST_CHRC_UUID)) // Server will write on this characteristics.
    {
        OICFree(uuid);
        OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Write characteristics is obtained");
        CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_WRITE_CHAR, bleServiceInfo);
        if (CA_STATUS_OK != retVal)
        {
            OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAAppendBLECharInfo failed ");
            return false;
        }
    }
    else
    {
        OICFree(uuid);
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "service_uuid characteristics is UNKNOWN");
        return false;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return true;
}