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" |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 41 | #define BLUEZ_ERROR_IFC "org.bluez.Error" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | // It would be nicer to retrieve this from bluez using GetDefaultAdapter, |
| 44 | // but this is only possible when the adapter is up (and hcid is running). |
| 45 | // It is much easier just to hardcode bluetooth adapter to hci0 |
| 46 | #define BLUETOOTH_ADAPTER_HCI_NUM 0 |
| 47 | #define BLUEZ_ADAPTER_OBJECT_NAME BLUEZ_DBUS_BASE_PATH "/hci0" |
| 48 | |
| 49 | #define BTADDR_SIZE 18 // size of BT address character array (including null) |
| 50 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 51 | // size of the dbus event loops pollfd structure, hopefully never to be grown |
| 52 | #define DEFAULT_INITIAL_POLLFD_COUNT 8 |
| 53 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | jfieldID get_field(JNIEnv *env, |
| 55 | jclass clazz, |
| 56 | const char *member, |
| 57 | const char *mtype); |
| 58 | |
| 59 | // LOGE and free a D-Bus error |
| 60 | // Using #define so that __FUNCTION__ resolves usefully |
| 61 | #define LOG_AND_FREE_DBUS_ERROR_WITH_MSG(err, msg) \ |
| 62 | { LOGE("%s: D-Bus error in %s: %s (%s)", __FUNCTION__, \ |
| 63 | dbus_message_get_member((msg)), (err)->name, (err)->message); \ |
| 64 | dbus_error_free((err)); } |
| 65 | #define LOG_AND_FREE_DBUS_ERROR(err) \ |
| 66 | { LOGE("%s: D-Bus error: %s (%s)", __FUNCTION__, \ |
| 67 | (err)->name, (err)->message); \ |
| 68 | dbus_error_free((err)); } |
| 69 | |
| 70 | struct event_loop_native_data_t { |
| 71 | DBusConnection *conn; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 72 | const char *adapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 74 | /* protects the thread */ |
| 75 | pthread_mutex_t thread_mutex; |
| 76 | pthread_t thread; |
| 77 | /* our comms socket */ |
| 78 | /* mem for the list of sockets to listen to */ |
| 79 | struct pollfd *pollData; |
| 80 | int pollMemberCount; |
| 81 | int pollDataSize; |
| 82 | /* mem for matching set of dbus watch ptrs */ |
| 83 | DBusWatch **watchData; |
| 84 | /* pair of sockets for event loop control, Reader and Writer */ |
| 85 | int controlFdR; |
| 86 | int controlFdW; |
| 87 | /* our vm and env Version for future env generation */ |
| 88 | JavaVM *vm; |
| 89 | int envVer; |
| 90 | /* reference to our java self */ |
| 91 | jobject me; |
Jaikumar Ganesh | b8aa037 | 2010-03-31 11:23:49 -0700 | [diff] [blame] | 92 | /* flag to indicate if the event loop thread is running */ |
| 93 | bool running; |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 94 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 96 | struct _Properties { |
| 97 | char name[32]; |
| 98 | int type; |
| 99 | }; |
| 100 | typedef struct _Properties Properties; |
| 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | dbus_bool_t dbus_func_args_async(JNIEnv *env, |
| 103 | DBusConnection *conn, |
| 104 | int timeout_ms, |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 105 | void (*reply)(DBusMessage *, void *, void *), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | void *user, |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 107 | void *nat, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | const char *path, |
| 109 | const char *ifc, |
| 110 | const char *func, |
| 111 | int first_arg_type, |
| 112 | ...); |
| 113 | |
| 114 | DBusMessage * dbus_func_args(JNIEnv *env, |
| 115 | DBusConnection *conn, |
| 116 | const char *path, |
| 117 | const char *ifc, |
| 118 | const char *func, |
| 119 | int first_arg_type, |
| 120 | ...); |
| 121 | |
| 122 | DBusMessage * dbus_func_args_error(JNIEnv *env, |
| 123 | DBusConnection *conn, |
| 124 | DBusError *err, |
| 125 | const char *path, |
| 126 | const char *ifc, |
| 127 | const char *func, |
| 128 | int first_arg_type, |
| 129 | ...); |
| 130 | |
| 131 | DBusMessage * dbus_func_args_timeout(JNIEnv *env, |
| 132 | DBusConnection *conn, |
| 133 | int timeout_ms, |
| 134 | const char *path, |
| 135 | const char *ifc, |
| 136 | const char *func, |
| 137 | int first_arg_type, |
| 138 | ...); |
| 139 | |
| 140 | DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env, |
| 141 | DBusConnection *conn, |
| 142 | int timeout_ms, |
| 143 | DBusError *err, |
| 144 | const char *path, |
| 145 | const char *ifc, |
| 146 | const char *func, |
| 147 | int first_arg_type, |
| 148 | va_list args); |
| 149 | |
| 150 | jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply); |
| 151 | jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 152 | jint dbus_returns_unixfd(JNIEnv *env, DBusMessage *reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply); |
| 154 | jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply); |
| 155 | jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 156 | 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] | 157 | jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply); |
| 158 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 159 | jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties, |
| 160 | const int max_num_properties); |
| 161 | jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg, |
| 162 | Properties *properties, int max_num_properties); |
| 163 | jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter); |
| 164 | jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter); |
| 165 | jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg); |
| 166 | jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 167 | jobjectArray parse_input_properties(JNIEnv *env, DBusMessageIter *iter); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 168 | jobjectArray parse_health_device_properties(JNIEnv *env, DBusMessageIter *iter); |
| 169 | jobjectArray parse_health_channel_properties(JNIEnv *env, DBusMessageIter *iter); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 170 | jobjectArray parse_input_property_change(JNIEnv *env, DBusMessage *msg); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 171 | jobjectArray parse_pan_property_change(JNIEnv *env, DBusMessage *msg); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 172 | jobjectArray parse_health_device_property_change(JNIEnv *env, DBusMessage *msg); |
| 173 | |
| 174 | void append_dict_args(DBusMessage *reply, const char *first_key, ...); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 175 | void append_variant(DBusMessageIter *iter, int type, void *val); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 176 | int get_bdaddr(const char *str, bdaddr_t *ba); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | void get_bdaddr_as_string(const bdaddr_t *ba, char *str); |
| 178 | |
| 179 | bool debug_no_encrypt(); |
| 180 | |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 181 | |
| 182 | // Result codes from Bluez DBus calls |
| 183 | #define BOND_RESULT_ERROR -1 |
| 184 | #define BOND_RESULT_SUCCESS 0 |
| 185 | #define BOND_RESULT_AUTH_FAILED 1 |
| 186 | #define BOND_RESULT_AUTH_REJECTED 2 |
| 187 | #define BOND_RESULT_AUTH_CANCELED 3 |
| 188 | #define BOND_RESULT_REMOTE_DEVICE_DOWN 4 |
| 189 | #define BOND_RESULT_DISCOVERY_IN_PROGRESS 5 |
| 190 | #define BOND_RESULT_AUTH_TIMEOUT 6 |
| 191 | #define BOND_RESULT_REPEATED_ATTEMPTS 7 |
| 192 | |
| 193 | #define PAN_DISCONNECT_FAILED_NOT_CONNECTED 1000 |
| 194 | #define PAN_CONNECT_FAILED_ALREADY_CONNECTED 1001 |
| 195 | #define PAN_CONNECT_FAILED_ATTEMPT_FAILED 1002 |
| 196 | #define PAN_OPERATION_GENERIC_FAILURE 1003 |
| 197 | #define PAN_OPERATION_SUCCESS 1004 |
| 198 | |
| 199 | #define INPUT_DISCONNECT_FAILED_NOT_CONNECTED 5000 |
| 200 | #define INPUT_CONNECT_FAILED_ALREADY_CONNECTED 5001 |
| 201 | #define INPUT_CONNECT_FAILED_ATTEMPT_FAILED 5002 |
| 202 | #define INPUT_OPERATION_GENERIC_FAILURE 5003 |
| 203 | #define INPUT_OPERATION_SUCCESS 5004 |
| 204 | |
Jaikumar Ganesh | b5d2d45 | 2011-09-07 14:16:52 -0700 | [diff] [blame] | 205 | #define HEALTH_OPERATION_SUCCESS 6000 |
| 206 | #define HEALTH_OPERATION_ERROR 6001 |
| 207 | #define HEALTH_OPERATION_INVALID_ARGS 6002 |
| 208 | #define HEALTH_OPERATION_GENERIC_FAILURE 6003 |
| 209 | #define HEALTH_OPERATION_NOT_FOUND 6004 |
| 210 | #define HEALTH_OPERATION_NOT_ALLOWED 6005 |
| 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | #endif |
| 213 | } /* namespace android */ |
| 214 | |
| 215 | #endif/*ANDROID_BLUETOOTH_COMMON_H*/ |