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

Commit 01a6b2ed authored by David Scherba's avatar David Scherba Committed by Steve Kondik
Browse files

Bluetooth: Generalizing common D-Bus helper functions to take destination parameters

Reworked D-Bus helper function calls in Bluetooth service implementations to start
passing destinations.
parent c2f22858
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
/*
** Copyright 2006, The Android Open Source Project
** Copyright (c) 2009, Code Aurora Forum, Inc. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
@@ -82,13 +83,6 @@ jfieldID get_field(JNIEnv *env, jclass clazz, const char *member,
    return field;
}

typedef struct {
    void (*user_cb)(DBusMessage *, void *, void *);
    void *user;
    void *nat;
    JNIEnv *env;
} dbus_async_call_t;

void dbus_func_args_async_callback(DBusPendingCall *call, void *data) {

    dbus_async_call_t *req = (dbus_async_call_t *)data;
@@ -120,6 +114,7 @@ static dbus_bool_t dbus_func_args_async_valist(JNIEnv *env,
                                                        void*),
                                        void *user,
                                        void *nat,
                                        const char *dest,
                                        const char *path,
                                        const char *ifc,
                                        const char *func,
@@ -131,7 +126,7 @@ static dbus_bool_t dbus_func_args_async_valist(JNIEnv *env,
    dbus_bool_t reply = FALSE;

    /* Compose the command */
    msg = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC, path, ifc, func);
    msg = dbus_message_new_method_call(dest, path, ifc, func);

    if (msg == NULL) {
        LOGE("Could not allocate D-Bus message object!");
@@ -163,6 +158,8 @@ static dbus_bool_t dbus_func_args_async_valist(JNIEnv *env,
                                         dbus_func_args_async_callback,
                                         pending,
                                         NULL);
        } else {
            free(pending);
        }
    }

