1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "resourceCoordinator_JNI.h"
25 #include "android_cpp11_compat.h"
29 atomic_bool threadRun;
30 thread ocProcessThread;
32 * To execute OCProcess when threadRun value is only true
39 if (OCProcess() != OC_STACK_OK)
48 * To callback log message from C++ to Java for android
50 void messageCallback(JNIEnv *env, jobject obj, const char *c_str)
52 jstring jstr = (env)->NewStringUTF(c_str);
53 jclass cls = env->GetObjectClass(obj);
54 jmethodID cbMessage = env->GetMethodID(cls, "cbMessage", "(Ljava/lang/String;)V");
55 env->CallVoidMethod(obj,cbMessage, jstr);
58 * for Hosting Device Side
60 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_OICCoordinatorStart
61 (JNIEnv *env, jobject obj)
67 messageCallback(env,obj,"already execute OICCoordinatorStart");
68 result = (jint)HOSTING_THREAD_ERROR;
73 messageCallback(env,obj,"OICCoordinatorStart");
74 result = (jint)OICStartCoordinate();
75 string str = "OICStartCoordinate result : ";
76 string result_str = std::to_string(result);
78 messageCallback(env,obj,str.c_str());
80 ocProcessThread = thread(ocProcessFunc);
85 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_OICCoordinatorStop
86 (JNIEnv *env, jobject obj)
88 messageCallback(env,obj,"OICCoordinatorStop");
91 if (ocProcessThread.joinable())
94 ocProcessThread.join();
98 messageCallback(env,obj,"The thread may be not running.");
99 result = (jint)HOSTING_THREAD_ERROR;
102 result = (jint)OICStopCoordinate();
103 string str = "OICStopCoordinate result : ";
104 string result_str = std::to_string(result);
106 messageCallback(env,obj,str.c_str());
110 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_ResourceHostingInit
111 (JNIEnv *env, jobject obj,jstring j_addr)
113 messageCallback(env,obj,"ResourceHostingInit");
114 const char* addr = env->GetStringUTFChars(j_addr,NULL);
117 return (jint)OCSTACK_ERROR;
119 if(OCInit(addr,USE_RANDOM_PORT,OC_CLIENT_SERVER)!=OC_STACK_OK)
121 messageCallback(env,obj,"OCStack init Error");
122 return (jint)OCSTACK_ERROR;
125 env->ReleaseStringUTFChars(j_addr,addr);
126 return (jint)OCSTACK_OK;
129 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_ResourceHostingTerminate
130 (JNIEnv *env, jobject obj)
132 messageCallback(env,obj,"ResourceHostingTerminate");
133 if (OCStop() != OC_STACK_OK)
136 messageCallback(env,obj,"OCStack stop error");
137 return (jint)OCSTACK_ERROR;
140 if (ocProcessThread.joinable())
143 ocProcessThread.join();
147 messageCallback(env,obj,"The thread may be not running.");
148 return (jint)HOSTING_THREAD_ERROR;
151 return (jint)OCSTACK_OK;