]> granicus.if.org Git - esp-idf/commitdiff
Component/bt: add only accept sepecified Authentication
authorzhiweijian <zhiweijian@espressif.com>
Thu, 27 Sep 2018 08:22:31 +0000 (16:22 +0800)
committerzhiweijian <zhiweijian@espressif.com>
Sat, 29 Sep 2018 13:23:55 +0000 (21:23 +0800)
20 files changed:
components/bt/bluedroid/api/include/api/esp_gap_ble_api.h
components/bt/bluedroid/bta/dm/bta_dm_act.c
components/bt/bluedroid/bta/dm/bta_dm_co.c
components/bt/bluedroid/bta/include/bta/bta_api.h
components/bt/bluedroid/bta/include/bta/bta_dm_co.h
components/bt/bluedroid/btc/core/btc_dm.c
components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c
components/bt/bluedroid/common/include/common/bte_appl.h
components/bt/bluedroid/stack/btm/btm_ble.c
components/bt/bluedroid/stack/include/stack/btm_api.h
components/bt/bluedroid/stack/include/stack/btm_ble_api.h
components/bt/bluedroid/stack/include/stack/smp_api.h
components/bt/bluedroid/stack/smp/include/smp_int.h
components/bt/bluedroid/stack/smp/smp_act.c
components/bt/bluedroid/stack/smp/smp_api.c
components/bt/bluedroid/stack/smp/smp_utils.c
examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c
examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c
examples/bluetooth/gatt_server/main/gatts_demo.c
examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c

index c4a5b95f9f6479865eac26f08b3565ddc49f1f23..2ca5d2f790a20a478f5f27c3f8936f131776f1bc 100644 (file)
@@ -60,6 +60,9 @@ typedef uint8_t esp_ble_key_type_t;
 #define ESP_LE_AUTH_REQ_SC_MITM_BOND        (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY | ESP_LE_AUTH_BOND)   /*!< 1101 */  /* relate to BTM_LE_AUTH_REQ_SC_MITM_BOND in stack/btm_api.h */
 typedef uint8_t   esp_ble_auth_req_t;         /*!< combination of the above bit pattern */
 
+#define ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE 0
+#define ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE  1
+
 /* relate to BTM_IO_CAP_xxx in stack/btm_api.h */
 #define ESP_IO_CAP_OUT                      0   /*!< DisplayOnly */         /* relate to BTM_IO_CAP_OUT in stack/btm_api.h */
 #define ESP_IO_CAP_IO                       1   /*!< DisplayYesNo */        /* relate to BTM_IO_CAP_IO in stack/btm_api.h */
@@ -266,6 +269,7 @@ typedef enum {
     ESP_BLE_SM_MAX_KEY_SIZE,
     ESP_BLE_SM_SET_STATIC_PASSKEY,
     ESP_BLE_SM_CLEAR_STATIC_PASSKEY,
+    ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH,
     ESP_BLE_SM_MAX_PARAM,
 } esp_ble_sm_param_t;
 
@@ -511,6 +515,7 @@ typedef struct
     uint8_t               fail_reason;           /*!< The HCI reason/error code for when success=FALSE */
     esp_ble_addr_type_t   addr_type;             /*!< Peer device address type */
     esp_bt_dev_type_t     dev_type;              /*!< Device type */
