void *deviceLevel2ReceiveThread::Entry()
{
    vscpEvent *pEvent;

    // Must be a valid main object pointer
    if (NULL == m_pMainThreadObj) return NULL;

    int rv;
    while (!TestDestroy() && !m_bQuit) {

        pEvent = new vscpEvent;
        if (NULL == pEvent) continue;
        rv = m_pMainThreadObj->m_pDeviceItem->m_proc_VSCPBlockingReceive(
                    m_pMainThreadObj->m_pDeviceItem->m_openHandle, pEvent, 500);

        if ((CANAL_ERROR_SUCCESS != rv) || (NULL == pEvent)) {
            delete pEvent;
            continue;
        }

        // Identify ourselves
        pEvent->obid = m_pMainThreadObj->m_pDeviceItem->m_pClientItem->m_clientID;

        // If timestamp is zero we set it here
        if ( 0 == pEvent->timestamp ) {
            pEvent->timestamp = vscp_makeTimeStamp();
        }

        // If no GUID is set,
        //      - Set driver GUID if define
        //      - Set interface GUID if no driver GUID defined.

        uint8_t ifguid[16];

        // Save nickname
        uint8_t nickname_msb = pEvent->GUID[14];
        uint8_t nickname_lsb = pEvent->GUID[15];

        // Set if to use
        memcpy( ifguid, pEvent->GUID, 16 );
        ifguid[14] = 0;
        ifguid[15] = 0;

        // If if is set to zero use interface id
        if ( vscp_isGUIDEmpty( ifguid ) ) {

            // Set driver GUID if set
            if ( !m_pMainThreadObj->m_pDeviceItem->m_interface_guid.isNULL() ) {
                m_pMainThreadObj->m_pDeviceItem->m_interface_guid.writeGUID( pEvent->GUID );
            }
            else {
                // If no driver GUID set use interface GUID
                m_pMainThreadObj->m_pDeviceItem->m_pClientItem->m_guid.writeGUID( pEvent->GUID );
            }

            // Preserve nickname
            pEvent->GUID[14] = nickname_msb;
            pEvent->GUID[15] = nickname_lsb;

        }

        // There must be room in the receive queue (even if rom (or whisky) has been better)
        if (m_pMainThreadObj->m_pCtrlObject->m_maxItemsInClientReceiveQueue >
                m_pMainThreadObj->m_pCtrlObject->m_clientOutputQueue.GetCount()) {

            m_pMainThreadObj->m_pCtrlObject->m_mutexClientOutputQueue.Lock();
            m_pMainThreadObj->m_pCtrlObject->m_clientOutputQueue.Append(pEvent);
            m_pMainThreadObj->m_pCtrlObject->m_semClientOutputQueue.Post();
            m_pMainThreadObj->m_pCtrlObject->m_mutexClientOutputQueue.Unlock();

        }
        else {
            if (NULL == pEvent) vscp_deleteVSCPevent(pEvent);
        }

    }

    return NULL;
}
void *
deviceLevel2ReceiveThread(void *pData)
{
    vscpEvent *pEvent;

    CDeviceItem *pDevItem = (CDeviceItem *)pData;
    if (NULL == pDevItem) {
        syslog(
          LOG_CRIT,
          "deviceLevel2ReceiveThread quitting due to NULL DevItem object.");
        return NULL;
    }

    int rv;
    while (!pDevItem->m_bQuit) {

        pEvent = new vscpEvent;
        if (NULL == pEvent) continue;
        rv = pDevItem->m_proc_VSCPBlockingReceive(
          pDevItem->m_openHandle, pEvent, 500);

        if ((CANAL_ERROR_SUCCESS != rv) || (NULL == pEvent)) {
            delete pEvent;
            continue;
        }

        // Identify ourselves
        pEvent->obid = pDevItem->m_pClientItem->m_clientID;

        // If timestamp is zero we set it here
        if (0 == pEvent->timestamp) {
            pEvent->timestamp = vscp_makeTimeStamp();
        }

        // If no GUID is set,
        //      - Set driver GUID if define
        //      - Set interface GUID if no driver GUID defined.

        uint8_t ifguid[16];

        // Save nickname
        uint8_t nickname_msb = pEvent->GUID[14];
        uint8_t nickname_lsb = pEvent->GUID[15];

        // Set if to use
        memcpy(ifguid, pEvent->GUID, 16);
        ifguid[14] = 0;
        ifguid[15] = 0;

        // If if is set to zero use interface id
        if (vscp_isGUIDEmpty(ifguid)) {

            // Set driver GUID if set
            if (!pDevItem->m_interface_guid.isNULL()) {
                pDevItem->m_interface_guid.writeGUID(pEvent->GUID);
            } else {
                // If no driver GUID set use interface GUID
                pDevItem->m_pClientItem->m_guid.writeGUID(pEvent->GUID);
            }

            // Preserve nickname
            pEvent->GUID[14] = nickname_msb;
            pEvent->GUID[15] = nickname_lsb;
        }

        // There must be room in the receive queue (even if room (or whisky) has
        // been better)
        if (pDevItem->m_pCtrlObject->m_maxItemsInClientReceiveQueue >
            pDevItem->m_pCtrlObject->m_clientOutputQueue.size()) {

            pthread_mutex_lock(
              &pDevItem->m_pCtrlObject->m_mutexClientOutputQueue);
            pDevItem->m_pCtrlObject->m_clientOutputQueue.push_back(pEvent);
            sem_post(&pDevItem->m_pCtrlObject->m_semClientOutputQueue);
            pthread_mutex_unlock(
              &pDevItem->m_pCtrlObject->m_mutexClientOutputQueue);

        } else {
            if (NULL == pEvent) vscp_deleteVSCPevent_v2(&pEvent);
        }
    }

    return NULL;
}