blob: ef9b66bec7bdc9d28d716c4798c087d1951c94b6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Greenwalt28d139f2009-04-02 22:41:08 -070027#include <pthread.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <stdint.h>
Robert Greenwalt28d139f2009-04-02 22:41:08 -070029#include <sys/poll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#ifdef HAVE_BLUETOOTH
32#include <dbus/dbus.h>
33#include <bluetooth/bluetooth.h>
34#endif
35
36namespace 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 Greenwalt28d139f2009-04-02 22:41:08 -070050// 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 Project9066cfe2009-03-03 19:31:44 -080053jfieldID 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
69struct event_loop_native_data_t {
70 DBusConnection *conn;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070071 const char *adapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Robert Greenwalt28d139f2009-04-02 22:41:08 -070073 /* 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};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070093struct _Properties {
94 char name[32];
95 int type;
96};
97typedef struct _Properties Properties;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099dbus_bool_t dbus_func_args_async(JNIEnv *env,
100 DBusConnection *conn,
101 int timeout_ms,
Robert Greenwalt28d139f2009-04-02 22:41:08 -0700102 void (*reply)(DBusMessage *, void *, void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 void *user,
Robert Greenwalt28d139f2009-04-02 22:41:08 -0700104 void *nat,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 const char *path,
106 const char *ifc,
107 const char *func,
108 int first_arg_type,
109 ...);
110
111DBusMessage * 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
119DBusMessage * 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
128DBusMessage * 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
137DBusMessage * 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
147jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply);
148jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply);
149jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply);
150jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply);
151jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700152jobjectArray dbus_returns_array_of_object_path(JNIEnv *env, DBusMessage *reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply);
154
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700155jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties,
156 const int max_num_properties);
157jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg,
158 Properties *properties, int max_num_properties);
159jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter);
160jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter);
161jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg);
162jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg);
163void append_variant(DBusMessageIter *iter, int type, void *val);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700164int get_bdaddr(const char *str, bdaddr_t *ba);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165void get_bdaddr_as_string(const bdaddr_t *ba, char *str);
166
167bool debug_no_encrypt();
168
169#endif
170} /* namespace android */
171
172#endif/*ANDROID_BLUETOOTH_COMMON_H*/