Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / resourceCoordinator_JNI.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 extern "C" {
22 #include "hosting.h"
23 }
24 #include "resourceCoordinator_JNI.h"
25 #include "android_cpp11_compat.h"
26
27 using namespace std;
28
29 atomic_bool threadRun;
30 thread ocProcessThread;
31 /*
32  * To execute OCProcess when threadRun value is only true
33  */
34 void ocProcessFunc()
35 {
36     while (threadRun)
37     {
38
39         if (OCProcess() != OC_STACK_OK)
40         {
41             //OCProcess ERROR
42         }
43
44         sleep(2);
45     }
46 }
47 /*
48  * To callback log message from C++ to Java for android
49  */
50 void messageCallback(JNIEnv *env, jobject obj, const char *c_str)
51 {
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);
56 }
57 /*
58  *  for Hosting Device Side
59  */
60 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_OICCoordinatorStart
61 (JNIEnv *env, jobject obj)
62 {
63     jint result = 0;
64     if(threadRun==true)
65     {
66
67         messageCallback(env,obj,"already execute OICCoordinatorStart");
68         result = (jint)HOSTING_THREAD_ERROR;
69         return result;
70     }
71     else
72     {
73         messageCallback(env,obj,"OICCoordinatorStart");
74         result = (jint)OICStartCoordinate();
75         string str = "OICStartCoordinate result : ";
76         string result_str = std::to_string(result);
77         str += result_str;
78         messageCallback(env,obj,str.c_str());
79         threadRun = true;
80         ocProcessThread = thread(ocProcessFunc);
81         return result;
82     }
83 }
84
85 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_OICCoordinatorStop
86 (JNIEnv *env, jobject obj)
87 {
88     messageCallback(env,obj,"OICCoordinatorStop");
89     jint result = 0;
90     //terminate Thread
91     if (ocProcessThread.joinable())
92     {
93         threadRun = false;
94         ocProcessThread.join();
95     }
96     else
97     {
98             messageCallback(env,obj,"The thread may be not running.");
99             result = (jint)HOSTING_THREAD_ERROR;
100             return result;
101     }
102     result = (jint)OICStopCoordinate();
103     string str = "OICStopCoordinate result : ";
104     string result_str = std::to_string(result);
105     str += result_str;
106     messageCallback(env,obj,str.c_str());
107     return result;
108 }
109
110 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_ResourceHostingInit
111 (JNIEnv *env, jobject obj,jstring j_addr)
112 {
113     messageCallback(env,obj,"ResourceHostingInit");
114     const char* addr = env->GetStringUTFChars(j_addr,NULL);
115
116     if (NULL == j_addr)
117         return (jint)OCSTACK_ERROR;
118
119     if(OCInit(addr,USE_RANDOM_PORT,OC_CLIENT_SERVER)!=OC_STACK_OK)
120     {
121         messageCallback(env,obj,"OCStack init Error");
122         return (jint)OCSTACK_ERROR;
123     }
124
125     env->ReleaseStringUTFChars(j_addr,addr);
126     return (jint)OCSTACK_OK;
127 }
128
129 JNIEXPORT jint JNICALL Java_com_example_resourcehostingsampleapp_ResourceHosting_ResourceHostingTerminate
130 (JNIEnv *env, jobject obj)
131 {
132     messageCallback(env,obj,"ResourceHostingTerminate");
133     if (OCStop() != OC_STACK_OK)
134     {
135
136         messageCallback(env,obj,"OCStack stop error");
137         return (jint)OCSTACK_ERROR;
138     }
139     //terminate Thread
140     if (ocProcessThread.joinable())
141     {
142         threadRun = false;
143         ocProcessThread.join();
144     }
145     else
146     {
147         messageCallback(env,obj,"The thread may be not running.");
148         return (jint)HOSTING_THREAD_ERROR;
149     }
150
151     return (jint)OCSTACK_OK;
152 }