Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-hosting / 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     if (NULL == j_addr)
95     {
96         return (jint)OCSTACK_ERROR;
97     }
98
99     if(OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS))
100     {
101         return (jint)OCSTACK_ERROR;
102     }
103
104     return (jint)OCSTACK_OK;
105 }
106
107 JNIEXPORT jint JNICALL Java_org_iotivity_ResourceHosting_ResourceHosting_ResourceHostingTerminate
108 (JNIEnv *env, jobject obj)
109 {
110     if (OCStop() != OC_STACK_OK)
111     {
112         return (jint)OCSTACK_ERROR;
113     }
114     //terminate Thread
115     if (ocProcessThread.joinable())
116     {
117         threadRun = false;
118         ocProcessThread.join();
119     }
120
121     return (jint)OCSTACK_OK;
122 }