紧急求助,使用gatt读写时,client端write某个char的时候报错。

lannister
Posts: 2
Joined: Wed Aug 23, 2023 9:17 am

紧急求助,使用gatt读写时,client端write某个char的时候报错。

Postby lannister » Wed Aug 23, 2023 9:30 am

背景:
ESP32-S3开发板,IDF4.4.2,使用BLE GATT,并且已配置SMP情况下:
SMP双端均配置OOB模式,配对绑定过程一切正常。
在Server端某个attr的权限配置为 ESP_GATT_PERM_WRITE_ENCRYPTED | ESP_GATT_PERM_READ_ENCRYPTED,

操作:
Client针对该attr进行write操作,调用如下:
esp_ble_gattc_write_char( gattc_num,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
handle,
strlen((const char *)write_char_data),
write_char_data,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);

现象:
Client打印报错 : E (11671) GATTC_DEMO: write char failed, error status = 5
提示报错为:ESP_GATT_INSUF_AUTHENTICATION

其他解释:
Client针对同一个attr进行read操作时,是可以正常进行数据读取的

请问是为什么呢?

ESP_zhanghaipeng
Posts: 42
Joined: Thu May 04, 2023 3:49 am

Re: 紧急求助,使用gatt读写时,client端write某个char的时候报错。

Postby ESP_zhanghaipeng » Wed Aug 23, 2023 11:27 am

ESP_GATT_INSUF_AUTHENTICATION 错误码的意思是在进行 GATT 操作时,由于缺乏足够的身份验证,操作被拒绝或不能成功完成。一般是由于没有加密成功造成。请确保已经加密成功。
你可以提供一下完整的 log 和 database 的代码吗?

lannister
Posts: 2
Joined: Wed Aug 23, 2023 9:17 am

Re: 紧急求助,使用gatt读写时,client端write某个char的时候报错。

Postby lannister » Thu Aug 24, 2023 5:36 am

Client端log如下:

Code: Select all

I (0) cpu_start: Starting scheduler on APP CPU.
W (411) BT_INIT: esp_bt_controller_mem_release not implemented, return OK
I (411) BT_INIT: BT controller compile version [05195c9]
I (411) phy_init: phy_version 503,13653eb,Jun 1 2022,17:47:08
I (461) system_api: Base MAC address is not set
I (461) system_api: read default base MAC address from EFUSE
I (461) BT_INIT: Bluetooth MAC: 34:85:18:bd:2d:02

I (491) GATTC_DEMO: REG_EVT
▒

letter:/$ I (1111) GATTC_DEMO: Searched Adv Data Len 26, Scan Response Len 26
I (1111) GATTC_DEMO: Searched Device Name Len :14 , name:
I (1121) GATTC_DEMO: ZBN_GATTS_DEMO
I (1121) GATTC_DEMO: Device bda :
I (1121) GATTC_DEMO: 48 27 e2 17 78 62
I (1131) GATTC_DEMO:

I (1131) GATTC_DEMO: connect to the remote device.
I (1141) GATTC_DEMO: stop scan successfully
I (1191) GATTC_DEMO: ESP_GATTC_CONNECT_EVT conn_id 0, if 3
I (1191) GATTC_DEMO: REMOTE BDA:
I (1191) GATTC_DEMO: 48 27 e2 17 78 62
I (1201) GATTC_DEMO: open success
I (1221) GATTC_DEMO: ESP_GAP_BLE_SEC_REQ_EVT
W (1221) BT_SMP: Non bonding: No keys will be exchanged
E (1261) BT_SMP: Association Model = SMP_MODEL_OOB

