removed warning from scons build for Android EDR
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrnwmonitor.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 <stdio.h>
22 #include <string.h>
23 #include <jni.h>
24
25 #include "caedrinterface.h"
26 #include "caedrnwmonitor.h"
27 #include "logger.h"
28 #include "oic_malloc.h"
29 #include "cathreadpool.h" /* for thread pool */
30 #include "camutex.h"
31 #include "uarraylist.h"
32 #include "caadapterutils.h"
33 #include "caedrserver.h"
34 #include "caedrutils.h"
35
36 #include "org_iotivity_ca_CaEdrInterface.h"
37
38 //#define DEBUG_MODE
39 #define TAG PCF("CA_EDR_MONITOR")
40
41 static JavaVM *g_jvm;
42 static jobject g_context;
43 static CAEDRNetworkStatusCallback g_networkChangeCb = NULL;
44
45 static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
46
47 void CAEDRNetworkMonitorJNISetContext()
48 {
49     OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJNISetContext");
50     g_context = (jobject) CANativeJNIGetContext();
51 }
52
53 //getting jvm
54 void CAEDRNetworkMonitorJniInit()
55 {
56     OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJniInit");
57     g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
58 }
59
60 CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
61 {
62     OIC_LOG(DEBUG, TAG, "IN");
63
64     CAEDRNetworkMonitorJniInit();
65     CANativeJNIGetJavaVM();
66
67     OIC_LOG(DEBUG, TAG, "OUT");
68     return CA_STATUS_OK;
69 }
70
71 void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallback)
72 {
73     OIC_LOG(DEBUG, TAG, "CAEDRSetNetworkChangeCallback");
74     g_networkChangeCb = networkChangeCallback;
75 }
76
77 void CAEDRTerminateNetworkMonitor(void)
78 {
79     OIC_LOG(DEBUG, TAG, "IN");
80
81     OIC_LOG(DEBUG, TAG, "OUT");
82 }
83
84 CAResult_t CAEDRStartNetworkMonitor()
85 {
86     OIC_LOG(DEBUG, TAG, "IN");
87
88     OIC_LOG(DEBUG, TAG, "OUT");
89     return CA_STATUS_OK;
90 }
91
92 CAResult_t CAEDRStopNetworkMonitor()
93 {
94     OIC_LOG(DEBUG, TAG, "IN");
95
96     OIC_LOG(DEBUG, TAG, "OUT");
97     return CA_STATUS_OK;
98 }
99
100 CAResult_t CAEDRClientSetCallbacks(void)
101 {
102     OIC_LOG(DEBUG, TAG, "IN");
103
104     OIC_LOG(DEBUG, TAG, "OUT");
105     return CA_STATUS_OK;
106 }
107
108 JNIEXPORT void JNICALL
109 Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobject obj,
110                                                               jint status)
111 {
112     if (!env || !obj)
113     {
114         OIC_LOG(ERROR, TAG, "parameter is null");
115         return;
116     }
117
118     // STATE_ON:12, STATE_OFF:10
119     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Network State Changed");
120
121     if (NULL == g_networkChangeCb)
122     {
123         OIC_LOG_V(DEBUG, TAG, "gNetworkChangeCb is null", status);
124     }
125
126     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
127     if (!jni_cid_BTAdapter)
128     {
129         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
130         return;
131     }
132
133     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
134     if (!id_state_on)
135     {
136         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
137         return;
138     }
139
140     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
141     if (!id_state_off)
142     {
143         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
144         return;
145     }
146
147     jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
148     jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
149
150     if (state_on == status)
151     {
152         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
153         CAEDRServerStartAcceptThread();
154         g_networkChangeCb(newStatus);
155     }
156     else if (state_off == status)
157     {
158         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
159         CAEDRNativeRemoveAllDeviceSocket(env);
160         CAEDRNativeRemoveAllDeviceState(env);
161         g_networkChangeCb(newStatus);
162     }
163 }
164
165 JNIEXPORT void JNICALL
166 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
167                                                                   jstring addr)
168 {
169     if (!env || !obj)
170     {
171         OIC_LOG(ERROR, TAG, "parameter is null");
172         return;
173     }
174
175     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
176
177     if (addr)
178     {
179         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
180         CAEDRNativeRemoveDevice(addr);
181     }
182 }