blob: 3364703d49e954c2bedacfb82cd509759171553f [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"
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -080041#define BLUEZ_ERROR_IFC "org.bluez.Error"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
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 Greenwalt28d139f2009-04-02 22:41:08 -070051// 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 Project9066cfe2009-03-03 19:31:44 -080054jfieldID 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
70struct event_loop_native_data_t {
71 DBusConnection *conn;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070072 const char *adapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Robert Greenwalt28d139f2009-04-02 22:41:08 -070074 /* 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 Ganeshb8aa0372010-03-31 11:23:49 -070092 /* flag to indicate if the event loop thread is running */
93 bool running;
Robert Greenwalt28d139f2009-04-02 22:41:08 -070094};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070096struct _Properties {
97 char name[32];
98 int type;
99};
100typedef struct _Properties Properties;
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102dbus_bool_t dbus_func_args_async(JNIEnv *env,
103 DBusConnection *conn,
104 int timeout_ms,
Robert Greenwalt28d139f2009-04-02 22:41:08 -0700105 void (*reply)(DBusMessage *, void *, void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 void *user,
Robert Greenwalt28d139f2009-04-02 22:41:08 -0700107 void *nat,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 const char *path,
109 const char *ifc,
110 const char *func,
111 int first_arg_type,
112 ...);
113
114DBusMessage * 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
122DBusMessage * 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
131DBusMessage * 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
140DBusMessage * 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
150jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply);
151jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply);
152jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply);
153jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply);
154jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700155jobjectArray dbus_returns_array_of_object_path(JNIEnv *env, DBusMessage *reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply);
157
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700158jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties,
159 const int max_num_properties);
160jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg,
161 Properties *properties, int max_num_properties);
162jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter);
163jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter);
164jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg);
165jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg);
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700166jobjectArray parse_input_properties(JNIEnv *env, DBusMessageIter *iter);
167jobjectArray parse_input_property_change(JNIEnv *env, DBusMessage *msg);
Danica Chang6fdd0c62010-08-11 14:54:43 -0700168jobjectArray parse_pan_property_change(JNIEnv *env, DBusMessage *msg);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700169void append_variant(DBusMessageIter *iter, int type, void *val);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700170int get_bdaddr(const char *str, bdaddr_t *ba);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171void get_bdaddr_as_string(const bdaddr_t *ba, char *str);
172
173bool debug_no_encrypt();
174
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -0800175
176// Result codes from Bluez DBus calls
177#define BOND_RESULT_ERROR -1
178#define BOND_RESULT_SUCCESS 0
179#define BOND_RESULT_AUTH_FAILED 1
180#define BOND_RESULT_AUTH_REJECTED 2
181#define BOND_RESULT_AUTH_CANCELED 3
182#define BOND_RESULT_REMOTE_DEVICE_DOWN 4
183#define BOND_RESULT_DISCOVERY_IN_PROGRESS 5
184#define BOND_RESULT_AUTH_TIMEOUT 6
185#define BOND_RESULT_REPEATED_ATTEMPTS 7
186
187#define PAN_DISCONNECT_FAILED_NOT_CONNECTED 1000
188#define PAN_CONNECT_FAILED_ALREADY_CONNECTED 1001
189#define PAN_CONNECT_FAILED_ATTEMPT_FAILED 1002
190#define PAN_OPERATION_GENERIC_FAILURE 1003
191#define PAN_OPERATION_SUCCESS 1004
192
193#define INPUT_DISCONNECT_FAILED_NOT_CONNECTED 5000
194#define INPUT_CONNECT_FAILED_ALREADY_CONNECTED 5001
195#define INPUT_CONNECT_FAILED_ATTEMPT_FAILED 5002
196#define INPUT_OPERATION_GENERIC_FAILURE 5003
197#define INPUT_OPERATION_SUCCESS 5004
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199#endif
200} /* namespace android */
201
202#endif/*ANDROID_BLUETOOTH_COMMON_H*/