Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / android / resource_hosting / jni / ResourceHosing_JNI.cpp
1
2 //******************************************************************
3 //
4 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 //      http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21
22 extern "C" {
23 #include "hosting.h"
24 }
25 #include "ResourceHosing_JNI.h"
26 #include "OCAndroid.h"
27
28 using namespace std;
29
30 atomic_bool threadRun;
31 thread ocProcessThread;
32 /*
33  * To execute OCProcess when threadRun value is only true
34  */
35 void ocProcessFunc()
36 {
37     while (threadRun)
38     {
39
40         if (OCProcess() != OC_STACK_OK)
41         {
42             return ;
43         }
44
45         sleep(2);
46     }
47 }
48
49 /*
50  *  for Hosting Device Side
51  */
52 JNIEXPORT jint JNICALL Java_org_iotivity_ResourceHosting_ResourceHosting_OICCoordinatorStart
53 (JNIEnv *env, jobject obj)
54 {
55     jint result = 0;
56     if(threadRun==true)
57     {
58         result = (jint)HOSTING_THREAD_ERROR;
59         return result;
60     }
61     else
62     {
63         result = (jint)OICStartCoordinate();
64
65         threadRun = true;
66         ocProcessThread = thread(ocProcessFunc);
67         return result;
68     }
69 }
70
71 JNIEXPORT jint JNICALL Java_org_iotivity_ResourceHosting_ResourceHosting_OICCoordinatorStop
72 (JNIEnv *env, jobject obj)
73 {
74     jint result = 0;
75     //terminate Thread
76     if (ocProcessThread.joinable())
77     {
78         threadRun = false;
79         ocProcessThread.join();
80     }
81     else
82     {
83             result = (jint)HOSTING_THREAD_ERROR;
84             return result;
85     }
86     result = (jint)OICStopCoordinate();
87
88     return result;
89 }
90
91 JNIEXPORT jint JNICALL Java_org_iotivity_ResourceHosting_ResourceHosting_ResourceHostingInit
92 (JNIEnv *env, jobject obj,jstring j_addr)
93 {
94     const char* addr = env->GetStringUTFChars(j_addr,NULL);
95
96     if (NULL == j_addr)
97         return (jint)OCSTACK_ERROR;
98
99     if(OCInit(addr,USE_RANDOM_PORT,OC_CLIENT_SERVER)!=OC_STACK_OK)
100     {
101         return (jint)OCSTACK_ERROR;
102     }
103
104     env->ReleaseStringUTFChars(j_addr,addr);
105     return (jint)OCSTACK_OK;
106 }
107
108 JNIEXPORT jint JNICALL Java_org_iotivity_ResourceHosting_ResourceHosting_ResourceHostingTerminate
109 (JNIEnv *env, jobject obj)
110 {
111     if (OCStop() != OC_STACK_OK)
112     {
113         return (jint)OCSTACK_ERROR;
114     }
115     //terminate Thread
116     if (ocProcessThread.joinable())
117     {
118         threadRun = false;
119         ocProcessThread.join();
120     }
121     else
122     {
123         return (jint)HOSTING_THREAD_ERROR;
124     }
125
126     return (jint)OCSTACK_OK;
127 }