Esempio n. 1
0
/**
 * ca_context_change_device:
 * @c: the context to change the backend device for
 * @device: the backend device to use, in a format that is specific to the backend.
 *
 * Specify the backend device to use. This function may be called not be called after
 * ca_context_open() suceeded. This function might suceed even when
 * the specified driver backend is not available. Use
 * ca_context_open() to find out whether the backend is available
 *
 * Depending on the backend use this might or might not cause all
 * currently playing event sounds to be moved to the new device..
 *
 * Returns: 0 on success, negative error code on error.
 */
int ca_context_change_device(ca_context *c, const char *device) {
    char *n;
    int ret;

    ca_return_val_if_fail(!ca_detect_fork(), CA_ERROR_FORKED);
    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_mutex_lock(c->mutex);

    if (!device)
        n = NULL;
    else if (!(n = ca_strdup(device))) {
        ret = CA_ERROR_OOM;
        goto fail;
    }

    ret = c->opened ? driver_change_device(c, n) : CA_SUCCESS;

    if (ret == CA_SUCCESS) {
        ca_free(c->device);
        c->device = n;
    } else
        ca_free(n);

fail:
    ca_mutex_unlock(c->mutex);

    return ret;
}
Esempio n. 2
0
/**
 * ca_context_set_driver:
 * @c: the context to change the backend driver for
 * @driver: the backend driver to use (e.g. "alsa", "pulse", "null", ...)
 *
 * Specify the backend driver used. This function may not be called again after
 * ca_context_open() suceeded. This function might suceed even when
 * the specified driver backend is not available. Use
 * ca_context_open() to find out whether the backend is available.
 *
 * Returns: 0 on success, negative error code on error.
 */
