/**
 * Unblocks Java thread that monitors specified event queue.
 *
 * @param queueId queue ID
 */
static void unblockMonitorThread(jint queueId) {
    EventQueue* pEventQueue;
    jint isolateId;
    JVMSPI_ThreadID thread;
    
    GET_EVENT_QUEUE_BY_ID(pEventQueue, queueId);

    if (IS_ISOLATE_QUEUE(queueId)) {
        /*
         * The event monitor thread has been saved as the "special" thread
         * of this particular isolate in order to avoid having to search
         * the entire list of threads.
         */
        isolateId = QUEUE_ID_TO_ISOLATE_ID(queueId);
        thread = SNI_GetSpecialThread(isolateId);
        if (thread != NULL) {
            midp_thread_unblock(thread);
        } else {
            REPORT_CRIT(LC_CORE,
                "StoreMIDPEventInVmThread: cannot find "
                "native event monitor thread");
        }
    } else {
        /* Unblock thread in the normal (slower) way */
        midp_thread_signal(EVENT_QUEUE_SIGNAL, queueId, 0);
    }

    pEventQueue->isMonitorBlocked = KNI_FALSE;    
}
Example #2
0
/**
 * public native void close();
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_links_Link_close(void)
{
    rendezvous *rp;
    KNI_StartHandles(1);
    KNI_DeclareHandle(thisObj);

    KNI_GetThisPointer(thisObj);
    rp = getNativePointer(thisObj);

    /* ignore if closed twice */
    if (rp != NULL) {
        if (rp->sender == JVM_CurrentIsolateID()
                && rp->msg != INVALID_REFERENCE_ID) {
            /* we're the sender, make sure to clean out our message */
            SNI_DeleteReference(rp->msg);
            rp->msg = INVALID_REFERENCE_ID;
        }

        rp->state = CLOSED;
        midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0);
        setNativePointer(thisObj, NULL);
        rp_decref(rp);
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #3
0
/* JAVADOC COMMENT ELIDED */
void jsr179_notify_location_event(
        jsr179_callback_type event,
        jsr179_handle provider,
        jsr179_result operation_result) {
    (void) event;
    midp_thread_signal(JSR179_LOCATION_SIGNAL, (jint)provider, operation_result);
}
Example #4
0
/**
 * Unlocks the device.
 * <p>Java declaration:
 * <pre>
 * private native boolean unlock0();
 * </pre>
 * @return KNI_TRUE in case of success, else KNI_FALSE 
 */
