1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "BluetoothPanServiceJni"
18
19 #define LOG_NDEBUG 0
20
21 #include "com_android_bluetooth.h"
22 #include "hardware/bt_pan.h"
23 #include "utils/Log.h"
24
25 #include <string.h>
26
27 #include <cutils/log.h>
28 #define info(fmt, ...) ALOGI("%s(L%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
29 #define debug(fmt, ...) \
30 ALOGD("%s(L%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
31 #define warn(fmt, ...) \
32 ALOGW("## WARNING : %s(L%d): " fmt "##", __func__, __LINE__, ##__VA_ARGS__)
33 #define error(fmt, ...) \
34 ALOGE("## ERROR : %s(L%d): " fmt "##", __func__, __LINE__, ##__VA_ARGS__)
35 #define asrt(s) \
36 if (!(s)) ALOGE("## %s(L%d): ASSERT %s failed! ##", __func__, __LINE__, #s)
37
38 namespace android {
39
40 static jmethodID method_onConnectStateChanged;
41 static jmethodID method_onControlStateChanged;
42
43 static const btpan_interface_t* sPanIf = NULL;
44 static jobject mCallbacksObj = NULL;
45
marshall_bda(const RawAddress * bd_addr)46 static jbyteArray marshall_bda(const RawAddress* bd_addr) {
47 CallbackEnv sCallbackEnv(__func__);
48 if (!sCallbackEnv.valid()) return NULL;
49
50 jbyteArray addr = sCallbackEnv->NewByteArray(sizeof(RawAddress));
51 if (!addr) {
52 ALOGE("Fail to new jbyteArray bd addr");
53 return NULL;
54 }
55 sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(RawAddress),
56 (jbyte*)bd_addr);
57 return addr;
58 }
59
control_state_callback(btpan_control_state_t state,int local_role,bt_status_t error,const char * ifname)60 static void control_state_callback(btpan_control_state_t state, int local_role,
61 bt_status_t error, const char* ifname) {
62 debug("state:%d, local_role:%d, ifname:%s", state, local_role, ifname);
63 if (mCallbacksObj == NULL) {
64 error("Callbacks Obj is NULL: '%s", __func__);
65 return;
66 }
67 CallbackEnv sCallbackEnv(__func__);
68 if (!sCallbackEnv.valid()) return;
69 ScopedLocalRef<jstring> js_ifname(sCallbackEnv.get(),
70 sCallbackEnv->NewStringUTF(ifname));
71 sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onControlStateChanged,
72 (jint)local_role, (jint)state, (jint)error,
73 js_ifname.get());
74 }
75
connection_state_callback(btpan_connection_state_t state,bt_status_t error,const RawAddress * bd_addr,int local_role,int remote_role)76 static void connection_state_callback(btpan_connection_state_t state,
77 bt_status_t error,
78 const RawAddress* bd_addr, int local_role,
79 int remote_role) {
80 debug("state:%d, local_role:%d, remote_role:%d", state, local_role,
81 remote_role);
82 if (mCallbacksObj == NULL) {
83 error("Callbacks Obj is NULL: '%s", __func__);
84 return;
85 }
86 CallbackEnv sCallbackEnv(__func__);
87 if (!sCallbackEnv.valid()) return;
88 ScopedLocalRef<jbyteArray> addr(sCallbackEnv.get(), marshall_bda(bd_addr));
89 if (!addr.get()) {
90 error("Fail to new jbyteArray bd addr for PAN channel state");
91 return;
92 }
93 sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onConnectStateChanged,
94 addr.get(), (jint)state, (jint)error,
95 (jint)local_role, (jint)remote_role);
96 }
97
98 static btpan_callbacks_t sBluetoothPanCallbacks = {
99 sizeof(sBluetoothPanCallbacks), control_state_callback,
100 connection_state_callback};
101
102 // Define native functions
103
classInitNative(JNIEnv * env,jclass clazz)104 static void classInitNative(JNIEnv* env, jclass clazz) {
105 method_onConnectStateChanged =
106 env->GetMethodID(clazz, "onConnectStateChanged", "([BIIII)V");
107 method_onControlStateChanged = env->GetMethodID(
108 clazz, "onControlStateChanged", "(IIILjava/lang/String;)V");
109
110 info("succeeds");
111 }
112 static const bt_interface_t* btIf;
113
initializeNative(JNIEnv * env,jobject object)114 static void initializeNative(JNIEnv* env, jobject object) {
115 debug("pan");
116 if (btIf) return;
117
118 btIf = getBluetoothInterface();
119 if (btIf == NULL) {
120 error("Bluetooth module is not loaded");
121 return;
122 }
123
124 if (sPanIf != NULL) {
125 ALOGW("Cleaning up Bluetooth PAN Interface before initializing...");
126 sPanIf->cleanup();
127 sPanIf = NULL;
128 }
129
130 if (mCallbacksObj != NULL) {
131 ALOGW("Cleaning up Bluetooth PAN callback object");
132 env->DeleteGlobalRef(mCallbacksObj);
133 mCallbacksObj = NULL;
134 }
135
136 sPanIf = (btpan_interface_t*)btIf->get_profile_interface(BT_PROFILE_PAN_ID);
137 if (sPanIf == NULL) {
138 error("Failed to get Bluetooth PAN Interface");
139 return;
140 }
141
142 mCallbacksObj = env->NewGlobalRef(object);
143
144 bt_status_t status = sPanIf->init(&sBluetoothPanCallbacks);
145 if (status != BT_STATUS_SUCCESS) {
146 error("Failed to initialize Bluetooth PAN, status: %d", status);
147 sPanIf = NULL;
148 if (mCallbacksObj != NULL) {
149 ALOGW("initialization failed: Cleaning up Bluetooth PAN callback object");
150 env->DeleteGlobalRef(mCallbacksObj);
151 mCallbacksObj = NULL;
152 }
153 return;
154 }
155 }
156
cleanupNative(JNIEnv * env,jobject object)157 static void cleanupNative(JNIEnv* env, jobject object) {
158 if (!btIf) return;
159
160 if (sPanIf != NULL) {
161 ALOGW("Cleaning up Bluetooth PAN Interface...");
162 sPanIf->cleanup();
163 sPanIf = NULL;
164 }
165
166 if (mCallbacksObj != NULL) {
167 ALOGW("Cleaning up Bluetooth PAN callback object");
168 env->DeleteGlobalRef(mCallbacksObj);
169 mCallbacksObj = NULL;
170 }
171 btIf = NULL;
172 }
173
connectPanNative(JNIEnv * env,jobject object,jbyteArray address,jint src_role,jint dest_role)174 static jboolean connectPanNative(JNIEnv* env, jobject object,
175 jbyteArray address, jint src_role,
176 jint dest_role) {
177 debug("in");
178 if (!sPanIf) return JNI_FALSE;
179
180 jbyte* addr = env->GetByteArrayElements(address, NULL);
181 if (!addr) {
182 error("Bluetooth device address null");
183 return JNI_FALSE;
184 }
185
186 jboolean ret = JNI_TRUE;
187 bt_status_t status = sPanIf->connect((RawAddress*)addr, src_role, dest_role);
188 if (status != BT_STATUS_SUCCESS) {
189 error("Failed PAN channel connection, status: %d", status);
190 ret = JNI_FALSE;
191 }
192 env->ReleaseByteArrayElements(address, addr, 0);
193
194 return ret;
195 }
196
disconnectPanNative(JNIEnv * env,jobject object,jbyteArray address)197 static jboolean disconnectPanNative(JNIEnv* env, jobject object,
198 jbyteArray address) {
199 if (!sPanIf) return JNI_FALSE;
200
201 jbyte* addr = env->GetByteArrayElements(address, NULL);
202 if (!addr) {
203 error("Bluetooth device address null");
204 return JNI_FALSE;
205 }
206
207 jboolean ret = JNI_TRUE;
208 bt_status_t status = sPanIf->disconnect((RawAddress*)addr);
209 if (status != BT_STATUS_SUCCESS) {
210 error("Failed disconnect pan channel, status: %d", status);
211 ret = JNI_FALSE;
212 }
213 env->ReleaseByteArrayElements(address, addr, 0);
214
215 return ret;
216 }
217
218 static JNINativeMethod sMethods[] = {
219 {"classInitNative", "()V", (void*)classInitNative},
220 {"initializeNative", "()V", (void*)initializeNative},
221 {"cleanupNative", "()V", (void*)cleanupNative},
222 {"connectPanNative", "([BII)Z", (void*)connectPanNative},
223 {"disconnectPanNative", "([B)Z", (void*)disconnectPanNative},
224 // TBD cleanup
225 };
226
register_com_android_bluetooth_pan(JNIEnv * env)227 int register_com_android_bluetooth_pan(JNIEnv* env) {
228 return jniRegisterNativeMethods(env, "com/android/bluetooth/pan/PanService",
229 sMethods, NELEM(sMethods));
230 }
231 }
232