Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bbb4110b authored by Casper Bonde's avatar Casper Bonde
Browse files

Obex over L2CAP + SDP search API



Each profile had its own implementation of ObexTransport.
These implementations were implemented very similar, and
could easily be merged into a common implementation.

Additionally it will make it easier to adopt the transport
to support L2CAP.

The SDP functionality is implemented in a way that is scalable,
hence adding new record types is easy.
Intents are currently used to distribute the SDP search results,
as we have observed that the new client side profiles have been
located outside the Bluetooth package.
We strongly recommend to keep all bluetooth profiles within the
Bluetooth package, to acoid the need for exposing all bluetooth
interfaces through the Android framework. For instance this new
SDP create API is internal to Bluetooth, hence cannot be used by
the external profiles - hence they cannot use OBEX over L2CAP.

The SDP search currently supports:
 - MAP both sides
 - OPP server (only the server needs an SDP record)
 - PBAP server (only the server needs SDP record)

The SDP create record currently supports:
 - MAP both sides
 - OPP server
 - PBAP server
The new l2cap sockets introduces a new wrapper class for a rfcomm
and an l2cap socket.
The wrapper design:
 - Creates two accept threads running while bluetooth is
   turned on.
 - When a connection is accepted the owner is asked wether or not
   to accept the connection if rejected, the connection will be
   rejected at obex level, else it will be accepted and the state
   is changed to busy.
 - Any further connections will be rejected at Obex level, until
   the state is changed back to idle.

Notes tor executing the test cases:
Test OBEX using local pipes(no BT) or two bluetooth enabled
devices.

Test SDP using two Bluetooth enabled devices

Start server testcase first on one device, then start
the matching client test on the other device.

You cannot run all tests in one go, as they need to run
on multiple devices.

Edit the test sequesce to add/remove/modify the sequence
of OBEX operations to perform.

Use the new SDP record create interface.

Change-Id: I3941793f9843abf4afa5ffbee40d1d01c118b29b
Signed-off-by: default avatarCasper Bonde <c.bonde@samsung.com>
parent 2e85ea37
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -13,7 +13,8 @@ LOCAL_SRC_FILES:= \
    com_android_bluetooth_hid.cpp \
    com_android_bluetooth_hdp.cpp \
    com_android_bluetooth_pan.cpp \
    com_android_bluetooth_gatt.cpp
    com_android_bluetooth_gatt.cpp \
    com_android_bluetooth_sdp.cpp

LOCAL_C_INCLUDES += \
    $(JNI_H_INCLUDE) \
+2 −0
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ int register_com_android_bluetooth_pan(JNIEnv* env);

int register_com_android_bluetooth_gatt (JNIEnv* env);

int register_com_android_bluetooth_sdp (JNIEnv* env);

}

#endif /* COM_ANDROID_BLUETOOTH_H */
+2 −1
Original line number Diff line number Diff line
@@ -67,7 +67,8 @@ static void btavrcp_remote_features_callback(bt_bdaddr_t* bd_addr, btrc_remote_f
    sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr);
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getRcFeatures, addr, (jint)features);
    checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
    sCallbackEnv->DeleteLocalRef(addr);
    /* TODO: I think we leak the addr object, we should add a
     * sCallbackEnv->DeleteLocalRef(addr) */
}