+    esp_ble_auth_req_t    auth_mode;             /*!< authentication mode */
 } esp_ble_auth_cmpl_t;                           /*!< The ble authentication complete cb type */
 
 /**
index 26d71f06c3686c3d6edfd48e4afca29d478061d9..95a17a4807076e5fff89a304ac8602316393337e 100644 (file)
@@ -4294,9 +4294,11 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D
 
     memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
     switch (event) {
-    case BTM_LE_IO_REQ_EVT:
+    case BTM_LE_IO_REQ_EVT: {
         // #if (BT_SSP_INCLUDED == TRUE)
-
+        UINT8 enable = bta_dm_co_ble_get_accept_auth_enable();
+        UINT8 origin_auth = bta_dm_co_ble_get_auth_req();
+        BTM_BleSetAcceptAuthMode(enable, origin_auth);
         bta_dm_co_ble_io_req(bda,
                              &p_data->io_req.io_cap,
                              &p_data->io_req.oob_data,
@@ -4311,6 +4313,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D
         APPL_TRACE_EVENT("io mitm: %d oob_data:%d\n", p_data->io_req.auth_req, p_data->io_req.oob_data);
 
         break;
+    }
 
     case BTM_LE_SEC_REQUEST_EVT:
         bdcpy(sec_event.ble_req.bd_addr, bda);
@@ -4386,7 +4389,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D
 
             }
         }
-
+        sec_event.auth_cmpl.auth_mode = p_data->complt.auth_mode;
         if (bta_dm_cb.p_sec_cback) {
             //bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
             bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
index 01641ba9c70653759f0a87b7e7280097822eb67e..586da2865e66169db2586ccabc96ffcecc8f1b7d 100644 (file)
 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
 #include "common/bte_appl.h"
+
+#define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE 0
+#define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE  1
+
 tBTE_APPL_CFG bte_appl_cfg = {
 #if SMP_INCLUDED == TRUE
     BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
@@ -40,7 +44,8 @@ tBTE_APPL_CFG bte_appl_cfg = {
     BTM_LOCAL_IO_CAPS_BLE,
     BTM_BLE_INITIATOR_KEY_SIZE,
     BTM_BLE_RESPONDER_KEY_SIZE,
-    BTM_BLE_MAX_KEY_SIZE
+    BTM_BLE_MAX_KEY_SIZE,
+    BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE
 };
 #endif
 
@@ -357,7 +362,7 @@ void bta_dm_co_ble_io_req(BD_ADDR bd_addr,  tBTA_IO_CAP *p_io_cap,
 #endif  ///SMP_INCLUDED == TRUE
 }
 
-void bta_dm_co_ble_set_io_cap(UINT8   ble_io_cap)
+void bta_dm_co_ble_set_io_cap(UINT8 ble_io_cap)
 {
 #if (SMP_INCLUDED == TRUE)
     if(ble_io_cap < BTM_IO_CAP_MAX ) {
@@ -368,7 +373,7 @@ void bta_dm_co_ble_set_io_cap(UINT8   ble_io_cap)
 #endif  ///SMP_INCLUDED == TRUE
 }
 
-void bta_dm_co_ble_set_auth_req(UINT8   ble_auth_req)
+void bta_dm_co_ble_set_auth_req(UINT8 ble_auth_req)
 {
 #if (SMP_INCLUDED == TRUE)
     bte_appl_cfg.ble_auth_req = ble_auth_req;
@@ -401,5 +406,32 @@ void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size)
     }
 #endif  ///SMP_INCLUDED == TRUE
 }
+
+void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)
+{
+#if (SMP_INCLUDED == TRUE)
+    if (enable) {
+        enable = BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE;
+    }
+    bte_appl_cfg.ble_accept_auth_enable = enable;
+#endif  ///SMP_INCLUDED == TRUE
+}
+
+UINT8 bta_dm_co_ble_get_accept_auth_enable(void)
+{
+#if (SMP_INCLUDED == TRUE)
+    return bte_appl_cfg.ble_accept_auth_enable;
+#endif  ///SMP_INCLUDED == TRUE
+    return 0;
+}
+
+UINT8 bta_dm_co_ble_get_auth_req(void)
+{
+#if (SMP_INCLUDED == TRUE)
+    return bte_appl_cfg.ble_auth_req;
+#endif  ///SMP_INCLUDED == TRUE
+    return 0;
+}
+
 #endif
 
index 1546389bed7de4ef9d4c7a7bd5bef91965ce4202..22a56f3bf65e979b29a6b829ae1eca5894262ff1 100644 (file)
@@ -765,6 +765,7 @@ typedef struct {
     UINT8           fail_reason;        /* The HCI reason/error code for when success=FALSE */
     tBLE_ADDR_TYPE  addr_type;          /* Peer device address type */
     tBT_DEVICE_TYPE dev_type;
+    UINT8           auth_mode;
 } tBTA_DM_AUTH_CMPL;
 
 
index 3d49a6987b4f0fc6e6452c8415efe8eb38cc437f..c69a266eaf23121f227059966eb4fb2851576ff3 100644 (file)
@@ -204,4 +204,10 @@ extern void bta_dm_co_ble_set_init_key_req(UINT8 init_key);
 extern void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key);
 
 extern void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size);
