static jobject g_foundDeviceListenerObject = NULL;
static jobject g_listenerObject = NULL;
static JavaVM *g_jvm = NULL;
+static jclass g_jni_cls_enum = NULL;
+static jmethodID g_jni_mid_enum = NULL;
JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
{
CANativeJNISetContext(env, context);
}
-void CAManagerConnectionStateChangedCB(CATransportAdapter_t adapter,
- const char *remote_address,
+void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
bool connected)
{
- LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
- adapter, remote_address, connected);
+ if (!info)
+ {
+ LOGE("info is NULL");
+ return;
+ }
if (!g_listenerObject)
{
return;
}
+ LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
+ info->adapter, info->addr, connected);
+
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
goto exit_error;
}
- jstring jni_address = (*env)->NewStringUTF(env, remote_address);
+ jstring jni_address = (*env)->NewStringUTF(env, info->addr);
if (!jni_address)
{
LOGE("jni_address is null");
goto exit_error;
}
- jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
- if (!jni_cls_enum)
+ if (g_jni_cls_enum && g_jni_mid_enum)
{
- LOGE("could not get jni_cls_enum");
- goto exit_error;
- }
-
- jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
- "(I)Lorg/iotivity/base/OcConnectivityType;");
- if (!jni_mid_enum)
- {
- LOGE("could not get Method ID (getInstance)");
- goto exit_error;
+ jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
+ g_jni_mid_enum, info->adapter);
+ (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
+ jni_adaptertype, jni_address,
+ (jboolean)connected);
}
- jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
- jni_mid_enum, adapter);
- (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
- jni_adaptertype, jni_address,
- (jboolean)connected);
-
exit_error:
if (isAttached)
{
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
goto exit_error;
}
- jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
- if (!jni_cls_enum)
+ if (g_jni_cls_enum && g_jni_mid_enum)
{
- LOGE("could not get jni_cls_enum");
- goto exit_error;
- }
+ jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
+ g_jni_mid_enum, adapter);
- jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
- "(I)Lorg/iotivity/base/OcConnectivityType;");
- if (!jni_mid_enum)
- {
- LOGE("could not get Method ID (getInstance)");
- goto exit_error;
+ (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
+ jni_adaptertype, (jboolean)enabled);
}
- jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
- jni_mid_enum, adapter);
-
- (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
- jni_adaptertype, (jboolean)enabled);
-
exit_error:
if (isAttached)
{
CAUtilClientInitialize(env, g_jvm, context);
- g_listenerObject = (*env)->NewGlobalRef(env, listener);
+ if (listener)
+ {
+ g_listenerObject = (*env)->NewGlobalRef(env, listener);
+ }
+
+ if (g_listenerObject)
+ {
+ jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
+ if (cls)
+ {
+ g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls);
+ }
+ if (g_jni_cls_enum)
+ {
+ g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance",
+ "(I)Lorg/iotivity/base/OcConnectivityType;");
+ }
+ }
CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
CAManagerConnectionStateChangedCB);
}
if (g_listenerObject)
{
(*env)->DeleteGlobalRef(env, g_listenerObject);
+ g_listenerObject = NULL;
+ }
+
+ if (g_jni_cls_enum)
+ {
+ (*env)->DeleteGlobalRef(env, g_jni_cls_enum);
+ g_jni_cls_enum = NULL;
}
}
jstring jaddress)
{
LOGI("CaManager_setAutoConnectionDeviceInfo");
+ if (!jaddress)
+ {
+ LOGE("jaddress is null");
+ return;
+ }
const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
if (!address)
}
CASetAutoConnectionDeviceInfo(address);
+
+ (*env)->ReleaseStringUTFChars(env, jaddress, address);
}
JNIEXPORT void JNICALL
jstring jaddress)
{
LOGI("CaManager_unsetAutoConnectionDeviceInfo");
+ if (!jaddress)
+ {
+ LOGE("jaddress is null");
+ return;
+ }
const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
if (!address)
}
CAUnsetAutoConnectionDeviceInfo(address);
+
+ (*env)->ReleaseStringUTFChars(env, jaddress, address);
}
JNIEXPORT void JNICALL
(void)clazz;
CAUtilCreateBond(env, device);
}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
+ jint intervalTime, jint workignCount)
+{
+ LOGI("setLeScanIntervalTimeImpl");
+ (void)env;
+ (void)clazz;
+ CAUtilSetLEScanInterval(intervalTime, workignCount);
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaInterface_stopLeScanImpl(JNIEnv *env, jclass clazz)
+{
+ LOGI("stopLeScan");
+ (void)env;
+ (void)clazz;
+ CAUtilStopLEScan();
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaInterface_startLeAdvertisingImpl(JNIEnv *env, jclass clazz)
+{
+ LOGI("startLeAdvertising");
+ (void)env;
+ (void)clazz;
+ CAUtilStartLEAdvertising();
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaInterface_stopLeAdvertisingImpl(JNIEnv *env, jclass clazz)
+{
+ LOGI("stopLeAdvertising");
+ (void)env;
+ (void)clazz;
+ CAUtilStopLEAdvertising();
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaInterface_setBTConfigureImpl(JNIEnv *env, jclass clazz, jint flag)
+{
+ LOGI("setConfigureImpl");
+ (void)env;
+ (void)clazz;
+ CAUtilConfig_t configs = {(CATransportBTFlags_t)flag};
+ CAUtilSetBTConfigure(configs);
+}
+
+JNIEXPORT jint JNICALL Java_org_iotivity_ca_CaInterface_setCipherSuiteImpl
+ (JNIEnv *env, jclass clazz, jint cipherSuite, jint adapter)
+{
+ LOGI("setCipherSuiteImpl");
+ (void)env;
+ (void)clazz;
+ CAResult_t ret = CASelectCipherSuite(cipherSuite, (CATransportAdapter_t) adapter);
+ if (CA_STATUS_OK != ret)
+ {
+ LOGE("CASelectCipherSuite has failed");
+ }
+ return ret;
+}
+
+JNIEXPORT jint JNICALL Java_org_iotivity_ca_CaInterface_disconnectTCPSessionImpl
+ (JNIEnv *env, jclass clazz, jstring address, jint port, jint transportFlags)
+{
+ LOGI("disconnectTCPSessionImpl");
+ (void)env;
+ (void)clazz;
+
+ if(!address)
+ {
+ LOGE("Java address is null");
+ return CA_STATUS_INVALID_PARAM;
+ }
+
+ //convert java string to native string
+ const char* nativeAddress = (*env)->GetStringUTFChars(env, address, NULL);
+ if (!nativeAddress)
+ {
+ LOGE("Native address is null");
+ return CA_STATUS_INVALID_PARAM;
+ }
+
+ CAResult_t result = CAUtilTCPDisconnectSession(nativeAddress, (int)port,
+ (CATransportFlags_t)transportFlags);
+ if (CA_STATUS_OK != result)
+ {
+ LOGE("disconnectTCPSessionImpl failed");
+ }
+ return result;
+}
+