/*========================================================================= * FUNCTION: nativetx([B[I[I[BII[BI)V (STATIC) * CLASS: com/sun/midp/crypto/ARC4 * TYPE: static native function * OVERVIEW: Transform the given buffer * INTERFACE (operand stack manipulation): * parameters: S array of S box values * X first set intermediate results * Y second set of intermediate results * inbuf input buffer of data * inoff offset in the provided input buffer * inlen length of data to be processed * outbuf output buffer of data * outoff offset in the provided output buffer * returns: <nothing> *=======================================================================*/ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_crypto_ARC4_nativetx() { unsigned int x; unsigned int y; unsigned int tx; unsigned int ty; long outoff; long len; long inoff; long temp; long i; outoff = KNI_GetParameterAsInt(8); len = KNI_GetParameterAsInt(6); inoff = KNI_GetParameterAsInt(5); KNI_StartHandles(5); KNI_DeclareHandle(outbuf); KNI_DeclareHandle(inbuf); KNI_DeclareHandle(Y); KNI_DeclareHandle(X); KNI_DeclareHandle(S); KNI_GetParameterAsObject(7, outbuf); KNI_GetParameterAsObject(4, inbuf); KNI_GetParameterAsObject(3, Y); KNI_GetParameterAsObject(2, X); KNI_GetParameterAsObject(1, S); /*copy in the counters*/ KNI_GetRawArrayRegion(X, 0, 4, (jbyte*)&temp); x = temp; KNI_GetRawArrayRegion(Y, 0, 4, (jbyte*)&temp); y = temp; for (i = 0 ; i < len; i++) { x = (x + 1) & 0xff; y = (y + JavaByteArray(S)[x]) & 0xff; tx = JavaByteArray(S)[x]; JavaByteArray(S)[x] = JavaByteArray(S)[y]; JavaByteArray(S)[y] = tx; ty = (unsigned int)(JavaByteArray(S)[x] + JavaByteArray(S)[y]) & 0xff; JavaByteArray(outbuf)[i+outoff] = JavaByteArray(S)[ty] ^ JavaByteArray(inbuf)[i+inoff]; } temp = x; KNI_SetRawArrayRegion(X, 0, 4, (jbyte*)&temp); temp = y; KNI_SetRawArrayRegion(Y, 0, 4, (jbyte*)&temp); KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Decodes the given byte array into the <tt>ImageData</tt>. * <p> * Java declaration: * <pre> * loadJPG(Ljavax/microedition/lcdui/ImageData;[BII)V * </pre> * * @param imageData the ImageData to load to * @param imageBytes A byte array containing the encoded JPEG image data * @param imageOffset The start of the image data within the byte array * @param imageLength The length of the image data in the byte array */ KNIEXPORT KNI_RETURNTYPE_VOID KNIDECL(javax_microedition_lcdui_ImageDataFactory_loadJPEG) { int length = KNI_GetParameterAsInt(4); int offset = KNI_GetParameterAsInt(3); unsigned char* srcBuffer = NULL; gxj_screen_buffer image; java_imagedata * midpImageData = NULL; /* variable to hold error codes */ gxutl_native_image_error_codes creationError = GXUTL_NATIVE_IMAGE_NO_ERROR; KNI_StartHandles(3); /* KNI_DeclareHandle(alphaData); */ KNI_DeclareHandle(pixelData); KNI_DeclareHandle(jpegData); KNI_DeclareHandle(imageData); KNI_GetParameterAsObject(2, jpegData); KNI_GetParameterAsObject(1, imageData); midpImageData = GXAPI_GET_IMAGEDATA_PTR(imageData); /* assert * (KNI_IsNullHandle(jpegData)) */ srcBuffer = (unsigned char *)JavaByteArray(jpegData); /* * JAVA_TRACE("loadJPEG jpegData length=%d %x\n", * JavaByteArray(jpegData)->length, srcBuffer); */ image.width = midpImageData->width; image.height = midpImageData->height; unhand(jbyte_array, pixelData) = midpImageData->pixelData; if (!KNI_IsNullHandle(pixelData)) { image.pixelData = (gxj_pixel_type *)JavaByteArray(pixelData); /* * JAVA_TRACE("loadJPEG pixelData length=%d\n", * JavaByteArray(pixelData)->length); */ } /* assert * (imagedata.pixelData != NULL) */ decode_jpeg((srcBuffer + offset), length, &image, &creationError); if (GXUTL_NATIVE_IMAGE_NO_ERROR != creationError) { KNI_ThrowNew(midpIllegalArgumentException, NULL); } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Creates or updates service record in the SDDB. * * @param handle handle of the service record to be updated; * if equals to 0, a new record will be created * @param classes device service classes associated with the record * @param data binary data containing attribute-value pairs in the format * identical to the one used in the AttributeList parameter of * the SDP_ServiceAttributeResponse PDU * @return service record handle, or 0 if the operation fails */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_kvem_jsr082_bluetooth_SDDB_updateRecord(void) { bt_sddbid_t retval; bt_record_t record; bt_sddbid_t old_id = (bt_sddbid_t)KNI_GetParameterAsInt(1); KNI_StartHandles(1); KNI_DeclareHandle(dataHandle); record.id = old_id; record.classes = KNI_GetParameterAsInt(2); KNI_GetParameterAsObject(3, dataHandle); record.data = JavaByteArray(dataHandle); record.size = KNI_GetArrayLength(dataHandle); if (javacall_bt_sddb_update_record(&record.id, record.classes, record.data, record.size) == JAVACALL_OK) { retval = record.id; if (old_id != BT_INVALID_SDDB_HANDLE) { bt_push_update_record(old_id, &record); } javacall_bt_stack_set_service_classes(javacall_bt_sddb_get_service_classes(0)); } else { retval = BT_INVALID_SDDB_HANDLE; } KNI_EndHandles(); KNI_ReturnInt(retval); }
/** * Retrieves service record from the SDDB. * * @param handle handle of the service record to be retrieved * @param data byte array which will receive the data, * or null for size query * @return size of the data read/required */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_kvem_jsr082_bluetooth_SDDB_readRecord(void) { jint retval; bt_record_t record; KNI_StartHandles(1); KNI_DeclareHandle(dataHandle); KNI_GetParameterAsObject(2, dataHandle); record.id = (bt_sddbid_t)KNI_GetParameterAsInt(1); if (KNI_IsNullHandle(dataHandle)) { record.data = NULL; record.size = 0; } else { record.data = JavaByteArray(dataHandle); record.size = KNI_GetArrayLength(dataHandle); } if (javacall_bt_sddb_read_record(record.id, &record.classes, record.data, &record.size) == JAVACALL_OK) { retval = record.size; } else { retval = 0; } KNI_EndHandles(); KNI_ReturnInt(retval); }
KNIEXPORT KNI_RETURNTYPE_BOOLEAN Java_com_sun_midp_jsr82emul_ConnectionEmul_getOutData() { int myHandle = (int)KNI_GetParameterAsInt(1); connection_info_t *info = &emul_data.handled_info[myHandle].conn; jboolean ret = KNI_TRUE; int buflen; LOG1("ConnectionEmul_getOutData(%d)", myHandle); if (info->flags & IN_USE) { KNI_StartHandles(1); KNI_DeclareHandle(buf); KNI_GetParameterAsObject(2, buf); buflen = KNI_GetArrayLength(buf); if (info->out_len < buflen) { LOG1("ConnectionEmul_getOutData(%d) requests too much data", myHandle); ret = KNI_FALSE; } else { memcpy(JavaByteArray(buf), info->out, buflen); info->out_len -= buflen; memmove(info->out, &info->out[buflen], info->out_len); info->out = midpRealloc(info->out, info->out_len); if (info->out_len == 0) { info->out = NULL; } } KNI_EndHandles(); } else { ret = KNI_FALSE; } KNI_ReturnBoolean(ret); }
/** * Gets the registered MIDlet name for the given inbound connection handle. * <p> * Java declaration: * <pre> * getMIDlet0(J[BI)I * </pre> * * @param handle The handle to inbound connection * @param midletName A byte array to store the MIDlet name * @param midletNameLength The size of <tt>midlet</tt> * * @return <tt>0</tt> if successful, otherwise <tt>-1</tt> */ KNIEXPORT KNI_RETURNTYPE_INT KNIDECL(com_sun_midp_io_j2me_push_ConnectionRegistry_getMIDlet0) { int midletNameLength; char* regentry; int regentryLength; int ret = -1; int handle; midletNameLength = (int)KNI_GetParameterAsInt(3); handle = (int)KNI_GetParameterAsInt(1); KNI_StartHandles(1); KNI_DeclareHandle(midletName); KNI_GetParameterAsObject(2, midletName); regentry = pushfindfd(handle); if (NULL != regentry) { regentryLength = strlen(regentry) + 1; /* Include trailing '\0' */ if (regentryLength < midletNameLength) { memcpy((char*)JavaByteArray(midletName), regentry, regentryLength); ret = 0; } midpFree(regentry); } KNI_EndHandles(); KNI_ReturnInt(ret); }
/** * Gets a raw IPv4 address for the given hostname. * <p> * Java declaration: * <pre> * static getIpNumber([B)I * </pre> * * @param szHost the hostname to lookup as a 'C' string * @return raw IPv4 address or <tt>-1</tt> if there was an error */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_datagram_Protocol_getIpNumber(void) { int len; int status; int ipn = -1; unsigned char ipBytes[MAX_ADDR_LENGTH]; void* context = NULL; void* handle; MidpReentryData* info; KNI_StartHandles(1); KNI_DeclareHandle(hostObject); KNI_GetParameterAsObject(1, hostObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ SNI_BEGIN_RAW_POINTERS; status = pcsl_network_gethostbyname_start( (char*)JavaByteArray(hostObject), ipBytes, MAX_ADDR_LENGTH, &len, &handle, &context); SNI_END_RAW_POINTERS; } else { /* Reinvocation after unblocking the thread */ handle = (void*)info->descriptor; /* IMPL NOTE: Please see 6440539 for details. */ /* All but windows implementations of pcsl_network_gethostbyname_finish */ /* ignore context parameter. Windows one expects status code there. */ context = (void*)info->status; status = pcsl_network_gethostbyname_finish(ipBytes, MAX_ADDR_LENGTH, &len, handle, context); } KNI_EndHandles(); if (status == PCSL_NET_SUCCESS) { /* * Convert the unsigned char ip bytes array into an integer * that represents a raw IP address. */ //ipn = pcsl_network_getRawIpNumber(ipBytes); memcpy(&ipn, ipBytes, MAX_ADDR_LENGTH); } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(HOST_NAME_LOOKUP_SIGNAL, (int)handle, context); } else { /* status is either PCSL_NET_IOERROR or PCSL_NET_INVALID */ ipn = -1; REPORT_INFO1(LC_PROTOCOL, "datagram::getIpNumber returns PCSL error code %d", status); /* * IOException is thrown at the Java layer when return value * is -1 */ //KNI_ThrowNew(midpIOException, "Host name could not be resolved"); } KNI_ReturnInt((jint)ipn); }
/** * Get pointer to internal buffer of Java byte array and * check that expected offset/length can be applied to the buffer * * @param byteArray Java byte array object to get buffer from * @param offset offset of the data needed in the buffer * @param length length of the data needed in the buffer * starting from the offset * @return pointer to the buffer, or NULL if offset/length are * are not applicable. */ static unsigned char *gx_get_java_byte_buffer(KNIDECLARGS kjobject byteArray, int offset, int length) { unsigned char *buffer = (unsigned char *)JavaByteArray(byteArray); int byteArrayLength = KNI_GetArrayLength(byteArray); if (offset < 0 || length < 0 || offset + length > byteArrayLength ) { KNI_ThrowNew(midpArrayIndexOutOfBoundsException, NULL); return NULL; } return buffer; }
KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_jsr82emul_DeviceEmul_deviceDiscovered() { javacall_bt_address addr; int cod = (int)KNI_GetParameterAsInt(2); LOG("DeviceEmul_deviceDiscovered()"); KNI_StartHandles(1); KNI_DeclareHandle(buf); KNI_GetParameterAsObject(1, buf); memcpy(addr, JavaByteArray(buf), BT_ADDRESS_SIZE); javanotify_bt_device_discovered(addr, cod); KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Native connection registry method to check in connections that are in * launch pending state for a specific MIDlet. * * @param suiteId Suite ID of the MIDlet * @param className Class name of the MIDlet as zero terminated ASCII byte * array */ KNIEXPORT void KNIDECL(com_sun_midp_io_j2me_push_ConnectionRegistry_checkInByMidlet0) { SuiteIdType suiteId; KNI_StartHandles(1); KNI_DeclareHandle(classNameObj); suiteId = KNI_GetParameterAsInt(1); KNI_GetParameterAsObject(2, classNameObj); SNI_BEGIN_RAW_POINTERS; pushcheckinbymidlet(suiteId, (char*)JavaByteArray(classNameObj)); SNI_END_RAW_POINTERS; KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Gets a raw IPv4 address for the given hostname. * <p> * Java declaration: * <pre> * getIpNumber([B)I * </pre> * * @param szHost the hostname to lookup as a 'C' string * @param ipBytes Output parameter that represents an unsigned char * array for an IP address * @return len Length of ipBytes * */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_socket_Protocol_getIpNumber0(void) { int len = -1; int status = PCSL_NET_INVALID; unsigned char ipBytes[MAX_ADDR_LENGTH]; void* context = NULL; void* pcslHandle; MidpReentryData* info; KNI_StartHandles(2); KNI_DeclareHandle(hostObject); KNI_DeclareHandle(ipAddressObject); KNI_GetParameterAsObject(1, hostObject); KNI_GetParameterAsObject(2, ipAddressObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ SNI_BEGIN_RAW_POINTERS; status = pcsl_network_gethostbyname_start( (char*)JavaByteArray(hostObject), ipBytes, MAX_ADDR_LENGTH, &len, &pcslHandle, &context); SNI_END_RAW_POINTERS; } else { /* Reinvocation after unblocking the thread */ pcslHandle = (void*)info->descriptor; /* IMPL NOTE: Please see 6440539 for details. */ /* All but windows implementations of pcsl_network_gethostbyname_finish */ /* ignore context parameter. Windows one expects status code there. */ context = (void*)info->status; status = pcsl_network_gethostbyname_finish(ipBytes, MAX_ADDR_LENGTH, &len, pcslHandle, context); } if (status == PCSL_NET_SUCCESS) { KNI_SetRawArrayRegion(ipAddressObject, 0, len, (jbyte *)ipBytes); } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(HOST_NAME_LOOKUP_SIGNAL, (int)pcslHandle, context); } else { /* must be PCSL_NET_IOERROR or PCSL_NET_INVALID */ len = -1; } KNI_EndHandles(); KNI_ReturnInt((jint)len); }
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); }
KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_jsr82emul_NotifierEmul_notifyAccepted() { int myHandle = KNI_GetParameterAsInt(1); int connHandle = KNI_GetParameterAsInt(2); accept_info_t *info = &emul_data.handled_info[myHandle].accept; connection_info_t *connInfo; LOG("NotifierEmul_notifyAccepted()"); /* Otherwize notifier is already closed */ /* Need revisit: what if it is already used by another connection or notifier... */ if (info->flags & IN_USE) { if (connHandle == -1) { javanotify_bt_protocol_event(JAVACALL_EVENT_BT_ACCEPT_COMPLETE, (javacall_handle)myHandle, JAVACALL_FAIL); } else { KNI_StartHandles(1); KNI_DeclareHandle(addr); KNI_GetParameterAsObject(3, addr); info->conn_handle = connHandle; memcpy(info->peer_addr, JavaByteArray(addr), BT_ADDRESS_SIZE); info->imtu = (int)KNI_GetParameterAsInt(4); info->omtu = (int)KNI_GetParameterAsInt(5); info->status = JAVACALL_OK; KNI_EndHandles(); connInfo = &emul_data.handled_info[connHandle].conn; /* Copying address and MTU's */ memcpy(connInfo->peer_addr, info->peer_addr, BT_ADDRESS_SIZE); connInfo->omtu = info->omtu; connInfo->imtu = info->imtu; javanotify_bt_protocol_event(JAVACALL_EVENT_BT_ACCEPT_COMPLETE, (javacall_handle)myHandle, JAVACALL_OK); } } KNI_ReturnVoid(); }
KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_jsr82emul_ConnectionEmul_notifyReceived() { int myHandle = KNI_GetParameterAsInt(1); int buflen = KNI_GetParameterAsInt(3); connection_info_t *info = &emul_data.handled_info[myHandle].conn; LOG1("ConnectionEmul_notifyReceived(%d)", myHandle); /* otherwise connection is already closed */ if (info->flags & IN_USE) { info->flags &= ~SKIP_REQUEST; LOG1("ConnectionEmul_notifyReceived() received %d", buflen); if (buflen == CONN_FAILURE) { info->receive_status = JAVACALL_FAIL; } else { info->receive_status = JAVACALL_OK; if (buflen == CONN_ENDOF_INP) { info->flags |= ENDOF_INP_REACHED; } else { KNI_StartHandles(1); KNI_DeclareHandle(buf); KNI_GetParameterAsObject(2, buf); APPEND_BUFFER(info->in, info->in_len, JavaByteArray(buf), buflen); KNI_EndHandles(); } LOG1("ConnectionEmul_notifyReceived() info->in_len %d", info->in_len); } javanotify_bt_protocol_event(JAVACALL_EVENT_BT_RECEIVE_COMPLETE, (javacall_handle)myHandle, info->receive_status); } KNI_ReturnVoid(); }
static void surface_acquire(AbstractSurface* surface, jobject surfaceHandle) { KNI_StartHandles(1); KNI_DeclareHandle(dataHandle); KNI_GetObjectField(surfaceHandle, ((JavaSurface *) surface)->javaArrayFieldID, dataHandle); switch(((JavaSurface*)surface)->javaArrayFieldSize) { case sizeof(jint): surface->super.data = JavaIntArray(dataHandle)->elements; break; case sizeof(jshort): surface->super.data = JavaShortArray(dataHandle)->elements; break; case sizeof(jbyte): surface->super.data = JavaByteArray(dataHandle)->elements; break; default: //shouldn't happen break; } KNI_EndHandles(); }
KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_jsr82emul_EmulationPolling_getRequest() { static jfieldID bufID = 0; static jfieldID lengthID = 0; int len = emul_data.req_offset > 512? 512 : emul_data.req_offset; KNI_StartHandles(3); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(classHandle); KNI_DeclareHandle(bufHandle); if (bufID == 0) { KNI_GetClassPointer(classHandle); bufID = KNI_GetFieldID(classHandle, "requestBuf", "[B"); lengthID = KNI_GetFieldID(classHandle, "length", "I"); } KNI_GetThisPointer(thisHandle); KNI_SetIntField(thisHandle, lengthID, (jint)len); if (emul_data.req_offset > 0) { LOG1("EmulationPolling_getRequest(): getting %d request bytes", emul_data.req_offset); KNI_GetObjectField(thisHandle, bufID, bufHandle); memcpy(JavaByteArray(bufHandle), emul_data.request, len); if (len < emul_data.req_offset) { emul_data.req_offset -= len; memmove(emul_data.request, &emul_data.request[len], emul_data.req_offset); } else { emul_data.req_offset = 0; } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Write to a serial port without blocking. * * @param hPort handle to a native serial port * @param b I/O buffer * @param off starting offset for data * @param len length of data * * @return number of bytes that were written * * @exception IOException if an I/O error occurs. */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_comm_Protocol_native_1writeBytes() { int length = (int)KNI_GetParameterAsInt(4); int offset = (int)KNI_GetParameterAsInt(3); int hPort = (int)KNI_GetParameterAsInt(1); int bytesWritten = 0; int status = PCSL_NET_IOERROR; void* context = NULL; MidpReentryData* info; KNI_StartHandles(1); KNI_DeclareHandle(bufferObject); KNI_GetParameterAsObject(2, bufferObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { if (hPort < 0) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Write to port: handle %d is invalid\n", hPort); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIllegalArgumentException, gKNIBuffer); } else { SNI_BEGIN_RAW_POINTERS; status = writeToPortStart(hPort, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, &context); SNI_END_RAW_POINTERS; } } else { /* reinvocation */ hPort = info->descriptor; context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = writeToPortFinish(hPort, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, context); SNI_END_RAW_POINTERS; } switch (status) { case PCSL_NET_SUCCESS: /*do nothing and return normally */ break; case PCSL_NET_INTERRUPTED: midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Writing to port %d has been interrupted\n", hPort); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpInterruptedIOException, gKNIBuffer); break; case PCSL_NET_WOULDBLOCK: midp_thread_wait(COMM_WRITE_SIGNAL, hPort, context); break; default: midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Writing to port %d was failed\n", hPort); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); } KNI_EndHandles(); KNI_ReturnInt((jint)bytesWritten); }
/** * 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); }
/** * Writes to the open socket connection. * <p> * Java declaration: * <pre> * write0([BII)I * </pre> * * @param b the buffer of the data to write * @param off the start offset in array <tt>b</tt> * at which the data is written. * @param len the number of bytes to write. * * @return the total number of bytes written */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_socket_Protocol_write0(void) { int length; int offset; void *pcslHandle; int bytesWritten = 0; int status = PCSL_NET_INVALID; void *context = NULL; MidpReentryData* info; length = (int)KNI_GetParameterAsInt(3); offset = (int)KNI_GetParameterAsInt(2); KNI_StartHandles(2); KNI_DeclareHandle(bufferObject); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, bufferObject); pcslHandle = (void *)(getMidpSocketProtocolPtr(thisObject)->handle); REPORT_INFO3(LC_PROTOCOL, "socket::write0 o=%d l=%d fd=%d\n", offset, length, pcslHandle); info = (MidpReentryData*)SNI_GetReentryData(NULL); START_NETWORK_INDICATOR; if (info == NULL) { /* First invocation */ if (INVALID_HANDLE == pcslHandle) { KNI_ThrowNew(midpIOException, "invalid handle during socket::write"); } else { INC_NETWORK_INDICATOR; SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_write_start(pcslHandle, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, &context); SNI_END_RAW_POINTERS; } } else { /* Reinvocation after unblocking the thread */ if (INVALID_HANDLE == pcslHandle) { /* closed by another thread */ KNI_ThrowNew(midpInterruptedIOException, "Interrupted IO error during socket::read"); DEC_NETWORK_INDICATOR; } else { if ((void *)info->descriptor != pcslHandle) { REPORT_CRIT2(LC_PROTOCOL, "socket::write Handles mismatched 0x%x != 0x%x\n", pcslHandle, info->descriptor); } context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_write_finish(pcslHandle, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, context); SNI_END_RAW_POINTERS; } } if (INVALID_HANDLE != pcslHandle) { if (status == PCSL_NET_SUCCESS) { DEC_NETWORK_INDICATOR; } else { REPORT_INFO1(LC_PROTOCOL, "socket::write error=%d\n", (int)pcsl_network_error(pcslHandle)); if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else if (status == PCSL_NET_INTERRUPTED) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Interrupted IO error %d during socket::write ", pcsl_network_error(pcslHandle)); KNI_ThrowNew(midpInterruptedIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } else { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError %d during socket:: write \n", pcsl_network_error(pcslHandle)); KNI_ThrowNew(midpIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } } } REPORT_INFO1(LC_PROTOCOL, "socket::write0 bytesWritten=%d\n", bytesWritten); KNI_EndHandles(); KNI_ReturnInt((jint)bytesWritten); }
/** * Load Java ImageData instance with image data in RAW format. * Image data is provided either in native buffer, or in Java * byte array. Java array is used with more priority. * * @param imageData Java ImageData object to be loaded with image data * @param nativeBuffer pointer to native buffer with raw image data, * this parameter is alternative to javaBuffer * @param javaBuffer Java byte array with raw image data, * this parameter is alternative to nativeBuffer * @param offset offset of the raw image data in the buffer * @param length length of the raw image data in the buffer * starting from the offset * * @return KNI_TRUE in the case ImageData is successfully loaded with * raw image data, otherwise KNI_FALSE. */ static int gx_load_imagedata_from_raw_buffer(KNIDECLARGS kjobject imageData, unsigned char *nativeBuffer, kjobject javaBuffer, int offset, int length) { int imageSize; int pixelSize, alphaSize; int status = KNI_FALSE; gxutl_image_buffer_raw *rawBuffer = NULL; KNI_StartHandles(2); KNI_DeclareHandle(pixelData); KNI_DeclareHandle(alphaData); do { /** Check native and Java buffer parameters */ if (!KNI_IsNullHandle(javaBuffer)) { if (nativeBuffer != NULL) { REPORT_ERROR(LC_LOWUI, "Native and Java buffers should not be used together"); break; } nativeBuffer = gx_get_java_byte_buffer(KNIPASSARGS javaBuffer, offset, length); } if (nativeBuffer == NULL) { REPORT_ERROR(LC_LOWUI, "Null raw image buffer is provided"); break; } /** Check header */ rawBuffer = (gxutl_image_buffer_raw *)(nativeBuffer + offset); if (memcmp(rawBuffer->header, gxutl_raw_header, 4) != 0) { REPORT_ERROR(LC_LOWUI, "Unexpected raw image type"); break; } imageSize = rawBuffer->width * rawBuffer->height; pixelSize = sizeof(gxj_pixel_type) * imageSize; alphaSize = 0; if (rawBuffer->hasAlpha) { alphaSize = sizeof(gxj_alpha_type) * imageSize; } /** Check data array length */ if ((unsigned int)length != (offsetof(gxutl_image_buffer_raw, data) + pixelSize + alphaSize)) { REPORT_ERROR(LC_LOWUI, "Raw image is corrupted"); break; } if (rawBuffer->hasAlpha) { /* Has alpha */ SNI_NewArray(SNI_BYTE_ARRAY, alphaSize, alphaData); if (KNI_IsNullHandle(alphaData)) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } /** Link the new array into ImageData to protect if from GC */ midp_set_jobject_field(KNIPASSARGS imageData, "alphaData", "[B", alphaData); /** New array allocation could cause GC and buffer moving */ if (!KNI_IsNullHandle(javaBuffer)) { nativeBuffer = gx_get_java_byte_buffer(KNIPASSARGS javaBuffer, offset, length); rawBuffer = (gxutl_image_buffer_raw *) (nativeBuffer + offset); } memcpy(JavaByteArray(alphaData), rawBuffer->data + pixelSize, alphaSize); } SNI_NewArray(SNI_BYTE_ARRAY, pixelSize, pixelData); if (KNI_IsNullHandle(pixelData)) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } midp_set_jobject_field(KNIPASSARGS imageData, "pixelData", "[B", pixelData); /** New array allocation could cause GC and buffer moving */ if (!KNI_IsNullHandle(javaBuffer)) { nativeBuffer = gx_get_java_byte_buffer(KNIPASSARGS javaBuffer, offset, length); rawBuffer = (gxutl_image_buffer_raw *) (nativeBuffer + offset); } memcpy(JavaByteArray(pixelData), rawBuffer->data, pixelSize); GXAPI_GET_IMAGEDATA_PTR(imageData)->width = (jint)rawBuffer->width; GXAPI_GET_IMAGEDATA_PTR(imageData)->height = (jint)rawBuffer->height; status = KNI_TRUE; } while(0); KNI_EndHandles(); return status; }
/** * Opens a TCP connection to a server. * <p> * Java declaration: * <pre> * open([BI)V * </pre> * * @param ipBytes Byte array that represents a raw IP address * @param port TCP port at host * * @return a native handle to the network connection. */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_io_j2me_socket_Protocol_open0(void) { int port; void *pcslHandle = INVALID_HANDLE; int status; void* context = NULL; MidpReentryData* info; port = (int)KNI_GetParameterAsInt(2); KNI_StartHandles(2); KNI_DeclareHandle(thisObject); KNI_DeclareHandle(bufferObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, bufferObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE; /** * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ if (midpCheckResourceLimit(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit exceeded for TCP client sockets"); KNI_ThrowNew(midpIOException, "Resource limit exceeded for TCP client sockets"); } else { SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_open_start( (unsigned char*)JavaByteArray(bufferObject), port, &pcslHandle, &context); SNI_END_RAW_POINTERS; if (status == PCSL_NET_SUCCESS) { getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle; if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } } else if (status == PCSL_NET_IOERROR) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError in socket::open = %d\n", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); } else if (status == PCSL_NET_CONNECTION_NOTFOUND) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "ConnectionNotFound error in socket::open :" " error = %d\n", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer); } else if (status == PCSL_NET_WOULDBLOCK) { INC_NETWORK_INDICATOR; getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle; if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } REPORT_INFO1(LC_PROTOCOL, " handle = %d\n", pcslHandle); midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else { REPORT_INFO(LC_PROTOCOL, "Unknown error during socket::open"); KNI_ThrowNew(midpIOException, NULL); } } } else { /* Reinvocation after unblocking the thread */ pcslHandle = (void *) info->descriptor; context = (void *)info->status; if (getMidpSocketProtocolPtr(thisObject)->handle != (jint)pcslHandle) { REPORT_CRIT2(LC_PROTOCOL, "socket::open Handles mismatched 0x%x != 0x%x\n", pcslHandle, getMidpSocketProtocolPtr(thisObject)->handle); } status = pcsl_socket_open_finish(pcslHandle, context); if (status == PCSL_NET_SUCCESS) { DEC_NETWORK_INDICATOR; } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else { DEC_NETWORK_INDICATOR; getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE; if (midpDecResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "error %d in socket::open", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer); } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Decodes the given byte array into the <tt>ImageData</tt>. * <p> * Java declaration: * <pre> * loadNative(Ljavax/microedition/lcdui/ImageDataFactory;[BII)Z * </pre> * * @param imageData the ImageData to load to * @param imageBytes A byte array containing the encoded PNG image data * @param offset The start of the image data within the byte array * @param length The length of the image data in the byte array * * @return true if able to decode */ KNIEXPORT KNI_RETURNTYPE_BOOLEAN KNIDECL(javax_microedition_lcdui_ImageDataFactory_loadNative) { int length = KNI_GetParameterAsInt(4); int offset = KNI_GetParameterAsInt(3); int status = KNI_TRUE; unsigned char* srcBuffer = NULL; PIXEL* imgPixelData = NULL; ALPHA* imgAlphaData = NULL; java_imagedata* midpImageData = NULL; TBool generateMask = EFalse; KNI_StartHandles(4); KNI_DeclareHandle(pixelData); KNI_DeclareHandle(alphaData); KNI_DeclareHandle(inData); KNI_DeclareHandle(imageData); KNI_GetParameterAsObject(2, inData); KNI_GetParameterAsObject(1, imageData); midpImageData = IMGAPI_GET_IMAGEDATA_PTR(imageData); srcBuffer = (unsigned char *)JavaByteArray(inData); int byteArrayLength = KNI_GetArrayLength(inData); if (offset >= 0 && length >= 0 && offset + length <= byteArrayLength) { TPtrC8 sourceData(srcBuffer + offset, length); TInt width; TInt height; if (static_cast<MApplication*>(Dll::Tls())->InitializeDecoder(sourceData, width, height, generateMask) == KErrNone) { midpImageData->width = width; midpImageData->height = height; SNI_NewArray(SNI_BYTE_ARRAY, midpImageData->width * midpImageData->height * 2, pixelData); if (generateMask) { SNI_NewArray(SNI_BYTE_ARRAY, midpImageData->width * midpImageData->height, alphaData); } if (!KNI_IsNullHandle(pixelData) && (!KNI_IsNullHandle(alphaData) || !generateMask)) { if (generateMask) { imgAlphaData = (ALPHA*)JavaByteArray(alphaData); } midp_set_jobject_field(KNIPASSARGS imageData, "pixelData", "[B", pixelData); imgPixelData = (PIXEL*)JavaByteArray(pixelData); if (static_cast<MApplication*>(Dll::Tls())->DecodeImage((char*)imgPixelData, (char*)imgAlphaData) != KErrNone) { status = KNI_FALSE; } if (imgAlphaData) { midp_set_jobject_field(KNIPASSARGS imageData, "alphaData", "[B", alphaData); } } else { status = KNI_FALSE; } } } KNI_EndHandles(); KNI_ReturnBoolean(status); }
/* * Performs client connection establishment. * * Note: the method gets native connection handle directly from * <code>handle<code> field of <code>L2CAPConnectionImpl</code> object. * * @param addr bluetooth address of device to connect to * @param psm Protocol Service Multiplexor (PSM) value * @return Negotiated ReceiveMTU and TransmitMTU. * 16 high bits is ReceiveMTU, 16 low bits is TransmitMTU. * * @throws IOException if any I/O error occurs */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_jsr082_bluetooth_btl2cap_L2CAPConnectionImpl_connect0(void) { unsigned char *address = NULL; int psm = (int)KNI_GetParameterAsInt(2); javacall_handle handle = JAVACALL_BT_INVALID_HANDLE; int status, i, imtu, omtu, mtus; void* context = NULL; MidpReentryData* info; javacall_bt_address addr; jfieldID connHandleID = NULL; KNI_StartHandles(3); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(arrayHandle); KNI_DeclareHandle(classHandle); KNI_GetThisPointer(thisHandle); KNI_GetClassPointer(classHandle); GET_FIELDID(classHandle, "handle", "I", connHandleID) KNI_GetParameterAsObject(1, arrayHandle); handle = (javacall_handle)KNI_GetIntField(thisHandle, connHandleID); REPORT_INFO1(LC_PROTOCOL, "btl2cap::connect handle=%d", handle); /* copy address from Java input array */ SNI_BEGIN_RAW_POINTERS; address = JavaByteArray(arrayHandle); for (i = 0; i < JAVACALL_BT_ADDRESS_SIZE; i++) { addr[i] = address[i]; } SNI_END_RAW_POINTERS; info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ // Need revisit: add resource counting /* * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } else { */ status = javacall_bt_l2cap_connect(handle, address, psm, &imtu, &omtu); if (status == JAVACALL_OK) { // Need revisit: add resource counting /* if (midpIncResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } */ } else if (status == JAVACALL_FAIL) { char* pError; javacall_bt_l2cap_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IO error in btl2cap::connect (%s)\n", pError); REPORT_INFO(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(gKNIBuffer)); } else if (status == JAVACALL_WOULD_BLOCK) { // Need revisit: add bluetooth activity indicator // INC_BT_INDICATOR; // Need revisit: add resource counting /* if (midpIncResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } */ REPORT_INFO1(LC_PROTOCOL, "btl2cap::connect is waiting for complete notify" ", handle = %d\n", handle); midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)handle, context); } else { char* pMsg = "Unknown error during btl2cap::connect"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } // } } else { /* Reinvocation after unblocking the thread */ context = info->pResult; if ((javacall_handle)info->descriptor != handle) { REPORT_CRIT2(LC_PROTOCOL, "btl2cap::connect handles mismatched %d != %d\n", handle, (javacall_handle)info->descriptor); } status = javacall_bt_l2cap_connect(handle, address, psm, &imtu, &omtu); if (status == JAVACALL_OK) { // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; } else if (status == JAVACALL_WOULD_BLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)handle, context); } else { char* pError; KNI_SetIntField(thisHandle, connHandleID, (jint)JAVACALL_BT_INVALID_HANDLE); // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; // Need revisit: add resource counting /* if (midpDecResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } */ javacall_bt_l2cap_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Error in btl2cap::connect (%s)", pError); REPORT_INFO(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpConnectionNotFoundException, EXCEPTION_MSG(gKNIBuffer)); } } mtus = (imtu << 16) & 0xFFFF0000; mtus |= omtu & 0xFFFF; KNI_EndHandles(); KNI_ReturnInt(mtus); }
/* * Reads data from a packet received via Bluetooth stack. * * Note: the method gets native connection handle directly from * <code>handle<code> field of <code>L2CAPConnectionImpl</code> object. * * @param buf the buffer to read to * @param offset he start offset in array <code>buf</code> * at which the data to be written * @param size the maximum number of bytes to read, * the rest of the packet is discarded. * @return total number of bytes read into the buffer or * <code>0</code> if a zero length packet is received * @throws IOException if an I/O error occurs */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_jsr082_bluetooth_btl2cap_L2CAPConnectionImpl_receive0(void) { int length, offset; javacall_handle handle; int bytesRead = -1; int status = JAVACALL_FAIL; void* context = NULL; MidpReentryData* info; jfieldID connHandleID = NULL; offset = (int)KNI_GetParameterAsInt(2); length = (int)KNI_GetParameterAsInt(3); KNI_StartHandles(3); KNI_DeclareHandle(arrayHandle); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(classHandle); KNI_GetThisPointer(thisHandle); KNI_GetClassPointer(classHandle); GET_FIELDID(classHandle, "handle", "I", connHandleID) handle = (javacall_handle)KNI_GetIntField(thisHandle, connHandleID); KNI_GetParameterAsObject(1, arrayHandle); REPORT_INFO3(LC_PROTOCOL, "btl2cap::receive off=%d len=%d handle=%d\n", offset, length, handle); info = (MidpReentryData*)SNI_GetReentryData(NULL); // Need revisit: add bluetooth activity indicator // START_BT_INDICATOR; if (info == NULL) { /* First invocation */ if (JAVACALL_BT_INVALID_HANDLE == handle) { KNI_ThrowNew(midpIOException, EXCEPTION_MSG( "Invalid handle during btl2cap::receive")); } else { // Need revisit: add bluetooth activity indicator // INC_BT_INDICATOR; SNI_BEGIN_RAW_POINTERS; status = javacall_bt_l2cap_receive(handle, (unsigned char*)&(JavaByteArray(arrayHandle)[offset]), length, &bytesRead); SNI_END_RAW_POINTERS; } } else { /* Reinvocation after unblocking the thread */ if (JAVACALL_BT_INVALID_HANDLE == handle) { /* closed by another thread */ KNI_ThrowNew(midpInterruptedIOException, EXCEPTION_MSG( "Interrupted IO error during btl2cap::receive")); // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; } else { if ((javacall_handle)info->descriptor != handle) { REPORT_CRIT2(LC_PROTOCOL, "btl2cap::receive handles mismatched %d != %d\n", handle, (javacall_handle)info->descriptor); } context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = javacall_bt_l2cap_receive(handle, (unsigned char*)&(JavaByteArray(arrayHandle)[offset]), length, &bytesRead); SNI_END_RAW_POINTERS; } } REPORT_INFO1(LC_PROTOCOL, "btl2cap::receive bytes=%d\n", bytesRead); if (JAVACALL_BT_INVALID_HANDLE != handle) { if (status == JAVACALL_OK) { // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; } else { char* pError; javacall_bt_l2cap_get_error(handle, &pError); REPORT_INFO1(LC_PROTOCOL, "btl2cap::receive (%s)\n", pError); if (status == JAVACALL_WOULD_BLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, (int)handle, context); } else if (status == JAVACALL_INTERRUPTED) { char* pError; javacall_bt_l2cap_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Interrupted IO error during btl2cap::receive (%s)", pError); KNI_ThrowNew(midpInterruptedIOException, EXCEPTION_MSG(gKNIBuffer)); // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; } else { char* pError; javacall_bt_l2cap_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Unknown error during btl2cap::receive (%s)", pError); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(gKNIBuffer)); // Need revisit: add bluetooth activity indicator // DEC_BT_INDICATOR; } } } // Need revisit: add bluetooth activity indicator // STOP_BT_INDICATOR; KNI_EndHandles(); KNI_ReturnInt((jint)bytesRead); }
/** * Decodes the given byte array and fill a passed in immutable <tt>ImageData</tt>. * <p> * Java declaration: * <pre> * createImmutableImageDecodeImage([Ljavax/microedition/lcdui/ImageData;BII)V * </pre> * * @param imageData The <tt>ImageData</tt> to be filled * @param intputData A byte array containing the encoded image data * @param offset The start of the image data within the byte array * @param length The length of the image data in the byte array */ KNIEXPORT KNI_RETURNTYPE_VOID Java_javax_microedition_lcdui_ImageDataFactory_createImmutableImageDecodeImage() { int length = KNI_GetParameterAsInt(4); int offset = KNI_GetParameterAsInt(3); unsigned char* srcBuffer = NULL; gxpport_image_native_handle newImagePtr; int imgWidth; int imgHeight; img_native_error_codes creationError = IMG_NATIVE_IMAGE_NO_ERROR; KNI_StartHandles(2); KNI_DeclareHandle(pngData); KNI_DeclareHandle(imageData); KNI_GetParameterAsObject(2, pngData); KNI_GetParameterAsObject(1, imageData); do { if ((offset < 0) || (length < 0) || (offset + length) > KNI_GetArrayLength(pngData)) { KNI_ThrowNew(midpArrayIndexOutOfBoundsException, NULL); break; } srcBuffer = (unsigned char *)JavaByteArray(pngData); SNI_BEGIN_RAW_POINTERS; gxpport_decodeimmutable_from_selfidentifying(srcBuffer + offset, length, &imgWidth, &imgHeight, &newImagePtr, &creationError); SNI_END_RAW_POINTERS; if (IMG_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR == creationError) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } if (IMG_NATIVE_IMAGE_RESOURCE_LIMIT == creationError) { KNI_ThrowNew(midpOutOfMemoryError, "Resource limit exceeded for immutable image"); break; } if (IMG_NATIVE_IMAGE_NO_ERROR != creationError) { KNI_ThrowNew(midpIllegalArgumentException, NULL); break; } { java_imagedata * dstImageDataPtr = IMGAPI_GET_IMAGEDATA_PTR(imageData); dstImageDataPtr->width = (jint)imgWidth; dstImageDataPtr->height = (jint)imgHeight; dstImageDataPtr->nativeImageData = (jint)newImagePtr; } } while (0); KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Receives a datagram. * <p> * Java declaration: * <pre> * receive0([BII)J * </pre> * * @param buf the data buffer * @param off the offset into the data buffer * @param len the length of the data in the buffer * @return The upper 32 bits contain the raw IPv4 address of the * host the datagram was received from. The next 16 bits * contain the port. The last 16 bits contain the number * of bytes received. */ KNIEXPORT KNI_RETURNTYPE_LONG Java_com_sun_midp_io_j2me_datagram_Protocol_receive0(void) { int offset; int length; void *socketHandle; jlong lres = 0; MidpReentryData* info; KNI_StartHandles(2); KNI_DeclareHandle(bufferObject); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, bufferObject); offset = (int)KNI_GetParameterAsInt(2); length = (int)KNI_GetParameterAsInt(3); socketHandle = (void *)getMidpDatagramProtocolPtr(thisObject)->nativeHandle; info = (MidpReentryData*)SNI_GetReentryData(NULL); REPORT_INFO3(LC_PROTOCOL, "datagram::receive0 off=%d len=%d handle=0x%x", offset, length, (int)socketHandle); if (socketHandle != INVALID_HANDLE) { int ipAddress; int port; int bytesReceived; /* * Check the push cache for a waiting datagram. * * If pusheddatagram() returns -1 [IMPL NOTE: the code checks for less * than zero; which is correct?], we need to read a datagram * ourselves. Otherwise, pusheddatagram() has returned a waiting * datagram and has set ipAddress and port to valid values. */ SNI_BEGIN_RAW_POINTERS; bytesReceived = pusheddatagram((int)socketHandle, &ipAddress, &port, (char*)&(JavaByteArray(bufferObject)[offset]), length); SNI_END_RAW_POINTERS; if (bytesReceived < 0) { int status; unsigned char ipBytes[MAX_ADDR_LENGTH]; void *context; if (info == NULL) { /* initial invocation */ INC_NETWORK_INDICATOR; SNI_BEGIN_RAW_POINTERS; status = pcsl_datagram_read_start( socketHandle, ipBytes, &port, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesReceived, &context); SNI_END_RAW_POINTERS; } else { /* reinvocation */ if ((void *)info->descriptor != socketHandle) { REPORT_CRIT2(LC_PROTOCOL, "datagram::send0 handle mismatch 0x%x != 0x%x\n", socketHandle, info->descriptor); } context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = pcsl_datagram_read_finish( socketHandle, ipBytes, &port, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesReceived, context); SNI_END_RAW_POINTERS; } if (status == PCSL_NET_SUCCESS) { memcpy(&ipAddress, ipBytes, MAX_ADDR_LENGTH); lres = pack_recv_retval(ipAddress, port, bytesReceived); DEC_NETWORK_INDICATOR; } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, (int)socketHandle, context); } else if (status == PCSL_NET_INTERRUPTED) { KNI_ThrowNew(midpInterruptedIOException, NULL); DEC_NETWORK_INDICATOR; } else { /* status == PCSL_NET_IOERROR */ midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "error code %d", pcsl_network_error(socketHandle)); KNI_ThrowNew(midpIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } } else { /* push gave us a datagram */ lres = pack_recv_retval(ipAddress, port, bytesReceived); } } else { if (info == NULL) { /* initial invocation */ KNI_ThrowNew(midpIOException, "socket closed"); } else { /* reinvocation */ KNI_ThrowNew(midpInterruptedIOException, NULL); DEC_NETWORK_INDICATOR; } } KNI_EndHandles(); KNI_ReturnLong(lres); }
/** * Accepts incoming client connection request. * * Note: the method gets native connection handle directly from * <code>handle<code> field of <code>BTSPPNotifierImpl</code> object. * * Note: new native connection handle to work with accepted incoming * client connection is setted directly to <code>handle</code> field of * appropriate <code>RFCOMMConnectionImpl</code> object. * * @throws IOException if an I/O error occurs */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_io_j2me_btspp_BTSPPNotifierImpl_accept0(void) { bt_handle_t handle = BT_INVALID_HANDLE; bt_handle_t peer_handle = BT_INVALID_HANDLE; MidpReentryData* info; int status = BT_RESULT_FAILURE; int processStatus = KNI_FALSE; void *context = NULL; bt_bdaddr_t peer_addr; unsigned char *address = NULL; KNI_StartHandles(2); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(arrayHandle); KNI_GetThisPointer(thisHandle); handle = (bt_handle_t)KNI_GetIntField(thisHandle, notifHandleID); KNI_GetObjectField(thisHandle, peerAddrID, arrayHandle); if (handle == BT_INVALID_HANDLE) { REPORT_ERROR(LC_PROTOCOL, "RFCOMM notifier was closed before btspp_notif::accept"); KNI_ThrowNew(midpInterruptedIOException, EXCEPTION_MSG( "RFCOMM notifier was closed")); } else { bt_pushid_t pushid = KNI_GetIntField(thisHandle, pushHandleID); if (pushid != BT_INVALID_PUSH_HANDLE) { if (bt_push_checkout_client(pushid, &peer_handle, peer_addr, NULL, NULL) == BT_RESULT_SUCCESS) { pushcheckoutaccept((int)handle); processStatus = KNI_TRUE; status = BT_RESULT_SUCCESS; } } if (peer_handle == BT_INVALID_HANDLE) { info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ REPORT_INFO1(LC_PROTOCOL, "btspp_notif::accept handle=%d\n", handle); // IMPL_NOTE: add resource counting /* * An incoming socket connection counts against the client socket * resource limit. */ /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); */ // } else { status = bt_rfcomm_accept_start( handle, &peer_handle, peer_addr, &context); processStatus = KNI_TRUE; // } } else { /* Reinvocation after unblocking the thread */ if ((bt_handle_t)info->descriptor != handle) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "btspp_notif::accept handles mismatched %d != %d\n", handle, info->descriptor); REPORT_CRIT(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIllegalStateException, EXCEPTION_MSG( "Internal error in btspp_notif::accept")); } else { // IMPL_NOTE: add resource counting /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets" REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } else { */ status = bt_rfcomm_accept_finish( handle, &peer_handle, peer_addr, context); processStatus = KNI_TRUE; // } } } } if (processStatus) { REPORT_INFO1(LC_PROTOCOL, "btspp_notif::accept server handle=%d\n", handle); if (status == BT_RESULT_SUCCESS) { int i; // IMPL_NOTE: add resource counting /* if (midpIncResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "btspp_notif: Resource limit update error"); } */ // store client native connection handle for temporary storing KNI_SetIntField(thisHandle, peerHandleID, (jint)peer_handle); // copy address to Java object field SNI_BEGIN_RAW_POINTERS; address = (unsigned char*)JavaByteArray(arrayHandle); for (i = 0; i < BT_ADDRESS_SIZE; i++) { address[i] = peer_addr[i]; } SNI_END_RAW_POINTERS; RegisterRFCOMMConnection0(peer_addr, peer_handle); REPORT_INFO(LC_PROTOCOL, "btspp_notif::accept incoming connection accepted!"); } else if (status == BT_RESULT_WOULDBLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, (int)handle, context); } else if (status == BT_RESULT_FAILURE) { char* pError; bt_rfcomm_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IO error in btspp_notif::accept (%s)\n", pError); REPORT_INFO(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(gKNIBuffer)); } else { char* pMsg = "Unknown error during btspp_notif::accept"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Sends a datagram. * <p> * Java declaration: * <pre> * send0(II[BII)I * </pre> * * @param ipNumber raw IPv4 address of the remote host * @param port UDP port of the remote host * @param buf the data buffer to send * @param off the offset into the data buffer * @param len the length of the data in the buffer * @return number of bytes sent */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_datagram_Protocol_send0(void) { int ipAddress; int port; int offset; int length; int bytesSent = 0; void *socketHandle; MidpReentryData* info; unsigned char ipBytes[MAX_ADDR_LENGTH]; KNI_StartHandles(2); KNI_DeclareHandle(bufferObject); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); ipAddress = (int)KNI_GetParameterAsInt(1); port = (int)KNI_GetParameterAsInt(2); KNI_GetParameterAsObject(3, bufferObject); offset = (int)KNI_GetParameterAsInt(4); length = (int)KNI_GetParameterAsInt(5); socketHandle = (void *)getMidpDatagramProtocolPtr(thisObject)->nativeHandle; REPORT_INFO5(LC_PROTOCOL, "datagram::send0 off=%d len=%d port=%d ip=0x%x handle=0x%x", offset, length, port, ipAddress, (int)socketHandle); /* Convert ipAddress(integer) to ipBytes */ memcpy(ipBytes, &ipAddress, sizeof(ipBytes)); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (socketHandle != INVALID_HANDLE) { int status; void *context; if (info == NULL) { /* initial invocation */ INC_NETWORK_INDICATOR; SNI_BEGIN_RAW_POINTERS; status = pcsl_datagram_write_start( socketHandle, ipBytes, port, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesSent, &context); SNI_END_RAW_POINTERS; } else { /* reinvocation */ if ((void *)info->descriptor != socketHandle) { REPORT_CRIT2(LC_PROTOCOL, "datagram::send0 handle mismatch 0x%x != 0x%x\n", socketHandle, info->descriptor); } context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = pcsl_datagram_write_finish( socketHandle, ipBytes, port, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesSent, context); SNI_END_RAW_POINTERS; } if (status == PCSL_NET_SUCCESS) { DEC_NETWORK_INDICATOR; } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)socketHandle, context); } else if (status == PCSL_NET_INTERRUPTED) { KNI_ThrowNew(midpInterruptedIOException, NULL); DEC_NETWORK_INDICATOR; } else { /* status == PCSL_NET_IOERROR */ midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "error code %d", pcsl_network_error(socketHandle)); KNI_ThrowNew(midpIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } } else { if (info == NULL) { /* initial invocation */ KNI_ThrowNew(midpIOException, "socket closed"); } else { /* reinvocation */ KNI_ThrowNew(midpInterruptedIOException, NULL); DEC_NETWORK_INDICATOR; } } KNI_EndHandles(); KNI_ReturnInt((jint)bytesSent); }
/** * Accepts incoming client connection request. * * Note: the method gets native connection handle directly from * <code>handle<code> field of <code>L2CAPNotifierImpl</code> object. * * Note: new native connection handle to work with accepted incoming * client connection is setted directly to <code>handle</code> field of * appropriate <code>L2CAPConnectionImpl</code> object. * * @return Negotiated ReceiveMTU and TransmitMTU. * 16 high bits is ReceiveMTU, 16 low bits is TransmitMTU. * @throws IOException if an I/O error occurs */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_btl2cap_L2CAPNotifierImpl_accept0(void) { javacall_handle handle = BT_INVALID_HANDLE; javacall_handle peer_handle = BT_INVALID_HANDLE; MidpReentryData* info; int status = JAVACALL_FAIL; int processStatus = KNI_FALSE; int imtu, omtu, mtus; void *context = NULL; javacall_bt_address peer_addr; unsigned char *address = NULL; KNI_StartHandles(2); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(arrayHandle); KNI_GetThisPointer(thisHandle); handle = (javacall_handle)KNI_GetIntField(thisHandle, notifHandleID); KNI_GetObjectField(thisHandle, peerAddrID, arrayHandle); if (handle == BT_INVALID_HANDLE) { REPORT_ERROR(LC_PROTOCOL, "L2CAP server socket was closed before btl2cap_notif::accept"); KNI_ThrowNew(midpInterruptedIOException, EXCEPTION_MSG( "L2CAP notifier was closed")); } else { bt_pushid_t pushid = KNI_GetIntField(thisHandle, pushHandleID); if (pushid != BT_INVALID_PUSH_HANDLE) { if (bt_push_checkout_client(pushid, &peer_handle, peer_addr, &imtu, &omtu) == JAVACALL_OK) { pushcheckoutaccept((int)handle); processStatus = KNI_TRUE; status = JAVACALL_OK; } } if (peer_handle == BT_INVALID_HANDLE) { info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ REPORT_INFO1(LC_PROTOCOL, "btl2cap_notif::accept handle=%d\n", handle); // Need revisit: add resource counting /* * An incoming socket connection counts against the client socket * resource limit. */ /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); */ // } else { status = javacall_bt_l2cap_accept( handle, &peer_handle, &peer_addr, &imtu, &omtu); processStatus = KNI_TRUE; // } } else { /* Reinvocation after unblocking the thread */ if ((javacall_handle)info->descriptor != handle) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "btl2cap_notif::accept handles mismatched %d != %d\n", handle, info->descriptor); REPORT_CRIT(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIllegalStateException, EXCEPTION_MSG( "Internal error in btl2cap_notif::accept")); } else { // Need revisit: add resource counting /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets" REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } else { */ status = javacall_bt_l2cap_accept( handle, &peer_handle, &peer_addr, &imtu, &omtu); processStatus = KNI_TRUE; // } } } } if (processStatus) { REPORT_INFO1(LC_PROTOCOL, "btl2cap_notif::accept server handle=%d\n", handle); if (status == JAVACALL_OK) { int i; // Need revisit: add resource counting /* if (midpIncResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "btl2cap_notif: Resource limit update error"); } */ // store client native connection handle for temporary storing KNI_SetIntField(thisHandle, peerHandleID, (jint)peer_handle); // copy address to Java object field SNI_BEGIN_RAW_POINTERS; address = JavaByteArray(arrayHandle); for (i = 0; i < BT_ADDRESS_SIZE; i++) { address[i] = peer_addr[i]; } SNI_END_RAW_POINTERS; REPORT_INFO(LC_PROTOCOL, "btl2cap_notif::accept incoming connection accepted!"); } else if (status == JAVACALL_WOULD_BLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, (int)handle, context); } else if (status == JAVACALL_FAIL) { char* pError; javacall_bt_l2cap_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IO error in btl2cap_notif::accept (%s)\n", pError); REPORT_INFO(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(gKNIBuffer)); } else { char* pMsg = "Unknown error during btl2cap_notif::accept"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } } } mtus = (imtu << 16) & 0xFFFF0000; mtus |= omtu & 0xFFFF; KNI_EndHandles(); KNI_ReturnInt(mtus); }