+
+/*
+* Class: org_iotivity_base_OcPlatform
+* Method: constructAccountManagerObject0
+* Signature: (Ljava/lang/String;I)Lorg/iotivity/base/OcAccountManager;
+*/
+JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0(
+ JNIEnv *env,
+ jclass clazz,
+ jstring jHost,
+ jint jConnectivityType)
+{
+#ifndef WITH_CLOUD
+ ThrowOcException(OC_STACK_ERROR,
+ "OCAccountManager is not supported. (Please build with WITH_CLOUD=1 option)");
+ return nullptr;
+#else
+ LOGD("OcPlatform_constructAccountManagerObject");
+ if (!jHost)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null");
+ return nullptr;
+ }
+
+ const char* charHost = env->GetStringUTFChars(jHost, nullptr);
+ if (!charHost)
+ {
+ ThrowOcException(JNI_EXCEPTION, "charHost is null");
+ return nullptr;
+ }
+ std::string host(charHost);
+ env->ReleaseStringUTFChars(jHost, charHost);
+
+ std::shared_ptr<OCAccountManager> accountManager = OCPlatform::constructAccountManagerObject(
+ host,
+ static_cast<OCConnectivityType>(jConnectivityType));
+
+ if (!accountManager)
+ {
+ ThrowOcException(OC_STACK_ERROR, "Failed to create OCAccountManager");
+ return nullptr;
+ }
+
+ JniOcAccountManager *jniOcAccountManager = new JniOcAccountManager(accountManager);
+
+ jobject jAccountManager = env->NewObject(g_cls_OcAccountManager, g_mid_OcAccountManager_ctor);
+ if (!jAccountManager)
+ {
+ delete jniOcAccountManager;
+ return nullptr;
+ }
+ SetHandle<JniOcAccountManager>(env, jAccountManager, jniOcAccountManager);
+ if (env->ExceptionCheck())
+ {
+ delete jniOcAccountManager;
+ return nullptr;
+ }
+ return jAccountManager;
+#endif
+}
+
+/*
+* Class: org_iotivity_base_OcPlatform
+* Method: getDeviceId
+* Signature: (I)V
+*/
+JNIEXPORT jbyteArray JNICALL Java_org_iotivity_base_OcPlatform_getDeviceId
+(JNIEnv *env, jobject thiz)
+{
+ LOGD("OcPlatform_getDeviceId");
+ OCUUIdentity deviceId;
+
+ jbyteArray ret = env->NewByteArray(UUID_IDENTITY_SIZE);
+ jbyte uuid[UUID_IDENTITY_SIZE];
+ try
+ {
+
+ OCStackResult result = OCPlatform::getDeviceId(&deviceId);
+ LOGD("OcPlatform_getDeviceId return from CPP");
+ if (OC_STACK_OK != result)
+ {
+ ThrowOcException(result, "Error while getting my device Id");
+ }
+ else
+ {
+ for(int i=0;i < UUID_IDENTITY_SIZE; i++)
+ {
+ uuid[i] =(jbyte) deviceId.id[i];
+ }
+ }
+
+ }
+ catch (OCException& e)
+ {
+ LOGE("%s", e.reason().c_str());
+ ThrowOcException(e.code(), e.reason().c_str());
+ }
+
+ env->SetByteArrayRegion(ret, 0, UUID_IDENTITY_SIZE, uuid);
+
+ return ret;
+}
+
+/*
+* Class: org_iotivity_base_OcPlatform
+* Method: setDeviceId
+* Signature: (Ljava/lang/byte;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setDeviceId(
+ JNIEnv *env, jobject thiz, jbyteArray data)
+{
+ LOGI("OcPlatform_setDeviceId");
+ OCUUIdentity deviceId;
+ try
+ {
+ OCStackResult result;
+ jbyte* uuid = env->GetByteArrayElements(data, 0);
+ jsize arrayLength = env->GetArrayLength(data);
+ if(arrayLength!=UUID_IDENTITY_SIZE)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "Byte length not equal to UUID_IDENTITY_SIZE");
+ }
+ else
+ {
+ for(int i=0;i < UUID_IDENTITY_SIZE; i++)
+ {
+ deviceId.id[i]=(jchar)uuid[i];
+ }
+ result = OCPlatform::setDeviceId(&deviceId);
+ if (OC_STACK_OK != result)
+ {
+ ThrowOcException(result, "Failed to set DeviceId");
+ }
+ }
+ }
+ catch (OCException& e)
+ {
+ LOGE("%s", e.reason().c_str());
+ ThrowOcException(e.code(), e.reason().c_str());
+ }
+}
+
+/*
+ * Class: org_iotivity_base_OcPlatform
+ * Method: findKeepAliveResourceImpl
+ * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcPlatform/KeepAliveListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findKeepAliveResourceImpl(
+ JNIEnv *env, jclass clazz, jstring jHost, jobject jListener)
+{
+#ifndef TCP_ADAPTER
+ ThrowOcException(OC_STACK_ERROR,
+ "findKeepAliveResource is not supported. (Please build with WITH_TCP=1 option)");
+ return;
+#else
+ LOGI("OcPlatform_findKeepAliveResource");
+ std::string host;
+ if (!jHost)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null");
+ return;
+ }
+ else
+ {
+ host = env->GetStringUTFChars(jHost, nullptr);
+ }
+
+ if (!jListener)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "onKeepAliveFoundListener cannot be null");
+ return;
+ }
+
+ JniKeepAliveListener *onKeepAliveFoundListener = AddKeepAliveListener(env, jListener);
+
+ KeepAliveCallback KeepAliveCallback = [onKeepAliveFoundListener](const int ret,
+ const OCRepresentation& rep)
+ {
+ onKeepAliveFoundListener->onKeepAliveListener(ret, rep);
+ };
+
+ try
+ {
+ OCStackResult result = OCPlatform::findKeepAliveResource(host, KeepAliveCallback);
+
+ if (OC_STACK_OK != result)
+ {
+ ThrowOcException(result, "findKeepAliveResource has failed");
+ }
+ }
+ catch (OCException& e)
+ {
+ LOGE("%s", e.reason().c_str());
+ ThrowOcException(e.code(), e.reason().c_str());
+ return;
+ }
+#endif
+}
+
+/*
+ * Class: org_iotivity_base_OcPlatform
+ * Method: sendKeepAliveRequestImpl
+ * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/KeepAliveListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendKeepAliveRequestImpl(
+ JNIEnv *env, jclass clazz, jstring jHost, jobject jRep, jobject jListener)
+{
+#ifndef TCP_ADAPTER
+ ThrowOcException(OC_STACK_ERROR,
+ "sendKeepAlive is not supported. (Please build with WITH_TCP=1 option)");
+ return;
+#else
+ LOGI("OcPlatform_sendKeepAliveRequest");
+ std::string host;
+ if (!jHost)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null");
+ return;
+ }
+ else
+ {
+ host = env->GetStringUTFChars(jHost, nullptr);
+ }
+
+ if (!jListener)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "keepAliveResponseListener cannot be null");
+ return;
+ };
+
+ JniKeepAliveListener *KeepAliveResponseListener = AddKeepAliveListener(env, jListener);
+
+ KeepAliveCallback KeepAliveCallback = [KeepAliveResponseListener](const int ret,
+ const OCRepresentation& rep)
+ {
+ KeepAliveResponseListener->onKeepAliveListener(ret, rep);
+ };
+
+ try
+ {
+ OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
+ OCStackResult result = OCPlatform::sendKeepAliveRequest(host, *rep, KeepAliveCallback);
+
+ if (OC_STACK_OK != result)
+ {
+ ThrowOcException(result, "sendKeepAliveRequest has failed");
+ }
+ }
+ catch (OCException& e)
+ {
+ LOGE("%s", e.reason().c_str());
+ ThrowOcException(e.code(), e.reason().c_str());
+ return;
+ }
+#endif
+}