/*
 * HA_clientinterface.cpp
 *
 */
#include "HA_ClientControlObject.h"
#include "HA_clientinterface.h"
namespace HARKAndroid {
ClientControlObject *HARKClientControl;
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACCreate(JNIEnv *env, jobject thiz)
{
	if (HARKAndroid::HARKClientControl == NULL) {
		HARKAndroid::HARKClientControl = new HARKAndroid::ClientControlObject;
		HARKAndroid::HARKClientControl->Create(env, thiz);
	}
	return(0);
}
#else
int HACCreate()
{
	if (HARKAndroid::HARKClientControl == NULL) {
		HARKAndroid::HARKClientControl = new HARKAndroid::ClientControlObject;
        HARKAndroid::HARKClientControl->Create();
	}
	return(0);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACStart(JNIEnv *env, jobject thiz)
{
	HARKAndroid::HARKClientControl->Start(env, thiz);
	return(0);
}
#else
int HACStart()
{
	HARKAndroid::HARKClientControl->Start();
	return(0);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACStop(JNIEnv *env, jobject thiz)
{
	int ret = HARKAndroid::HARKClientControl->Stop(env, thiz);
	return(ret);
}
#else
int HACStop()
{
	int ret = HARKAndroid::HARKClientControl->Stop();
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jboolean HACisStop(JNIEnv *env, jobject thiz)
{
	jboolean ret = HARKAndroid::HARKClientControl->isStop();
	return(ret);
}
#else
bool HACisStop()
{
	bool ret = HARKAndroid::HARKClientControl->isStop();
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortA(JNIEnv *env, jobject thiz)
#else
int HACConnectPortA()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortA();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortA(JNIEnv *env, jobject thiz, jint port, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortA(port, ar);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortA(JNIEnv *env, jobject thiz, jint port, jintArray channel, jintArray length, jintArray advance, jdoubleArray mic_acoustics)
{
	jboolean b;
	jdouble *acoustics = env->GetDoubleArrayElements(mic_acoustics, &b);
	jint *adv = env->GetIntArrayElements(advance, &b);
	jint *len = env->GetIntArrayElements(length, &b);
	jint *ch  = env->GetIntArrayElements(channel, &b);
	//ar_num[0] = env->GetArrayLength(array);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortA(port, ch, len, adv, acoustics);
	env->ReleaseIntArrayElements(channel, ch, 0);
	env->ReleaseIntArrayElements(length, len, 0);
	env->ReleaseIntArrayElements(advance, adv, 0);
	env->ReleaseDoubleArrayElements(mic_acoustics, acoustics, 0);
	return(ret);
}
#else
int HACRecvFromPortA(int port, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortA(port, array);
	return(ret);
}
int HACRecvFromPortA(int port, int *channel, int *length, int *advance, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortA(port, channel, length, advance, array);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortA(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortA(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortA(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortB(JNIEnv *env, jobject thiz)
#else
int HACConnectPortB()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortB();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortB(JNIEnv *env, jobject thiz, jint port, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortB(port, ar);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortB(JNIEnv *env, jobject thiz, jint port, jintArray channel, jintArray length, jintArray advance, jintArray fft_length, jdoubleArray mic_spectra)
{
	jboolean b;
	jdouble *spectra = env->GetDoubleArrayElements(mic_spectra, &b);
	jint *fft_len = env->GetIntArrayElements(fft_length, &b);
	jint *adv = env->GetIntArrayElements(advance, &b);
	jint *len = env->GetIntArrayElements(length, &b);
	jint *ch = env->GetIntArrayElements(channel, &b);
	//ar_num[0] = env->GetArrayLength(array);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortB(port, ch, len, adv, fft_len, spectra);
	env->ReleaseIntArrayElements(channel, ch, 0);
	env->ReleaseIntArrayElements(length, len, 0);
	env->ReleaseIntArrayElements(advance, adv, 0);
	env->ReleaseIntArrayElements(fft_length, fft_len, 0);
	env->ReleaseDoubleArrayElements(mic_spectra, spectra, 0);
	return(ret);
}
#else
int HACRecvFromPortB(int port, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortB(port, array);
	return(ret);
}
int HACRecvFromPortB(int port, int *channel, int *length, int *advance, int *fft_length, float *array)
{
  int ret = HARKAndroid::HARKClientControl->RecvFromPortB(port, channel, length, advance, fft_length, array);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortB(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortB(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortB(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortC(JNIEnv *env, jobject thiz)
#else
int HACConnectPortC()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortC();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortC(JNIEnv *env, jobject thiz, jint port, jintArray array_num, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	jint *ar_num = env->GetIntArrayElements(array_num, &b);
	ar_num[0] = env->GetArrayLength(array);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortC(port, ar_num, ar);
	env->ReleaseIntArrayElements(array_num, ar_num, 0);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortC(JNIEnv *env, jobject thiz, jint port, jintArray source_count, jintArray source_ids, jdoubleArray source_coordinates)
{
	jboolean b;
	jdouble *coordinates = env->GetDoubleArrayElements(source_coordinates, &b);
	jint *ids = env->GetIntArrayElements(source_ids, &b);
	jint *count = env->GetIntArrayElements(source_count, &b);
	count[0] = env->GetArrayLength(source_coordinates);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortC(port, count, ids, coordinates);
	env->ReleaseIntArrayElements(source_count, count, 0);
	env->ReleaseIntArrayElements(source_ids, ids, 0);
	env->ReleaseDoubleArrayElements(source_coordinates, coordinates, 0);
	return(ret);
}
#else
int HACRecvFromPortC(int port, int *array_num, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortC(port, array_num, array);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortC(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortC(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortC(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortD(JNIEnv *env, jobject thiz)
#else
int HACConnectPortD()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortD();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortD(JNIEnv *env, jobject thiz, jint port, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortD(port, ar);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortD(JNIEnv *env, jobject thiz, jint port, jintArray source_count, jintArray fft_length, jintArray source_ids, jdoubleArray source_coordinates, jdoubleArray source_spectra)
{
	jboolean b;
	jdouble *spectra = env->GetDoubleArrayElements(source_spectra, &b);
	jdouble *coordinates = env->GetDoubleArrayElements(source_coordinates, &b);
	jint *ids = env->GetIntArrayElements(source_ids, &b);
	jint *length = env->GetIntArrayElements(fft_length, &b);
	jint *count = env->GetIntArrayElements(source_count, &b);
	count[0] = env->GetArrayLength(source_coordinates);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortD(port, count, length, ids, coordinates, spectra);
	env->ReleaseIntArrayElements(source_count, count, 0);
	env->ReleaseIntArrayElements(fft_length, length, 0);
	env->ReleaseIntArrayElements(source_ids, ids, 0);
	env->ReleaseDoubleArrayElements(source_coordinates, coordinates, 0);
	env->ReleaseDoubleArrayElements(source_spectra, spectra, 0);
	return(ret);
}
#else
int HACRecvFromPortD(int port, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortD(port, array);
	return(ret);
}

int HACRecvFromPortD(const int port, int *source_count, int *fft_length, int *source_ids, float *source_coordinates, float *source_spectra)
{
        int ret = HARKAndroid::HARKClientControl->RecvFromPortD(port, source_count, fft_length, source_ids, source_coordinates, source_spectra);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortD(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortD(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortD(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortE(JNIEnv *env, jobject thiz)
#else
int HACConnectPortE()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortE();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortE(JNIEnv *env, jobject thiz, jint port, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortE(port, ar);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortE(JNIEnv *env, jobject thiz, jint port, jintArray source_count, jintArray banks, jintArray source_ids, jdoubleArray source_coordinates, jdoubleArray source_features)
{
	jboolean b;
	jdouble *features = env->GetDoubleArrayElements(source_features, &b);
	jdouble *coordinates = env->GetDoubleArrayElements(source_coordinates, &b);
	jint *ids = env->GetIntArrayElements(source_ids, &b);
	jint *bank = env->GetIntArrayElements(banks, &b);
	jint *count = env->GetIntArrayElements(source_count, &b);
	count[0] = env->GetArrayLength(source_coordinates);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortE(port, count, bank, ids, coordinates, features);
	env->ReleaseIntArrayElements(source_count, count, 0);
	env->ReleaseIntArrayElements(banks, bank, 0);
	env->ReleaseIntArrayElements(source_ids, ids, 0);
	env->ReleaseDoubleArrayElements(source_coordinates, coordinates, 0);
	env->ReleaseDoubleArrayElements(source_features, features, 0);
	return(ret);
}
#else
int HACRecvFromPortE(int port, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortE(port, array);
	return(ret);
}
int HACRecvFromPortE(int port, int *source_count, int *banks, int *source_ids, float *source_coordinates, float *features)
{
        int ret = HARKAndroid::HARKClientControl->RecvFromPortE(port, source_count, banks, source_ids, source_coordinates, features);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortE(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortE(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortE(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortF(JNIEnv *env, jobject thiz)
#else
int HACConnectPortF()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortF();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortF(JNIEnv *env, jobject thiz, jint port, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortF(port, ar);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortF(JNIEnv *env, jobject thiz, jint port, jintArray source_count, jintArray advance, jintArray source_ids, jdoubleArray source_coordinates, jdoubleArray source_acoustics)
{
	jboolean b;
	jdouble *acoustics = env->GetDoubleArrayElements(source_acoustics, &b);
	jdouble *coordinates = env->GetDoubleArrayElements(source_coordinates, &b);
	jint *ids = env->GetIntArrayElements(source_ids, &b);
	jint *adv = env->GetIntArrayElements(advance, &b);
	jint *count = env->GetIntArrayElements(source_count, &b);
	count[0] = env->GetArrayLength(source_coordinates);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortF(port, count, adv, ids, coordinates, acoustics);
	env->ReleaseIntArrayElements(source_count, count, 0);
	env->ReleaseIntArrayElements(advance, adv, 0);
	env->ReleaseIntArrayElements(source_ids, ids, 0);
	env->ReleaseDoubleArrayElements(source_coordinates, coordinates, 0);
	env->ReleaseDoubleArrayElements(source_acoustics, acoustics, 0);
	return(ret);
}
#else
int HACRecvFromPortF(int port, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortF(port, array);
	return(ret);
}
int HACRecvFromPortF(int port, int *source_count, int *advance, int *source_ids, float *source_coordinates, float *acoustics)
{
        int ret = HARKAndroid::HARKClientControl->RecvFromPortF(port, source_count, advance, source_ids, source_coordinates, acoustics);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortF(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortF(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortF(port);
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACConnectPortFwithSrc(JNIEnv *env, jobject thiz)
#else
int HACConnectPortFwithSrc()
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ConnectPortFwithSrc();
	return(ret);
}

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACRecvFromPortFwithSrc(JNIEnv *env, jobject thiz, jint port, jintArray array_src, jdoubleArray array)
{
	jboolean b;
	jdouble *ar = env->GetDoubleArrayElements(array, &b);
	jint *ar_src = env->GetIntArrayElements(array_src, &b);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortFwithSrc(port, ar_src, ar);
	env->ReleaseIntArrayElements(array_src, ar_src, 0);
	env->ReleaseDoubleArrayElements(array, ar, 0);
	return(ret);
}
jint HACRecvFromPortFwithSrc(JNIEnv *env, jobject thiz, jint port, jintArray source_count, jintArray advance, jintArray source_ids, jdoubleArray source_coordinates, jdoubleArray source_acoustics)
{
	jboolean b;
	jdouble *acoustics = env->GetDoubleArrayElements(source_acoustics, &b);
	jdouble *coordinates = env->GetDoubleArrayElements(source_coordinates, &b);
	jint *ids = env->GetIntArrayElements(source_ids, &b);
	jint *adv = env->GetIntArrayElements(advance, &b);
	jint *count = env->GetIntArrayElements(source_count, &b);
	count[0] = env->GetArrayLength(source_coordinates);
	int ret = HARKAndroid::HARKClientControl->RecvFromPortFwithSrc(port, count, adv, ids, coordinates, acoustics);
	env->ReleaseIntArrayElements(source_count, count, 0);
	env->ReleaseIntArrayElements(advance, adv, 0);
	env->ReleaseIntArrayElements(source_ids, ids, 0);
	env->ReleaseDoubleArrayElements(source_coordinates, coordinates, 0);
	env->ReleaseDoubleArrayElements(source_acoustics, acoustics, 0);
	return(ret);
}
#else
int HACRecvFromPortFwithSrc(int port, int *array_src, float *array)
{
	int ret = HARKAndroid::HARKClientControl->RecvFromPortFwithSrc(port, array_src, array);
	return(ret);
}
int HACRecvFromPortFwithSrc(int port, int *source_count, int *advance, int *source_ids, float *source_coordinates, float *acoustics)
{
        int ret = HARKAndroid::HARKClientControl->RecvFromPortFwithSrc(port, source_count, advance, source_ids, source_coordinates, acoustics);
	return(ret);
}
#endif // ENABLE_JNI_FOR_ANDROID

#ifdef ENABLE_JNI_FOR_ANDROID
jint HACClosePortFwithSrc(JNIEnv *env, jobject thiz, jint port)
#else
int HACClosePortFwithSrc(int port)
#endif // ENABLE_JNI_FOR_ANDROID
{
	int ret = HARKAndroid::HARKClientControl->ClosePortFwithSrc(port);
	return(ret);
}