+
+extern void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable);
+
+extern UINT8 bta_dm_co_ble_get_accept_auth_enable(void);
+
+extern UINT8 bta_dm_co_ble_get_auth_req(void);
 #endif
index 985d578bdf25debf4f2e65567ee925155987942b..fa50a99c73a50fff5bf3cb9ebb6c099bd9f01076 100644 (file)
@@ -671,6 +671,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
         param.ble_security.auth_cmpl.key_present = p_data->auth_cmpl.key_present;
         memcpy(param.ble_security.auth_cmpl.bd_addr, p_data->auth_cmpl.bd_addr, sizeof(BD_ADDR));
         memcpy(param.ble_security.auth_cmpl.key, p_data->auth_cmpl.key, sizeof(LINK_KEY));
+        param.ble_security.auth_cmpl.auth_mode = p_data->auth_cmpl.auth_mode;
         btc_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
         break;
     }
index e3c4262f8c128540252f264d367153bee1924d9c..a64b66debff45c6317d98d4036f595a1f5c81de3 100644 (file)
@@ -1138,6 +1138,12 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
                 BTA_DmBleSetStaticPasskey(false, 0);
                 break;
             }
+            case ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: {
+                uint8_t enable = 0;
+                STREAM_TO_UINT8(enable, value);
+                bta_dm_co_ble_set_accept_auth_enable(enable);
+                break;
+            }
             default:
                 break;
         }
index 47a0184b7fb0ad2c4d21525431512552453c3d98..4fe465c35aee0f90fc320d36749450ddc081372a 100644 (file)
@@ -31,6 +31,7 @@ typedef struct {
     UINT8   ble_init_key;
     UINT8   ble_resp_key;
     UINT8   ble_max_key_size;
+    UINT8   ble_accept_auth_enable;
 #endif
 
 } tBTE_APPL_CFG;
index 617f818bdaf3f7c01baac4a91b5bcb24a8b648b1..259a54ffb264f1d03ca8972928f994dc7a88e2d7 100644 (file)
@@ -423,12 +423,45 @@ void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
 #endif
 }
 
+/*******************************************************************************
+**
+** Function         BTM_BleSetStaticPasskey
+**
+** Description      This function is called to set static passkey
+**
+**
+** Parameters:      add          - set static passkey when add is TRUE
+**                                 clear static passkey when add is FALSE
+**                  passkey      - static passkey
+**
+**
+*******************************************************************************/
 void BTM_BleSetStaticPasskey(BOOLEAN add, UINT32 passkey)
 {
 #if SMP_INCLUDED == TRUE
     SMP_SetStaticPasskey(add, passkey);
 #endif
 }
+
+/*******************************************************************************
+**
+** Function         BTM_BleSetAcceptAuthMode
+**
+** Description      This function is called to set only accept specified Authentication
+**
+**
+** Parameters:      enable         - Whether to enable this function
+**
+**                  auth_mode      - Authentication mode
+**
+**
+*******************************************************************************/
+void BTM_BleSetAcceptAuthMode(UINT8 enable, UINT8 auth_mode)
+{
+#if SMP_INCLUDED == TRUE
+    SMP_SetAcceptAuthMode(enable, auth_mode);
+#endif
+}
 /*******************************************************************************
 **
 ** Function         BTM_BleConfirmReply
index c483268aec12bac61c99551c8c2c16ddb8b1c1db..f686a12175240677f30c951a4ad56f9e104c4c2e 100644 (file)
@@ -1642,6 +1642,7 @@ typedef struct {
     UINT8 sec_level;
     BOOLEAN is_pair_cancel;
     BOOLEAN smp_over_br;
+    tSMP_AUTH_REQ auth_mode;
 } tBTM_LE_COMPLT;
 #endif
 
index e0d67ee63bea55a3b841cc027132a08670613862..cadf970edb401abf0ee8772d94ba9d55d55fa067 100644 (file)
@@ -1332,6 +1332,21 @@ void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey);
 *******************************************************************************/
 void BTM_BleSetStaticPasskey(BOOLEAN add, UINT32 passkey);
 
