Skip to content

Commit 72e5eb8

Browse files
author
Ben Leadbetter
committed
fix: fixes sysex next impl broken with 'empty' packets
1 parent 4dddc34 commit 72e5eb8

File tree

2 files changed

+218
-3
lines changed

2 files changed

+218
-3
lines changed

src/sysex7.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,11 @@ impl<'a, U: crate::buffer::Unit> core::iter::Iterator for PayloadIterator<'a, U>
675675
return None;
676676
}
677677

678+
self.skip_empty_packets_ump();
679+
678680
let ret = Some(self.value_ump());
679681
self.advance_ump();
682+
680683
ret
681684
}
682685
_ => unreachable!(),
@@ -773,19 +776,30 @@ impl<'a, U: crate::buffer::Unit> PayloadIterator<'a, U> {
773776
self.size_cache == 0
774777
}
775778

779+
fn skip_empty_packets_ump(&mut self) {
780+
while !self.finished_ump() && self.current_packet_size_ump() == 0 {
781+
self.payload_index = 0;
782+
self.packet_index += 1;
783+
}
784+
}
785+
776786
fn advance_ump(&mut self) {
777787
self.payload_index += 1;
778788
if !self.finished_ump() {
779789
self.size_cache -= 1;
780790
}
781791

782-
let current_packet_size =
783-
Self::packet_size(&self.data_ump()[self.packet_index * 2..self.packet_index * 2 + 2]);
784-
if self.payload_index == current_packet_size {
792+
if self.payload_index == self.current_packet_size_ump() {
785793
// end of packet
786794
self.packet_index += 1;
787795
self.payload_index = 0;
788796
}
797+
798+
self.skip_empty_packets_ump();
799+
}
800+
801+
fn current_packet_size_ump(&self) -> usize {
802+
Self::packet_size(&self.data_ump()[self.packet_index * 2..self.packet_index * 2 + 2])
789803
}
790804

791805
fn packet_size(packet: &[u32]) -> usize {
@@ -1347,6 +1361,52 @@ mod tests {
13471361
assert_eq!(payload.nth(0), None);
13481362
}
13491363

1364+
#[test]
1365+
fn payload_bytes_nth_with_empty_packets() {
1366+
let buffer = [
1367+
0x3010_0000_u32,
1368+
0x0000_0000,
1369+
0x3021_0000,
1370+
0x0000_0000,
1371+
0x3022_0102,
1372+
0x0000_0000,
1373+
0x3020_0000,
1374+
0x0000_0000,
1375+
0x3020_0000,
1376+
0x0000_0000,
1377+
0x3023_0304,
1378+
0x0500_0000,
1379+
0x3024_0607,
1380+
0x0809_0000,
1381+
0x3025_0A0B,
1382+
0x0C0D_0E00,
1383+
0x3026_0F10,
1384+
0x1112_1314,
1385+
0x3025_1516,
1386+
0x1718_1900,
1387+
0x3034_1A1B,
1388+
0x1C1D_0000,
1389+
0x0000_0000,
1390+
0x0000_0000,
1391+
0x0000_0000,
1392+
0x0000_0000,
1393+
0x0000_0000,
1394+
];
1395+
let message = Sysex7::try_from(&buffer[..]).unwrap();
1396+
let mut payload = message.payload();
1397+
assert_eq!(payload.len(), 30);
1398+
assert_eq!(payload.next(), Some(u7::new(0x0)));
1399+
assert_eq!(payload.len(), 29);
1400+
assert_eq!(payload.nth(4), Some(u7::new(0x5)));
1401+
assert_eq!(payload.len(), 24);
1402+
assert_eq!(payload.nth(12), Some(u7::new(0x12)));
1403+
assert_eq!(payload.len(), 11);
1404+
assert_eq!(payload.nth(10), Some(u7::new(0x1D)));
1405+
assert_eq!(payload.len(), 0);
1406+
assert_eq!(payload.next(), None);
1407+
assert_eq!(payload.len(), 0);
1408+
}
1409+
13501410
#[test]
13511411
fn payload_bytes_len() {
13521412
assert_eq!(

src/sysex8.rs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ impl<'a> core::iter::Iterator for PayloadIterator<'a> {
214214
if self.finished() {
215215
return None;
216216
}
217+
218+
// skip empty packets
219+
while !self.finished() && self.size_of_current_packet() == 0 {
220+
self.payload_index = 0;
221+
self.packet_index += 1;
222+
}
223+
217224
let ret = Some(self.value());
218225
self.advance();
219226
ret
@@ -866,6 +873,154 @@ mod tests {
866873
assert_eq!(payload.next(), None);
867874
}
868875

876+
#[test]
877+
fn payload_nth_non_contiguous_payload() {
878+
let message = Sysex8::try_from(
879+
&[
880+
// empty
881+
0x5411_BB00,
882+
0x0000_0000,
883+
0x0000_0000,
884+
0x0000_0000,
885+
0x5422_BB00,
886+
0x0000_0000,
887+
0x0000_0000,
888+
0x0000_0000,
889+
0x5423_BB01,
890+
0x0200_0000,
891+
0x0000_0000,
892+
0x0000_0000,
893+
// empty
894+
0x5421_BB00,
895+
0x0000_0000,
896+
0x0000_0000,
897+
0x0000_0000,
898+
// empty
899+
0x5421_BB00,
900+
0x0000_0000,
901+
0x0000_0000,
902+
0x0000_0000,
903+
0x5424_BB03,
904+
0x0405_0000,
905+
0x0000_0000,
906+
0x0000_0000,
907+
0x5425_BB06,
908+
0x0708_0900,
909+
0x0000_0000,
910+
0x0000_0000,
911+
0x5426_BB0A,
912+
0x0B0C_0D0E,
913+
0x0000_0000,
914+
0x0000_0000,
915+
0x5427_BB0F,
916+
0x1011_1213,
917+
0x1400_0000,
918+
0x0000_0000,
919+
0x5428_BB15,
920+
0x1617_1819,
921+
0x1A1B_0000,
922+
0x0000_0000,
923+
0x5429_BB1C,
924+
0x1D1E_1F20,
925+
0x2122_2300,
926+
0x0000_0000,
927+
0x542A_BB24,
928+
0x2526_2728,
929+
0x292A_2B2C,
930+
0x0000_0000,
931+
0x5436_BB2D,
932+
0x2E2F_3031,
933+
0x0000_0000,
934+
0x0000_0000,
935+
][..],
936+
)
937+
.unwrap();
938+
let mut payload = message.payload();
939+
assert_eq!(payload.len(), 50);
940+
assert_eq!(payload.nth(13), Some(0x0D));
941+
assert_eq!(payload.len(), 36);
942+
assert_eq!(payload.nth(11), Some(0x19));
943+
assert_eq!(payload.len(), 24);
944+
assert_eq!(payload.nth(11), Some(0x25));
945+
assert_eq!(payload.len(), 12);
946+
assert_eq!(payload.nth(4), Some(0x2A));
947+
assert_eq!(payload.len(), 7);
948+
assert_eq!(payload.nth(5), Some(0x30));
949+
assert_eq!(payload.len(), 1);
950+
assert_eq!(payload.next(), Some(0x31));
951+
assert_eq!(payload.len(), 0);
952+
assert_eq!(payload.next(), None);
953+
}
954+
955+
#[test]
956+
fn payload_next_non_contiguous_payload() {
957+
let message = Sysex8::try_from(
958+
&[
959+
// empty
960+
0x5411_BB00,
961+
0x0000_0000,
962+
0x0000_0000,
963+
0x0000_0000,
964+
0x5422_BB00,
965+
0x0000_0000,
966+
0x0000_0000,
967+
0x0000_0000,
968+
0x5423_BB01,
969+
0x0200_0000,
970+
0x0000_0000,
971+
0x0000_0000,
972+
// empty
973+
0x5421_BB00,
974+
0x0000_0000,
975+
0x0000_0000,
976+
0x0000_0000,
977+
// empty
978+
0x5421_BB00,
979+
0x0000_0000,
980+
0x0000_0000,
981+
0x0000_0000,
982+
0x5424_BB03,
983+
0x0405_0000,
984+
0x0000_0000,
985+
0x0000_0000,
986+
0x5425_BB06,
987+
0x0708_0900,
988+
0x0000_0000,
989+
0x0000_0000,
990+
0x5426_BB0A,
991+
0x0B0C_0D0E,
992+
0x0000_0000,
993+
0x0000_0000,
994+
0x5427_BB0F,
995+
0x1011_1213,
996+
0x1400_0000,
997+
0x0000_0000,
998+
0x5428_BB15,
999+
0x1617_1819,
1000+
0x1A1B_0000,
1001+
0x0000_0000,
1002+
0x5429_BB1C,
1003+
0x1D1E_1F20,
1004+
0x2122_2300,
1005+
0x0000_0000,
1006+
0x542A_BB24,
1007+
0x2526_2728,
1008+
0x292A_2B2C,
1009+
0x0000_0000,
1010+
0x5436_BB2D,
1011+
0x2E2F_3031,
1012+
0x0000_0000,
1013+
0x0000_0000,
1014+
][..],
1015+
)
1016+
.unwrap();
1017+
let mut payload = message.payload();
1018+
for i in 0..50 {
1019+
assert_eq!(payload.len(), 50 - i);
1020+
assert_eq!(payload.next(), Some(i as u8));
1021+
}
1022+
}
1023+
8691024
#[test]
8701025
fn set_payload() {
8711026
let mut message = Sysex8::<std::vec::Vec<u32>>::new();

0 commit comments

Comments
 (0)