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 = "OCRESOURCE-JNI";
25 JNIEXPORT jobject JNICALL jniOicGet(JNIEnv *env, jobject jobj, jobject jattributeHandler)
28 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
29 __android_log_print(ANDROID_LOG_ERROR, TAG, "get() resource = %p\n", resource);
31 JNICallBackContext *callbackContext;
32 string key = (*resource)->uri() + "/GET";
33 std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
34 if(iter == gJNICallBackContextList.end()) {
35 gJNICallBackContextList[key] = new JNICallBackContext(*resource, env->NewGlobalRef(jattributeHandler));
36 callbackContext = gJNICallBackContextList[key];
37 __android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
40 iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
41 callbackContext = iter->second;
44 OC::QueryParamsMap test;
45 __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base get()\n");
47 (*resource)->get(test,
48 [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
53 // double check it's all ok
54 int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
55 if (getEnvStat == JNI_EDETACHED) {
56 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
57 if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
58 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
61 __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
62 } else if (getEnvStat == JNI_OK) {
64 } else if (getEnvStat == JNI_EVERSION) {
65 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
68 // save C++ ocheaderoption ptr now
69 jmethodID option_constructor = env->GetMethodID(g_ocheaderoption_clazz, "<init>", "(J)V");
70 int size = headerOptions.size();
71 jobjectArray joptions = env->NewObjectArray(size, g_ocheaderoption_clazz, 0);
72 for (int n=0; n<size;n++) {
73 jobject jelem = env->NewObject(g_ocheaderoption_clazz, option_constructor, \
74 (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
76 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu", (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
78 env->SetObjectArrayElement(joptions, n, jelem);
79 env->DeleteLocalRef(jelem);
82 // save C++ ocrepresentation ptr now
83 jmethodID representation_constructor = env->GetMethodID(g_ocrepresentation_clazz, "<init>", "(J)V");
84 jobject jrepresentation = env->NewObject(g_ocrepresentation_clazz, representation_constructor, \
85 (jlong) reinterpret_cast<jlong>(&rep));
86 if(jrepresentation == NULL) {
87 __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCRepresentation class");
91 jclass cb_clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
92 if (cb_clazz == NULL) {
93 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
96 // Find Callback function
97 jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
98 "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
100 __android_log_print(ANDROID_LOG_ERROR, TAG, "onGet.Callback() is not defined in JAVA");
104 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA GetCallback");
105 env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
107 __android_log_print(ANDROID_LOG_INFO, TAG, "getCB detach");
108 g_JavaVM->DetachCurrentThread();
116 JNIEXPORT jobject JNICALL jniOicPut(JNIEnv *env, jobject jobj, jobject jocrepresentation, jobject jattributeHandler)
118 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
119 __android_log_print(ANDROID_LOG_ERROR, TAG, "put1() resource = %p\n", resource);
121 JNICallBackContext *callbackContext;
122 string key = (*resource)->uri() + "/PUT";
123 std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
124 if(iter == gJNICallBackContextList.end()) {
125 gJNICallBackContextList[key] = new JNICallBackContext(*resource, env->NewGlobalRef(jattributeHandler));
126 callbackContext = gJNICallBackContextList[key];
127 __android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
130 iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
131 callbackContext = iter->second;
134 OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jocrepresentation);
135 OC::QueryParamsMap test;
136 __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base put()\n");
138 (*resource)->put(*rep, test,
139 [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
144 // double check it's all ok
145 int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
146 if (getEnvStat == JNI_EDETACHED) {
147 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
148 if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
149 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
152 __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
153 } else if (getEnvStat == JNI_OK) {
155 } else if (getEnvStat == JNI_EVERSION) {
156 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
159 // save C++ ocheaderoption ptr now
160 jmethodID option_constructor = env->GetMethodID(g_ocheaderoption_clazz, "<init>", "(J)V");
161 int size = headerOptions.size();
162 jobjectArray joptions = env->NewObjectArray(size, g_ocheaderoption_clazz, 0);
163 for (int n=0; n<size;n++) {
164 jobject jelem = env->NewObject(g_ocheaderoption_clazz, option_constructor, \
165 (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
167 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu", (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
169 env->SetObjectArrayElement(joptions, n, jelem);
170 env->DeleteLocalRef(jelem);
173 // save C++ ocrepresentation ptr now
174 jmethodID representation_constructor = env->GetMethodID(g_ocrepresentation_clazz, "<init>", "(J)V");
175 jobject jrepresentation = env->NewObject(g_ocrepresentation_clazz, representation_constructor, \
176 (jlong) reinterpret_cast<jlong>(&rep));
177 if(jrepresentation == NULL) {
178 __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCRepresentation class");
182 jclass cb_clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
183 if (cb_clazz == NULL) {
184 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
187 // Find Callback function
188 jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
189 "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
191 __android_log_print(ANDROID_LOG_ERROR, TAG, "onPut.Callback() is not defined in JAVA");
195 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA PutCallback");
196 env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
198 __android_log_print(ANDROID_LOG_INFO, TAG, "putCB detach");
199 g_JavaVM->DetachCurrentThread();
205 JNIEXPORT jobject JNICALL jniOicPost(JNIEnv *env, jobject jobj, jobject jocrepresentation, jobject jattributeHandler)
207 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
208 __android_log_print(ANDROID_LOG_ERROR, TAG, "post1() resource = %p\n", resource);
210 JNICallBackContext *callbackContext;
211 string key = (*resource)->uri() + "/POST";
212 std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
213 if(iter == gJNICallBackContextList.end()) {
214 gJNICallBackContextList[key] = new JNICallBackContext(*resource, env->NewGlobalRef(jattributeHandler));
215 callbackContext = gJNICallBackContextList[key];
216 __android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
219 iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
220 callbackContext = iter->second;
223 OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jocrepresentation);
224 OC::QueryParamsMap test;
225 __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base post()\n");
227 (*resource)->post(*rep, test,
228 [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
233 // double check it's all ok
234 int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
235 if (getEnvStat == JNI_EDETACHED) {
236 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
237 if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
238 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
241 __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
242 } else if (getEnvStat == JNI_OK) {
244 } else if (getEnvStat == JNI_EVERSION) {
245 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
248 // save C++ ocheaderoption ptr now
249 jmethodID option_constructor = env->GetMethodID(g_ocheaderoption_clazz, "<init>", "(J)V");
250 int size = headerOptions.size();
251 jobjectArray joptions = env->NewObjectArray(size, g_ocheaderoption_clazz, 0);
252 for (int n=0; n<size;n++) {
253 jobject jelem = env->NewObject(g_ocheaderoption_clazz, option_constructor, \
254 (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
256 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu", (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
258 env->SetObjectArrayElement(joptions, n, jelem);
259 env->DeleteLocalRef(jelem);
262 // save C++ ocrepresentation ptr now
263 jmethodID representation_constructor = env->GetMethodID(g_ocrepresentation_clazz, "<init>", "(J)V");
264 jobject jrepresentation = env->NewObject(g_ocrepresentation_clazz, representation_constructor, \
265 (jlong) reinterpret_cast<jlong>(&rep));
266 if(jrepresentation == NULL) {
267 __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCRepresentation class");
271 jclass cb_clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
272 if (cb_clazz == NULL) {
273 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
276 // Find Callback function
277 jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
278 "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
280 __android_log_print(ANDROID_LOG_ERROR, TAG, "onPost.Callback() is not defined in JAVA");
284 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA PostCallback");
285 env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
287 __android_log_print(ANDROID_LOG_INFO, TAG, "postCB detach");
288 g_JavaVM->DetachCurrentThread();
295 JNIEXPORT jobject JNICALL jniOicObserve(JNIEnv *env, jobject jobj, jint jobservetype, jobject jattributeHandler)
297 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
298 __android_log_print(ANDROID_LOG_ERROR, TAG, "observe1() resource = %p\n", resource);
300 JNICallBackContext *callbackContext;
301 string key = (*resource)->uri() + "/OBSERVE";
302 std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
303 if(iter == gJNICallBackContextList.end()) {
304 gJNICallBackContextList[key] = new JNICallBackContext(*resource, env->NewGlobalRef(jattributeHandler));
305 callbackContext = gJNICallBackContextList[key];
306 __android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
309 iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
310 callbackContext = iter->second;
313 OC::QueryParamsMap test;
314 __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base observe()\n");
316 (*resource)->observe((OC::ObserveType) jobservetype, test,
317 [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode, const int seqNum)
322 // double check it's all ok
323 int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
324 if (getEnvStat == JNI_EDETACHED) {
325 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
326 if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
327 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
330 __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
331 } else if (getEnvStat == JNI_OK) {
333 } else if (getEnvStat == JNI_EVERSION) {
334 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
337 // save C++ ocheaderoption ptr now
338 jmethodID option_constructor = env->GetMethodID(g_ocheaderoption_clazz, "<init>", "(J)V");
339 int size = headerOptions.size();
340 jobjectArray joptions = env->NewObjectArray(size, g_ocheaderoption_clazz, 0);
341 for (int n=0; n<size;n++) {
342 jobject jelem = env->NewObject(g_ocheaderoption_clazz, option_constructor, \
343 (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
345 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu", (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
347 env->SetObjectArrayElement(joptions, n, jelem);
348 env->DeleteLocalRef(jelem);
351 // save C++ ocrepresentation ptr now
352 jmethodID representation_constructor = env->GetMethodID(g_ocrepresentation_clazz, "<init>", "(J)V");
353 jobject jrepresentation = env->NewObject(g_ocrepresentation_clazz, representation_constructor, \
354 (jlong) reinterpret_cast<jlong>(&rep));
355 if(jrepresentation == NULL) {
356 __android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCRepresentation class");
360 jclass cb_clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
361 if (cb_clazz == NULL) {
362 __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
365 // Find Callback function
366 jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
367 "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;II)V");
369 __android_log_print(ANDROID_LOG_ERROR, TAG, "onObserve.Callback() is not defined in JAVA");
373 __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA ObserveCallback");
374 env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode, seqNum);
376 __android_log_print(ANDROID_LOG_INFO, TAG, "observeCB detach");
377 g_JavaVM->DetachCurrentThread();
384 JNIEXPORT jobject JNICALL jniOicCancelObserve(JNIEnv *env, jobject jobj)
386 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
387 __android_log_print(ANDROID_LOG_ERROR, TAG, "cancelObserve1() resource = %p\n", resource);
388 __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base cancelObserve()\n");
390 (*resource)->cancelObserve();
394 JNIEXPORT jstring JNICALL uri(JNIEnv *env, jobject jobj)
396 __android_log_print(ANDROID_LOG_ERROR, TAG, "uri()");
397 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
398 return env->NewStringUTF((*resource)->uri().c_str());
401 JNIEXPORT jstring JNICALL host(JNIEnv *env, jobject jobj)
403 __android_log_print(ANDROID_LOG_ERROR, TAG, "host()");
404 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
405 return env->NewStringUTF((*resource)->host().c_str());
408 JNIEXPORT jobjectArray JNICALL getResourceTypes(JNIEnv *env, jobject jobj)
410 __android_log_print(ANDROID_LOG_ERROR, TAG, "getResourceTypes()");
411 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
413 std::vector<std::string> resourceTypes = (*resource)->getResourceTypes();
415 int size = resourceTypes.size();
416 jclass clazz = env->FindClass("java/lang/String");
417 jobjectArray jresourceTypes = env->NewObjectArray(size, clazz, 0);
419 for (int n=0; n<size;n++) {
420 jstring jstr = env->NewStringUTF(resourceTypes[n].c_str());
421 env->SetObjectArrayElement(jresourceTypes, n, jstr);
424 return jresourceTypes;
427 JNIEXPORT jobjectArray JNICALL getResourceInterfaces(JNIEnv *env, jobject jobj)
429 __android_log_print(ANDROID_LOG_ERROR, TAG, "getResourceInterfaces()");
430 std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
432 std::vector<std::string> resourceInterfaces = (*resource)->getResourceInterfaces();
434 int size = resourceInterfaces.size();
435 jclass clazz = env->FindClass("java/lang/String");
436 jobjectArray jresourceInterfaces = env->NewObjectArray(size, clazz, 0);
438 for (int n=0; n<size;n++) {
439 jstring jstr = env->NewStringUTF(resourceInterfaces[n].c_str());
440 env->SetObjectArrayElement(jresourceInterfaces, n, jstr);
443 return jresourceInterfaces;