From: Szymon Janc Date: Fri, 25 Oct 2013 15:15:01 +0000 (+0200) Subject: android: Add support for setting adapter scan mode X-Git-Tag: android-x86-4.4-r3~7154 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=9be3bd6609c7d6bf8c602db9dfa45cdcef070c57;p=android-x86%2Fexternal-bluetooth-bluez.git android: Add support for setting adapter scan mode This allows to set all modes defined: none, connectable and 'connectable and discoverable'. --- diff --git a/android/adapter.c b/android/adapter.c index 13cce8ffe..a6c5704bb 100644 --- a/android/adapter.c +++ b/android/adapter.c @@ -322,6 +322,26 @@ static bool set_mode(uint16_t opcode, uint8_t mode) return false; } +static bool set_discoverable(uint8_t mode, uint16_t timeout) +{ + struct mgmt_cp_set_discoverable cp; + + memset(&cp, 0, sizeof(cp)); + cp.val = mode; + cp.timeout = htobs(timeout); + + DBG("mode %u timeout %u", mode, timeout); + + if (mgmt_send(adapter->mgmt, MGMT_OP_SET_DISCOVERABLE, + adapter->index, sizeof(cp), &cp, + set_mode_complete, adapter, NULL) > 0) + return true; + + error("Failed to set mode discoverable"); + + return false; +} + static void send_adapter_name(void) { struct hal_ev_adapter_props_changed *ev; @@ -365,13 +385,64 @@ static bool get_property(void *buf, uint16_t len) } } +static uint8_t set_scan_mode(void *buf, uint16_t len) +{ + uint8_t *mode = buf; + bool conn, disc, cur_conn, cur_disc; + + cur_conn = adapter->current_settings & MGMT_SETTING_CONNECTABLE; + cur_disc = adapter->current_settings & MGMT_SETTING_DISCOVERABLE; + + DBG("connectable %u discoverable %d mode %u", cur_conn, cur_disc, + *mode); + + switch (*mode) { + case HAL_ADAPTER_SCAN_MODE_NONE: + if (!cur_conn && !cur_disc) + return HAL_STATUS_DONE; + + conn = false; + disc = false; + break; + case HAL_ADAPTER_SCAN_MODE_CONN: + if (cur_conn && !cur_disc) + return HAL_STATUS_DONE; + + conn = true; + disc = false; + break; + case HAL_ADAPTER_SCAN_MODE_CONN_DISC: + if (cur_conn && cur_disc) + return HAL_STATUS_DONE; + + conn = true; + disc = true; + break; + default: + return HAL_STATUS_FAILED; + } + + if (cur_conn != conn) { + if (!set_mode(MGMT_OP_SET_CONNECTABLE, conn ? 0x01 : 0x00)) + return HAL_STATUS_FAILED; + } + + if (cur_disc != disc) { + if (!set_discoverable(disc ? 0x01 : 0x00, 0)) + return HAL_STATUS_FAILED; + } + + return HAL_STATUS_SUCCESS; +} + static uint8_t set_property(void *buf, uint16_t len) { struct hal_cmd_set_adapter_prop *cmd = buf; switch (cmd->type) { - case HAL_PROP_ADAPTER_NAME: case HAL_PROP_ADAPTER_SCAN_MODE: + return set_scan_mode(cmd->val, cmd->len); + case HAL_PROP_ADAPTER_NAME: case HAL_PROP_ADAPTER_DISC_TIMEOUT: default: DBG("Unhandled property type 0x%x", cmd->type);