3 * Copyright (c) 2020-2021 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Provides an generic implementation of SoftwareUpdateManager features
22 * for use on various platforms.
27 // #if CHIP_DEVICE_CONFIG_ENABLE_SOFTWARE_UPDATE_MANAGER
29 #include <platform/SoftwareUpdateManager.h>
31 #include <system/SystemPacketBuffer.h>
34 namespace DeviceLayer {
37 using namespace chip::Inet;
40 * Provides a generic implementation of Software Update Manager features that works on multiple platforms.
42 * This template contains implementations of select features from the SoftwareUpdateManager abstract
43 * interface that are suitable for use on all platforms. It is intended to be inherited (directly
44 * or indirectly) by the SoftwareUpdateManagerImpl class, which also appears as the template's ImplClass
47 template <class ImplClass>
48 class GenericSoftwareUpdateManagerImpl
52 // ===== Methods that implement the SoftwareUpdateManager abstract interface.
55 SoftwareUpdateManager::State _GetState();
57 void _SetRetryPolicyCallback(SoftwareUpdateManager::RetryPolicyCallback aRetryPolicyCallback);
59 static void _DefaultEventHandler(void * apAppState, SoftwareUpdateManager::EventType aEvent,
60 const SoftwareUpdateManager::InEventParam & aInParam,
61 SoftwareUpdateManager::OutEventParam & aOutParam);
64 CHIP_ERROR _CheckNow();
65 CHIP_ERROR _PrepareImageStorageComplete(CHIP_ERROR aError);
66 CHIP_ERROR _ImageInstallComplete(CHIP_ERROR aError);
67 CHIP_ERROR _SetQueryIntervalWindow(uint32_t aMinWaitTimeMs, uint32_t aMaxWaitTimeMs);
68 CHIP_ERROR _SetEventCallback(void * aAppState, SoftwareUpdateManager::EventCallback aEventCallback);
70 // ===== Members for use by the implementation subclass.
73 void DownloadComplete();
74 void SoftwareUpdateFinished(CHIP_ERROR aError);
76 CHIP_ERROR InstallImage();
77 CHIP_ERROR StoreImageBlock(uint32_t aLength, uint8_t * aData);
80 // ===== Private members reserved for use by this class only.
83 void CheckImageState();
84 void CheckImageIntegrity();
85 void DriveState(SoftwareUpdateManager::State aNextState);
86 void GetEventState(int32_t & aEventState);
87 void HandleImageQueryResponse(chip::System::PacketBuffer * aPayload);
89 void StartImageInstall();
90 void PrepareImageStorage();
92 CHIP_ERROR PrepareQuery();
94 uint32_t GetNextWaitTimeInterval();
95 uint32_t ComputeNextScheduledWaitTimeInterval();
97 static void PrepareBinding(intptr_t arg);
98 static void StartDownload(intptr_t arg);
99 static void HandleHoldOffTimerExpired(::chip::System::Layer * aLayer, void * aAppState, ::chip::System::Error aError);
100 static void DefaultRetryPolicyCallback(void * aAppState, SoftwareUpdateManager::RetryParam & aRetryParam,
101 uint32_t & aOutIntervalMsec);
103 SoftwareUpdateManager::State mState;
107 char mURI[CHIP_DEVICE_CONFIG_SOFTWARE_UPDATE_URI_LEN];
109 SoftwareUpdateManager::EventCallback mEventHandlerCallback;
110 SoftwareUpdateManager::RetryPolicyCallback mRetryPolicyCallback;
112 chip::System::PacketBuffer * mImageQueryPacketBuffer;
114 bool mScheduledCheckEnabled;
116 bool mIgnorePartialImage;
118 uint64_t mNumBytesToDownload;
119 uint64_t mStartOffset;
121 uint32_t mMinWaitTimeMs;
122 uint32_t mMaxWaitTimeMs;
125 uint16_t mRetryCounter;
127 ImplClass * Impl() { return static_cast<ImplClass *>(this); }
130 // Instruct the compiler to instantiate the template only when explicitly told to do so.
131 extern template class Internal::GenericSoftwareUpdateManagerImpl<SoftwareUpdateManagerImpl>;
133 } // namespace Internal
134 } // namespace DeviceLayer
137 // #endif // CHIP_DEVICE_CONFIG_ENABLE_SOFTWARE_UPDATE_MANAGER