fn verify_device(
    device_id: u64,
    file_path: &std::path::Path,
    message_sender: &std::sync::mpsc::Sender<Message>,
) -> Result<()> {
    // Set the step so UI knows it
    message_sender
        .send(Message::DeviceUpdateStep(DeviceUpdateStep::Verify))
        .ok();

    // Find the device by its id and open it
    let mut device = dfudev::DfuDevice::find_by_id(device_id)?.unwrap();
    device.open()?;

    // Make sure device is in idle state before operations start
    device.abort_request()?;

    // Make sure status is OK
    while let Ok(status) = device.getstatus_request() {
        if status.bStatus == dfudev::DeviceStatusCode::OK {
            break;
        } else {
            device.clrstatus_request()?;
        }
    }

    let mut file = dfufile::DfuFile::open(file_path)?;

    match &file.content {
        dfufile::Content::Plain => {
            return Err(Box::new(Error::PlainDfuNotSupported));
        }
        dfufile::Content::DfuSe(content) => {
            let num_images = content.images.len();

            for (image_no, image) in content.images.iter().enumerate() {
                let alt_setting = image.target_prefix.bAlternateSetting;
                let target = device
                    .info
                    .alt_settings
                    .iter()
                    .find(|&alt| alt.0 == alt_setting);

                if let Some(target) = target {
                    let memory_segment = dfudev::dfuse::MemorySegment::from_string_desc(&target.1);
                    let transfer_size = memory_segment
                        .regions
                        .iter()
                        .min_by_key(|x| x.sector_size)
                        .unwrap()
                        .sector_size;
                    let transfer_size =
                        std::cmp::min(transfer_size as u16, device.info.dfu_transfer_size);
                    log::debug!(
                        "Found target \"{}\" for alt setting {}. Transfer size is {} bytes",
                        memory_segment.name,
                        target.0,
                        transfer_size
                    );

                    let num_elements = image.image_elements.len();

                    for (element_no, element) in image.image_elements.iter().enumerate() {
                        log::debug!(
                            "Reading element at address 0x{:08X}, size {}",
                            element.dwElementAddress,
                            element.dwElementSize
                        );
                        let start_address = element.dwElementAddress;
                        let end_address = start_address + element.dwElementSize;
                        let mut read_address = start_address;

                        dfudev::dfuse::set_address(&device, read_address)?;

                        let mut block_no = 0;
                        let num_blocks = (end_address - start_address) / transfer_size as u32;

                        while read_address < end_address {
                            let chunk_size =
                                std::cmp::min(transfer_size as u32, end_address - read_address);

                            let mut device_data = vec![0; chunk_size as usize];
                            device.upload_request(block_no + 2, &mut device_data)?;

                            let mut file_data = vec![0; chunk_size as usize];
                            element.read_at(
                                &mut file.file,
                                read_address - start_address,
                                &mut file_data,
                            )?;

                            if device_data != file_data {
                                return Err(Box::new(Error::VerificationFailed(read_address)));
                            }

                            let progress = (block_no as f32) / (num_blocks as f32)
                                * ((image_no + 1) as f32)
                                / (num_images as f32)
                                * ((element_no + 1) as f32)
                                / (num_elements as f32);
                            message_sender
                                .send(Message::DeviceVerifyProgress(progress))
                                .ok();

                            read_address += chunk_size;
                            block_no += 1;
                        }
                    }
                } else {
                    return Err(Box::new(Error::TargetNotFound(alt_setting)));
                }
            }
        }
    }

    // Final cleanup
    device.abort_request()?;
    device.close();

    Ok(())
}
/// Verifys the data in the device.
Verifys the data in the device.
Verifys the data in the device .
fn verify_device(
    device_id: u64,
    file_path: &std::path::Path,
    message_sender: &std::sync::mpsc::Sender<Message>,
) -> Result<()> {
    // Set the step so UI knows it
    message_sender
        .send(Message::DeviceUpdateStep(DeviceUpdateStep::Verify))
        .ok();
    // Find the device by its id and open it
    let mut device = dfudev::DfuDevice::find_by_id(device_id)?.unwrap();
    device.open()?;
    // Make sure device is in idle state before operations start
    device.abort_request()?;
    // Make sure status is OK
    while let Ok(status) = device.getstatus_request() {
        if status.bStatus == dfudev::DeviceStatusCode::OK {
            break;
        } else {
            device.clrstatus_request()?;
        }
    }
    let mut file = dfufile::DfuFile::open(file_path)?;
    match &file.content {
        dfufile::Content::Plain => {
            return Err(Box::new(Error::PlainDfuNotSupported));
        }
        dfufile::Content::DfuSe(content) => {
            let num_images = content.images.len();
            for (image_no, image) in content.images.iter().enumerate() {
                let alt_setting = image.target_prefix.bAlternateSetting;
                let target = device
                    .info
                    .alt_settings
                    .iter()
                    .find(|&alt| alt.0 == alt_setting);
                if let Some(target) = target {
                    let memory_segment = dfudev::dfuse::MemorySegment::from_string_desc(&target.1);
                    let transfer_size = memory_segment
                        .regions
                        .iter()
                        .min_by_key(|x| x.sector_size)
                        .unwrap()
                        .sector_size;
                    let transfer_size =
                        std::cmp::min(transfer_size as u16, device.info.dfu_transfer_size);
                    log::debug!(
                        "Found target \"{}\" for alt setting {}. Transfer size is {} bytes",
                        memory_segment.name,
                        target.0,
                        transfer_size
                    );
                    let num_elements = image.image_elements.len();
                    for (element_no, element) in image.image_elements.iter().enumerate() {
                        log::debug!(
                            "Reading element at address 0x{:08X}, size {}",
                            element.dwElementAddress,
                            element.dwElementSize
                        );
                        let start_address = element.dwElementAddress;
                        let end_address = start_address + element.dwElementSize;
                        let mut read_address = start_address;
                        dfudev::dfuse::set_address(&device, read_address)?;
                        let mut block_no = 0;
                        let num_blocks = (end_address - start_address) / transfer_size as u32;
                        while read_address < end_address {
                            let chunk_size =
                                std::cmp::min(transfer_size as u32, end_address - read_address);
                            let mut device_data = vec![0; chunk_size as usize];
                            device.upload_request(block_no + 2, &mut device_data)?;
                            let mut file_data = vec![0; chunk_size as usize];
                            element.read_at(
                                &mut file.file,
                                read_address - start_address,
                                &mut file_data,
                            )?;
                            if device_data != file_data {
                                return Err(Box::new(Error::VerificationFailed(read_address)));
                            }
                            let progress = (block_no as f32) / (num_blocks as f32)
                                * ((image_no + 1) as f32)
                                / (num_images as f32)
                                * ((element_no + 1) as f32)
                                / (num_elements as f32);
                            message_sender
                                .send(Message::DeviceVerifyProgress(progress))
                                .ok();
                            read_address += chunk_size;
                            block_no += 1;
                        }
                    }
                } else {
                    return Err(Box::new(Error::TargetNotFound(alt_setting)));
                }
            }
        }
    }
    // Final cleanup
    device.abort_request()?;
    device.close();
    Ok(())
}
fn verify_device ( device_id : u64 , file_path : & std :: path :: Path , message_sender : & std :: sync :: mpsc :: Sender < Message > , ) -> Result < ( ) > { // Set the step so UI knows it message_sender . send ( Message :: DeviceUpdateStep ( DeviceUpdateStep :: Verify ) ) . ok ( ) ; // Find the device by its id and open it let mut device = dfudev :: DfuDevice :: find_by_id ( device_id ) ? . unwrap ( ) ; device . open ( ) ? ; // Make sure device is in idle state before operations start device . abort_request ( ) ? ; // Make sure status is OK while let Ok ( status ) = device . getstatus_request ( ) { if status . bStatus == dfudev :: DeviceStatusCode :: OK { break ; } else { device . clrstatus_request ( ) ? ; } } let mut file = dfufile :: DfuFile :: open ( file_path ) ? ; match & file . content { dfufile :: Content :: Plain => { return Err ( Box :: new ( Error :: PlainDfuNotSupported ) ) ; } dfufile :: Content :: DfuSe ( content ) => { let num_images = content . images . len ( ) ; for ( image_no , image ) in content . images . iter ( ) . enumerate ( ) { let alt_setting = image . target_prefix . bAlternateSetting ; let target = device . info . alt_settings . iter ( ) . find ( | & alt | alt . 0 == alt_setting ) ; if let Some ( target ) = target { let memory_segment = dfudev :: dfuse :: MemorySegment :: from_string_desc ( & target . 1 ) ; let transfer_size = memory_segment . regions . iter ( ) . min_by_key ( | x | x . sector_size ) . unwrap ( ) . sector_size ; let transfer_size = std :: cmp :: min ( transfer_size as u16 , device . info . dfu_transfer_size ) ; log :: debug ! ( " \" \" " memory_segment name target 0 transfer_size ) ; let num_elements = image . image_elements . len ( ) ; for ( element_no , element ) in image . image_elements . iter ( ) . enumerate ( ) { log :: debug ! ( " " element dwElementAddress element dwElementSize ) ; let start_address = element . dwElementAddress ; let end_address = start_address + element . dwElementSize ; let mut read_address = start_address ; dfudev :: dfuse :: set_address ( & device , read_address ) ? ; let mut block_no = 0 ; let num_blocks = ( end_address - start_address ) / transfer_size as u32 ; while read_address < end_address { let chunk_size = std :: cmp :: min ( transfer_size as u32 , end_address - read_address ) ; let mut device_data = vec ! [ 0 chunk_size as usize ] ; device . upload_request ( block_no + 2 , & mut device_data ) ? ; let mut file_data = vec ! [ 0 chunk_size as usize ] ; element . read_at ( & mut file . file , read_address - start_address , & mut file_data , ) ? ; if device_data != file_data { return Err ( Box :: new ( Error :: VerificationFailed ( read_address ) ) ) ; } let progress = ( block_no as f32 ) / ( num_blocks as f32 ) * ( ( image_no + 1 ) as f32 ) / ( num_images as f32 ) * ( ( element_no + 1 ) as f32 ) / ( num_elements as f32 ) ; message_sender . send ( Message :: DeviceVerifyProgress ( progress ) ) . ok ( ) ; read_address += chunk_size ; block_no += 1 ; } } } else { return Err ( Box :: new ( Error :: TargetNotFound ( alt_setting ) ) ) ; } } } } // Final cleanup device . abort_request ( ) ? ; device . close ( ) ; Ok ( ( ) ) }
Verifys the data in the device.
Verifys the data in the device .
device_id
u64
file_path
&std::path::Path
message_sender
&std::sync::mpsc::Sender<Message>