Skip to content

Commit 09586b0

Browse files
committed
refactor(ble_conn_mgr): Support set the ATT error code
1 parent eaa7262 commit 09586b0

File tree

15 files changed

+265
-65
lines changed

15 files changed

+265
-65
lines changed

components/bluetooth/ble_conn_mgr/include/esp_ble_conn_mgr.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -14,6 +14,64 @@ extern "C"
1414
{
1515
#endif
1616

17+
/**
18+
* Success code and error codes
19+
*/
20+
#define ESP_IOT_ATT_SUCCESS 0x00
21+
#define ESP_IOT_ATT_INVALID_HANDLE 0x01
22+
#define ESP_IOT_ATT_READ_NOT_PERMIT 0x02
23+
#define ESP_IOT_ATT_WRITE_NOT_PERMIT 0x03
24+
#define ESP_IOT_ATT_INVALID_PDU 0x04
25+
#define ESP_IOT_ATT_INSUF_AUTHENTICATION 0x05
26+
#define ESP_IOT_ATT_REQ_NOT_SUPPORTED 0x06
27+
#define ESP_IOT_ATT_INVALID_OFFSET 0x07
28+
#define ESP_IOT_ATT_INSUF_AUTHORIZATION 0x08
29+
#define ESP_IOT_ATT_PREPARE_Q_FULL 0x09
30+
#define ESP_IOT_ATT_NOT_FOUND 0x0a
31+
#define ESP_IOT_ATT_NOT_LONG 0x0b
32+
#define ESP_IOT_ATT_INSUF_KEY_SIZE 0x0c
33+
#define ESP_IOT_ATT_INVALID_ATTR_LEN 0x0d
34+
#define ESP_IOT_ATT_ERR_UNLIKELY 0x0e
35+
#define ESP_IOT_ATT_INSUF_ENCRYPTION 0x0f
36+
#define ESP_IOT_ATT_UNSUPPORT_GRP_TYPE 0x10
37+
#define ESP_IOT_ATT_INSUF_RESOURCE 0x11
38+
#define ESP_IOT_ATT_DATABASE_OUT_OF_SYNC 0x12
39+
#define ESP_IOT_ATT_VALUE_NOT_ALLOWED 0x13
40+
41+
/**
42+
* Application error codes
43+
*/
44+
#define ESP_IOT_ATT_NO_RESOURCES 0x80
45+
#define ESP_IOT_ATT_INTERNAL_ERROR 0x81
46+
#define ESP_IOT_ATT_WRONG_STATE 0x82
47+
#define ESP_IOT_ATT_DB_FULL 0x83
48+
#define ESP_IOT_ATT_BUSY 0x84
49+
#define ESP_IOT_ATT_ERROR 0x85
50+
#define ESP_IOT_ATT_CMD_STARTED 0x86
51+
#define ESP_IOT_ATT_ILLEGAL_PARAMETER 0x87
52+
#define ESP_IOT_ATT_PENDING 0x88
53+
#define ESP_IOT_ATT_AUTH_FAIL 0x89
54+
#define ESP_IOT_ATT_MORE 0x8a
55+
#define ESP_IOT_ATT_INVALID_CFG 0x8b
56+
#define ESP_IOT_ATT_SERVICE_STARTED 0x8c
57+
#define ESP_IOT_ATT_ENCRYPED_MITM ESP_IOT_ATT_SUCCESS
58+
#define ESP_IOT_ATT_ENCRYPED_NO_MITM 0x8d
59+
#define ESP_IOT_ATT_NOT_ENCRYPTED 0x8e
60+
#define ESP_IOT_ATT_CONGESTED 0x8f
61+
#define ESP_IOT_ATT_DUP_REG 0x90
62+
#define ESP_IOT_ATT_ALREADY_OPEN 0x91
63+
#define ESP_IOT_ATT_CANCEL 0x92
64+
65+
/**
66+
* Common profile and service error codes
67+
*/
68+
#define ESP_IOT_ATT_STACK_RSP 0xE0
69+
#define ESP_IOT_ATT_APP_RSP 0xE1
70+
#define ESP_IOT_ATT_UNKNOWN_ERROR 0XEF
71+
#define ESP_IOT_ATT_CCC_CFG_ERR 0xFD
72+
#define ESP_IOT_ATT_PRC_IN_PROGRESS 0xFE
73+
#define ESP_IOT_ATT_OUT_OF_RANGE 0xFF
74+
1775
/**
1876
* BLE device name cannot be larger than this value
1977
* 31 bytes (max scan response size) - 1 byte (length) - 1 byte (type) = 29 bytes
@@ -37,6 +95,7 @@ extern "C"
3795
* @param[out] outbuf Variable to store data, it'll free by connection management component
3896
* @param[out] outlen Variable to store data length
3997
* @param[in] priv_data Private data context
98+
* @param[in] att_status The attribute return status
4099
*
41100
* @return
42101
* - ESP_OK on successful
@@ -47,7 +106,8 @@ typedef esp_err_t (*esp_ble_conn_cb_t)(const uint8_t *inbuf,
47106
uint16_t inlen,
48107
uint8_t **outbuf,
49108
uint16_t *outlen,
50-
void *priv_data);
109+
void *priv_data,
110+
uint8_t *att_status);
51111

52112
/** @cond **/
53113
/* BLE CONN_MGR EVENTS BASE */

components/bluetooth/ble_conn_mgr/src/esp_nimble.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -1393,6 +1393,7 @@ static int esp_ble_conn_gap_event(struct ble_gap_event *event, void *arg)
13931393
static int esp_ble_conn_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
13941394
{
13951395
esp_err_t rc = ESP_OK;
1396+
uint8_t att_status = ESP_IOT_ATT_SUCCESS;
13961397
char buf[BLE_UUID_STR_LEN];
13971398

13981399
uint8_t *data_buf = NULL;
@@ -1407,22 +1408,26 @@ static int esp_ble_conn_access_cb(uint16_t conn_handle, uint16_t attr_handle, st
14071408
case BLE_GATT_ACCESS_OP_READ_CHR:
14081409
ESP_LOGI(TAG, "Read attempted for characteristic UUID = %s, attr_handle = %d",
14091410
ble_uuid_to_str(ctxt->chr->uuid, buf), attr_handle);
1410-
if (chr && chr->uuid_fn && !chr->uuid_fn(NULL, 0, &outbuf, &outlen, NULL)) {
1411+
if (chr && chr->uuid_fn && !chr->uuid_fn(NULL, 0, &outbuf, &outlen, NULL, &att_status)) {
14111412
esp_ble_conn_on_gatts_attr_value_set(attr_handle, ctxt->chr->uuid, outlen, outbuf);
14121413
}
14131414

1414-
rc = esp_ble_conn_on_gatts_attr_value_get(attr_handle, &outlen, &outbuf);
1415-
if (rc != 0) {
1415+
if (esp_ble_conn_on_gatts_attr_value_get(attr_handle, &outlen, &outbuf) != 0) {
14161416
ESP_LOGE(TAG, "Failed to read characteristic with attr_handle = %d", attr_handle);
1417-
return rc;
1417+
return ESP_IOT_ATT_INTERNAL_ERROR;
1418+
}
1419+
1420+
if (os_mbuf_append(ctxt->om, outbuf, outlen) != 0) {
1421+
ESP_LOGE(TAG, "Failed to append mbuf");
1422+
return ESP_IOT_ATT_INTERNAL_ERROR;
14181423
}
1419-
rc = os_mbuf_append(ctxt->om, outbuf, outlen);
1424+
14201425
break;
14211426
case BLE_GATT_ACCESS_OP_WRITE_CHR:
14221427
/* If empty packet is received, return */
14231428
if (ctxt->om->om_len == 0) {
14241429
ESP_LOGD(TAG,"Empty packet");
1425-
return ESP_OK;
1430+
return ESP_IOT_ATT_SUCCESS;
14261431
}
14271432

14281433
/* Save the length of entire data */
@@ -1433,38 +1438,41 @@ static int esp_ble_conn_access_cb(uint16_t conn_handle, uint16_t attr_handle, st
14331438
data_buf = calloc(1, data_len);
14341439
if (data_buf == NULL) {
14351440
ESP_LOGE(TAG, "Error allocating memory for characteristic value");
1436-
return BLE_ATT_ERR_INSUFFICIENT_RES;
1441+
return ESP_IOT_ATT_INSUF_RESOURCE;
14371442
}
14381443

14391444
rc = ble_hs_mbuf_to_flat(ctxt->om, data_buf, data_len, &data_buf_len);
14401445
if (rc != 0) {
14411446
ESP_LOGE(TAG, "Error getting data from memory buffers");
14421447
free(data_buf);
1443-
return BLE_ATT_ERR_UNLIKELY;
1448+
return ESP_IOT_ATT_ERR_UNLIKELY;
14441449
}
14451450

14461451
if (chr) {
14471452
if (chr->uuid_fn) {
1448-
rc = chr->uuid_fn(data_buf, data_buf_len, &outbuf, &outlen, NULL);
1453+
rc = chr->uuid_fn(data_buf, data_buf_len, &outbuf, &outlen, NULL, &att_status);
14491454
} else {
14501455
esp_ble_conn_event_send(s_conn_session, ESP_BLE_CONN_EVENT_DATA_RECEIVE, data_buf, data_buf_len, NULL);
14511456
}
14521457
} else {
14531458
ESP_LOGE(TAG, "Error getting character from uuid buffers");
14541459
}
14551460

1456-
rc = esp_ble_conn_on_gatts_attr_value_set(attr_handle, ctxt->chr->uuid, outlen, outbuf);
1461+
if (esp_ble_conn_on_gatts_attr_value_set(attr_handle, ctxt->chr->uuid, outlen, outbuf)!= 0) {
1462+
ESP_LOGE(TAG, "Failed to read characteristic with attr_handle = %d", attr_handle);
1463+
return ESP_IOT_ATT_INTERNAL_ERROR;
1464+
}
14571465

14581466
free(data_buf);
14591467
data_buf = NULL;
14601468
break;
14611469

14621470
default:
1463-
rc = BLE_ATT_ERR_UNLIKELY;
1471+
att_status = ESP_IOT_ATT_ERR_UNLIKELY;
14641472
break;
14651473
}
14661474

1467-
return rc;
1475+
return att_status;
14681476
}
14691477

14701478
static void esp_ble_conn_on_reset(int reason)
@@ -1504,6 +1512,7 @@ static void esp_ble_conn_on_gatts_register(struct ble_gatt_register_ctxt *ctxt,
15041512
{
15051513
char buf[BLE_UUID_STR_LEN];
15061514
uint8_t *outbuf = NULL;
1515+
uint8_t att_status = ESP_IOT_ATT_SUCCESS;
15071516
uint16_t outlen = 0;
15081517
esp_ble_conn_character_t *chr = NULL;
15091518

@@ -1523,7 +1532,7 @@ static void esp_ble_conn_on_gatts_register(struct ble_gatt_register_ctxt *ctxt,
15231532
chr = esp_ble_conn_find_character_with_uuid(ctxt->chr.chr_def->uuid);
15241533
if (chr) {
15251534
if (chr->uuid_fn) {
1526-
chr->uuid_fn(NULL, 0, &outbuf, &outlen, NULL);
1535+
chr->uuid_fn(NULL, 0, &outbuf, &outlen, NULL, &att_status);
15271536
}
15281537
} else {
15291538
ESP_LOGI(TAG, "No characteristic(%s) found", ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf));

components/bluetooth/ble_services/ans/src/esp_ans.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -77,30 +77,34 @@ static esp_err_t ble_ans_unr_alert_notify(uint8_t cat_id)
7777
}
7878

7979
static esp_err_t esp_ans_sup_new_alert_cat_cb(const uint8_t *inbuf, uint16_t inlen,
80-
uint8_t **outbuf, uint16_t *outlen, void *priv_data)
80+
uint8_t **outbuf, uint16_t *outlen, void *priv_data, uint8_t *att_status)
8181
{
8282
uint8_t len = 0;
8383
if (inbuf || !outbuf || !outlen) {
84+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
8485
return ESP_ERR_INVALID_ARG;
8586
}
8687

8788
len = sizeof(ble_ans_new_alert_cat);
8889
*outbuf = calloc(1, len);
8990
if (!(*outbuf)) {
91+
*att_status = ESP_IOT_ATT_INSUF_RESOURCE;
9092
return ESP_ERR_NO_MEM;
9193
}
9294

9395
memcpy(*outbuf, &ble_ans_new_alert_cat, len);
9496
*outlen = len;
97+
*att_status = ESP_IOT_ATT_SUCCESS;
9598

9699
return ESP_OK;
97100
}
98101

99102
static esp_err_t esp_ans_new_alert_cb(const uint8_t *inbuf, uint16_t inlen,
100-
uint8_t **outbuf, uint16_t *outlen, void *priv_data)
103+
uint8_t **outbuf, uint16_t *outlen, void *priv_data, uint8_t *att_status)
101104
{
102105
uint8_t len = MIN(inlen, sizeof(ble_ans_new_alert_val));
103106
if (!outbuf || !outlen) {
107+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
104108
return ESP_ERR_INVALID_ARG;
105109
}
106110

@@ -112,40 +116,48 @@ static esp_err_t esp_ans_new_alert_cb(const uint8_t *inbuf, uint16_t inlen,
112116

113117
*outbuf = calloc(1, len);
114118
if (!(*outbuf)) {
119+
*att_status = ESP_IOT_ATT_INSUF_RESOURCE;
115120
return ESP_ERR_NO_MEM;
116121
}
117122

118123
memcpy(*outbuf, ble_ans_new_alert_val, len);
119124
*outlen = len;
120125

126+
*att_status = ESP_IOT_ATT_SUCCESS;
127+
121128
return ESP_OK;
122129
}
123130

124131
static esp_err_t esp_ans_sup_unr_alert_cat_cb(const uint8_t *inbuf, uint16_t inlen,
125-
uint8_t **outbuf, uint16_t *outlen, void *priv_data)
132+
uint8_t **outbuf, uint16_t *outlen, void *priv_data, uint8_t *att_status)
126133
{
127134
uint8_t len = 0;
128135
if (inbuf || !outbuf || !outlen) {
136+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
129137
return ESP_ERR_INVALID_ARG;
130138
}
131139

132140
len = sizeof(ble_ans_unr_alert_cat);
133141
*outbuf = calloc(1, len);
134142
if (!(*outbuf)) {
143+
*att_status = ESP_IOT_ATT_INSUF_RESOURCE;
135144
return ESP_ERR_NO_MEM;
136145
}
137146

138147
memcpy(*outbuf, &ble_ans_unr_alert_cat, len);
139148
*outlen = len;
140149

150+
*att_status = ESP_IOT_ATT_SUCCESS;
151+
141152
return ESP_OK;
142153
}
143154

144155
static esp_err_t esp_ans_unr_alert_stat_cb(const uint8_t *inbuf, uint16_t inlen,
145-
uint8_t **outbuf, uint16_t *outlen, void *priv_data)
156+
uint8_t **outbuf, uint16_t *outlen, void *priv_data, uint8_t *att_status)
146157
{
147158
uint8_t len = MIN(inlen, sizeof(ble_ans_unr_alert_stat));
148159
if (!outbuf || !outlen) {
160+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
149161
return ESP_ERR_INVALID_ARG;
150162
}
151163

@@ -157,24 +169,28 @@ static esp_err_t esp_ans_unr_alert_stat_cb(const uint8_t *inbuf, uint16_t inlen,
157169

158170
*outbuf = calloc(1, len);
159171
if (!(*outbuf)) {
172+
*att_status = ESP_IOT_ATT_INSUF_RESOURCE;
160173
return ESP_ERR_NO_MEM;
161174
}
162175

163176
memcpy(*outbuf, ble_ans_unr_alert_stat, len);
164177
*outlen = len;
165178

179+
*att_status = ESP_IOT_ATT_SUCCESS;
180+
166181
return ESP_OK;
167182
}
168183

169184
static esp_err_t esp_ans_alert_notify_ctrl_cb(const uint8_t *inbuf, uint16_t inlen,
170-
uint8_t **outbuf, uint16_t *outlen, void *priv_data)
185+
uint8_t **outbuf, uint16_t *outlen, void *priv_data, uint8_t *att_status)
171186
{
172187
/* ANS Control point command and category variables */
173188
uint8_t cmd_id;
174189
uint8_t cat_id;
175190
uint8_t cat_bit_mask;
176191
uint8_t len = MIN(inlen, sizeof(ble_ans_alert_not_ctrl_pt));
177192
if (!inbuf || !outbuf || !outlen) {
193+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
178194
return ESP_ERR_INVALID_ARG;
179195
}
180196

@@ -191,6 +207,7 @@ static esp_err_t esp_ans_alert_notify_ctrl_cb(const uint8_t *inbuf, uint16_t inl
191207
cat_bit_mask = cat_id;
192208
} else {
193209
/* invalid category ID */
210+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
194211
return ESP_ERR_INVALID_ARG;
195212
}
196213

@@ -232,9 +249,12 @@ static esp_err_t esp_ans_alert_notify_ctrl_cb(const uint8_t *inbuf, uint16_t inl
232249
}
233250
break;
234251
default:
252+
*att_status = ESP_IOT_ATT_INTERNAL_ERROR;
235253
return ESP_ERR_INVALID_ARG;
236254
}
237255

256+
*att_status = ESP_IOT_ATT_SUCCESS;
257+
238258
return ESP_OK;
239259
}
240260

0 commit comments

Comments
 (0)