Bluetooth: Use {start,stop}_discovery_complete handler for cmd_complete

Sending the required cmd_complete for the management commands should be
done in one place and not in multiple places. Especially for Start and
Stop Discovery commands this is split into to sending it in case of
failure from the complete handler, but in case of success from the
event state update function triggering mgmt_discovering. This is way
too convoluted and since hci_request serializes the HCI command
processing, send the cmd_complete response from the complete handler
for all cases.

Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Marcel Holtmann 2014-12-04 11:36:35 +01:00 committed by Johan Hedberg
parent f5a969f23b
commit 11e6e25d05
1 changed files with 35 additions and 70 deletions

View File

@ -3686,64 +3686,53 @@ done:
return err; return err;
} }
static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
{
struct pending_cmd *cmd;
u8 type;
int err;
hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
if (!cmd)
return -ENOENT;
type = hdev->discovery.type;
err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
&type, sizeof(type));
mgmt_pending_remove(cmd);
return err;
}
static void start_discovery_complete(struct hci_dev *hdev, u8 status) static void start_discovery_complete(struct hci_dev *hdev, u8 status)
{ {
unsigned long timeout = 0; struct pending_cmd *cmd;
unsigned long timeout;
BT_DBG("status %d", status); BT_DBG("status %d", status);
if (status) { hci_dev_lock(hdev);
hci_dev_lock(hdev);
mgmt_start_discovery_failed(hdev, status); cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
hci_dev_unlock(hdev); if (cmd) {
return; u8 type = hdev->discovery.type;
cmd_complete(cmd->sk, hdev->id, cmd->opcode,
mgmt_status(status), &type, sizeof(type));
mgmt_pending_remove(cmd);
}
if (status) {
hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
goto unlock;
} }
hci_dev_lock(hdev);
hci_discovery_set_state(hdev, DISCOVERY_FINDING); hci_discovery_set_state(hdev, DISCOVERY_FINDING);
hci_dev_unlock(hdev);
switch (hdev->discovery.type) { switch (hdev->discovery.type) {
case DISCOV_TYPE_LE: case DISCOV_TYPE_LE:
timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
break; break;
case DISCOV_TYPE_INTERLEAVED: case DISCOV_TYPE_INTERLEAVED:
timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
break; break;
case DISCOV_TYPE_BREDR: case DISCOV_TYPE_BREDR:
timeout = 0;
break; break;
default: default:
BT_ERR("Invalid discovery type %d", hdev->discovery.type); BT_ERR("Invalid discovery type %d", hdev->discovery.type);
timeout = 0;
break;
} }
if (!timeout) if (timeout)
return; queue_delayed_work(hdev->workqueue,
&hdev->le_scan_disable, timeout);
queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); unlock:
hci_dev_unlock(hdev);
} }
static int start_discovery(struct sock *sk, struct hci_dev *hdev, static int start_discovery(struct sock *sk, struct hci_dev *hdev,
@ -3915,36 +3904,26 @@ failed:
return err; return err;
} }
static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
{
struct pending_cmd *cmd;
int err;
cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
if (!cmd)
return -ENOENT;
err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
&hdev->discovery.type, sizeof(hdev->discovery.type));
mgmt_pending_remove(cmd);
return err;
}
static void stop_discovery_complete(struct hci_dev *hdev, u8 status) static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
{ {
struct pending_cmd *cmd;
BT_DBG("status %d", status); BT_DBG("status %d", status);
hci_dev_lock(hdev); hci_dev_lock(hdev);
if (status) { cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
mgmt_stop_discovery_failed(hdev, status); if (cmd) {
goto unlock; u8 type = hdev->discovery.type;
cmd_complete(cmd->sk, hdev->id, cmd->opcode,
mgmt_status(status), &type, sizeof(type));
mgmt_pending_remove(cmd);
} }
hci_discovery_set_state(hdev, DISCOVERY_STOPPED); if (!status)
hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
unlock:
hci_dev_unlock(hdev); hci_dev_unlock(hdev);
} }
@ -6909,23 +6888,9 @@ void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
void mgmt_discovering(struct hci_dev *hdev, u8 discovering) void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
{ {
struct mgmt_ev_discovering ev; struct mgmt_ev_discovering ev;
struct pending_cmd *cmd;
BT_DBG("%s discovering %u", hdev->name, discovering); BT_DBG("%s discovering %u", hdev->name, discovering);
if (discovering)
cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
else
cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
if (cmd != NULL) {
u8 type = hdev->discovery.type;
cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type,
sizeof(type));
mgmt_pending_remove(cmd);
}
memset(&ev, 0, sizeof(ev)); memset(&ev, 0, sizeof(ev));
ev.type = hdev->discovery.type; ev.type = hdev->discovery.type;
ev.discovering = discovering; ev.discovering = discovering;