Skip to content

Commit 2a9d475

Browse files
committed
ble_gatts: fix desc val read error
1 parent 889c971 commit 2a9d475

File tree

1 file changed

+89
-67
lines changed

1 file changed

+89
-67
lines changed

main/src/user/ble_gatts.c

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define GATTS_CHAR_UUID_VFX 0x5301
3535
#define GATTS_NUM_HANDLE_VFX 4
3636

37+
static uint16_t desc_val_ota = 0x0000;
38+
static uint16_t desc_val_vfx = 0x0000;
39+
3740
static const char *s_gatts_conn_state_str[] = {"disconnected", "connected"};
3841

3942
static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
@@ -69,19 +72,29 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
6972

7073
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
7174
rsp.attr_value.handle = param->read.handle;
72-
rsp.attr_value.len = strlen(app_get_version()) < (ESP_GATT_DEF_BLE_MTU_SIZE - 3) ?
73-
strlen(app_get_version()) : (ESP_GATT_DEF_BLE_MTU_SIZE - 3);
74-
memcpy(rsp.attr_value.value, app_get_version(), rsp.attr_value.len);
75+
76+
if (param->read.handle == gatts_profile_tbl[PROFILE_IDX_OTA].descr_handle) {
77+
rsp.attr_value.len = 2;
78+
memcpy(rsp.attr_value.value, &desc_val_ota, sizeof(desc_val_ota));
79+
} else {
80+
rsp.attr_value.len = strlen(app_get_version()) < (ESP_GATT_DEF_BLE_MTU_SIZE - 3) ?
81+
strlen(app_get_version()) : (ESP_GATT_DEF_BLE_MTU_SIZE - 3);
82+
memcpy(rsp.attr_value.value, app_get_version(), rsp.attr_value.len);
83+
}
7584

