JNIEXPORT void JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_deleteResource (JNIEnv *env, jclass object, jobject jResource) { if (!jResource) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "No resource has been passed!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, jResource); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Simulator resource not found!"); return; } try { SimulatorManager::getInstance()->deleteResource(resource); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); } }
JNIEXPORT void JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_getPlatformInfo (JNIEnv *env, jobject interfaceObject, jobject jListener) { if (!jListener) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Invalid callback!"); return; } JniPlatformInfoListener *platformInfoListener = new JniPlatformInfoListener(env, jListener); PlatformInfoCallback callback = std::bind([platformInfoListener](PlatformInfo & platformInfo) { platformInfoListener->onPlatformInfoReceived(platformInfo); delete platformInfoListener; }, std::placeholders::_1); try { SimulatorManager::getInstance()->getPlatformInfo(callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return; } }
JNIEXPORT void JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_setDeviceInfo (JNIEnv *env, jobject interfaceObject, jstring deviceInfo) { if (!deviceInfo) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid device info!"); return; } const char *deviceName = env->GetStringUTFChars(deviceInfo, NULL); try { SimulatorManager::getInstance()->setDeviceInfo(deviceName); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return; } env->ReleaseStringUTFChars(deviceInfo, deviceName); }
JNIEXPORT void JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_findResource (JNIEnv *env, jobject object, jstring jResourceType, jobject jListener) { if (!jListener) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Invalid callback!"); return; } const char *typeCStr = NULL; std::string resourceType; if (jResourceType) { typeCStr = env->GetStringUTFChars(jResourceType, NULL); resourceType = typeCStr; } JNIFoundResourceListener *resourceListener = new JNIFoundResourceListener(); resourceListener->setJavaFoundResourceListener(env, jListener); try { if (!jResourceType) { SimulatorManager::getInstance()->findResource( std::bind(&JNIFoundResourceListener::onFoundResource, resourceListener, std::placeholders::_1)); } else { SimulatorManager::getInstance()->findResource(resourceType, std::bind(&JNIFoundResourceListener::onFoundResource, resourceListener, std::placeholders::_1)); } } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); return; } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); return; } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return; } if (typeCStr) env->ReleaseStringUTFChars(jResourceType, typeCStr); }
JNIEXPORT jint JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation (JNIEnv *env, jobject object, jint automationType, jobject listener) { SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return SIMULATOR_BAD_OBJECT; } if (!listener) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Start Resource Automation failed! Callback not set"); return SIMULATOR_INVALID_CALLBACK; } jweak jlistenerRef = env->NewWeakGlobalRef(listener); updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID) { onAutomationComplete(jlistenerRef, uri, automationID); }; AutomationType type = AutomationType::NORMAL; if (1 == automationType) { type = AutomationType::RECURRENT; } int automationId = -1; try { automationId = resource->startUpdateAutomation(type, callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); } return automationId; }
JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange (JNIEnv *env, jobject object, jstring attrName, jint min, jint max) { if (!attrName) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL); if (!attrNameCStr) { throwSimulatorException(env, SIMULATOR_ERROR, "String error!"); return; } resource->setRange(attrNameCStr, static_cast<int>(min), static_cast<int>(max)); env->ReleaseStringUTFChars(attrName, attrNameCStr); }
JNIEXPORT void JNICALL Java_org_oic_simulator_client_SimulatorRemoteResource_startObserve (JNIEnv *env, jobject object, jobject queryParamsMap, jobject listener) { VALIDATE_CALLBACK(env, listener) SimulatorRemoteResourceSP resource = SimulatorRemoteResourceToCpp(env, object); VALIDATE_OBJECT(env, resource) try { std::map<std::string, std::string> queryParams = JniQueryParameter(env).toCpp(queryParamsMap); SimulatorRemoteResource::ObserveNotificationCallback callback = std::bind([]( const std::string & uid, const int errorCode, SimulatorResourceModelSP representation, const int seq, const std::shared_ptr<JniListenerHolder> &listenerRef) { onObserveCallback(listenerRef->get(), uid, errorCode, representation, seq); }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, JniListenerHolder::create(env, listener)); resource->observe(ObserveType::OBSERVE, callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } }
JNIEXPORT jint JNICALL Java_org_oic_simulator_server_SimulatorSingleResource_startResourceUpdation (JNIEnv *env, jobject object, jobject type, jint interval, jobject listener) { VALIDATE_CALLBACK_RET(env, listener, -1) SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object); VALIDATE_OBJECT_RET(env, singleResource, -1) jobject listenerRef = env->NewGlobalRef(listener); updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id) { onAutoUpdationComplete(listenerRef, uri, id); }; try { AutomationType automationType = AutomationTypeToCpp(env, type); int id = singleResource->startResourceUpdation(automationType, interval, callback); return id; } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } return -1; }
JNIEXPORT void JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_setPlatformInfo (JNIEnv *env, jobject interfaceObject, jobject jPlatformInfo) { if (!jPlatformInfo) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid platform info!"); return; } JPlatformInfo jniPlatformInfo(env); try { PlatformInfo platformInfo = jniPlatformInfo.toCPP(jPlatformInfo); SimulatorManager::getInstance()->setPlatformInfo(platformInfo); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return; } }
JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesString (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues) { if (!jKey || jAllowedValues) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } const char *keyCStr = env->GetStringUTFChars(jKey, NULL); if (!keyCStr) { throwSimulatorException(env, SIMULATOR_ERROR, "String error!"); return; } resource->setAllowedValues(keyCStr, convertStringVector(env, jAllowedValues)); env->ReleaseStringUTFChars(jKey, keyCStr); }
JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeString (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue) { if (!jKey) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, jobject); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } std::string key = env->GetStringUTFChars(jKey, NULL); std::string value = env->GetStringUTFChars(jValue, NULL); SimulatorResourceModel::Attribute att; att.setName(key); att.setValue(value); resource->addAttribute(att); }
JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback (JNIEnv *env, jobject object, jobject jcallback) { if (!jcallback) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set"); return; } jweak jlistenerRef = env->NewWeakGlobalRef(jcallback); SimulatorResourceServer::ObserverCB callback = [jlistenerRef](const std::string & uri, ObservationStatus state, const ObserverInfo & observerInfo) { onObserverChange(jlistenerRef, uri, state, observerInfo); }; SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } resource->setObserverCallback(callback); }
JNIEXPORT jint JNICALL Java_org_oic_simulator_client_SimulatorRemoteResource_startVerification (JNIEnv *env, jobject object, jint reqType, jobject listener) { VALIDATE_CALLBACK_RET(env, listener, -1) SimulatorRemoteResourceSP resource = SimulatorRemoteResourceToCpp(env, object); VALIDATE_OBJECT_RET(env, resource, -1) // Convert RequestType RequestType type; switch (reqType) { case 0: type = RequestType::RQ_TYPE_GET; break; case 1: type = RequestType::RQ_TYPE_PUT; break; case 2: type = RequestType::RQ_TYPE_POST; break; case 3: type = RequestType::RQ_TYPE_DELETE; break; default: return -1; } try { SimulatorRemoteResource::StateCallback callback = std::bind([]( const std::string & uid, int id, OperationState opState, const std::shared_ptr<JniListenerHolder> &listenerRef) { onVerificationCallback(listenerRef->get(), uid, id, opState); }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, JniListenerHolder::create(env, listener)); return resource->startVerification(type, callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (NoSupportException &e) { throwNoSupportException(env, e.what()); } catch (OperationInProgressException &e) { throwOperationInProgressException(env, e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } return -1; }
JNIEXPORT void JNICALL Java_org_oic_simulator_client_SimulatorRemoteResource_nativePost (JNIEnv *env, jobject object, jstring resInterface, jobject queryParamsMap, jobject representation, jobject listener) { VALIDATE_CALLBACK(env, listener) SimulatorRemoteResourceSP resource = SimulatorRemoteResourceToCpp(env, object); VALIDATE_OBJECT(env, resource) try { JniString jniInterface(env, resInterface); std::map<std::string, std::string> queryParams = JniQueryParameter(env).toCpp(queryParamsMap); SimulatorResourceModel resModel; simulatorResourceModelToCpp(env, representation, resModel); SimulatorResourceModelSP resModelSP(new SimulatorResourceModel(resModel)); SimulatorRemoteResource::ResponseCallback callback = std::bind([]( const std::string & uid, int errorCode, SimulatorResourceModelSP representation, const std::shared_ptr<JniListenerHolder> &listenerRef) { onPostCallback(listenerRef->get(), uid, errorCode, representation); }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, JniListenerHolder::create(env, listener)); resource->post(jniInterface.get(), queryParams, resModelSP, callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (NoSupportException &e) { throwNoSupportException(env, e.what()); } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); } }
JNIEXPORT void JNICALL Java_org_oic_simulator_server_SimulatorSingleResource_removeAttribute (JNIEnv *env, jobject object, jstring attrName) { VALIDATE_INPUT(env, !attrName, "Attribute name is null!") SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object); VALIDATE_OBJECT(env, singleResource) try { JniString jniAttrName(env, attrName); singleResource->removeAttribute(jniAttrName.get()); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } }
JNIEXPORT void JNICALL Java_org_oic_simulator_client_SimulatorRemoteResource_stopVerification (JNIEnv *env, jobject object, jint id) { SimulatorRemoteResourceSP resource = SimulatorRemoteResourceToCpp(env, object); VALIDATE_OBJECT(env, resource) try { resource->stopVerification(id); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } catch (NoSupportException &e) { throwNoSupportException(env, e.what()); } }
JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute (JNIEnv *env, jobject jobject, jstring jKey) { if (!jKey) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, jobject); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } std::string str = env->GetStringUTFChars(jKey, NULL); resource->removeAttribute(str); }
JNIEXPORT jobject JNICALL Java_org_oic_simulator_client_SimulatorRemoteResource_setConfigInfo (JNIEnv *env, jobject object, jstring configPath) { VALIDATE_INPUT_RET(env, !configPath, "Path is null!", nullptr) SimulatorRemoteResourceSP resource = SimulatorRemoteResourceToCpp(env, object); VALIDATE_OBJECT_RET(env, resource, nullptr) try { JniString jniConfigPath(env, configPath); SimulatorResourceModelSP repSchema = resource->configure(jniConfigPath.get()); return simulatorResourceModelToJava(env, *(repSchema.get())); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); } return nullptr; }
JNIEXPORT jobject JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_createResource (JNIEnv *env, jclass object, jstring configPath, jobject listener) { if (!configPath) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Configuration file path is empty!"); return nullptr; } if (!listener) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Resource model change callback not set!"); return nullptr; } jweak jlistenerRef = env->NewWeakGlobalRef(listener); SimulatorResourceServer::ResourceModelChangedCB callback = [jlistenerRef](const std::string & uri, const SimulatorResourceModel & resModel) { onResourceModelChange(jlistenerRef, uri, resModel); }; const char *configPathCStr = env->GetStringUTFChars(configPath, NULL); SimulatorResourceServerSP resource = NULL; try { resource = SimulatorManager::getInstance()->createResource( configPathCStr, callback); if (nullptr == resource) { if (configPathCStr) env->ReleaseStringUTFChars(configPath, configPathCStr); return NULL; } } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); return nullptr; } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); return nullptr; } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return nullptr; } JniSimulatorResource *jniSimResource = new JniSimulatorResource(resource); jobject jSimulatorResource = JniSimulatorResource::toJava(env, reinterpret_cast<jlong>(jniSimResource)); jniSimResource->setResourceInfo(env, jSimulatorResource); if (configPathCStr) env->ReleaseStringUTFChars(configPath, configPathCStr); return jSimulatorResource; }
JNIEXPORT jobjectArray JNICALL Java_org_oic_simulator_SimulatorManagerNativeInterface_createResources (JNIEnv *env, jclass object, jstring configPath, jint count, jobject listener) { if (!configPath) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Configuration file path is empty!"); return nullptr; } if (!listener) { throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Resource model change callback not set!"); return nullptr; } jweak jlistenerRef = env->NewWeakGlobalRef(listener); SimulatorResourceServer::ResourceModelChangedCB callback = [jlistenerRef](const std::string & uri, const SimulatorResourceModel & resModel) { onResourceModelChange(jlistenerRef, uri, resModel); }; const char *configPathCStr = env->GetStringUTFChars(configPath, NULL); std::vector<SimulatorResourceServerSP> resources; try { resources = SimulatorManager::getInstance()->createResource(configPathCStr, count, callback); } catch (InvalidArgsException &e) { throwInvalidArgsException(env, e.code(), e.what()); return nullptr; } catch (SimulatorException &e) { throwSimulatorException(env, e.code(), e.what()); return nullptr; } catch (...) { throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); return nullptr; } // Construct the object array and send it java layer jobjectArray resourceArray = env->NewObjectArray(resources.size(), gSimulatorClassRefs.classSimulatorResource, NULL); if (resourceArray) { for (size_t i = 0; i < resources.size(); i++) { JniSimulatorResource *jniSimResource = new JniSimulatorResource(resources[i]); jobject jSimulatorResource = JniSimulatorResource::toJava(env, reinterpret_cast<jlong>(jniSimResource)); jniSimResource->setResourceInfo(env, jSimulatorResource); env->SetObjectArrayElement(resourceArray, i, jSimulatorResource); } } if (configPathCStr) env->ReleaseStringUTFChars(configPath, configPathCStr); return resourceArray; }