Imported Upstream version 0.9.2
[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     // STATE_ON:12, STATE_OFF:10
113     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Network State Changed");
114
115     if (NULL == g_networkChangeCb)
116     {
117         OIC_LOG_V(DEBUG, TAG, "gNetworkChangeCb is null", status);
118     }
119
120     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
121     if (!jni_cid_BTAdapter)
122     {
123         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
124         return;
125     }
126
127     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
128     if (!id_state_on)
129     {
130         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
131         return;
132     }
133
134     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
135     if (!id_state_off)
136     {
137         OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
138         return;
139     }
140
141     jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
142     jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
143
144     if (state_on == status)
145     {
146         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
147         CAEDRServerStartAcceptThread();
148         g_networkChangeCb(newStatus);
149     }
150     else if (state_off == status)
151     {
152         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
153         CAEDRNativeRemoveAllDeviceSocket(env);
154         CAEDRNativeRemoveAllDeviceState(env);
155         g_networkChangeCb(newStatus);
156     }
157 }
158
159 JNIEXPORT void JNICALL
160 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
161                                                                    jstring addr)
162 {
163     OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
164
165     if (addr)
166     {
167         CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
168         CAEDRNativeRemoveDevice(addr);
169     }
170 }