[IOT-771] Reintroduce missing Linux BLE adapter code.
[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     OIC_LOG(DEBUG, TAG, "OUT");
80     return CA_STATUS_OK;
81 }
82
83 CAResult_t CAStartLEAdapter()
84 {
85     // Nothing to do.
86
87     return CA_STATUS_OK;
88 }
89
90 CAResult_t CAStopLEAdapter()
91 {
92     // Nothing to do.
93
94     return CA_STATUS_OK;
95 }
96
97 CAResult_t CAInitLENwkMonitorMutexVaraibles()
98 {
99     OIC_LOG(DEBUG, TAG, "IN");
100     if (NULL == gCALEDeviceStateChangedCbMutex)
101     {
102         gCALEDeviceStateChangedCbMutex = ca_mutex_new();
103         if (NULL == gCALEDeviceStateChangedCbMutex)
104         {
105             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
106             return CA_STATUS_FAILED;
107         }
108     }
109
110     OIC_LOG(DEBUG, TAG, "OUT");
111     return CA_STATUS_OK;
112
113 }
114
115 void CATerminateLENwkMonitorMutexVaraibles()
116 {
117     OIC_LOG(DEBUG, TAG, "IN");
118
119     ca_mutex_free(gCALEDeviceStateChangedCbMutex);
120     gCALEDeviceStateChangedCbMutex = NULL;
121
122     OIC_LOG(DEBUG, TAG, "OUT");
123 }
124
125 CAResult_t CAGetLEAdapterState()
126 {
127     OIC_LOG(DEBUG, TAG, "IN");
128
129     if (!g_jvm)
130     {
131         OIC_LOG(ERROR, TAG, "g_jvm is null");
132         return CA_STATUS_FAILED;
133     }
134
135     bool isAttached = false;
136     JNIEnv* env;
137     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
138     if (JNI_OK != res)
139     {
140         OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
141         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
142
143         if (JNI_OK != res)
144         {
145             OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
146             return CA_STATUS_FAILED;
147         }
148         isAttached = true;
149     }
150
151     if (!CALEIsEnableBTAdapter(env))
152     {
153         OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
154         if (isAttached)
155         {
156             (*g_jvm)->DetachCurrentThread(g_jvm);
157         }
158         return CA_ADAPTER_NOT_ENABLED;
159     }
160
161     if (isAttached)
162     {
163         (*g_jvm)->DetachCurrentThread(g_jvm);
164     }
165
166     OIC_LOG(DEBUG, TAG, "OUT");
167     return CA_STATUS_OK;
168 }
169
170 CAResult_t CAInitializeLENetworkMonitor()
171 {
172     OIC_LOG(DEBUG, TAG, "IN");
173
174     CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
175     if (CA_STATUS_OK != res)
176     {
177         OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
178         return CA_STATUS_FAILED;
179     }
180
181     CALENetworkMonitorJNISetContext();
182     CALENetworkMonitorJniInit();
183
184     OIC_LOG(DEBUG, TAG, "OUT");
185
186     return CA_STATUS_OK;
187
188 }
189
190 void CATerminateLENetworkMonitor()
191 {
192     OIC_LOG(DEBUG, TAG, "IN");
193
194     CATerminateLENwkMonitorMutexVaraibles();
195
196     OIC_LOG(DEBUG, TAG, "OUT");
197 }
198
199 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
200 {
201     OIC_LOG(DEBUG, TAG, "IN");
202
203     OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
204
205     ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
206     CALESetNetStateCallback(callback);
207     ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
208
209     OIC_LOG(DEBUG, TAG, "OUT");
210     return CA_STATUS_OK;
211 }
212
213 CAResult_t CAUnSetLEAdapterStateChangedCb()
214 {
215     OIC_LOG(DEBUG, TAG, "it is not required in this platform");
216     return CA_STATUS_OK;
217 }
218
219 JNIEXPORT void JNICALL
220 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
221                                                                    jint status)
222 {
223     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
224     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
225
226     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Network State Changed");
227
228     if (!gCALEDeviceStateChangedCallback)
229     {
230         OIC_LOG_V(ERROR, TAG, "gNetworkChangeCb is null", status);
231         return;
232     }
233
234     if (BT_STATE_ON == status) // STATE_ON:12
235     {
236         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
237         CALEClientCreateDeviceList();
238         CALEServerCreateCachedDeviceList();
239
240         CAResult_t res = CALEClientStartScan();
241         if (CA_STATUS_OK != res)
242         {
243             OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
244         }
245
246         res = CALEStartAdvertise();
247         if (CA_STATUS_OK != res)
248         {
249             OIC_LOG(ERROR, TAG, "CALEStartAdvertise has failed");
250         }
251
252         gCALEDeviceStateChangedCallback(newStatus);
253     }
254     else if (BT_STATE_OFF == status) // STATE_OFF:10
255     {
256         // remove obj for client
257         CAResult_t res = CALEClientRemoveAllGattObjs(env);
258         if (CA_STATUS_OK != res)
259         {
260             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
261         }
262
263         res = CALEClientRemoveAllScanDevices(env);
264         if (CA_STATUS_OK != res)
265         {
266             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
267         }
268
269         res = CALEClientRemoveAllDeviceState();
270         if (CA_STATUS_OK != res)
271         {
272             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
273         }
274
275         // remove obej for server
276         res = CALEServerRemoveAllDevices(env);
277         if (CA_STATUS_OK != res)
278         {
279             OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
280         }
281
282         CALEClientSetScanFlag(false);
283
284         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
285         gCALEDeviceStateChangedCallback(newStatus);
286     }
287 }
288
289 JNIEXPORT void JNICALL
290 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
291                                                                        jstring addr)
292 {
293     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
294     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
295     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
296     VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
297
298     // remove obj for client
299     CAResult_t 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 }