replace : iotivity -> iotivity-sec
[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 "octhread.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("OIC_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     if (!threadPool)
63     {
64         return CA_STATUS_FAILED;
65     }
66     else
67     {
68         CAEDRNetworkMonitorJniInit();
69         CANativeJNIGetJavaVM();
70     }
71
72     return CA_STATUS_OK;
73 }
74
75 void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallback)
76 {
77     OIC_LOG(DEBUG, TAG, "CAEDRSetNetworkChangeCallback");
78     g_networkChangeCb = networkChangeCallback;
79 }
80
81 void CAEDRTerminateNetworkMonitor(void)
82 {
83 }
84
85 CAResult_t CAEDRStartNetworkMonitor()
86 {
87     return CA_STATUS_OK;
88 }
89
90 CAResult_t CAEDRStopNetworkMonitor()
91 {
92     return CA_STATUS_OK;
93 }
94
95 CAResult_t CAEDRClientSetCallbacks(void)
96 {
97     return CA_STATUS_OK;
98 }
99
100 JNIEXPORT void JNICALL
101 Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobject obj,
102                                                               jint status)
103 {
104     if (!env || !obj)
105     {
106         OIC_LOG(ERROR, TAG, "parameter is null");
107         return;
108     }
109
110     // STATE_ON:12, STATE_OFF:10
111     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Network State Changed");
112
113     if (NULL == g_networkChangeCb)
114     {
115         OIC_LOG(DEBUG, TAG, "g_networkChangeCb is null");
116         return;
117     }
118
119     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
120     if (!jni_cid_BTAdapter)
121     {
122         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
123         return;
124     }
125
126     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
127     if (!id_state_on)
128     {
129         OIC_LOG(ERROR, TAG, "id_state_on is null");
130         return;
131     }
132
133     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
134     if (!id_state_off)
135     {
136         OIC_LOG(ERROR, TAG, "id_state_off is null");
137         return;
138     }
139
140     jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
141     jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
142
143     if (state_on == status)
144     {
145         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
146
147         CAResult_t res = CAEDRStartReceiveThread(false);
148         if (CA_STATUS_OK != res)
149         {
150             OIC_LOG(ERROR, TAG, "Failed to CAEDRStartReceiveThread");
151             return;
152         }
153         CAEDRServerStartAcceptThread();
154         g_networkChangeCb(newStatus);
155     }
156     else if (state_off == status)
157     {
158         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
159         CAEDRServerStop();
160         CAEDRNativeSocketCloseToAll(env);
161         CAEDRNativeRemoveAllDeviceState();
162         CAEDRNativeRemoveAllDeviceSocket(env);
163         g_networkChangeCb(newStatus);
164     }
165 }
166
167 JNIEXPORT void JNICALL
168 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
169                                                                   jstring addr)
170 {
171     if (!env || !obj)
172     {
173         OIC_LOG(ERROR, TAG, "parameter is null");
174         return;
175     }
176
177     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
178
179     if (addr)
180     {
181         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
182         CAEDRNativeRemoveDevice(addr);
183     }
184 }
185
186 JNIEXPORT void JNICALL
187 Java_org_iotivity_ca_CaEdrInterface_caEdrConnectionStateChangedCallback(JNIEnv *env,
188                                                                         jobject obj,
189                                                                         jstring addr,
190                                                                         jint isConnected)
191 {
192     if (!env || !obj || !addr)
193     {
194         OIC_LOG(ERROR, TAG, "parameter is null");
195         return;
196     }
197
198     OIC_LOG_V(DEBUG, TAG, "CaEdrInterface - Connection State Changed : %d", isConnected);
199
200     if (!isConnected)
201     {
202         const char *address = (*env)->GetStringUTFChars(env, addr, NULL);
203         if (!address)
204         {
205             OIC_LOG(ERROR, TAG, "address is null");
206             return;
207         }
208
209         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
210         CAEDRNativeRemoveDevice(address);
211         (*env)->ReleaseStringUTFChars(env, addr, address);
212     }
213 }