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 // Global map to maintain callback functions and ocresource
24 // with key = URL + callback operation
25 // e.g. key = /a/light/GET
26 std::map<string, JNICallBackContext*> gJNICallBackContextList;
28 JavaVM* g_JavaVM = NULL;
30 jclass g_ocplatform_clazz;
31 jclass g_ocresource_clazz;
32 jclass g_ocheaderoption_clazz;
33 jclass g_ocrepresentation_clazz;
34 jclass g_platformcfg_clazz;
36 static const char* TAG = "OCSTACK-JNI";
38 static JNINativeMethod ocplatform_method_table[] = {
39 { "configure", "(Lorg/iotivity/base/PlatformConfig;)V", (void *) configure},
40 { "findResource", "(Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/AbstractFindCallback;)I", (void *) findResource }
43 static int ocplatform_method_table_size = sizeof(ocplatform_method_table) / sizeof(ocplatform_method_table[0]);
45 static JNINativeMethod platformconfig_method_table[] = {
46 { "createNativeInstance", "(IILjava/lang/String;II)J", (void *) createNativeInstance },
49 static int platformconfig_method_table_size = sizeof(platformconfig_method_table) / sizeof(platformconfig_method_table[0]);
51 static JNINativeMethod ocresource_method_table[] = {
52 { "get", "(Lorg/iotivity/base/AbstractGetCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicGet},
54 { "put", "(Lorg/iotivity/base/OCRepresentation;Lorg/iotivity/base/AbstractPutCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicPut},
55 { "post", "(Lorg/iotivity/base/OCRepresentation;Lorg/iotivity/base/AbstractPostCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicPost},
56 { "observe", "(ILorg/iotivity/base/AbstractObserveCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicObserve},
57 { "cancelObserve", "()Lorg/iotivity/base/OCStackResult;", (void *) jniOicCancelObserve},
58 { "uri", "()Ljava/lang/String;", (void *) uri},
59 { "host", "()Ljava/lang/String;", (void *) host},
60 { "getResourceTypes", "()[Ljava/lang/String;", (void *) getResourceTypes},
61 { "getResourceInterfaces", "()[Ljava/lang/String;", (void *) getResourceInterfaces},
64 static int ocresource_method_table_size = sizeof(ocresource_method_table) / sizeof(ocresource_method_table[0]);
66 static JNINativeMethod ocheaderoption_method_table[] = {
67 { "getOptionID", "()I", (void *) getOptionID},
68 { "getOptionData", "()Ljava/lang/String;", (void *) getOptionData},
69 { "OCHeaderOptionConstructor", "(ILjava/lang/String;)J", (void *) OCHeaderOptionConstructor},
72 static int ocheaderoption_method_table_size = sizeof(ocheaderoption_method_table) / sizeof(ocheaderoption_method_table[0]);
74 static JNINativeMethod ocrepresentation_method_table[] = {
75 { "OCRepresentationConstructor", "()J", (void *) OCRepresentationConstructor},
76 { "getUri", "()Ljava/lang/String;", (void *) getUri},
77 { "getValueInt", "(Ljava/lang/String;)I", (void *) getValueInt},
78 { "getValueBool", "(Ljava/lang/String;)Z", (void *) getValueBool},
79 { "getValueString", "(Ljava/lang/String;)Ljava/lang/String;", (void *) getValueString},
80 { "setValueInt", "(Ljava/lang/String;I)V", (void *) setValueInt},
81 { "setValueBool", "(Ljava/lang/String;Z)V", (void *) setValueBool},
82 { "setValueString", "(Ljava/lang/String;Ljava/lang/String;)V", (void *) setValueString},
83 { "hasAttribute", "(Ljava/lang/String;)Z", (void *) hasAttribute},
86 static int ocrepresentation_method_table_size = sizeof(ocrepresentation_method_table) / sizeof(ocrepresentation_method_table[0]);
89 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
94 __android_log_print(ANDROID_LOG_ERROR, TAG, "g_JavaVM = %p\n", g_JavaVM);
96 if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
102 clazz = env->FindClass("org/iotivity/base/OCPlatform");
103 g_ocplatform_clazz = (jclass) env->NewGlobalRef(clazz);
104 env->DeleteLocalRef(clazz);
105 ret = env->RegisterNatives(g_ocplatform_clazz, ocplatform_method_table, ocplatform_method_table_size);
106 __android_log_print(ANDROID_LOG_ERROR, TAG, "load ocplatform = %d\n", ret);
108 clazz = env->FindClass("org/iotivity/base/OCResource");
109 g_ocresource_clazz = (jclass) env->NewGlobalRef(clazz);
110 env->DeleteLocalRef(clazz);
111 ret = env->RegisterNatives(g_ocresource_clazz, ocresource_method_table, ocresource_method_table_size);
112 __android_log_print(ANDROID_LOG_ERROR, TAG, "load ocresource = %d\n", ret);
114 clazz = env->FindClass("org/iotivity/base/PlatformConfig");
115 g_platformcfg_clazz = (jclass) env->NewGlobalRef(clazz);
116 env->DeleteLocalRef(clazz);
117 ret = env->RegisterNatives(g_platformcfg_clazz, platformconfig_method_table, platformconfig_method_table_size);
118 __android_log_print(ANDROID_LOG_ERROR, TAG, "load platformconfig = %d\n", ret);
120 clazz = env->FindClass("org/iotivity/base/OCHeaderOption");
121 g_ocheaderoption_clazz = (jclass) env->NewGlobalRef(clazz);
122 env->DeleteLocalRef(clazz);
123 ret = env->RegisterNatives(g_ocheaderoption_clazz, ocheaderoption_method_table, ocheaderoption_method_table_size);
124 __android_log_print(ANDROID_LOG_ERROR, TAG, "load ocheaderoption = %d\n", ret);
126 clazz = env->FindClass("org/iotivity/base/OCRepresentation");
127 g_ocrepresentation_clazz = (jclass) env->NewGlobalRef(clazz);
128 env->DeleteLocalRef(clazz);
129 ret = env->RegisterNatives(g_ocrepresentation_clazz, ocrepresentation_method_table, ocrepresentation_method_table_size);
130 __android_log_print(ANDROID_LOG_ERROR, TAG, "load ocrepresentation = %d\n", ret);
132 return JNI_VERSION_1_6;