Merge branch 'feature/btdm_legacy_paring_v3.1' into 'release/v3.1'

component/bt: Add Legacy Pairing (backport 3.1)

See merge request idf/esp-idf!3193
This commit is contained in:
Jiang Jiang Jian 2018-09-07 14:44:19 +08:00
commit 35785d08fd
17 changed files with 530 additions and 85 deletions

View File

@ -240,4 +240,50 @@ esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list)
return (ret == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (ret == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
esp_err_t esp_bt_gap_set_pin(esp_bt_pin_type_t pin_type, uint8_t pin_code_len, esp_bt_pin_code_t pin_code)
{
btc_msg_t msg;
btc_gap_bt_args_t arg;
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_SET_PIN_TYPE;
arg.set_pin_type.pin_type = pin_type;
if (pin_type == ESP_BT_PIN_TYPE_FIXED){
arg.set_pin_type.pin_code_len = pin_code_len;
memcpy(arg.set_pin_type.pin_code, pin_code, pin_code_len);
} else {
arg.set_pin_type.pin_code_len = 0;
memset(arg.set_pin_type.pin_code, 0, ESP_BT_PIN_CODE_LEN);
}
return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_code_len, esp_bt_pin_code_t pin_code)
{
btc_msg_t msg;
btc_gap_bt_args_t arg;
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_PIN_REPLY;
arg.pin_reply.accept = accept;
arg.pin_reply.pin_code_len = pin_code_len;
memcpy(arg.pin_reply.bda.address, bd_addr, sizeof(esp_bd_addr_t));
memcpy(arg.pin_reply.pin_code, pin_code, pin_code_len);
return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
#endif /* #if BTC_GAP_BT_INCLUDED == TRUE */ #endif /* #if BTC_GAP_BT_INCLUDED == TRUE */

View File

@ -102,6 +102,14 @@ typedef enum {
ESP_BT_COD_SRVC_INFORMATION = 0x400, /*!< Information, e.g., WEB-server, WAP-server */ ESP_BT_COD_SRVC_INFORMATION = 0x400, /*!< Information, e.g., WEB-server, WAP-server */
} esp_bt_cod_srvc_t; } esp_bt_cod_srvc_t;
typedef enum{
ESP_BT_PIN_TYPE_VARIABLE = 0, /*!< Refer to BTM_PIN_TYPE_VARIABLE */
ESP_BT_PIN_TYPE_FIXED = 1, /*!< Refer to BTM_PIN_TYPE_FIXED */
} esp_bt_pin_type_t;
#define ESP_BT_PIN_CODE_LEN 16 /*!< Max pin code length */
typedef uint8_t esp_bt_pin_code_t[ESP_BT_PIN_CODE_LEN]; /*!< Pin Code (upto 128 bits) MSB is 0 */
/// Bits of major service class field /// Bits of major service class field
#define ESP_BT_COD_SRVC_BIT_MASK (0xffe000) /*!< Major service bit mask */ #define ESP_BT_COD_SRVC_BIT_MASK (0xffe000) /*!< Major service bit mask */
#define ESP_BT_COD_SRVC_BIT_OFFSET (13) /*!< Major service bit offset */ #define ESP_BT_COD_SRVC_BIT_OFFSET (13) /*!< Major service bit offset */
@ -149,6 +157,7 @@ typedef enum {
ESP_BT_GAP_RMT_SRVCS_EVT, /*!< get remote services event */ ESP_BT_GAP_RMT_SRVCS_EVT, /*!< get remote services event */
ESP_BT_GAP_RMT_SRVC_REC_EVT, /*!< get remote service record event */ ESP_BT_GAP_RMT_SRVC_REC_EVT, /*!< get remote service record event */
ESP_BT_GAP_AUTH_CMPL_EVT, /*!< AUTH complete event */ ESP_BT_GAP_AUTH_CMPL_EVT, /*!< AUTH complete event */
ESP_BT_GAP_PIN_REQ_EVT, /*!< Legacy Pairing Pin code request */
ESP_BT_GAP_READ_RSSI_DELTA_EVT, /*!< read rssi event */ ESP_BT_GAP_READ_RSSI_DELTA_EVT, /*!< read rssi event */
ESP_BT_GAP_EVT_MAX, ESP_BT_GAP_EVT_MAX,
} esp_bt_gap_cb_event_t; } esp_bt_gap_cb_event_t;
@ -216,6 +225,15 @@ typedef union {
esp_bt_status_t stat; /*!< authentication complete status */ esp_bt_status_t stat; /*!< authentication complete status */
uint8_t device_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1]; /*!< device name */ uint8_t device_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1]; /*!< device name */
} auth_cmpl; /*!< authentication complete parameter struct */ } auth_cmpl; /*!< authentication complete parameter struct */
/**
* @brief ESP_BT_GAP_PIN_REQ_EVT
*/
struct pin_req_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
bool min_16_digit; /*!< TRUE if the pin returned must be at least 16 digits */
} pin_req; /*!< pin request parameter struct */
} esp_bt_gap_cb_param_t; } esp_bt_gap_cb_param_t;
/** /**
@ -447,6 +465,38 @@ int esp_bt_gap_get_bond_device_num(void);
*/ */
esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list); esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list);
/**
* @brief Set pin type and default pin code for legacy pairing.
*
* @param[in] pin_type: Use variable or fixed pin.
* If pin_type is ESP_BT_PIN_TYPE_VARIABLE, pin_code and pin_code_len
* will be ignored, and ESP_BT_GAP_PIN_REQ_EVT will come when control
* requests for pin code.
* Else, will use fixed pin code and not callback to users.
* @param[in] pin_code_len: Length of pin_code
* @param[in] pin_code: Pin_code
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*/
esp_err_t esp_bt_gap_set_pin(esp_bt_pin_type_t pin_type, uint8_t pin_code_len, esp_bt_pin_code_t pin_code);
/**
* @brief Reply the pin_code to the peer device for legacy pairing
* when ESP_BT_GAP_PIN_REQ_EVT is coming.
*
* @param[in] bd_addr: BD address of the peer
* @param[in] accept: Pin_code reply successful or declined.
* @param[in] pin_code_len: Length of pin_code
* @param[in] pin_code: Pin_code
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*/
esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_code_len, esp_bt_pin_code_t pin_code);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -973,6 +973,21 @@ void bta_dm_bond_cancel (tBTA_DM_MSG *p_data)
} }
/*******************************************************************************
**
** Function bta_dm_set_pin_type
**
** Description Set the pin type and fixed pin
**
**
** Returns void
**
*******************************************************************************/
void bta_dm_set_pin_type (tBTA_DM_MSG *p_data)
{
BTM_SetPinType (p_data->set_pin_type.pin_type, p_data->set_pin_type.p_pin, p_data->set_pin_type.pin_len);
}
/******************************************************************************* /*******************************************************************************
** **
** Function bta_dm_pin_reply ** Function bta_dm_pin_reply

View File

@ -436,6 +436,29 @@ void BTA_DmBondCancel(BD_ADDR bd_addr)
} }
/*******************************************************************************
**
** Function BTA_DMSetPinType
**
** Description This function set pin type as BTM_PIN_TYPE_FIXED or BTM_PIN_TYPE_VARIABLE
**
**
** Returns void
**
*******************************************************************************/
void BTA_DMSetPinType (UINT8 pin_type, UINT8 *pin_code, UINT8 pin_code_len)
{
tBTA_DM_API_SET_PIN_TYPE *p_msg;
if ((p_msg = (tBTA_DM_API_SET_PIN_TYPE *) osi_malloc(sizeof(tBTA_DM_API_SET_PIN_TYPE))) != NULL) {
p_msg->hdr.event = BTA_DM_API_SET_PIN_TYPE_EVT;
p_msg->pin_type = pin_type;
p_msg->pin_len = pin_code_len;
memcpy(p_msg->p_pin, pin_code, pin_code_len);
bta_sys_sendmsg(p_msg);
}
}
/******************************************************************************* /*******************************************************************************
** **
** Function BTA_DmPinReply ** Function BTA_DmPinReply
@ -448,7 +471,6 @@ void BTA_DmBondCancel(BD_ADDR bd_addr)
** **
*******************************************************************************/ *******************************************************************************/
void BTA_DmPinReply(BD_ADDR bd_addr, BOOLEAN accept, UINT8 pin_len, UINT8 *p_pin) void BTA_DmPinReply(BD_ADDR bd_addr, BOOLEAN accept, UINT8 pin_len, UINT8 *p_pin)
{ {
tBTA_DM_API_PIN_REPLY *p_msg; tBTA_DM_API_PIN_REPLY *p_msg;

View File

@ -53,42 +53,43 @@ typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG *p_data);
const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = { const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
/* device manager local device API events */ /* device manager local device API events */
bta_dm_enable, /* 0 BTA_DM_API_ENABLE_EVT */ bta_dm_enable, /* BTA_DM_API_ENABLE_EVT */
bta_dm_disable, /* 1 BTA_DM_API_DISABLE_EVT */ bta_dm_disable, /* BTA_DM_API_DISABLE_EVT */
bta_dm_set_dev_name, /* 2 BTA_DM_API_SET_NAME_EVT */ bta_dm_set_dev_name, /* BTA_DM_API_SET_NAME_EVT */
bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */ bta_dm_set_visibility, /* BTA_DM_API_SET_VISIBILITY_EVT */
bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */ bta_dm_acl_change, /* BTA_DM_ACL_CHANGE_EVT */
bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */ bta_dm_add_device, /* BTA_DM_API_ADD_DEVICE_EVT */
bta_dm_close_acl, /* 10 BTA_DM_API_REMOVE_ACL_EVT */ bta_dm_close_acl, /* BTA_DM_API_REMOVE_ACL_EVT */
#if (SMP_INCLUDED == TRUE) #if (SMP_INCLUDED == TRUE)
/* security API events */ /* security API events */
bta_dm_bond, /* 11 BTA_DM_API_BOND_EVT */ bta_dm_bond, /* BTA_DM_API_BOND_EVT */
bta_dm_bond_cancel, /* 12 BTA_DM_API_BOND_CANCEL_EVT */ bta_dm_bond_cancel, /* BTA_DM_API_BOND_CANCEL_EVT */
bta_dm_pin_reply, /* 13 BTA_DM_API_PIN_REPLY_EVT */ bta_dm_set_pin_type, /* BTA_DM_API_SET_PIN_TYPE_EVT */
bta_dm_pin_reply, /* BTA_DM_API_PIN_REPLY_EVT */
#endif ///SMP_INCLUDED == TRUE #endif ///SMP_INCLUDED == TRUE
#if (BTA_DM_PM_INCLUDED == TRUE) #if (BTA_DM_PM_INCLUDED == TRUE)
/* power manger events */ /* power manger events */
bta_dm_pm_btm_status, /* 16 BTA_DM_PM_BTM_STATUS_EVT */ bta_dm_pm_btm_status, /* BTA_DM_PM_BTM_STATUS_EVT */
bta_dm_pm_timer, /* 17 BTA_DM_PM_TIMER_EVT*/ bta_dm_pm_timer, /* BTA_DM_PM_TIMER_EVT */
#endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */ #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
/* simple pairing events */ /* simple pairing events */
#if (SMP_INCLUDED == TRUE) #if (SMP_INCLUDED == TRUE)
bta_dm_confirm, /* 18 BTA_DM_API_CONFIRM_EVT */ bta_dm_confirm, /* BTA_DM_API_CONFIRM_EVT */
bta_dm_set_encryption, /* BTA_DM_API_SET_ENCRYPTION_EVT */ bta_dm_set_encryption, /* BTA_DM_API_SET_ENCRYPTION_EVT */
#endif ///SMP_INCLUDED == TRUE #endif ///SMP_INCLUDED == TRUE
#if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE) #if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
bta_dm_loc_oob, /* 20 BTA_DM_API_LOC_OOB_EVT */ bta_dm_loc_oob, /* BTA_DM_API_LOC_OOB_EVT */
bta_dm_ci_io_req_act, /* 21 BTA_DM_CI_IO_REQ_EVT */ bta_dm_ci_io_req_act, /* BTA_DM_CI_IO_REQ_EVT */
bta_dm_ci_rmt_oob_act, /* 22 BTA_DM_CI_RMT_OOB_EVT */ bta_dm_ci_rmt_oob_act, /* BTA_DM_CI_RMT_OOB_EVT */
#endif /* BTM_OOB_INCLUDED */ #endif /* BTM_OOB_INCLUDED */
#if BLE_INCLUDED == TRUE #if BLE_INCLUDED == TRUE
#if SMP_INCLUDED == TRUE #if SMP_INCLUDED == TRUE
bta_dm_add_blekey, /* BTA_DM_API_ADD_BLEKEY_EVT */ bta_dm_add_blekey, /* BTA_DM_API_ADD_BLEKEY_EVT */
bta_dm_add_ble_device, /* BTA_DM_API_ADD_BLEDEVICE_EVT */ bta_dm_add_ble_device, /* BTA_DM_API_ADD_BLEDEVICE_EVT */
bta_dm_ble_passkey_reply, /* BTA_DM_API_BLE_PASSKEY_REPLY_EVT */ bta_dm_ble_passkey_reply, /* BTA_DM_API_BLE_PASSKEY_REPLY_EVT */
bta_dm_ble_confirm_reply, /* BTA_DM_API_BLE_CONFIRM_REPLY_EVT */ bta_dm_ble_confirm_reply, /* BTA_DM_API_BLE_CONFIRM_REPLY_EVT */
bta_dm_security_grant, bta_dm_security_grant,
#endif ///SMP_INCLUDED == TRUE #endif ///SMP_INCLUDED == TRUE
bta_dm_ble_set_bg_conn_type, bta_dm_ble_set_bg_conn_type,
@ -96,18 +97,18 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
bta_dm_ble_set_conn_scan_params, /* BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT */ bta_dm_ble_set_conn_scan_params, /* BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT */
bta_dm_ble_set_scan_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */ bta_dm_ble_set_scan_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */
bta_dm_ble_set_scan_fil_params, /* BTA_DM_API_BLE_SCAN_FIL_PARAM_EVT */ bta_dm_ble_set_scan_fil_params, /* BTA_DM_API_BLE_SCAN_FIL_PARAM_EVT */
bta_dm_ble_observe, /* BTA_DM_API_BLE_OBSERVE_EVT*/ bta_dm_ble_observe, /* BTA_DM_API_BLE_OBSERVE_EVT */
bta_dm_ble_scan, /* BTA_DM_API_BLE_SCAN_EVT */ bta_dm_ble_scan, /* BTA_DM_API_BLE_SCAN_EVT */
bta_dm_ble_update_conn_params, /* BTA_DM_API_UPDATE_CONN_PARAM_EVT */ bta_dm_ble_update_conn_params, /* BTA_DM_API_UPDATE_CONN_PARAM_EVT */
/* This handler function added by /* This handler function added by
Yulong at 2016/9/9 to support the Yulong at 2016/9/9 to support the
random address setting for the APP */ random address setting for the APP */
bta_dm_ble_set_rand_address, /* BTA_DM_API_SET_RAND_ADDR_EVT*/ bta_dm_ble_set_rand_address, /* BTA_DM_API_SET_RAND_ADDR_EVT */
/* This handler function added by /* This handler function added by
Yulong at 2016/10/19 to support Yulong at 2016/10/19 to support
stop the ble advertising setting stop the ble advertising setting
by the APP */ by the APP */
bta_dm_ble_stop_advertising, /* BTA_DM_API_BLE_STOP_ADV_EVT*/ bta_dm_ble_stop_advertising, /* BTA_DM_API_BLE_STOP_ADV_EVT */
#if BLE_PRIVACY_SPT == TRUE #if BLE_PRIVACY_SPT == TRUE
bta_dm_ble_config_local_privacy, /* BTA_DM_API_LOCAL_PRIVACY_EVT */ bta_dm_ble_config_local_privacy, /* BTA_DM_API_LOCAL_PRIVACY_EVT */
#endif #endif
@ -119,7 +120,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
data to HCI */ data to HCI */
bta_dm_ble_set_adv_config_raw, /* BTA_DM_API_BLE_SET_ADV_CONFIG_RAW_EVT */ bta_dm_ble_set_adv_config_raw, /* BTA_DM_API_BLE_SET_ADV_CONFIG_RAW_EVT */
bta_dm_ble_set_scan_rsp, /* BTA_DM_API_BLE_SET_SCAN_RSP_EVT */ bta_dm_ble_set_scan_rsp, /* BTA_DM_API_BLE_SET_SCAN_RSP_EVT */
/* New function to allow set raw scan /* New function to allow set raw scan
response data to HCI */ response data to HCI */
bta_dm_ble_set_scan_rsp_raw, /* BTA_DM_API_BLE_SET_SCAN_RSP_RAW_EVT */ bta_dm_ble_set_scan_rsp_raw, /* BTA_DM_API_BLE_SET_SCAN_RSP_RAW_EVT */
bta_dm_ble_broadcast, /* BTA_DM_API_BLE_BROADCAST_EVT */ bta_dm_ble_broadcast, /* BTA_DM_API_BLE_BROADCAST_EVT */
@ -129,10 +130,10 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
bta_dm_scan_filter_param_setup, /* BTA_DM_API_SCAN_FILTER_SETUP_EVT */ bta_dm_scan_filter_param_setup, /* BTA_DM_API_SCAN_FILTER_SETUP_EVT */
bta_dm_enable_scan_filter, /* BTA_DM_API_SCAN_FILTER_ENABLE_EVT */ bta_dm_enable_scan_filter, /* BTA_DM_API_SCAN_FILTER_ENABLE_EVT */
#endif #endif
bta_dm_ble_multi_adv_enb, /* BTA_DM_API_BLE_MULTI_ADV_ENB_EVT*/ bta_dm_ble_multi_adv_enb, /* BTA_DM_API_BLE_MULTI_ADV_ENB_EVT */
bta_dm_ble_multi_adv_upd_param, /* BTA_DM_API_BLE_MULTI_ADV_PARAM_UPD_EVT */ bta_dm_ble_multi_adv_upd_param, /* BTA_DM_API_BLE_MULTI_ADV_PARAM_UPD_EVT */
bta_dm_ble_multi_adv_data, /* BTA_DM_API_BLE_MULTI_ADV_DATA_EVT */ bta_dm_ble_multi_adv_data, /* BTA_DM_API_BLE_MULTI_ADV_DATA_EVT */
btm_dm_ble_multi_adv_disable, /* BTA_DM_API_BLE_MULTI_ADV_DISABLE_EVT */ btm_dm_ble_multi_adv_disable, /* BTA_DM_API_BLE_MULTI_ADV_DISABLE_EVT */
bta_dm_ble_setup_storage, /* BTA_DM_API_BLE_SETUP_STORAGE_EVT */ bta_dm_ble_setup_storage, /* BTA_DM_API_BLE_SETUP_STORAGE_EVT */
bta_dm_ble_enable_batch_scan, /* BTA_DM_API_BLE_ENABLE_BATCH_SCAN_EVT */ bta_dm_ble_enable_batch_scan, /* BTA_DM_API_BLE_ENABLE_BATCH_SCAN_EVT */
bta_dm_ble_disable_batch_scan, /* BTA_DM_API_BLE_DISABLE_BATCH_SCAN_EVT */ bta_dm_ble_disable_batch_scan, /* BTA_DM_API_BLE_DISABLE_BATCH_SCAN_EVT */
@ -142,9 +143,9 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
bta_dm_ble_disconnect, /* BTA_DM_API_BLE_DISCONNECT_EVT */ bta_dm_ble_disconnect, /* BTA_DM_API_BLE_DISCONNECT_EVT */
#endif #endif
bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */ bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */
bta_dm_disable_test_mode, /* BTA_DM_API_DISABLE_TEST_MODE_EVT */ bta_dm_disable_test_mode, /* BTA_DM_API_DISABLE_TEST_MODE_EVT */
bta_dm_execute_callback, /* BTA_DM_API_EXECUTE_CBACK_EVT */ bta_dm_execute_callback, /* BTA_DM_API_EXECUTE_CBACK_EVT */
bta_dm_remove_all_acl, /* BTA_DM_API_REMOVE_ALL_ACL_EVT */ bta_dm_remove_all_acl, /* BTA_DM_API_REMOVE_ALL_ACL_EVT */
bta_dm_remove_device, /* BTA_DM_API_REMOVE_DEVICE_EVT */ bta_dm_remove_device, /* BTA_DM_API_REMOVE_DEVICE_EVT */

View File

@ -61,6 +61,7 @@ enum {
/* security API events */ /* security API events */
BTA_DM_API_BOND_EVT, BTA_DM_API_BOND_EVT,
BTA_DM_API_BOND_CANCEL_EVT, BTA_DM_API_BOND_CANCEL_EVT,
BTA_DM_API_SET_PIN_TYPE_EVT,
BTA_DM_API_PIN_REPLY_EVT, BTA_DM_API_PIN_REPLY_EVT,
#endif ///SMP_INCLUDED == TRUE #endif ///SMP_INCLUDED == TRUE
#if (BTA_DM_PM_INCLUDED == TRUE) #if (BTA_DM_PM_INCLUDED == TRUE)
@ -271,6 +272,14 @@ typedef struct {
tBTA_TRANSPORT transport; tBTA_TRANSPORT transport;
} tBTA_DM_API_BOND_CANCEL; } tBTA_DM_API_BOND_CANCEL;
/* data type for BTA_DM_API_SET_PIN_TYPE_EVT */
typedef struct {
BT_HDR hdr;
UINT8 pin_type;
UINT8 pin_len;
UINT8 p_pin[PIN_CODE_LEN];
} tBTA_DM_API_SET_PIN_TYPE;
/* data type for BTA_DM_API_PIN_REPLY_EVT */ /* data type for BTA_DM_API_PIN_REPLY_EVT */
typedef struct { typedef struct {
BT_HDR hdr; BT_HDR hdr;
@ -744,6 +753,7 @@ typedef union {
tBTA_DM_API_BOND_CANCEL bond_cancel; tBTA_DM_API_BOND_CANCEL bond_cancel;
tBTA_DM_API_SET_PIN_TYPE set_pin_type;
tBTA_DM_API_PIN_REPLY pin_reply; tBTA_DM_API_PIN_REPLY pin_reply;
tBTA_DM_API_LOC_OOB loc_oob; tBTA_DM_API_LOC_OOB loc_oob;
@ -1166,6 +1176,7 @@ extern void bta_dm_set_scan_config(tBTA_DM_MSG *p_data);
extern void bta_dm_vendor_spec_command(tBTA_DM_MSG *p_data); extern void bta_dm_vendor_spec_command(tBTA_DM_MSG *p_data);
extern void bta_dm_bond (tBTA_DM_MSG *p_data); extern void bta_dm_bond (tBTA_DM_MSG *p_data);
extern void bta_dm_bond_cancel (tBTA_DM_MSG *p_data); extern void bta_dm_bond_cancel (tBTA_DM_MSG *p_data);
extern void bta_dm_set_pin_type (tBTA_DM_MSG *p_data);
extern void bta_dm_pin_reply (tBTA_DM_MSG *p_data); extern void bta_dm_pin_reply (tBTA_DM_MSG *p_data);
extern void bta_dm_acl_change(tBTA_DM_MSG *p_data); extern void bta_dm_acl_change(tBTA_DM_MSG *p_data);
extern void bta_dm_add_device (tBTA_DM_MSG *p_data); extern void bta_dm_add_device (tBTA_DM_MSG *p_data);

View File

@ -1560,6 +1560,18 @@ extern void BTA_DmBondByTransport(BD_ADDR bd_addr, tBTA_TRANSPORT transport);
*******************************************************************************/ *******************************************************************************/
extern void BTA_DmBondCancel(BD_ADDR bd_addr); extern void BTA_DmBondCancel(BD_ADDR bd_addr);
/*******************************************************************************
**
** Function BTA_DMSetPinType
**
** Description This function sets pin type as BTM_PIN_TYPE_FIXED or BTM_PIN_TYPE_VARIABLE
**
**
** Returns void
**
*******************************************************************************/
extern void BTA_DMSetPinType (UINT8 pin_type, UINT8 *pin_code, UINT8 pin_code_len);
/******************************************************************************* /*******************************************************************************
** **
** Function BTA_DmPinReply ** Function BTA_DmPinReply

View File

@ -182,7 +182,7 @@ static void btc_dm_remove_ble_bonding_keys(void)
static void btc_dm_save_ble_bonding_keys(void) static void btc_dm_save_ble_bonding_keys(void)
{ {
if(!(pairing_cb.ble.is_penc_key_rcvd || pairing_cb.ble.is_pid_key_rcvd || pairing_cb.ble.is_pcsrk_key_rcvd || if (!(pairing_cb.ble.is_penc_key_rcvd || pairing_cb.ble.is_pid_key_rcvd || pairing_cb.ble.is_pcsrk_key_rcvd ||
pairing_cb.ble.is_lenc_key_rcvd || pairing_cb.ble.is_lcsrk_key_rcvd || pairing_cb.ble.is_lidk_key_rcvd)) { pairing_cb.ble.is_lenc_key_rcvd || pairing_cb.ble.is_lcsrk_key_rcvd || pairing_cb.ble.is_lidk_key_rcvd)) {
return ; return ;
} }
@ -379,6 +379,27 @@ static void btc_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
(void) status; (void) status;
} }
static void btc_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
{
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_PIN_REQ_EVT;
param.pin_req.min_16_digit = p_pin_req->min_16_digit;
memcpy(param.pin_req.bda, p_pin_req->bd_addr, ESP_BD_ADDR_LEN);
ret = btc_transfer_context(&msg, &param,
sizeof(esp_bt_gap_cb_param_t), NULL);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
}
#endif /// BTC_GAP_BT_INCLUDED == TRUE
}
tBTA_SERVICE_MASK btc_get_enabled_services_mask(void) tBTA_SERVICE_MASK btc_get_enabled_services_mask(void)
{ {
return btc_enabled_services; return btc_enabled_services;
@ -488,6 +509,8 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
break; break;
} }
case BTA_DM_PIN_REQ_EVT: case BTA_DM_PIN_REQ_EVT:
BTC_TRACE_DEBUG("BTA_DM_PIN_REQ_EVT");
btc_dm_pin_req_evt(&p_data->pin_req);
break; break;
case BTA_DM_AUTH_CMPL_EVT: case BTA_DM_AUTH_CMPL_EVT:
btc_dm_auth_cmpl_evt(&p_data->auth_cmpl); btc_dm_auth_cmpl_evt(&p_data->auth_cmpl);

View File

@ -645,6 +645,14 @@ esp_err_t btc_gap_bt_remove_bond_device(btc_gap_bt_args_t *arg)
return ESP_BT_STATUS_FAIL; return ESP_BT_STATUS_FAIL;
} }
static void btc_gap_bt_set_pin_type(btc_gap_bt_args_t *arg){
BTA_DMSetPinType (arg->set_pin_type.pin_type, arg->set_pin_type.pin_code, arg->set_pin_type.pin_code_len);
}
static void btc_gap_bt_pin_reply(btc_gap_bt_args_t *arg){
BTA_DmPinReply(arg->pin_reply.bda.address, arg->pin_reply.accept, arg->pin_reply.pin_code_len, arg->pin_reply.pin_code);
}
void btc_gap_bt_call_handler(btc_msg_t *msg) void btc_gap_bt_call_handler(btc_msg_t *msg)
{ {
btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg; btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg;
@ -682,6 +690,14 @@ void btc_gap_bt_call_handler(btc_msg_t *msg)
btc_gap_bt_remove_bond_device(msg->arg); btc_gap_bt_remove_bond_device(msg->arg);
break; break;
} }
case BTC_GAP_BT_ACT_SET_PIN_TYPE:{
btc_gap_bt_set_pin_type(arg);
break;
}
case BTC_GAP_BT_ACT_PIN_REPLY: {
btc_gap_bt_pin_reply(arg);
break;
}
default: default:
break; break;
} }
@ -715,6 +731,7 @@ void btc_gap_bt_cb_deep_free(btc_msg_t *msg)
break; break;
case BTC_GAP_BT_READ_RSSI_DELTA_EVT: case BTC_GAP_BT_READ_RSSI_DELTA_EVT:
case BTC_GAP_BT_AUTH_CMPL_EVT: case BTC_GAP_BT_AUTH_CMPL_EVT:
case BTC_GAP_BT_PIN_REQ_EVT:
break; break;
default: default:
BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act); BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act);
@ -745,6 +762,10 @@ void btc_gap_bt_cb_handler(btc_msg_t *msg)
btc_gap_bt_cb_to_app(ESP_BT_GAP_AUTH_CMPL_EVT, (esp_bt_gap_cb_param_t *)msg->arg); btc_gap_bt_cb_to_app(ESP_BT_GAP_AUTH_CMPL_EVT, (esp_bt_gap_cb_param_t *)msg->arg);
break; break;
} }
case BTC_GAP_BT_PIN_REQ_EVT:{
btc_gap_bt_cb_to_app(ESP_BT_GAP_PIN_REQ_EVT, (esp_bt_gap_cb_param_t *)msg->arg);
break;
}
default: default:
BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act); BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act);
break; break;

View File

@ -28,6 +28,7 @@ typedef enum {
BTC_GAP_BT_SEARCH_SERVICE_RECORD_EVT, BTC_GAP_BT_SEARCH_SERVICE_RECORD_EVT,
BTC_GAP_BT_READ_RSSI_DELTA_EVT, BTC_GAP_BT_READ_RSSI_DELTA_EVT,
BTC_GAP_BT_AUTH_CMPL_EVT, BTC_GAP_BT_AUTH_CMPL_EVT,
BTC_GAP_BT_PIN_REQ_EVT,
}btc_gap_bt_evt_t; }btc_gap_bt_evt_t;
typedef enum { typedef enum {
@ -39,6 +40,8 @@ typedef enum {
BTC_GAP_BT_ACT_SET_COD, BTC_GAP_BT_ACT_SET_COD,
BTC_GAP_BT_ACT_READ_RSSI_DELTA, BTC_GAP_BT_ACT_READ_RSSI_DELTA,
BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE, BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE,
BTC_GAP_BT_ACT_SET_PIN_TYPE,
BTC_GAP_BT_ACT_PIN_REPLY,
} btc_gap_bt_act_t; } btc_gap_bt_act_t;
/* btc_bt_gap_args_t */ /* btc_bt_gap_args_t */
@ -79,6 +82,22 @@ typedef union {
struct rm_bond_device_args { struct rm_bond_device_args {
bt_bdaddr_t bda; bt_bdaddr_t bda;
} rm_bond_device; } rm_bond_device;
// BTC_GAP_BT_ACT_SET_PIN_TYPE
struct set_pin_type_args {
esp_bt_pin_type_t pin_type;
uint8_t pin_code_len;
esp_bt_pin_code_t pin_code;
} set_pin_type;
// BTC_GAP_BT_ACT_PIN_REPLY
struct pin_reply_args {
bt_bdaddr_t bda;
bool accept;
uint8_t pin_code_len;
esp_bt_pin_code_t pin_code;
} pin_reply;
} btc_gap_bt_args_t; } btc_gap_bt_args_t;
void btc_gap_bt_call_handler(btc_msg_t *msg); void btc_gap_bt_call_handler(btc_msg_t *msg);

View File

@ -127,7 +127,7 @@
#else #else
#define SMP_INCLUDED FALSE #define SMP_INCLUDED FALSE
#define BLE_PRIVACY_SPT FALSE #define BLE_PRIVACY_SPT FALSE
#endif /* CONFIG_GATTC_ENABLE */ #endif /* CONFIG_SMP_ENABLE */
#if (CONFIG_BT_ACL_CONNECTIONS) #if (CONFIG_BT_ACL_CONNECTIONS)
#define MAX_ACL_CONNECTIONS CONFIG_BT_ACL_CONNECTIONS #define MAX_ACL_CONNECTIONS CONFIG_BT_ACL_CONNECTIONS

View File

@ -59,7 +59,7 @@ void app_main()
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
#endif #endif
.sample_rate = 44100, .sample_rate = 44100,
.bits_per_sample = 16, .bits_per_sample = 16,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
.communication_format = I2S_COMM_FORMAT_I2S_MSB, .communication_format = I2S_COMM_FORMAT_I2S_MSB,
.dma_buf_count = 6, .dma_buf_count = 6,
@ -113,8 +113,39 @@ void app_main()
/* Bluetooth device name, connection mode and profile set up */ /* Bluetooth device name, connection mode and profile set up */
bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL); bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
/*
* Set default parameters for Legacy Pairing
* Use fixed pin code
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_set_pin(pin_type, 4, pin_code);
} }
void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{
switch (event) {
case ESP_BT_GAP_AUTH_CMPL_EVT:{
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
} else {
ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
}
break;
}
default: {
ESP_LOGI(BT_AV_TAG, "event: %d", event);
break;
}
}
return;
}
static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
{ {
@ -125,6 +156,9 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
char *dev_name = "ESP_SPEAKER"; char *dev_name = "ESP_SPEAKER";
esp_bt_dev_set_device_name(dev_name); esp_bt_dev_set_device_name(dev_name);
/* register GAP callback function */
esp_bt_gap_register_callback(bt_app_gap_cb);
/* initialize A2DP sink */ /* initialize A2DP sink */
esp_a2d_register_callback(&bt_app_a2d_cb); esp_a2d_register_callback(&bt_app_a2d_cb);
esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb); esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);

View File

@ -133,6 +133,14 @@ void app_main()
/* Bluetooth device name, connection mode and profile set up */ /* Bluetooth device name, connection mode and profile set up */
bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL); bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
/*
* Set default parameters for Legacy Pairing
* Use variable pin, input pin code when pairing
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);
} }
static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len) static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
@ -254,6 +262,23 @@ void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
} }
} }
case ESP_BT_GAP_PIN_REQ_EVT:{
ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
if (param->pin_req.min_16_digit) {
ESP_LOGI(BT_AV_TAG, "Input pin code: 0000 0000 0000 0000");
esp_bt_pin_code_t pin_code = {0};
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
} else {
ESP_LOGI(BT_AV_TAG, "Input pin code: 1234");
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
}
break;
}
default: { default: {
ESP_LOGI(BT_AV_TAG, "event: %d", event); ESP_LOGI(BT_AV_TAG, "event: %d", event);
break; break;

View File

@ -103,6 +103,42 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
} }
} }
void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{
switch (event) {
case ESP_BT_GAP_AUTH_CMPL_EVT:{
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
} else {
ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
}
break;
}
case ESP_BT_GAP_PIN_REQ_EVT:{
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
if (param->pin_req.min_16_digit) {
ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
esp_bt_pin_code_t pin_code = {0};
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
} else {
ESP_LOGI(SPP_TAG, "Input pin code: 1234");
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
}
break;
}
default: {
ESP_LOGI(SPP_TAG, "event: %d", event);
break;
}
}
return;
}
void app_main() void app_main()
@ -136,6 +172,11 @@ void app_main()
return; return;
} }
if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK) {
ESP_LOGE(SPP_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) { if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) {
ESP_LOGE(SPP_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret)); ESP_LOGE(SPP_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
return; return;
@ -145,5 +186,13 @@ void app_main()
ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret)); ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
return; return;
} }
/*
* Set default parameters for Legacy Pairing
* Use variable pin, input pin code when pairing
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);
} }

View File

@ -165,33 +165,59 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{ {
switch(event){ switch(event){
case ESP_BT_GAP_DISC_RES_EVT: case ESP_BT_GAP_DISC_RES_EVT:
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_RES_EVT"); ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_RES_EVT");
esp_log_buffer_hex(SPP_TAG, param->disc_res.bda, ESP_BD_ADDR_LEN); esp_log_buffer_hex(SPP_TAG, param->disc_res.bda, ESP_BD_ADDR_LEN);
for (int i = 0; i < param->disc_res.num_prop; i++){ for (int i = 0; i < param->disc_res.num_prop; i++){
if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_EIR if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_EIR
&& get_name_from_eir(param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)){ && get_name_from_eir(param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)){
esp_log_buffer_char(SPP_TAG, peer_bdname, peer_bdname_len); esp_log_buffer_char(SPP_TAG, peer_bdname, peer_bdname_len);
if (strlen(remote_device_name) == peer_bdname_len if (strlen(remote_device_name) == peer_bdname_len
&& strncmp(peer_bdname, remote_device_name, peer_bdname_len) == 0) { && strncmp(peer_bdname, remote_device_name, peer_bdname_len) == 0) {
memcpy(peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN); memcpy(peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN);
esp_spp_start_discovery(peer_bd_addr); esp_spp_start_discovery(peer_bd_addr);
esp_bt_gap_cancel_discovery(); esp_bt_gap_cancel_discovery();
}
} }
} }
break; }
case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_STATE_CHANGED_EVT"); case ESP_BT_GAP_DISC_STATE_CHANGED_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_STATE_CHANGED_EVT");
case ESP_BT_GAP_RMT_SRVCS_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVCS_EVT"); case ESP_BT_GAP_RMT_SRVCS_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVCS_EVT");
case ESP_BT_GAP_RMT_SRVC_REC_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVC_REC_EVT"); case ESP_BT_GAP_RMT_SRVC_REC_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVC_REC_EVT");
default: break;
break; case ESP_BT_GAP_AUTH_CMPL_EVT:{
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
} else {
ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
}
break;
}
case ESP_BT_GAP_PIN_REQ_EVT:{
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
if (param->pin_req.min_16_digit) {
ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
esp_bt_pin_code_t pin_code = {0};
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
} else {
ESP_LOGI(SPP_TAG, "Input pin code: 1234");
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
}
break;
}
default:
break;
} }
} }
@ -244,5 +270,13 @@ void app_main()
ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret)); ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
return; return;
} }
/*
* Set default parameters for Legacy Pairing
* Use variable pin, input pin code when pairing
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);
} }

View File

@ -105,6 +105,43 @@ static void esp_spp_stack_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param
spp_task_work_dispatch((spp_task_cb_t)esp_spp_cb, event, param, sizeof(esp_spp_cb_param_t), NULL); spp_task_work_dispatch((spp_task_cb_t)esp_spp_cb, event, param, sizeof(esp_spp_cb_param_t), NULL);
} }
void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{
switch (event) {
case ESP_BT_GAP_AUTH_CMPL_EVT:{
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
} else {
ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
}
break;
}
case ESP_BT_GAP_PIN_REQ_EVT:{
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
if (param->pin_req.min_16_digit) {
ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
esp_bt_pin_code_t pin_code = {0};
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
} else {
ESP_LOGI(SPP_TAG, "Input pin code: 1234");
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
}
break;
}
default: {
ESP_LOGI(SPP_TAG, "event: %d", event);
break;
}
}
return;
}
void app_main() void app_main()
{ {
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
@ -135,6 +172,11 @@ void app_main()
return; return;
} }
if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK) {
ESP_LOGE(SPP_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
if (esp_spp_register_callback(esp_spp_stack_cb) != ESP_OK) { if (esp_spp_register_callback(esp_spp_stack_cb) != ESP_OK) {
ESP_LOGE(SPP_TAG, "%s spp register failed", __func__); ESP_LOGE(SPP_TAG, "%s spp register failed", __func__);
return; return;
@ -146,5 +188,13 @@ void app_main()
ESP_LOGE(SPP_TAG, "%s spp init failed", __func__); ESP_LOGE(SPP_TAG, "%s spp init failed", __func__);
return; return;
} }
/*
* Set default parameters for Legacy Pairing
* Use variable pin, input pin code when pairing
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);
} }

View File

@ -145,33 +145,59 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{ {
switch(event){ switch(event){
case ESP_BT_GAP_DISC_RES_EVT: case ESP_BT_GAP_DISC_RES_EVT:
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_RES_EVT"); ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_RES_EVT");
esp_log_buffer_hex(SPP_TAG, param->disc_res.bda, ESP_BD_ADDR_LEN); esp_log_buffer_hex(SPP_TAG, param->disc_res.bda, ESP_BD_ADDR_LEN);
for (int i = 0; i < param->disc_res.num_prop; i++){ for (int i = 0; i < param->disc_res.num_prop; i++){
if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_EIR if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_EIR
&& get_name_from_eir(param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)){ && get_name_from_eir(param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)){
esp_log_buffer_char(SPP_TAG, peer_bdname, peer_bdname_len); esp_log_buffer_char(SPP_TAG, peer_bdname, peer_bdname_len);
if (strlen(remote_device_name) == peer_bdname_len if (strlen(remote_device_name) == peer_bdname_len
&& strncmp(peer_bdname, remote_device_name, peer_bdname_len) == 0) { && strncmp(peer_bdname, remote_device_name, peer_bdname_len) == 0) {
memcpy(peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN); memcpy(peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN);
esp_spp_start_discovery(peer_bd_addr); esp_spp_start_discovery(peer_bd_addr);
esp_bt_gap_cancel_discovery(); esp_bt_gap_cancel_discovery();
}
} }
} }
break; }
case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_STATE_CHANGED_EVT"); case ESP_BT_GAP_DISC_STATE_CHANGED_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_DISC_STATE_CHANGED_EVT");
case ESP_BT_GAP_RMT_SRVCS_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVCS_EVT"); case ESP_BT_GAP_RMT_SRVCS_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVCS_EVT");
case ESP_BT_GAP_RMT_SRVC_REC_EVT: break;
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVC_REC_EVT"); case ESP_BT_GAP_RMT_SRVC_REC_EVT:
break; ESP_LOGI(SPP_TAG, "ESP_BT_GAP_RMT_SRVC_REC_EVT");
default: break;
break; case ESP_BT_GAP_AUTH_CMPL_EVT:{
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
} else {
ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
}
break;
}
case ESP_BT_GAP_PIN_REQ_EVT:{
ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
if (param->pin_req.min_16_digit) {
ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
esp_bt_pin_code_t pin_code = {0};
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
} else {
ESP_LOGI(SPP_TAG, "Input pin code: 1234");
esp_bt_pin_code_t pin_code;
pin_code[0] = '1';
pin_code[1] = '2';
pin_code[2] = '3';
pin_code[3] = '4';
esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
}
break;
}
default:
break;
} }
} }
@ -230,5 +256,12 @@ void app_main()
ESP_LOGE(SPP_TAG, "%s spp init failed", __func__); ESP_LOGE(SPP_TAG, "%s spp init failed", __func__);
return; return;
} }
/*
* Set default parameters for Legacy Pairing
* Use variable pin, input pin code when pairing
*/
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);
} }