Home | History | Annotate | Download | only in jni
      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 #define BLUEZ_ERROR_IFC           "org.bluez.Error"
     42 
     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 
     51 // size of the dbus event loops pollfd structure, hopefully never to be grown
     52 #define DEFAULT_INITIAL_POLLFD_COUNT 8
     53 
     54 jfieldID get_field(JNIEnv *env,
     55                    jclass clazz,
     56                    const char *member,
     57                    const char *mtype);
     58 
     59 // ALOGE 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     {   ALOGE("%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     {   ALOGE("%s: D-Bus error: %s (%s)", __FUNCTION__, \
     67         (err)->name, (err)->message); \
     68         dbus_error_free((err)); }
     69 
     70 struct event_loop_native_data_t {
     71     DBusConnection *conn;
     72     const char *adapter;
     73 
     74     /* 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;
     92     /* flag to indicate if the event loop thread is running */
     93     bool running;
     94 };
     95 
     96 struct _Properties {
     97     char name[32];
     98     int type;
     99 };
    100 typedef struct _Properties Properties;
    101 
    102 dbus_bool_t dbus_func_args_async(JNIEnv *env,
    103                                  DBusConnection *conn,
    104                                  int timeout_ms,
    105                                  void (*reply)(DBusMessage *, void *, void *),
    106                                  void *user,
    107                                  void *nat,
    108                                  const char *path,
    109                                  const char *ifc,
    110                                  const char *func,
    111                                  int first_arg_type,
    112                                  ...);
    113 
    114 DBusMessage * 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 
    122 DBusMessage * 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 
    131 DBusMessage * 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 
    140 DBusMessage * 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 
    150 jint dbus_returns_int32(JNIEnv *env, DBusMessage *reply);
    151 jint dbus_returns_uint32(JNIEnv *env, DBusMessage *reply);
    152 jint dbus_returns_unixfd(JNIEnv *env, DBusMessage *reply);
    153 jstring dbus_returns_string(JNIEnv *env, DBusMessage *reply);
    154 jboolean dbus_returns_boolean(JNIEnv *env, DBusMessage *reply);
    155 jobjectArray dbus_returns_array_of_strings(JNIEnv *env, DBusMessage *reply);
    156 jobjectArray dbus_returns_array_of_object_path(JNIEnv *env, DBusMessage *reply);
    157 jbyteArray dbus_returns_array_of_bytes(JNIEnv *env, DBusMessage *reply);
    158 
    159 jobjectArray parse_properties(JNIEnv *env, DBusMessageIter *iter, Properties *properties,
    160                               const int max_num_properties);
    161 jobjectArray parse_property_change(JNIEnv *env, DBusMessage *msg,
    162                                    Properties *properties, int max_num_properties);
    163 jobjectArray parse_adapter_properties(JNIEnv *env, DBusMessageIter *iter);
    164 jobjectArray parse_remote_device_properties(JNIEnv *env, DBusMessageIter *iter);
    165 jobjectArray parse_remote_device_property_change(JNIEnv *env, DBusMessage *msg);
    166 jobjectArray parse_adapter_property_change(JNIEnv *env, DBusMessage *msg);
    167 jobjectArray parse_input_properties(JNIEnv *env, DBusMessageIter *iter);
    168 jobjectArray parse_health_device_properties(JNIEnv *env, DBusMessageIter *iter);
    169 jobjectArray parse_health_channel_properties(JNIEnv *env, DBusMessageIter *iter);
    170 jobjectArray parse_input_property_change(JNIEnv *env, DBusMessage *msg);
    171 jobjectArray parse_pan_property_change(JNIEnv *env, DBusMessage *msg);
    172 jobjectArray parse_health_device_property_change(JNIEnv *env, DBusMessage *msg);
    173 
    174 void append_dict_args(DBusMessage *reply, const char *first_key, ...);
    175 void append_variant(DBusMessageIter *iter, int type, void *val);
    176 int get_bdaddr(const char *str, bdaddr_t *ba);
    177 void get_bdaddr_as_string(const bdaddr_t *ba, char *str);
    178 
    179 bool debug_no_encrypt();
    180 
    181 
    182 // Result codes from Bluez DBus calls
    183 #define BOND_RESULT_ERROR                      -1
    184 #define BOND_RESULT_SUCCESS                     0
    185 #define BOND_RESULT_AUTH_FAILED                 1
    186 #define BOND_RESULT_AUTH_REJECTED               2
    187 #define BOND_RESULT_AUTH_CANCELED               3
    188 #define BOND_RESULT_REMOTE_DEVICE_DOWN          4
    189 #define BOND_RESULT_DISCOVERY_IN_PROGRESS       5
    190 #define BOND_RESULT_AUTH_TIMEOUT                6
    191 #define BOND_RESULT_REPEATED_ATTEMPTS           7
    192 
    193 #define PAN_DISCONNECT_FAILED_NOT_CONNECTED  1000
    194 #define PAN_CONNECT_FAILED_ALREADY_CONNECTED 1001
    195 #define PAN_CONNECT_FAILED_ATTEMPT_FAILED    1002
    196 #define PAN_OPERATION_GENERIC_FAILURE        1003
    197 #define PAN_OPERATION_SUCCESS                1004
    198 
    199 #define INPUT_DISCONNECT_FAILED_NOT_CONNECTED  5000
    200 #define INPUT_CONNECT_FAILED_ALREADY_CONNECTED 5001
    201 #define INPUT_CONNECT_FAILED_ATTEMPT_FAILED    5002
    202 #define INPUT_OPERATION_GENERIC_FAILURE        5003
    203 #define INPUT_OPERATION_SUCCESS                5004
    204 
    205 #define HEALTH_OPERATION_SUCCESS               6000
    206 #define HEALTH_OPERATION_ERROR                 6001
    207 #define HEALTH_OPERATION_INVALID_ARGS          6002
    208 #define HEALTH_OPERATION_GENERIC_FAILURE       6003
    209 #define HEALTH_OPERATION_NOT_FOUND             6004
    210 #define HEALTH_OPERATION_NOT_ALLOWED           6005
    211 
    212 #endif
    213 } /* namespace android */
    214 
    215 #endif/*ANDROID_BLUETOOTH_COMMON_H*/
    216