KNIEXPORT KNI_RETURNTYPE_INT 
KNIDECL(com_sun_cardreader_PlatformCardDevice_unlock0) {
    jboolean retcode = KNI_FALSE;
    
    if (javacall_carddevice_unlock() == JAVACALL_OK) {
        midp_thread_signal(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, 0);
        javacall_carddevice_clear_error();
        retcode = KNI_TRUE;
    }
    
    KNI_ReturnInt(retcode);
}
Example #5
0
/* JAVADOC COMMENT ELIDED */
void /*OPTIONAL*/jsr179_notify_location_proximity(
        jsr179_handle provider,
        jdouble latitude,
        jdouble longitude,
        jfloat proximityRadius,
        jsr179_location* pLocationInfo,
        jsr179_result operation_result) {

    (void) latitude;
    (void) longitude;
    (void) proximityRadius;
    (void) pLocationInfo;

    midp_thread_signal(JSR179_LOCATION_SIGNAL, (jint)provider, operation_result);
}
Example #6
0
File: emul.c Project: sfsy1989/j2me
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_midp_jsr82emul_DeviceEmul_initDevice() {
    LOG("DeviceEmul_initDevice()");
    
    KNI_StartHandles(1);
    KNI_DeclareHandle(buf);
    KNI_GetParameterAsObject(1, buf);
    memcpy(emul_data.local_addr, JavaByteArray(buf), BT_ADDRESS_SIZE);
    KNI_EndHandles();
    
    emul_data.device_class_base = DEFAULT_COD;
    emul_data.device_class = loadCod();
    emul_data.access_code = KNI_GetParameterAsInt(2);

    state |= DEVICE_INITED;
    midp_thread_signal(JSR82_SIGNAL, BTE_SIGNAL_HANDLE, 0);

    KNI_ReturnInt((jint)emul_data.device_class);
}
Example #7
0
/**
 * private static native void setLinks0(int isolateid, Link[] linkarray);
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_links_LinkPortal_setLinks0(void)
{
    int targetIsolate;
    int len;
    int i;
    int ok = 1;
    rendezvous *rp;
    rendezvous **newrppa = NULL;

    KNI_StartHandles(2);
    KNI_DeclareHandle(linkArray);
    KNI_DeclareHandle(linkObj);

    targetIsolate = KNI_GetParameterAsInt(1);
    KNI_GetParameterAsObject(2, linkArray);

    if (KNI_IsNullHandle(linkArray)) {
        len = 0;
    } else {
        len = KNI_GetArrayLength(linkArray);
    }

    for (i = 0; i < len; i++) {
        KNI_GetObjectArrayElement(linkArray, i, linkObj);
        rp = getNativePointer(linkObj);
        if (rp == NULL || rp->state == CLOSED) {
            ok = 0;
            KNI_ThrowNew(midpIllegalArgumentException, NULL);
            break;
        }
    }

    if (ok && portals == NULL) {
        portals =
            (portal *)pcsl_mem_malloc(JVM_MaxIsolates() * sizeof(portal));
        if (portals == NULL) {
            ok = 0;
            KNI_ThrowNew(midpOutOfMemoryError, NULL);
        } else {
            int i;
            for (i = 0; i < JVM_MaxIsolates(); i++) {
                portals[i].count = -1;
                portals[i].rppa = NULL;
            }
        }
    }

    if (ok && len > 0) {
        newrppa = (rendezvous **)pcsl_mem_malloc(len * sizeof(rendezvous *));
        if (newrppa == NULL) {
            KNI_ThrowNew(midpOutOfMemoryError, NULL);
            ok = 0;
        }
    }

    if (ok) {
        portal *pp = &portals[targetIsolate];

        portal_free(pp);

        /* at this point the portal's count is zero and rppa is null */

        if (len > 0) {
            for (i = 0; i < len; i++) {
                KNI_GetObjectArrayElement(linkArray, i, linkObj);
                rp = getNativePointer(linkObj);
                /* rp not null, checked above */
                newrppa[i] = rp;
                rp_incref(rp);
            }
            pp->count = len;
            pp->rppa = newrppa;
        } else if (KNI_IsNullHandle(linkArray)) {
            pp->count = -1;
            pp->rppa = NULL;
        } else {
            /* len == 0 */
            pp->count = 0;
            pp->rppa = NULL;
        }

        midp_thread_signal(LINK_PORTAL_SIGNAL, targetIsolate, 0);
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #8
0
/**
 * private native void send0(LinkMessage msg)
 *     throws ClosedLinkException,
 *            InterruptedIOException,
 *            IOException;
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_links_Link_send0(void)
{
    rendezvous *rp;
    KNI_StartHandles(3);
    KNI_DeclareHandle(thisObj);
    KNI_DeclareHandle(messageObj);
    KNI_DeclareHandle(otherMessageObj);

    KNI_GetThisPointer(thisObj);
    KNI_GetParameterAsObject(1, messageObj);

    rp = getNativePointer(thisObj);

    if (rp == NULL) {
        if (SNI_GetReentryData(NULL) == NULL) {
            KNI_ThrowNew(midpClosedLinkException, NULL);
        } else {
            KNI_ThrowNew(midpInterruptedIOException, NULL);
        }
    } else if (JVM_CurrentIsolateID() != rp->sender) {
        KNI_ThrowNew(midpIllegalArgumentException, NULL);
    } else {
        switch (rp->state) {
            case IDLE:
                rp->msg = SNI_AddStrongReference(messageObj);
                rp->state = SENDING;
                midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                break;

            case RECEIVING:
                rp->msg = SNI_AddStrongReference(messageObj);
                rp->state = RENDEZVOUS;
                midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0);
                midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                break;

            case SENDING:
            case RENDEZVOUS:
                midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                break;

            case DONE:
                getReference(rp->msg, "send0/DONE", otherMessageObj);
                if (KNI_IsSameObject(messageObj, otherMessageObj)) {
                    /* it's our message, finish processing */
                    SNI_DeleteReference(rp->msg);
                    rp->msg = INVALID_REFERENCE_ID;
                    rp->state = IDLE;
                    midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0);
                    if (rp->retcode != OK) {
                        KNI_ThrowNew(midpIOException, NULL);
                    }
                } else {
                    /* somebody else's message, just go back to sleep */
                    midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                }

                break;
            case CLOSED:
                setNativePointer(thisObj, NULL);
                if (rp->msg != INVALID_REFERENCE_ID) {
                    /* a message was stranded in the link; clean it out */
                    SNI_DeleteReference(rp->msg);
                    rp->msg = INVALID_REFERENCE_ID;
                }
                rp_decref(rp);
                if (SNI_GetReentryData(NULL) == NULL) {
                    KNI_ThrowNew(midpClosedLinkException, NULL);
                } else {
                    KNI_ThrowNew(midpInterruptedIOException, NULL);
                }
                break;
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #9
0
/**
 * private native void receive0(LinkMessage msg, Link link)
 *         throws ClosedLinkException,
 *                InterruptedIOException,
 *                IOException;
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_links_Link_receive0(void)
{
    rendezvous *rp;

    KNI_StartHandles(4);
    KNI_DeclareHandle(thisObj);
    KNI_DeclareHandle(recvMessageObj);
    KNI_DeclareHandle(sendMessageObj);
    KNI_DeclareHandle(linkObj);

    KNI_GetThisPointer(thisObj);
    KNI_GetParameterAsObject(1, recvMessageObj);
    KNI_GetParameterAsObject(2, linkObj);

    rp = getNativePointer(thisObj);

    if (rp == NULL) {
        if (SNI_GetReentryData(NULL) == NULL) {
            KNI_ThrowNew(midpClosedLinkException, NULL);
        } else {
            KNI_ThrowNew(midpInterruptedIOException, NULL);
        }
    } else if (JVM_CurrentIsolateID() != rp->receiver) {
        KNI_ThrowNew(midpIllegalArgumentException, NULL);
    } else {
        jboolean ok;

        switch (rp->state) {
            case IDLE:
                rp->state = RECEIVING;
                midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                break;

            case SENDING:
                getReference(rp->msg, "receive0/SENDING",
                    sendMessageObj);
                ok = copy(sendMessageObj, recvMessageObj, linkObj);
                if (ok) {
                    rp->retcode = OK;
                } else {
                    rp->retcode = ERROR;
                    KNI_ThrowNew(midpIOException, NULL);
                }
                rp->state = DONE;
                midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0);
                break;

            case RENDEZVOUS:
                getReference(rp->msg, "receive0/RENDEZVOUS", sendMessageObj);
                ok = copy(sendMessageObj, recvMessageObj, linkObj);
                if (ok) {
                    rp->retcode = OK;
                } else {
                    rp->retcode = ERROR;
                    KNI_ThrowNew(midpIOException, NULL);
                }
                rp->state = DONE;
                midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0);

                break;

            case RECEIVING:
            case DONE:
                midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL);
                break;

            case CLOSED:
                setNativePointer(thisObj, NULL);
                rp_decref(rp);
                if (SNI_GetReentryData(NULL) == NULL) {
                    KNI_ThrowNew(midpClosedLinkException, NULL);
                } else {
                    KNI_ThrowNew(midpInterruptedIOException, NULL);
                }
                break;
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #10
0
/**
 * Closes the socket connection.
 * <p>
 * Java declaration:
 * <pre>
 *     close0(V)V
 * </pre>
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_socket_Protocol_close0(void) {
    void *pcslHandle;
    int status = PCSL_NET_INVALID;
    void* context = NULL;
    MidpReentryData* info;

    KNI_StartHandles(1);
    KNI_DeclareHandle(thisObject);
    KNI_GetThisPointer(thisObject);


    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    if (info == NULL) {
        /* initial invocation */
        pcslHandle = (void *)(getMidpSocketProtocolPtr(thisObject)->handle);

        if (INVALID_HANDLE == pcslHandle) {
            KNI_ThrowNew(midpIOException,
                "invalid handle during socket::close");
        } else {
            status = pcsl_socket_close_start(pcslHandle, &context);

            getMidpSocketProtocolPtr(thisObject)->handle =
                (jint)INVALID_HANDLE;

            midp_thread_signal(NETWORK_READ_SIGNAL, (int)pcslHandle, 0);
            midp_thread_signal(NETWORK_WRITE_SIGNAL, (int)pcslHandle, 0);
        }
    } else {
        /* reinvocation */
        pcslHandle = (void *)(info->descriptor);
        context = info->pResult;
        status = pcsl_socket_close_finish(pcslHandle, context);
    }
 
    REPORT_INFO1(LC_PROTOCOL, "socket::close handle=%d\n", pcslHandle);

    if (INVALID_HANDLE != pcslHandle) {
        if (status == PCSL_NET_SUCCESS) {
            if (midpDecResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) {
                REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); 
            }
        } else if (status == PCSL_NET_WOULDBLOCK) {
            REPORT_INFO1(LC_PROTOCOL, "socket::close = 0x%x blocked\n", 
                         pcslHandle);
            /* IMPL NOTE: unclear whether this is the right signal */
            midp_thread_wait(NETWORK_READ_SIGNAL, (int)pcslHandle, context);
        } else {
            /* must be PCSL_NET_IOERROR */
            midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                    "IOError in socket::close = %d\n", 
                    (int)pcsl_network_error(pcslHandle));
            REPORT_INFO1(LC_PROTOCOL, "%s", gKNIBuffer);
            KNI_ThrowNew(midpIOException, gKNIBuffer);
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #11
0
/**
 * The typedef of the security permission listener that is notified 
 * when the native security manager has permission checking result.
 *
 * @param handle  - The handle for the permission check session
 * @param granted - true if the permission is granted, false if denied.
 */
