11/*
2- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Unlicense OR CC0-1.0
55 */
1010#include "nimble/nimble_port_freertos.h"
1111#include "host/ble_hs.h"
1212
13- #define BLE_PAWR_NUM_SUBEVTS (5)
14- #define BLE_PAWR_SUB_INTERVAL (12) /*!< Interval between subevents (N * 1.25 ms) */
15- #define BLE_PAWR_RSP_SLOT_DELAY (1) /*!< The first response slot delay (N * 1.25 ms)*/
16- #define BLE_PAWR_RSP_SLOT_SPACING (32) /*!< Time between response slots (N * 0.125 ms) */
17- #define BLE_PAWR_NUM_RSP_SLOTS (3) /*!< Number of subevent response slots */
18- #define BLE_PAWR_SUB_DATA_LEN (10)
13+ #define BLE_PAWR_EVENT_INTERVAL (520)
14+ #define BLE_PAWR_NUM_SUBEVTS (10)
15+ #define BLE_PAWR_SUB_INTERVAL (52) /*!< Interval between subevents (N * 1.25 ms) */
16+ #define BLE_PAWR_RSP_SLOT_DELAY (5) /*!< The first response slot delay (N * 1.25 ms)*/
17+ #define BLE_PAWR_RSP_SLOT_SPACING (10) /*!< Time between response slots (N * 0.125 ms) */
18+ #define BLE_PAWR_NUM_RSP_SLOTS (25) /*!< Number of subevent response slots */
19+ #define BLE_PAWR_SUB_DATA_LEN (20)
1920
20- #define TAG "NimBLE_BLE_PAwR "
21+ #define TAG "NimBLE_BLE_PAwR_CONN "
2122
2223static struct ble_gap_set_periodic_adv_subev_data_params sub_data_params [BLE_PAWR_NUM_SUBEVTS ];
2324static uint8_t sub_data_pattern [BLE_PAWR_SUB_DATA_LEN ] = {0 };
@@ -71,18 +72,28 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
7172 uint8_t phy_mask ;
7273
7374 switch (event -> type ) {
74- case BLE_GAP_EVENT_CONNECT :
75- printf ("\n" );
76- ESP_LOGI (TAG , "Connection established, conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n" ,event -> connect .conn_handle ,event -> connect .adv_handle , event -> connect .status );
77- rc = ble_gap_conn_find (event -> connect .conn_handle , & desc );
78- if (rc == 0 ){
79- print_conn_desc (& desc );
80- }
81- else {
82- ESP_LOGE (TAG ,"Failed to find Conn Information" );
83- }
84-
85- return 0 ;
75+
76+ case BLE_GAP_EVENT_LINK_ESTAB :
77+ if (event -> link_estab .status == 0 ) {
78+ ESP_LOGI (TAG , "[Connection established], conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n" ,event -> link_estab .conn_handle , event -> link_estab .adv_handle , event -> link_estab .status );
79+ rc = ble_gap_conn_find (event -> link_estab .conn_handle , & desc );
80+ if (rc == 0 ) {
81+ print_conn_desc (& desc );
82+ }
83+ else {
84+ ESP_LOGE (TAG ,"Failed to find Conn Information" );
85+ }
86+ } else {
87+ ESP_LOGW (TAG , "[Connection Failed], conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n" ,event -> link_estab .conn_handle , event -> link_estab .adv_handle , event -> link_estab .status );
88+ conn = 0 ;
89+ }
90+
91+ return 0 ;
92+ case BLE_GAP_EVENT_DISCONNECT :
93+ ESP_LOGI (TAG , "[Disconnected], conn_handle = 0x%02x, status = 0x%0x\n" ,event -> disconnect .conn .conn_handle ,event -> disconnect .reason );
94+ conn = 0 ;
95+ return 0 ;
96+
8697 case BLE_GAP_EVENT_PER_SUBEV_DATA_REQ :
8798 ESP_LOGI (TAG , "[Request] data: %x, subevt start:%d, subevt count:%d" ,
8899 sub_data_pattern [0 ],
@@ -115,20 +126,17 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
115126
116127 case BLE_GAP_EVENT_PER_SUBEV_RESP :
117128
118- if (event -> periodic_adv_response .data_status == BLE_GAP_PER_ADV_DATA_STATUS_INCOMPLETE ) {
119- // ESP_LOGI(TAG,"Incomplete response report received, discarding \n");
120- }
121- else if (event -> periodic_adv_response .data_status == BLE_GAP_PER_ADV_DATA_STATUS_RX_FAILED ) {
122- // ESP_LOGI(TAG,"Controller failed to received the AUX_SYNC_SUBEVENT_RSP\n");
123- }
124- else if (event -> periodic_adv_response .data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE ) {
125- ESP_LOGI (TAG , "[Response] subevent:%d, response_slot:%d, data_length:%d, data:%x" ,
129+ if (event -> periodic_adv_response .data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE ) {
130+ ESP_LOGI (TAG , "[Response] subevent:%d, response_slot:%d, data_length:%d" ,
126131 event -> periodic_adv_response .subevent ,
127132 event -> periodic_adv_response .response_slot ,
128- event -> periodic_adv_response .data_length ,
129- event -> periodic_adv_response .data [0 ]);
133+ event -> periodic_adv_response .data_length );
134+ const uint8_t * data = event -> periodic_adv_response .data ;
135+ ESP_LOGI (TAG , "data: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x" ,
136+ data [0 ], data [1 ], data [2 ], data [3 ], data [4 ], data [5 ], data [6 ], data [7 ], data [8 ], data [9 ]);
137+
130138 peer_addr .type = 0 ;
131- memcpy (peer_addr .val ,& event -> periodic_adv_response .data [1 ],6 );
139+ memcpy (peer_addr .val ,& event -> periodic_adv_response .data [2 ],6 );
132140
133141 adv_handle = event -> periodic_adv_response .adv_handle ;
134142 subevent = event -> periodic_adv_response .subevent ;
@@ -138,15 +146,16 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
138146 rc = ble_gap_connect_with_synced (0 ,adv_handle ,subevent ,& peer_addr ,30000 ,phy_mask ,NULL ,NULL ,NULL ,gap_event_cb ,NULL );
139147 if (rc != 0 ) {
140148 ESP_LOGI (TAG ,"Error: Failed to connect to device , rc = %d\n" ,rc );
149+ }else {
150+ ESP_LOGI (TAG ,"Connection create sent, adv handle = %d, subevent = %d" , adv_handle , subevent );
141151 }
142152 conn = 1 ;
143153 }
144- }
145- else if (event -> periodic_adv_response .data_status == BLE_GAP_PER_ADV_DATA_STATUS_TRUNCATED ) {
146- // ESP_LOGI(TAG,"Truncated response report received, discarding\n");
147- }
148- else {
149- ESP_LOGE (TAG ,"Invalid data status\n" );
154+ } else {
155+ ESP_LOGE (TAG , "[Response] subevent:%d, response_slot:%d, rsp_data status:%d" ,
156+ event -> periodic_adv_response .subevent ,
157+ event -> periodic_adv_response .response_slot ,
158+ event -> periodic_adv_response .data_status );
150159 }
151160
152161 return 0 ;
@@ -185,14 +194,14 @@ start_periodic_adv(void)
185194 params .primary_phy = BLE_HCI_LE_PHY_CODED ;
186195 params .secondary_phy = BLE_HCI_LE_PHY_1M ;
187196 params .sid = 0 ;
188- params .itvl_min = BLE_GAP_ADV_ITVL_MS (100 );
189- params .itvl_max = BLE_GAP_ADV_ITVL_MS (100 );
197+ params .itvl_min = BLE_GAP_ADV_ITVL_MS (50 );
198+ params .itvl_max = BLE_GAP_ADV_ITVL_MS (50 );
190199
191200 rc = ble_gap_ext_adv_configure (instance , & params , NULL , gap_event_cb , NULL );
192201 assert (rc == 0 );
193202
194203 memset (& adv_fields , 0 , sizeof (adv_fields ));
195- adv_fields .name = (const uint8_t * )"Nimble_PAwR " ;
204+ adv_fields .name = (const uint8_t * )"Nimble_PAwR_CONN " ;
196205 adv_fields .name_len = strlen ((char * )adv_fields .name );
197206
198207 /* mbuf chain will be increased if needed */
@@ -212,9 +221,9 @@ start_periodic_adv(void)
212221 pparams .itvl_max = BLE_GAP_PERIODIC_ITVL_MS (3000 );
213222 /* Configure the parameters of PAwR. */
214223 pparams .num_subevents = BLE_PAWR_NUM_SUBEVTS ;
215- pparams .subevent_interval = BLE_GAP_PERIODIC_ITVL_MS ( 300 ) ;
216- pparams .response_slot_delay = BLE_GAP_PERIODIC_ITVL_MS ( 80 ) ;
217- pparams .response_slot_spacing = 0xFF ;
224+ pparams .subevent_interval = BLE_PAWR_SUB_INTERVAL ;
225+ pparams .response_slot_delay = BLE_PAWR_RSP_SLOT_DELAY ;
226+ pparams .response_slot_spacing = BLE_PAWR_RSP_SLOT_SPACING ;
218227 pparams .num_response_slots = BLE_PAWR_NUM_RSP_SLOTS ;
219228
220229 rc = ble_gap_periodic_adv_configure (instance , & pparams );
0 commit comments