int ca_context_set_driver(ca_context *c, const char *driver) {
    char *n;
    int ret;

    ca_return_val_if_fail(!ca_detect_fork(), CA_ERROR_FORKED);
    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_mutex_lock(c->mutex);
    ca_return_val_if_fail_unlock(!c->opened, CA_ERROR_STATE, c->mutex);

    if (!driver)
        n = NULL;
    else if (!(n = ca_strdup(driver))) {
        ret = CA_ERROR_OOM;
        goto fail;
    }

    ca_free(c->driver);
    c->driver = n;

    ret = CA_SUCCESS;

fail:
    ca_mutex_unlock(c->mutex);

    return ret;
}
Esempio n. 3
0
CAResult_t CAAddNewCharacteristicsToGattServer(const char *svcPath, const char *charUUID,
        const char *charValue, int charValueLen, bool read)
{

    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");

    char *charFlags[1];
    if(read)
    {
        charFlags[0] = "notify";
    }
    else
    {
        charFlags[0] = "write-without-response";
    }

    size_t flagLen = sizeof(charFlags) / sizeof(charFlags[0]);

    char *charPath = NULL;
    int ret = bt_gatt_add_characteristic(charUUID, charValue, charValueLen, charFlags, flagLen,
                  svcPath, &charPath);

    if (0 != ret || NULL == charPath)
    {
        OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
                  "bt_gatt_add_characteristic  failed with ret [%d]", ret);
        return CA_STATUS_FAILED;
    }

    OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG,
              "bt_gatt_add_characteristic charPath obtained: %s", charPath);

    ca_mutex_lock(g_bleCharacteristicMutex);

    if (read)
    {
        if (NULL != g_gattReadCharPath)
        {
            OICFree(g_gattReadCharPath);
            g_gattReadCharPath = NULL;
        }
        g_gattReadCharPath = charPath;

    }
    else
    {
        if (NULL != g_gattWriteCharPath)
        {
            OICFree(g_gattWriteCharPath);
            g_gattWriteCharPath = NULL;
        }
        g_gattWriteCharPath = charPath;
    }

    ca_mutex_unlock(g_bleCharacteristicMutex);

    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
    return CA_STATUS_OK;
}
static void CALETearDownDBus(CALEContext * context)
{
    assert(context != NULL);

    /*
      Minimize the time we hold the global lock by only clearing the
      global state, and pushing resource finalization outside the global
      lock.
    */
    ca_mutex_lock(context->lock);

    GDBusConnection * const connection = context->connection;
    context->connection = NULL;

    GDBusObjectManager * const object_manager = context->object_manager;
    context->object_manager = NULL;

    GList * const objects = context->objects;
    context->objects = NULL;

    GList * const adapters = context->adapters;
    context->adapters = NULL;

    GList * const devices = context->devices;
    context->devices = NULL;

    guint const interfaces_added   = context->interfaces_added_sub_id;
    guint const interfaces_removed = context->interfaces_removed_sub_id;

    context->interfaces_added_sub_id   = 0;
    context->interfaces_removed_sub_id = 0;

    ca_mutex_unlock(context->lock);

    // Destroy the device proxies list.
    g_list_free_full(devices, g_object_unref);

    // Destroy the adapter proxies list.
    g_list_free_full(adapters, g_object_unref);

    // Destroy the list of objects obtained from the ObjectManager.
    g_list_free_full(objects, g_object_unref);

    // Destroy the ObjectManager proxy.
    if (object_manager != NULL)
    {
        g_object_unref(object_manager);
    }

    // Tear down the D-Bus connection to the system bus.
    if (connection != NULL)
    {
        g_dbus_connection_signal_unsubscribe(connection,
                                             interfaces_added);
        g_dbus_connection_signal_unsubscribe(connection,
                                             interfaces_removed);
        g_object_unref(connection);
    }
}
Esempio n. 5
0
void CAPeripheralForEachService(GFunc func, void * user_data)
{
    ca_mutex_lock(g_context.lock);

    g_list_foreach(g_context.gatt_services, func, user_data);

    ca_mutex_unlock(g_context.lock);
}
CAResult_t CAUnSetLEAdapterStateChangedCb()
{
    ca_mutex_lock(g_context.lock);
    g_context.on_device_state_changed = NULL;
    ca_mutex_unlock(g_context.lock);

    return CA_STATUS_OK;
}
Esempio n. 7
0
void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
{
    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
    ca_mutex_lock(g_bleServerThreadPoolMutex);
    g_bleServerThreadPool = handle;
    ca_mutex_unlock(g_bleServerThreadPoolMutex);
    OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
}
CAResult_t CAStartLEGattClient()
{
    CAResult_t result = CACentralStart(&g_context);

    if (result != CA_STATUS_OK)
    {
        return result;
    }

    ca_mutex_lock(g_context.lock);
    bool found_peripherals = (g_context.devices != NULL);
    ca_mutex_unlock(g_context.lock);

    if (!found_peripherals)
    {
        // Wait for LE peripherals to be discovered.

        // Number of times to wait for discovery to complete.
        static int const retries = 5;

        static uint64_t const timeout =
            2 * MICROSECS_PER_SEC;  // Microseconds

        if (!CALEWaitForNonEmptyList(&g_context.devices,
                                     retries,
                                     timeout))
        {
            return result;
        }
    }

    /*
      Stop discovery so that we can connect to LE peripherals.
      Otherwise, the bluetooth subsystem will claim the adapter is
      busy.
    */

    result = CACentralStopDiscovery(&g_context);

    if (result != CA_STATUS_OK)
    {
        return result;
    }

    bool const connected = CACentralConnectToAll(&g_context);

    if (!connected)
    {
        return result;
    }

    /**
     * @todo Verify notifications have been enabled on all response
     *       characteristics.
     */

    return CAGattClientInitialize(&g_context);
}
Esempio n. 9
0
CAResult_t  CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
        const uint8_t *data, uint32_t dataLen,
        CALETransferType_t type, int32_t position)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    VERIFY_NON_NULL(data, TZ_BLE_CLIENT_TAG, "data is NULL");

    if (0 >= dataLen)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "dataLen is less than or equal zero. Invalid input!");
        return CA_STATUS_INVALID_PARAM;
    }

    BLEServiceInfo *bleServiceInfo = NULL;

    CAResult_t ret =  CA_STATUS_FAILED;

    ca_mutex_lock(g_bleServiceListMutex);
    if ( LE_UNICAST == type)
    {
        VERIFY_NON_NULL(remoteAddress, TZ_BLE_CLIENT_TAG, "remoteAddress is NULL");

        ret = CAGetBLEServiceInfo(g_bLEServiceList, remoteAddress, &bleServiceInfo);
    }
    else if ( LE_MULTICAST == type)
    {
        ret = CAGetBLEServiceInfoByPosition(g_bLEServiceList, position, &bleServiceInfo);
    }
    ca_mutex_unlock(g_bleServiceListMutex);

    if (CA_STATUS_OK != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "CAGetBLEServiceInfoByPosition is failed");
        return CA_STATUS_FAILED;
    }

    VERIFY_NON_NULL(bleServiceInfo, TZ_BLE_CLIENT_TAG, "bleServiceInfo is NULL");

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Updating the data of length [%u] to [%s] ", dataLen,
              bleServiceInfo->bdAddress);

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Updating to write char [%s]",
              bleServiceInfo->read_char);

    int result = bt_gatt_set_characteristic_value(bleServiceInfo->write_char, (unsigned char *)data,
                     dataLen);
    if (BT_ERROR_NONE != result)
    {
        OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
                  "bt_gatt_set_characteristic_value Failed with return val [%d]",
                  result);
        return CA_STATUS_FAILED;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
    return CA_STATUS_OK;
}
Esempio n. 10
0
void CAStartBleGattClientThread(void *data)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    ca_mutex_lock(g_bleClientStateMutex);

    if (true  == g_isBleGattClientStarted)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "Gatt Client is already running!!");
        ca_mutex_unlock(g_bleClientStateMutex);
        return;
    }

    CAResult_t  ret = CABleGattSetScanParameter();
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "CABleSetScanParameter Failed");
        ca_mutex_unlock(g_bleClientStateMutex);
        CATerminateLEGattClient();
        return;
    }

    ret = CABleGattSetCallbacks();
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "CABleGattSetCallbacks Failed");
        ca_mutex_unlock(g_bleClientStateMutex);
        CATerminateLEGattClient();
        return;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "Starting LE device discovery");

    ret = CABleGattStartDeviceDiscovery();
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_start_device_discovery Failed");
        ca_mutex_unlock(g_bleClientStateMutex);
        CATerminateLEGattClient();
        return;
    }

    g_isBleGattClientStarted = true;

    ca_mutex_unlock(g_bleClientStateMutex);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "Giveing the control to threadPool");

    GMainContext *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_CLIENT_TAG, "OUT");
}
Esempio n. 11
0
static bool CAPeripheralRegisterGattServices(
    CAPeripheralContext * context)
{
    assert(context != NULL);

    bool success = true;

    ca_mutex_lock(context->lock);

    for (GList * l = context->gatt_services; l != NULL; l = l->next)
    {
        CAGattService * const service = l->data;

        // Register the OIC service with the corresponding BlueZ Gatt
        // Manager.

        /*
          org.bluez.GattManager1.RegisterService() accepts two
          parameters: the service object path, and an options
          dictionary.  No options are used so pass a NULL pointer to
          reflect an empty dictionary.
        */
        GVariant * const parameters =
            g_variant_new("(oa{sv})", service->object_path, NULL);

        GError * error = NULL;

        GVariant * const ret =
            g_dbus_proxy_call_sync(
                service->gatt_manager,
                "RegisterService",
                parameters,
                G_DBUS_CALL_FLAGS_NONE,
                -1,    // timeout (default == -1),
                NULL,  // cancellable
                &error);

        if (ret == NULL)
        {
            OIC_LOG_V(ERROR,
                      TAG,
                      "GATT service registration failed: %s",
                      error->message);

            g_error_free(error);

            success = false;

            break;
        }

        g_variant_unref(ret);
    }

    ca_mutex_unlock(context->lock);

    return success;
}
Esempio n. 12
0
void CABleGattDescriptorDiscoveredCb(int result, unsigned char format, int total,
                                     bt_gatt_attribute_h descriptor,
                                     bt_gatt_attribute_h characteristic, void *userData)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

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

    VERIFY_NON_NULL_VOID(stTemp, TZ_BLE_CLIENT_TAG, "malloc failed!");

    stTemp->desc = (uint8_t *)OICMalloc(total);
    if (NULL == stTemp->desc)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "malloc failed");
        OICFree(stTemp);
        return;
    }
    memcpy(stTemp->desc, descriptor, total);

    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "result[%d] format[%d] total[%d]", result, format, total);
    OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "characteristic [%s]", (const char *) characteristic);


    bt_gatt_clone_attribute_handle(&(stTemp->characteristic), characteristic);
    stTemp->total = total;

    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->characteristic);
        OICFree(stTemp->desc);
        OICFree(stTemp);
        ca_mutex_unlock(g_bleClientThreadPoolMutex);
        return;
    }

    CAResult_t ret = ca_thread_pool_add_task(g_bleClientThreadPool,
                                            CASetCharacteristicDescriptorValueThread,
                                            stTemp);
    if (CA_STATUS_OK != ret)
    {
        OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "ca_thread_pool_add_task failed");
        bt_gatt_destroy_attribute_handle(stTemp->characteristic);
        OICFree(stTemp->desc);
        OICFree(stTemp);
        ca_mutex_unlock(g_bleClientThreadPoolMutex);
        return;
    }

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG,
            "LE Client initialization flow complete");

    ca_mutex_unlock(g_bleClientThreadPoolMutex);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
