1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2006-2010 Nokia Corporation 6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> 7 * 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 * 23 */ 24 25 #include <bluetooth/bluetooth.h> 26 #include <bluetooth/sdp.h> 27 #include <bluetooth/hci.h> 28 #include <bluetooth/hci_lib.h> 29 #include <dbus/dbus.h> 30 #include <glib.h> 31 32 #define ADAPTER_INTERFACE "org.bluez.Adapter" 33 34 #define MODE_OFF 0x00 35 #define MODE_CONNECTABLE 0x01 36 #define MODE_DISCOVERABLE 0x02 37 #define MODE_UNKNOWN 0xff 38 39 /* Discover states */ 40 #define STATE_IDLE 0 41 #define STATE_DISCOV 1 42 #define STATE_RESOLVNAME 2 43 #define STATE_SUSPENDED 3 44 45 #define MAX_NAME_LENGTH 248 46 47 /* Invalid SSP passkey value used to indicate negative replies */ 48 #define INVALID_PASSKEY 0xffffffff 49 50 typedef enum { 51 NAME_ANY, 52 NAME_NOT_REQUIRED, /* used by get remote name without name resolving */ 53 NAME_REQUIRED, /* remote name needs be resolved */ 54 NAME_REQUESTED, /* HCI remote name request was sent */ 55 } name_status_t; 56 57 struct btd_adapter; 58 59 struct link_key_info { 60 bdaddr_t bdaddr; 61 unsigned char key[16]; 62 uint8_t type; 63 uint8_t pin_len; 64 }; 65 66 struct remote_dev_info { 67 bdaddr_t bdaddr; 68 int8_t rssi; 69 uint32_t class; 70 char *name; 71 char *alias; 72 dbus_bool_t legacy; 73 name_status_t name_status; 74 gboolean le; 75 char **uuids; 76 size_t uuid_count; 77 GSList *services; 78 uint8_t bdaddr_type; 79 uint8_t flags; 80 }; 81 82 void btd_adapter_start(struct btd_adapter *adapter); 83 84 int btd_adapter_stop(struct btd_adapter *adapter); 85 86 void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode, 87 uint8_t *on_mode, gboolean *pairable); 88 89 struct btd_device *adapter_get_device(DBusConnection *conn, 90 struct btd_adapter *adapter, const char *address); 91 92 struct btd_device *adapter_find_device(struct btd_adapter *adapter, const char *dest); 93 94 void adapter_remove_device(DBusConnection *conn, struct btd_adapter *adapter, 95 struct btd_device *device, 96 gboolean remove_storage); 97 98 int adapter_resolve_names(struct btd_adapter *adapter); 99 100 struct btd_adapter *adapter_create(DBusConnection *conn, int id); 101 gboolean adapter_init(struct btd_adapter *adapter); 102 void adapter_remove(struct btd_adapter *adapter); 103 uint16_t adapter_get_dev_id(struct btd_adapter *adapter); 104 const gchar *adapter_get_path(struct btd_adapter *adapter); 105 void adapter_get_address(struct btd_adapter *adapter, bdaddr_t *bdaddr); 106 void adapter_set_state(struct btd_adapter *adapter, int state); 107 int adapter_get_state(struct btd_adapter *adapter); 108 int adapter_get_discover_type(struct btd_adapter *adapter); 109 struct remote_dev_info *adapter_search_found_devices(struct btd_adapter *adapter, 110 struct remote_dev_info *match); 111 void adapter_update_found_devices(struct btd_adapter *adapter, bdaddr_t *bdaddr, 112 uint32_t class, int8_t rssi, 113 uint8_t *data); 114 int adapter_remove_found_device(struct btd_adapter *adapter, bdaddr_t *bdaddr); 115 void adapter_emit_device_found(struct btd_adapter *adapter, 116 struct remote_dev_info *dev); 117 void adapter_mode_changed(struct btd_adapter *adapter, uint8_t scan_mode); 118 void adapter_update_local_name(struct btd_adapter *adapter, const char *name); 119 void adapter_service_insert(struct btd_adapter *adapter, void *rec); 120 void adapter_service_remove(struct btd_adapter *adapter, void *rec); 121 void btd_adapter_class_changed(struct btd_adapter *adapter, 122 uint32_t new_class); 123 void btd_adapter_pairable_changed(struct btd_adapter *adapter, 124 gboolean pairable); 125 126 struct agent *adapter_get_agent(struct btd_adapter *adapter); 127 void adapter_add_connection(struct btd_adapter *adapter, 128 struct btd_device *device); 129 void adapter_remove_connection(struct btd_adapter *adapter, 130 struct btd_device *device); 131 gboolean adapter_has_discov_sessions(struct btd_adapter *adapter); 132 void adapter_suspend_discovery(struct btd_adapter *adapter); 133 void adapter_resume_discovery(struct btd_adapter *adapter); 134 135 struct btd_adapter *btd_adapter_ref(struct btd_adapter *adapter); 136 void btd_adapter_unref(struct btd_adapter *adapter); 137 138 int btd_adapter_set_class(struct btd_adapter *adapter, uint8_t major, 139 uint8_t minor); 140 141 struct btd_adapter_driver { 142 const char *name; 143 int (*probe) (struct btd_adapter *adapter); 144 void (*remove) (struct btd_adapter *adapter); 145 }; 146 147 typedef void (*service_auth_cb) (DBusError *derr, void *user_data); 148 149 int btd_register_adapter_driver(struct btd_adapter_driver *driver); 150 void btd_unregister_adapter_driver(struct btd_adapter_driver *driver); 151 int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst, 152 const char *uuid, service_auth_cb cb, void *user_data); 153 int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst); 154 155 const char *adapter_any_get_path(void); 156 157 const char *btd_adapter_any_request_path(void); 158 void btd_adapter_any_release_path(void); 159 gboolean adapter_is_pairable(struct btd_adapter *adapter); 160 gboolean adapter_powering_down(struct btd_adapter *adapter); 161 162 int btd_adapter_restore_powered(struct btd_adapter *adapter); 163 int btd_adapter_switch_online(struct btd_adapter *adapter); 164 int btd_adapter_switch_offline(struct btd_adapter *adapter); 165 166 typedef void (*bt_hci_result_t) (uint8_t status, gpointer user_data); 167 168 struct btd_adapter_ops { 169 int (*setup) (void); 170 void (*cleanup) (void); 171 int (*set_powered) (int index, gboolean powered); 172 int (*set_discoverable) (int index, gboolean discoverable); 173 int (*set_pairable) (int index, gboolean pairable); 174 int (*set_limited_discoverable) (int index, gboolean limited); 175 int (*start_discovery) (int index); 176 int (*stop_discovery) (int index); 177 178 int (*resolve_name) (int index, bdaddr_t *bdaddr); 179 int (*cancel_resolve_name) (int index, bdaddr_t *bdaddr); 180 int (*set_name) (int index, const char *name); 181 int (*set_dev_class) (int index, uint8_t major, uint8_t minor); 182 int (*set_fast_connectable) (int index, gboolean enable); 183 int (*read_clock) (int index, bdaddr_t *bdaddr, int which, int timeout, 184 uint32_t *clock, uint16_t *accuracy); 185 int (*read_bdaddr) (int index, bdaddr_t *bdaddr); 186 int (*block_device) (int index, bdaddr_t *bdaddr); 187 int (*unblock_device) (int index, bdaddr_t *bdaddr); 188 int (*get_conn_list) (int index, GSList **conns); 189 int (*read_local_features) (int index, uint8_t *features); 190 int (*disconnect) (int index, bdaddr_t *bdaddr); 191 int (*remove_bonding) (int index, bdaddr_t *bdaddr); 192 int (*pincode_reply) (int index, bdaddr_t *bdaddr, const char *pin, 193 size_t pin_len); 194 int (*confirm_reply) (int index, bdaddr_t *bdaddr, gboolean success); 195 int (*passkey_reply) (int index, bdaddr_t *bdaddr, uint32_t passkey); 196 int (*enable_le) (int index); 197 int (*encrypt_link) (int index, bdaddr_t *bdaddr, bt_hci_result_t cb, 198 gpointer user_data); 199 int (*set_did) (int index, uint16_t vendor, uint16_t product, 200 uint16_t version); 201 int (*add_uuid) (int index, uuid_t *uuid, uint8_t svc_hint); 202 int (*remove_uuid) (int index, uuid_t *uuid); 203 int (*disable_cod_cache) (int index); 204 int (*restore_powered) (int index); 205 int (*load_keys) (int index, GSList *keys, gboolean debug_keys); 206 int (*set_io_capability) (int index, uint8_t io_capability); 207 int (*create_bonding) (int index, bdaddr_t *bdaddr, uint8_t io_cap); 208 int (*cancel_bonding) (int index, bdaddr_t *bdaddr); 209 int (*read_local_oob_data) (int index); 210 int (*add_remote_oob_data) (int index, bdaddr_t *bdaddr, uint8_t *hash, 211 uint8_t *randomizer); 212 int (*remove_remote_oob_data) (int index, bdaddr_t *bdaddr); 213 int (*set_link_timeout) (int index, bdaddr_t *bdaddr, uint32_t num_slots); 214 int (*retry_authentication) (int index, bdaddr_t *bdaddr); 215 }; 216 217 int btd_register_adapter_ops(struct btd_adapter_ops *ops, gboolean priority); 218 void btd_adapter_cleanup_ops(struct btd_adapter_ops *btd_adapter_ops); 219 int adapter_ops_setup(void); 220 221 typedef void (*btd_adapter_powered_cb) (struct btd_adapter *adapter, 222 gboolean powered); 223 void btd_adapter_register_powered_callback(struct btd_adapter *adapter, 224 btd_adapter_powered_cb cb); 225 void btd_adapter_unregister_powered_callback(struct btd_adapter *adapter, 226 btd_adapter_powered_cb cb); 227 228 /* If TRUE, enables fast connectabe, i.e. reduces page scan interval and changes 229 * type. If FALSE, disables fast connectable, i.e. sets page scan interval and 230 * type to default values. Valid for both connectable and discoverable modes. */ 231 int btd_adapter_set_fast_connectable(struct btd_adapter *adapter, 232 gboolean enable); 233 234 int btd_adapter_read_clock(struct btd_adapter *adapter, bdaddr_t *bdaddr, 235 int which, int timeout, uint32_t *clock, 236 uint16_t *accuracy); 237 238 int btd_adapter_block_address(struct btd_adapter *adapter, bdaddr_t *bdaddr); 239 int btd_adapter_unblock_address(struct btd_adapter *adapter, bdaddr_t *bdaddr); 240 241 int btd_adapter_disconnect_device(struct btd_adapter *adapter, 242 bdaddr_t *bdaddr); 243 244 int btd_adapter_remove_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr); 245 246 int btd_adapter_pincode_reply(struct btd_adapter *adapter, bdaddr_t *bdaddr, 247 const char *pin, size_t pin_len); 248 int btd_adapter_confirm_reply(struct btd_adapter *adapter, bdaddr_t *bdaddr, 249 gboolean success); 250 int btd_adapter_passkey_reply(struct btd_adapter *adapter, bdaddr_t *bdaddr, 251 uint32_t passkey); 252 253 int btd_adapter_encrypt_link(struct btd_adapter *adapter, bdaddr_t *bdaddr, 254 bt_hci_result_t cb, gpointer user_data); 255 256 int btd_adapter_retry_authentication(struct btd_adapter *adapter, 257 bdaddr_t *bdaddr); 258 259 int btd_adapter_set_did(struct btd_adapter *adapter, uint16_t vendor, 260 uint16_t product, uint16_t version); 261 262 int adapter_create_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr, 263 uint8_t io_cap); 264 265 int adapter_cancel_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr); 266 267 int btd_adapter_read_local_oob_data(struct btd_adapter *adapter); 268 269 int btd_adapter_add_remote_oob_data(struct btd_adapter *adapter, 270 bdaddr_t *bdaddr, uint8_t *hash, uint8_t *randomizer); 271 272 int btd_adapter_remove_remote_oob_data(struct btd_adapter *adapter, 273 bdaddr_t *bdaddr); 274