1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Android JNI interface.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuDefs.hpp"
25#include "tcuApp.hpp"
26#include "tcuAndroidExecService.hpp"
27#include "tcuTestLog.hpp"
28#include "tcuCommandLine.hpp"
29
30#include <string>
31#include <vector>
32#include <cstring>
33
34#include <jni.h>
35#include <stdlib.h>
36#include <android/log.h>
37
38// ExecService entry points.
39
40static jfieldID getExecServiceField (JNIEnv* env, jobject obj)
41{
42	jclass cls = env->GetObjectClass(obj);
43	TCU_CHECK_INTERNAL(cls);
44
45	jfieldID fid = env->GetFieldID(cls, "m_server", "J");
46	TCU_CHECK_INTERNAL(fid);
47
48	return fid;
49}
50
51static tcu::Android::ExecService* getExecService (JNIEnv* env, jobject obj)
52{
53	jfieldID field = getExecServiceField(env, obj);
54	return (tcu::Android::ExecService*)(deIntptr)env->GetLongField(obj, field);
55}
56
57static void setExecService (JNIEnv* env, jobject obj, tcu::Android::ExecService* service)
58{
59	jfieldID field = getExecServiceField(env, obj);
60	env->SetLongField(obj, field, (jlong)(deIntptr)service);
61}
62
63static void logException (const std::exception& e)
64{
65	__android_log_print(ANDROID_LOG_ERROR, "dEQP", "%s", e.what());
66}
67
68DE_BEGIN_EXTERN_C
69
70JNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_startServer (JNIEnv* env, jobject obj, jint port)
71{
72	tcu::Android::ExecService*	service		= DE_NULL;
73	JavaVM*						vm			= DE_NULL;
74
75	try
76	{
77		DE_ASSERT(!getExecService(env, obj));
78
79		env->GetJavaVM(&vm);
80		TCU_CHECK_INTERNAL(vm);
81
82		service = new tcu::Android::ExecService(vm, obj, port);
83		service->start();
84
85		setExecService(env, obj, service);
86	}
87	catch (const std::exception& e)
88	{
89		logException(e);
90		delete service;
91		tcu::die("ExecService.startServer() failed");
92	}
93}
94
95JNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_stopServer (JNIEnv* env, jobject obj)
96{
97	try
98	{
99		tcu::Android::ExecService* service = getExecService(env, obj);
100		TCU_CHECK_INTERNAL(service);
101
102		service->stop();
103		delete service;
104
105		setExecService(env, obj, DE_NULL);
106	}
107	catch (const std::exception& e)
108	{
109		logException(e);
110		tcu::die("ExecService.stopServer() failed");
111	}
112}
113
114DE_END_EXTERN_C
115