Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / GenericSoftwareUpdateManagerImpl.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
4  *    Copyright (c) 2019 Google LLC.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *          Provides an generic implementation of SoftwareUpdateManager features
22  *          for use on various platforms.
23  */
24
25 #pragma once
26
27 // #if CHIP_DEVICE_CONFIG_ENABLE_SOFTWARE_UPDATE_MANAGER
28
29 #include <platform/SoftwareUpdateManager.h>
30
31 #include <system/SystemPacketBuffer.h>
32
33 namespace chip {
34 namespace DeviceLayer {
35 namespace Internal {
36
37 using namespace chip::Inet;
38
39 /**
40  * Provides a generic implementation of Software Update Manager features that works on multiple platforms.
41  *
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
45  * parameter.
46  */
47 template <class ImplClass>
48 class GenericSoftwareUpdateManagerImpl
49 {
50
51 protected:
52     // ===== Methods that implement the SoftwareUpdateManager abstract interface.
53
54     bool _IsInProgress();
55     SoftwareUpdateManager::State _GetState();
56
57     void _SetRetryPolicyCallback(SoftwareUpdateManager::RetryPolicyCallback aRetryPolicyCallback);
58
59     static void _DefaultEventHandler(void * apAppState, SoftwareUpdateManager::EventType aEvent,
60                                      const SoftwareUpdateManager::InEventParam & aInParam,
61                                      SoftwareUpdateManager::OutEventParam & aOutParam);
62
63     CHIP_ERROR _Abort();
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);
69
70     // ===== Members for use by the implementation subclass.
71
72     void DoInit();
73     void DownloadComplete();
74     void SoftwareUpdateFinished(CHIP_ERROR aError);
75
76     CHIP_ERROR InstallImage();
77     CHIP_ERROR StoreImageBlock(uint32_t aLength, uint8_t * aData);
78
79 private:
80     // ===== Private members reserved for use by this class only.
81
82     void Cleanup();
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);
88     void SendQuery();
89     void StartImageInstall();
90     void PrepareImageStorage();
91
92     CHIP_ERROR PrepareQuery();
93
94     uint32_t GetNextWaitTimeInterval();
95     uint32_t ComputeNextScheduledWaitTimeInterval();
96
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);
102
103     SoftwareUpdateManager::State mState;
104
105     void * mAppState;
106
107     char mURI[CHIP_DEVICE_CONFIG_SOFTWARE_UPDATE_URI_LEN];
108
109     SoftwareUpdateManager::EventCallback mEventHandlerCallback;
110     SoftwareUpdateManager::RetryPolicyCallback mRetryPolicyCallback;
111
112     chip::System::PacketBuffer * mImageQueryPacketBuffer;
113
114     bool mScheduledCheckEnabled;
115     bool mShouldRetry;
116     bool mIgnorePartialImage;
117
118     uint64_t mNumBytesToDownload;
119     uint64_t mStartOffset;
120
121     uint32_t mMinWaitTimeMs;
122     uint32_t mMaxWaitTimeMs;
123     uint32_t mEventId;
124
125     uint16_t mRetryCounter;
126
127     ImplClass * Impl() { return static_cast<ImplClass *>(this); }
128 };
129
130 // Instruct the compiler to instantiate the template only when explicitly told to do so.
131 extern template class Internal::GenericSoftwareUpdateManagerImpl<SoftwareUpdateManagerImpl>;
132
133 } // namespace Internal
134 } // namespace DeviceLayer
135 } // namespace chip
136
137 // #endif // CHIP_DEVICE_CONFIG_ENABLE_SOFTWARE_UPDATE_MANAGER