Exemplo n.º 1
0
void* PCSCLite::HandlerFunction(void* arg) {

    LONG result = SCARD_S_SUCCESS;
    AsyncBaton* async_baton = static_cast<AsyncBaton*>(arg);
    PCSCLite* pcsclite = async_baton->pcsclite;
    async_baton->async_result = new AsyncResult();
    while (!pcsclite->m_closing && (result == SCARD_S_SUCCESS)) {
        /* Lock mutex. It'll be unlocked after the callback has been sent */
        pthread_mutex_lock(&pcsclite->m_mutex);
        /* Get card readers */
        result = pcsclite->get_card_readers(pcsclite, async_baton->async_result);
        if (result == SCARD_E_NO_READERS_AVAILABLE) {
            result = SCARD_S_SUCCESS;
        }

        /* Store the result in the baton */
        async_baton->async_result->result = result;
        /* Notify the nodejs thread */
        uv_async_send(&async_baton->async);
        if (pcsclite->m_pnp) {
            /* Start checking for status change */
            result = SCardGetStatusChange(pcsclite->m_card_context,
                                          INFINITE,
                                          &pcsclite->m_card_reader_state,
                                          1);
        } else {
            /*  If PnP is not supported, just wait for 1 second */
            sleep(1);
        }
    }

    async_baton->async_result->do_exit = true;
    uv_async_send(&async_baton->async);

    return NULL;
}
Exemplo n.º 2
0
void PCSCLite::HandlerFunction(void* arg) {

    LONG result = SCARD_S_SUCCESS;
    AsyncBaton* async_baton = static_cast<AsyncBaton*>(arg);
    PCSCLite* pcsclite = async_baton->pcsclite;
    async_baton->async_result = new AsyncResult();

    while (!pcsclite->m_state) {
        /* Get card readers */
        result = pcsclite->get_card_readers(pcsclite, async_baton->async_result);
        if (result == (LONG)SCARD_E_NO_READERS_AVAILABLE) {
            result = SCARD_S_SUCCESS;
        }

        /* Store the result in the baton */
        async_baton->async_result->result = result;
        if (result != SCARD_S_SUCCESS) {
            async_baton->async_result->err_msg = error_msg("SCardListReaders",
                                                           result);
        }

        /* Notify the nodejs thread */
        uv_async_send(&async_baton->async);

        if (result == SCARD_S_SUCCESS) {
            if (pcsclite->m_pnp) {
                /* Set current status */
                pcsclite->m_card_reader_state.dwCurrentState =
                    pcsclite->m_card_reader_state.dwEventState;
                /* Start checking for status change */
                result = SCardGetStatusChange(pcsclite->m_card_context,
                                              INFINITE,
                                              &pcsclite->m_card_reader_state,
                                              1);

                uv_mutex_lock(&pcsclite->m_mutex);
                async_baton->async_result->result = result;
                if (pcsclite->m_state) {
                    uv_cond_signal(&pcsclite->m_cond);
                }

                if (result != SCARD_S_SUCCESS) {
                    pcsclite->m_state = 2;
                    async_baton->async_result->err_msg =
                      error_msg("SCardGetStatusChange", result);
                }

                uv_mutex_unlock(&pcsclite->m_mutex);
            } else {
                /*  If PnP is not supported, just wait for 1 second */
                Sleep(1000);
            }
        } else {
            /* Error on last card access, stop monitoring */
            pcsclite->m_state = 2;
        }
    }

    async_baton->async_result->do_exit = true;
    uv_async_send(&async_baton->async);
}