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