rust/bt-ascs: Use preferred presentation delay

A Preferred QoS request built its Config QoS from the minimum
Presentation_Delay the peer supports, and ignored the preferred window
the peer publishes in the Codec Configured state. BAP v1.0.2 Section
5.6.2 says the requested delay should fall inside that window. A peer
that prefers a longer delay, for example to cover its rendering path,
got a value it did not ask for.

Request the low end of the preferred window instead. A peer with no
preference gets the supported minimum, as before. A new test covers a
peer whose preferred minimum is above its supported minimum.

Test: cargo test -p bt-ascs
Change-Id: Ica419f8279665314be7fd3ec8e980e016a6a6964
Reviewed-on: https://bluetooth-review.googlesource.com/c/bluetooth/+/4124
diff --git a/rust/bt-ascs/src/client.rs b/rust/bt-ascs/src/client.rs
index 58641d5..6aa4ac7 100644
--- a/rust/bt-ascs/src/client.rs
+++ b/rust/bt-ascs/src/client.rs
@@ -113,7 +113,14 @@
                     },
                 ))?;
 
-                let presentation_delay = params.presentation_delay_range.min().clone();
+                // BAP v1.0.2 Section 5.6.2 says the requested
+                // Presentation_Delay should be inside the peer's preferred
+                // range. preferred_range() returns the supported range when the
+                // peer has no preference. Request the low end of that range.
+                // This picks a delay for each ASE on its own. A caller that
+                // needs one value per direction (BAP v1.0.2 Section 7.1.3) has
+                // to combine the ranges before calling.
+                let presentation_delay = params.presentation_delay_range.preferred_range().0;
 
                 Ok(QosConfiguration {
                     ase_id,
@@ -1108,6 +1115,92 @@
     }
 
     #[test]
+    fn configure_qos_preferred_uses_preferred_delay_min() {
+        let mut service = setup_fake_service();
+        let client_fut = AudioStreamControlServiceClient::<FakeTypes>::create(service.clone());
+        let mut client = run_to_completion(client_fut).expect("client creation should succeed");
+
+        // Same as `configure_qos_success`, except that the sink ASE publishes a
+        // Preferred_Presentation_Delay_Min of 20000us inside its supported
+        // range of [10000, 40000]us.
+        #[rustfmt::skip]
+        let sink_value = vec![
+            0x01, // ASE ID: 1
+            0x01, // ASE State: Codec Configured
+            0x00, // Preferred Framing
+            0x01, // Preferred PHY
+            0x02, // Preferred Retransmission Number
+            0x0A, 0x00, // Max Transport Latency
+            0x10, 0x27, 0x00, // Presentation Delay Min: 10000
+            0x40, 0x9C, 0x00, // Presentation Delay Max: 40000
+            0x20, 0x4E, 0x00, // Preferred Presentation Delay Min: 20000
+            0x00, 0x00, 0x00, // Preferred Presentation Delay Max: no preference
+            0x06, 0x00, 0x00, 0x00, 0x00, // Codec ID
+            0x00, // Codec Specific Configuration Length
+        ];
+        client.endpoints.sink.get_mut(&SINK_ASE_HANDLE).unwrap().endpoint =
+            AudioStreamEndpoint::from_char_value(
+                SINK_ASE_HANDLE,
+                AudioDirection::Sink,
+                &sink_value,
+            )
+            .unwrap();
+
+        let req = QosConfigurationRequest::Preferred {
+            ase_id: AseId(1),
+            cig_id: CigId::try_from(1).unwrap(),
+            cis_id: CisId::try_from(1).unwrap(),
+            sdu_interval: SduInterval::try_from(std::time::Duration::from_micros(10000)).unwrap(),
+            max_sdu: MaxSdu::try_from(100).unwrap(),
+        };
+
+        #[rustfmt::skip]
+        service.expect_characteristic_value(
+            &CONTROL_POINT_HANDLE,
+            vec![
+                0x02, // Opcode: Config QoS
+                0x01, // Num ASEs: 1
+                0x01, // ASE ID: 1
+                0x01, 0x01, 0x10, 0x27, 0x00, 0x00, 0x01, 0x64, 0x00, 0x02, 0x0A, 0x00, 0x20, 0x4E, 0x00, // Preferred QoS, delay 20000
+            ],
+        );
+
+        #[rustfmt::skip]
+        service.notify(
+            &CONTROL_POINT_HANDLE,
+            Ok(CharacteristicNotification {
+                handle: CONTROL_POINT_HANDLE,
+                value: vec![
+                    0x02, // Opcode: Config QoS
+                    0x01, // Num ASEs: 1
+                    0x01, 0x00, 0x00, // ASE ID: 1, Success
+                ],
+                maybe_truncated: false,
+            }),
+        );
+
+        #[rustfmt::skip]
+        service.notify(
+            &SINK_ASE_HANDLE,
+            Ok(CharacteristicNotification {
+                handle: SINK_ASE_HANDLE,
+                value: vec![
+                    0x01, // ASE ID: 1
+                    0x02, // ASE State: QoS Configured
+                    0x01, 0x01, 0x10, 0x27, 0x00, 0x00, 0x01, 0x64, 0x00, 0x02, 0x0A, 0x00, 0x20, 0x4E, 0x00,
+                ],
+                maybe_truncated: false,
+            }),
+        );
+
+        let configure_qos_fut = client.configure_qos(vec![req]);
+        let outcome = run_to_completion(configure_qos_fut).expect("configure QoS should succeed");
+
+        assert_eq!(outcome.rejected().len(), 0);
+        assert_eq!(client.endpoints.sink[&SINK_ASE_HANDLE].endpoint.state, AseState::QosConfigured);
+    }
+
+    #[test]
     fn configure_qos_fail_invalid_start_state() {
         let service = setup_fake_service();
         let client_fut = AudioStreamControlServiceClient::<FakeTypes>::create(service.clone());