blob: 69092ddb028df7ee1e8252120339ad482d128566 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
Robert Greenwalt28d139f2009-04-02 22:41:08 -070072 /* protects the thread */
73 pthread_mutex_t thread_mutex;
74 pthread_t thread;
75 /* our comms socket */
76 /* mem for the list of sockets to listen to */
77 struct pollfd *pollData;
78 int pollMemberCount;
79 int pollDataSize;
80 /* mem for matching set of dbus watch ptrs */
81 DBusWatch **watchData;
82 /* pair of sockets for event loop control, Reader and Writer */
83 int controlFdR;
84 int controlFdW;
85 /* our vm and env Version for future env generation */
86 JavaVM *vm;
87 int envVer;
88 /* reference to our java self */
89 jobject me;
90};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92dbus_bool_t dbus_func_args_async(JNIEnv *env,
93 DBusConnection *conn,
94 int timeout_ms,
Robert Greenwalt28d139f2009-04-02 22:41:08 -070095 void (*reply)(DBusMessage *, void *, void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 void *user,
Robert Greenwalt28d139f2009-04-02 22:41:08 -070097 void *nat,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 const char *path,
99 const char *ifc,
100 const char *func,
101 int first_arg_type,
102 ...);
103
104DBusMessage * dbus_func_args(JNIEnv *env,
105 DBusConnection *conn,
106 const char *path,
107 const char *ifc,
108 const char *func,
109 int first_arg_type,
110 ...);
111
112DBusMessage * dbus_func_args_error(JNIEnv *env,
113 DBusConnection *conn,
114 DBusError *err,
115 const char *path,
116 const char *ifc,
117 const char *func,
118 int first_arg_type,
119 ...);
120
121DBusMessage * dbus_func_args_timeout(JNIEnv *env,
122 DBusConnection *conn,
123 int timeout_ms,
124 const char *path,
125 const char *ifc,
126 const char *func,
127 int first_arg_type,
128 ...);
129
130DBusMessage * dbus_func_args_timeout_valist(JNIEnv *env,
131 DBusConnection *conn,
132 int timeout_ms,
133 DBusError *err,
134 const char *path,
135 const char *ifc,
136 const char *func,
137 int first_arg_type,
138 va_list args);
139
140jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply);
141jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply);
142jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply);
143jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply);
144jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply);
145jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply);
146
147void get_bdaddr(const char *str, bdaddr_t *ba);
148void get_bdaddr_as_string(const bdaddr_t *ba, char *str);
149
150bool debug_no_encrypt();
151
152#endif
153} /* namespace android */
154
155#endif/*ANDROID_BLUETOOTH_COMMON_H*/