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