]> granicus.if.org Git - esp-idf/commitdiff
Modify esp_bt_gap_set_scan_mode() to be able to set connection modes as well as disco...
authorHrishikesh Dhayagude <hrishi@espressif.com>
Mon, 9 Apr 2018 10:30:48 +0000 (16:00 +0530)
committerHrishikesh Dhayagude <hrishi@espressif.com>
Wed, 13 Mar 2019 05:54:00 +0000 (11:24 +0530)
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

13 files changed:
components/bt/bluedroid/api/esp_gap_bt_api.c
components/bt/bluedroid/api/include/api/esp_gap_bt_api.h
components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c
components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h
examples/bluetooth/a2dp_gatts_coex/main/main.c
examples/bluetooth/a2dp_sink/main/bt_app_av.c
examples/bluetooth/a2dp_sink/main/main.c
examples/bluetooth/a2dp_source/main/main.c
examples/bluetooth/bt_discovery/main/bt_discovery.c
examples/bluetooth/bt_spp_acceptor/main/example_spp_acceptor_demo.c
examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c
examples/bluetooth/bt_spp_vfs_acceptor/main/example_spp_vfs_acceptor_demo.c
examples/bluetooth/bt_spp_vfs_initiator/main/example_spp_vfs_initiator_demo.c

index ac0326e11f50f2c2c01a5666d2a6ffc561c4e579..ff3f945c480ef23d483115fa860c9c8e0518cc9e 100644 (file)
@@ -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);
 }
index f5b2c8f15a2678579ff3976e8b9067dbabb3f8ce..084cabec23e1ae8937010ef63aac03a8beca8687 100644 (file)
@@ -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.
index a5af828e1a50cb20d163718c6f105198c29c5081..8de802f2ea5dd9451a46f2f721c540959a74512e 100644 (file)
@@ -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: {
index 67e03cb3f96285312dd7f26d09a5a0bb5ead2c2f..51e685965b766e9d321b2dec03eedc09f6652ac7 100644 (file)
@@ -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
index 3b042714a1b941e13eb782874a946b7efb5e3f37..e3be5becf6c20ee4d2479f7c23662a6470cd5f30 100644 (file)
@@ -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:
index d9bbbfbed8603ffc01c221f8a0fce6a6d71b6942..304e826e4b7677b366271120c5e41e81e4af41d2 100644 (file)
@@ -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;
     }
index 6a08fdc8a485975df0094597cb6f52554b77d63c..7145e87c77714dc47adea1abf212b33d33d9b618 100644 (file)
@@ -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:
index 448a0fec12ce265091b731c151a0cda9c5016810..dbf7618b8c0b428b55ab5b0e611e5cf99cd2a9d8 100644 (file)
@@ -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;
     }
index 9fa3a7561aa145f220e56ab6ab50ee1b491c9ad6..af753cb09e8f60c5da0dc7e8e4fca2c66b76be92 100644 (file)
@@ -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);
index e73241e300f170efd3eb5139f9c800025bcbca16..feadac1521d32c3ae73636e11ee24f1841241d17 100644 (file)
@@ -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:
index 91cf6940bd255983c257030ccb49c8840c9fa9b3..a805b73e8be02b6a6eb6ff25c7863de5b6d5c7c5 100644 (file)
@@ -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;
index fdd75cc8696988c0c18ebc47b7a9778c2deeb2e8..a24d691ecf3078a2d1f35eea8a1fff9267b3104c 100644 (file)
@@ -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:
index 4a290255369ec48b221d03b3f1931fba9fa47ed7..ea8e79987ccd8f64b79e7b09d83c4438401a9378 100644 (file)
@@ -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;