Ejemplo n.º 1
0
void CAStartLEGattServerThread(void *data)
{
    OIC_LOG(DEBUG, TAG, "IN");
    ca_mutex_lock(g_leServerStateMutex);
    if (true == g_isLEGattServerStarted)
    {
        OIC_LOG(ERROR, TAG, "Gatt Server is already running");
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    CAResult_t ret  =  CAInitLEGattServer();
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAInitLEGattService failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    char *serviceUUID = CA_GATT_SERVICE_UUID;

    ret  = CAAddNewLEServiceInGattServer(serviceUUID);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

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

    // For Read Characteristics.
    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
                                              CA_LE_INITIAL_BUF_SIZE, true);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

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


    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
            CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    ret = CARegisterLEServicewithGattServer(g_gattSvcPath);
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    ret = CALEStartAdvertise(serviceUUID);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
        ca_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return;
    }

    g_isLEGattServerStarted = true;

    ca_mutex_unlock(g_leServerStateMutex);

    OIC_LOG(DEBUG, 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, TAG, "OUT");
}
Ejemplo n.º 2
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");
}
Ejemplo n.º 3
0
CAResult_t CAStartLEGattServer()
{
    OIC_LOG(DEBUG, TAG, "IN");

    oc_mutex_lock(g_leServerStateMutex);
    if (true == g_isLEGattServerStarted)
    {
        OIC_LOG(ERROR, TAG, "Gatt Server is already running");
        oc_mutex_unlock(g_leServerStateMutex);
        return CA_STATUS_OK;
    }

    CAResult_t ret = CAInitLEGattServer();
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAInitLEGattServer failed[%d]", ret);
        oc_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return CA_STATUS_FAILED;
    }

    ret  = CAAddNewLEServiceInGattServer(CA_GATT_SERVICE_UUID);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret);
        oc_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return CA_STATUS_FAILED;
    }

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

    // For Read Characteristics.
    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
                                              CA_LE_INITIAL_BUF_SIZE, true);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
        oc_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return CA_STATUS_FAILED;
    }

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


    ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
            CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
        oc_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return CA_STATUS_FAILED;
    }

    ret = CARegisterLEServicewithGattServer(g_gattSvcPath);
    if (CA_STATUS_OK != ret )
    {
        OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret);
        oc_mutex_unlock(g_leServerStateMutex);
        CATerminateLEGattServer();
        return CA_STATUS_FAILED;
    }

    /*
     * TV Easysetup does not use IoTivity BLE advertisement.
     */

    // 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;
    // }

    g_isLEGattServerStarted = true;

    oc_mutex_unlock(g_leServerStateMutex);

    OIC_LOG(DEBUG, TAG, "OUT");
    return CA_STATUS_OK;
}