iotivity 0.9.0
[platform/upstream/iotivity.git] / android / Base / app / jni / ocplatform-jni.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 MediaTek All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ocstack-jni.h"
22
23 static const char* TAG = "OCPLATFORM-JNI";
24
25 JNIEXPORT void JNICALL configure(JNIEnv *env, jobject obj, jobject jcfg)
26 {
27     OC::PlatformConfig *cfg = getHandle<OC::PlatformConfig>(env, jcfg);
28
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);
34
35     OC::OCPlatform::Configure(*cfg);
36 }
37
38
39 JNIEXPORT jint JNICALL findResource(JNIEnv* env, jobject obj, jstring jhost, jstring juri, jobject found)
40 {
41         string host = env->GetStringUTFChars(jhost,0);
42         string uri = env->GetStringUTFChars(juri,0);
43
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());
51     }
52     else
53         callbackContext = iter->second;
54
55     __android_log_print(ANDROID_LOG_ERROR, TAG, "Calling oic base findresource()\n");
56     try {
57         OC::OCPlatform::findResource(host, uri,
58             [callbackContext](std::shared_ptr<OC::OCResource> resource)
59             {
60                  JNIEnv * env;
61
62                 // Attach to JavaVM
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");
69                     }
70                     else
71                         __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
72                 } else if (getEnvStat == JNI_OK) {
73                 //
74                 } else if (getEnvStat == JNI_EVERSION) {
75                     __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
76                 }
77
78                 // save the C++ resource ptr now
79                 jmethodID resource_constructor = env->GetMethodID(g_ocresource_clazz, "<init>", "(J)V");
80
81                 callbackContext->m_resource = resource;
82                 jobject jresource = env->NewObject(g_ocresource_clazz, resource_constructor, \
83                                         (jlong) reinterpret_cast<jlong>(&callbackContext->m_resource));
84
85                 if(jresource == NULL) {
86                     __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCResource class");
87                     return;
88                 }
89
90                 jclass clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
91                 if (clazz == NULL) {
92                     __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
93                 }
94
95                 // Find Callback function
96                 jmethodID mid = env->GetMethodID(clazz, "Callback", \
97                         "(Lorg/iotivity/base/OCResource;)V");
98                 if(mid == NULL) {
99                     __android_log_print(ANDROID_LOG_ERROR, TAG, "FoundResource.Callback() is not defined in JAVA");
100                     return;
101                 }
102
103                 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA FindCallback");
104                 try {
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());
108                 }
109
110                 __android_log_print(ANDROID_LOG_INFO, TAG, "fineResourceCB detach");
111                 env->DeleteLocalRef(jresource);
112                 g_JavaVM->DetachCurrentThread();
113
114             }
115         );
116     }
117     catch(OC::OCException& e) {
118         __android_log_print(ANDROID_LOG_ERROR, TAG, "FindCB() exception : %s", e.reason(e).c_str());
119     }
120     __android_log_print(ANDROID_LOG_ERROR, TAG, "Called oic base findresource()\n");
121
122     return 0;
123 }
124