+/*******************************************************************************
+**
+** Function         BTM_BleSetAcceptAuthMode
+**
+** Description      This function is called to set only accept specified Authentication
+**
+**
+** Parameters:      enable         - Whether to enable this function
+**
+**                  auth_mode      - Authentication mode
+**
+**
+*******************************************************************************/
+void BTM_BleSetAcceptAuthMode(UINT8 enable, UINT8 auth_mode);
+
 /*******************************************************************************
 **
 ** Function         BTM_BleConfirmReply
index 6a3ca5afa82b1cec669cfc277688e1f5b743754d..b4b6af14b362013af30449ec54e41cab0dd68907 100644 (file)
@@ -224,6 +224,7 @@ typedef struct {
     tSMP_SEC_LEVEL sec_level;
     BOOLEAN is_pair_cancel;
     BOOLEAN smp_over_br;
+    tSMP_AUTH_REQ auth_mode;
 } tSMP_CMPL;
 
 typedef struct {
@@ -416,6 +417,8 @@ extern void SMP_PasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey);
 *******************************************************************************/
 extern void SMP_SetStaticPasskey (BOOLEAN add, UINT32 passkey);
 
+extern void SMP_SetAcceptAuthMode (UINT8 enable, UINT8 auth_mode);
+
 /*******************************************************************************
 **
 ** Function         SMP_ConfirmReply
index 029869efcc222180ab3c98617b2ee2f0362f9c72..55bf3e279b56148924853a0687f8181ff17f217d 100644 (file)
@@ -335,6 +335,8 @@ typedef struct {
     BOOLEAN         wait_for_authorization_complete;
     BOOLEAN         use_static_passkey;
     UINT32          static_passkey;
+    BOOLEAN         accept_specified_sec_auth;
+    tSMP_AUTH_REQ   origin_loc_auth_req;
 } tSMP_CB;
 
 /* Server Action functions are of this type */
index bf3fe7def6ac7b63f2be028dd49fef3f4d8a8c77..2534a587ef6171cb75a1e3605674c52d139ec7ec 100644 (file)
@@ -551,6 +551,14 @@ void smp_proc_pair_cmd(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
                 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
                 return;
             }
+            if(p_cb->accept_specified_sec_auth) {
+                if((p_cb->origin_loc_auth_req & p_cb->peer_auth_req & p_cb->loc_auth_req) != p_cb->origin_loc_auth_req ) {
+                    SMP_TRACE_ERROR("%s pairing failed - slave requires 0x%x auth but peer auth req 0x%x local auth req 0x%x",
+                                    __func__, p_cb->origin_loc_auth_req, p_cb->peer_auth_req, p_cb->loc_auth_req);
+                    reason = SMP_PAIR_AUTH_FAIL;
+                    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
+                }
+            }
 
             if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
                 if (smp_request_oob_data(p_cb)) {
@@ -573,6 +581,15 @@ void smp_proc_pair_cmd(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
             return;
         }
 
+        if (p_cb->accept_specified_sec_auth) {
+            if ((p_cb->origin_loc_auth_req & p_cb->peer_auth_req & p_cb->loc_auth_req) != p_cb->origin_loc_auth_req ) {
+                SMP_TRACE_ERROR("%s pairing failed - master requires 0x%x auth but peer auth req 0x%x local auth req 0x%x",
+                                    __func__, p_cb->origin_loc_auth_req, p_cb->peer_auth_req, p_cb->loc_auth_req);
+                reason = SMP_PAIR_AUTH_FAIL;
+                smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
+            }
+        }
+
         if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
             if (smp_request_oob_data(p_cb)) {
                 return;
index ecd222c22adc54fd8a362d5bc477b47c48f3bf58..8ec1394dd87fa53830b881ed68ec8d91f7a3fb39 100644 (file)
@@ -328,6 +328,19 @@ void SMP_PasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
     return;
 }
 
+/*******************************************************************************
+**
+** Function         SMP_SetStaticPasskey
+**
+** Description      This function is called to set static passkey
+**
+**
+** Parameters:      add          - set static passkey when add is TRUE
+**                                 clear static passkey when add is FALSE
+**                  passkey      - static passkey
+**
+**
+*******************************************************************************/
 void SMP_SetStaticPasskey (BOOLEAN add, UINT32 passkey)
 {
     SMP_TRACE_DEBUG("static passkey %6d", passkey);
@@ -340,6 +353,28 @@ void SMP_SetStaticPasskey (BOOLEAN add, UINT32 passkey)
         p_cb->use_static_passkey = false;
     }
 }
