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 
enablePanNative(JNIEnv * env,jobject object,jint local_role)174 static jboolean enablePanNative(JNIEnv* env, jobject object, jint local_role) {
175   bt_status_t status = BT_STATUS_FAIL;
176   debug("in");
177   if (sPanIf) status = sPanIf->enable(local_role);
178   debug("out");
179   return status == BT_STATUS_SUCCESS ? JNI_TRUE : JNI_FALSE;
180 }
getPanLocalRoleNative(JNIEnv * env,jobject object)181 static jint getPanLocalRoleNative(JNIEnv* env, jobject object) {
182   debug("in");
183   int local_role = 0;
184   if (sPanIf) local_role = sPanIf->get_local_role();
185   debug("out");
186   return (jint)local_role;
187 }
188 
connectPanNative(JNIEnv * env,jobject object,jbyteArray address,jint src_role,jint dest_role)189 static jboolean connectPanNative(JNIEnv* env, jobject object,
190                                  jbyteArray address, jint src_role,
191                                  jint dest_role) {
192   debug("in");
193   if (!sPanIf) return JNI_FALSE;
194 
195   jbyte* addr = env->GetByteArrayElements(address, NULL);
196   if (!addr) {
197     error("Bluetooth device address null");
198     return JNI_FALSE;
199   }
200 
201   jboolean ret = JNI_TRUE;
202   bt_status_t status = sPanIf->connect((RawAddress*)addr, src_role, dest_role);
203   if (status != BT_STATUS_SUCCESS) {
204     error("Failed PAN channel connection, status: %d", status);
205     ret = JNI_FALSE;
206   }
207   env->ReleaseByteArrayElements(address, addr, 0);
208 
209   return ret;
210 }
211 
disconnectPanNative(JNIEnv * env,jobject object,jbyteArray address)212 static jboolean disconnectPanNative(JNIEnv* env, jobject object,
213                                     jbyteArray address) {
214   if (!sPanIf) return JNI_FALSE;
215 
216   jbyte* addr = env->GetByteArrayElements(address, NULL);
217   if (!addr) {
218     error("Bluetooth device address null");
219     return JNI_FALSE;
220   }
221 
222   jboolean ret = JNI_TRUE;
223   bt_status_t status = sPanIf->disconnect((RawAddress*)addr);
224   if (status != BT_STATUS_SUCCESS) {
225     error("Failed disconnect pan channel, status: %d", status);
226     ret = JNI_FALSE;
227   }
228   env->ReleaseByteArrayElements(address, addr, 0);
229 
230   return ret;
231 }
232 
233 static JNINativeMethod sMethods[] = {
234     {"classInitNative", "()V", (void*)classInitNative},
235     {"initializeNative", "()V", (void*)initializeNative},
236     {"cleanupNative", "()V", (void*)cleanupNative},
237     {"connectPanNative", "([BII)Z", (void*)connectPanNative},
238     {"enablePanNative", "(I)Z", (void*)enablePanNative},
239     {"getPanLocalRoleNative", "()I", (void*)getPanLocalRoleNative},
240     {"disconnectPanNative", "([B)Z", (void*)disconnectPanNative},
241     // TBD cleanup
242 };
243 
register_com_android_bluetooth_pan(JNIEnv * env)244 int register_com_android_bluetooth_pan(JNIEnv* env) {
245   return jniRegisterNativeMethods(env, "com/android/bluetooth/pan/PanService",
246                                   sMethods, NELEM(sMethods));
247 }
248 }
249