1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "simulator_resource_model_jni.h"
22 #include "simulator_device_info_jni.h"
23 #include "simulator_platform_info_jni.h"
24 #include "simulator_exceptions_jni.h"
25 #include "simulator_resource_utils_jni.h"
26 #include "simulator_utils_jni.h"
27 #include "jni_sharedobject_holder.h"
28 #include "jni_listener_holder.h"
29 #include "jni_string.h"
31 #include "simulator_manager.h"
33 class JNILogger : public ILogger
36 void setJavaLogger(JNIEnv *env, jobject logger)
38 m_logger = env->NewWeakGlobalRef(logger);
41 void write(std::string time, ILogger::Level level, std::string message)
43 JNIEnv *env = GetEnv();
47 jobject logger = env->NewLocalRef(m_logger);
54 jclass loggerCls = env->GetObjectClass(logger);
61 jmethodID writeMId = env->GetMethodID(loggerCls, "write",
62 "(Ljava/lang/String;ILjava/lang/String;)V");
69 jstring msg = env->NewStringUTF(message.c_str());
70 jstring timeStr = env->NewStringUTF(time.c_str());
71 env->CallVoidMethod(logger, writeMId, timeStr, static_cast<jint>(level), msg);
72 env->DeleteLocalRef(msg);
73 env->DeleteLocalRef(timeStr);
81 static void onResourceFound(jobject listener, SimulatorRemoteResourceSP remoteResource)
83 JNIEnv *env = GetEnv();
87 jclass listenerCls = env->GetObjectClass(listener);
88 jmethodID callbackMethod = env->GetMethodID(listenerCls, "onResourceFound",
89 "(Lorg/oic/simulator/client/SimulatorRemoteResource;)V");
91 jobject resource = CreateSimulatorRemoteResource(env, remoteResource);
95 env->CallVoidMethod(listener, callbackMethod, resource);
99 static void onDeviceInfoReceived(jobject listener, const std::string &hostUri,
100 DeviceInfo &deviceInfo)
102 JNIEnv *env = GetEnv();
106 jclass listenerCls = env->GetObjectClass(listener);
107 jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onDeviceFound",
108 "(Ljava/lang/String;Lorg/oic/simulator/DeviceInfo;)V");
111 jstring jHostUri = env->NewStringUTF(hostUri.c_str());
112 jobject jDeviceInfo = JniDeviceInfo(env).toJava(deviceInfo);
119 env->CallVoidMethod(listener, listenerMethodId, jHostUri, jDeviceInfo);
120 if (env->ExceptionCheck())
129 static void onPlatformInfoReceived(jobject listener, const std::string &hostUri,
130 PlatformInfo &platformInfo)
132 JNIEnv *env = GetEnv();
136 jclass listenerCls = env->GetObjectClass(listener);
137 jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onPlatformFound",
138 "(Ljava/lang/String;Lorg/oic/simulator/PlatformInfo;)V");
140 jstring jHostUri = env->NewStringUTF(hostUri.c_str());
141 jobject jPlatformInfo = JniPlatformInfo(env).toJava(platformInfo);
148 env->CallVoidMethod(listener, listenerMethodId, jHostUri, jPlatformInfo);
149 if (env->ExceptionCheck())
162 JNIEXPORT jobject JNICALL
163 Java_org_oic_simulator_SimulatorManager_nativeCreateResource
164 (JNIEnv *env, jobject /*object*/, jstring jConfigPath)
166 VALIDATE_INPUT_RET(env, !jConfigPath, "Path is null!", nullptr)
170 JniString jniPath(env, jConfigPath);
171 SimulatorResourceSP resource = SimulatorManager::getInstance()->createResource(
173 return CreateSimulatorResource(env, resource);
175 catch (InvalidArgsException &e)
177 ThrowInvalidArgsException(env, e.code(), e.what());
179 catch (SimulatorException &e)
181 ThrowSimulatorException(env, e.code(), e.what());
187 JNIEXPORT jobject JNICALL
188 Java_org_oic_simulator_SimulatorManager_nativeCreateResources
189 (JNIEnv *env, jobject /*object*/, jstring jConfigPath, jint jCount)
191 VALIDATE_INPUT_RET(env, !jConfigPath, "Path is null!", nullptr)
192 VALIDATE_INPUT_RET(env, !jConfigPath || jCount < 0, "Invalid count value!", nullptr)
196 JniString jniPath(env, jConfigPath);
197 std::vector<SimulatorResourceSP> resources =
198 SimulatorManager::getInstance()->createResource(jniPath.get(), jCount);
199 return CreateSimulatorResourceVector(env, resources);
201 catch (InvalidArgsException &e)
203 ThrowInvalidArgsException(env, e.code(), e.what());
205 catch (SimulatorException &e)
207 ThrowSimulatorException(env, e.code(), e.what());
213 JNIEXPORT jobject JNICALL
214 Java_org_oic_simulator_SimulatorManager_nativeCreateSingleResource
215 (JNIEnv *env, jobject /*object*/, jstring jName, jstring jUri, jstring jResourceType)
217 VALIDATE_INPUT_RET(env, !jName, "Name is null!", nullptr)
218 VALIDATE_INPUT_RET(env, !jUri, "URI is null!", nullptr)
219 VALIDATE_INPUT_RET(env, !jResourceType, "Resource type is null!", nullptr)
223 JniString jniName(env, jName);
224 JniString jniUri(env, jUri);
225 JniString jniResourceType(env, jResourceType);
227 SimulatorSingleResourceSP resource = SimulatorManager::getInstance()->createSingleResource(
228 jniName.get(), jniUri.get(), jniResourceType.get());
229 return CreateSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
231 catch (InvalidArgsException &e)
233 ThrowInvalidArgsException(env, e.code(), e.what());
235 catch (SimulatorException &e)
237 ThrowSimulatorException(env, e.code(), e.what());
243 JNIEXPORT jobject JNICALL
244 Java_org_oic_simulator_SimulatorManager_nativeCreateCollectionResource
245 (JNIEnv *env, jobject /*object*/, jstring jName, jstring jUri, jstring jResourceType)
247 VALIDATE_INPUT_RET(env, !jName, "Name is null!", nullptr)
248 VALIDATE_INPUT_RET(env, !jUri, "URI is null!", nullptr)
249 VALIDATE_INPUT_RET(env, !jResourceType, "Resource type is null!", nullptr)
253 JniString jniName(env, jName);
254 JniString jniUri(env, jUri);
255 JniString jniResourceType(env, jResourceType);
257 SimulatorCollectionResourceSP resource = SimulatorManager::getInstance()->createCollectionResource(
258 jniName.get(), jniUri.get(), jniResourceType.get());
259 return CreateSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
261 catch (InvalidArgsException &e)
263 ThrowInvalidArgsException(env, e.code(), e.what());
265 catch (SimulatorException &e)
267 ThrowSimulatorException(env, e.code(), e.what());
273 JNIEXPORT void JNICALL
274 Java_org_oic_simulator_SimulatorManager_nativeSearchResource
275 (JNIEnv *env, jobject /*object*/, jstring jResourceType, jobject jListener)
277 VALIDATE_CALLBACK(env, jListener)
279 ResourceFindCallback callback = std::bind([](
280 std::shared_ptr<SimulatorRemoteResource> resource,
281 const std::shared_ptr<JniListenerHolder> &listenerRef)
283 onResourceFound(listenerRef->get(), resource);
284 }, std::placeholders::_1, JniListenerHolder::create(env, jListener));
290 SimulatorManager::getInstance()->findResource(callback);
294 JniString type(env, jResourceType);
295 SimulatorManager::getInstance()->findResource(type.get(), callback);
299 catch (InvalidArgsException &e)
301 ThrowInvalidArgsException(env, e.code(), e.what());
303 catch (SimulatorException &e)
305 ThrowSimulatorException(env, e.code(), e.what());
309 JNIEXPORT void JNICALL
310 Java_org_oic_simulator_SimulatorManager_nativeSetDeviceInfo
311 (JNIEnv *env, jobject /*object*/, jstring jDeviceName)
313 VALIDATE_INPUT(env, !jDeviceName, "Device name is null!")
317 JniString jniDeviceName(env, jDeviceName);
318 SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get());
320 catch (InvalidArgsException &e)
322 ThrowInvalidArgsException(env, e.code(), e.what());
324 catch (SimulatorException &e)
326 ThrowSimulatorException(env, e.code(), e.what());
330 JNIEXPORT void JNICALL
331 Java_org_oic_simulator_SimulatorManager_nativeSetDeviceInfo2
332 (JNIEnv *env, jobject /*object*/, jstring jDeviceName, jstring jProtocolIndependentID)
334 VALIDATE_INPUT(env, !jDeviceName, "Device name is null!")
338 JniString jniDeviceName(env, jDeviceName);
339 JniString jniProtocolIndependentID(env, jProtocolIndependentID);
340 SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get(), jniProtocolIndependentID.get());
342 catch (InvalidArgsException &e)
344 ThrowInvalidArgsException(env, e.code(), e.what());
346 catch (SimulatorException &e)
348 ThrowSimulatorException(env, e.code(), e.what());
352 JNIEXPORT void JNICALL
353 Java_org_oic_simulator_SimulatorManager_nativeFindDevices
354 (JNIEnv *env, jobject /*object*/, jstring jHostUri, jobject jListener)
356 VALIDATE_CALLBACK(env, jListener)
358 DeviceInfoCallback callback = std::bind([](const std::string & host, DeviceInfo & deviceInfo,
359 const std::shared_ptr<JniListenerHolder> &listenerRef)
361 onDeviceInfoReceived(listenerRef->get(), host, deviceInfo);
362 }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, jListener));
366 JniString jniHostUri(env, jHostUri);
367 SimulatorManager::getInstance()->getDeviceInfo(jniHostUri.get(), callback);
369 catch (InvalidArgsException &e)
371 ThrowInvalidArgsException(env, e.code(), e.what());
373 catch (SimulatorException &e)
375 ThrowSimulatorException(env, e.code(), e.what());
379 JNIEXPORT void JNICALL
380 Java_org_oic_simulator_SimulatorManager_nativeSetPlatformInfo
381 (JNIEnv *env, jobject /*object*/, jobject jPlatformInfo)
383 VALIDATE_INPUT(env, !jPlatformInfo, "Platform info is null!")
387 PlatformInfo info = JniPlatformInfo(env).toCpp(jPlatformInfo);
388 SimulatorManager::getInstance()->setPlatformInfo(info);
390 catch (SimulatorException &e)
392 ThrowSimulatorException(env, e.code(), e.what());
396 JNIEXPORT void JNICALL
397 Java_org_oic_simulator_SimulatorManager_nativeGetPlatformInformation
398 (JNIEnv *env, jobject /*object*/, jstring jHostUri, jobject jListener)
400 VALIDATE_CALLBACK(env, jListener)
402 PlatformInfoCallback callback = std::bind([](const std::string & host,
403 PlatformInfo & platformInfo,
404 const std::shared_ptr<JniListenerHolder> &listenerRef)
406 onPlatformInfoReceived(listenerRef->get(), host, platformInfo);
407 }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, jListener));
411 JniString jniHostUri(env, jHostUri);
412 SimulatorManager::getInstance()->getPlatformInfo(jniHostUri.get(), callback);
414 catch (InvalidArgsException &e)
416 ThrowInvalidArgsException(env, e.code(), e.what());
418 catch (SimulatorException &e)
420 ThrowSimulatorException(env, e.code(), e.what());
424 JNIEXPORT void JNICALL
425 Java_org_oic_simulator_SimulatorManager_nativeSetLogger
426 (JNIEnv *env, jobject /*object*/, jobject jLogger)
428 static std::shared_ptr<JNILogger> target(new JNILogger());
429 target->setJavaLogger(env, jLogger);
430 SimulatorManager::getInstance()->setLogger(target);