diff --git a/esp-bootloader-esp-idf/CHANGELOG.md b/esp-bootloader-esp-idf/CHANGELOG.md index 10c77edf992..719bdebcc26 100644 --- a/esp-bootloader-esp-idf/CHANGELOG.md +++ b/esp-bootloader-esp-idf/CHANGELOG.md @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `OtaUpdater::ota_data()` (#4413) ### Changed +- `Ota::new()` now takes `FlashRegion` by value (#4413) ### Fixed diff --git a/esp-bootloader-esp-idf/src/ota.rs b/esp-bootloader-esp-idf/src/ota.rs index 7d8465143f9..3adc181d36d 100644 --- a/esp-bootloader-esp-idf/src/ota.rs +++ b/esp-bootloader-esp-idf/src/ota.rs @@ -149,7 +149,7 @@ pub struct Ota<'a, F> where F: embedded_storage::Storage, { - flash: &'a mut FlashRegion<'a, F>, + flash: FlashRegion<'a, F>, ota_partition_count: usize, } @@ -165,10 +165,7 @@ where /// doesn't represent a Data/Ota partition or the size is unexpected. /// /// [Error::InvalidArgument] if the `ota_partition_count` exceeds the maximum or if it's 0. - pub fn new( - flash: &'a mut FlashRegion<'a, F>, - ota_partition_count: usize, - ) -> Result, Error> { + pub fn new(flash: FlashRegion<'a, F>, ota_partition_count: usize) -> Result, Error> { if ota_partition_count == 0 || ota_partition_count > 16 { return Err(Error::InvalidArgument); } @@ -432,12 +429,12 @@ mod tests { data: [0xff; 0x2000], }; - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 2).unwrap(); + let mut sut = Ota::new(mock_region, 2).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Factory @@ -482,12 +479,12 @@ mod tests { mock_flash.data[0x0000..][..0x20].copy_from_slice(SLOT_COUNT_1_VALID); mock_flash.data[0x1000..][..0x20].copy_from_slice(SLOT_INITIAL); - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 2).unwrap(); + let mut sut = Ota::new(mock_region, 2).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Ota0 @@ -522,12 +519,12 @@ mod tests { mock_flash.data[0x0000..][..0x20].copy_from_slice(SLOT_COUNT_1_VALID); mock_flash.data[0x1000..][..0x20].copy_from_slice(SLOT_COUNT_2_NEW); - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 2).unwrap(); + let mut sut = Ota::new(mock_region, 2).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Ota1 @@ -560,12 +557,12 @@ mod tests { data: [0xff; 0x2000], }; - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 2).unwrap(); + let mut sut = Ota::new(mock_region, 2).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Factory @@ -626,12 +623,12 @@ mod tests { data: [0xff; 0x2000], }; - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 4).unwrap(); + let mut sut = Ota::new(mock_region, 4).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Factory @@ -711,12 +708,12 @@ mod tests { data: [0xff; 0x2000], }; - let mut mock_region = FlashRegion { + let mock_region = FlashRegion { raw: mock_entry, flash: &mut mock_flash, }; - let mut sut = Ota::new(&mut mock_region, 16).unwrap(); + let mut sut = Ota::new(mock_region, 16).unwrap(); assert_eq!( sut.current_app_partition().unwrap(), AppPartitionSubType::Factory diff --git a/esp-bootloader-esp-idf/src/ota_updater.rs b/esp-bootloader-esp-idf/src/ota_updater.rs index 0ea4940648a..b564c6749b6 100644 --- a/esp-bootloader-esp-idf/src/ota_updater.rs +++ b/esp-bootloader-esp-idf/src/ota_updater.rs @@ -68,19 +68,20 @@ where }) } - fn with_ota( - &mut self, - f: impl FnOnce(crate::ota::Ota<'_, F>) -> Result, - ) -> Result { + /// Returns an [`Ota`] for accessing the OTA-data partition. + /// + /// # Errors + /// [Error::Invalid] if no OTA data partition was found. + pub fn ota_data(&mut self) -> Result, Error> { let ota_part = self .pt .find_partition(crate::partitions::PartitionType::Data( crate::partitions::DataPartitionSubType::Ota, ))?; if let Some(ota_part) = ota_part { - let mut ota_part = ota_part.as_embedded_storage(self.flash); - let ota = crate::ota::Ota::new(&mut ota_part, self.ota_count)?; - f(ota) + let ota_part = ota_part.as_embedded_storage(self.flash); + let ota = crate::ota::Ota::new(ota_part, self.ota_count)?; + Ok(ota) } else { Err(Error::Invalid) } @@ -114,7 +115,7 @@ where /// Returns the currently selected app partition. pub fn selected_partition(&mut self) -> Result { - self.with_ota(|mut ota| ota.current_app_partition()) + self.ota_data()?.current_app_partition() } /// Get the [OtaImageState] of the currently selected partition. @@ -122,7 +123,7 @@ where /// # Errors /// A [Error::InvalidState] if no partition is currently selected. pub fn current_ota_state(&mut self) -> Result { - self.with_ota(|mut ota| ota.current_ota_state()) + self.ota_data()?.current_ota_state() } /// Set the [OtaImageState] of the currently selected slot. @@ -130,7 +131,7 @@ where /// # Errors /// A [Error::InvalidState] if no partition is currently selected. pub fn set_current_ota_state(&mut self, state: OtaImageState) -> Result<(), Error> { - self.with_ota(|mut ota| ota.set_current_ota_state(state)) + self.ota_data()?.set_current_ota_state(state) } /// Selects the next active OTA-slot as current. @@ -139,7 +140,7 @@ where /// activated partition. pub fn activate_next_partition(&mut self) -> Result<(), Error> { let next_slot = self.next_ota_part()?; - self.with_ota(|mut ota| ota.set_current_app_partition(next_slot)) + self.ota_data()?.set_current_app_partition(next_slot) } /// Returns a [FlashRegion] along with the [AppPartitionSubType] for the