3535
3636#include < dfu/dfu_target.h>
3737
38- #ifdef CONFIG_SUIT
38+ #ifdef CONFIG_DFU_TARGET_SUIT
3939#include < dfu/dfu_target_suit.h>
4040#else
41- #include < dfu/dfu_multi_image.h>
4241#include < dfu/dfu_target_mcuboot.h>
4342#include < zephyr/dfu/mcuboot.h>
4443#endif
44+ #include < dfu/dfu_multi_image.h>
4545
4646#include < zephyr/logging/log.h>
4747#include < zephyr/pm/device.h>
@@ -96,21 +96,32 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownloadImpl()
9696{
9797 mHeaderParser .Init ();
9898 mParams = {};
99- #ifndef CONFIG_SUIT
99+ #ifdef CONFIG_DFU_TARGET_SUIT
100+ ReturnErrorOnFailure (System::MapErrorZephyr (dfu_target_suit_set_buf (mBuffer , sizeof (mBuffer ))));
101+ #else
100102 ReturnErrorOnFailure (System::MapErrorZephyr (dfu_target_mcuboot_set_buf (mBuffer , sizeof (mBuffer ))));
103+ #endif // CONFIG_DFU_TARGET_SUIT
101104 ReturnErrorOnFailure (System::MapErrorZephyr (dfu_multi_image_init (mBuffer , sizeof (mBuffer ))));
102105
103106 for (int image_id = 0 ; image_id < CONFIG_UPDATEABLE_IMAGE_NUMBER; ++image_id)
104107 {
105108 dfu_image_writer writer;
109+
110+ #ifdef CONFIG_DFU_TARGET_SUIT
111+ // The first image is the SUIT manifest and must be placed in id=0, while all other images must be placed in id + 1,
112+ // because id=1 is dedicated for internal DFU purposes when the SUIT manifest contains the firmware.
113+ // In our case, we use cache processing, so we need to put firmware images starting from id=2.
114+ writer.image_id = image_id == 0 ? image_id : image_id + 1 ;
115+ writer.open = [](int id, size_t size) { return dfu_target_init (DFU_TARGET_IMAGE_TYPE_SUIT, id, size, nullptr ); };
116+ #else
106117 writer.image_id = image_id;
107118 writer.open = [](int id, size_t size) { return dfu_target_init (DFU_TARGET_IMAGE_TYPE_MCUBOOT, id, size, nullptr ); };
108- writer.write = [](const uint8_t * chunk, size_t chunk_size) { return dfu_target_write (chunk, chunk_size); };
109- writer.close = [](bool success) { return success ? dfu_target_done (success) : dfu_target_reset (); };
119+ #endif
120+ writer.write = [](const uint8_t * chunk, size_t chunk_size) { return dfu_target_write (chunk, chunk_size); };
121+ writer.close = [](bool success) { return success ? dfu_target_done (success) : dfu_target_reset (); };
110122
111123 ReturnErrorOnFailure (System::MapErrorZephyr (dfu_multi_image_register_writer (&writer)));
112124 };
113- #endif
114125
115126#ifdef CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE
116127 dfu_image_writer cdWriter;
@@ -137,24 +148,14 @@ CHIP_ERROR OTAImageProcessorImpl::Finalize()
137148 PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadComplete );
138149 DFUSync::GetInstance ().Free (mDfuSyncMutexId );
139150
140- #ifdef CONFIG_SUIT
141- mDfuTargetSuitInitialized = false ;
142- return System::MapErrorZephyr (dfu_target_done (true ));
143- #else
144151 return System::MapErrorZephyr (dfu_multi_image_done (true ));
145- #endif
146152}
147153
148154CHIP_ERROR OTAImageProcessorImpl::Abort ()
149155{
150156 CHIP_ERROR error;
151157
152- #ifdef CONFIG_SUIT
153- error = System::MapErrorZephyr (dfu_target_reset ());
154- mDfuTargetSuitInitialized = false ;
155- #else
156158 error = System::MapErrorZephyr (dfu_multi_image_done (false ));
157- #endif
158159
159160 DFUSync::GetInstance ().Free (mDfuSyncMutexId );
160161 TriggerFlashAction (ExternalFlashManager::Action::SLEEP);
@@ -167,10 +168,6 @@ CHIP_ERROR OTAImageProcessorImpl::Apply()
167168{
168169 PostOTAStateChangeEvent (DeviceLayer::kOtaApplyInProgress );
169170
170- #ifdef CONFIG_SUIT
171- mDfuTargetSuitInitialized = false ;
172- #endif
173-
174171 // Schedule update of all images
175172 int err = dfu_target_schedule_update (-1 );
176173
@@ -184,7 +181,11 @@ CHIP_ERROR OTAImageProcessorImpl::Apply()
184181 [](System::Layer *, void * /* context */ ) {
185182 PlatformMgr ().HandleServerShuttingDown ();
186183 k_msleep (CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS);
184+ #ifdef CONFIG_DFU_TARGET_SUIT
185+ dfu_target_suit_reboot ();
186+ #else
187187 Reboot (SoftwareRebootReason::kSoftwareUpdate );
188+ #endif
188189 },
189190 nullptr /* context */ );
190191 }
@@ -204,16 +205,6 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock)
204205
205206 CHIP_ERROR error = ProcessHeader (aBlock);
206207
207- #ifdef CONFIG_SUIT
208- if (!mDfuTargetSuitInitialized && error == CHIP_NO_ERROR)
209- {
210- ReturnErrorOnFailure (System::MapErrorZephyr (dfu_target_suit_set_buf (mBuffer , sizeof (mBuffer ))));
211- ReturnErrorOnFailure (System::MapErrorZephyr (
212- dfu_target_init (DFU_TARGET_IMAGE_TYPE_SUIT, 0 , static_cast <size_t >(mParams .totalFileBytes ), nullptr )));
213- mDfuTargetSuitInitialized = true ;
214- }
215- #endif
216-
217208 if (error == CHIP_NO_ERROR)
218209 {
219210 // DFU target library buffers data internally, so do not clone the block data.
@@ -223,12 +214,8 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock)
223214 }
224215 else
225216 {
226- #ifdef CONFIG_SUIT
227- int err = dfu_target_write (aBlock.data (), aBlock.size ());
228- #else
229217 int err = dfu_multi_image_write (static_cast <size_t >(mParams .downloadedBytes ), aBlock.data (), aBlock.size ());
230218 mParams .downloadedBytes += aBlock.size ();
231- #endif
232219 error = System::MapErrorZephyr (err);
233220 }
234221 }
0 commit comments