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> |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 27 | #include <pthread.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <stdint.h> |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 29 | #include <sys/poll.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_BLUETOOTH |
| 32 | #include <dbus/dbus.h> |
| 33 | #include <bluetooth/bluetooth.h> |
| 34 | #endif |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | #ifdef HAVE_BLUETOOTH |
| 39 | #define BLUEZ_DBUS_BASE_PATH "/org/bluez" |
| 40 | #define BLUEZ_DBUS_BASE_IFC "org.bluez" |
| 41 | |
| 42 | // It would be nicer to retrieve this from bluez using GetDefaultAdapter, |
| 43 | // but this is only possible when the adapter is up (and hcid is running). |
| 44 | // It is much easier just to hardcode bluetooth adapter to hci0 |
| 45 | #define BLUETOOTH_ADAPTER_HCI_NUM 0 |
| 46 | #define BLUEZ_ADAPTER_OBJECT_NAME BLUEZ_DBUS_BASE_PATH "/hci0" |
| 47 | |
| 48 | #define BTADDR_SIZE 18 // size of BT address character array (including null) |
| 49 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 50 | // size of the dbus event loops pollfd structure, hopefully never to be grown |
| 51 | #define DEFAULT_INITIAL_POLLFD_COUNT 8 |
| 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | jfieldID get_field(JNIEnv *env, |
| 54 | jclass clazz, |
| 55 | const char *member, |
| 56 | const char *mtype); |
| 57 | |
| 58 | // LOGE and free a D-Bus error |
| 59 | // Using #define so that __FUNCTION__ resolves usefully |
| 60 | #define LOG_AND_FREE_DBUS_ERROR_WITH_MSG(err, msg) \ |
| 61 | { LOGE("%s: D-Bus error in %s: %s (%s)", __FUNCTION__, \ |
| 62 | dbus_message_get_member((msg)), (err)->name, (err)->message); \ |
| 63 | dbus_error_free((err)); } |
| 64 | #define LOG_AND_FREE_DBUS_ERROR(err) \ |
| 65 | { LOGE("%s: D-Bus error: %s (%s)", __FUNCTION__, \ |
| 66 | (err)->name, (err)->message); \ |
| 67 | dbus_error_free((err)); } |
| 68 | |
| 69 | struct event_loop_native_data_t { |
| 70 | DBusConnection *conn; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 71 | const char *adapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 73 | /* protects the thread */ |
| 74 | pthread_mutex_t thread_mutex; |
| 75 | pthread_t thread; |
| 76 | /* our comms socket */ |
| 77 | /* mem for the list of sockets to listen to */ |
| 78 | struct pollfd *pollData; |
| 79 | int pollMemberCount; |
| 80 | int pollDataSize; |
| 81 | /* mem for matching set of dbus watch ptrs */ |
| 82 | DBusWatch **watchData; |
| 83 | /* pair of sockets for event loop control, Reader and Writer */ |
| 84 | int controlFdR; |
| 85 | int controlFdW; |
| 86 | /* our vm and env Version for future env generation */ |
| 87 | JavaVM *vm; |
| 88 | int envVer; |
| 89 | /* reference to our java self */ |
| 90 | jobject me; |
Jaikumar Ganesh | b8aa037 | 2010-03-31 11:23:49 -0700 | [diff] [blame^] | 91 | /* flag to indicate if the event loop thread is running */ |
| 92 | bool running; |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 93 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 95 | struct _Properties { |
| 96 | char name[32]; |
| 97 | int type; |
| 98 | }; |
| 99 | typedef struct _Properties Properties; |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | dbus_bool_t dbus_func_args_async(JNIEnv *env, |
| 102 | DBusConnection *conn, |
| 103 | int timeout_ms, |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 104 | void (*reply)(DBusMessage *, void *, void *), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | void *user, |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 106 | void *nat, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | const char *path, |
| 108 | const char *ifc, |
| 109 | const char *func, |
| 110 | int first_arg_type, |
| 111 | ...); |
| 112 | |
| 113 | DBusMessage * dbus_func_args(JNIEnv *env, |
| 114 | DBusConnection *conn, |
| 115 | const char *path, |
| 116 | const char *ifc, |
| 117 | const char *func, |
| 118 | int first_arg_type, |
| 119 | ...); |
| 120 | |
| 121 | DBusMessage * dbus_func_args_error(JNIEnv *env, |
| 122 | DBusConnection *conn, |
| 123 | DBusError *err, |
| 124 | const char *path, |
| 125 | const char *ifc, |
| 126 | const char *func, |
| 127 | int first_arg_type, |
| 128 | ...); |
| 129 | |
| 130 | DBusMessage * dbus_func_args_timeout(JNIEnv *env, |
| 131 | DBusConnection *conn, |
| 132 | int timeout_ms, |
| 133 | const char *path, |
| 134 | const char *ifc, |
| 135 | const char *func, |
| 136 | int first_arg_type, |
| 137 | ...); |
| 138 | |
| 139 | DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env, |
| 140 | DBusConnection *conn, |
| 141 | int timeout_ms, |
| 142 | DBusError *err, |
| 143 | const char *path, |
| 144 | const char *ifc, |
| 145 | const char *func, |
| 146 | int first_arg_type, |
| 147 | va_list args); |
| 148 | |
| 149 | jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply); |
| 150 | jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply); |
| 151 | jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply); |
| 152 | jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply); |
| 153 | jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 154 | jobjectArray dbus_returns_array_of_object_path(JNIEnv *env, DBusMessage *reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply); |
| 156 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 157 | jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties, |
| 158 | const int max_num_properties); |
| 159 | jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg, |
| 160 | Properties *properties, int max_num_properties); |
| 161 | jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter); |
| 162 | jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter); |
| 163 | jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg); |
| 164 | jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg); |
| 165 | void append_variant(DBusMessageIter *iter, int type, void *val); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 166 | int get_bdaddr(const char *str, bdaddr_t *ba); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | void get_bdaddr_as_string(const bdaddr_t *ba, char *str); |
| 168 | |
| 169 | bool debug_no_encrypt(); |
| 170 | |
| 171 | #endif |
| 172 | } /* namespace android */ |
| 173 | |
| 174 | #endif/*ANDROID_BLUETOOTH_COMMON_H*/ |