95c2c392770592a46bfa4a562d20f523bbb8eee1
[contrib/iotivity.git] / android / Base / app / jni / ocresource-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 = "OCRESOURCE-JNI";
24
25 JNIEXPORT jobject JNICALL jniOicGet(JNIEnv *env, jobject jobj, jobject jattributeHandler)
26 {
27
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);
30
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());
38     }
39     else {
40         iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
41         callbackContext = iter->second;
42     }
43
44         OC::QueryParamsMap test;
45     __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base get()\n");
46
47     (*resource)->get(test,
48         [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
49         {
50              JNIEnv * env;
51
52             // Attach to JavaVM
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");
59                 }
60                 else
61                     __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
62             } else if (getEnvStat == JNI_OK) {
63             //
64             } else if (getEnvStat == JNI_EVERSION) {
65                 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
66             }
67
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]));
75
76                 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu",  (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
77
78                 env->SetObjectArrayElement(joptions, n, jelem);
79                 env->DeleteLocalRef(jelem);
80             }
81
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");
88                 return;
89             }
90
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");
94             }
95
96             // Find Callback function
97             jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
98                     "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
99             if(cb_mid == NULL) {
100                 __android_log_print(ANDROID_LOG_ERROR, TAG, "onGet.Callback() is not defined in JAVA");
101                 return;
102             }
103
104             __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA GetCallback");
105             env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
106
107             __android_log_print(ANDROID_LOG_INFO, TAG, "getCB detach");
108             g_JavaVM->DetachCurrentThread();
109
110         }
111         );
112     return NULL;
113
114 }
115
116 JNIEXPORT jobject JNICALL jniOicPut(JNIEnv *env, jobject jobj, jobject jocrepresentation, jobject jattributeHandler)
117 {
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);
120
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());
128     }
129     else {
130         iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
131         callbackContext = iter->second;
132     }
133
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");
137
138     (*resource)->put(*rep, test,
139         [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
140         {
141              JNIEnv * env;
142
143             // Attach to JavaVM
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");
150                 }
151                 else
152                     __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
153             } else if (getEnvStat == JNI_OK) {
154             //
155             } else if (getEnvStat == JNI_EVERSION) {
156                 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
157             }
158
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]));
166
167                 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu",  (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
168
169                 env->SetObjectArrayElement(joptions, n, jelem);
170                 env->DeleteLocalRef(jelem);
171             }
172
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");
179                 return;
180             }
181
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");
185             }
186
187             // Find Callback function
188             jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
189                     "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
190             if(cb_mid == NULL) {
191                 __android_log_print(ANDROID_LOG_ERROR, TAG, "onPut.Callback() is not defined in JAVA");
192                 return;
193             }
194
195             __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA PutCallback");
196             env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
197
198             __android_log_print(ANDROID_LOG_INFO, TAG, "putCB detach");
199             g_JavaVM->DetachCurrentThread();
200         }
201         );
202     return NULL;
203 }
204
205 JNIEXPORT jobject JNICALL jniOicPost(JNIEnv *env, jobject jobj, jobject jocrepresentation, jobject jattributeHandler)
206 {
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);
209
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());
217     }
218     else {
219         iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
220         callbackContext = iter->second;
221     }
222
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");
226
227     (*resource)->post(*rep, test,
228         [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode)
229         {
230              JNIEnv * env;
231
232             // Attach to JavaVM
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");
239                 }
240                 else
241                     __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
242             } else if (getEnvStat == JNI_OK) {
243             //
244             } else if (getEnvStat == JNI_EVERSION) {
245                 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
246             }
247
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]));
255
256                 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu",  (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
257
258                 env->SetObjectArrayElement(joptions, n, jelem);
259                 env->DeleteLocalRef(jelem);
260             }
261
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");
268                 return;
269             }
270
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");
274             }
275
276             // Find Callback function
277             jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
278                     "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;I)V");
279             if(cb_mid == NULL) {
280                 __android_log_print(ANDROID_LOG_ERROR, TAG, "onPost.Callback() is not defined in JAVA");
281                 return;
282             }
283
284             __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA PostCallback");
285             env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode);
286
287             __android_log_print(ANDROID_LOG_INFO, TAG, "postCB detach");
288             g_JavaVM->DetachCurrentThread();
289
290             }
291     );
292     return NULL;
293 }
294
295 JNIEXPORT jobject JNICALL jniOicObserve(JNIEnv *env, jobject jobj, jint jobservetype, jobject jattributeHandler)
296 {
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);
299
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());
307     }
308     else {
309         iter->second->m_callBackFunction = env->NewGlobalRef(jattributeHandler);
310         callbackContext = iter->second;
311     }
312
313         OC::QueryParamsMap test;
314     __android_log_print(ANDROID_LOG_ERROR, TAG, "calling oic base observe()\n");
315
316     (*resource)->observe((OC::ObserveType) jobservetype, test,
317         [callbackContext](const OC::HeaderOptions& headerOptions, const OC::OCRepresentation& rep, const int eCode, const int seqNum)
318         {
319              JNIEnv * env;
320
321             // Attach to JavaVM
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");
328                 }
329                 else
330                     __android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
331             } else if (getEnvStat == JNI_OK) {
332             //
333             } else if (getEnvStat == JNI_EVERSION) {
334                 __android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
335             }
336
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]));
344
345                 __android_log_print(ANDROID_LOG_ERROR, TAG, "jelem : %llu",  (jlong) reinterpret_cast<jlong>(&headerOptions[n]));
346
347                 env->SetObjectArrayElement(joptions, n, jelem);
348                 env->DeleteLocalRef(jelem);
349             }
350
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");
357                 return;
358             }
359
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");
363             }
364
365             // Find Callback function
366             jmethodID cb_mid = env->GetMethodID(cb_clazz, "Callback", \
367                     "([Lorg/iotivity/base/OCHeaderOption;Lorg/iotivity/base/OCRepresentation;II)V");
368             if(cb_mid == NULL) {
369                 __android_log_print(ANDROID_LOG_ERROR, TAG, "onObserve.Callback() is not defined in JAVA");
370                 return;
371             }
372
373             __android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA ObserveCallback");
374             env->CallVoidMethod(callbackContext->m_callBackFunction, cb_mid, joptions, jrepresentation, eCode, seqNum);
375
376             __android_log_print(ANDROID_LOG_INFO, TAG, "observeCB detach");
377             g_JavaVM->DetachCurrentThread();
378
379         }
380     );
381     return NULL;
382 }
383
384 JNIEXPORT jobject JNICALL jniOicCancelObserve(JNIEnv *env, jobject jobj)
385 {
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");
389
390     (*resource)->cancelObserve();
391     return NULL;
392 }
393
394 JNIEXPORT jstring JNICALL uri(JNIEnv *env, jobject jobj)
395 {
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());
399 }
400
401 JNIEXPORT jstring JNICALL host(JNIEnv *env, jobject jobj)
402 {
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());
406 }
407
408 JNIEXPORT jobjectArray JNICALL getResourceTypes(JNIEnv *env, jobject jobj)
409 {
410     __android_log_print(ANDROID_LOG_ERROR, TAG, "getResourceTypes()");
411     std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
412
413     std::vector<std::string> resourceTypes = (*resource)->getResourceTypes();
414
415     int size = resourceTypes.size();
416     jclass clazz = env->FindClass("java/lang/String");
417     jobjectArray jresourceTypes = env->NewObjectArray(size, clazz, 0);
418
419     for (int n=0; n<size;n++) {
420         jstring jstr = env->NewStringUTF(resourceTypes[n].c_str());
421         env->SetObjectArrayElement(jresourceTypes, n, jstr);
422     }
423
424     return jresourceTypes;
425 }
426
427 JNIEXPORT jobjectArray JNICALL getResourceInterfaces(JNIEnv *env, jobject jobj)
428 {
429     __android_log_print(ANDROID_LOG_ERROR, TAG, "getResourceInterfaces()");
430     std::shared_ptr<OC::OCResource> *resource = getHandle<std::shared_ptr<OC::OCResource>>(env, jobj);
431
432     std::vector<std::string> resourceInterfaces = (*resource)->getResourceInterfaces();
433
434     int size = resourceInterfaces.size();
435     jclass clazz = env->FindClass("java/lang/String");
436     jobjectArray jresourceInterfaces = env->NewObjectArray(size, clazz, 0);
437
438     for (int n=0; n<size;n++) {
439         jstring jstr = env->NewStringUTF(resourceInterfaces[n].c_str());
440         env->SetObjectArrayElement(jresourceInterfaces, n, jstr);
441     }
442
443     return jresourceInterfaces;
444 }
445