+
+/*******************************************************************************
+**
+** Function         SMP_SetAcceptAuthMode
+**
+** Description      This function is called to set only accept specified Authentication
+**
+**
+** Parameters:      enable         - Whether to enable this function
+**
+**                  auth_mode      - Authentication mode
+**
+**
+*******************************************************************************/
+void SMP_SetAcceptAuthMode (UINT8 enable, UINT8 auth_mode)
+{
+    tSMP_CB *p_cb = & smp_cb;
+
+    p_cb->accept_specified_sec_auth = enable;
+    p_cb->origin_loc_auth_req = auth_mode;
+    
+}
 /*******************************************************************************
 **
 ** Function         SMP_ConfirmReply
index cdb9c63fa7fb8c4d786ab145893635b3d79bf1fc..eeaf66b2955a6a14e6fa6153c82db5ad60e19a48 100644 (file)
@@ -970,9 +970,10 @@ void smp_proc_pairing_cmpl(tSMP_CB *p_cb)
 
     evt_data.cmplt.reason = p_cb->status;
     evt_data.cmplt.smp_over_br = p_cb->smp_over_br;
-
+    evt_data.cmplt.auth_mode = 0;
     if (p_cb->status == SMP_SUCCESS) {
         evt_data.cmplt.sec_level = p_cb->sec_level;
+        evt_data.cmplt.auth_mode = (p_cb->peer_auth_req & p_cb->loc_auth_req);
     }
 
     evt_data.cmplt.is_pair_cancel  = FALSE;
index ed842b64cfae3687d515e60efd1a24e6f4be5f6a..cfdad9f28029d3689861cdca739b11c31c5f621d 100644 (file)
@@ -126,6 +126,39 @@ static const char *esp_key_type_to_str(esp_ble_key_type_t key_type)
      return key_str;
 }
 
+static char *esp_auth_req_to_str(esp_ble_auth_req_t auth_req)
+{
+   char *auth_str = NULL;
+   switch(auth_req) {
+    case ESP_LE_AUTH_NO_BOND:
+        auth_str = "ESP_LE_AUTH_NO_BOND";
+        break;
+    case ESP_LE_AUTH_BOND:
+        auth_str = "ESP_LE_AUTH_BOND";
+        break;
+    case ESP_LE_AUTH_REQ_MITM:
+        auth_str = "ESP_LE_AUTH_REQ_MITM";
+        break;
+    case ESP_LE_AUTH_REQ_SC_ONLY:
+        auth_str = "ESP_LE_AUTH_REQ_SC_ONLY";
+        break;
+    case ESP_LE_AUTH_REQ_SC_BOND:
+        auth_str = "ESP_LE_AUTH_REQ_SC_BOND";
+        break;
+    case ESP_LE_AUTH_REQ_SC_MITM:
+        auth_str = "ESP_LE_AUTH_REQ_SC_MITM";
+        break;
+    case ESP_LE_AUTH_REQ_SC_MITM_BOND:
+        auth_str = "ESP_LE_AUTH_REQ_SC_MITM_BOND";
+        break;
+    default:
+        auth_str = "INVALID BLE AUTH REQ";
+        break;
+   }
+
+   return auth_str;
+}
+
 static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
 {
     esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
@@ -302,7 +335,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
         ESP_LOGI(GATTC_TAG, "Write char success ");
         break;
     case ESP_GATTC_DISCONNECT_EVT:
-        ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
+        ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = 0x%x", p_data->disconnect.reason);
         connect = false;
         get_service = false;
         break;
@@ -379,6 +412,11 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
                 (bd_addr[4] << 8) + bd_addr[5]);
         ESP_LOGI(GATTC_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type);
         ESP_LOGI(GATTC_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail");
+        if (!param->ble_security.auth_cmpl.success) {
+            ESP_LOGI(GATTC_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason);
+        } else {
+            ESP_LOGI(GATTC_TAG, "auth mode = %s",esp_auth_req_to_str(param->ble_security.auth_cmpl.auth_mode));    
+        }
         break;
     }
     case ESP_GAP_BLE_SCAN_RESULT_EVT: {
index 88d2e74fbe9bd99ee3fc23912716ff9571b7c607..06be1a72e64f5d64d4ab2bc8658a9d136f37f665 100644 (file)
@@ -227,6 +227,39 @@ static char *esp_key_type_to_str(esp_ble_key_type_t key_type)
    return key_str;
 }
 
+static char *esp_auth_req_to_str(esp_ble_auth_req_t auth_req)
+{
+   char *auth_str = NULL;
+   switch(auth_req) {
+    case ESP_LE_AUTH_NO_BOND:
+        auth_str = "ESP_LE_AUTH_NO_BOND";
+        break;
+    case ESP_LE_AUTH_BOND:
+        auth_str = "ESP_LE_AUTH_BOND";
+        break;
+    case ESP_LE_AUTH_REQ_MITM:
+        auth_str = "ESP_LE_AUTH_REQ_MITM";
+        break;
+    case ESP_LE_AUTH_REQ_SC_ONLY:
+        auth_str = "ESP_LE_AUTH_REQ_SC_ONLY";
+        break;
+    case ESP_LE_AUTH_REQ_SC_BOND:
+        auth_str = "ESP_LE_AUTH_REQ_SC_BOND";
+        break;
+    case ESP_LE_AUTH_REQ_SC_MITM:
+        auth_str = "ESP_LE_AUTH_REQ_SC_MITM";
+        break;
+    case ESP_LE_AUTH_REQ_SC_MITM_BOND:
+        auth_str = "ESP_LE_AUTH_REQ_SC_MITM_BOND";
+        break;
+    default:
+        auth_str = "INVALID BLE AUTH REQ";
+        break;
+   }
+
+   return auth_str;
+}
+
 static void show_bonded_devices(void)
 {
     int dev_num = esp_ble_get_bond_device_num();
@@ -322,6 +355,8 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
         ESP_LOGI(GATTS_TABLE_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail");
         if(!param->ble_security.auth_cmpl.success) {
             ESP_LOGI(GATTS_TABLE_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason);
+        } else {
+            ESP_LOGI(GATTS_TABLE_TAG, "auth mode = %s",esp_auth_req_to_str(param->ble_security.auth_cmpl.auth_mode));
         }
         show_bonded_devices();
         break;
@@ -398,7 +433,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event,
             esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
             break;
         case ESP_GATTS_DISCONNECT_EVT:
-            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_DISCONNECT_EVT");
+            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
             /* start advertising again when missing the connect */
             esp_ble_gap_start_advertising(&heart_rate_adv_params);
             break;