static void midpPermissionListener(jint requestHandle, jboolean granted) {
    midp_thread_signal(SECURITY_CHECK_SIGNAL, requestHandle, (int)granted);
}
Example #12
0
/**
 * Performs data transfer to the device. This method must be called within
 * <tt>synchronize</tt> block with the Slot object.
 * <p>Java declaration:
 * <pre>
 * public native static int exchangeAPDU0(Handle h, Slot slot,
                                          byte[] request, byte[] response) 
                            throws IOException;
 * </pre>
 * @param h Connection handle. Can be null for internal purposes
 * @param slot Slot object. Unused when <tt>h</tt> is not null. 
 * Must be provided if <tt>h</tt> is null.
 * @param request Buffer with request data
 * @param response Buffer for response data
 * @return Length of response data
 * @exception NullPointerException if any parameter is null
 * @exception IllegalArgumentException if request does not contain proper APDU
 * @exception InterruptedIOException if the connection handle is suddenly closed
 * in the middle of exchange or the card was removed and inserted again
 * @exception IOException if any I/O troubles occured
 */
KNIEXPORT KNI_RETURNTYPE_INT 
Java_com_sun_midp_io_j2me_apdu_APDUManager_exchangeAPDU0() {
    jint retcode = -1;
    MidpReentryData* info;
    void *context = NULL;
    JSR177_STATUSCODE status_code;
    JSR177_CARD_MOVEMENT movements;
    jsize tx_length, rx_length, rx_length_max;
    jbyte *tx_buffer, *rx_buffer;
    jbyte *cur;
    jbyte case_2[5];
    int Lc, Le;
    int cla, channel;
    ConnectionHandle *h;
    CardSlot *card_slot;
    char *err_msg;
    jbyte getResponseAPDU[5];
    
    KNI_StartHandles(4);
    KNI_DeclareHandle(connection_handle);
    KNI_DeclareHandle(slot_handle);
    KNI_DeclareHandle(request_handle);
    KNI_DeclareHandle(response_handle);
    
    info = (MidpReentryData*)SNI_GetReentryData(NULL);

    // If the Handle object is provided we get a Slot object from it
    KNI_GetParameterAsObject(1, connection_handle);
    if (!KNI_IsNullHandle(connection_handle)) {
        h = unhand(ConnectionHandle,(connection_handle));
        card_slot = h->cardSlot;
        if (card_slot == NULL) {
            KNI_ThrowNew(midpNullPointerException, "Slot object is null");
            goto end;
        }
    } else {
        h = NULL;
        KNI_GetParameterAsObject(2, slot_handle);
        if (KNI_IsNullHandle(slot_handle)) {
            KNI_ThrowNew(midpNullPointerException, "Handle and slot are null");
            goto end;
        }
        card_slot = unhand(CardSlot,(slot_handle));
    }
    
    KNI_GetParameterAsObject(3, request_handle);
    if (KNI_IsNullHandle(request_handle)) {
        KNI_ThrowNew(midpNullPointerException, "Request APDU is null");
        goto end;
    }
    tx_length = KNI_GetArrayLength(request_handle);
    tx_buffer = SNI_GetRawArrayPointer(request_handle);
    
    KNI_GetParameterAsObject(4, response_handle);
    if (KNI_IsNullHandle(response_handle)) {
        KNI_ThrowNew(midpNullPointerException, "Response buffer is null");
        goto end;
    }
    rx_length_max = KNI_GetArrayLength(response_handle);
    rx_buffer = SNI_GetRawArrayPointer(response_handle);
    
    if (h != NULL && 
            (!h->opened || h->cardSessionId != card_slot->cardSessionId)) {
        char *msg = "Connection closed";
        if (!card_slot->locked) {
            KNI_ThrowNew(midpIOException, msg);
            goto end;
        } else {
            KNI_ThrowNew(midpInterruptedIOException, msg);
            goto unlock_end;
        }
    }

    if (!card_slot->powered) {
        char *msg = "Card not powered up";
        if (!card_slot->locked) {
            KNI_ThrowNew(midpIOException, msg);
            goto end;
        } else {
            KNI_ThrowNew(midpInterruptedIOException, msg);
            goto unlock_end;
        }
    }
    
    if (tx_length < 4) { // invalid APDU: too short
    invalid_apdu:
        KNI_ThrowNew(midpIllegalArgumentException, "Invalid APDU");
        goto end;
    }
    
    // trying to guess the case
    if (tx_length == 4) { // case 1
        Lc = Le = 0;
    } else {
        Lc = (tx_buffer[4]&0xFF);
        if (tx_length == 5) { // case 2
            Le = Lc;
            Lc = 0;
            if (Le == 0) {
                Le = 256;
            }
        } else if (tx_length == 5 + Lc) { // case 3
            Le = 0;
        } else { // case 4
            if (5 + Lc >= tx_length) { // invalid APDU: bad Lc field
                goto invalid_apdu;
            }
            Le = tx_buffer[5 + Lc] & 0xFF;
            if (Le == 0) {
                Le = 256;
            }
        }
    }
    
    // if APDU of case 4 has Lc=0 then we transform it to case 2
    if (tx_length > 5 && Lc == 0) {
        memcpy(case_2, tx_buffer, 4);
        case_2[4] = tx_buffer[5];
        tx_buffer = case_2;
        tx_length = 5;
    } 
    
    // trimming APDU
    if (tx_length > 5 + Lc + 1) {
        tx_length = 5 + Lc + 1;
    }

    cla = tx_buffer[0] & 0xf8; // mask channel and secure bit
    channel = cla != 0 && (cla < 0x80 || cla > 0xA0) ? 0 : tx_buffer[0] & 3;
    
    // locked slot means that we are in the middle of an exchange, 
    // otherwise we should start a data transfer
    if (!card_slot->locked) {
        card_slot->received = 0;
        cur = rx_buffer;
    } else {
        cur = rx_buffer + card_slot->received;
    }
    
    do { // infinite loop
        int sw1, sw2;

        rx_length = rx_length_max - (jint)(cur - rx_buffer);
        if (rx_length < Le + 2) { 
            err_msg = "Too long response";
            goto err_mess;
        }
        if (info == NULL || info->status == SIGNAL_LOCK) {
            if (!card_slot->locked) {
                status_code = jsr177_lock();
                if (status_code == JSR177_STATUSCODE_WOULD_BLOCK) {
                    midp_thread_wait(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, NULL);
                    goto end;
                }
                if (status_code != JSR177_STATUSCODE_OK) {
                    goto err;
                }
                card_slot->locked = KNI_TRUE;

// Since this line slot is locked
                status_code = jsr177_select_slot(card_slot->slot);
                if (status_code != JSR177_STATUSCODE_OK) {
                    goto err;
                }
            }
            status_code = 
                jsr177_xfer_data_start(tx_buffer, tx_length, 
                    cur, &rx_length, &context);
        } else {
            context = info->pResult;
            status_code = 
                jsr177_xfer_data_finish(tx_buffer, tx_length, 
                    cur, &rx_length, context);
        }
        if (jsr177_card_movement_events(&movements) == JSR177_STATUSCODE_OK) {
            if ((movements & JSR177_CARD_MOVEMENT_MASK) != 0) {
                err_msg = "Card changed";
                jsr177_set_error(err_msg);
                if (jsr177_get_error((jbyte*)gKNIBuffer, KNI_BUFFER_SIZE)) {
                    err_msg = gKNIBuffer;
                }
                card_slot->powered = KNI_FALSE;
                goto interrupted;
            }
        }
        if (status_code == JSR177_STATUSCODE_WOULD_BLOCK) {
            midp_thread_wait(CARD_READER_DATA_SIGNAL, SIGNAL_XFER, context);
            card_slot->received = (jint)(cur - rx_buffer);
            goto end;
        }
        
        if (status_code != JSR177_STATUSCODE_OK) {
        err:
            if (jsr177_get_error((jbyte*)gKNIBuffer, KNI_BUFFER_SIZE)) {
                err_msg = gKNIBuffer;
            } else {
                err_msg = "exchangeAPDU0()";
            }
        err_mess:
            KNI_ThrowNew(midpIOException, err_msg);
            if (card_slot->locked) {
                status_code = jsr177_unlock(); // ignore status_code
                card_slot->locked = KNI_FALSE;
                midp_thread_signal(CARD_READER_DATA_SIGNAL, 
                                               SIGNAL_LOCK, SIGNAL_LOCK);
            }
            goto end;
        }
        if (rx_length < 2) {
            err_msg = "Response error";
            goto err_mess;
        }
        
        if (h != NULL && 
                (!h->opened || h->cardSessionId != card_slot->cardSessionId)) {
            err_msg = "Handle invalid or closed";
        interrupted:
            KNI_ThrowNew(midpInterruptedIOException, err_msg);
            goto unlock_end;
        }
        sw1 = cur[rx_length - 2] & 0xFF;
        sw2 = cur[rx_length - 1] & 0xFF;
        
        if (sw1 == 0x6C && sw2 != 0x00 && Le != 0) {
            tx_buffer[tx_length - 1] = sw2;
            Le = sw2;
            info = NULL;
            continue;
        }
        
        cur += rx_length;
        if (Le == 0 || (sw1 != 0x61 &&
            (channel != 0 || !card_slot->SIMPresent ||
             (sw1 != 0x62 && sw1 != 0x63 &&
              sw1 != 0x9E && sw1 != 0x9F)))) {
            break;
        }
        cur -= 2; // delete last SW1/SW2 from buffer
        Le = sw1 == 0x62 || sw1 == 0x63 ? 0 : sw2;
        
        memset(getResponseAPDU, 0, sizeof getResponseAPDU);
        tx_buffer = getResponseAPDU;
        tx_buffer[0] = channel;
        tx_buffer[1] = 0xC0;
        tx_buffer[4] = Le;
        if (Le == 0) {
            Le = 256;
        }
        tx_length = 5;
        info = NULL;
    } while(1);
    retcode = (jint)(cur - rx_buffer);

unlock_end:
    card_slot->locked = KNI_FALSE;
    midp_thread_signal(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, SIGNAL_LOCK);
    status_code = jsr177_unlock(); 
    if (status_code != JSR177_STATUSCODE_OK) {
        goto err;
    }

end:
    KNI_EndHandles();
    KNI_ReturnInt(retcode);
}
Example #13
0
/**
 * Performs reset of the card in the slot. This method must be called within
 * <tt>synchronize</tt> block with the Slot object.
 * <p>Java declaration:
 * <pre>
 * public native static byte[] reset0(Slot cardSlot) throws IOException;
 * </pre>
 * @param cardSlot Slot object
 * @return byte array with ATR
 * @exception NullPointerException if parameter is null
 * @exception IOException if any i/o troubles occured
 */
