Merge branch 'master' into resource-encapsulation
[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     if (!threadPool)
65     {
66         return CA_STATUS_FAILED;
67     }
68     else
69     {
70         CAEDRNetworkMonitorJniInit();
71         CANativeJNIGetJavaVM();
72     }
73
74     OIC_LOG(DEBUG, TAG, "OUT");
75     return CA_STATUS_OK;
76 }
77
78 void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallback)
79 {
80     OIC_LOG(DEBUG, TAG, "CAEDRSetNetworkChangeCallback");
81     g_networkChangeCb = networkChangeCallback;
82 }
83
84 void CAEDRTerminateNetworkMonitor(void)
85 {
86     OIC_LOG(DEBUG, TAG, "IN");
87
88     OIC_LOG(DEBUG, TAG, "OUT");
89 }
90
91 CAResult_t CAEDRStartNetworkMonitor()
92 {
93     OIC_LOG(DEBUG, TAG, "IN");
94
95     OIC_LOG(DEBUG, TAG, "OUT");
96     return CA_STATUS_OK;
97 }
98
99 CAResult_t CAEDRStopNetworkMonitor()
100 {
101     OIC_LOG(DEBUG, TAG, "IN");
102
103     OIC_LOG(DEBUG, TAG, "OUT");
104     return CA_STATUS_OK;
105 }
106
107 CAResult_t CAEDRClientSetCallbacks(void)
108 {
109     OIC_LOG(DEBUG, TAG, "IN");
110
111     OIC_LOG(DEBUG, TAG, "OUT");
112     return CA_STATUS_OK;
113 }
114
115 JNIEXPORT void JNICALL
116 Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobject obj,
117                                                               jint status)
118 {
119     if (!env || !obj)
120     {
121         OIC_LOG(ERROR, TAG, "parameter is null");
122         return;
123     }
124
125     // STATE_ON:12, STATE_OFF:10
126     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Network State Changed");
127
128     if (NULL == g_networkChangeCb)
129     {
130         OIC_LOG_V(DEBUG, TAG, "gNetworkChangeCb is null", status);
131         return;
132     }
133
134     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
135     if (!jni_cid_BTAdapter)
136     {
137         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
138         return;
139     }
140
141     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
142     if (!id_state_on)
143     {
144         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
145         return;
146     }
147
148     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
149     if (!id_state_off)
150     {
151         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
152         return;
153     }
154
155     jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
156     jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
157
158     if (state_on == status)
159     {
160         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
161         CAEDRServerStartAcceptThread();
162         g_networkChangeCb(newStatus);
163     }
164     else if (state_off == status)
165     {
166         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
167         CAEDRNativeRemoveAllDeviceSocket(env);
168         CAEDRNativeRemoveAllDeviceState(env);
169         g_networkChangeCb(newStatus);
170     }
171 }
172
173 JNIEXPORT void JNICALL
174 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
175                                                                   jstring addr)
176 {
177     if (!env || !obj)
178     {
179         OIC_LOG(ERROR, TAG, "parameter is null");
180         return;
181     }
182
183     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
184
185     if (addr)
186     {
187         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
188         CAEDRNativeRemoveDevice(addr);
189     }
190 }