static void btavrcp_get_play_status_callback() {
+25 −127
Original line number Diff line number Diff line
@@ -17,16 +17,14 @@
#define LOG_TAG "BluetoothServiceJni"
#include "com_android_bluetooth.h"
#include "hardware/bt_sock.h"
#include "hardware/bt_mce.h"
#include "utils/Log.h"
#include "utils/misc.h"
#include "cutils/properties.h"
#include "android_runtime/AndroidRuntime.h"
#include "android_runtime/Log.h"

#include <string.h>
#include <pthread.h>

#include <binder/Parcel.h>
#include <sys/stat.h>
#include <fcntl.h>

@@ -45,12 +43,10 @@ static jmethodID method_discoveryStateChangeCallback;
static jmethodID method_setWakeAlarm;
static jmethodID method_acquireWakeLock;
static jmethodID method_releaseWakeLock;
static jmethodID method_deviceMasInstancesFoundCallback;
static jmethodID method_energyInfo;

static const bt_interface_t *sBluetoothInterface = NULL;
static const btsock_interface_t *sBluetoothSocketInterface = NULL;
static const btmce_interface_t *sBluetoothMceInterface = NULL;
static JNIEnv *callbackEnv = NULL;

static jobject sJniAdapterServiceObj;
@@ -528,12 +524,10 @@ static int acquire_wake_lock_callout(const char *lock_name) {
    JNIEnv *env;
    JavaVM *vm = AndroidRuntime::getJavaVM();
    jint status = vm->GetEnv((void **)&env, JNI_VERSION_1_6);

    if (status != JNI_OK && status != JNI_EDETACHED) {
        ALOGE("%s unable to get environment for JNI call", __func__);
        return BT_STATUS_FAIL;
    }

    if (status == JNI_EDETACHED && vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {
        ALOGE("%s unable to attach thread to VM", __func__);
        return BT_STATUS_FAIL;
@@ -564,12 +558,10 @@ static int release_wake_lock_callout(const char *lock_name) {
        ALOGE("%s unable to get environment for JNI call", __func__);
        return BT_STATUS_FAIL;
    }

    if (status == JNI_EDETACHED && vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {
        ALOGE("%s unable to attach thread to VM", __func__);
        return BT_STATUS_FAIL;
    }

    jboolean ret = JNI_FALSE;
    jstring lock_name_jni = env->NewStringUTF(lock_name);
    if (lock_name_jni) {
@@ -578,11 +570,9 @@ static int release_wake_lock_callout(const char *lock_name) {
    } else {
        ALOGE("%s unable to allocate string: %s", __func__, lock_name);
    }

    if (status == JNI_EDETACHED) {
        vm->DetachCurrentThread();
    }

    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
}

@@ -596,73 +586,6 @@ static void alarmFiredNative(JNIEnv *env, jobject obj) {
    }
}

static void remote_mas_instances_callback(bt_status_t status, bt_bdaddr_t *bd_addr,
                                          int num_instances, btmce_mas_instance_t *instances)
{
    if (!checkCallbackThread()) {
       ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);
       return;
    }

    ALOGV("%s: Status is: %d, Instances: %d", __FUNCTION__, status, num_instances);

    if (status != BT_STATUS_SUCCESS) {
        ALOGE("%s: Status %d is incorrect", __FUNCTION__, status);
        return;
    }

    callbackEnv->PushLocalFrame(ADDITIONAL_NREFS);

    jbyteArray addr = NULL;
    jobjectArray a_name = NULL;
    jintArray a_scn = NULL;
    jintArray a_masid = NULL;
    jintArray a_msgtype = NULL;
    jclass mclass;

    mclass = callbackEnv->FindClass("java/lang/String");

    addr = callbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
    if (addr == NULL) goto clean;

    callbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*)bd_addr);

    a_name = callbackEnv->NewObjectArray(num_instances, mclass, NULL);
    if (a_name == NULL) goto clean;

    a_scn = callbackEnv->NewIntArray(num_instances);
    if (a_scn == NULL) goto clean;

    a_masid = callbackEnv->NewIntArray(num_instances);
    if (a_masid == NULL) goto clean;

    a_msgtype = callbackEnv->NewIntArray(num_instances);
    if (a_msgtype == NULL) goto clean;

    for (int i = 0; i < num_instances; i++) {
        jstring name = callbackEnv->NewStringUTF(instances[i].p_name);

        callbackEnv->SetObjectArrayElement(a_name, i, name);
        callbackEnv->SetIntArrayRegion(a_scn, i, 1, &instances[i].scn);
        callbackEnv->SetIntArrayRegion(a_masid, i, 1, &instances[i].id);
        callbackEnv->SetIntArrayRegion(a_msgtype, i, 1, &instances[i].msg_types);

        callbackEnv->DeleteLocalRef(name);
    }

    callbackEnv->CallVoidMethod(sJniCallbacksObj, method_deviceMasInstancesFoundCallback,
            (jint) status, addr, a_name, a_scn, a_masid, a_msgtype);
    checkAndClearExceptionFromCallback(callbackEnv, __FUNCTION__);

clean:
    if (addr != NULL) callbackEnv->DeleteLocalRef(addr);
    if (a_name != NULL) callbackEnv->DeleteLocalRef(a_name);
    if (a_scn != NULL) callbackEnv->DeleteLocalRef(a_scn);
    if (a_masid != NULL) callbackEnv->DeleteLocalRef(a_masid);
    if (a_msgtype != NULL) callbackEnv->DeleteLocalRef(a_msgtype);
    callbackEnv->PopLocalFrame(NULL);
}

static bt_os_callouts_t sBluetoothOsCallouts = {
    sizeof(sBluetoothOsCallouts),
    set_wake_alarm_callout,
@@ -670,10 +593,7 @@ static bt_os_callouts_t sBluetoothOsCallouts = {
    release_wake_lock_callout,
};

static btmce_callbacks_t sBluetoothMceCallbacks = {
    sizeof(sBluetoothMceCallbacks),
    remote_mas_instances_callback,
};


static void classInitNative(JNIEnv* env, jclass clazz) {
    int err;
@@ -710,9 +630,6 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
    method_setWakeAlarm = env->GetMethodID(clazz, "setWakeAlarm", "(JZ)Z");
    method_acquireWakeLock = env->GetMethodID(clazz, "acquireWakeLock", "(Ljava/lang/String;)Z");
    method_releaseWakeLock = env->GetMethodID(clazz, "releaseWakeLock", "(Ljava/lang/String;)Z");
    method_deviceMasInstancesFoundCallback = env->GetMethodID(jniCallbackClass,
                                                    "deviceMasInstancesFoundCallback",
                                                    "(I[B[Ljava/lang/String;[I[I[I)V");
    method_energyInfo = env->GetMethodID(clazz, "energyInfoCallback", "(IIJJJJ)V");

    char value[PROPERTY_VALUE_MAX];
@@ -762,16 +679,6 @@ static bool initNative(JNIEnv* env, jobject obj) {
                ALOGE("Error getting socket interface");
        }

        if ( (sBluetoothMceInterface = (btmce_interface_t *)
                  sBluetoothInterface->get_profile_interface(BT_PROFILE_MAP_CLIENT_ID)) == NULL) {
                ALOGE("Error getting mapclient interface");
        } else {
            if ( (sBluetoothMceInterface->init(&sBluetoothMceCallbacks)) != BT_STATUS_SUCCESS) {
                ALOGE("Failed to initialize Bluetooth MCE");
                sBluetoothMceInterface = NULL;
            }
        }

        return JNI_TRUE;
    }
    return JNI_FALSE;
@@ -1090,25 +997,6 @@ static jboolean getRemoteServicesNative(JNIEnv *env, jobject obj, jbyteArray add
    return result;
}

static jboolean getRemoteMasInstancesNative(JNIEnv *env, jobject obj, jbyteArray address) {
    ALOGV("%s:",__FUNCTION__);

    jbyte *addr = NULL;
    jboolean result = JNI_FALSE;
    if (!sBluetoothMceInterface) return result;

    addr = env->GetByteArrayElements(address, NULL);
    if (addr == NULL) {
        jniThrowIOException(env, EINVAL);
        return result;
    }

    int ret = sBluetoothMceInterface->get_remote_mas_instances((bt_bdaddr_t *)addr);
    env->ReleaseByteArrayElements(address, addr, NULL);
    result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
    return result;
}

static int connectSocketNative(JNIEnv *env, jobject object, jbyteArray address, jint type,
                                   jbyteArray uuidObj, jint channel, jint flag) {
    jbyte *addr = NULL, *uuid = NULL;
@@ -1123,11 +1011,13 @@ static int connectSocketNative(JNIEnv *env, jobject object, jbyteArray address,
        goto Fail;
    }

    if(uuidObj != NULL) {
        uuid = env->GetByteArrayElements(uuidObj, NULL);
        if (!uuid) {
            ALOGE("failed to get uuid");
            goto Fail;
        }
    }

    if ( (status = sBluetoothSocketInterface->connect((bt_bdaddr_t *) addr, (btsock_type_t) type,
                       (const uint8_t*) uuid, channel, &socket_fd, flag)) != BT_STATUS_SUCCESS) {
@@ -1137,7 +1027,7 @@ static int connectSocketNative(JNIEnv *env, jobject object, jbyteArray address,


    if (socket_fd < 0) {
        ALOGE("Fail to creat file descriptor on socket fd");
        ALOGE("Fail to create file descriptor on socket fd");
        goto Fail;
    }
    env->ReleaseByteArrayElements(address, addr, 0);
@@ -1153,7 +1043,7 @@ Fail:

static int createSocketChannelNative(JNIEnv *env, jobject object, jint type,
                                     jstring name_str, jbyteArray uuidObj, jint channel, jint flag) {
    const char *service_name;
    const char *service_name = NULL;
    jbyte *uuid = NULL;
    int socket_fd;
    bt_status_t status;
@@ -1162,13 +1052,17 @@ static int createSocketChannelNative(JNIEnv *env, jobject object, jint type,

    ALOGV("%s: SOCK FLAG = %x", __FUNCTION__, flag);

    if(name_str != NULL) {
        service_name = env->GetStringUTFChars(name_str, NULL);
    }

    if(uuidObj != NULL) {
        uuid = env->GetByteArrayElements(uuidObj, NULL);
        if (!uuid) {
            ALOGE("failed to get uuid");
            goto Fail;
        }
    }
    if ( (status = sBluetoothSocketInterface->listen((btsock_type_t) type, service_name,
                       (const uint8_t*) uuid, channel, &socket_fd, flag)) != BT_STATUS_SUCCESS) {
        ALOGE("Socket listen failed: %d", status);
@@ -1186,7 +1080,6 @@ static int createSocketChannelNative(JNIEnv *env, jobject object, jint type,
Fail:
    if (service_name) env->ReleaseStringUTFChars(name_str, service_name);
    if (uuid) env->ReleaseByteArrayElements(uuidObj, uuid, 0);

    return -1;
}

@@ -1235,7 +1128,6 @@ static JNINativeMethod sMethods[] = {
    {"pinReplyNative", "([BZI[B)Z", (void*) pinReplyNative},
    {"sspReplyNative", "([BIZI)Z", (void*) sspReplyNative},
    {"getRemoteServicesNative", "([B)Z", (void*) getRemoteServicesNative},
    {"getRemoteMasInstancesNative", "([B)Z", (void*) getRemoteMasInstancesNative},
    {"connectSocketNative", "([BI[BII)I", (void*) connectSocketNative},
    {"createSocketChannelNative", "(ILjava/lang/String;[BII)I",
     (void*) createSocketChannelNative},
@@ -1323,5 +1215,11 @@ jint JNI_OnLoad(JavaVM *jvm, void *reserved)
        ALOGE("jni gatt registration failure: %d", status);
        return JNI_ERR;
    }

    if ((status = android::register_com_android_bluetooth_sdp(e)) < 0) {
        ALOGE("jni sdp registration failure: %d", status);
        return JNI_ERR;
    }

    return JNI_VERSION_1_6;
}
+513 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading