The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | ** Copyright 2006, 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 | #ifndef ANDROID_BLUETOOTH_COMMON_H |
| 18 | #define ANDROID_BLUETOOTH_COMMON_H |
| 19 | |
| 20 | // Set to 0 to enable verbose bluetooth logging |
| 21 | #define LOG_NDEBUG 1 |
| 22 | |
| 23 | #include "jni.h" |
| 24 | #include "utils/Log.h" |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #ifdef HAVE_BLUETOOTH |
| 30 | #include <dbus/dbus.h> |
| 31 | #include <bluetooth/bluetooth.h> |
| 32 | #endif |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | #ifdef HAVE_BLUETOOTH |
| 37 | #define BLUEZ_DBUS_BASE_PATH "/org/bluez" |
| 38 | #define BLUEZ_DBUS_BASE_IFC "org.bluez" |
| 39 | |
| 40 | // It would be nicer to retrieve this from bluez using GetDefaultAdapter, |
| 41 | // but this is only possible when the adapter is up (and hcid is running). |
| 42 | // It is much easier just to hardcode bluetooth adapter to hci0 |
| 43 | #define BLUETOOTH_ADAPTER_HCI_NUM 0 |
| 44 | #define BLUEZ_ADAPTER_OBJECT_NAME BLUEZ_DBUS_BASE_PATH "/hci0" |
| 45 | |
| 46 | #define BTADDR_SIZE 18 // size of BT address character array (including null) |
| 47 | |
| 48 | jfieldID get_field(JNIEnv *env, |
| 49 | jclass clazz, |
| 50 | const char *member, |
| 51 | const char *mtype); |
| 52 | |
| 53 | // LOGE and free a D-Bus error |
| 54 | // Using #define so that __FUNCTION__ resolves usefully |
| 55 | #define LOG_AND_FREE_DBUS_ERROR_WITH_MSG(err, msg) \ |
| 56 | { LOGE("%s: D-Bus error in %s: %s (%s)", __FUNCTION__, \ |
| 57 | dbus_message_get_member((msg)), (err)->name, (err)->message); \ |
| 58 | dbus_error_free((err)); } |
| 59 | #define LOG_AND_FREE_DBUS_ERROR(err) \ |
| 60 | { LOGE("%s: D-Bus error: %s (%s)", __FUNCTION__, \ |
| 61 | (err)->name, (err)->message); \ |
| 62 | dbus_error_free((err)); } |
| 63 | |
| 64 | struct event_loop_native_data_t { |
| 65 | DBusConnection *conn; |
| 66 | /* These variables are set in waitForAndDispatchEventNative() and are |
| 67 | valid only within the scope of this function. At any other time, they |
| 68 | are NULL. */ |
| 69 | jobject me; |
| 70 | JNIEnv *env; |
| 71 | }; |
| 72 | |
| 73 | dbus_bool_t dbus_func_args_async_valist(JNIEnv *env, |
| 74 | DBusConnection *conn, |
| 75 | int timeout_ms, |
| 76 | void (*reply)(DBusMessage *, void *), |
| 77 | void *user, |
| 78 | const char *path, |
| 79 | const char *ifc, |
| 80 | const char *func, |
| 81 | int first_arg_type, |
| 82 | va_list args); |
| 83 | |
| 84 | dbus_bool_t dbus_func_args_async(JNIEnv *env, |
| 85 | DBusConnection *conn, |
| 86 | int timeout_ms, |
| 87 | void (*reply)(DBusMessage *, void *), |
| 88 | void *user, |
| 89 | const char *path, |
| 90 | const char *ifc, |
| 91 | const char *func, |
| 92 | int first_arg_type, |
| 93 | ...); |
| 94 | |
| 95 | DBusMessage * dbus_func_args(JNIEnv *env, |
| 96 | DBusConnection *conn, |
| 97 | const char *path, |
| 98 | const char *ifc, |
| 99 | const char *func, |
| 100 | int first_arg_type, |
| 101 | ...); |
| 102 | |
| 103 | DBusMessage * dbus_func_args_error(JNIEnv *env, |
| 104 | DBusConnection *conn, |
| 105 | DBusError *err, |
| 106 | const char *path, |
| 107 | const char *ifc, |
| 108 | const char *func, |
| 109 | int first_arg_type, |
| 110 | ...); |
| 111 | |
| 112 | DBusMessage * dbus_func_args_timeout(JNIEnv *env, |
| 113 | DBusConnection *conn, |
| 114 | int timeout_ms, |
| 115 | const char *path, |
| 116 | const char *ifc, |
| 117 | const char *func, |
| 118 | int first_arg_type, |
| 119 | ...); |
| 120 | |
| 121 | DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env, |
| 122 | DBusConnection *conn, |
| 123 | int timeout_ms, |
| 124 | DBusError *err, |
| 125 | const char *path, |
| 126 | const char *ifc, |
| 127 | const char *func, |
| 128 | int first_arg_type, |
| 129 | va_list args); |
| 130 | |
| 131 | jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply); |
| 132 | jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply); |
| 133 | jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply); |
| 134 | jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply); |
| 135 | jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply); |
| 136 | jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply); |
| 137 | |
| 138 | void get_bdaddr(const char *str, bdaddr_t *ba); |
| 139 | void get_bdaddr_as_string(const bdaddr_t *ba, char *str); |
| 140 | |
| 141 | bool debug_no_encrypt(); |
| 142 | |
| 143 | #endif |
| 144 | } /* namespace android */ |
| 145 | |
| 146 | #endif/*ANDROID_BLUETOOTH_COMMON_H*/ |