KNI_RETURNTYPE_BYTE
KNIDECL(com_sun_midp_security_Permissions_getMaxValue) {
    
    int str_len;
    jbyte value;
    jchar jbuff[64];
    char  domain_name[64], group_name[64];

    KNI_StartHandles(2);
    KNI_DeclareHandle(domain);
    KNI_DeclareHandle(group);

    value = 0;
    KNI_GetParameterAsObject(1, domain);
    KNI_GetParameterAsObject(2, group);
    if (!KNI_IsNullHandle(domain) && !KNI_IsNullHandle(group)) {
        str_len = KNI_GetStringLength(domain);
        KNI_GetStringRegion(domain, 0, str_len, jbuff);
        jchar_to_char(jbuff, domain_name, str_len);
        str_len = KNI_GetStringLength(group);
        KNI_GetStringRegion(group, 0, str_len, jbuff);
        jchar_to_char(jbuff, group_name, str_len);
        value = (jbyte)permissions_get_max_value(domain_name, group_name);
    }

    KNI_EndHandles();
    KNI_ReturnByte(value);
}
Example #2
0
KNIEXPORT KNI_RETURNTYPE_VOID
KNIDECL(com_sun_midp_main_CDCInit_initMidpNativeStates) {
    jchar jbuff[1024];
    static char conf_buff[1024], store_buff[1024];
    int max = sizeof(conf_buff) - 1;
    int len, i;

    KNI_StartHandles(2);    
    KNI_DeclareHandle(config);
    KNI_DeclareHandle(storage);
    
    KNI_GetParameterAsObject(1, config);
    KNI_GetParameterAsObject(2, storage);
	
    len = KNI_GetStringLength(config);
    if (len > max) {
        len = max;
    }
    KNI_GetStringRegion(config, 0, len, jbuff);
    for (i=0; i<len; i++) {
        conf_buff[i] = (char)jbuff[i];
    }
    conf_buff[len] = 0;
    len = KNI_GetStringLength(storage);
    if (len > max) {
        len = max;
    }
    KNI_GetStringRegion(storage, 0, len, jbuff);
    for (i=0; i<len; i++) {
        store_buff[i] = (char)jbuff[i];
    }
    store_buff[len] = 0;

    initCDCEvents();

    midpSetAppDir(store_buff);
    midpSetConfigDir(conf_buff);
    
    if (midpInitialize() != 0) {
        printf("midpInitialize() failed\n");

    }

    if (midpInitCallback(VM_LEVEL, midpInitializeUI, midpFinalizeUI) != 0) {
        printf("midpInitCallback(VM_LEVEL, ...) failed\n");
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Example #3
0
/**
 * Compare two strings using locale- and level-specific rules.
 * <p>
 * Java declaration:
 * <pre>
 *     compare0(Ljava/lang/String;Ljava/lang/String;II)I
 * </pre>
 *
 * @param locale_index  the locale index in supported locales list
 * @param hstr1         first string to compare
 * @param hstr2         second string to compare
 * @param level         the collation level to use
 * @return negative if <code>s1</code> belongs before <code>s2</code>,
 *      zero if the strings are equal, positive if <code>s1</code> belongs
 *      after <code>s2</code>
 */
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_j2me_global_StringComparatorImpl_compare0() {
   jint locale_index = KNI_GetParameterAsInt(1);
   jint level = KNI_GetParameterAsInt(4);
   jchar *s1, *s2;
   jsize s1_len, s2_len;
   jint res, compare_result = 0;

   KNI_StartHandles(2);
   KNI_DeclareHandle(hstr1);
   KNI_DeclareHandle(hstr2);
   KNI_GetParameterAsObject(2, hstr1);
   KNI_GetParameterAsObject(3, hstr2);

   s1_len = KNI_GetStringLength(hstr1);
   if (s1_len == -1){
		KNI_ThrowNew(midpNullPointerException, NULL);
   } else {
	   s1 = (jchar *)midpMalloc(s1_len * sizeof(jchar));
	   if (NULL == s1) {
		   KNI_ThrowNew(midpOutOfMemoryError, 
			   "Cannot allocate string for collation");
	   } else {
		   s2_len = KNI_GetStringLength(hstr2);
		   if (s2_len == -1){
			   KNI_ThrowNew(midpNullPointerException, NULL);
		   } else {
			   s2 = (jchar *)midpMalloc(s2_len * sizeof(jchar));
			   if (NULL == s2) {
				   KNI_ThrowNew(midpOutOfMemoryError, 
					   "Cannot allocate string for collation");
			   } else {
				   KNI_GetStringRegion(hstr1, 0, s1_len, s1);
				   KNI_GetStringRegion(hstr2, 0, s2_len, s2);
				   res = jsr238_compare_strings(locale_index, s1, s1_len, s2, s2_len, 
													   level, &compare_result);
				   if (res < 0){
						KNI_ThrowNew(midpRuntimeException,"Error comparing strings");
				   }
				   midpFree(s2);
			   }
		   }
		   midpFree(s1);
	   }
   }

   KNI_EndHandles();
   KNI_ReturnInt(compare_result);
}
Example #4
0
/**
 * Gets a handle to specific character encoding conversion routine.
 * <p>
 * Java declaration:
 * <pre>
 *     getHandler(Ljava/lang/String;)I
 * </pre>
 *
 * @param encoding character encoding
 *
 * @return identifier for requested handler, or <tt>-1</tt> if
 *         the encoding was not supported.
 */
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_cldc_i18n_j2me_Conv_getHandler() {
    jint     result = 0;

    KNI_StartHandles(1);

    KNI_DeclareHandle(str);
    KNI_GetParameterAsObject(1, str);

    if (!KNI_IsNullHandle(str)) {
        int      strLen = KNI_GetStringLength(str);
	jchar* strBuf;

	/* Instead of always multiplying the length by sizeof(jchar),
	 * we shift left by 1. This can be done because jchar has a
	 * size of 2 bytes.
	 */
	strBuf = (jchar*)midpMalloc(strLen<<1);
	if (strBuf != NULL) { 
	    KNI_GetStringRegion(str, 0, strLen, strBuf);
	    result = getLcConvMethodsID(strBuf, strLen);
	    midpFree(strBuf);
	} else {
	    KNI_ThrowNew(midpOutOfMemoryError, NULL);
	}
    }

    KNI_EndHandles();
    KNI_ReturnInt(result);
}
Example #5
0
/*  private native int nSetLocator ( int handle , String locator ) ; */
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_mmedia_DirectRecord_nSetLocator() {
    jint handle = KNI_GetParameterAsInt(1);
    KNIPlayerInfo* pKniInfo = (KNIPlayerInfo*)handle;
    jint locatorLength;
    jchar* locator = NULL;
    jint returnValue = 0;
    javacall_result ret;

    KNI_StartHandles(1);
    KNI_DeclareHandle(locatorHandle);
    KNI_GetParameterAsObject(2, locatorHandle);
    
    locatorLength = KNI_GetStringLength(locatorHandle);
    locator = MMP_MALLOC(locatorLength * sizeof(jchar));
    if (locator) {
        KNI_GetStringRegion(locatorHandle, 0, locatorLength, locator);
        if (pKniInfo && pKniInfo->pNativeHandle) {
            ret = javacall_media_recording_handled_by_native(pKniInfo->pNativeHandle, locator, locatorLength);
            if (JAVACALL_INVALID_ARGUMENT == ret) {
                returnValue = -1;
                REPORT_ERROR1(LC_MMAPI, "[kni_record] Set recording location \
                    return JAVACALL_INVALID_ARGUMENT handle=%d\n", pKniInfo->pNativeHandle);
            } else {
                if (JAVACALL_OK == ret) {
/**
 * Get index of supported locales for device resources by its name.
 * <p>
 * Java declaration:
 * <pre>
 *     getDevLocaleIndex(Ljava/lang/String)I
 * </pre>
 *
 * @param locale name
 * @return internal index of locale or -1 if locale is not supported 
 */
KNIEXPORT KNI_RETURNTYPE_INT
KNIDECL(com_sun_j2me_global_DevResourceManagerFactory_getDevLocaleIndex) {
	jint result =-1, index = 0;
	jsize len = 0;
	int error = 0;
	jchar* locale_name;

	KNI_StartHandles(1);
	KNI_DeclareHandle(hstr1);
	KNI_GetParameterAsObject(1, hstr1);

	if (KNI_IsNullHandle(hstr1)) {
		locale_name = NULL;
	} else  {
		len = KNI_GetStringLength(hstr1);
		locale_name = (jchar *)JAVAME_MALLOC((len + 1) * sizeof(jchar));
		if (NULL == locale_name) {
		   KNI_ThrowNew(jsropOutOfMemoryError, 
			   "Out of memory");
		   error = 1;
		} else {
			KNI_GetStringRegion(hstr1, 0, len, locale_name);
			locale_name[len]=0;
		}
	}

	if (!error){
		result = jsr238_get_resource_locale_index(locale_name, &index);
		if (result < 0) index =-1;
		JAVAME_FREE(locale_name);
	}

	KNI_EndHandles();
	KNI_ReturnInt(index);
}
Example #7
0
static void sendControlStringField(KNIDECLARGS kjobject objectHandle,
                                   kjobject stringObj, jfieldID fieldID) {
    int len;
    jchar *data = NULL;
    (void) _arguments;
    (void) _p_mb;
    (void) _ee;

    KNI_GetObjectField(objectHandle, fieldID, stringObj);
    len = KNI_GetStringLength(stringObj);
    if (len > 0) {
        data = (jchar*)midpMalloc(len * sizeof(jchar));
        if (data == NULL) {
            len = 0; /* IMPL_NOTE: throw out of memory */
        } else {
            KNI_GetStringRegion(stringObj, 0, len, data);
        }
    }

    write(controlPipe[1], &len, sizeof(int));
    if (len > 0) {
        write(controlPipe[1], data, len * sizeof(jchar));
        midpFree(data);
    }
}
/**
 * Gets the value of the specified property key in the internal
 * property set. If the key is not found in the internal property
 * set, the application property set is then searched.
 * <p>
 * Java declaration:
 * <pre>
 *     getProperty0(Ljava.lang.String;)Ljava.lang.String;
 * <pre>
 *
 * @param key The key to search for
 *
 * @return The value associated with <tt>key<tt> if found, otherwise
 *         <tt>null<tt>
 */
KNIEXPORT KNI_RETURNTYPE_OBJECT
KNIDECL(com_sun_midp_main_Configuration_getProperty0) {
    jchar* uStr;
    const char* key;
    const char* value;
    int strLen;

    KNI_StartHandles(2);
    KNI_DeclareHandle(str);
    KNI_DeclareHandle(result);

    KNI_GetParameterAsObject(1, str);
    strLen = KNI_GetStringLength(str);

    if (strLen <= 0 || (uStr = (jchar*) midpMalloc(strLen * sizeof(jchar))) == NULL) {
        KNI_ThrowNew(midpOutOfMemoryError, NULL);
    } else {
        KNI_GetStringRegion(str, 0, strLen, uStr);
        key = UnicodeToCString(uStr, strLen);
        midpFree(uStr);

        /* Look up the property value */
        value = getInternalProperty(key);
        midpFree((void *)key);

        if (value != NULL) {
            KNI_NewStringUTF(value, result);
        } else {
            KNI_ReleaseHandle(result);
        }
    }
    KNI_EndHandlesAndReturnObject(result);
}
Example #9
0
/**
 * Get index of supported locales for collation by its name.
 * <p>
 * Java declaration:
 * <pre>
 *     getDevLocaleIndex(Ljava/lang/String)I
 * </pre>
 *
 * @param locale name
 * @return internal index of locale or -1 if locale is not supported 
 */
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_j2me_global_CollationAbstractionLayerImpl_getCollationLocaleIndex() {
	jint result =-1, index = 0;
	jsize len = 0;
	int error = 0;
	jchar* locale_name;

	KNI_StartHandles(1);
	KNI_DeclareHandle(hstr1);
	KNI_GetParameterAsObject(1, hstr1);

	if (KNI_IsNullHandle(hstr1)) {
		locale_name = NULL;
	} else  {
		len = KNI_GetStringLength(hstr1);
		locale_name = (jchar *)midpMalloc((len + 1) * sizeof(jchar));
		if (NULL == locale_name) {
		   KNI_ThrowNew(midpOutOfMemoryError, 
			   "Out of memory");
		   error = 1;
		} else {
			KNI_GetStringRegion(hstr1, 0, len, locale_name);
			locale_name[len]=0;
		}
	}

	if (!error){
		result = jsr238_get_collation_locale_index(locale_name, &index);
		if (result < 0) index =-1;
		midpFree(locale_name);
	}

	KNI_EndHandles();
	KNI_ReturnInt(index);
}
KNIEXPORT KNI_RETURNTYPE_OBJECT
KNIDECL(KNITest_getStringRegion) {
    jchar buf[20];
    jint offset = KNI_GetParameterAsInt(2);
    jint len = KNI_GetParameterAsInt(3);
    KNI_StartHandles(2);
    KNI_DeclareHandle(str);
    KNI_DeclareHandle(newStr);
    KNI_GetParameterAsObject(1, str);
    KNI_GetStringRegion(str, offset, len, buf);
    KNI_NewString(buf, len, newStr);
    KNI_EndHandlesAndReturnObject(newStr);
}
Example #11
0
/**
 * Create fill buffer of javacall_utf16 from the specified Java platform String object.
 *
 * @param java_str pointer to the Java platform String instance
 * @param utf16_str address of buffer to fill
 * @param length length of buffer to fill
 * @return status of the operation
 */
javacall_result jsrop_jstring_to_utf16(jstring java_str,
                    javacall_utf16_string utf16_str, int length) {
    javacall_result res = JAVACALL_FAIL;
    jsize string_length = KNI_GetStringLength(java_str);

    if (string_length <= length) {
        KNI_GetStringRegion(java_str, 0, string_length, (jchar *)utf16_str);
        *(utf16_str + string_length) = 0;
        res = JAVACALL_OK;
    }
    else {
        res = JAVACALL_INVALID_ARGUMENT;
    }
    return res;
}
KNI_RETURNTYPE_OBJECT
KNIDECL(com_sun_midp_security_Permissions_loadGroupPermissions)
{
    int lines, i1;
	unsigned int str_len;
    void *array;
    jchar jbuff[64];
    char  group_name[64];

    KNI_StartHandles(3);
    KNI_DeclareHandle(members);
    KNI_DeclareHandle(tmpString);
    KNI_DeclareHandle(group);

    KNI_GetParameterAsObject(1, group);
    if (!KNI_IsNullHandle(group)) {
        str_len = KNI_GetStringLength(group);
        if (str_len <= sizeof(group_name)-1) {
            KNI_GetStringRegion(group, 0, str_len, jbuff);
            jchar_to_char(jbuff, group_name, str_len);
            lines = permissions_load_group_permissions(&array, group_name);
            if (lines > 0) {
                char **list = (char**)array;
                SNI_NewArray(SNI_STRING_ARRAY,  lines, members);
                if (KNI_IsNullHandle(members))
                    KNI_ThrowNew(midpOutOfMemoryError, NULL);
                else
                    for (i1 = 0; i1 < lines; i1++) {
                        KNI_NewStringUTF(list[i1], tmpString);
                        KNI_SetObjectArrayElement(members, (jint)i1,
                                                            tmpString);
                    }
                permissions_dealloc(array);
            } else
                KNI_ReleaseHandle(members);  /* set object to NULL */
        }
    } else
        KNI_ThrowNew(midpNullPointerException, "null group parameter");


    KNI_EndHandlesAndReturnObject(members);
}
Example #13
0
/**
 * Allocates a jchar array and copies all Unicode characters
 * from the given java.lang.String object (specified by jStringHandle)
 * to the allocated array.
 * A pointer to the array is stored in pAddr.
 * The caller MUST free the allocated array with midpFree() after use.
 *
 * If the given java.lang.String is null, NULL_LEN will be returned
 * and pAddr will be set to NULL.
 * If out of memory, OUT_OF_MEM_LEN will be returned
 * and pAddr will be set to NULL.
 *
 * @param jStringHandle KNI Java String object handle
 * @param pAddr points to a jchar* variable receiving the address of the buffer
 *              with a new unicode string. The caller MUST free this memory
 *              using midpFree when it's not needed anymore.
 *              Receives NULL if no memory has been allocated
 *              (error or null string).
 *
 * @return the new unicode string length, or one of the values:
 *          NULL_LEN if the string is null
 *          OUT_OF_MEM_LEN in the case of out-of-memory error
 */
jint midp_jstring_to_address_and_length(jstring jStringHandle, jchar* * pAddr) {
    jchar* result_data;
    jint result_len;

    result_data = NULL;
    result_len  = KNI_GetStringLength(jStringHandle);

    if (result_len < 0) {
        result_len = NULL_LEN;
    } else if (result_len > 0) {
    	result_data = (jchar*)midpMalloc(result_len * sizeof (jchar));
        if (result_data == NULL) {
            result_len = OUT_OF_MEM_LEN;
        } else {
            KNI_GetStringRegion(jStringHandle, 0, result_len, result_data);
        }
    }

    *pAddr = result_data;
    return result_len;
}
Example #14
0
/**
 * Passes the URL to the native handler. Return true to signal the suite
 * that it must exit before the request can be handled, or false if
 * the request was spawned in the background. Throw ConnectionNotFoundException
 * if we know at the time of this call the request cannot be handled.
 * <p>
 * Java declaration:
 * <pre>
 *     boolean dispatchPlatformRequest(String url)
 *             throws ConnectionNotFoundException
 * </pre>
 * Java parameter:
 * <pre>
 *     url - The URL String to handle
 * </pre>
 */
KNIEXPORT KNI_RETURNTYPE_BOOLEAN
KNIDECL(com_sun_midp_main_CldcPlatformRequest_dispatchPlatformRequest) {
    jsize urlLen;
    char* pszUrl;
    jchar* temp;
    int i;
    int connectionFound = KNI_FALSE;

    KNI_StartHandles(1);
    KNI_DeclareHandle(urlObj);

    KNI_GetParameterAsObject(1, urlObj);
    if (!KNI_IsNullHandle(urlObj)) {
        urlLen = KNI_GetStringLength(urlObj);
        if (urlLen >= 0) {
            pszUrl = (char*)midpMalloc((urlLen + 1) * sizeof (jchar));
            if (pszUrl != NULL) {
                temp = (jchar*)pszUrl;
                KNI_GetStringRegion(urlObj, 0, urlLen, (jchar*)temp);

                /* simply convert the unicode by stripping the high byte */
                for (i = 0; i < urlLen; i++) {
                    pszUrl[i] = (char)temp[i];
                }

                pszUrl[urlLen] = 0;
                connectionFound = platformRequest(pszUrl);
                midpFree(pszUrl);
            }
        }
    }

    KNI_EndHandles();

    if (!connectionFound) {
        KNI_ThrowNew(midpConnectionNotFoundException, NULL);
    }

    KNI_ReturnBoolean(KNI_FALSE);
}
Example #15
0
/**
 * Create javacall_utf16_string from the specified Java platform String object.
 * The caller is responsible for freeing the created javacall_utf16_string when done.
 *
 * @param java_str pointer to the Java platform String instance. 
 * @param utf16_str address of variable to receive the javacall_utf16_string instance.
 *                  NULL if java_str is null
 * @return status of the operation
 */
javacall_result jsrop_jstring_to_utf16_string(jstring java_str,
                             javacall_utf16_string * utf16_str) {
    javacall_result res = JAVACALL_FAIL;
    if (!KNI_IsNullHandle(java_str)) {
        jsize string_length = KNI_GetStringLength(java_str);
        javacall_utf16_string str_buffer = JAVAME_MALLOC((string_length + 1) *
                                                    sizeof(javacall_utf16));
        if (str_buffer != NULL) {
            KNI_GetStringRegion(java_str, 0, string_length, (jchar *)str_buffer);
            *(str_buffer + string_length) = 0;
            *utf16_str = str_buffer;
            res = JAVACALL_OK;
        }
        else {
            res = JAVACALL_OUT_OF_MEMORY;
        }
    } else {
        *utf16_str = NULL;
        res = JAVACALL_OK;
    }
    return res;
}
Example #16
0
/**
 * Create pcsl_string from the specified Java String object.
 * The caller is responsible for freeing the created pcsl_string when done.
 *
 * @param java_str pointer to the Java String instance
 * @param pcsl_str pointer to the pcsl_string instance
 * @return status of the operation
 */
pcsl_string_status midp_jstring_to_pcsl_string(jstring java_str,
					       pcsl_string * pcsl_str) {
  if (pcsl_str == NULL) {
    return PCSL_STRING_EINVAL;
  }

  if (KNI_IsNullHandle(java_str)) {
    * pcsl_str = PCSL_STRING_NULL;
    return PCSL_STRING_OK;
  } else {
    const jsize length  = KNI_GetStringLength(java_str);

    if (length < 0) {
      * pcsl_str = PCSL_STRING_NULL;
      return PCSL_STRING_ERR;
    } else if (length == 0) {
      * pcsl_str = PCSL_STRING_EMPTY;
      return PCSL_STRING_OK;
    } else {
      jchar * buffer = pcsl_mem_malloc(length * sizeof(jchar));

      if (buffer == NULL) {
	* pcsl_str = PCSL_STRING_NULL;
	return PCSL_STRING_ENOMEM;
      }

      KNI_GetStringRegion(java_str, 0, length, buffer);

      {
	pcsl_string_status status =
	  pcsl_string_convert_from_utf16(buffer, length, pcsl_str);

	pcsl_mem_free(buffer);

	return status;
      }
    }
  }
}
Example #17
0
/**
 * Create a new MIDP string from a KNI String object.
 * If KNI String is null, an NULL_LEN length MidpString will be returned.
 * If out of memory a OUT_OF_MEM_LEN length MidpString will be returned.
 * The caller is responsible for calling midpFreeString() after use.
 * This function should not be used directly,
 * use the midpNewString macro.
 *
 * @param jStringHandle KNI Java String object handle
 * @param filename provided by the midpNewString macro
 * @param line provided by the midpNewString macro
 *
 * @return a new unicode string
 */
MidpString midpNewStringImpl(jstring jStringHandle, char* filename,
       int line) {
    MidpString result;

    (void)filename;                               /* Avoid compiler warnings */
    (void)line;                                   /* Avoid compiler warnings */

    result.data = NULL;
    result.len  = KNI_GetStringLength(jStringHandle);

    if (result.len < 0) {
        result.len = NULL_LEN;
    } else if (result.len > 0) {
    	result.data = (jchar*)midpMallocImpl(result.len * sizeof (jchar),
					     filename, line);
	if (result.data == NULL) {
            result.len = OUT_OF_MEM_LEN;
	} else {
	    KNI_GetStringRegion(jStringHandle, 0, result.len, result.data);
	}
    }

    return result;
}
Example #18
0
/**
 * Open a serial port by system dependent device name.
 *
 * @param name device name of the port
 * @param baud baud rate to set the port at
 * @param flags options for the serial port
 *
 * @return handle to a native serial port
 *
 * @exception  IOException  if an I/O error occurs.
 */
KNIEXPORT KNI_RETURNTYPE_INT
    Java_com_sun_midp_io_j2me_comm_Protocol_native_1openByName() {

    int    flags = (int)KNI_GetParameterAsInt(3);
    int    baud = (int)KNI_GetParameterAsInt(2);
    int    nameLen;
    char   szName[MAX_NAME_LEN];
    jchar* temp;
    int    hPort = (int)INVALID_HANDLE;
    int    i;
    int status = PCSL_NET_IOERROR;
    void* context = NULL;
    MidpReentryData* info;

    KNI_StartHandles(1);
    KNI_DeclareHandle(nameObject);
    KNI_GetParameterAsObject(1, nameObject);
    
    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    if (info == NULL) {
        nameLen = KNI_GetStringLength(nameObject);
        if (nameLen > MAX_NAME_LEN) {
            midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                "Serial device name has wrong length: %d\n", nameLen);
            REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer);
            KNI_ThrowNew(midpIllegalArgumentException, gKNIBuffer);
        } else {
            temp = (jchar*)szName;
            KNI_GetStringRegion(nameObject, 0, nameLen, temp);

            /* device names are in ASCII */
            for (i = 0; i < nameLen; i++) {
                szName[i] = (char)temp[i];
            }
            szName[nameLen] = 0;
		
            status = openPortByNameStart(szName, baud, flags, &hPort, &context);
        }
    } else {
        /* reinvocation */
        hPort = info->descriptor;
        context = info->pResult;
        status = openPortByNameFinish(szName, baud, flags, &hPort, context);	
    }

    switch (status) {
        case PCSL_NET_SUCCESS:			
            /* do nothing and return normally */
            break;
        case PCSL_NET_INTERRUPTED:			
            midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                "Opening port %s has been interrupted\n", szName);
            REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer);
            KNI_ThrowNew(midpInterruptedIOException, gKNIBuffer);
            break;
        case PCSL_NET_WOULDBLOCK:			
            midp_thread_wait(COMM_OPEN_SIGNAL, hPort, context);
            break;
        default:	   
            midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                "Opening port %s was failed\n", szName);
            REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer);
            KNI_ThrowNew(midpIOException, gKNIBuffer);
    }

    KNI_EndHandles();
    KNI_ReturnInt((jint)hPort);
}
Example #19
0
KNIEXPORT KNI_RETURNTYPE_INT
KNIDECL(com_sun_mmedia_DefaultConfiguration_nListContentTypesOpen) {
    javacall_int32 proto_mask = 0;
    javacall_bool deviceProtocol = JAVACALL_FALSE;
    const javacall_media_configuration *cfg;
    const javacall_media_caps *caps;
    unsigned int len;
    ListIterator *iterator = NULL;
    char *p;
    
    /* stack buffers. Trying to avoid malloc if a string is not big */
    jchar stack_string16_buffer[MAX_PROTOCOLNAME_LEN], *string16 = NULL;
    char stack_string_buffer[MAX_PROTOCOLNAME_LEN], *proto = NULL;
    
    KNI_StartHandles(1);
    KNI_DeclareHandle(stringObj);
    KNI_GetParameterAsObject(1, stringObj);

    do {
        if (KNI_IsNullHandle(stringObj)) {
            proto = NULL;
        } else {
            len = KNI_GetStringLength(stringObj);
            /* if the string is longer than the stack buffer try to malloc it */
            if (len >= sizeof stack_string16_buffer / sizeof stack_string16_buffer[0]) {
                string16 = MMP_MALLOC((len + 1) * sizeof *string16);
                if (string16 == NULL) {
                    KNI_ThrowNew(jsropOutOfMemoryError, NULL);
                    break;
                }
            } else {
                string16 = stack_string16_buffer;
            }
            if (len >= sizeof stack_string_buffer) {
                proto = MMP_MALLOC(len + 1);
                if (proto == NULL) {
                    KNI_ThrowNew(jsropOutOfMemoryError, NULL);
                    break;
                }
            } else {
                proto = stack_string_buffer;
            }
            KNI_GetStringRegion(stringObj, 0, len, string16);
            if (simple_jcharString_to_asciiString(string16, len, proto, len + 1) != JAVACALL_OK) {
                KNI_ThrowNew(jsropIllegalArgumentException, "Illegal character in protocol name");
                break;
            }
        }
        if (proto != NULL) {
            unsigned int i;
            
            /* trying to find protocol by name */
            for (i = 0; i < sizeof protocolNames / sizeof protocolNames[0]; i++) {
                if (protocolNames[i].proto_name != NULL && !javautil_stricmp(protocolNames[i].proto_name, proto)) {
                    proto_mask |= protocolNames[i].proto_mask;
                }
            }
            if (!javautil_stricmp(proto, DEVICE_PROTOCOL)) {
                deviceProtocol = JAVACALL_TRUE;
            }
        }
        if (proto != NULL && proto_mask == 0 && !deviceProtocol) {
            /* Requested protocol wasn't found. Return 0 */
            break;
        }
        
        if (javacall_media_get_configuration(&cfg) != JAVACALL_OK) {
            KNI_ThrowNew(jsropRuntimeException, "Couldn't get MMAPI configuration");
            break;
        }
        
        /* how long will be list of content types? */
        len = 0;
        for (caps = cfg->mediaCaps; caps != NULL && caps->mediaFormat != NULL; caps++) {
            if (proto == NULL || (caps->wholeProtocols & proto_mask) != 0
                    || (caps->streamingProtocols & proto_mask) != 0) {
                len += strlen(caps->contentTypes) + 1; /* +1 for space char */
            }
        }
        if (proto == NULL || deviceProtocol) {
            if (cfg->supportDeviceMIDI) {
                len += strlen(MIME_AUDIO_MIDI) + 1;
            }
            if (cfg->supportDeviceTone) {
                len += strlen(MIME_AUDIO_TONE) + 1;
            }
        }
     
        if (len == 0) {
            /* No MIME types were found for provided protocol. Return 0 */
            break;
        }
     
        iterator = (ListIterator*)MMP_MALLOC(
            sizeof *iterator + len); /* zero terminator instead of last space */
        if (iterator == NULL) {
            KNI_ThrowNew(jsropOutOfMemoryError, NULL);
            break;
        }
    
        /* initialize the iterator */
        iterator->current = iterator->list;
    
        /* filling the list of content types */
        p = iterator->list;
        for (caps = cfg->mediaCaps; caps != NULL && caps->mediaFormat != NULL; caps++) {
            if (proto == NULL || (caps->wholeProtocols & proto_mask) != 0
                    || (caps->streamingProtocols & proto_mask) != 0) {
                int types_len = strlen(caps->contentTypes);
                memcpy(p, caps->contentTypes, types_len);
                p += types_len;
                *p++ = ' ';
            }
        }
        if (proto == NULL || deviceProtocol) {
            int types_len;
            if (cfg->supportDeviceMIDI) {
                types_len = strlen(MIME_AUDIO_MIDI);
                memcpy(p, MIME_AUDIO_MIDI, types_len);
                p += types_len;
                *p++ = ' ';
            }
            if (cfg->supportDeviceTone) {
                types_len = strlen(MIME_AUDIO_TONE);
                memcpy(p, MIME_AUDIO_TONE, types_len);
                p += types_len;
                *p++ = ' ';
            }
        }
        p--; *p = '\0'; /* replace last space with zero */
        
        mmapi_string_delete_duplicates(iterator->list);
    } while (0);

    /* freeing buffers */
    if (proto != NULL && proto != stack_string_buffer) {
        MMP_FREE(proto);
    }
    if (string16 != NULL && string16 != stack_string16_buffer) {
        MMP_FREE(string16);
    }
    KNI_EndHandles();
    KNI_ReturnInt((jint)iterator); 
}
Example #20
0
KNIEXPORT KNI_RETURNTYPE_INT
KNIDECL(com_sun_mmedia_DefaultConfiguration_nListProtocolsOpen) {
    const javacall_media_configuration *cfg;
    javacall_media_caps *caps;
    ListIterator *iterator = NULL;
    javacall_int32 proto_mask = 0;
    javacall_bool supportDeviceProtocol = JAVACALL_FALSE;
    char *p = NULL;
    
    /* stack buffers. Trying to avoid malloc if a string is not big */
    jchar stack_string16_buffer[MAX_PROTOCOLNAME_LEN], *string16 = NULL;
    char stack_string_buffer[MAX_PROTOCOLNAME_LEN], *mime = NULL;
    
    KNI_StartHandles(1);
    KNI_DeclareHandle(stringObj);
    KNI_GetParameterAsObject(1, stringObj);

    do {
        if (KNI_IsNullHandle(stringObj)) {
            mime = NULL;
        } else {
            unsigned int len = KNI_GetStringLength(stringObj);
            /* if the string is longer than the stack buffer try to malloc it */
            if (len >= sizeof stack_string16_buffer / sizeof stack_string16_buffer[0]) {
                string16 = MMP_MALLOC((len + 1) * sizeof *string16);
                if (string16 == NULL) {
                    KNI_ThrowNew(jsropOutOfMemoryError, NULL);
                    break;
                }
            } else {
                string16 = stack_string16_buffer;
            }
            if (len >= sizeof stack_string_buffer / sizeof stack_string_buffer[0]) {
                mime = MMP_MALLOC(len + 1);
                if (mime == NULL) {
                    KNI_ThrowNew(jsropOutOfMemoryError, NULL);
                    break;
                }
            } else {
                mime = stack_string_buffer;
            }
            KNI_GetStringRegion(stringObj, 0, len, string16);
            if (simple_jcharString_to_asciiString(string16, len, mime, len + 1) != JAVACALL_OK) {
                KNI_ThrowNew(jsropIllegalArgumentException, "Illegal character in MIME type name");
                break;
            }
        }
        if (javacall_media_get_configuration(&cfg) != JAVACALL_OK) {
            KNI_ThrowNew(jsropRuntimeException, "Couldn't get MMAPI configuration");
            break;
        }
        
        if ((mime == NULL || !javautil_stricmp(mime, MIME_AUDIO_MIDI)) && 
                    cfg->supportDeviceMIDI) {
            supportDeviceProtocol = JAVACALL_TRUE;
        }
        if (!supportDeviceProtocol && 
                    (mime == NULL || !javautil_stricmp(mime, MIME_AUDIO_TONE)) && 
                    cfg->supportDeviceTone) {
            supportDeviceProtocol = JAVACALL_TRUE;
        }
        /* trying to find given MIME type among caps->contentTypes */
        for (caps = cfg->mediaCaps; caps != NULL && caps->mediaFormat != NULL; caps++) {
            if (caps->wholeProtocols != 0 || caps->streamingProtocols != 0) {
                if (mime != NULL) {
                    char *s;
                    int m_len = strlen(mime);
                    
                    for (p = (char *)caps->contentTypes; p != NULL; p = s) {
                        int p_len;
                        
                        while (*p == ' ') {
                            p++;
                        }
                        if ((s = strchr(p, ' ')) != NULL) {
                            p_len = (int)(s - p);
                        } else {
                            p_len = strlen(p);
                        }
                        if (p_len == m_len && !javautil_strnicmp(mime, p, p_len)) {
                            break;
                        }
                    }
                }
                if (mime == NULL || p != NULL) {
                    proto_mask |= caps->wholeProtocols;
                    proto_mask |= caps->streamingProtocols;
                }
            }
        }
        
        if (proto_mask != 0 || supportDeviceProtocol) {
            /* some protocols were found */
            unsigned int i;
            unsigned int len = 0;
            
            /* trying to resolve protocol names: calculating needed memory */
            for (i = 0; i < sizeof protocolNames / sizeof protocolNames[0]; i++) {
                if ((protocolNames[i].proto_mask & proto_mask) != 0 && protocolNames[i].proto_name != NULL) {
                    len += strlen(protocolNames[i].proto_name) + 1; /* +1 for space char */
                }
            }
            if (supportDeviceProtocol) {
                len += strlen(DEVICE_PROTOCOL) + 1;
            }
            if (len == 0) {
                /* Protocol wasn't found in the protocol name table */
                KNI_ThrowNew(jsropRuntimeException, "Incorrect MMAPI configuration: missing protocol name");
                break;
            }
         
            iterator = (ListIterator*)MMP_MALLOC(
                sizeof *iterator + len); /* zero terminator instead of last space */
            if (iterator == NULL) {
                KNI_ThrowNew(jsropOutOfMemoryError, NULL);
                break;
            }
        
            /* initialize the iterator */
            iterator->current = iterator->list;
        
            /* building the list of protocols */
            p = iterator->list;
            for (i = 0; i < sizeof protocolNames / sizeof protocolNames[0]; i++) {
                if ((protocolNames[i].proto_mask & proto_mask) != 0 && protocolNames[i].proto_name != NULL) {
                    int proto_len = strlen(protocolNames[i].proto_name);
                    memcpy(p, protocolNames[i].proto_name, proto_len);
                    p += proto_len;
                    *p++ = ' ';
                }
            }
            if (supportDeviceProtocol) {
                int proto_len = strlen(DEVICE_PROTOCOL);
                memcpy(p, DEVICE_PROTOCOL, proto_len);
                p += proto_len;
                *p++ = ' ';
            }
            p--; *p = '\0'; /* replace last space with zero */
        } else {
            /* No protocols were found for provided MIME type. Return 0 */
            break;
        }
        mmapi_string_delete_duplicates(iterator->list);
    } while (0);

    /* freeing buffers */
    if (mime != NULL && mime != stack_string_buffer) {
        MMP_FREE(mime);
    }
    if (string16 != NULL && string16 != stack_string16_buffer) {
        MMP_FREE(string16);
    }
    KNI_EndHandles();
    KNI_ReturnInt((jint)iterator); 
}
Example #21
0
/**
 * Copies the contents of fromMsg to the contents of toMsg. Both must be
 * instances of LinkMessage. The toLink object must be an instance of Link.
 * It's filled in if the contents of fromMsg are a Link.  Returns KNI_TRUE if
 * successful, otherwise KNI_FALSE.
 */
static jboolean
copy(jobject fromMsg, jobject toMsg, jobject toLink) {
    jboolean retval;

    KNI_StartHandles(6);
    KNI_DeclareHandle(byteArrayClass);
    KNI_DeclareHandle(stringClass);
    KNI_DeclareHandle(linkClass);
    KNI_DeclareHandle(fromContents);
    KNI_DeclareHandle(newString);
    KNI_DeclareHandle(newByteArray);

    KNI_FindClass("[B", byteArrayClass);
    KNI_FindClass("java/lang/String", stringClass);
    KNI_FindClass("com/sun/midp/links/Link", linkClass);
    getContents(fromMsg, fromContents);
    
    if (KNI_IsInstanceOf(fromContents, byteArrayClass)) {
        /* do a byte array copy */
        jint fromOffset;
        jint fromLength;

        getRange(fromMsg, &fromOffset, &fromLength);

        SNI_NewArray(SNI_BYTE_ARRAY, fromLength, newByteArray);
        if (KNI_IsNullHandle(newByteArray)) {
            retval = KNI_FALSE;
        } else {
            KNI_GetRawArrayRegion(fromContents, fromOffset, fromLength,
                SNI_GetRawArrayPointer(newByteArray));
            setContents(toMsg, newByteArray);
            setRange(toMsg, 0, fromLength);
            retval = KNI_TRUE;
        }
    } else if (KNI_IsInstanceOf(fromContents, stringClass)) {
        /* do a string copy */
        jchar *buf;
        jsize slen = KNI_GetStringLength(fromContents);

        SNI_NewArray(SNI_BYTE_ARRAY, slen*sizeof(jchar), newByteArray);

        if (KNI_IsNullHandle(newByteArray)) {
            retval = KNI_FALSE;
        } else {
            buf = SNI_GetRawArrayPointer(newByteArray);
            KNI_GetStringRegion(fromContents, 0, slen, buf);
            KNI_NewString(buf, slen, newString);
            setContents(toMsg, newString);
            retval = KNI_TRUE;
        }
    } else if (KNI_IsInstanceOf(fromContents, linkClass)) {
        /* copy the link */
        rendezvous *rp = getNativePointer(fromContents);
        setNativePointer(toLink, rp);
        rp_incref(rp);
        setContents(toMsg, toLink);
        retval = KNI_TRUE;
    } else {
        retval = KNI_FALSE;
    }

    KNI_EndHandles();
    return retval;
}
Example #22
0
/**
 * Sends an SMS message.
 *
 * @param handle The handle to the open SMS connection.
 * @param messageType The type of message: binary or text.
 * @param address The SMS-formatted address.
 * @param destPort The port number of the recipient.
 * @param sourcePort The port number of the sender.
 * @param messageBuffer The buffer containing the SMS message.
 *
 * @return Always returns <code>0</code>.
 */
KNIEXPORT KNI_RETURNTYPE_INT
KNIDECL(com_sun_midp_io_j2me_sms_Protocol_send0) {
    WMA_STATUS status = WMA_ERR;
    jint messageLength = 0;
    jint messageType;
    jint sourcePort;
    jint destPort;
    jint handle;
    jint msAddress_len;
    jchar* msAddress_data;
    int i;
    unsigned char *pAddress = NULL;
    unsigned char *pMessageBuffer = NULL;
    jboolean stillWaiting = KNI_FALSE;
    jboolean trySend = KNI_FALSE;
    void *pdContext = NULL;
#if ENABLE_REENTRY
    MidpReentryData *info;
    jsr120_sms_message_state_data *messageStateData = NULL;
#endif
    jboolean isOpen;

    KNI_StartHandles(4);

    KNI_DeclareHandle(this);
    KNI_DeclareHandle(thisClass);
    KNI_GetThisPointer(this);
    KNI_GetObjectClass(this, thisClass);
    isOpen = KNI_GetBooleanField(this, KNI_GetFieldID(thisClass, "open", "Z"));
    
    if (isOpen) { /* No close in progress */
        KNI_DeclareHandle(messageBuffer);
        KNI_DeclareHandle(address);

        handle = KNI_GetParameterAsInt(1);
        messageType = KNI_GetParameterAsInt(2);
        KNI_GetParameterAsObject(3, address);
        destPort = KNI_GetParameterAsInt(4);
        sourcePort = KNI_GetParameterAsInt(5);
        KNI_GetParameterAsObject(6, messageBuffer);

        do {
#if ENABLE_REENTRY
            info = (MidpReentryData*)SNI_GetReentryData(NULL);
            if (info == NULL) {	  /* First invocation. */
#endif
                if (KNI_IsNullHandle(address)) {

                    KNI_ThrowNew(midpIllegalArgumentException, NULL);
                    break;
                } else {
                    msAddress_len = KNI_GetStringLength(address);
                    msAddress_data = (jchar *)pcsl_mem_malloc(msAddress_len * sizeof (jchar));
                    if (msAddress_data == NULL) {

                        KNI_ThrowNew(midpOutOfMemoryError, NULL);
                        break;
                    } else {

                        KNI_GetStringRegion(address, 0, msAddress_len, msAddress_data);
                        pAddress = (unsigned char*)pcsl_mem_malloc(msAddress_len + 1);
                        if (pAddress != NULL) {
                            for (i = 0; i < msAddress_len; i++) {
                                pAddress[i] = (unsigned char)msAddress_data[i];
                            }	
                            pAddress[msAddress_len] = 0;
                        }
                        //pAddress = (unsigned char *)midpJcharsToChars(msAddress);
                        pcsl_mem_free(msAddress_data);

                        if (!KNI_IsNullHandle(messageBuffer)) {
                            messageLength = KNI_GetArrayLength(messageBuffer);
                        }
                        if (messageLength >= 0) {
                            if (messageLength > 0) {
                                pMessageBuffer = (unsigned char *)pcsl_mem_malloc(messageLength);
                                memset(pMessageBuffer, 0, messageLength);
                                KNI_GetRawArrayRegion(messageBuffer, 0, messageLength,
                                                      (jbyte *)pMessageBuffer);
                            }

                            trySend = KNI_TRUE;
                        }
                    }
                }
#if ENABLE_REENTRY
            } else { /* Reinvocation after unblocking the thread. */

                if (info->pResult == NULL) {
                    /* waiting for mms_send_completed event */
                    if (info->status == WMA_ERR) {
                        KNI_ThrowNew(midpInterruptedIOException, "Sending SMS");
                    }
                    break;
                }
                messageStateData = info->pResult;
                pMessageBuffer = messageStateData->pMessageBuffer;
                pAddress = messageStateData->pAddress;
                pdContext = messageStateData->pdContext;

                trySend = KNI_TRUE;
            }
#endif

            if (trySend == KNI_TRUE) {
                /* send message. */
                status = jsr120_send_sms((jchar)messageType,
                                         pAddress,
                                         pMessageBuffer,
                                         (jchar)messageLength,
                                         (jchar)sourcePort,
                                         (jchar)destPort,
                                         handle,
                                         &pdContext);

                if (status == WMA_ERR) {
                    KNI_ThrowNew(midpIOException, "Sending SMS");
                    break;
                } 
#if ENABLE_REENTRY
                else if (status == WMA_NET_WOULDBLOCK) {
                    if (messageStateData == NULL) {
                        messageStateData =
                            (jsr120_sms_message_state_data *)pcsl_mem_malloc(
                                sizeof(*messageStateData));
                        messageStateData->pMessageBuffer = pMessageBuffer;
                        messageStateData->pAddress = pAddress;
                    }

                    messageStateData->pdContext = pdContext;

                    /* Block calling Java Thread. */
                    midp_thread_wait(WMA_SMS_WRITE_SIGNAL, handle,
                                     messageStateData);

                    stillWaiting = KNI_TRUE;
                    break;
                } else {
                    /* waiting for sms_send_completed event */
                    midp_thread_wait(WMA_SMS_WRITE_SIGNAL, handle, NULL);
                }
#endif
            }
        } while (0);

        if (!stillWaiting) {
            pcsl_mem_free(pMessageBuffer);
            pcsl_mem_free(pAddress);
        }
    }

    KNI_EndHandles();

    KNI_ReturnInt(0); /* currently ignored. */
}