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