I (1261) GATTC_DEMO: ESP_GAP_BLE_OOB_REQ_EVT
oob value = 23 ea f f8 d4 ae f5 5f 22 1e 76 59 8d 13 54 35
I (1501) GATTC_DEMO: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 32,latency = 0, timeout = 400
I (1671) GATTC_DEMO: ESP_GAP_BLE_AUTH_CMPL_EVT
I (1671) GATTC_DEMO: remote BD_ADDR: 4827e2177862
I (1671) GATTC_DEMO: address type = 0
I (1671) GATTC_DEMO: pair status = success
I (1681) GATTC_DEMO: auth mode = ESP_LE_AUTH_REQ_MITM
I (2391) GATTC_DEMO: DIS_SRVC_CMPL_EVT
I (2391) GATTC_DEMO: discover service complete conn_id 0
I (2391) GATTC_DEMO: SEARCH RES: conn_id = 0 is primary service 1
I (2391) GATTC_DEMO: start handle 40 end handle 65535 current handle value 40
I (2401) GATTC_DEMO: service found
I (2411) GATTC_DEMO: UUID16: ff
I (2411) GATTC_DEMO: Get service information from remote device
I (2421) GATTC_DEMO: ESP_GATTC_SEARCH_CMPL_EVT
I (2421) GATTC_DEMO: attr count 3
I (2431) GATTC_DEMO: notify reg, attr handle 46
I (2431) GATTC_DEMO: ESP_GATTC_REG_FOR_NOTIFY_EVT
I (2441) GATTC_DEMO: write ccd, handle 47
I (2471) GATTC_DEMO: ESP_GATTC_CFG_MTU_EVT, Status 0, MTU 250, conn_id 0
I (2631) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT, receive notify value:
I (2631) GATTC_DEMO: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e
I (2631) GATTC_DEMO: write descr success

letter:/$
letter:/$ write 42 "shfurgyuvrtb"
input handle 42; service_start_handle: 40
Return: 1, 0x00000001

letter:/$ E (11671) GATTC_DEMO: write char failed, error status = 5
letter:/$
letter:/$ read 42
input handle 42; service_start_handle: 40
Return: 1, 0x00000001

letter:/$ I (1688471) GATTC_DEMO: ESP_GATTC_READ_CHAR_EVT,handle 42 conn id 0
I (1688471) GATTC_DEMO: read value:
I (1688471) GATTC_DEMO: "data":{"WorkMo
I (1688471) GATTC_DEMO: de":"0","Inverte
I (1688481) GATTC_DEMO: r":"1","router":
I (1688481) GATTC_DEMO: "1","mqtt":"1"},

Server端log如下:

Code: Select all

I (332) sleep: Enable automatic switching of GPIO sleep configuration
I (339) esp_core_dump_uart: Init core dump to UART
I (345) coexist: coexist rom version e7ae62f
I (350) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
&▒

letter:/$ I (418) BLE_MOD: bt module external cb reg success 1
I (418) BLE_MOD: bt module external cb reg success 2
I (428) BLE_MOD: bt module external cb reg success 3
W (448) BT_INIT: esp_bt_controller_mem_release not implemented, return OK
I (448) BT_INIT: BT controller compile version [05195c9]
I (458) phy_init: phy_version 503,13653eb,Jun 1 2022,17:47:08
I (508) system_api: Base MAC address is not set
I (508) system_api: read default base MAC address from EFUSE
I (508) BT_INIT: Bluetooth MAC: 48:27:e2:17:78:62

I (528) BLE_MOD: BD ADDR: 48:27:e2:17:78:62

I (528) BLE_MOD: module smp setting

I (538) BLE_MOD: [GATTS]1 - ESP_GATTS_REG_EVT
I (538) BLE_MOD: create attr table pass!
I (548) BLE_MOD: [GATTS]2 - ESP_GATTS_CREAT_ATTR_TAB_EVT
I (548) BLE_MOD: create attribute table successfully, the number handle = 8

I (558) BLE_MOD: [GAP]1 - SET_LOCAL_PRIVACY_COMPLETE_EVT
I (558) BLE_MOD: [GATTS]3 - SERVICE_START_EVT
I (568) BLE_MOD: status 0, service_handle 40
I (568) BLE_MOD: [GAP]2 - ADV_DATA_SET_COMPLETE_EVT
I (578) BLE_MOD: [GAP]3 - SCAN_RSP_DATA_SET_COMPLETE_EVT
I (588) BLE_MOD: [GAP]4 - ADV_START_COMPLETE_EVT
I (588) BLE_MOD: advertising start successfully
I (598) BLE_MOD: bt ble module start success!
I (758) BLE_MOD: [GATTS]4 - ESP_GATTS_CONNECT_EVT
I (758) BLE_MOD: conn_id = 0
I (758) BLE_MOD: 34 85 18 bd 2d 02
I (808) BLE_MOD: [GAP]5 - OOB_REQ_EVT
I (808) BLE_MOD: reply oob key,value:
I (808) BLE_MOD: 23 ea 0f f8 d4 ae f5 5f 22 1e 76 59 8d 13 54 35
I (1068) BLE_MOD: [GAP]6 - GAP_BLE_UPDATE_CONN_PARAMS
I (1068) BLE_MOD: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 32,latency = 0, timeout = 400
I (1188) BLE_MOD: [GAP]7 - ESP_GAP_BLE_AUTH_CMPL_EVT
I (1188) BLE_MOD: remote BD_ADDR: 348518bd2d02
I (1188) BLE_MOD: address type = 0
I (1198) BLE_MOD: pair status = success
I (1198) BLE_MOD: auth mode = ESP_LE_AUTH_REQ_MITM
I (1208) BLE_MOD: Bonded devices number : 0
I (1208) BLE_MOD: Bonded devices list : 0

