rust/bt-bass: Fix BisSync synchronization for NO_PREFERENCE

Reset BisSync to 0 when starting from NO_PREFERENCE before applying the
bitmask so that single-index synchronization is set correctly.

Bug: 534436439
Test: cargo test

Change-Id: If8586b619a1da5efc688ea0fb0eab565d9c08566
Reviewed-on: https://bluetooth-review.googlesource.com/c/bluetooth/+/3280
diff --git a/rust/bt-bass/src/types.rs b/rust/bt-bass/src/types.rs
index e8db40e..30a4388 100644
--- a/rust/bt-bass/src/types.rs
+++ b/rust/bt-bass/src/types.rs
@@ -550,8 +550,12 @@
         }
         let bit_mask = 0b1 << (bis_index - 1);
 
-        // Clear the bit that we're interested in setting.
-        self.0 &= !(0b1 << bis_index - 1);
+        if self.0 == Self::NO_PREFERENCE {
+            // No preference should be re-set to 0 so that all subsequent bit_mask
+            // operations correctly set the bits for the specified `bis_index`.
+            // See BASS v1.0.1 Section 3.1.1.4 Table 3.5.
+            self.0 = 0;
+        }
         self.0 |= bit_mask;
         Ok(())
     }
@@ -1103,6 +1107,21 @@
     }
 
     #[test]
+    fn synchronize_to_index_from_default() {
+        let mut bis_sync_default = BisSync::default();
+        assert_eq!(u32::from(bis_sync_default.clone()), 0xFFFFFFFF);
+
+        // An initial synchronization of no preference should correctly be set with the
+        // requested index.
+        bis_sync_default.synchronize_to_index(1).expect("should succeed");
+        assert_eq!(u32::from(bis_sync_default.clone()), 0x1);
+
+        // Additional index should be accumulated correctly.
+        bis_sync_default.synchronize_to_index(6).expect("should succeed");
+        assert_eq!(u32::from(bis_sync_default), 0x21);
+    }
+
+    #[test]
     fn pa_sync_from_str() {
         let sync = PaSync::from_str("PaSyncOff").expect("should succeed");
         assert_eq!(sync, PaSync::DoNotSync);