@@ -12,9 +12,9 @@ use nrf_rpc::ble::cgm::{
1212 CgmMeasurement , encode_uuid_16,
1313} ;
1414use nrf_rpc:: ble:: {
15- BT_GATT_CCC_NOTIFY , BT_LE_SCAN_TYPE_ACTIVE , BtConnLeCreateParam , BtGattDiscoverParams ,
16- BtGattDiscoverType , BtGattSubscribeParams , BtLeConnParam , BtLeScanParam , GattDiscoverResult ,
17- ScanResultData ,
15+ BleEvent , BT_GATT_CCC_NOTIFY , BT_LE_SCAN_TYPE_ACTIVE , BtConnLeCreateParam ,
16+ BtGattDiscoverParams , BtGattDiscoverType , BtGattSubscribeParams , BtLeConnParam , BtLeScanParam ,
17+ GattDiscoverResult , ScanResultData ,
1818} ;
1919use babble_bridge:: { LogOutput , TestProcesses , spawn_zephyr_rpc_server_with_socat} ;
2020use nrf_rpc:: { RpcClient , TransportError , ble:: Ble , uart_transport:: { Uart , UartTransport } } ;
@@ -191,7 +191,7 @@ impl Uart for MockUart {
191191 }
192192 }
193193
194- async fn write ( & mut self , data : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
194+ async fn write ( & mut self , data : & [ u8 ] ) -> Result < usize , Self :: Error > {
195195 println ! (
196196 "MockUart: Sending {} bytes to {}: {:02X?}" ,
197197 data. len( ) ,
@@ -226,6 +226,10 @@ impl Uart for MockUart {
226226 async fn delay_ms ( & mut self , ms : u32 ) {
227227 std:: thread:: sleep ( Duration :: from_millis ( ms as u64 ) ) ;
228228 }
229+
230+ fn has_buffered_data ( & mut self ) -> bool {
231+ !self . rx_buffer . lock ( ) . unwrap ( ) . is_empty ( )
232+ }
229233}
230234
231235/// Helper to convert hex string to bytes
@@ -742,7 +746,16 @@ fn test_cgm_full_central_flow_inner() {
742746 let max_scan_results = 50 ;
743747
744748 for i in 0 ..max_scan_results {
745- let result = embassy_futures:: block_on ( ble. wait_for_scan_result ( ) ) ;
749+ let result = embassy_futures:: block_on ( async {
750+ loop {
751+ match ble. next_event ( ) . await {
752+ Ok ( BleEvent :: ScanResult ( s) ) => return Ok ( s) ,
753+ Ok ( BleEvent :: ScanTimeout ) => return Err ( nrf_rpc:: ble:: BleError :: RpcError ) ,
754+ Ok ( _) => continue ,
755+ Err ( e) => return Err ( e) ,
756+ }
757+ }
758+ } ) ;
746759 match result {
747760 Ok ( scan) => {
748761 let name = scan. device_name ( ) . unwrap_or ( "<unknown>" ) ;
@@ -809,7 +822,16 @@ fn test_cgm_full_central_flow_inner() {
809822 // Step 6: Wait for the "connected" callback event
810823 // ------------------------------------------------------------------
811824 println ! ( "[Step 6] Waiting for connection event..." ) ;
812- let conn_event = embassy_futures:: block_on ( ble. wait_for_connection ( ) ) ;
825+ let conn_event = embassy_futures:: block_on ( async {
826+ for _ in 0 ..20 {
827+ match ble. next_event ( ) . await {
828+ Ok ( BleEvent :: Connected ( e) ) => return Ok ( e) ,
829+ Ok ( _) => continue ,
830+ Err ( e) => return Err ( e) ,
831+ }
832+ }
833+ Err ( nrf_rpc:: ble:: BleError :: RpcError )
834+ } ) ;
813835 assert ! (
814836 conn_event. is_ok( ) ,
815837 "Did not receive connection event: {:?}" ,
@@ -846,7 +868,22 @@ fn test_cgm_full_central_flow_inner() {
846868
847869 // Wait for the SMP exchange to complete (passkey exchange + security level 4)
848870 println ! ( "[Step 6a] Waiting for security level 4..." ) ;
849- let result = embassy_futures:: block_on ( ble. wait_for_security_level ( 4 ) ) ;
871+ let result = embassy_futures:: block_on ( async {
872+ for _ in 0 ..30 {
873+ match ble. next_event ( ) . await {
874+ Ok ( BleEvent :: SecurityChanged { level, err : 0 } ) if level >= 4 => return Ok ( level) ,
875+ Ok ( BleEvent :: PasskeyConfirm ( _) ) => {
876+ let _ = ble. bt_conn_auth_passkey_confirm ( ) . await ;
877+ }
878+ Ok ( BleEvent :: PairingConfirm ) => {
879+ let _ = ble. bt_conn_auth_pairing_confirm ( ) . await ;
880+ }
881+ Ok ( _) => continue ,
882+ Err ( e) => return Err ( e) ,
883+ }
884+ }
885+ Err ( nrf_rpc:: ble:: BleError :: RpcError )
886+ } ) ;
850887 assert ! (
851888 result. is_ok( ) ,
852889 "Failed to achieve security level 4: {:?}" ,
@@ -884,7 +921,15 @@ fn test_cgm_full_central_flow_inner() {
884921 let mut cgm_service_end_handle: Option < u16 > = None ;
885922
886923 for i in 0 ..20 {
887- let result = embassy_futures:: block_on ( ble. wait_for_gatt_discover_result ( ) ) ;
924+ let result = embassy_futures:: block_on ( async {
925+ loop {
926+ match ble. next_event ( ) . await {
927+ Ok ( BleEvent :: GattDiscovery ( r) ) => return Ok ( r) ,
928+ Ok ( _) => continue ,
929+ Err ( e) => return Err ( e) ,
930+ }
931+ }
932+ } ) ;
888933 match result {
889934 Ok ( GattDiscoverResult :: Service {
890935 handle,
@@ -951,7 +996,15 @@ fn test_cgm_full_central_flow_inner() {
951996 let mut cgm_meas_value_handle: Option < u16 > = None ;
952997
953998 for i in 0 ..20 {
954- let result = embassy_futures:: block_on ( ble. wait_for_gatt_discover_result ( ) ) ;
999+ let result = embassy_futures:: block_on ( async {
1000+ loop {
1001+ match ble. next_event ( ) . await {
1002+ Ok ( BleEvent :: GattDiscovery ( r) ) => return Ok ( r) ,
1003+ Ok ( _) => continue ,
1004+ Err ( e) => return Err ( e) ,
1005+ }
1006+ }
1007+ } ) ;
9551008 match result {
9561009 Ok ( GattDiscoverResult :: Characteristic {
9571010 handle,
@@ -1017,7 +1070,15 @@ fn test_cgm_full_central_flow_inner() {
10171070 let mut ccc_handle: Option < u16 > = None ;
10181071
10191072 for i in 0 ..20 {
1020- let result = embassy_futures:: block_on ( ble. wait_for_gatt_discover_result ( ) ) ;
1073+ let result = embassy_futures:: block_on ( async {
1074+ loop {
1075+ match ble. next_event ( ) . await {
1076+ Ok ( BleEvent :: GattDiscovery ( r) ) => return Ok ( r) ,
1077+ Ok ( _) => continue ,
1078+ Err ( e) => return Err ( e) ,
1079+ }
1080+ }
1081+ } ) ;
10211082 match result {
10221083 Ok ( GattDiscoverResult :: Descriptor { handle, uuid_16 } ) => {
10231084 println ! ( " Desc #{}: UUID=0x{:04X} handle={}" , i, uuid_16, handle) ;
@@ -1085,7 +1146,15 @@ fn test_cgm_full_central_flow_inner() {
10851146 let max_notification_attempts = 30 ;
10861147
10871148 for attempt in 0 ..max_notification_attempts {
1088- let result = embassy_futures:: block_on ( ble. wait_for_gatt_notification ( ) ) ;
1149+ let result = embassy_futures:: block_on ( async {
1150+ loop {
1151+ match ble. next_event ( ) . await {
1152+ Ok ( BleEvent :: GattNotification ( n) ) => return Ok ( n) ,
1153+ Ok ( _) => continue ,
1154+ Err ( e) => return Err ( e) ,
1155+ }
1156+ }
1157+ } ) ;
10891158 match result {
10901159 Ok ( notif) => {
10911160 println ! (
0 commit comments