From d9ce0bb705b155c2f51aabc929026888a3d7a7e8 Mon Sep 17 00:00:00 2001 From: Hrishikesh Dhayagude Date: Mon, 9 Apr 2018 16:00:48 +0530 Subject: [PATCH] Modify esp_bt_gap_set_scan_mode() to be able to set connection modes as well as discoverable modes The current API is insufficient to allow the users to set different combinations of connection and discoverable mode Users should be able to choose the device modes independently: 1. Connectable- i. Non-connectable mode ii. Connectable mode 2. Discoverable- i. Non-discoverable mode ii. Limited discoverable mode iii. General discoverable mode --- components/bt/bluedroid/api/esp_gap_bt_api.c | 5 ++-- .../api/include/api/esp_gap_bt_api.h | 18 +++++++---- .../btc/profile/std/gap/btc_gap_bt.c | 30 +++++++++++-------- .../btc/profile/std/include/btc_gap_bt.h | 3 +- .../bluetooth/a2dp_gatts_coex/main/main.c | 2 +- examples/bluetooth/a2dp_sink/main/bt_app_av.c | 4 +-- examples/bluetooth/a2dp_sink/main/main.c | 2 +- examples/bluetooth/a2dp_source/main/main.c | 8 ++--- .../bt_discovery/main/bt_discovery.c | 2 +- .../main/example_spp_acceptor_demo.c | 2 +- .../main/example_spp_initiator_demo.c | 2 +- .../main/example_spp_vfs_acceptor_demo.c | 2 +- .../main/example_spp_vfs_initiator_demo.c | 2 +- 13 files changed, 48 insertions(+), 34 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index ac0326e11f..ff3f945c48 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -37,7 +37,7 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback) return ESP_OK; } -esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode) +esp_err_t esp_bt_gap_set_scan_mode(esp_bt_connection_mode_t c_mode, esp_bt_discovery_mode_t d_mode) { btc_msg_t msg; btc_gap_bt_args_t arg; @@ -49,7 +49,8 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GAP_BT; msg.act = BTC_GAP_BT_ACT_SET_SCAN_MODE; - arg.set_scan_mode.mode = mode; + arg.set_scan_mode.c_mode = c_mode; + arg.set_scan_mode.d_mode = d_mode; return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h index f5b2c8f15a..084cabec23 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h @@ -47,10 +47,15 @@ typedef enum { /// Discoverability and Connectability mode typedef enum { - ESP_BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */ - ESP_BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */ - ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectable */ -} esp_bt_scan_mode_t; + ESP_BT_NON_CONNECTABLE, /*!< Non-connectable */ + ESP_BT_CONNECTABLE, /*!< Connectable */ +} esp_bt_connection_mode_t; + +typedef enum { + ESP_BT_NON_DISCOVERABLE, /*!< Non-discoverable */ + ESP_BT_LIMITED_DISCOVERABLE, /*!< Limited Discoverable */ + ESP_BT_GENERAL_DISCOVERABLE, /*!< General Discoverable */ +} esp_bt_discovery_mode_t; /// Bluetooth Device Property type typedef enum { @@ -350,7 +355,8 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback); * @brief Set discoverability and connectability mode for legacy bluetooth. This function should * be called after esp_bluedroid_enable() completes successfully * - * @param[in] mode : one of the enums of bt_scan_mode_t + * @param[in] c_mode : one of the enums of esp_bt_connection_mode_t + * @param[in] d_mode : one of the enums of esp_bt_discovery_mode_t * * @return * - ESP_OK : Succeed @@ -358,7 +364,7 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback); * - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled * - ESP_FAIL: others */ -esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode); +esp_err_t esp_bt_gap_set_scan_mode(esp_bt_connection_mode_t c_mode, esp_bt_discovery_mode_t d_mode); /** * @brief Start device discovery. This function should be called after esp_bluedroid_enable() completes successfully. diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c index a5af828e1a..8de802f2ea 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -48,29 +48,35 @@ static inline void btc_gap_bt_cb_to_app(esp_bt_gap_cb_event_t event, esp_bt_gap_ } } -static void btc_bt_set_scan_mode(esp_bt_scan_mode_t mode) +static void btc_bt_set_scan_mode(esp_bt_connection_mode_t c_mode, esp_bt_discovery_mode_t d_mode) { tBTA_DM_DISC disc_mode; tBTA_DM_CONN conn_mode; - switch (mode) { - case ESP_BT_SCAN_MODE_NONE: - disc_mode = BTA_DM_NON_DISC; + switch (c_mode) { + case ESP_BT_NON_CONNECTABLE: conn_mode = BTA_DM_NON_CONN; break; - - case ESP_BT_SCAN_MODE_CONNECTABLE: - disc_mode = BTA_DM_NON_DISC; + case ESP_BT_CONNECTABLE: conn_mode = BTA_DM_CONN; break; + default: + BTC_TRACE_WARNING("invalid connection mode (0x%x)", c_mode); + return; + } - case ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: + switch (d_mode) { + case ESP_BT_NON_DISCOVERABLE: + disc_mode = BTA_DM_NON_DISC; + break; + case ESP_BT_LIMITED_DISCOVERABLE: + disc_mode = BTA_DM_LIMITED_DISC; + break; + case ESP_BT_GENERAL_DISCOVERABLE: disc_mode = BTA_DM_GENERAL_DISC; - conn_mode = BTA_DM_CONN; break; - default: - BTC_TRACE_WARNING("invalid scan mode (0x%x)", mode); + BTC_TRACE_WARNING("invalid discovery mode (0x%x)", d_mode); return; } @@ -761,7 +767,7 @@ void btc_gap_bt_call_handler(btc_msg_t *msg) BTC_TRACE_DEBUG("%s act %d\n", __func__, msg->act); switch (msg->act) { case BTC_GAP_BT_ACT_SET_SCAN_MODE: { - btc_bt_set_scan_mode(arg->set_scan_mode.mode); + btc_bt_set_scan_mode(arg->set_scan_mode.c_mode, arg->set_scan_mode.d_mode); break; } case BTC_GAP_BT_ACT_START_DISCOVERY: { diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h index 67e03cb3f9..51e685965b 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h @@ -54,7 +54,8 @@ typedef enum { typedef union { // BTC_BT_GAP_ACT_SET_SCAN_MODE, struct set_bt_scan_mode_args { - esp_bt_scan_mode_t mode; + esp_bt_connection_mode_t c_mode; + esp_bt_discovery_mode_t d_mode; } set_scan_mode; // BTC_GAP_BT_ACT_START_DISCOVERY diff --git a/examples/bluetooth/a2dp_gatts_coex/main/main.c b/examples/bluetooth/a2dp_gatts_coex/main/main.c index 3b042714a1..e3be5becf6 100644 --- a/examples/bluetooth/a2dp_gatts_coex/main/main.c +++ b/examples/bluetooth/a2dp_gatts_coex/main/main.c @@ -618,7 +618,7 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) esp_avrc_ct_register_callback(bt_app_rc_ct_cb); /* set discoverable and connectable mode, wait to be connected */ - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); break; } default: diff --git a/examples/bluetooth/a2dp_sink/main/bt_app_av.c b/examples/bluetooth/a2dp_sink/main/bt_app_av.c index d9bbbfbed8..304e826e4b 100644 --- a/examples/bluetooth/a2dp_sink/main/bt_app_av.c +++ b/examples/bluetooth/a2dp_sink/main/bt_app_av.c @@ -100,9 +100,9 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) ESP_LOGI(BT_AV_TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", s_a2d_conn_state_str[a2d->conn_stat.state], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){ - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_NONE); + esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE); } break; } diff --git a/examples/bluetooth/a2dp_sink/main/main.c b/examples/bluetooth/a2dp_sink/main/main.c index 6a08fdc8a4..7145e87c77 100644 --- a/examples/bluetooth/a2dp_sink/main/main.c +++ b/examples/bluetooth/a2dp_sink/main/main.c @@ -187,7 +187,7 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) esp_avrc_ct_register_callback(bt_app_rc_ct_cb); /* set discoverable and connectable mode, wait to be connected */ - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); break; } default: diff --git a/examples/bluetooth/a2dp_source/main/main.c b/examples/bluetooth/a2dp_source/main/main.c index 448a0fec12..dbf7618b8c 100644 --- a/examples/bluetooth/a2dp_source/main/main.c +++ b/examples/bluetooth/a2dp_source/main/main.c @@ -328,7 +328,7 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) esp_a2d_source_init(); /* set discoverable and connectable mode */ - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); /* start device discovery */ ESP_LOGI(BT_AV_TAG, "Starting device discovery..."); @@ -434,7 +434,7 @@ static void bt_app_av_state_connecting(uint16_t event, void *param) ESP_LOGI(BT_AV_TAG, "a2dp connected"); s_a2d_state = APP_AV_STATE_CONNECTED; s_media_state = APP_AV_MEDIA_STATE_IDLE; - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_NONE); + esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE); } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { s_a2d_state = APP_AV_STATE_UNCONNECTED; } @@ -530,7 +530,7 @@ static void bt_app_av_state_connected(uint16_t event, void *param) if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { ESP_LOGI(BT_AV_TAG, "a2dp disconnected"); s_a2d_state = APP_AV_STATE_UNCONNECTED; - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); } break; } @@ -564,7 +564,7 @@ static void bt_app_av_state_disconnecting(uint16_t event, void *param) if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { ESP_LOGI(BT_AV_TAG, "a2dp disconnected"); s_a2d_state = APP_AV_STATE_UNCONNECTED; - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); } break; } diff --git a/examples/bluetooth/bt_discovery/main/bt_discovery.c b/examples/bluetooth/bt_discovery/main/bt_discovery.c index 9fa3a7561a..af753cb09e 100644 --- a/examples/bluetooth/bt_discovery/main/bt_discovery.c +++ b/examples/bluetooth/bt_discovery/main/bt_discovery.c @@ -255,7 +255,7 @@ void bt_app_gap_start_up(void) esp_bt_dev_set_device_name(dev_name); /* set discoverable and connectable mode, wait to be connected */ - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); /* register GAP callback function */ esp_bt_gap_register_callback(bt_app_gap_cb); diff --git a/examples/bluetooth/bt_spp_acceptor/main/example_spp_acceptor_demo.c b/examples/bluetooth/bt_spp_acceptor/main/example_spp_acceptor_demo.c index e73241e300..feadac1521 100644 --- a/examples/bluetooth/bt_spp_acceptor/main/example_spp_acceptor_demo.c +++ b/examples/bluetooth/bt_spp_acceptor/main/example_spp_acceptor_demo.c @@ -57,7 +57,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) case ESP_SPP_INIT_EVT: ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT"); esp_bt_dev_set_device_name(EXCAMPLE_DEVICE_NAME); - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); esp_spp_start_srv(sec_mask,role_slave, 0, SPP_SERVER_NAME); break; case ESP_SPP_DISCOVERY_COMP_EVT: diff --git a/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c b/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c index 91cf6940bd..a805b73e8b 100644 --- a/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c +++ b/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c @@ -104,7 +104,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) case ESP_SPP_INIT_EVT: ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT"); esp_bt_dev_set_device_name(EXCAMPLE_DEVICE_NAME); - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps); break; diff --git a/examples/bluetooth/bt_spp_vfs_acceptor/main/example_spp_vfs_acceptor_demo.c b/examples/bluetooth/bt_spp_vfs_acceptor/main/example_spp_vfs_acceptor_demo.c index fdd75cc869..a24d691ecf 100644 --- a/examples/bluetooth/bt_spp_vfs_acceptor/main/example_spp_vfs_acceptor_demo.c +++ b/examples/bluetooth/bt_spp_vfs_acceptor/main/example_spp_vfs_acceptor_demo.c @@ -76,7 +76,7 @@ static void esp_spp_cb(uint16_t e, void *p) case ESP_SPP_INIT_EVT: ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT"); esp_bt_dev_set_device_name(EXCAMPLE_DEVICE_NAME); - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); esp_spp_start_srv(sec_mask,role_slave, 0, SPP_SERVER_NAME); break; case ESP_SPP_DISCOVERY_COMP_EVT: diff --git a/examples/bluetooth/bt_spp_vfs_initiator/main/example_spp_vfs_initiator_demo.c b/examples/bluetooth/bt_spp_vfs_initiator/main/example_spp_vfs_initiator_demo.c index 4a29025536..ea8e79987c 100644 --- a/examples/bluetooth/bt_spp_vfs_initiator/main/example_spp_vfs_initiator_demo.c +++ b/examples/bluetooth/bt_spp_vfs_initiator/main/example_spp_vfs_initiator_demo.c @@ -114,7 +114,7 @@ static void esp_spp_cb(uint16_t e, void *p) case ESP_SPP_INIT_EVT: ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT"); esp_bt_dev_set_device_name(EXCAMPLE_DEVICE_NAME); - esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps); break; -- 2.40.0