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 <pthread.h> 28 #include <stdint.h> 29 #include <sys/poll.h> 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 50 // size of the dbus event loops pollfd structure, hopefully never to be grown 51 #define DEFAULT_INITIAL_POLLFD_COUNT 8 52 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; 71 const char *adapter; 72 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; 91 }; 92 93 struct _Properties { 94 char name[32]; 95 int type; 96 }; 97 typedef struct _Properties Properties; 98 99 dbus_bool_t dbus_func_args_async(JNIEnv *env, 100 DBusConnection *conn, 101 int timeout_ms, 102 void (*reply)(DBusMessage *, void *, void *), 103 void *user, 104 void *nat, 105 const char *path, 106 const char *ifc, 107 const char *func, 108 int first_arg_type, 109 ...); 110 111 DBusMessage * dbus_func_args(JNIEnv *env, 112 DBusConnection *conn, 113 const char *path, 114 const char *ifc, 115 const char *func, 116 int first_arg_type, 117 ...); 118 119 DBusMessage * dbus_func_args_error(JNIEnv *env, 120 DBusConnection *conn, 121 DBusError *err, 122 const char *path, 123 const char *ifc, 124 const char *func, 125 int first_arg_type, 126 ...); 127 128 DBusMessage * dbus_func_args_timeout(JNIEnv *env, 129 DBusConnection *conn, 130 int timeout_ms, 131 const char *path, 132 const char *ifc, 133 const char *func, 134 int first_arg_type, 135 ...); 136 137 DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env, 138 DBusConnection *conn, 139 int timeout_ms, 140 DBusError *err, 141 const char *path, 142 const char *ifc, 143 const char *func, 144 int first_arg_type, 145 va_list args); 146 147 jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply); 148 jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply); 149 jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply); 150 jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply); 151 jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply); 152 jobjectArray dbus_returns_array_of_object_path(JNIEnv *env, DBusMessage *reply); 153 jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply); 154 155 jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties, 156 const int max_num_properties); 157 jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg, 158 Properties *properties, int max_num_properties); 159 jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter); 160 jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter); 161 jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg); 162 jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg); 163 void append_variant(DBusMessageIter *iter, int type, void *val); 164 int get_bdaddr(const char *str, bdaddr_t *ba); 165 void get_bdaddr_as_string(const bdaddr_t *ba, char *str); 166 167 bool debug_no_encrypt(); 168 169 #endif 170 } /* namespace android */ 171 172 #endif/*ANDROID_BLUETOOTH_COMMON_H*/ 173