Skip to content

Commit 92e6fc6

Browse files
committed
refactor
1 parent bf8ef51 commit 92e6fc6

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

esp-bootloader-esp-idf/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Changed
1515

1616
- `OtaUpdater::with_ota` is now public (#4413)
17+
- `Ota` now takes `FlashRegion` by value (#4413)
1718

1819
### Fixed
1920

esp-bootloader-esp-idf/src/ota.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub struct Ota<'a, F>
149149
where
150150
F: embedded_storage::Storage,
151151
{
152-
flash: &'a mut FlashRegion<'a, F>,
152+
flash: FlashRegion<'a, F>,
153153
ota_partition_count: usize,
154154
}
155155

@@ -165,10 +165,7 @@ where
165165
/// doesn't represent a Data/Ota partition or the size is unexpected.
166166
///
167167
/// [Error::InvalidArgument] if the `ota_partition_count` exceeds the maximum or if it's 0.
168-
pub fn new(
169-
flash: &'a mut FlashRegion<'a, F>,
170-
ota_partition_count: usize,
171-
) -> Result<Ota<'a, F>, Error> {
168+
pub fn new(flash: FlashRegion<'a, F>, ota_partition_count: usize) -> Result<Ota<'a, F>, Error> {
172169
if ota_partition_count == 0 || ota_partition_count > 16 {
173170
return Err(Error::InvalidArgument);
174171
}
@@ -432,12 +429,12 @@ mod tests {
432429
data: [0xff; 0x2000],
433430
};
434431

435-
let mut mock_region = FlashRegion {
432+
let mock_region = FlashRegion {
436433
raw: mock_entry,
437434
flash: &mut mock_flash,
438435
};
439436

440-
let mut sut = Ota::new(&mut mock_region, 2).unwrap();
437+
let mut sut = Ota::new(mock_region, 2).unwrap();
441438
assert_eq!(
442439
sut.current_app_partition().unwrap(),
443440
AppPartitionSubType::Factory
@@ -482,12 +479,12 @@ mod tests {
482479
mock_flash.data[0x0000..][..0x20].copy_from_slice(SLOT_COUNT_1_VALID);
483480
mock_flash.data[0x1000..][..0x20].copy_from_slice(SLOT_INITIAL);
484481

485-
let mut mock_region = FlashRegion {
482+
let mock_region = FlashRegion {
486483
raw: mock_entry,
487484
flash: &mut mock_flash,
488485
};
489486

490-
let mut sut = Ota::new(&mut mock_region, 2).unwrap();
487+
let mut sut = Ota::new(mock_region, 2).unwrap();
491488
assert_eq!(
492489
sut.current_app_partition().unwrap(),
493490
AppPartitionSubType::Ota0
@@ -522,12 +519,12 @@ mod tests {
522519
mock_flash.data[0x0000..][..0x20].copy_from_slice(SLOT_COUNT_1_VALID);
523520
mock_flash.data[0x1000..][..0x20].copy_from_slice(SLOT_COUNT_2_NEW);
524521

525-
let mut mock_region = FlashRegion {
522+
let mock_region = FlashRegion {
526523
raw: mock_entry,
527524
flash: &mut mock_flash,
528525
};
529526

530-
let mut sut = Ota::new(&mut mock_region, 2).unwrap();
527+
let mut sut = Ota::new(mock_region, 2).unwrap();
531528
assert_eq!(
532529
sut.current_app_partition().unwrap(),
533530
AppPartitionSubType::Ota1
@@ -560,12 +557,12 @@ mod tests {
560557
data: [0xff; 0x2000],
561558
};
562559

563-
let mut mock_region = FlashRegion {
560+
let mock_region = FlashRegion {
564561
raw: mock_entry,
565562
flash: &mut mock_flash,
566563
};
567564

568-
let mut sut = Ota::new(&mut mock_region, 2).unwrap();
565+
let mut sut = Ota::new(mock_region, 2).unwrap();
569566
assert_eq!(
570567
sut.current_app_partition().unwrap(),
571568
AppPartitionSubType::Factory
@@ -626,12 +623,12 @@ mod tests {
626623
data: [0xff; 0x2000],
627624
};
628625

629-
let mut mock_region = FlashRegion {
626+
let mock_region = FlashRegion {
630627
raw: mock_entry,
631628
flash: &mut mock_flash,
632629
};
633630

634-
let mut sut = Ota::new(&mut mock_region, 4).unwrap();
631+
let mut sut = Ota::new(mock_region, 4).unwrap();
635632
assert_eq!(
636633
sut.current_app_partition().unwrap(),
637634
AppPartitionSubType::Factory
@@ -711,12 +708,12 @@ mod tests {
711708
data: [0xff; 0x2000],
712709
};
713710

714-
let mut mock_region = FlashRegion {
711+
let mock_region = FlashRegion {
715712
raw: mock_entry,
716713
flash: &mut mock_flash,
717714
};
718715

719-
let mut sut = Ota::new(&mut mock_region, 16).unwrap();
716+
let mut sut = Ota::new(mock_region, 16).unwrap();
720717
assert_eq!(
721718
sut.current_app_partition().unwrap(),
722719
AppPartitionSubType::Factory

esp-bootloader-esp-idf/src/ota_updater.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ where
6868
})
6969
}
7070

71-
/// Helper function to access the OTA data partition.
71+
/// Returns an [`Ota`] for accessing the OTA-data partition.
7272
///
7373
/// # Errors
7474
/// [Error::Invalid] if no OTA data partition was found.
75-
pub fn with_ota<R>(
76-
&mut self,
77-
f: impl FnOnce(crate::ota::Ota<'_, F>) -> Result<R, Error>,
78-
) -> Result<R, Error> {
75+
pub fn ota_data(&mut self) -> Result<crate::ota::Ota<'_, F>, Error> {
7976
let ota_part = self
8077
.pt
8178
.find_partition(crate::partitions::PartitionType::Data(
8279
crate::partitions::DataPartitionSubType::Ota,
8380
))?;
8481
if let Some(ota_part) = ota_part {
85-
let mut ota_part = ota_part.as_embedded_storage(self.flash);
86-
let ota = crate::ota::Ota::new(&mut ota_part, self.ota_count)?;
87-
f(ota)
82+
let ota_part = ota_part.as_embedded_storage(self.flash);
83+
let ota = crate::ota::Ota::new(ota_part, self.ota_count)?;
84+
Ok(ota)
8885
} else {
8986
Err(Error::Invalid)
9087
}
@@ -118,23 +115,23 @@ where
118115

119116
/// Returns the currently selected app partition.
120117
pub fn selected_partition(&mut self) -> Result<crate::partitions::AppPartitionSubType, Error> {
121-
self.with_ota(|mut ota| ota.current_app_partition())
118+
self.ota_data()?.current_app_partition()
122119
}
123120

124121
/// Get the [OtaImageState] of the currently selected partition.
125122
///
126123
/// # Errors
127124
/// A [Error::InvalidState] if no partition is currently selected.
128125
pub fn current_ota_state(&mut self) -> Result<OtaImageState, Error> {
129-
self.with_ota(|mut ota| ota.current_ota_state())
126+
self.ota_data()?.current_ota_state()
130127
}
131128

132129
/// Set the [OtaImageState] of the currently selected slot.
133130
///
134131
/// # Errors
135132
/// A [Error::InvalidState] if no partition is currently selected.
136133
pub fn set_current_ota_state(&mut self, state: OtaImageState) -> Result<(), Error> {
137-
self.with_ota(|mut ota| ota.set_current_ota_state(state))
134+
self.ota_data()?.set_current_ota_state(state)
138135
}
139136

140137
/// Selects the next active OTA-slot as current.
@@ -143,7 +140,7 @@ where
143140
/// activated partition.
144141
pub fn activate_next_partition(&mut self) -> Result<(), Error> {
145142
let next_slot = self.next_ota_part()?;
146-
self.with_ota(|mut ota| ota.set_current_app_partition(next_slot))
143+
self.ota_data()?.set_current_app_partition(next_slot)
147144
}
148145

149146
/// Returns a [FlashRegion] along with the [AppPartitionSubType] for the

0 commit comments

Comments
 (0)