KNIEXPORT KNI_RETURNTYPE_OBJECT 
Java_com_sun_midp_io_j2me_apdu_APDUManager_reset0() {
    MidpReentryData* info;
    void *context = NULL;
    JSR177_STATUSCODE status_code;
    CardSlot *card_slot;
    jsize atr_length;
    char *err_msg;
    // IMPL_NOTE: I assumed that maximum length of ATR is 256 bytes
    jbyte atr_buffer[256]; 
    
    KNI_StartHandles(2);
    KNI_DeclareHandle(slot_handle);
    KNI_DeclareHandle(atr_handle);
    
    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    
    KNI_GetParameterAsObject(1, slot_handle);
    if (!KNI_IsNullHandle(slot_handle)) {
        card_slot = unhand(CardSlot,(slot_handle));
    } else {
        KNI_ThrowNew(midpNullPointerException, "Slot object is null");
        goto end;
    }
    
    atr_length = sizeof atr_buffer;
    
    if (info == NULL || info->status == SIGNAL_LOCK) {
        if (!card_slot->locked) {
            status_code = jsr177_lock();
            if (status_code == JSR177_STATUSCODE_WOULD_BLOCK) {
                midp_thread_wait(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, NULL);
                goto end;
            }
            if (status_code != JSR177_STATUSCODE_OK) {
                goto err;
            }
            card_slot->locked = KNI_TRUE;
// Since this line slot is locked
            status_code = jsr177_select_slot(card_slot->slot);
            if (status_code != JSR177_STATUSCODE_OK) {
                goto err;
            }
        }
        status_code = jsr177_reset_start(atr_buffer, &atr_length, &context);
    } else {
        context = info->pResult;
        status_code = 
            jsr177_reset_finish(atr_buffer, &atr_length, context);
    }
    if (status_code == JSR177_STATUSCODE_WOULD_BLOCK) {
        midp_thread_wait(CARD_READER_DATA_SIGNAL, SIGNAL_RESET, context);
        goto end;
    }

    if (status_code != JSR177_STATUSCODE_OK) {
    err:
        if (jsr177_get_error((jbyte*)gKNIBuffer, KNI_BUFFER_SIZE)) {
            err_msg = gKNIBuffer;
        } else {
            err_msg = "reset0()";
        }
        KNI_ThrowNew(midpIOException, err_msg);
        if (card_slot->locked) {
            status_code = jsr177_unlock(); // ignore status_code
            card_slot->locked = KNI_FALSE;
            midp_thread_signal(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, SIGNAL_LOCK);
        }
        goto end;
    }
    status_code = jsr177_is_sat(card_slot->slot, &card_slot->SIMPresent);
    if (status_code != JSR177_STATUSCODE_OK) {
        goto err;
    }
    
    card_slot->cardSessionId++;
    card_slot->powered = KNI_TRUE;
    card_slot->locked = KNI_FALSE;
    midp_thread_signal(CARD_READER_DATA_SIGNAL, SIGNAL_LOCK, SIGNAL_LOCK);
    status_code = jsr177_unlock();
    if (status_code != JSR177_STATUSCODE_OK) {
        goto err;
    }
    SNI_NewArray(SNI_BYTE_ARRAY, atr_length, atr_handle);
    memcpy(JavaByteArray(atr_handle), atr_buffer, atr_length);
end:
    KNI_EndHandlesAndReturnObject(atr_handle);
}
Example #14
0
/**
 * Closes the datagram connection.
 * <p>
 * Java declaration:
 * <pre>
 *     close0(V)V
 * </pre>
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_datagram_Protocol_close0(void) {
    void *socketHandle;
    MidpReentryData *info;

    KNI_StartHandles(1);
    KNI_DeclareHandle(thisObject);
    KNI_GetThisPointer(thisObject);

    socketHandle =
        (void *)getMidpDatagramProtocolPtr(thisObject)->nativeHandle;

    info = (MidpReentryData*)SNI_GetReentryData(NULL);

    REPORT_INFO1(LC_PROTOCOL, "datagram::close handle=0x%x",
        (int)socketHandle);

    if (socketHandle != INVALID_HANDLE) {
        int status;

        status = pushcheckin((int)socketHandle);
        if (status == -1) {
            void *context = NULL;

            if (info == NULL) {
                /* first invocation */
                INC_NETWORK_INDICATOR;
                status = pcsl_datagram_close_start(socketHandle, &context);

                getMidpDatagramProtocolPtr(thisObject)->nativeHandle =
                    (jint)INVALID_HANDLE;

                midp_thread_signal(NETWORK_READ_SIGNAL, (int)socketHandle, 0);
                midp_thread_signal(NETWORK_WRITE_SIGNAL, (int)socketHandle, 0);
            } else {
                /* reinvocation */
                socketHandle = (void *)(info->descriptor);
                context = info->pResult;
                status = pcsl_datagram_close_finish(socketHandle, context);
            }

            if (status == PCSL_NET_WOULDBLOCK) {
                /* IMPL NOTE: unclear whether this is the right signal */
                midp_thread_wait(NETWORK_READ_SIGNAL, (int)socketHandle,
                    context);
            } else {
                /* PCSL_NET_SUCCESS or PCSL_NET_IOERROR */
                DEC_NETWORK_INDICATOR;
                if (midpDecResourceCount(RSC_TYPE_UDP, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL,
                        "datagram::close0 resource limit update error");
                }
                if (status != PCSL_NET_SUCCESS) {
                    midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                        "error code %d", pcsl_network_error(socketHandle));
                    REPORT_INFO1(LC_PROTOCOL, "datagram::close %s",
                        gKNIBuffer);
                    KNI_ThrowNew(midpIOException, gKNIBuffer);
                }
            }
        } else {
            /* it was checked into push; don't really close the socket
               but notify a possible listeners to be unblocked */
            getMidpDatagramProtocolPtr(thisObject)->nativeHandle =
                                                   (jint)INVALID_HANDLE;
            midp_thread_signal(NETWORK_READ_SIGNAL, (int)socketHandle, 0);
            midp_thread_signal(NETWORK_WRITE_SIGNAL, (int)socketHandle, 0);
        }
    } else {
        if (info == NULL) {
            /* first invocation */
            /* already closed, do nothing */
        } else {
            /* reinvocation */
            DEC_NETWORK_INDICATOR;
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}