From f0962acb9d133ac3295c4dac5205950029903ddf Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 7 Jan 2013 12:18:17 +0200 Subject: [PATCH] core: Move mgmt pair device command to adapter code --- src/adapter.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/mgmt.c | 47 +---------------------------------------------- src/mgmt.h | 2 -- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index d3e5dd417..030a64b38 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -4188,11 +4188,54 @@ int btd_adapter_passkey_reply(struct btd_adapter *adapter, passkey); } +static void pair_device_complete(uint8_t status, uint16_t length, + const void *param, void *user_data) +{ + const struct mgmt_rp_pair_device *rp = param; + struct btd_adapter *adapter = user_data; + + DBG("%s (0x%02x)", mgmt_errstr(status), status); + + if (status != MGMT_STATUS_SUCCESS && length < sizeof(*rp)) { + error("Pair device failed: %s (0x%02x)", + mgmt_errstr(status), status); + return; + } + + if (length < sizeof(*rp)) { + error("Too small pair device response"); + return; + } + + adapter_bonding_complete(adapter, &rp->addr.bdaddr, rp->addr.type, + status); +} + int adapter_create_bonding(struct btd_adapter *adapter, const bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap) { + struct mgmt_cp_pair_device cp; + char addr[18]; + suspend_discovery(adapter); - return mgmt_create_bonding(adapter->dev_id, bdaddr, addr_type, io_cap); + + ba2str(bdaddr, addr); + DBG("hci%u bdaddr %s type %d io_cap 0x%02x", + adapter->dev_id, addr, addr_type, io_cap); + + memset(&cp, 0, sizeof(cp)); + bacpy(&cp.addr.bdaddr, bdaddr); + cp.addr.type = addr_type; + cp.io_cap = io_cap; + + if (mgmt_send(adapter->mgmt, MGMT_OP_PAIR_DEVICE, + adapter->dev_id, sizeof(cp), &cp, + pair_device_complete, adapter, NULL) > 0) + return 0; + + error("Failed to pair %s for hci%u", addr, adapter->dev_id); + + return -EIO; } int adapter_cancel_bonding(struct btd_adapter *adapter, const bdaddr_t *bdaddr, diff --git a/src/mgmt.c b/src/mgmt.c index b0e091588..c71cea4de 100644 --- a/src/mgmt.c +++ b/src/mgmt.c @@ -438,24 +438,6 @@ static void mgmt_user_confirm_request(uint16_t index, void *buf, } } -static void pair_device_complete(uint16_t index, uint8_t status, - void *buf, size_t len) -{ - struct mgmt_rp_pair_device *rp = buf; - char addr[18]; - - if (len < sizeof(*rp)) { - error("Too small pair_device complete event"); - return; - } - - ba2str(&rp->addr.bdaddr, addr); - - DBG("hci%d %s pairing complete status %u", index, addr, status); - - bonding_complete(index, &rp->addr, status); -} - static void mgmt_cmd_complete(uint16_t index, void *buf, size_t len) { struct mgmt_ev_cmd_complete *ev = buf; @@ -534,7 +516,7 @@ static void mgmt_cmd_complete(uint16_t index, void *buf, size_t len) DBG("set_io_capability complete"); break; case MGMT_OP_PAIR_DEVICE: - pair_device_complete(index, ev->status, ev->data, len); + DBG("pair_device complete"); break; case MGMT_OP_USER_CONFIRM_REPLY: DBG("user_confirm_reply complete"); @@ -976,33 +958,6 @@ void mgmt_cleanup(void) } } -int mgmt_create_bonding(int index, const bdaddr_t *bdaddr, uint8_t addr_type, - uint8_t io_cap) -{ - char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pair_device)]; - struct mgmt_hdr *hdr = (void *) buf; - struct mgmt_cp_pair_device *cp = (void *) &buf[sizeof(*hdr)]; - char addr[18]; - - ba2str(bdaddr, addr); - DBG("hci%d bdaddr %s type %d io_cap 0x%02x", - index, addr, addr_type, io_cap); - - memset(buf, 0, sizeof(buf)); - hdr->opcode = htobs(MGMT_OP_PAIR_DEVICE); - hdr->len = htobs(sizeof(*cp)); - hdr->index = htobs(index); - - bacpy(&cp->addr.bdaddr, bdaddr); - cp->addr.type = addr_type; - cp->io_cap = io_cap; - - if (write(mgmt_sock, &buf, sizeof(buf)) < 0) - return -errno; - - return 0; -} - int mgmt_cancel_bonding(int index, const bdaddr_t *bdaddr, uint8_t addr_type) { char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_addr_info)]; diff --git a/src/mgmt.h b/src/mgmt.h index 91ea08566..86dd8429c 100644 --- a/src/mgmt.h +++ b/src/mgmt.h @@ -25,8 +25,6 @@ int mgmt_setup(void); void mgmt_cleanup(void); -int mgmt_create_bonding(int index, const bdaddr_t *bdaddr, uint8_t addr_type, - uint8_t io_cap); int mgmt_cancel_bonding(int index, const bdaddr_t *bdaddr, uint8_t addr_type); int mgmt_pincode_reply(int index, const bdaddr_t *bdaddr, const char *pin, -- 2.47.3