1 //******************************************************************
3 // Copyright 2014 MediaTek All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 "ocstack-jni.h"
23 static const char* TAG = "OCPLATFORM-JNI";
25 JNIEXPORT void JNICALL configure(JNIEnv *env, jobject obj, jobject jcfg)
27 OC::PlatformConfig *cfg = getHandle<OC::PlatformConfig>(env, jcfg);
29 __android_log_print(ANDROID_LOG_INFO, TAG, "service : %d\n", (int) cfg->serviceType);
30 __android_log_print(ANDROID_LOG_INFO, TAG, "mode : %d\n", (int) cfg->mode);
31 __android_log_print(ANDROID_LOG_INFO, TAG, "IP : %s\n", (cfg->ipAddress).c_str());
32 __android_log_print(ANDROID_LOG_INFO, TAG, "port : %d\n", cfg->port);
33 __android_log_print(ANDROID_LOG_INFO, TAG, "QOS : %d\n", (int) cfg->QoS);
35 OC::OCPlatform::Configure(*cfg);
39 JNIEXPORT jint JNICALL findResource(JNIEnv* env, jobject obj, jstring jhost, jstring juri, jobject found)
41 string host = env->GetStringUTFChars(jhost,0);
42 string uri = env->GetStringUTFChars(juri,0);
44 JNICallBackContext *callbackContext;
45 string key = uri + "/FIND";
46 std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
47 if(iter == gJNICallBackContextList.end()) {
48 gJNICallBackContextList[key] = new JNICallBackContext(env->NewGlobalRef(found));
49 callbackContext = gJNICallBackContextList[key];
50 __android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
53 callbackContext = iter->second;
55 __android_log_print(ANDROID_LOG_ERROR, TAG, "Calling oic base findresource()\n");
57 OC::OCPlatform::findResource(host, uri,
58 [callbackContext](std::shared_ptr<OC::OCResource> resource)
63 // double check it's all ok
64 int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
65 if (getEnvStat == JNI_EDETACHED) {
66 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
67 if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
68 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
71 __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
72 } else if (getEnvStat == JNI_OK) {
74 } else if (getEnvStat == JNI_EVERSION) {
75 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
78 // save the C++ resource ptr now
79 jmethodID resource_constructor = env->GetMethodID(g_ocresource_clazz, "<init>", "(J)V");
81 callbackContext->m_resource = resource;
82 jobject jresource = env->NewObject(g_ocresource_clazz, resource_constructor, \
83 (jlong) reinterpret_cast<jlong>(&callbackContext->m_resource));
85 if(jresource == NULL) {
86 __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCResource class");
90 jclass clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
92 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
95 // Find Callback function
96 jmethodID mid = env->GetMethodID(clazz, "Callback", \
97 "(Lorg/iotivity/base/OCResource;)V");
99 __android_log_print(ANDROID_LOG_ERROR, TAG, "FoundResource.Callback() is not defined in JAVA");
103 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA FindCallback");
105 env->CallVoidMethod(callbackContext->m_callBackFunction, mid, jresource);
106 } catch(OC::OCException& e) {
107 __android_log_print(ANDROID_LOG_ERROR, TAG, "callbackContext() exception : %s", e.reason(e).c_str());
110 __android_log_print(ANDROID_LOG_INFO, TAG, "fineResourceCB detach");
111 env->DeleteLocalRef(jresource);
112 g_JavaVM->DetachCurrentThread();
117 catch(OC::OCException& e) {
118 __android_log_print(ANDROID_LOG_ERROR, TAG, "FindCB() exception : %s", e.reason(e).c_str());
120 __android_log_print(ANDROID_LOG_ERROR, TAG, "Called oic base findresource()\n");