Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / calenwmonitor.c
1 /******************************************************************
2  *
3  * Copyright 2014 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 #include <jni.h>
22 #include <stdio.h>
23 #include <android/log.h>
24 #include "logger.h"
25 #include "calenwmonitor.h"
26 #include "caleclient.h"
27 #include "caleserver.h"
28 #include "caleutils.h"
29 #include "caleinterface.h"
30 #include "caadapterutils.h"
31
32 #include "camutex.h"
33
34 #include "org_iotivity_ca_CaLeClientInterface.h"
35
36 #define TAG PCF("CA_LE_MONITOR")
37
38 #define BT_STATE_ON (12)
39 #define BT_STATE_OFF (10)
40
41 static JavaVM *g_jvm;
42
43 /**
44  * @var gCALEDeviceStateChangedCallback
45  * @brief Maintains the callback to be notified on device state changed.
46  */
47 static CALEDeviceStateChangedCallback gCALEDeviceStateChangedCallback = NULL;
48
49 /**
50  * @var gCALEDeviceStateChangedCbMutex
51  * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
52  *           of the LE adapter gets change.
53  */
54 static ca_mutex gCALEDeviceStateChangedCbMutex = NULL;
55
56 //getting context
57 void CALENetworkMonitorJNISetContext()
58 {
59     OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
60 }
61
62 //getting jvm
63 void CALENetworkMonitorJniInit()
64 {
65     OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
66     g_jvm = CANativeJNIGetJavaVM();
67 }
68
69 void CALESetNetStateCallback(CALEDeviceStateChangedCallback callback)
70 {
71     OIC_LOG(DEBUG, TAG, "CALESetNetStateCallback");
72     gCALEDeviceStateChangedCallback = callback;
73 }
74
75 CAResult_t CAInitializeLEAdapter()
76 {
77     OIC_LOG(DEBUG, TAG, "IN");
78
79     CALENetworkMonitorJNISetContext();
80     CALENetworkMonitorJniInit();
81
82     OIC_LOG(DEBUG, TAG, "OUT");
83     return CA_STATUS_OK;
84 }
85
86 CAResult_t CAStartLEAdapter()
87 {
88     // Nothing to do.
89
90     return CA_STATUS_OK;
91 }
92
93 CAResult_t CAInitLENwkMonitorMutexVaraibles()
94 {
95     OIC_LOG(DEBUG, TAG, "IN");
96     if (NULL == gCALEDeviceStateChangedCbMutex)
97     {
98         gCALEDeviceStateChangedCbMutex = ca_mutex_new();
99         if (NULL == gCALEDeviceStateChangedCbMutex)
100         {
101             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
102             return CA_STATUS_FAILED;
103         }
104     }
105
106     OIC_LOG(DEBUG, TAG, "OUT");
107     return CA_STATUS_OK;
108
109 }
110
111 void CATerminateLENwkMonitorMutexVaraibles()
112 {
113     OIC_LOG(DEBUG, TAG, "IN");
114
115     ca_mutex_free(gCALEDeviceStateChangedCbMutex);
116     gCALEDeviceStateChangedCbMutex = NULL;
117
118     OIC_LOG(DEBUG, TAG, "OUT");
119 }
120
121 CAResult_t CAGetLEAdapterState()
122 {
123     OIC_LOG(DEBUG, TAG, "IN");
124
125     if (!g_jvm)
126     {
127         OIC_LOG(ERROR, TAG, "g_jvm is null");
128         return CA_STATUS_FAILED;
129     }
130
131     bool isAttached = false;
132     JNIEnv* env;
133     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
134     if (JNI_OK != res)
135     {
136         OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
137         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
138
139         if (JNI_OK != res)
140         {
141             OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
142             return CA_STATUS_FAILED;
143         }
144         isAttached = true;
145     }
146
147     if (!CALEIsEnableBTAdapter(env))
148     {
149         OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
150         if (isAttached)
151         {
152             (*g_jvm)->DetachCurrentThread(g_jvm);
153         }
154         return CA_ADAPTER_NOT_ENABLED;
155     }
156
157     if (isAttached)
158     {
159         (*g_jvm)->DetachCurrentThread(g_jvm);
160     }
161
162     OIC_LOG(DEBUG, TAG, "OUT");
163     return CA_STATUS_OK;
164 }
165
166 CAResult_t CAInitializeLENetworkMonitor()
167 {
168     OIC_LOG(DEBUG, TAG, "IN");
169
170     CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
171     if (CA_STATUS_OK != res)
172     {
173         OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
174         return CA_STATUS_FAILED;
175     }
176
177     OIC_LOG(DEBUG, TAG, "OUT");
178
179     return CA_STATUS_OK;
180
181 }
182
183 void CATerminateLENetworkMonitor()
184 {
185     OIC_LOG(DEBUG, TAG, "IN");
186
187     CATerminateLENwkMonitorMutexVaraibles();
188
189     OIC_LOG(DEBUG, TAG, "OUT");
190 }
191
192 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
193 {
194     OIC_LOG(DEBUG, TAG, "IN");
195
196     OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
197
198     ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
199     CALESetNetStateCallback(callback);
200     ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
201
202     OIC_LOG(DEBUG, TAG, "OUT");
203     return CA_STATUS_OK;
204 }
205
206 CAResult_t CAUnSetLEAdapterStateChangedCb()
207 {
208     OIC_LOG(DEBUG, TAG, "it is not required in this platform");
209     return CA_STATUS_OK;
210 }
211
212 JNIEXPORT void JNICALL
213 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
214                                                                    jint status)
215 {
216     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
217     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
218
219     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Network State Changed");
220
221     if (!gCALEDeviceStateChangedCallback)
222     {
223         OIC_LOG_V(ERROR, TAG, "gNetworkChangeCb is null", status);
224         return;
225     }
226
227     if (BT_STATE_ON == status) // STATE_ON:12
228     {
229         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
230         CALEClientCreateDeviceList();
231         CALEServerCreateCachedDeviceList();
232
233         CAResult_t res = CALEClientStartScan();
234         if (CA_STATUS_OK != res)
235         {
236             OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
237         }
238
239         res = CALEStartAdvertise();
240         if (CA_STATUS_OK != res)
241         {
242             OIC_LOG(ERROR, TAG, "CALEStartAdvertise has failed");
243         }
244
245         gCALEDeviceStateChangedCallback(newStatus);
246     }
247     else if (BT_STATE_OFF == status) // STATE_OFF:10
248     {
249         // remove obj for client
250         CAResult_t res = CALEClientRemoveAllGattObjs(env);
251         if (CA_STATUS_OK != res)
252         {
253             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
254         }
255
256         res = CALEClientRemoveAllScanDevices(env);
257         if (CA_STATUS_OK != res)
258         {
259             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
260         }
261
262         res = CALEClientRemoveAllDeviceState();
263         if (CA_STATUS_OK != res)
264         {
265             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
266         }
267
268         // remove obej for server
269         res = CALEServerRemoveAllDevices(env);
270         if (CA_STATUS_OK != res)
271         {
272             OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
273         }
274
275         CALEClientSetScanFlag(false);
276
277         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
278         gCALEDeviceStateChangedCallback(newStatus);
279     }
280 }
281
282 JNIEXPORT void JNICALL
283 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
284                                                                        jstring addr)
285 {
286     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
287     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
288     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
289     VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
290
291     // remove obj for client
292     CAResult_t res = CALEClientRemoveGattObjForAddr(env, addr);
293     if (CA_STATUS_OK != res)
294     {
295         OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
296     }
297
298     res = CALEClientRemoveDeviceInScanDeviceList(env, addr);
299     if (CA_STATUS_OK != res)
300     {
301         OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
302     }
303
304     // remove obej for server
305     res = CALEServerRemoveDevice(env, addr);
306     if (CA_STATUS_OK != res)
307     {
308         OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
309     }
310
311 }