7685
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
7786
break;
7887
}
7988
case ESP_GATTS_WRITE_EVT: {
80-
if (!param->write.need_rsp) {
81-
if (!param->write.is_prep) {
89+
if (!param->write.is_prep) {
90+
if (param->write.handle == gatts_profile_tbl[PROFILE_IDX_OTA].descr_handle) {
91+
desc_val_ota = param->write.value[1] << 8 | param->write.value[0];
92+
} else {
8293
ota_exec((const char *)param->write.value, param->write.len);
8394
}
84-
} else {
95+
}
96+
97+
if (param->write.need_rsp) {
8598
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
8699
}
87100
break;
@@ -188,102 +201,111 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
188201
case ESP_GATTS_READ_EVT: {
189202
esp_gatt_rsp_t rsp;
190203

191-
#ifdef CONFIG_ENABLE_VFX
192-
vfx_config_t *vfx = vfx_get_conf();
193-
#ifndef CONFIG_AUDIO_INPUT_NONE
194-
uint8_t ain_mode = ain_get_mode();
195-
#endif
196-
#endif
197-
198204
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
199205
rsp.attr_value.handle = param->read.handle;
200-
rsp.attr_value.len = 8;
201-
/*
202-
BTT0: VFX Enabled
203-
BIT1: Backlight Enabled
204-
BIT2: Cube Mode Enabled
205-
BIT3: Audio Input Enabled
206-
*/
207-
rsp.attr_value.value[0] = (
208-
0
206+
207+
if (param->read.handle == gatts_profile_tbl[PROFILE_IDX_VFX].descr_handle) {
208+
rsp.attr_value.len = 2;
209+
memcpy(rsp.attr_value.value, &desc_val_vfx, sizeof(desc_val_vfx));
210+
} else {
211+
rsp.attr_value.len = 8;
212+
/*
213+
BTT0: VFX Enabled
214+
BIT1: Backlight Enabled
215+
BIT2: Cube Mode Enabled
216+
BIT3: Audio Input Enabled
217+
*/
218+
rsp.attr_value.value[0] = (
219+
0
209220
#ifdef CONFIG_ENABLE_VFX
210-
| BIT0
221+
| BIT0
211222
#if defined(CONFIG_VFX_OUTPUT_ST7735) || defined(CONFIG_VFX_OUTPUT_ST7789)
212-
| BIT1
223+
| BIT1
213224
#endif
214225
#if defined(CONFIG_VFX_OUTPUT_WS2812) || defined(CONFIG_VFX_OUTPUT_CUBE0414)
215-
| BIT2
226+
| BIT2
216227
#endif
217228
#ifndef CONFIG_AUDIO_INPUT_NONE
218-
| BIT3
229+
| BIT3
219230
#endif
220231
#endif
221-
);
232+
);
222233
#ifdef CONFIG_ENABLE_VFX
223-
rsp.attr_value.value[1] = vfx->mode;
224-
rsp.attr_value.value[2] = vfx->scale_factor >> 8;
225-
rsp.attr_value.value[3] = vfx->scale_factor & 0xFF;
226-
rsp.attr_value.value[4] = vfx->lightness >> 8;
227-
rsp.attr_value.value[5] = vfx->lightness & 0xFF;
228-
rsp.attr_value.value[6] = vfx->backlight;
234+
vfx_config_t *vfx = vfx_get_conf();
229235
#ifndef CONFIG_AUDIO_INPUT_NONE
230-
rsp.attr_value.value[7] = ain_mode;
236+
uint8_t ain_mode = ain_get_mode();
237+
#endif
238+
rsp.attr_value.value[1] = vfx->mode;
239+
rsp.attr_value.value[2] = vfx->scale_factor >> 8;
240+
rsp.attr_value.value[3] = vfx->scale_factor & 0xFF;
241+
rsp.attr_value.value[4] = vfx->lightness >> 8;
242+
rsp.attr_value.value[5] = vfx->lightness & 0xFF;
243+
rsp.attr_value.value[6] = vfx->backlight;
244+
#ifndef CONFIG_AUDIO_INPUT_NONE
245+
rsp.attr_value.value[7] = ain_mode;
231246
#endif
232247
#endif
248+
}
233249

234250
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
235251
break;
236252
}
237253
case ESP_GATTS_WRITE_EVT: {
254+
if (!param->write.is_prep) {
255+
if (param->write.handle == gatts_profile_tbl[PROFILE_IDX_VFX].descr_handle) {
256+
desc_val_vfx = param->write.value[1] << 8 | param->write.value[0];
257+
} else {
238258
#ifdef CONFIG_ENABLE_VFX
239-
vfx_config_t *vfx = vfx_get_conf();
259+
vfx_config_t *vfx = vfx_get_conf();
240260
#ifndef CONFIG_AUDIO_INPUT_NONE
241-
uint8_t ain_mode = ain_get_mode();
261+
uint8_t ain_mode = ain_get_mode();
242262
#endif
243263
#endif
244-
if (!param->write.is_prep) {
245-
switch (param->write.value[0]) {
246-
case 0xEF: {
247-
if (param->write.len == 1) { // Restore Default Configuration
264+
switch (param->write.value[0]) {
265+
case 0xEF: {
266+
if (param->write.len == 1) { // Restore Default Configuration
248267
#ifdef CONFIG_ENABLE_VFX
249-
vfx->mode = DEFAULT_VFX_MODE;
250-
vfx->scale_factor = DEFAULT_VFX_SCALE_FACTOR;
251-
vfx->lightness = DEFAULT_VFX_LIGHTNESS;
252-
vfx->backlight = DEFAULT_VFX_BACKLIGHT;
253-
vfx_set_conf(vfx);
254-
app_setenv("VFX_INIT_CFG", vfx, sizeof(vfx_config_t));
268+
vfx->mode = DEFAULT_VFX_MODE;
269+
vfx->scale_factor = DEFAULT_VFX_SCALE_FACTOR;
270+
vfx->lightness = DEFAULT_VFX_LIGHTNESS;
271+
vfx->backlight = DEFAULT_VFX_BACKLIGHT;
272+
vfx_set_conf(vfx);
273+
app_setenv("VFX_INIT_CFG", vfx, sizeof(vfx_config_t));
255274
#ifndef CONFIG_AUDIO_INPUT_NONE
256-
ain_mode = DEFAULT_AIN_MODE;
257-
ain_set_mode(ain_mode);
258-
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(uint8_t));
275+
ain_mode = DEFAULT_AIN_MODE;
276+
ain_set_mode(ain_mode);
277+
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(uint8_t));
259278
#endif
260279
#endif
261-
} else if (param->write.len == 8) { // Update with New Configuration
280+
} else if (param->write.len == 8) { // Update with New Configuration
262281
#ifdef CONFIG_ENABLE_VFX
263-
vfx->mode = param->write.value[1];
264-
vfx->scale_factor = param->write.value[2] << 8 | param->write.value[3];
265-
vfx->lightness = (param->write.value[4] << 8 | param->write.value[5]) % 0x0200;
266-
vfx->backlight = param->write.value[6];
267-
vfx_set_conf(vfx);
268-
app_setenv("VFX_INIT_CFG", vfx, sizeof(vfx_config_t));
282+
vfx->mode = param->write.value[1];
283+
vfx->scale_factor = param->write.value[2] << 8 | param->write.value[3];
284+
vfx->lightness = (param->write.value[4] << 8 | param->write.value[5]) % 0x0200;
285+
vfx->backlight = param->write.value[6];
286+
vfx_set_conf(vfx);
287+
app_setenv("VFX_INIT_CFG", vfx, sizeof(vfx_config_t));
269288
#ifndef CONFIG_AUDIO_INPUT_NONE
270-
ain_mode = param->write.value[7];
271-
ain_set_mode(ain_mode);
272-
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(uint8_t));
289+
ain_mode = param->write.value[7];
290+
ain_set_mode(ain_mode);
291+
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(uint8_t));
273292
#endif
274293
#endif
275-
} else {
276-
ESP_LOGE(GATTS_VFX_TAG, "command 0x%02X error", param->write.value[0]);
294+
} else {
295+
ESP_LOGE(GATTS_VFX_TAG, "command 0x%02X error", param->write.value[0]);
296+
}
297+
break;
298+
}
299+
default:
300+
ESP_LOGW(GATTS_VFX_TAG, "unknown command: 0x%02X", param->write.value[0]);
301+
break;
277302
}
278-
break;
279-
}
280-
default:
281-
ESP_LOGW(GATTS_VFX_TAG, "unknown command: 0x%02X", param->write.value[0]);
282-
break;
283303
}
284304
}
285305

286-
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
306+
if (param->write.need_rsp) {
307+
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
308+
}
287309
break;
288310
}
289311
case ESP_GATTS_EXEC_WRITE_EVT:

0 commit comments

Comments
 (0)