@@ -525,10 +560,12 @@ void app_main()
     uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
     //set static passkey
     uint32_t passkey = 123456;
+    uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
     esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
     esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
     esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
     esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
+    esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
     /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribut to you,
     and the response key means which key you can distribut to the Master;
     If your BLE device act as a master, the response key means you hope which types of key of the slave should distribut to you,
index 57f17b1dd26d8726c02cbb88e4666240c9cfa78a..1cb7afb9e92c7f498b0a9796b53abe5d3a7054e9 100644 (file)
@@ -481,7 +481,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
         break;
     }
     case ESP_GATTS_DISCONNECT_EVT:
-        ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
+        ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
         esp_ble_gap_start_advertising(&adv_params);
         break;
     case ESP_GATTS_CONF_EVT:
index 704e735b904c24c654ac3a95cf923f5c2ead4363..92b1c06301177efabdab798415d63c07fef7ba5b 100644 (file)
@@ -452,7 +452,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
             esp_ble_gap_update_conn_params(&conn_params);
             break;
         case ESP_GATTS_DISCONNECT_EVT:
-            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_DISCONNECT_EVT, reason = %d", param->disconnect.reason);
+            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_DISCONNECT_EVT, reason = 0x%x", param->disconnect.reason);
             esp_ble_gap_start_advertising(&adv_params);
             break;
         case ESP_GATTS_CREAT_ATTR_TAB_EVT:{