I (1988) BLE_MOD: [GATTS]5 - ESP_GATTS_MTU_EVT MTU 250
I (2148) BLE_MOD: [GATTS]6 - ESP_GATTS_WRITE_EVT
I (2148) BLE_MOD: handle = 47, value len = 2, need_rsp = 0 value:
I (2148) BLE_MOD:
I (2158) BLE_MOD: notify enable
I (2158) BLE_MOD: [GATTS]7 - ESP_GATTS_CONF_EVT
I (2168) BLE_MOD: status = 0, attr_handle 46

letter:/$
letter:/$ I (1687968) BLE_MOD: [GATTS]8 - ESP_GATTS_READ_EVT
I (1687968) BLE_MOD: conn_id 0,handle 42
acquire data: "data"

Server端SMP配置代码如下:

Code: Select all

//set static passkey
uint32_t passkey = 123456;
//set auth_req
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM; //bonding
esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;
//set max key size
uint8_t key_size = 16;
//发起者密钥
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
//响应者密钥
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
//关闭仅接收指定的SMP身份验证要求
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE; //必须绑定才能开启
//关闭支持OOB 带外数据
uint8_t oob_support = ESP_BLE_OOB_ENABLE;

//设置静态密钥
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));
//设置IO能力 
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));
//关闭仅接收指定的SMP身份验证要求
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
//关闭支持OOB 带外数据
esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t)); 
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
Client端SMP配置如下:

Code: Select all

esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM; //使用MITM保护和未启用连接的安全连接
esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;
uint8_t key_size = 16; //the key size should be 7~16 bytes
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t oob_support = ESP_BLE_OOB_ENABLE; //开启OOB
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_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
Server端character datebase如下:

Code: Select all

static const esp_gatts_attr_db_t bt_module_gatt_db[BT_MODULE_HRS_IDX_NB] =
{
/* Service Declaration */
[BT_MODULE_IDX_SVC] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&uuid_tpye_val.gatts_uuid_pri_service, //UUID Length,Value
ESP_GATT_PERM_READ, //Permission
sizeof(uint16_t), sizeof(uuid_info.gatts_primary_service_uuid), //Element Max,Current Length
(uint8_t *)&uuid_info.gatts_primary_service_uuid} //Element value
},

/*******************************
*    General   Charateristic
********************************/  

/* Router Characteristic */
//Declaration
[GN_CHAR_ROUTER_DECLA] =
{
    {ESP_GATT_AUTO_RSP}, 
    {ESP_UUID_LEN_16, (uint8_t *)&uuid_tpye_val.gatts_uuid_char_declare, 
     ESP_GATT_PERM_READ,
     sizeof(uint8_t), sizeof(uint8_t), 
     (uint8_t *)&char_perm.rw }
},
// Value 
[GN_CHAR_ROUTER_VAL] =
{
    {ESP_GATT_RSP_BY_APP}, 
    {ESP_UUID_LEN_16, (uint8_t *)&uuid_info.gatts_char_router_info,      
     ESP_GATT_PERM_WRITE_ENCRYPTED | ESP_GATT_PERM_READ_ENCRYPTED,
     BT_MODULE_CHAR_VAL_LEN_MAX, sizeof(byRouterInfo), 
     (uint8_t *)&byRouterInfo[0]}
},
}


Who is online

Users browsing this forum: No registered users and 185 guests