|
| 1 | +use uefi::prelude::BootServices; |
| 2 | +use uefi::proto::network::snp::SimpleNetwork; |
| 3 | +use uefi::proto::network::MacAddress; |
| 4 | + |
| 5 | + |
| 6 | +pub fn test(bt: &BootServices) { |
| 7 | + info!("Testing the simple network protocol"); |
| 8 | + |
| 9 | + let handles = bt |
| 10 | + .find_handles::<SimpleNetwork>() |
| 11 | + .expect("Failed to get handles for `SimpleNetwork` protocol"); |
| 12 | + |
| 13 | + for handle in handles { |
| 14 | + |
| 15 | + let simple_network = bt.open_protocol_exclusive::<SimpleNetwork>(handle); |
| 16 | + if simple_network.is_err() { continue; } |
| 17 | + let simple_network = simple_network.unwrap(); |
| 18 | + |
| 19 | + // Check shutdown |
| 20 | + simple_network.shutdown().expect("Failed to shutdown Simple Network"); |
| 21 | + |
| 22 | + // Check stop |
| 23 | + simple_network.stop().expect("Failed to stop Simple Network"); |
| 24 | + |
| 25 | + // Check start |
| 26 | + simple_network.start().expect("Failed to start Simple Network"); |
| 27 | + |
| 28 | + // Check initialize |
| 29 | + simple_network.initialize(None, None) |
| 30 | + .expect("Failed to initialize Simple Network"); |
| 31 | + |
| 32 | + simple_network.reset_statistics().unwrap(); |
| 33 | + |
| 34 | + // Reading the interrupt status clears it |
| 35 | + simple_network.get_interrupt_status().unwrap(); |
| 36 | + |
| 37 | + // Set receive filters |
| 38 | + simple_network.receive_filters(0x01 | 0x02 | 0x04 | 0x08 | 0x10, 0, false, None, None) |
| 39 | + .expect("Failed to set receive filters"); |
| 40 | + |
| 41 | + // Check media |
| 42 | + if !simple_network.mode().media_present_supported || !simple_network.mode().media_present { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + let payload = &[0u8; 46]; |
| 47 | + |
| 48 | + let dest_addr = MacAddress([0xffu8;32]); |
| 49 | + assert!(!simple_network.get_interrupt_status().unwrap().transmit_interrupt()); |
| 50 | + // Send the frame |
| 51 | + simple_network.transmit( |
| 52 | + simple_network.mode().media_header_size as usize, |
| 53 | + payload, |
| 54 | + None, |
| 55 | + Some(&dest_addr), |
| 56 | + Some(&0x0800), |
| 57 | + ) |
| 58 | + .expect("Failed to transmit frame"); |
| 59 | + |
| 60 | + info!("Waiting for the transmit"); |
| 61 | + while !simple_network.get_interrupt_status().unwrap().transmit_interrupt() {} |
| 62 | + |
| 63 | + // Attempt to receive a frame |
| 64 | + let mut buffer = [0u8; 1500]; |
| 65 | + |
| 66 | + let mut count = 0; |
| 67 | + |
| 68 | + info!("Waiting for the reception"); |
| 69 | + while count < 1_000 { |
| 70 | + let result = simple_network.receive( |
| 71 | + &mut buffer, |
| 72 | + None, |
| 73 | + None, |
| 74 | + None, |
| 75 | + None |
| 76 | + ); |
| 77 | + if result.is_ok() { break; } |
| 78 | + count += 1; |
| 79 | + } |
| 80 | + |
| 81 | + // Get stats |
| 82 | + let stats = simple_network.collect_statistics().expect("Failed to collect statistics"); |
| 83 | + info!("Stats: {:?}", stats); |
| 84 | + |
| 85 | + // One frame should have been transmitted and one received |
| 86 | + assert_eq!(stats.tx_total_frames().unwrap(), 1); |
| 87 | + assert_eq!(stats.rx_total_frames().unwrap(), 1); |
| 88 | + } |
| 89 | +} |
0 commit comments