Esempio n. 13
0
CAResult_t CASetLEAdapterStateChangedCb(
    CALEDeviceStateChangedCallback callback)
{
    ca_mutex_lock(g_context.lock);
    g_context.on_device_state_changed = callback;
    ca_mutex_unlock(g_context.lock);

    return CA_STATUS_OK;
}
Esempio n. 14
0
CAResult_t CAUnsetLENWConnectionStateChangedCb()
{
    OIC_LOG(DEBUG, TAG, "IN");
    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
    g_bleConnectionStateChangedCallback = NULL;
    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
    OIC_LOG(DEBUG, TAG, "OUT");
    return CA_STATUS_OK;
}
Esempio n. 15
0
CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList)
{
    OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");

    VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "u_array_list is null");
    VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null");
    VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
                    "g_networkMonitorContextMutex is null");

    // Get the interface and ipaddress information from cache
    ca_mutex_lock(g_networkMonitorContextMutex);
    if (!g_networkMonitorContext->netInterfaceList
        || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
    {
        OIC_LOG(ERROR, IP_MONITOR_TAG, "Network not enabled");
        ca_mutex_unlock(g_networkMonitorContextMutex);
        return CA_ADAPTER_NOT_ENABLED;
    }

    uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
    OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo list length [%d]",
            list_length);
    for (uint32_t list_index = 0; list_index < list_length; list_index++)
    {
        CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
                g_networkMonitorContext->netInterfaceList, list_index);
        if (!info)
        {
            continue;
        }
        OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo ip [%s]",
                  info->ipAddress);
        CANetInfo_t *newNetinfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
        if (!newNetinfo)
        {
            OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed!");
            ca_mutex_unlock(g_networkMonitorContextMutex);
            return CA_MEMORY_ALLOC_FAILED;
        }

        *newNetinfo = *info;

        CAResult_t result = u_arraylist_add(*netInterfaceList, (void *) newNetinfo);
        if (CA_STATUS_OK != result)
        {
            OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
            ca_mutex_unlock(g_networkMonitorContextMutex);
            return CA_STATUS_FAILED;
        }
    }

    ca_mutex_unlock(g_networkMonitorContextMutex);

    OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
    return CA_STATUS_OK;
}
Esempio n. 16
0
int ca_context_play_full(ca_context *c, uint32_t id, ca_proplist *p, ca_finish_callback_t cb, void *userdata) {
    int ret;
    const char *t;
    ca_bool_t enabled = TRUE;

    ca_return_val_if_fail(!ca_detect_fork(), CA_ERROR_FORKED);
    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(!userdata || cb, CA_ERROR_INVALID);

    ca_mutex_lock(c->mutex);

    ca_return_val_if_fail_unlock(ca_proplist_contains(p, CA_PROP_EVENT_ID) ||
                                 ca_proplist_contains(c->props, CA_PROP_EVENT_ID) ||
                                 ca_proplist_contains(p, CA_PROP_MEDIA_FILENAME) ||
                                 ca_proplist_contains(c->props, CA_PROP_MEDIA_FILENAME), CA_ERROR_INVALID, c->mutex);

    ca_mutex_lock(c->props->mutex);
    if ((t = ca_proplist_gets_unlocked(c->props, CA_PROP_CANBERRA_ENABLE)))
        enabled = !ca_streq(t, "0");
    ca_mutex_unlock(c->props->mutex);

    ca_mutex_lock(p->mutex);
    if ((t = ca_proplist_gets_unlocked(p, CA_PROP_CANBERRA_ENABLE)))
        enabled = !ca_streq(t, "0");
    ca_mutex_unlock(p->mutex);

    ca_return_val_if_fail_unlock(enabled, CA_ERROR_DISABLED, c->mutex);

    if ((ret = context_open_unlocked(c)) < 0)
        goto finish;

    ca_assert(c->opened);

    ret = driver_play(c, id, p, cb, userdata);
	
	vizaudio_display(p);
finish:

    ca_mutex_unlock(c->mutex);

    return ret;
}
CAResult_t CAEDRStopUnicastServer()
{
    OIC_LOG(DEBUG, TAG, "CAEDRStopUnicastServer");

    ca_mutex_lock(g_mutexUnicastServer);
    g_stopUnicast = true;
    ca_mutex_unlock(g_mutexUnicastServer);

    return CA_STATUS_OK;
}
Esempio n. 18
0
void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
{
    OIC_LOG(DEBUG, TAG, "IN");

    ca_mutex_lock(g_leReqRespCbMutex);
    g_leServerDataReceivedCallback = callback;
    ca_mutex_unlock(g_leReqRespCbMutex);

    OIC_LOG(DEBUG, TAG, "OUT");
}
Esempio n. 19
0
void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    ca_mutex_lock(g_bleClientThreadPoolMutex);
    g_bleClientThreadPool = handle;
    ca_mutex_unlock(g_bleClientThreadPoolMutex);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