@@ -177,6 +174,7 @@ dbus_bool_t dbus_func_args_async(JNIEnv *env,
                                 void (*reply)(DBusMessage *, void *, void*),
                                 void *user,
                                 void *nat,
                                 const char *dest,
                                 const char *path,
                                 const char *ifc,
                                 const char *func,
@@ -188,7 +186,7 @@ dbus_bool_t dbus_func_args_async(JNIEnv *env,
    ret = dbus_func_args_async_valist(env, conn,
                                      timeout_ms,
                                      reply, user, nat,
                                      path, ifc, func,
                                      dest, path, ifc, func,
                                      first_arg_type, lst);
    va_end(lst);
    return ret;
@@ -204,6 +202,7 @@ DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env,
                                            DBusConnection *conn,
                                            int timeout_ms,
                                            DBusError *err,
                                            const char *dest,
                                            const char *path,
                                            const char *ifc,
                                            const char *func,
@@ -220,7 +219,7 @@ DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env,
    }

    /* Compose the command */
    msg = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC, path, ifc, func);
    msg = dbus_message_new_method_call(dest, path, ifc, func);

    if (msg == NULL) {
        LOGE("Could not allocate D-Bus message object!");
@@ -250,6 +249,7 @@ done:
DBusMessage * dbus_func_args_timeout(JNIEnv *env,
                                     DBusConnection *conn,
                                     int timeout_ms,
                                     const char *dest,
                                     const char *path,
                                     const char *ifc,
                                     const char *func,
@@ -259,7 +259,7 @@ DBusMessage * dbus_func_args_timeout(JNIEnv *env,
    va_list lst;
    va_start(lst, first_arg_type);
    ret = dbus_func_args_timeout_valist(env, conn, timeout_ms, NULL,
                                        path, ifc, func,
                                        dest, path, ifc, func,
                                        first_arg_type, lst);
    va_end(lst);
    return ret;
@@ -267,6 +267,7 @@ DBusMessage * dbus_func_args_timeout(JNIEnv *env,

DBusMessage * dbus_func_args(JNIEnv *env,
                             DBusConnection *conn,
                             const char *dest,
                             const char *path,
                             const char *ifc,
                             const char *func,
@@ -276,7 +277,7 @@ DBusMessage * dbus_func_args(JNIEnv *env,
    va_list lst;
    va_start(lst, first_arg_type);
    ret = dbus_func_args_timeout_valist(env, conn, -1, NULL,
                                        path, ifc, func,
                                        dest, path, ifc, func,
                                        first_arg_type, lst);
    va_end(lst);
    return ret;
@@ -285,6 +286,7 @@ DBusMessage * dbus_func_args(JNIEnv *env,
DBusMessage * dbus_func_args_error(JNIEnv *env,
                                   DBusConnection *conn,
                                   DBusError *err,
                                   const char *dest,
                                   const char *path,
                                   const char *ifc,
                                   const char *func,
@@ -294,7 +296,7 @@ DBusMessage * dbus_func_args_error(JNIEnv *env,
    va_list lst;
    va_start(lst, first_arg_type);
    ret = dbus_func_args_timeout_valist(env, conn, -1, err,
                                        path, ifc, func,
                                        dest, path, ifc, func,
                                        first_arg_type, lst);
    va_end(lst);
    return ret;
+32 −1
Original line number Diff line number Diff line
/*
** Copyright 2006, The Android Open Source Project
** Copyright (c) 2009, Code Aurora Forum, Inc. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
@@ -17,8 +18,10 @@
#ifndef ANDROID_BLUETOOTH_COMMON_H
#define ANDROID_BLUETOOTH_COMMON_H

// Set to 0 to enable verbose bluetooth logging
// Set to 0 to enable verbose, debug, and/or info bluetooth logging
#define LOG_NDEBUG 1
#define LOG_NDDEBUG 1
#define LOG_NIDEBUG 1

#include "jni.h"
#include "utils/Log.h"
@@ -36,9 +39,13 @@
namespace android {

#ifdef HAVE_BLUETOOTH
#define BLUEZ_DBUS_BASE_SVC       "org.bluez"
#define BLUEZ_DBUS_BASE_PATH      "/org/bluez"
#define BLUEZ_DBUS_BASE_IFC       "org.bluez"

#define ANDROID_DBUS_AGENT_BASE_PATH "/android/bluetooth"
#define ANDROID_PASSKEY_AGENT_PATH   ANDROID_DBUS_AGENT_BASE_PATH"/agent"

// It would be nicer to retrieve this from bluez using GetDefaultAdapter,
// but this is only possible when the adapter is up (and hcid is running).
// It is much easier just to hardcode bluetooth adapter to hci0
@@ -50,6 +57,13 @@ namespace android {
// size of the dbus event loops pollfd structure, hopefully never to be grown
#define DEFAULT_INITIAL_POLLFD_COUNT 8

typedef struct {
    void (*user_cb)(DBusMessage *, void *, void *);
    void *user;
    void *nat;
    JNIEnv *env;
} dbus_async_call_t;

jfieldID get_field(JNIEnv *env,
                   jclass clazz,
                   const char *member,
@@ -96,12 +110,25 @@ struct _Properties {
};
typedef struct _Properties Properties;

dbus_bool_t dbus_func_args_async_valist(JNIEnv *env,
                                        DBusConnection *conn,
                                        int timeout_ms,
                                        void (*reply)(DBusMessage *, void *),
                                        void *user,
                                        const char *dest,
                                        const char *path,
                                        const char *ifc,
                                        const char *func,
                                        int first_arg_type,
                                        va_list args);

dbus_bool_t dbus_func_args_async(JNIEnv *env,
                                 DBusConnection *conn,
                                 int timeout_ms,
                                 void (*reply)(DBusMessage *, void *, void *),
                                 void *user,
                                 void *nat,
                                 const char *dest,
                                 const char *path,
                                 const char *ifc,
                                 const char *func,
@@ -110,6 +137,7 @@ dbus_bool_t dbus_func_args_async(JNIEnv *env,

DBusMessage * dbus_func_args(JNIEnv *env,
                             DBusConnection *conn,
                             const char *dest,
                             const char *path,
                             const char *ifc,
                             const char *func,
@@ -119,6 +147,7 @@ DBusMessage * dbus_func_args(JNIEnv *env,
DBusMessage * dbus_func_args_error(JNIEnv *env,
                                   DBusConnection *conn,
                                   DBusError *err,
                                   const char *dest,
                                   const char *path,
                                   const char *ifc,
                                   const char *func,
@@ -128,6 +157,7 @@ DBusMessage * dbus_func_args_error(JNIEnv *env,
DBusMessage * dbus_func_args_timeout(JNIEnv *env,
                                     DBusConnection *conn,
                                     int timeout_ms,
                                     const char *dest,
                                     const char *path,
                                     const char *ifc,
                                     const char *func,
@@ -138,6 +168,7 @@ DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env,
                                            DBusConnection *conn,
                                            int timeout_ms,
                                            DBusError *err,
                                            const char *dest,
                                            const char *path,
                                            const char *ifc,
                                            const char *func,
+6 −2
Original line number Diff line number Diff line
/*
** Copyright 2008, The Android Open Source Project
** Copyright (c) 2009, Code Aurora Forum, Inc. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
@@ -107,7 +108,7 @@ static jobjectArray getSinkPropertiesNative(JNIEnv *env, jobject object,

        const char *c_path = env->GetStringUTFChars(path, NULL);
        reply = dbus_func_args_timeout(env,
                                   nat->conn, -1, c_path,
                                   nat->conn, -1, BLUEZ_DBUS_BASE_SVC, c_path,
                                   "org.bluez.AudioSink", "GetProperties",
                                   DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(path, c_path);
@@ -127,7 +128,6 @@ static jobjectArray getSinkPropertiesNative(JNIEnv *env, jobject object,
    return NULL;
}


static jboolean connectSinkNative(JNIEnv *env, jobject object, jstring path) {
#ifdef HAVE_BLUETOOTH
    LOGV(__FUNCTION__);
@@ -135,6 +135,7 @@ static jboolean connectSinkNative(JNIEnv *env, jobject object, jstring path) {
        const char *c_path = env->GetStringUTFChars(path, NULL);

        bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat,
                                    BLUEZ_DBUS_BASE_SVC,
                                    c_path, "org.bluez.AudioSink", "Connect",
                                    DBUS_TYPE_INVALID);

@@ -153,6 +154,7 @@ static jboolean disconnectSinkNative(JNIEnv *env, jobject object,
        const char *c_path = env->GetStringUTFChars(path, NULL);

        bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat,
                                    BLUEZ_DBUS_BASE_SVC,
                                    c_path, "org.bluez.AudioSink", "Disconnect",
                                    DBUS_TYPE_INVALID);

@@ -170,6 +172,7 @@ static jboolean suspendSinkNative(JNIEnv *env, jobject object,
    if (nat) {
        const char *c_path = env->GetStringUTFChars(path, NULL);
        bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat,
                           BLUEZ_DBUS_BASE_SVC,
                           c_path, "org.bluez.audio.Sink", "Suspend",
                           DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(path, c_path);
@@ -186,6 +189,7 @@ static jboolean resumeSinkNative(JNIEnv *env, jobject object,
    if (nat) {
        const char *c_path = env->GetStringUTFChars(path, NULL);
        bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat,
                           BLUEZ_DBUS_BASE_SVC,
                           c_path, "org.bluez.audio.Sink", "Resume",
                           DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(path, c_path);
+17 −8
Original line number Diff line number Diff line
/*
** Copyright 2008, The Android Open Source Project
** Copyright (c) 2009, Code Aurora Forum, Inc. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
@@ -169,7 +170,7 @@ static DBusHandlerResult event_filter(DBusConnection *conn, DBusMessage *msg,
DBusHandlerResult agent_event_filter(DBusConnection *conn,
                                     DBusMessage *msg,
                                     void *data);
static int register_agent(native_data_t *nat,
static int register_agents(native_data_t *nat,
                           const char *agent_path, const char *capabilities);

static const DBusObjectPathVTable agent_vtable = {
@@ -211,6 +212,7 @@ static jboolean setUpEventLoop(native_data_t *nat) {
            LOG_AND_FREE_DBUS_ERROR(&err);
            return JNI_FALSE;
        }

        dbus_bus_add_match(nat->conn,
                "type='signal',interface='"BLUEZ_DBUS_BASE_IFC".Adapter'",
                &err);
@@ -218,6 +220,7 @@ static jboolean setUpEventLoop(native_data_t *nat) {
            LOG_AND_FREE_DBUS_ERROR(&err);
            return JNI_FALSE;
        }

        dbus_bus_add_match(nat->conn,
                "type='signal',interface='"BLUEZ_DBUS_BASE_IFC".Device'",
                &err);
@@ -225,6 +228,7 @@ static jboolean setUpEventLoop(native_data_t *nat) {
            LOG_AND_FREE_DBUS_ERROR(&err);
            return JNI_FALSE;
        }

        dbus_bus_add_match(nat->conn,
                "type='signal',interface='org.bluez.AudioSink'",
                &err);
@@ -233,9 +237,9 @@ static jboolean setUpEventLoop(native_data_t *nat) {
            return JNI_FALSE;
        }

        const char *agent_path = "/android/bluetooth/agent";
        const char *agent_path = ANDROID_PASSKEY_AGENT_PATH;
        const char *capabilities = "DisplayYesNo";
        if (register_agent(nat, agent_path, capabilities) < 0) {
        if (register_agents(nat, agent_path, capabilities) < 0) {
            dbus_connection_unregister_object_path (nat->conn, agent_path);
            return JNI_FALSE;
        }
@@ -244,7 +248,6 @@ static jboolean setUpEventLoop(native_data_t *nat) {
    return JNI_FALSE;
}


const char * get_adapter_path(DBusConnection *conn) {
    DBusMessage *msg = NULL, *reply = NULL;
    DBusError err;
@@ -300,7 +303,7 @@ failed:
    return NULL;
}

static int register_agent(native_data_t *nat,
static int register_agents(native_data_t *nat,
                           const char * agent_path, const char * capabilities)
{
    DBusMessage *msg, *reply;
@@ -324,6 +327,7 @@ static int register_agent(native_data_t *nat,
              __FUNCTION__);
        return -1;
    }

    dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &agent_path,
                             DBUS_TYPE_STRING, &capabilities,
                             DBUS_TYPE_INVALID);
@@ -340,7 +344,10 @@ static int register_agent(native_data_t *nat,
        return -1;
    }

    LOGV("Registered Passkey Agent Path %s", agent_path);

    dbus_message_unref(reply);

    dbus_connection_flush(nat->conn);

    return 0;
@@ -1200,6 +1207,8 @@ static JNINativeMethod sMethods[] = {
};

int register_android_server_BluetoothEventLoop(JNIEnv *env) {
    LOGV(__FUNCTION__);

    return AndroidRuntime::registerNativeMethods(env,
            "android/server/BluetoothEventLoop", sMethods, NELEM(sMethods));
}
+12 −4
Original line number Diff line number Diff line
/*
** Copyright 2006, The Android Open Source Project
** Copyright (c) 2009, Code Aurora Forum, Inc. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
@@ -14,6 +15,7 @@
** limitations under the License.
*/

#define DBUS_SVC_NAME BLUEZ_DBUS_BASE_SVC
#define DBUS_ADAPTER_IFACE BLUEZ_DBUS_BASE_IFC ".Adapter"
#define DBUS_DEVICE_IFACE BLUEZ_DBUS_BASE_IFC ".Device"
#define LOG_TAG "BluetoothService.cpp"
@@ -309,6 +311,7 @@ static jboolean createPairedDeviceNative(JNIEnv *env, jobject object,
                                        onCreatePairedDeviceResult, // callback
                                        context_address,
                                        eventLoopNat,
                                        DBUS_SVC_NAME,
                                        get_adapter_path(env, object),
                                        DBUS_ADAPTER_IFACE,
                                        "CreatePairedDevice",
@@ -339,7 +342,7 @@ static jint getDeviceServiceChannelNative(JNIEnv *env, jobject object,
        LOGV("... pattern = %s", c_pattern);
        LOGV("... attr_id = %#X", attr_id);
        DBusMessage *reply =
            dbus_func_args(env, nat->conn, c_path,
            dbus_func_args(env, nat->conn, DBUS_SVC_NAME, c_path,
                           DBUS_DEVICE_IFACE, "GetServiceAttributeValue",
                           DBUS_TYPE_STRING, &c_pattern,
                           DBUS_TYPE_UINT16, &attr_id,
@@ -364,7 +367,7 @@ static jboolean cancelDeviceCreationNative(JNIEnv *env, jobject object,
        dbus_error_init(&err);
        LOGV("... address = %s", c_address);
        DBusMessage *reply =
            dbus_func_args_timeout(env, nat->conn, -1,
            dbus_func_args_timeout(env, nat->conn, -1, DBUS_SVC_NAME,
                                   get_adapter_path(env, object),
                                   DBUS_ADAPTER_IFACE, "CancelDeviceCreation",
                                   DBUS_TYPE_STRING, &c_address,
@@ -395,6 +398,7 @@ static jboolean removeDeviceNative(JNIEnv *env, jobject object, jstring object_p
                                        NULL,
                                        NULL,
                                        NULL,
                                        DBUS_SVC_NAME,
                                        get_adapter_path(env, object),
                                        DBUS_ADAPTER_IFACE,
                                        "RemoveDevice",
@@ -558,7 +562,7 @@ static jobjectArray getDevicePropertiesNative(JNIEnv *env, jobject object,

        const char *c_path = env->GetStringUTFChars(path, NULL);
        reply = dbus_func_args_timeout(env,
                                   nat->conn, -1, c_path,
                                   nat->conn, -1, DBUS_SVC_NAME, c_path,
                                   DBUS_DEVICE_IFACE, "GetProperties",
                                   DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(path, c_path);
@@ -596,7 +600,7 @@ static jobjectArray getAdapterPropertiesNative(JNIEnv *env, jobject object) {
        dbus_error_init(&err);

        reply = dbus_func_args_timeout(env,
                                   nat->conn, -1, get_adapter_path(env, object),
                                   nat->conn, -1, DBUS_SVC_NAME, get_adapter_path(env, object),
                                   DBUS_ADAPTER_IFACE, "GetProperties",
                                   DBUS_TYPE_INVALID);
        if (!reply) {
@@ -770,6 +774,7 @@ static jboolean createDeviceNative(JNIEnv *env, jobject object,
                                        onCreateDeviceResult,
                                        context_address,
                                        eventLoopNat,
                                        DBUS_SVC_NAME,
                                        get_adapter_path(env, object),
                                        DBUS_ADAPTER_IFACE,
                                        "CreateDevice",
@@ -805,6 +810,7 @@ static jboolean discoverServicesNative(JNIEnv *env, jobject object,
                                        onDiscoverServicesResult,
                                        context_path,
                                        eventLoopNat,
                                        DBUS_SVC_NAME,
                                        c_path,
                                        DBUS_DEVICE_IFACE,
                                        "DiscoverServices",
@@ -830,6 +836,7 @@ static jint addRfcommServiceRecordNative(JNIEnv *env, jobject object,
        LOGV("... uuid2 = %llX", uuidLsb);
        LOGV("... channel = %d", channel);
        DBusMessage *reply = dbus_func_args(env, nat->conn,
                           DBUS_SVC_NAME,
                           get_adapter_path(env, object),
                           DBUS_ADAPTER_IFACE, "AddRfcommServiceRecord",
                           DBUS_TYPE_STRING, &c_name,
@@ -851,6 +858,7 @@ static jboolean removeServiceRecordNative(JNIEnv *env, jobject object, jint hand
    if (nat) {
        LOGV("... handle = %X", handle);
        DBusMessage *reply = dbus_func_args(env, nat->conn,
                           DBUS_SVC_NAME,
                           get_adapter_path(env, object),
                           DBUS_ADAPTER_IFACE, "RemoveServiceRecord",
                           DBUS_TYPE_UINT32, &handle,