summaryrefslogtreecommitdiffstats
path: root/jni/com_android_bluetooth_hdp.cpp
blob: 8556e6a89211beb3d02b625a92784aa491ae4ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "BluetoothHealthServiceJni"

#define LOG_NDEBUG 0

#define CHECK_CALLBACK_ENV                                                      \
   if (!checkCallbackThread()) {                                                \
       ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);\
       return;                                                                  \
   }

#include "com_android_bluetooth.h"
#include "hardware/bt_hl.h"
#include "utils/Log.h"
#include "android_runtime/AndroidRuntime.h"

#include <string.h>

namespace android {

static jmethodID method_onAppRegistrationState;
static jmethodID method_onChannelStateChanged;

static const bthl_interface_t *sBluetoothHdpInterface = NULL;
static jobject mCallbacksObj = NULL;
static JNIEnv *sCallbackEnv = NULL;

static bool checkCallbackThread() {
    sCallbackEnv = getCallbackEnv();

    JNIEnv* env = AndroidRuntime::getJNIEnv();
    if (sCallbackEnv != env || sCallbackEnv == NULL) {
        ALOGE("Callback env check fail: env: %p, callback: %p", env, sCallbackEnv);
        return false;
    }
    return true;
}

// Define callback functions
static void app_registration_state_callback(int app_id, bthl_app_reg_state_t state) {
    CHECK_CALLBACK_ENV
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onAppRegistrationState, app_id,
                                 (jint) state);
    checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
}

static void channel_state_callback(int app_id, bt_bdaddr_t *bd_addr, int mdep_cfg_index,
                                   int channel_id, bthl_channel_state_t state, int fd) {
    jbyteArray addr;
    jobject fileDescriptor = NULL;

    CHECK_CALLBACK_ENV
    addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
    if (!addr) {
        ALOGE("Fail to new jbyteArray bd addr for channel state");
        checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
        return;
    }

    // TODO(BT) check if fd is only valid for BTHH_CONN_STATE_CONNECTED state
    if (state == BTHL_CONN_STATE_CONNECTED) {
        fileDescriptor = jniCreateFileDescriptor(sCallbackEnv, fd);
        if (!fileDescriptor) {
            ALOGE("Failed to convert file descriptor, fd: %d", fd);
            sCallbackEnv->DeleteLocalRef(addr);
            return;
        }
    }

    sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr);
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onChannelStateChanged, app_id, addr,
                                 mdep_cfg_index, channel_id, (jint) state, fileDescriptor);
    checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
    sCallbackEnv->DeleteLocalRef(addr);
}

static bthl_callbacks_t sBluetoothHdpCallbacks = {
    sizeof(sBluetoothHdpCallbacks),
    app_registration_state_callback,
    channel_state_callback
};

// Define native functions

static void classInitNative(JNIEnv* env, jclass clazz) {
    int err;
//    const bt_interface_t* btInf;
//    bt_status_t status;

    method_onAppRegistrationState = env->GetMethodID(clazz, "onAppRegistrationState", "(II)V");
    method_onChannelStateChanged = env->GetMethodID(clazz, "onChannelStateChanged",
                                                    "(I[BIIILjava/io/FileDescriptor;)V");

/*
    if ( (btInf = getBluetoothInterface()) == NULL) {
        ALOGE("Bluetooth module is not loaded");
        return;
    }

    if ( (sBluetoothHdpInterface = (bthl_interface_t *)
          btInf->get_profile_interface(BT_PROFILE_HEALTH_ID)) == NULL) {
        ALOGE("Failed to get Bluetooth Handsfree Interface");
        return;
    }

    // TODO(BT) do this only once or
    //          Do we need to do this every time the BT reenables?
    if ( (status = sBluetoothHdpInterface->init(&sBluetoothHdpCallbacks)) != BT_STATUS_SUCCESS) {
        ALOGE("Failed to initialize Bluetooth HDP, status: %d", status);
        sBluetoothHdpInterface = NULL;
        return;
    }
*/

    ALOGI("%s: succeeds", __FUNCTION__);
}

static void initializeNative(JNIEnv *env, jobject object) {
    const bt_interface_t* btInf;
    bt_status_t status;

    if ( (btInf = getBluetoothInterface()) == NULL) {
        ALOGE("Bluetooth module is not loaded");
        return;
    }

    if (sBluetoothHdpInterface !=NULL) {
         ALOGW("Cleaning up Bluetooth Health Interface before initializing...");
         sBluetoothHdpInterface->cleanup();
         sBluetoothHdpInterface = NULL;
    }

    if (mCallbacksObj != NULL) {
         ALOGW("Cleaning up Bluetooth Health callback object");
         env->DeleteGlobalRef(mCallbacksObj);
         mCallbacksObj = NULL;
    }

    if ( (sBluetoothHdpInterface = (bthl_interface_t *)
          btInf->get_profile_interface(BT_PROFILE_HEALTH_ID)) == NULL) {
        ALOGE("Failed to get Bluetooth Health Interface");
        return;
    }

    if ( (status = sBluetoothHdpInterface->init(&sBluetoothHdpCallbacks)) != BT_STATUS_SUCCESS) {
        ALOGE("Failed to initialize Bluetooth HDP, status: %d", status);
        sBluetoothHdpInterface = NULL;
        return;
    }

    mCallbacksObj = env->NewGlobalRef(object);
}

static void cleanupNative(JNIEnv *env, jobject object) {
    const bt_interface_t* btInf;
    bt_status_t status;

    if ( (btInf = getBluetoothInterface()) == NULL) {
        ALOGE("Bluetooth module is not loaded");
        return;
    }

    if (sBluetoothHdpInterface !=NULL) {
        ALOGW("Cleaning up Bluetooth Health Interface...");
        sBluetoothHdpInterface->cleanup();
        sBluetoothHdpInterface = NULL;
    }

    if (mCallbacksObj != NULL) {
        ALOGW("Cleaning up Bluetooth Health object");
        env->DeleteGlobalRef(mCallbacksObj);
        mCallbacksObj = NULL;
    }
}

static jint registerHealthAppNative(JNIEnv *env, jobject object, jint data_type,
                                       jint role, jstring name, jint channel_type) {
    bt_status_t status;
    bthl_mdep_cfg_t mdep_cfg;
    bthl_reg_param_t reg_param;
    int app_id;

    if (!sBluetoothHdpInterface) {
        ALOGE("Failed register health app. No Bluetooth Health Interface available");
        return -1;
    }

    mdep_cfg.mdep_role = (bthl_mdep_role_t) role;
    mdep_cfg.data_type = data_type;
    mdep_cfg.channel_type = (bthl_channel_type_t) channel_type;
    // TODO(BT) pass all the followings in from java instead of reuse name
    mdep_cfg.mdep_description = env->GetStringUTFChars(name, NULL);
    reg_param.application_name = env->GetStringUTFChars(name, NULL);
    reg_param.provider_name = NULL;
    reg_param.srv_name = NULL;
    reg_param.srv_desp = NULL;
    reg_param.number_of_mdeps = 1;
    reg_param.mdep_cfg = &mdep_cfg;

    if ( (status = sBluetoothHdpInterface->register_application(&reg_param, &app_id)) !=
         BT_STATUS_SUCCESS) {
        ALOGE("Failed register health app, status: %d", status);
        return -1;
    }

    env->ReleaseStringUTFChars(name, mdep_cfg.mdep_description);
    env->ReleaseStringUTFChars(name, reg_param.application_name);
    return app_id;
}

static jboolean unregisterHealthAppNative(JNIEnv *env, jobject object, int app_id) {
    bt_status_t status;
    if (!sBluetoothHdpInterface) return JNI_FALSE;

    if ((status = sBluetoothHdpInterface->unregister_application(app_id)) != BT_STATUS_SUCCESS) {
        ALOGE("Failed to unregister app %d, status: %d", app_id, status);
    }
    return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

static jint connectChannelNative(JNIEnv *env, jobject object,
                                 jbyteArray address, jint app_id) {
    bt_status_t status;
    jbyte *addr;
    jint chan_id;
    if (!sBluetoothHdpInterface) return -1;

    addr = env->GetByteArrayElements(address, NULL);
    if (!addr) {
        ALOGE("Bluetooth device address null");
        return -1;
    }

    if ( (status = sBluetoothHdpInterface->connect_channel(app_id, (bt_bdaddr_t *) addr,
                                                           0, &chan_id)) !=
         BT_STATUS_SUCCESS) {
        ALOGE("Failed HDP channel connection, status: %d", status);
        chan_id = -1;
    }
    env->ReleaseByteArrayElements(address, addr, 0);

    return chan_id;
}

static jboolean disconnectChannelNative(JNIEnv *env, jobject object, jint channel_id) {
    bt_status_t status;
    if (!sBluetoothHdpInterface) return JNI_FALSE;

    if ( (status = sBluetoothHdpInterface->destroy_channel(channel_id)) !=
         BT_STATUS_SUCCESS) {
        ALOGE("Failed disconnect health channel, status: %d", status);
        return JNI_FALSE;
    }
    return JNI_TRUE;
}

static JNINativeMethod sMethods[] = {
    {"classInitNative", "()V", (void *) classInitNative},
    {"initializeNative", "()V", (void *) initializeNative},
    {"cleanupNative", "()V", (void *) cleanupNative},
    {"registerHealthAppNative", "(IILjava/lang/String;I)I", (void *) registerHealthAppNative},
    {"unregisterHealthAppNative", "(I)Z", (void *) unregisterHealthAppNative},
    {"connectChannelNative", "([BI)I", (void *) connectChannelNative},
    {"disconnectChannelNative", "(I)Z", (void *) disconnectChannelNative},
};

int register_com_android_bluetooth_hdp(JNIEnv* env)
{
    return jniRegisterNativeMethods(env, "com/android/bluetooth/hdp/HealthService",
                                    sMethods, NELEM(sMethods));
}

}