Merge branch 'master' into ra.to.merge
[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     }
132
133     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
134     if (!jni_cid_BTAdapter)
135     {
136         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
137         return;
138     }
139
140     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
141     if (!id_state_on)
142     {
143         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
144         return;
145     }
146
147     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
148     if (!id_state_off)
149     {
150         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
151         return;
152     }
153
154     jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
155     jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
156
157     if (state_on == status)
158     {
159         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
160         CAEDRServerStartAcceptThread();
161         g_networkChangeCb(newStatus);
162     }
163     else if (state_off == status)
164     {
165         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
166         CAEDRNativeRemoveAllDeviceSocket(env);
167         CAEDRNativeRemoveAllDeviceState(env);
168         g_networkChangeCb(newStatus);
169     }
170 }
171
172 JNIEXPORT void JNICALL
173 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
174                                                                   jstring addr)
175 {
176     if (!env || !obj)
177     {
178         OIC_LOG(ERROR, TAG, "parameter is null");
179         return;
180     }
181
182     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
183
184     if (addr)
185     {
186         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
187         CAEDRNativeRemoveDevice(addr);
188     }
189 }