6060#define BLE_SLAVE_LATENCY 0
6161#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
6262
63+ #ifndef BLE_GAP_ADV_MAX_SIZE
64+ #define BLE_GAP_ADV_MAX_SIZE 31
65+ #endif
66+
6367#define SD_TEST_OR_ENABLE () \
6468if (ble_drv_stack_enabled() == 0) { \
6569 (void)ble_drv_stack_enable(); \
@@ -90,6 +94,17 @@ static mp_obj_t mp_gattc_disc_service_observer;
9094static mp_obj_t mp_gattc_disc_char_observer ;
9195static mp_obj_t mp_gattc_char_data_observer ;
9296
97+ #if (BLUETOOTH_SD == 140 )
98+ static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET ;
99+ static uint8_t m_scan_buffer_data [BLE_GAP_SCAN_BUFFER_MIN ];
100+
101+ static ble_data_t m_scan_buffer =
102+ {
103+ m_scan_buffer_data ,
104+ BLE_GAP_SCAN_BUFFER_MIN
105+ };
106+ #endif
107+
93108#include "nrf_nvic.h"
94109
95110nrf_nvic_state_t nrf_nvic_state = {0 };
@@ -133,7 +148,7 @@ uint32_t ble_drv_stack_enable(void) {
133148 err_code = sd_nvic_EnableIRQ (SWI2_EGU2_IRQn );
134149
135150 BLE_DRIVER_LOG ("IRQ enable status: " UINT_FMT "\n" , (uint16_t )err_code );
136-
151+
137152 // Enable BLE stack.
138153#if (BLE_API_VERSION == 2 )
139154 ble_enable_params_t ble_enable_params ;
@@ -359,12 +374,7 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
359374 SD_TEST_OR_ENABLE ();
360375
361376 uint8_t byte_pos = 0 ;
362-
363- #if (BLUETOOTH_SD == 140 )
364- uint8_t adv_data [BLE_GAP_ADV_SR_MAX_LEN_DEFAULT ];
365- #else
366377 uint8_t adv_data [BLE_GAP_ADV_MAX_SIZE ];
367- #endif
368378
369379 if (p_adv_params -> device_name_len > 0 ) {
370380 ble_gap_conn_sec_mode_t sec_mode ;
@@ -518,11 +528,7 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
518528 }
519529
520530 if ((p_adv_params -> data_len > 0 ) && (p_adv_params -> p_data != NULL )) {
521- #if (BLUETOOTH_SD == 140 )
522- if (p_adv_params -> data_len + byte_pos > BLE_GAP_ADV_SR_MAX_LEN_DEFAULT ) {
523- #else
524531 if (p_adv_params -> data_len + byte_pos > BLE_GAP_ADV_MAX_SIZE ) {
525- #endif
526532 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
527533 "Can not fit data into the advertisment packet." ));
528534 }
@@ -533,55 +539,59 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
533539
534540 // scan response data not set
535541 uint32_t err_code ;
536- #if (BLUETOOTH_SD == 140 )
537- const ble_data_t ble_adv_data = {
538- .p_data = adv_data ,
539- .len = byte_pos
540- };
541-
542- if ((err_code = sd_ble_gap_adv_data_set (BLE_GAP_ADV_SET_HANDLE_DEFAULT , & ble_adv_data , NULL )) != 0 ) {
543- #else
542+ #if (BLUETOOTH_SD == 132 )
544543 if ((err_code = sd_ble_gap_adv_data_set (adv_data , byte_pos , NULL , 0 )) != 0 ) {
545- #endif
544+
546545 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
547546 "Can not apply advertisment data. status: 0x" HEX2_FMT , (uint16_t )err_code ));
548547 }
549548 BLE_DRIVER_LOG ("Set Adv data size: " UINT_FMT "\n" , byte_pos );
549+ #endif
550550
551551 static ble_gap_adv_params_t m_adv_params ;
552552
553553 // initialize advertising params
554554 memset (& m_adv_params , 0 , sizeof (m_adv_params ));
555555 if (p_adv_params -> connectable ) {
556556#if (BLUETOOTH_SD == 140 )
557- m_adv_params .properties .connectable = 1 ;
558- m_adv_params .properties .scannable = 1 ;
557+ m_adv_params .properties .type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED ;
559558#else
560559 m_adv_params .type = BLE_GAP_ADV_TYPE_ADV_IND ;
561560#endif
562561 } else {
563562#if (BLUETOOTH_SD == 140 )
564- m_adv_params .properties .connectable = 0 ;
563+ m_adv_params .properties .type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED ;
565564#else
566565 m_adv_params .type = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND ;
567566#endif
568567 }
569568
570569 m_adv_params .p_peer_addr = NULL ; // undirected advertisement
571- m_adv_params .fp = BLE_GAP_ADV_FP_ANY ;
572570 m_adv_params .interval = MSEC_TO_UNITS (100 , UNIT_0_625_MS ); // approx 8 ms
573571#if (BLUETOOTH_SD == 140 )
574- m_adv_params .duration = 0 ; // infinite advertisment
575- m_adv_params .properties . legacy_pdu = 1 ;
572+ m_adv_params .duration = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED ;
573+ m_adv_params .filter_policy = BLE_GAP_ADV_FP_ANY ;
576574 m_adv_params .primary_phy = BLE_GAP_PHY_1MBPS ;
577575#else
576+ m_adv_params .fp = BLE_GAP_ADV_FP_ANY ;
578577 m_adv_params .timeout = 0 ; // infinite advertisment
579578#endif
580579
581580 ble_drv_advertise_stop ();
582581
583582#if (BLUETOOTH_SD == 140 )
584- err_code = sd_ble_gap_adv_start (BLE_GAP_ADV_SET_HANDLE_DEFAULT , & m_adv_params , BLE_CONN_CFG_TAG_DEFAULT );
583+ const ble_gap_adv_data_t ble_gap_adv_data = {
584+ .adv_data = {
585+ .p_data = adv_data ,
586+ .len = byte_pos
587+ }
588+ };
589+
590+ if ((err_code = sd_ble_gap_adv_set_configure (& m_adv_handle , & ble_gap_adv_data , & m_adv_params )) != 0 ) {
591+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
592+ "Can not apply advertisment data. status: 0x" HEX2_FMT , (uint16_t )err_code ));
593+ }
594+ err_code = sd_ble_gap_adv_start (m_adv_handle , BLE_CONN_CFG_TAG_DEFAULT );
585595#elif (BLUETOOTH_SD == 132 && BLE_API_VERSION == 4 )
586596 err_code = sd_ble_gap_adv_start (& m_adv_params , BLE_CONN_CFG_TAG_DEFAULT );
587597#else
@@ -601,7 +611,7 @@ void ble_drv_advertise_stop(void) {
601611 if (m_adv_in_progress == true) {
602612 uint32_t err_code ;
603613#if (BLUETOOTH_SD == 140 )
604- if ((err_code = sd_ble_gap_adv_stop (BLE_GAP_ADV_SET_HANDLE_DEFAULT )) != 0 ) {
614+ if ((err_code = sd_ble_gap_adv_stop (m_adv_handle )) != 0 ) {
605615#else
606616 if ((err_code = sd_ble_gap_adv_stop ()) != 0 ) {
607617#endif
@@ -738,7 +748,6 @@ void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
738748 ;
739749 }
740750}
741-
742751void ble_drv_scan_start (void ) {
743752 SD_TEST_OR_ENABLE ();
744753
@@ -749,16 +758,16 @@ void ble_drv_scan_start(void) {
749758 scan_params .interval = MSEC_TO_UNITS (100 , UNIT_0_625_MS );
750759 scan_params .window = MSEC_TO_UNITS (100 , UNIT_0_625_MS );
751760#if (BLUETOOTH_SD == 140 )
752- scan_params .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL ;
753- scan_params .filter_duplicates = BLE_GAP_SCAN_DUPLICATES_SUPPRESS ;
754- scan_params .scan_phy = BLE_GAP_PHY_1MBPS ;
755- scan_params .duration = 0 ; // Infinite
756- #else
757- scan_params .timeout = 0 ; // Infinite
761+ scan_params .scan_phys = BLE_GAP_PHY_1MBPS ;
758762#endif
763+ scan_params .timeout = 0 ; // Infinite
759764
760765 uint32_t err_code ;
766+ #if (BLUETOOTH_SD == 140 )
767+ if ((err_code = sd_ble_gap_scan_start (& scan_params , & m_scan_buffer )) != 0 ) {
768+ #else
761769 if ((err_code = sd_ble_gap_scan_start (& scan_params )) != 0 ) {
770+ #endif
762771 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
763772 "Can not start scanning. status: 0x" HEX2_FMT , (uint16_t )err_code ));
764773 }
@@ -775,11 +784,7 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
775784 scan_params .active = 1 ;
776785 scan_params .interval = MSEC_TO_UNITS (100 , UNIT_0_625_MS );
777786 scan_params .window = MSEC_TO_UNITS (100 , UNIT_0_625_MS );
778- #if (BLUETOOTH_SD == 140 )
779- scan_params .duration = 0 ; // Infinite
780- #else
781787 scan_params .timeout = 0 ; // Infinite
782- #endif
783788
784789 ble_gap_addr_t addr ;
785790 memset (& addr , 0 , sizeof (addr ));
@@ -962,8 +967,13 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
962967 .is_scan_resp = p_ble_evt -> evt .gap_evt .params .adv_report .scan_rsp ,
963968#endif
964969 .rssi = p_ble_evt -> evt .gap_evt .params .adv_report .rssi ,
970+ #if (BLUETOOTH_SD == 140 )
971+ .data_len = p_ble_evt -> evt .gap_evt .params .adv_report .data .len ,
972+ .p_data = p_ble_evt -> evt .gap_evt .params .adv_report .data .p_data ,
973+ #else
965974 .data_len = p_ble_evt -> evt .gap_evt .params .adv_report .dlen ,
966975 .p_data = p_ble_evt -> evt .gap_evt .params .adv_report .data ,
976+ #endif
967977#if (BLUETOOTH_SD == 132 )
968978 .adv_type = p_ble_evt -> evt .gap_evt .params .adv_report .type
969979#endif
0 commit comments