Esempio n. 20
0
void CAHandleRequestResponseCallbacks()
{
#ifdef SINGLE_THREAD
    CAReadData();
    CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
#else
#ifdef SINGLE_HANDLE
    // parse the data and call the callbacks.
    // #1 parse the data
    // #2 get endpoint

    ca_mutex_lock(g_receiveThread.threadMutex);

    u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);

    ca_mutex_unlock(g_receiveThread.threadMutex);

    if (NULL == item)
    {
        return;
    }

    // get values
    void *msg = item->msg;

    if (NULL == msg)
    {
        return;
    }

    // get endpoint
    CAData_t *td = (CAData_t *) msg;

    if (td->requestInfo && g_requestHandler)
    {
        OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
        g_requestHandler(td->remoteEndpoint, td->requestInfo);
    }
    else if (td->responseInfo && g_responseHandler)
    {
        OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
        g_responseHandler(td->remoteEndpoint, td->responseInfo);
    }
    else if (td->errorInfo && g_errorHandler)
    {
        OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
        g_errorHandler(td->remoteEndpoint, td->errorInfo);
    }

    CADestroyData(msg, sizeof(CAData_t));
    OICFree(item);

#endif /* SINGLE_HANDLE */
#endif
}
Esempio n. 21
0
CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
{
    OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");

    // Input validation
    VERIFY_NON_NULL(threadPool, IP_SERVER_TAG, "Thread pool handle is NULL");

    // Initialize mutex
    if (CA_STATUS_OK != CAIPServerCreateMutex())
    {
        OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create mutex!");
        return CA_STATUS_FAILED;
    }

    ca_mutex_lock(g_mutexAdapterServerContext);
    g_adapterIPServerContext = (CAAdapterIPServerContext_t *) OICCalloc(1,
                                 sizeof(CAAdapterIPServerContext_t));

    if (!g_adapterIPServerContext)
    {
        OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
        ca_mutex_unlock(g_mutexAdapterServerContext);
        return CA_MEMORY_ALLOC_FAILED;
    }

    g_adapterIPServerContext->threadPool = threadPool;

    ca_mutex_unlock(g_mutexAdapterServerContext);

    ca_mutex_lock(g_mutexServerInfoList);

    g_serverInfoList = u_arraylist_create();
    if (!g_serverInfoList)
    {
        OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
        ca_mutex_unlock(g_mutexServerInfoList);
        return CA_MEMORY_ALLOC_FAILED;
    }
    ca_mutex_unlock(g_mutexServerInfoList);
    OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
    return CA_STATUS_OK;
}
Esempio n. 22
0
void task(void *data)
{
    printf("[TASK] Task is executing: data: %s\n", (char *) data);

    //Signal the condition that task has been completed
    printf("[TASK] Signaling the condition\n");
    ca_mutex_lock(g_mutex);
    g_condFlag = true;
    ca_cond_signal(g_cond);
    ca_mutex_unlock(g_mutex);
}
Esempio n. 23
0
static void CALEStartEventLoop(void * data)
{
    CALEContext * const context = data;

    assert(context != NULL);

    // Create the event loop.
    GMainContext * const loop_context = g_main_context_new();
    GMainLoop * const event_loop = g_main_loop_new(loop_context, FALSE);

    g_main_context_push_thread_default(loop_context);

    /*
      We have to do the BlueZ object manager client initialization and
      signal subscription here so that the corresponding asynchronous
      signal handling occurs in the same thread as the one running the
      GLib event loop.
    */
    if (CALESetUpDBus(&g_context))
    {
        ca_mutex_lock(context->lock);

        assert(context->event_loop == NULL);
        context->event_loop = event_loop;

        ca_mutex_unlock(context->lock);

        /*
          Add an idle handler that notifies a thread waiting for the
          GLib event loop to run that the event loop is actually
          running.  We do this in the context of the event loop itself
          to avoid race conditions.
        */
        GSource * const source = g_idle_source_new();
        g_source_set_priority(source, G_PRIORITY_HIGH_IDLE);
        g_source_set_callback(source,
                              CALEEventLoopStarted,
                              &context->le_started,  // data
                              NULL);                 // notify
        (void) g_source_attach(source, loop_context);
        g_source_unref(source);

        g_main_loop_run(event_loop);  // Blocks until loop is quit.

        CALETearDownDBus(&g_context);
    }

    /*
      Clean up in the same thread to avoid having to explicitly bump
      the ref count to retain ownership.
    */
    g_main_context_unref(loop_context);
    g_main_loop_unref(event_loop);
}
Esempio n. 24
0
CAResult_t CAIPStartNetworkMonitor()
{
    OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");

    ca_mutex_lock(g_stopNetworkMonitorMutex);
    g_stopNetworkMonitor = false;
    ca_mutex_unlock(g_stopNetworkMonitorMutex);

    OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
    return CA_STATUS_OK;
}
void CAManagerSetAutoConnectionFlag(JNIEnv *env, jstring jaddress, bool flag)
{
    OIC_LOG(DEBUG, TAG, "IN-CAManagerSetAutoConnectionFlag");
    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(jaddress, TAG, "jaddress");

    ca_mutex_lock(g_deviceACDataListMutex);

    const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
    if (!address)
    {
        OIC_LOG(ERROR, TAG, "address is null");
        ca_mutex_unlock(g_deviceACDataListMutex);
        return;
    }

    size_t length = u_arraylist_length(g_deviceACDataList);
    for (size_t idx = 0; idx < length; idx++)
    {
        CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
                                                                           idx);
        if (!curData)
        {
            OIC_LOG(ERROR, TAG, "curData is null");
            (*env)->ReleaseStringUTFChars(env, jaddress, address);
            ca_mutex_unlock(g_deviceACDataListMutex);
            return;
        }

        const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
        if (!setAddress)
        {
            OIC_LOG(ERROR, TAG, "address is null");
            (*env)->ReleaseStringUTFChars(env, jaddress, address);
            ca_mutex_unlock(g_deviceACDataListMutex);
            return;
        }

        if (!strcmp(setAddress, address))
        {
            OIC_LOG_V(DEBUG, TAG, "flag is set to %d", flag);
            curData->isAutoConnect = flag;
            (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
            (*env)->ReleaseStringUTFChars(env, jaddress, address);
            ca_mutex_unlock(g_deviceACDataListMutex);
            return;
        }
        (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
    }
    (*env)->ReleaseStringUTFChars(env, jaddress, address);
    ca_mutex_unlock(g_deviceACDataListMutex);

    OIC_LOG(DEBUG, TAG, "OUT-CAManagerSetAutoConnectionFlag");
}
Esempio n. 26
0
static CAResult_t CAIPStartPacketReceiverHandler()
{
    OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");

    ca_mutex_lock(g_mutexServerInfoList);

    uint32_t listLength = u_arraylist_length(g_serverInfoList);

    ca_mutex_unlock(g_mutexServerInfoList);

    ca_mutex_lock(g_mutexAdapterServerContext);

    if (!g_adapterIPServerContext)
    {
        OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterIPServerContext NULL");
        ca_mutex_unlock(g_mutexAdapterServerContext);
        return CA_STATUS_FAILED;
    }

    if (1 == listLength) //Its first time.
    {
        g_packetHandlerStopFlag = false;
        if (CA_STATUS_OK != ca_thread_pool_add_task(g_adapterIPServerContext->threadPool,
                                                   CAReceiveHandler, NULL ))
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "thread_pool_add_task failed!");
            ca_mutex_unlock(g_mutexAdapterServerContext);
            return CA_STATUS_FAILED;
        }
        OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread started successfully.");
    }
    else
    {
        OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread already is running");
    }
    ca_mutex_unlock(g_mutexAdapterServerContext);

    OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");

    return CA_STATUS_OK;
}
Esempio n. 27
0
void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
{
    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");

    ca_mutex_lock(g_bleReqRespClientCbMutex);

    g_bleClientDataReceivedCallback = callback;

    ca_mutex_unlock(g_bleReqRespClientCbMutex);

    OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
CAResult_t CAEDRStopMulticastServer()
{
    OIC_LOG(DEBUG, TAG, "CAEDRStopMulticastServer");

    ca_mutex_lock(g_mutexMulticastServer);
    g_stopMulticast = true;
    ca_mutex_unlock(g_mutexMulticastServer);

    OIC_LOG(INFO, TAG, "Multicast server stopped");

    return CA_STATUS_OK;
}
Esempio n. 29
0
void CATerminateLEGattServer()
{
    OIC_LOG(DEBUG, TAG, "IN");

    // Service and characteristics path will be freed by the platform.
    ca_mutex_lock(g_leServiceMutex);
    g_gattSvcPath = NULL;
    ca_mutex_unlock(g_leServiceMutex);

    ca_mutex_lock(g_leCharacteristicMutex);
    g_gattReadCharPath = NULL;
    g_gattWriteCharPath = NULL;
    ca_mutex_unlock(g_leCharacteristicMutex);

    ca_mutex_lock(g_leServerThreadPoolMutex);
    g_leServerThreadPool = NULL;
    ca_mutex_unlock(g_leServerThreadPoolMutex);

    // Terminating all mutex variables.
    CATerminateGattServerMutexVariables();
    OIC_LOG(DEBUG, TAG, "OUT");
}
Esempio n. 30
0
CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
{
    OIC_LOG(DEBUG, TAG, "IN");

    OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");

    ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
    CALESetNetStateCallback(callback);
    ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);

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