|
27 | 27 |
|
28 | 28 | pub mod buffer;
|
29 | 29 | pub mod commands;
|
| 30 | +pub mod test; |
30 | 31 |
|
31 | 32 | // ----- Crates -----
|
32 | 33 |
|
@@ -564,6 +565,13 @@ impl<H: ArrayLength<u8>> HidIoPacketBuffer<H> {
|
564 | 565 | // Get packet type
|
565 | 566 | let ptype = packet_type(packet_data)?;
|
566 | 567 |
|
| 568 | + // Check if this a sync packet |
| 569 | + if ptype == HidIoPacketType::Sync { |
| 570 | + self.ptype = ptype; |
| 571 | + self.done = true; |
| 572 | + return Ok(1); |
| 573 | + } |
| 574 | + |
567 | 575 | // Get payload_len
|
568 | 576 | let payload_len = payload_len(packet_data)?;
|
569 | 577 | let packet_len = payload_len + 2;
|
@@ -677,7 +685,7 @@ impl<H: ArrayLength<u8>> HidIoPacketBuffer<H> {
|
677 | 685 |
|
678 | 686 | // Make sure serialization worked
|
679 | 687 | len = writer.written_len();
|
680 |
| - if len < 5 { |
| 688 | + if self.ptype == HidIoPacketType::Sync && len < 2 || self.ptype != HidIoPacketType::Sync && len < 5 { |
681 | 689 | error!(
|
682 | 690 | "Serialization too small: {} -> {:02X?}",
|
683 | 691 | len,
|
@@ -790,6 +798,13 @@ where
|
790 | 798 | // Calculate total length of serialized output
|
791 | 799 | let serialized_len = self.serialized_len();
|
792 | 800 |
|
| 801 | + // Determine if this is a sync packet (much simpler serialization) |
| 802 | + if self.ptype == HidIoPacketType::Sync { |
| 803 | + let mut state = serializer.serialize_seq(Some(1))?; |
| 804 | + state.serialize_element(&hdr_byte)?; |
| 805 | + return state.end(); |
| 806 | + } |
| 807 | + |
793 | 808 | // Serialize as a sequence
|
794 | 809 | let mut state = serializer.serialize_seq(Some(serialized_len as usize))?;
|
795 | 810 |
|
@@ -933,221 +948,8 @@ impl<H: ArrayLength<u8>> fmt::Display for HidIoPacketBuffer<H> {
|
933 | 948 | #[lang = "eh_personality"]
|
934 | 949 | fn eh_personality() {}
|
935 | 950 |
|
936 |
| -//#[cfg(all(not(test), target_feature = "thumb-mode"))] |
937 | 951 | #[cfg(all(not(test), feature = "device"))]
|
938 | 952 | #[panic_handler]
|
939 | 953 | fn panic(_info: &PanicInfo) -> ! {
|
940 | 954 | loop {}
|
941 | 955 | }
|
942 |
| - |
943 |
| -// ----- Tests ----- |
944 |
| - |
945 |
| -#[cfg(test)] |
946 |
| -mod test { |
947 |
| - use super::*; |
948 |
| - use flexi_logger::Logger; |
949 |
| - use heapless::consts::{U1, U110, U170, U60, U7}; |
950 |
| - |
951 |
| - enum LogError { |
952 |
| - CouldNotStartLogger, |
953 |
| - } |
954 |
| - |
955 |
| - /// Lite logging setup |
956 |
| - fn setup_logging_lite() -> Result<(), LogError> { |
957 |
| - match Logger::with_env_or_str("") |
958 |
| - .format(flexi_logger::colored_default_format) |
959 |
| - .format_for_files(flexi_logger::colored_detailed_format) |
960 |
| - .duplicate_to_stderr(flexi_logger::Duplicate::All) |
961 |
| - .start() |
962 |
| - { |
963 |
| - Err(_) => Err(LogError::CouldNotStartLogger), |
964 |
| - Ok(_) => Ok(()), |
965 |
| - } |
966 |
| - } |
967 |
| - |
968 |
| - /// Loopback helper |
969 |
| - /// Serializes, deserializes, then checks if same as original |
970 |
| - fn loopback_serializer<H: ArrayLength<u8> + core::cmp::PartialEq>( |
971 |
| - mut buffer: HidIoPacketBuffer<H>, |
972 |
| - data: &mut [u8], |
973 |
| - ) { |
974 |
| - // Serialize |
975 |
| - let data = match buffer.serialize_buffer(data) { |
976 |
| - Ok(data) => data, |
977 |
| - Err(err) => { |
978 |
| - assert!(false, "Serialized Buffer failed: {:?}", err); |
979 |
| - &[0u8; 0] |
980 |
| - } |
981 |
| - }; |
982 |
| - |
983 |
| - // Validate serialization worked |
984 |
| - assert!(data.len() > 0, "Serialization bytes:{}", data.len()); |
985 |
| - |
986 |
| - // Deserialize while there are bytes left |
987 |
| - let mut deserialized = HidIoPacketBuffer::new(); |
988 |
| - let mut bytes_used = 0; |
989 |
| - while bytes_used != data.len() { |
990 |
| - // Remove already processed bytes |
991 |
| - let slice = &data[bytes_used..]; |
992 |
| - match deserialized.decode_packet(&mut slice.to_vec()) { |
993 |
| - Ok(result) => { |
994 |
| - bytes_used += result as usize; |
995 |
| - } |
996 |
| - _ => { |
997 |
| - panic!("Failured decoding packet"); |
998 |
| - } |
999 |
| - }; |
1000 |
| - } |
1001 |
| - |
1002 |
| - // Set the max_len as decode_packet does not infer this (not enough information from datastream) |
1003 |
| - deserialized.max_len = buffer.max_len; |
1004 |
| - |
1005 |
| - // Validate buffers are the same |
1006 |
| - assert!( |
1007 |
| - buffer == deserialized, |
1008 |
| - "\nInput:{}\nSerialized:{:#?}\nOutput:{}", |
1009 |
| - buffer, |
1010 |
| - data.len(), |
1011 |
| - deserialized |
1012 |
| - ); |
1013 |
| - |
1014 |
| - // Validate all bytes used |
1015 |
| - assert!( |
1016 |
| - data.len() == bytes_used, |
1017 |
| - "Serialized:{}, Deserialized Used:{}", |
1018 |
| - data.len(), |
1019 |
| - bytes_used |
1020 |
| - ); |
1021 |
| - } |
1022 |
| - |
1023 |
| - /// Generates a single byte payload buffer |
1024 |
| - /// Serializes, deserializes, then checks if same as original |
1025 |
| - #[test] |
1026 |
| - fn single_byte_payload_test() { |
1027 |
| - setup_logging_lite().ok(); |
1028 |
| - |
1029 |
| - // Create single byte payload buffer |
1030 |
| - let buffer = HidIoPacketBuffer::<U1> { |
1031 |
| - // Data packet |
1032 |
| - ptype: HidIoPacketType::Data, |
1033 |
| - // Test packet id |
1034 |
| - id: HidIoCommandID::TestPacket, |
1035 |
| - // Standard USB 2.0 FS packet length |
1036 |
| - max_len: 64, |
1037 |
| - // Single byte, 0xAC |
1038 |
| - data: Vec::from_slice(&[0xAC]).unwrap(), |
1039 |
| - // Ready to go |
1040 |
| - done: true, |
1041 |
| - }; |
1042 |
| - |
1043 |
| - // Run loopback serializer, handles all test validation |
1044 |
| - let mut data = [0u8; 64]; |
1045 |
| - loopback_serializer(buffer, &mut data); |
1046 |
| - } |
1047 |
| - |
1048 |
| - /// Generates a full packet payload buffer |
1049 |
| - /// Serializes, deserializes, then checks if same as original |
1050 |
| - #[test] |
1051 |
| - fn full_packet_payload_test() { |
1052 |
| - setup_logging_lite().ok(); |
1053 |
| - |
1054 |
| - // Create single byte payload buffer |
1055 |
| - let buffer = HidIoPacketBuffer::<U60> { |
1056 |
| - // Data packet |
1057 |
| - ptype: HidIoPacketType::Data, |
1058 |
| - // Test packet id |
1059 |
| - id: HidIoCommandID::TestPacket, |
1060 |
| - // Standard USB 2.0 FS packet length |
1061 |
| - max_len: 64, |
1062 |
| - // 60 bytes, 0xAC; requires 2 byte header, and 2 bytes for id, which is 64 bytes |
1063 |
| - data: Vec::from_slice(&[0xAC; 60]).unwrap(), |
1064 |
| - // Ready to go |
1065 |
| - done: true, |
1066 |
| - }; |
1067 |
| - |
1068 |
| - // Run loopback serializer, handles all test validation |
1069 |
| - let mut data = [0u8; 65]; |
1070 |
| - loopback_serializer(buffer, &mut data); |
1071 |
| - } |
1072 |
| - |
1073 |
| - /// Generates a two packet payload buffer |
1074 |
| - /// Serializes, deserializes, then checks if same as original |
1075 |
| - #[test] |
1076 |
| - fn two_packet_continued_payload_test() { |
1077 |
| - setup_logging_lite().ok(); |
1078 |
| - |
1079 |
| - // Create single byte payload buffer |
1080 |
| - let buffer = HidIoPacketBuffer::<U110> { |
1081 |
| - // Data packet |
1082 |
| - ptype: HidIoPacketType::Data, |
1083 |
| - // Test packet id |
1084 |
| - id: HidIoCommandID::TestPacket, |
1085 |
| - // Standard USB 2.0 FS packet length |
1086 |
| - max_len: 64, |
1087 |
| - // 110 bytes, 0xAC: 60 then 50 (62 then 52) |
1088 |
| - data: Vec::from_slice(&[0xAC; 110]).unwrap(), |
1089 |
| - // Ready to go |
1090 |
| - done: true, |
1091 |
| - }; |
1092 |
| - |
1093 |
| - // Run loopback serializer, handles all test validation |
1094 |
| - let mut data = [0u8; 128]; |
1095 |
| - loopback_serializer(buffer, &mut data); |
1096 |
| - } |
1097 |
| - |
1098 |
| - /// Generates a three packet payload buffer |
1099 |
| - /// Serializes, deserializes, then checks if same as original |
1100 |
| - #[test] |
1101 |
| - fn three_packet_continued_payload_test() { |
1102 |
| - setup_logging_lite().ok(); |
1103 |
| - |
1104 |
| - // Create single byte payload buffer |
1105 |
| - let buffer = HidIoPacketBuffer::<U170> { |
1106 |
| - // Data packet |
1107 |
| - ptype: HidIoPacketType::Data, |
1108 |
| - // Test packet id |
1109 |
| - id: HidIoCommandID::TestPacket, |
1110 |
| - // Standard USB 2.0 FS packet length |
1111 |
| - max_len: 64, |
1112 |
| - // 170 bytes, 0xAC: 60, 60 then 50 (62, 62 then 52) |
1113 |
| - data: Vec::from_slice(&[0xAC; 170]).unwrap(), |
1114 |
| - // Ready to go |
1115 |
| - done: true, |
1116 |
| - }; |
1117 |
| - |
1118 |
| - // Run loopback serializer, handles all test validation |
1119 |
| - let mut data = [0u8; 200]; |
1120 |
| - loopback_serializer(buffer, &mut data); |
1121 |
| - } |
1122 |
| - |
1123 |
| - /// Tests hid_bitmask2vec and hid_vec2bitmask |
1124 |
| - #[test] |
1125 |
| - fn hid_vec2bitmask2vec_test() { |
1126 |
| - setup_logging_lite().ok(); |
1127 |
| - |
1128 |
| - let inputvec: Vec<u8, U7> = Vec::from_slice(&[1, 2, 3, 4, 5, 100, 255]).unwrap(); |
1129 |
| - |
1130 |
| - // Convert, then convert back |
1131 |
| - let bitmask = match hid_vec2bitmask(&inputvec) { |
1132 |
| - Ok(bitmask) => bitmask, |
1133 |
| - Err(e) => { |
1134 |
| - assert!(false, "Failed to run hid_vec2bitmask: {:?}", e); |
1135 |
| - Vec::new() |
1136 |
| - } |
1137 |
| - }; |
1138 |
| - let new_vec = match hid_bitmask2vec(&bitmask) { |
1139 |
| - Ok(new_vec) => new_vec, |
1140 |
| - Err(e) => { |
1141 |
| - assert!(false, "Failed to run hid_bitmask2vec: {:?}", e); |
1142 |
| - Vec::new() |
1143 |
| - } |
1144 |
| - }; |
1145 |
| - |
1146 |
| - // Compare with original |
1147 |
| - assert_eq!( |
1148 |
| - inputvec, new_vec, |
1149 |
| - "Bitmask test failed! Input: {:?}\nOutput: {:?}", |
1150 |
| - inputvec, new_vec, |
1151 |
| - ); |
1152 |
| - } |
1153 |
| -} |
0 commit comments