iotivity 0.9.0
[platform/upstream/iotivity.git] / android / Base / app / jni / ocstack-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 // 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;
27
28 JavaVM* g_JavaVM = NULL;
29
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;
35
36 static const char* TAG = "OCSTACK-JNI";
37
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 }
41 };
42
43 static int ocplatform_method_table_size = sizeof(ocplatform_method_table) / sizeof(ocplatform_method_table[0]);
44
45 static JNINativeMethod platformconfig_method_table[] = {
46   { "createNativeInstance", "(IILjava/lang/String;II)J", (void *) createNativeInstance },
47 };
48
49 static int platformconfig_method_table_size = sizeof(platformconfig_method_table) / sizeof(platformconfig_method_table[0]);
50
51 static JNINativeMethod ocresource_method_table[] = {
52   { "get", "(Lorg/iotivity/base/AbstractGetCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicGet},
53
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},
62 };
63
64 static int ocresource_method_table_size = sizeof(ocresource_method_table) / sizeof(ocresource_method_table[0]);
65
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},
70 };
71
72 static int ocheaderoption_method_table_size = sizeof(ocheaderoption_method_table) / sizeof(ocheaderoption_method_table[0]);
73
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},
84 };
85
86 static int ocrepresentation_method_table_size = sizeof(ocrepresentation_method_table) / sizeof(ocrepresentation_method_table[0]);
87
88 // JNI OnLoad
89 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
90 {
91   JNIEnv* env;
92
93   g_JavaVM = vm;
94   __android_log_print(ANDROID_LOG_ERROR, TAG, "g_JavaVM = %p\n", g_JavaVM);
95
96   if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
97     return JNI_ERR;
98   } else {
99     jint ret;
100     jclass clazz;
101
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);
107
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);
113
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);
119
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);
125
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);
131
132     return JNI_VERSION_1_6;
133   }
134 }