3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2006-2007 Nokia Corporation
6
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
35
#include <sys/param.h>
36
#include <sys/ioctl.h>
37
#include <sys/socket.h>
39
#include <bluetooth/bluetooth.h>
40
#include <bluetooth/hci.h>
41
#include <bluetooth/hci_lib.h>
42
#include <bluetooth/sdp.h>
45
#include <dbus/dbus.h>
56
#include "glib-helper.h"
57
#include "dbus-common.h"
61
static DBusConnection *connection = NULL;
63
const char *class_to_icon(uint32_t class)
65
switch ((class & 0x1f00) >> 8) {
69
switch ((class & 0xfc) >> 2) {
80
return "network-wireless";
82
switch ((class & 0xfc) >> 2) {
85
return "audio-card"; /* Headset */
87
return "audio-card"; /* Headphone */
91
switch ((class & 0xc0) >> 6) {
93
switch ((class & 0x1e) >> 2) {
96
return "input-gaming";
100
return "input-keyboard";
102
switch ((class & 0x1e) >> 2) {
104
return "input-tablet";
106
return "input-mouse";
114
return "camera-photo";
121
DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
124
case 0x00: /* success */
125
return dbus_message_new_method_return(msg);
127
case 0x04: /* page timeout */
128
case 0x08: /* connection timeout */
129
case 0x10: /* connection accept timeout */
130
case 0x22: /* LMP response timeout */
131
case 0x28: /* instant passed - is this a timeout? */
132
return dbus_message_new_error(msg,
133
ERROR_INTERFACE ".AuthenticationTimeout",
134
"Authentication Timeout");
135
case 0x17: /* too frequent pairing attempts */
136
return dbus_message_new_error(msg,
137
ERROR_INTERFACE ".RepeatedAttempts",
138
"Repeated Attempts");
141
case 0x18: /* pairing not allowed (e.g. gw rejected attempt) */
142
return dbus_message_new_error(msg,
143
ERROR_INTERFACE ".AuthenticationRejected",
144
"Authentication Rejected");
146
case 0x07: /* memory capacity */
147
case 0x09: /* connection limit */
148
case 0x0a: /* synchronous connection limit */
149
case 0x0d: /* limited resources */
150
case 0x13: /* user ended the connection */
151
case 0x14: /* terminated due to low resources */
152
return dbus_message_new_error(msg,
153
ERROR_INTERFACE ".AuthenticationCanceled",
154
"Authentication Canceled");
156
case 0x05: /* authentication failure */
157
case 0x0E: /* rejected due to security reasons - is this auth failure? */
158
case 0x25: /* encryption mode not acceptable - is this auth failure? */
159
case 0x26: /* link key cannot be changed - is this auth failure? */
160
case 0x29: /* pairing with unit key unsupported - is this auth failure? */
161
case 0x2f: /* insufficient security - is this auth failure? */
163
return dbus_message_new_error(msg,
164
ERROR_INTERFACE ".AuthenticationFailed",
165
"Authentication Failed");
169
/*****************************************************************
171
* Section reserved to HCI commands confirmation handling and low
172
* level events(eg: device attached/dettached.
174
*****************************************************************/
176
static void pincode_cb(struct agent *agent, DBusError *err, const char *pincode,
177
struct btd_device *device)
179
struct btd_adapter *adapter = device_get_adapter(device);
180
pin_code_reply_cp pr;
184
struct pending_auth_info *auth;
185
uint16_t dev_id = adapter_get_dev_id(adapter);
186
struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
188
/* No need to reply anything if the authentication already failed */
189
if (bonding && bonding->hci_status)
192
dev = hci_open_dev(dev_id);
194
error("hci_open_dev(%d): %s (%d)", dev_id,
195
strerror(errno), errno);
199
adapter_get_address(adapter, &sba);
200
device_get_address(device, &dba);
202
auth = adapter_find_auth_request(adapter, &dba);
205
hci_send_cmd(dev, OGF_LINK_CTL,
206
OCF_PIN_CODE_NEG_REPLY, 6, &dba);
210
len = strlen(pincode);
212
set_pin_length(&sba, len);
214
memset(&pr, 0, sizeof(pr));
215
bacpy(&pr.bdaddr, &dba);
216
memcpy(pr.pin_code, pincode, len);
218
hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY, PIN_CODE_REPLY_CP_SIZE, &pr);
222
auth->replied = TRUE;
228
int hcid_dbus_request_pin(int dev, bdaddr_t *sba, struct hci_conn_info *ci)
231
struct btd_adapter *adapter;
232
struct btd_device *device;
233
struct agent *agent = NULL;
236
adapter = manager_find_adapter(sba);
238
error("No matching adapter found");
242
ba2str(&ci->bdaddr, addr);
244
device = adapter_find_device(adapter, addr);
247
agent = device_get_agent(device);
250
agent = adapter_get_agent(adapter);
256
device = adapter_create_device(connection, adapter, addr);
261
ret = agent_request_pincode(agent, device,
262
(agent_pincode_cb) pincode_cb,
265
struct pending_auth_info *auth;
266
auth = adapter_new_auth_request(adapter, &ci->bdaddr,
274
static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
276
struct btd_device *device = user_data;
277
struct btd_adapter *adapter = device_get_adapter(device);
278
user_confirm_reply_cp cp;
280
struct pending_auth_info *auth;
281
uint16_t dev_id = adapter_get_dev_id(adapter);
282
struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
284
/* No need to reply anything if the authentication already failed */
285
if (bonding && bonding->hci_status)
288
dd = hci_open_dev(dev_id);
290
error("Unable to open hci%d", dev_id);
294
memset(&cp, 0, sizeof(cp));
295
device_get_address(device, &cp.bdaddr);
297
auth = adapter_find_auth_request(adapter, &cp.bdaddr);
300
hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
301
USER_CONFIRM_REPLY_CP_SIZE, &cp);
303
hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_REPLY,
304
USER_CONFIRM_REPLY_CP_SIZE, &cp);
307
auth->replied = TRUE;
314
static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
317
struct btd_device *device = user_data;
318
struct btd_adapter *adapter = device_get_adapter(device);
319
user_passkey_reply_cp cp;
322
struct pending_auth_info *auth;
323
uint16_t dev_id = adapter_get_dev_id(adapter);
324
struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
326
/* No need to reply anything if the authentication already failed */
327
if (bonding && bonding->hci_status)
330
dd = hci_open_dev(dev_id);
332
error("Unable to open hci%d", dev_id);
336
device_get_address(device, &dba);
338
memset(&cp, 0, sizeof(cp));
339
bacpy(&cp.bdaddr, &dba);
340
cp.passkey = passkey;
342
auth = adapter_find_auth_request(adapter, &dba);
345
hci_send_cmd(dd, OGF_LINK_CTL,
346
OCF_USER_PASSKEY_NEG_REPLY, 6, &dba);
348
hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_REPLY,
349
USER_PASSKEY_REPLY_CP_SIZE, &cp);
352
auth->replied = TRUE;
359
static int get_auth_requirements(bdaddr_t *local, bdaddr_t *remote,
362
struct hci_auth_info_req req;
368
dev_id = hci_devid(addr);
372
dd = hci_open_dev(dev_id);
376
memset(&req, 0, sizeof(req));
377
bacpy(&req.bdaddr, remote);
379
err = ioctl(dd, HCIGETAUTHINFO, (unsigned long) &req);
381
debug("HCIGETAUTHINFO failed: %s (%d)",
382
strerror(errno), errno);
395
int hcid_dbus_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
397
struct btd_adapter *adapter;
398
struct btd_device *device;
402
struct pending_auth_info *auth;
405
adapter = manager_find_adapter(sba);
407
error("No matching adapter found");
411
dev_id = adapter_get_dev_id(adapter);
413
if (get_auth_requirements(sba, dba, &type) < 0) {
416
dd = hci_open_dev(dev_id);
418
error("Unable to open hci%d", dev_id);
422
hci_send_cmd(dd, OGF_LINK_CTL,
423
OCF_USER_CONFIRM_NEG_REPLY, 6, dba);
432
device = adapter_get_device(connection, adapter, addr);
434
error("Device creation failed");
438
/* If no MITM protection required, auto-accept */
439
if (!(device_get_auth(device) & 0x01) && !(type & 0x01)) {
442
dd = hci_open_dev(dev_id);
444
error("Unable to open hci%d", dev_id);
448
hci_send_cmd(dd, OGF_LINK_CTL,
449
OCF_USER_CONFIRM_REPLY, 6, dba);
456
agent = device_get_agent(device);
459
agent = adapter_get_agent(adapter);
462
error("No agent available for user confirm request");
466
if (agent_request_confirmation(agent, device, passkey,
467
confirm_cb, device) < 0) {
468
error("Requesting passkey failed");
472
auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_CONFIRM);
478
int hcid_dbus_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
480
struct btd_adapter *adapter;
481
struct btd_device *device;
482
struct agent *agent = NULL;
484
struct pending_auth_info *auth;
486
adapter = manager_find_adapter(sba);
488
error("No matching adapter found");
494
device = adapter_get_device(connection, adapter, addr);
497
agent = device_get_agent(device);
500
agent = adapter_get_agent(adapter);
503
error("No agent available for user confirm request");
507
if (agent_request_passkey(agent, device, passkey_cb, device) < 0) {
508
error("Requesting passkey failed");
512
auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_PASSKEY);
518
int hcid_dbus_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
520
struct btd_adapter *adapter;
521
struct btd_device *device;
522
struct agent *agent = NULL;
524
struct pending_auth_info *auth;
526
adapter = manager_find_adapter(sba);
528
error("No matching adapter found");
534
device = adapter_get_device(connection, adapter, addr);
536
agent = device_get_agent(device);
539
agent = adapter_get_agent(adapter);
542
error("No agent available for user confirm request");
546
if (agent_display_passkey(agent, device, passkey) < 0) {
547
error("Displaying passkey failed");
551
auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_NOTIFY);
557
void hcid_dbus_bonding_process_complete(bdaddr_t *local, bdaddr_t *peer,
560
struct btd_adapter *adapter;
563
struct btd_device *device;
564
struct bonding_request_info *bonding;
565
struct pending_auth_info *auth;
567
debug("hcid_dbus_bonding_process_complete: status=%02x", status);
569
ba2str(peer, peer_addr);
571
adapter = manager_find_adapter(local);
573
error("Unable to find matching adapter");
577
bonding = adapter_get_bonding_info(adapter);
579
if (bonding && bacmp(&bonding->bdaddr, peer))
583
device = adapter_get_device(connection, adapter, peer_addr);
585
/* This should really only happen if we run out of
587
error("Unable to get device object!");
588
status = HCI_REJECTED_LIMITED_RESOURCES;
592
if (status && bonding)
593
bonding->hci_status = status;
595
auth = adapter_find_auth_request(adapter, peer);
597
/* This means that there was no pending PIN or SSP token request
598
* from the controller, i.e. this is not a new pairing */
599
debug("hcid_dbus_bonding_process_complete: no pending auth request");
604
agent_cancel(auth->agent);
606
adapter_remove_auth_request(adapter, peer);
608
/* If this is a new pairing send the appropriate signal for it
609
* and proceed with service discovery */
611
if (device_set_paired(connection, device, bonding) && bonding)
612
adapter_free_bonding_request(adapter);
618
return; /* skip: no bonding req pending */
621
reply = new_authentication_return(bonding->msg,
622
HCI_OE_USER_ENDED_CONNECTION);
624
reply = new_authentication_return(bonding->msg, status);
626
g_dbus_send_message(connection, reply);
628
adapter_free_bonding_request(adapter);
631
void hcid_dbus_inquiry_start(bdaddr_t *local)
633
struct btd_adapter *adapter;
636
adapter = manager_find_adapter(local);
638
error("Unable to find matching adapter");
642
state = adapter_get_state(adapter);
643
state |= STD_INQUIRY;
644
adapter_set_state(adapter, state);
646
* Cancel pending remote name request and clean the device list
647
* when inquiry is supported in periodic inquiry idle state.
649
if (adapter_get_state(adapter) & PERIODIC_INQUIRY)
650
pending_remote_name_cancel(adapter);
652
/* Disable name resolution for non D-Bus clients */
653
if (!adapter_has_discov_sessions(adapter)) {
654
state = adapter_get_state(adapter);
655
state &= ~RESOLVE_NAME;
656
adapter_set_state(adapter, state);
660
static int found_device_req_name(struct btd_adapter *adapter)
662
struct hci_request rq;
664
remote_name_req_cp cp;
665
struct remote_dev_info *dev, match;
666
int dd, req_sent = 0;
667
uint16_t dev_id = adapter_get_dev_id(adapter);
669
memset(&match, 0, sizeof(struct remote_dev_info));
670
bacpy(&match.bdaddr, BDADDR_ANY);
671
match.name_status = NAME_REQUIRED;
673
dev = adapter_search_found_devices(adapter, &match);
677
dd = hci_open_dev(dev_id);
681
memset(&rq, 0, sizeof(rq));
682
rq.ogf = OGF_LINK_CTL;
683
rq.ocf = OCF_REMOTE_NAME_REQ;
685
rq.clen = REMOTE_NAME_REQ_CP_SIZE;
687
rq.rlen = EVT_CMD_STATUS_SIZE;
688
rq.event = EVT_CMD_STATUS;
690
/* send at least one request or return failed if the list is empty */
692
/* flag to indicate the current remote name requested */
693
dev->name_status = NAME_REQUESTED;
695
memset(&rp, 0, sizeof(rp));
696
memset(&cp, 0, sizeof(cp));
697
bacpy(&cp.bdaddr, &dev->bdaddr);
698
cp.pscan_rep_mode = 0x02;
700
if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0)
701
error("Unable to send the HCI remote name request: %s (%d)",
702
strerror(errno), errno);
709
error("Remote name request failed with status 0x%02x",
712
/* if failed, request the next element */
713
/* remove the element from the list */
714
adapter_remove_found_device(adapter, &dev->bdaddr);
716
/* get the next element */
717
dev = adapter_search_found_devices(adapter, &match);
728
void hcid_dbus_inquiry_complete(bdaddr_t *local)
730
struct btd_adapter *adapter;
734
adapter = manager_find_adapter(local);
736
error("Unable to find matching adapter");
740
path = adapter_get_path(adapter);
742
/* Out of range verification */
743
if ((adapter_get_state(adapter) & PERIODIC_INQUIRY) &&
744
!(adapter_get_state(adapter) & STD_INQUIRY))
745
adapter_update_oor_devices(adapter);
748
* The following scenarios can happen:
749
* 1. standard inquiry: always send discovery completed signal
750
* 2. standard inquiry + name resolving: send discovery completed
751
* after name resolving
752
* 3. periodic inquiry: skip discovery completed signal
753
* 4. periodic inquiry + standard inquiry: always send discovery
756
* Keep in mind that non D-Bus requests can arrive.
758
if (found_device_req_name(adapter) == 0)
761
/* reset the discover type to be able to handle D-Bus and non D-Bus
763
state = adapter_get_state(adapter);
764
state &= ~STD_INQUIRY;
765
state &= ~PERIODIC_INQUIRY;
766
adapter_set_state(adapter, state);
769
void hcid_dbus_periodic_inquiry_start(bdaddr_t *local, uint8_t status)
771
struct btd_adapter *adapter;
774
/* Don't send the signal if the cmd failed */
778
adapter = manager_find_adapter(local);
780
error("No matching adapter found");
784
state = adapter_get_state(adapter);
785
state |= PERIODIC_INQUIRY;
786
adapter_set_state(adapter, state);
789
void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status)
791
struct btd_adapter *adapter;
794
/* Don't send the signal if the cmd failed */
798
adapter = manager_find_adapter(local);
800
error("No matching adapter found");
804
/* reset the discover type to be able to handle D-Bus and non D-Bus
806
state = adapter_get_state(adapter);
807
state &= ~PERIODIC_INQUIRY;
808
adapter_set_state(adapter, state);
811
static char *extract_eir_name(uint8_t *data, uint8_t *type)
824
return strndup((char *) (data + 2), data[0] - 1);
830
static void append_dict_valist(DBusMessageIter *iter,
831
const char *first_key,
834
DBusMessageIter dict;
839
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
840
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
841
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
842
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
846
type = va_arg(var_args, int);
847
val = va_arg(var_args, void *);
848
dbus_message_iter_append_dict_entry(&dict, key, type, val);
849
key = va_arg(var_args, char *);
852
dbus_message_iter_close_container(iter, &dict);
855
static void emit_device_found(const char *path, const char *address,
856
const char *first_key, ...)
859
DBusMessageIter iter;
862
signal = dbus_message_new_signal(path, ADAPTER_INTERFACE,
865
error("Unable to allocate new %s.DeviceFound signal",
869
dbus_message_iter_init_append(signal, &iter);
870
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &address);
872
va_start(var_args, first_key);
873
append_dict_valist(&iter, first_key, var_args);
876
dbus_connection_send(connection, signal, NULL);
878
dbus_message_unref(signal);
881
void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class,
882
int8_t rssi, uint8_t *data)
884
char filename[PATH_MAX + 1];
885
struct btd_adapter *adapter;
886
char local_addr[18], peer_addr[18], *alias, *name, *tmp_name;
887
const char *path, *icon, *paddr = peer_addr;
888
struct remote_dev_info *dev, match;
889
dbus_int16_t tmp_rssi = rssi;
890
uint8_t name_type = 0x00;
891
name_status_t name_status;
894
ba2str(local, local_addr);
895
ba2str(peer, peer_addr);
897
adapter = manager_find_adapter(local);
899
error("No matching adapter found");
903
write_remote_class(local, peer, class);
906
write_remote_eir(local, peer, data);
909
* workaround to identify situation when the daemon started and
910
* a standard inquiry or periodic inquiry was already running
912
if (!(adapter_get_state(adapter) & STD_INQUIRY) &&
913
!(adapter_get_state(adapter) & PERIODIC_INQUIRY)) {
914
state = adapter_get_state(adapter);
915
state |= PERIODIC_INQUIRY;
916
adapter_set_state(adapter, state);
918
/* Out of range list update */
919
if (adapter_get_state(adapter) & PERIODIC_INQUIRY)
920
adapter_remove_oor_device(adapter, peer_addr);
922
memset(&match, 0, sizeof(struct remote_dev_info));
923
bacpy(&match.bdaddr, peer);
924
match.name_status = NAME_SENT;
925
/* if found: don't send the name again */
926
dev = adapter_search_found_devices(adapter, &match);
930
/* the inquiry result can be triggered by NON D-Bus client */
931
if (adapter_get_state(adapter) & RESOLVE_NAME)
932
name_status = NAME_REQUIRED;
934
name_status = NAME_NOT_REQUIRED;
936
create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "aliases");
937
alias = textfile_get(filename, peer_addr);
939
create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "names");
940
name = textfile_get(filename, peer_addr);
944
alias = g_strdup(peer_addr);
945
g_strdelimit(alias, ":", '-');
947
alias = g_strdup(name);
950
tmp_name = extract_eir_name(data, &name_type);
952
if (name_type == 0x09) {
953
write_device_name(local, peer, tmp_name);
954
name_status = NAME_NOT_REQUIRED;
968
path = adapter_get_path(adapter);
969
icon = class_to_icon(class);
972
if (name_type != 0x08)
973
name_status = NAME_SENT;
975
emit_device_found(path, paddr,
976
"Address", DBUS_TYPE_STRING, &paddr,
977
"Class", DBUS_TYPE_UINT32, &class,
978
"Icon", DBUS_TYPE_STRING, &icon,
979
"RSSI", DBUS_TYPE_INT16, &tmp_rssi,
980
"Name", DBUS_TYPE_STRING, &name,
981
"Alias", DBUS_TYPE_STRING, &alias, NULL);
985
emit_device_found(path, paddr,
986
"Address", DBUS_TYPE_STRING, &paddr,
987
"Class", DBUS_TYPE_UINT32, &class,
988
"Icon", DBUS_TYPE_STRING, &icon,
989
"RSSI", DBUS_TYPE_INT16, &tmp_rssi,
990
"Alias", DBUS_TYPE_STRING, &alias, NULL);
995
/* add in the list to track name sent/pending */
996
adapter_add_found_device(adapter, peer, rssi, name_status);
999
void hcid_dbus_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
1002
const char *paddr = peer_addr;
1003
uint32_t old_class = 0;
1004
struct btd_adapter *adapter;
1005
struct btd_device *device;
1006
const gchar *dev_path;
1008
read_remote_class(local, peer, &old_class);
1010
if (old_class == class)
1013
adapter = manager_find_adapter(local);
1015
error("No matching adapter found");
1019
ba2str(peer, peer_addr);
1020
device = adapter_find_device(adapter, paddr);
1025
dev_path = device_get_path(device);
1027
dbus_connection_emit_property_changed(connection, dev_path,
1028
DEVICE_INTERFACE, "Class",
1029
DBUS_TYPE_UINT32, &class);
1032
void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status,
1035
struct btd_adapter *adapter;
1036
char srcaddr[18], dstaddr[18];
1037
const gchar *dev_path;
1040
adapter = manager_find_adapter(local);
1042
error("No matching adapter found");
1046
ba2str(local, srcaddr);
1047
ba2str(peer, dstaddr);
1050
struct btd_device *device;
1052
device = adapter_find_device(adapter, dstaddr);
1056
dev_path = device_get_path(device);
1058
dbus_connection_emit_property_changed(connection,
1059
dev_path, DEVICE_INTERFACE,
1060
"Name", DBUS_TYPE_STRING, &name);
1062
if (read_device_alias(srcaddr, dstaddr,
1063
alias, sizeof(alias)) < 1) {
1065
dbus_connection_emit_property_changed(connection,
1066
dev_path, DEVICE_INTERFACE,
1067
"Alias", DBUS_TYPE_STRING, &name);
1072
/* remove from remote name request list */
1073
adapter_remove_found_device(adapter, peer);
1075
/* check if there is more devices to request names */
1076
if (found_device_req_name(adapter) == 0)
1079
state = adapter_get_state(adapter);
1080
state &= ~PERIODIC_INQUIRY;
1081
state &= ~STD_INQUIRY;
1082
adapter_set_state(adapter, state);
1085
void hcid_dbus_link_key_notify(bdaddr_t *local, bdaddr_t *peer)
1088
struct btd_device *device;
1089
struct btd_adapter *adapter;
1090
struct bonding_request_info *bonding;
1092
adapter = manager_find_adapter(local);
1094
error("No matching adapter found");
1098
ba2str(peer, peer_addr);
1100
device = adapter_find_device(adapter, peer_addr);
1102
bonding = adapter_get_bonding_info(adapter);
1104
if (!device_get_connected(device))
1105
device_set_secmode3_conn(device, TRUE);
1107
hcid_dbus_bonding_process_complete(local, peer, 0);
1110
void hcid_dbus_conn_complete(bdaddr_t *local, uint8_t status, uint16_t handle,
1114
struct btd_adapter *adapter;
1115
struct bonding_request_info *bonding;
1116
struct btd_device *device;
1118
adapter = manager_find_adapter(local);
1120
error("No matching adapter found");
1124
ba2str(peer, peer_addr);
1126
device = adapter_get_device(connection, adapter, peer_addr);
1129
struct pending_auth_info *auth;
1131
auth = adapter_find_auth_request(adapter, peer);
1132
if (auth && auth->agent)
1133
agent_cancel(auth->agent);
1135
adapter_remove_auth_request(adapter, peer);
1138
device_set_secmode3_conn(device, FALSE);
1140
bonding = adapter_get_bonding_info(adapter);
1142
bonding->hci_status = status;
1145
device_set_connected(connection, device, TRUE);
1147
/* add in the active connetions list */
1148
adapter_add_active_conn(adapter, peer, handle);
1152
void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status,
1153
uint16_t handle, uint8_t reason)
1157
struct btd_adapter *adapter;
1158
struct btd_device *device;
1159
struct active_conn_info *dev;
1160
struct pending_auth_info *auth;
1162
struct bonding_request_info *bonding;
1165
error("Disconnection failed: 0x%02x", status);
1169
adapter = manager_find_adapter(local);
1171
error("No matching adapter found");
1175
dev = adapter_search_active_conn_by_handle(adapter, handle);
1177
error("No matching connection for handle %u", handle);
1181
ba2str(&dev->bdaddr, peer_addr);
1183
dev_id = adapter_get_dev_id(adapter);
1185
/* clean pending HCI cmds */
1186
hci_req_queue_remove(dev_id, &dev->bdaddr);
1188
/* Cancel D-Bus/non D-Bus requests */
1189
auth = adapter_find_auth_request(adapter, &dev->bdaddr);
1190
if (auth && auth->agent)
1191
agent_cancel(auth->agent);
1193
adapter_remove_auth_request(adapter, &dev->bdaddr);
1195
bonding = adapter_get_bonding_info(adapter);
1196
/* Check if there is a pending Bonding request */
1197
if (bonding && (bacmp(&bonding->bdaddr, &dev->bdaddr) == 0)) {
1198
if (bonding->cancel) {
1199
/* reply authentication canceled */
1200
reply = new_authentication_return(bonding->msg,
1201
HCI_OE_USER_ENDED_CONNECTION);
1202
g_dbus_send_message(connection, reply);
1204
reply = new_authentication_return(bonding->msg,
1205
HCI_AUTHENTICATION_FAILURE);
1206
dbus_connection_send(connection, reply, NULL);
1207
dbus_message_unref(reply);
1209
adapter_free_bonding_request(adapter);
1212
adapter_remove_active_conn(adapter, dev);
1214
device = adapter_find_device(adapter, peer_addr);
1216
device_set_connected(connection, device, FALSE);
1218
if (device_is_temporary(device)) {
1219
debug("Removing temporary device %s", peer_addr);
1220
adapter_remove_device(connection, adapter, device);
1225
int set_limited_discoverable(int dd, const uint8_t *cls, gboolean limited)
1228
int num = (limited ? 2 : 1);
1229
uint8_t lap[] = { 0x33, 0x8b, 0x9e, 0x00, 0x8b, 0x9e };
1234
if (hci_write_current_iac_lap(dd, num, lap, HCI_REQ_TIMEOUT) < 0) {
1236
error("Can't write current IAC LAP: %s(%d)",
1237
strerror(err), err);
1243
return 0; /* Already limited */
1245
dev_class = (cls[2] << 16) | ((cls[1] | 0x20) << 8) | cls[0];
1247
if (!(cls[1] & 0x20))
1248
return 0; /* Already clear */
1250
dev_class = (cls[2] << 16) | ((cls[1] & 0xdf) << 8) | cls[0];
1253
if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
1255
error("Can't write class of device: %s (%d)",
1256
strerror(err), err);
1263
int set_service_classes(int dd, const uint8_t *cls, uint8_t value)
1267
if (cls[2] == value)
1268
return 0; /* Already set */
1270
dev_class = (value << 16) | (cls[1] << 8) | cls[0];
1272
if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
1274
error("Can't write class of device: %s (%d)",
1275
strerror(err), err);
1282
int set_major_class(int dd, const uint8_t *cls, uint8_t major)
1286
dev_class = (cls[2] << 16) | ((cls[1] & 0x20) << 8) |
1287
((major & 0xdf) << 8) | 0x00;
1289
if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
1291
error("Can't write class of device: %s (%d)",
1292
strerror(err), err);
1299
int set_minor_class(int dd, const uint8_t *cls, uint8_t minor)
1303
dev_class = (cls[2] << 16) | (cls[1] << 8) | minor;
1305
if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
1307
error("Can't write class of device: %s (%d)",
1308
strerror(err), err);
1315
int set_major_and_minor_class(int dd, const uint8_t *cls,
1316
uint8_t major, uint8_t minor)
1320
dev_class = (cls[2] << 16) | ((cls[1] & 0x20) << 8) |
1321
((major & 0xdf) << 8) | minor;
1323
if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
1325
error("Can't write class of device: %s (%d)",
1326
strerror(err), err);
1333
/* Section reserved to device HCI callbacks */
1335
void hcid_dbus_setname_complete(bdaddr_t *local)
1338
read_local_name_rp rp;
1339
struct hci_request rq;
1340
const char *pname = (char *) rp.name;
1341
char local_addr[18], name[249];
1343
ba2str(local, local_addr);
1345
id = hci_devid(local_addr);
1347
error("No matching device id for %s", local_addr);
1351
dd = hci_open_dev(id);
1353
error("HCI device open failed: hci%d", id);
1354
memset(&rp, 0, sizeof(rp));
1356
memset(&rq, 0, sizeof(rq));
1357
rq.ogf = OGF_HOST_CTL;
1358
rq.ocf = OCF_READ_LOCAL_NAME;
1360
rq.rlen = READ_LOCAL_NAME_RP_SIZE;
1361
rq.event = EVT_CMD_COMPLETE;
1363
if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
1364
error("Sending getting name command failed: %s (%d)",
1365
strerror(errno), errno);
1367
} else if (rp.status) {
1368
error("Getting name failed with status 0x%02x",
1375
strncpy(name, pname, sizeof(name) - 1);
1380
void hcid_dbus_setscan_enable_complete(bdaddr_t *local)
1382
struct btd_adapter *adapter;
1383
read_scan_enable_rp rp;
1384
struct hci_request rq;
1388
adapter = manager_find_adapter(local);
1390
error("No matching adapter found");
1394
dev_id = adapter_get_dev_id(adapter);
1396
dd = hci_open_dev(dev_id);
1398
error("HCI device open failed: hci%d", dev_id);
1402
memset(&rq, 0, sizeof(rq));
1403
rq.ogf = OGF_HOST_CTL;
1404
rq.ocf = OCF_READ_SCAN_ENABLE;
1406
rq.rlen = READ_SCAN_ENABLE_RP_SIZE;
1407
rq.event = EVT_CMD_COMPLETE;
1409
if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
1410
error("Sending read scan enable command failed: %s (%d)",
1411
strerror(errno), errno);
1416
error("Getting scan enable failed with status 0x%02x",
1421
adapter_remove_discov_timeout(adapter);
1423
if (adapter_get_scan_mode(adapter) != rp.enable)
1424
adapter_mode_changed(adapter, rp.enable);
1431
void hcid_dbus_write_class_complete(bdaddr_t *local)
1433
struct btd_adapter *adapter;
1438
adapter = manager_find_adapter(local);
1440
error("No matching adapter found");
1444
dev_id = adapter_get_dev_id(adapter);
1446
dd = hci_open_dev(dev_id);
1448
error("HCI device open failed: hci%d", dev_id);
1452
if (hci_read_class_of_dev(dd, cls, HCI_REQ_TIMEOUT) < 0) {
1453
error("Can't read class of device on hci%d: %s (%d)",
1454
dev_id, strerror(errno), errno);
1459
adapter_set_class(adapter, cls);
1460
write_local_class(local, cls);
1465
void hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t *local)
1467
struct btd_adapter *adapter;
1473
adapter = manager_find_adapter(local);
1475
error("No matching adapter found");
1479
dev_id = adapter_get_dev_id(adapter);
1480
path = adapter_get_path(adapter);
1482
dd = hci_open_dev(dev_id);
1484
error("HCI adapter open failed: %s", path);
1488
if (hci_read_simple_pairing_mode(dd, &mode,
1489
HCI_REQ_TIMEOUT) < 0) {
1490
error("Can't read class of adapter on %s: %s(%d)",
1491
path, strerror(errno), errno);
1496
adapter_update_ssp_mode(adapter, dd, mode);
1501
int hcid_dbus_get_io_cap(bdaddr_t *local, bdaddr_t *remote,
1502
uint8_t *cap, uint8_t *auth)
1504
struct btd_adapter *adapter;
1505
struct btd_device *device;
1506
struct agent *agent = NULL;
1509
adapter = manager_find_adapter(local);
1511
error("No matching adapter found");
1515
if (get_auth_requirements(local, remote, auth) < 0)
1518
ba2str(remote, addr);
1520
/* For CreatePairedDevice use dedicated bonding */
1521
device = adapter_find_device(adapter, addr);
1523
agent = device_get_agent(device);
1528
agent = adapter_get_agent(adapter);
1531
/* This is the non bondable mode case */
1532
if (device && device_get_auth(device) > 0x01) {
1533
debug("Bonding request, but no agent present");
1537
/* No agent available, and no bonding case */
1539
debug("Allowing no bonding without agent");
1540
/* No input, no output */
1545
error("No agent available for IO capability");
1549
if (device && *auth < 0x02) {
1550
/* If remote requests dedicated bonding follow that lead */
1551
if (device_get_auth(device) == 0x02 ||
1552
device_get_auth(device) == 0x03)
1556
*cap = agent_get_io_capability(agent);
1561
int hcid_dbus_set_io_cap(bdaddr_t *local, bdaddr_t *remote,
1562
uint8_t cap, uint8_t auth)
1564
struct btd_adapter *adapter;
1565
struct btd_device *device;
1568
adapter = manager_find_adapter(local);
1570
error("No matching adapter found");
1574
ba2str(remote, addr);
1576
device = adapter_get_device(connection, adapter, addr);
1578
device_set_cap(device, cap);
1579
device_set_auth(device, auth);
1585
static int inquiry_cancel(int dd, int to)
1587
struct hci_request rq;
1590
memset(&rq, 0, sizeof(rq));
1591
rq.ogf = OGF_LINK_CTL;
1592
rq.ocf = OCF_INQUIRY_CANCEL;
1593
rq.rparam = &status;
1594
rq.rlen = sizeof(status);
1595
rq.event = EVT_CMD_COMPLETE;
1597
if (hci_send_req(dd, &rq, to) < 0)
1601
errno = bt_error(status);
1608
static int remote_name_cancel(int dd, bdaddr_t *dba, int to)
1610
remote_name_req_cancel_cp cp;
1611
struct hci_request rq;
1614
memset(&rq, 0, sizeof(rq));
1615
memset(&cp, 0, sizeof(cp));
1617
bacpy(&cp.bdaddr, dba);
1619
rq.ogf = OGF_LINK_CTL;
1620
rq.ocf = OCF_REMOTE_NAME_REQ_CANCEL;
1622
rq.clen = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
1623
rq.rparam = &status;
1624
rq.rlen = sizeof(status);
1625
rq.event = EVT_CMD_COMPLETE;
1627
if (hci_send_req(dd, &rq, to) < 0)
1631
errno = bt_error(status);
1638
int cancel_discovery(struct btd_adapter *adapter)
1640
struct remote_dev_info *dev, match;
1642
uint16_t dev_id = adapter_get_dev_id(adapter);
1644
dd = hci_open_dev(dev_id);
1649
* If there is a pending read remote name request means
1650
* that the inquiry complete event was already received
1652
memset(&match, 0, sizeof(struct remote_dev_info));
1653
bacpy(&match.bdaddr, BDADDR_ANY);
1654
match.name_status = NAME_REQUESTED;
1656
dev = adapter_search_found_devices(adapter, &match);
1658
if (remote_name_cancel(dd, &dev->bdaddr, HCI_REQ_TIMEOUT) < 0) {
1659
error("Read remote name cancel failed: %s, (%d)",
1660
strerror(errno), errno);
1664
if (inquiry_cancel(dd, HCI_REQ_TIMEOUT) < 0) {
1665
error("Inquiry cancel failed:%s (%d)",
1666
strerror(errno), errno);
1676
static int periodic_inquiry_exit(int dd, int to)
1678
struct hci_request rq;
1681
memset(&rq, 0, sizeof(rq));
1682
rq.ogf = OGF_LINK_CTL;
1683
rq.ocf = OCF_EXIT_PERIODIC_INQUIRY;
1684
rq.rparam = &status;
1685
rq.rlen = sizeof(status);
1686
rq.event = EVT_CMD_COMPLETE;
1688
if (hci_send_req(dd, &rq, to) < 0)
1699
int cancel_periodic_discovery(struct btd_adapter *adapter)
1701
struct remote_dev_info *dev, match;
1703
uint16_t dev_id = adapter_get_dev_id(adapter);
1705
dd = hci_open_dev(dev_id);
1709
/* find the pending remote name request */
1710
memset(&match, 0, sizeof(struct remote_dev_info));
1711
bacpy(&match.bdaddr, BDADDR_ANY);
1712
match.name_status = NAME_REQUESTED;
1714
dev = adapter_search_found_devices(adapter, &match);
1716
if (remote_name_cancel(dd, &dev->bdaddr, HCI_REQ_TIMEOUT) < 0) {
1717
error("Read remote name cancel failed: %s, (%d)",
1718
strerror(errno), errno);
1723
/* ovewrite err if necessary: stop periodic inquiry has higher
1725
if (periodic_inquiry_exit(dd, HCI_REQ_TIMEOUT) < 0) {
1726
error("Periodic Inquiry exit failed:%s (%d)",
1727
strerror(errno), errno);
1736
/* Most of the functions in this module require easy access to a connection so
1737
* we keep it global here and provide these access functions the other (few)
1738
* modules that require access to it */
1740
void set_dbus_connection(DBusConnection *conn)
1745
DBusConnection *get_dbus_connection(void)