JNIEXPORT void JNICALL
Java_com_phidgets_Phidget_disableLogging(JNIEnv *env, jclass cls)
{
	int error;
	if ((error = CPhidget_disableLogging()))
		PH_THROW(error);
}
JNIEXPORT jstring JNICALL
Java_com_phidgets_Dictionary_nativeGetKey(JNIEnv *env, jobject obj, jstring key)
{
    int error;

    char val[1024];

    jboolean iscopy;
    const char *keyString = (*env)->GetStringUTFChars(env, key, &iscopy);

    CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
                                 (*env)->GetLongField(env, obj, dictionary_handle_fid);

    if ((error = CPhidgetDictionary_getKey(h, keyString, val, sizeof(val))))
    {
        PH_THROW(error);
        (*env)->ReleaseStringUTFChars(env, key, keyString);
        return NULL;
    }
    else
    {
        (*env)->ReleaseStringUTFChars(env, key, keyString);
        return (*env)->NewStringUTF(env, val);
    }
}
JNIEXPORT void JNICALL
Java_com_phidgets_ServoPhidget_setServoParameters(JNIEnv *env, jobject obj, jint index, jdouble minUs, jdouble maxUs, jdouble degrees)
{
	CPhidgetServoHandle h = (CPhidgetServoHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
	if ((error = CPhidgetServo_setServoParameters(h, index, minUs, maxUs, degrees)))
		PH_THROW(error);
}
JNIEXPORT void JNICALL
Java_com_phidgets_Phidget_nativeDelete(JNIEnv *env, jobject obj)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	int error;
	
	if ((error = CPhidget_delete(h)))
		PH_THROW(error);
}
JNIEXPORT void JNICALL
Java_com_phidgets_FrequencyCounterPhidget_reset(JNIEnv *env, jobject obj, jint index)
{
	CPhidgetFrequencyCounterHandle h = (CPhidgetFrequencyCounterHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;

	if ((error = CPhidgetFrequencyCounter_reset(h, index)))
		PH_THROW(error);
}
JNIEXPORT jlong JNICALL
Java_com_phidgets_Dictionary_create(JNIEnv *env, jobject obj)
{
    int error;
    CPhidgetDictionaryHandle dict;

    if ((error = CPhidgetDictionary_create(&dict)))
        PH_THROW(error);
    return (jlong)(uintptr_t)dict;
}
JNIEXPORT jlong JNICALL
Java_com_phidgets_Manager_create(JNIEnv *env, jobject obj)
{
	int error;
	CPhidgetManagerHandle phidm;
	
	if ((error = CPhidgetManager_create(&phidm)))
		PH_THROW(error);
	return (jlong)(uintptr_t)phidm; 
}
JNIEXPORT jint JNICALL
Java_com_phidgets_BridgePhidget_getGain(JNIEnv *env, jobject obj, jint index)
{
	CPhidgetBridgeHandle h = (CPhidgetBridgeHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
	CPhidgetBridge_Gain v;
	if ((error = CPhidgetBridge_getGain(h, index, &v)))
		PH_THROW(error);
	return (jint)v;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_FrequencyCounterPhidget_getFilter(JNIEnv *env, jobject obj, jint index)
{
	CPhidgetFrequencyCounterHandle h = (CPhidgetFrequencyCounterHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
	CPhidgetFrequencyCounter_FilterType v;
	if ((error = CPhidgetFrequencyCounter_getFilter(h, index, &v)))
		PH_THROW(error);
	return (jint)v;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_ServoPhidget_getServoType(JNIEnv *env, jobject obj, jint index)
{
	CPhidgetServoHandle h = (CPhidgetServoHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
	CPhidget_ServoType v;
	if ((error = CPhidgetServo_getServoType(h, index, &v)))
		PH_THROW(error);
	return (jint)v;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_TemperatureSensorPhidget_getThermocoupleType(JNIEnv *env, jobject obj, jint index)
{
	CPhidgetTemperatureSensorHandle h = (CPhidgetTemperatureSensorHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
	CPhidgetTemperatureSensor_ThermocoupleType v;
	if ((error = CPhidgetTemperatureSensor_getThermocoupleType(h, index, &v)))
		PH_THROW(error);
	return (jint)v;
}
JNIEXPORT void JNICALL
Java_com_phidgets_Phidget_nativeEnableLogging(JNIEnv *env, jclass cls, jint level, jstring file)
{
	int error;
    jboolean iscopy;
    const char *textString = file ? (*env)->GetStringUTFChars(
		env, file, &iscopy) : NULL;
	if ((error = CPhidget_enableLogging(level, (char *)textString)))
		PH_THROW(error);
	(*env)->ReleaseStringUTFChars(env, file, textString);
}
JNIEXPORT void JNICALL
Java_com_phidgets_Manager_nativeClose(JNIEnv *env, jobject obj)
{
	int error;
	
	CPhidgetManagerHandle h = (CPhidgetManagerHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, manager_handle_fid);

	if ((error = CPhidgetManager_close(h)))
		PH_THROW(error);
}
JNIEXPORT void JNICALL
Java_com_phidgets_Dictionary_nativeDelete(JNIEnv *env, jobject obj)
{
    int error;

    CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
                                 (*env)->GetLongField(env, obj, dictionary_handle_fid);

    if ((error = CPhidgetDictionary_delete(h)))
        PH_THROW(error);
}
JNIEXPORT void JNICALL 
Java_com_phidgets_Phidget_waitForAttachment(JNIEnv *env, jobject obj, jint timeout)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	int error;
	
	if ((error = CPhidget_waitForAttachment(h, timeout)))
		PH_THROW(error);

	return;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_RFIDPhidget_getLastTagProtocol (JNIEnv *env, jobject obj)
{
	CPhidgetRFIDHandle h = (CPhidgetRFIDHandle)(uintptr_t)
	    (*env)->GetLongField( env, obj, handle_fid);
	int error;
	char *tag;
	CPhidgetRFID_Protocol proto;
	if ((error = CPhidgetRFID_getLastTag2(h, &tag, &proto)))
		PH_THROW(error);

	return (jint)proto;
}
JNIEXPORT void JNICALL
Java_com_phidgets_Phidget_setDeviceLabel(JNIEnv *env, jobject obj, jstring v)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);
	int error;
    jboolean iscopy;
    const char *textString = (*env)->GetStringUTFChars(
                env, v, &iscopy);
	if ((error = CPhidget_setDeviceLabel(h, (char *)textString)))
		PH_THROW(error);
	(*env)->ReleaseStringUTFChars(env, v, textString);
}
JNIEXPORT jint JNICALL
Java_com_phidgets_Phidget_getSerialNumber(JNIEnv *env, jobject obj)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	int error;
	int ser;
	
	if ((error = CPhidget_getSerialNumber(h, &ser)))
		PH_THROW(error);

	return ser;
}
JNIEXPORT jboolean JNICALL
Java_com_phidgets_Dictionary_isAttached(JNIEnv *env, jobject obj)
{
    CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
                       obj, dictionary_handle_fid);
    int error;
    int s;

    if ((error = CPhidget_getDeviceStatus(h, &s)))
        PH_THROW(error);

    return s == PHIDGET_ATTACHED;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_Phidget_getDeviceVersion(JNIEnv *env, jobject obj)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	int error;
	int ver;
	
	if ((error = CPhidget_getDeviceVersion(h, &ver)))
		PH_THROW(error);

	return ver;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_Phidget_getDeviceClass(JNIEnv *env, jobject obj)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	int error;
	CPhidget_DeviceClass cls;
	
	if ((error = CPhidget_getDeviceClass(h, &cls)))
		PH_THROW(error);

	return (int)cls;
}
JNIEXPORT jboolean JNICALL
Java_com_phidgets_Manager_isAttachedToServer(JNIEnv *env, jobject obj)
{
	CPhidgetManagerHandle h = (CPhidgetManagerHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, manager_handle_fid);
	int error;
	int s;
	
	if ((error = CPhidgetManager_getServerStatus(h, &s)))
		PH_THROW(error);

	return s == PHIDGET_ATTACHED;
}
JNIEXPORT jstring JNICALL
Java_com_phidgets_Phidget_getDeviceLabel(JNIEnv *env, jobject obj)
{
	CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, handle_fid);
	const char *label;
	int error;
	
	if ((error = CPhidget_getDeviceLabel(h, &label)))
		PH_THROW(error);

	return (*env)->NewStringUTF(env, label);
}
JNIEXPORT jstring JNICALL
Java_com_phidgets_Manager_getServerID(JNIEnv *env, jobject obj)
{
	CPhidgetManagerHandle h = (CPhidgetManagerHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, manager_handle_fid);
	int error;
	const char *id;

	if ((error = CPhidgetManager_getServerID(h, &id)))
		PH_THROW(error);

	return (*env)->NewStringUTF(env, id);
}
JNIEXPORT jstring JNICALL
Java_com_phidgets_Dictionary_getServerAddress(JNIEnv *env, jobject obj)
{
    CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
                                 obj, dictionary_handle_fid);
    int error;
    int port;
    const char *addr;

    if ((error = CPhidgetDictionary_getServerAddress(h, &addr, &port)))
        PH_THROW(error);

    return (*env)->NewStringUTF(env, addr);
}
JNIEXPORT jint JNICALL
Java_com_phidgets_Manager_getServerPort(JNIEnv *env, jobject obj)
{
	CPhidgetManagerHandle h = (CPhidgetManagerHandle)(uintptr_t)(*env)->GetLongField(env,
	    obj, manager_handle_fid);
	int error;
	int port;
	const char *addr;

	if ((error = CPhidgetManager_getServerAddress(h, &addr, &port)))
		PH_THROW(error);

	return port;
}
JNIEXPORT void JNICALL
Java_com_phidgets_Phidget_nativeLog(JNIEnv *env, jclass cls, jint level, jstring id, jstring log)
{
	int error;
    jboolean iscopy1;
    const char *textString1 = (*env)->GetStringUTFChars(
                env, id, &iscopy1);
    jboolean iscopy2;
    const char *textString2 = (*env)->GetStringUTFChars(
                env, log, &iscopy2);
	if ((error = CPhidget_log(level, (char *)textString1, (char *)textString2)))
		PH_THROW(error);
	(*env)->ReleaseStringUTFChars(env, id, textString1);
	(*env)->ReleaseStringUTFChars(env, log, textString2);
}
JNIEXPORT jstring JNICALL
Java_com_phidgets_RFIDPhidget_getLastTag (JNIEnv *env, jobject obj)
{
	CPhidgetRFIDHandle h = (CPhidgetRFIDHandle)(uintptr_t)
	    (*env)->GetLongField( env, obj, handle_fid);
	int error;
	char *tag;
	jstring jb;
	CPhidgetRFID_Protocol proto;
	if ((error = CPhidgetRFID_getLastTag2(h, &tag, &proto)))
		PH_THROW(error);

	jb=(*env)->NewStringUTF(env, tag);
	return jb;
}
JNIEXPORT void JNICALL
Java_com_phidgets_Dictionary_nativeRemoveKey(JNIEnv *env, jobject obj, jstring pattern)
{
    int error;

    jboolean iscopy;
    const char *patternString = (*env)->GetStringUTFChars(
                                    env, pattern, &iscopy);

    CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
                                 (*env)->GetLongField(env, obj, dictionary_handle_fid);

    if ((error = CPhidgetDictionary_removeKey(h, patternString)))
        PH_THROW(error);

    (*env)->ReleaseStringUTFChars(env, pattern, patternString);
}
JNIEXPORT void JNICALL Java_com_phidgets_RFIDPhidget_write
  (JNIEnv *env, jobject obj, jstring tag, jint proto, jboolean lock)
{
	int error;
	
    jboolean iscopy;
    const char *tagString = (*env)->GetStringUTFChars(
                env, tag, &iscopy);

	CPhidgetRFIDHandle h = (CPhidgetRFIDHandle)(uintptr_t)
	    (*env)->GetLongField(env, obj, handle_fid);

	if ((error = CPhidgetRFID_write(h, (char *)tagString, (CPhidgetRFID_Protocol)proto, (int)lock)))
		PH_THROW(error);

	(*env)->ReleaseStringUTFChars(env, tag, tagString);
}