/** * 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); }
/** * Write to a serial port without blocking. * * @param hPort handle to a native serial port * @param pBuffer I/O buffer * @param nNumberOfBytesToWrite length of data * @param pBytesWritten returns the number of bytes written after * successful write operation; only set if this function returns * PCSL_NET_SUCCESS * @param context ptr to data saved before sleeping * * @return PCSL_NET_SUCCESS for successful write operation;\n * PCSL_NET_WOULDBLOCK if the operation would block,\n * PCSL_NET_INTERRUPTED for an Interrupted IO Exception\n * PCSL_NET_IOERROR for all other errors */ int writeToPortFinish(int hPort, char* pBuffer, int nNumberOfBytesToWrite, int* pBytesWritten, void *context) { (void)context; return writeToPortStart(hPort, pBuffer, nNumberOfBytesToWrite, pBytesWritten, NULL); }