1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
25 #include "caedrinterface.h"
26 #include "caedrnwmonitor.h"
28 #include "oic_malloc.h"
29 #include "cathreadpool.h" /* for thread pool */
31 #include "uarraylist.h"
32 #include "caadapterutils.h"
33 #include "caedrserver.h"
34 #include "caedrutils.h"
36 #include "org_iotivity_ca_CaEdrInterface.h"
39 #define TAG PCF("OIC_CA_EDR_MONITOR")
42 static jobject g_context;
43 static CAEDRNetworkStatusCallback g_networkChangeCb = NULL;
45 static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
47 void CAEDRNetworkMonitorJNISetContext()
49 OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJNISetContext");
50 g_context = (jobject) CANativeJNIGetContext();
54 void CAEDRNetworkMonitorJniInit()
56 OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJniInit");
57 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
60 CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
64 return CA_STATUS_FAILED;
68 CAEDRNetworkMonitorJniInit();
69 CANativeJNIGetJavaVM();
75 void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallback)
77 OIC_LOG(DEBUG, TAG, "CAEDRSetNetworkChangeCallback");
78 g_networkChangeCb = networkChangeCallback;
81 void CAEDRTerminateNetworkMonitor(void)
85 CAResult_t CAEDRStartNetworkMonitor()
90 CAResult_t CAEDRStopNetworkMonitor()
95 CAResult_t CAEDRClientSetCallbacks(void)
100 JNIEXPORT void JNICALL
101 Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobject obj,
106 OIC_LOG(ERROR, TAG, "parameter is null");
110 // STATE_ON:12, STATE_OFF:10
111 OIC_LOG(DEBUG, TAG, "CaEdrInterface - Network State Changed");
113 if (NULL == g_networkChangeCb)
115 OIC_LOG(DEBUG, TAG, "g_networkChangeCb is null");
119 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
120 if (!jni_cid_BTAdapter)
122 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
126 jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
129 OIC_LOG(ERROR, TAG, "id_state_on is null");
133 jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
136 OIC_LOG(ERROR, TAG, "id_state_off is null");
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);
143 if (state_on == status)
145 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
147 CAResult_t res = CAEDRStartReceiveThread(false);
148 if (CA_STATUS_OK != res)
150 OIC_LOG(ERROR, TAG, "Failed to CAEDRStartReceiveThread");
153 CAEDRServerStartAcceptThread();
154 g_networkChangeCb(newStatus);
156 else if (state_off == status)
158 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
160 CAEDRNativeSocketCloseToAll(env);
161 CAEDRNativeRemoveAllDeviceState();
162 CAEDRNativeRemoveAllDeviceSocket(env);
163 g_networkChangeCb(newStatus);
167 JNIEXPORT void JNICALL
168 Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, jobject obj,
173 OIC_LOG(ERROR, TAG, "parameter is null");
177 OIC_LOG(DEBUG, TAG, "CaEdrInterface - Bond State Changed");
181 CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
182 CAEDRNativeRemoveDevice(addr);
186 JNIEXPORT void JNICALL
187 Java_org_iotivity_ca_CaEdrInterface_caEdrConnectionStateChangedCallback(JNIEnv *env,
192 if (!env || !obj || !addr)
194 OIC_LOG(ERROR, TAG, "parameter is null");
198 OIC_LOG_V(DEBUG, TAG, "CaEdrInterface - Connection State Changed : %d", isConnected);
202 const char *address = (*env)->GetStringUTFChars(env, addr, NULL);
205 OIC_LOG(ERROR, TAG, "address is null");
209 CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
210 CAEDRNativeRemoveDevice(address);
211 (*env)->ReleaseStringUTFChars(env, addr, address);