@@ -43,20 +43,11 @@ static const char *s_gatts_conn_state_str[] = {"disconnected", "connected"};
4343static void profile_ota_event_handler (esp_gatts_cb_event_t event , esp_gatt_if_t gatts_if , esp_ble_gatts_cb_param_t * param );
4444static void profile_vfx_event_handler (esp_gatts_cb_event_t event , esp_gatt_if_t gatts_if , esp_ble_gatts_cb_param_t * param );
4545
46- /* one gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
4746gatts_profile_inst_t gatts_profile_tbl [PROFILE_IDX_MAX ] = {
4847 [PROFILE_IDX_OTA ] = { .gatts_cb = profile_ota_event_handler , .gatts_if = ESP_GATT_IF_NONE },
4948 [PROFILE_IDX_VFX ] = { .gatts_cb = profile_vfx_event_handler , .gatts_if = ESP_GATT_IF_NONE }
5049};
5150
52- void gatts_ota_send_notification (const char * data , uint32_t len )
53- {
54- esp_ble_gatts_send_indicate (gatts_profile_tbl [PROFILE_IDX_OTA ].gatts_if ,
55- gatts_profile_tbl [PROFILE_IDX_OTA ].conn_id ,
56- gatts_profile_tbl [PROFILE_IDX_OTA ].char_handle ,
57- len , (uint8_t * )data , false);
58- }
59-
6051static void profile_ota_event_handler (esp_gatts_cb_event_t event , esp_gatt_if_t gatts_if , esp_ble_gatts_cb_param_t * param )
6152{
6253 switch (event ) {
@@ -118,7 +109,7 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
118109 NULL ,
119110 NULL );
120111 if (add_char_ret ) {
121- ESP_LOGE (GATTS_OTA_TAG , "add char failed, error code =%x " , add_char_ret );
112+ ESP_LOGE (GATTS_OTA_TAG , "failed to add char: %d " , add_char_ret );
122113 }
123114 break ;
124115 case ESP_GATTS_ADD_INCL_SRVC_EVT :
@@ -134,7 +125,7 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
134125 NULL ,
135126 NULL );
136127 if (add_descr_ret ) {
137- ESP_LOGE (GATTS_OTA_TAG , "add char descr failed, error code =%x " , add_descr_ret );
128+ ESP_LOGE (GATTS_OTA_TAG , "failed to add char descr: %d " , add_descr_ret );
138129 }
139130 break ;
140131 case ESP_GATTS_ADD_CHAR_DESCR_EVT :
@@ -146,35 +137,37 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
146137 break ;
147138 case ESP_GATTS_STOP_EVT :
148139 break ;
149- case ESP_GATTS_CONNECT_EVT : {
140+ case ESP_GATTS_CONNECT_EVT :
150141 xEventGroupClearBits (user_event_group , BLE_GATTS_IDLE_BIT );
151142
152143 esp_ble_gap_stop_advertising ();
153144
154- uint8_t * bda = param -> connect .remote_bda ;
155145 ESP_LOGI (GATTS_OTA_TAG , "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]" ,
156- s_gatts_conn_state_str [1 ], bda [0 ], bda [1 ], bda [2 ], bda [3 ], bda [4 ], bda [5 ]);
146+ s_gatts_conn_state_str [1 ],
147+ param -> connect .remote_bda [0 ], param -> connect .remote_bda [1 ],
148+ param -> connect .remote_bda [2 ], param -> connect .remote_bda [3 ],
149+ param -> connect .remote_bda [4 ], param -> connect .remote_bda [5 ]);
157150
158151 gatts_profile_tbl [PROFILE_IDX_OTA ].conn_id = param -> connect .conn_id ;
159152
160153 break ;
161- }
162- case ESP_GATTS_DISCONNECT_EVT : {
163- uint8_t * bda = param -> connect .remote_bda ;
154+ case ESP_GATTS_DISCONNECT_EVT :
164155 ESP_LOGI (GATTS_OTA_TAG , "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]" ,
165- s_gatts_conn_state_str [0 ], bda [0 ], bda [1 ], bda [2 ], bda [3 ], bda [4 ], bda [5 ]);
156+ s_gatts_conn_state_str [0 ],
157+ param -> connect .remote_bda [0 ], param -> connect .remote_bda [1 ],
158+ param -> connect .remote_bda [2 ], param -> connect .remote_bda [3 ],
159+ param -> connect .remote_bda [4 ], param -> connect .remote_bda [5 ]);
166160
167161 ota_end ();
168162
169163 EventBits_t uxBits = xEventGroupGetBits (user_event_group );
170164 if (!(uxBits & OS_PWR_RESET_BIT ) && !(uxBits & OS_PWR_SLEEP_BIT )) {
171- esp_ble_gap_start_advertising ( & adv_params );
165+ ble_gap_start_advertising ( );
172166 }
173167
174168 xEventGroupSetBits (user_event_group , BLE_GATTS_IDLE_BIT );
175169
176170 break ;
177- }
178171 case ESP_GATTS_CONF_EVT :
179172 case ESP_GATTS_OPEN_EVT :
180173 case ESP_GATTS_CANCEL_OPEN_EVT :
@@ -285,7 +278,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
285278 app_setenv ("AIN_INIT_CFG" , & ain_mode , sizeof (ain_mode_t ));
286279 #endif
287280 } else {
288- ESP_LOGE (GATTS_VFX_TAG , "command 0x%02X error " , param -> write .value [0 ]);
281+ ESP_LOGE (GATTS_VFX_TAG , "invalid command: 0x%02X" , param -> write .value [0 ]);
289282 }
290283 break ;
291284 default :
@@ -320,7 +313,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
320313 NULL ,
321314 NULL );
322315 if (add_char_ret ) {
323- ESP_LOGE (GATTS_VFX_TAG , "add char failed, error code =%x " , add_char_ret );
316+ ESP_LOGE (GATTS_VFX_TAG , "failed to add char: %d " , add_char_ret );
324317 }
325318 break ;
326319 case ESP_GATTS_ADD_INCL_SRVC_EVT :
@@ -336,7 +329,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
336329 NULL ,
337330 NULL );
338331 if (add_descr_ret ) {
339- ESP_LOGE (GATTS_VFX_TAG , "add char descr failed, error code =%x " , add_descr_ret );
332+ ESP_LOGE (GATTS_VFX_TAG , "failed to add char descr: %d " , add_descr_ret );
340333 }
341334 break ;
342335 case ESP_GATTS_ADD_CHAR_DESCR_EVT :
@@ -348,22 +341,23 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
348341 break ;
349342 case ESP_GATTS_STOP_EVT :
350343 break ;
351- case ESP_GATTS_CONNECT_EVT : {
352- uint8_t * bda = param -> connect .remote_bda ;
344+ case ESP_GATTS_CONNECT_EVT :
353345 ESP_LOGI (GATTS_VFX_TAG , "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]" ,
354- s_gatts_conn_state_str [1 ], bda [0 ], bda [1 ], bda [2 ], bda [3 ], bda [4 ], bda [5 ]);
346+ s_gatts_conn_state_str [1 ],
347+ param -> connect .remote_bda [0 ], param -> connect .remote_bda [1 ],
348+ param -> connect .remote_bda [2 ], param -> connect .remote_bda [3 ],
349+ param -> connect .remote_bda [4 ], param -> connect .remote_bda [5 ]);
355350
356351 gatts_profile_tbl [PROFILE_IDX_VFX ].conn_id = param -> connect .conn_id ;
357352
358353 break ;
359- }
360- case ESP_GATTS_DISCONNECT_EVT : {
361- uint8_t * bda = param -> connect .remote_bda ;
354+ case ESP_GATTS_DISCONNECT_EVT :
362355 ESP_LOGI (GATTS_VFX_TAG , "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]" ,
363- s_gatts_conn_state_str [0 ], bda [0 ], bda [1 ], bda [2 ], bda [3 ], bda [4 ], bda [5 ]);
364-
356+ s_gatts_conn_state_str [0 ],
357+ param -> connect .remote_bda [0 ], param -> connect .remote_bda [1 ],
358+ param -> connect .remote_bda [2 ], param -> connect .remote_bda [3 ],
359+ param -> connect .remote_bda [4 ], param -> connect .remote_bda [5 ]);
365360 break ;
366- }
367361 case ESP_GATTS_CONF_EVT :
368362 case ESP_GATTS_OPEN_EVT :
369363 case ESP_GATTS_CANCEL_OPEN_EVT :
@@ -377,23 +371,17 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
377371
378372void ble_gatts_event_handler (esp_gatts_cb_event_t event , esp_gatt_if_t gatts_if , esp_ble_gatts_cb_param_t * param )
379373{
380- /* if event is register event, store the gatts_if for each profile */
381374 if (event == ESP_GATTS_REG_EVT ) {
382375 if (param -> reg .status == ESP_GATT_OK ) {
383376 gatts_profile_tbl [param -> reg .app_id ].gatts_if = gatts_if ;
384377 } else {
385- ESP_LOGE (BLE_GATTS_TAG , "reg app failed, app_id %04x, status %d" ,
386- param -> reg .app_id ,
387- param -> reg .status );
378+ ESP_LOGE (BLE_GATTS_TAG , "reg app failed, app_id: %04x, status: %d" , param -> reg .app_id , param -> reg .status );
388379 return ;
389380 }
390381 }
391382
392- /* if the gatts_if equal to profile A, call profile A cb handler,
393- * so here call each profile's callback */
394383 for (int idx = 0 ; idx < PROFILE_IDX_MAX ; idx ++ ) {
395- if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
396- gatts_if == gatts_profile_tbl [idx ].gatts_if ) {
384+ if (gatts_if == ESP_GATT_IF_NONE || gatts_if == gatts_profile_tbl [idx ].gatts_if ) {
397385 if (gatts_profile_tbl [idx ].gatts_cb ) {
398386 gatts_profile_tbl [idx ].gatts_cb (event , gatts_if , param );
399387 }
0 commit comments