1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "update_engine_controller.h"
16 #include <fstream>
17 #include "securec.h"
18 #include "intell_voice_log.h"
19
20 #include "update_adapter_listener.h"
21 #include "history_info_mgr.h"
22 #include "time_util.h"
23 #include "scope_guard.h"
24 #include "engine_callback_message.h"
25 #include "adapter_callback_service.h"
26 #include "string_util.h"
27 #include "intell_voice_definitions.h"
28
29 #define LOG_TAG "UpdateEngineController"
30
31 using namespace OHOS::IntellVoiceUtils;
32
33 namespace OHOS {
34 namespace IntellVoiceEngine {
35 static constexpr int32_t MS_PER_S = 1000;
36
37 std::atomic<bool> UpdateEngineController::isUpdating_ = false;
38
UpdateEngineController()39 UpdateEngineController::UpdateEngineController()
40 {
41 }
42
~UpdateEngineController()43 UpdateEngineController::~UpdateEngineController()
44 {
45 }
46
OnTimerEvent(TimerEvent & info)47 void UpdateEngineController::OnTimerEvent(TimerEvent &info)
48 {
49 INTELL_VOICE_LOG_INFO("TimerEvent %{public}d", timerId_);
50 EngineCallbackMessage::CallFunc(HANDLE_UPDATE_RETRY);
51 }
52
UpdateRetryProc()53 bool UpdateEngineController::UpdateRetryProc()
54 {
55 std::lock_guard<std::mutex> lock(updateEngineMutex_);
56 if (isForceReleased_) {
57 INTELL_VOICE_LOG_WARN("force to stop, no need to retry");
58 return true;
59 }
60 if (updateStrategy_ != nullptr && !updateStrategy_->UpdateRestrain()) {
61 if (CreateUpdateEngine(updateStrategy_->param_)) {
62 INTELL_VOICE_LOG_INFO("retry update, times %{public}d", retryTimes_);
63 timerId_ = INVALID_ID;
64 return true;
65 }
66 updateResult_ = UpdateState::UPDATE_STATE_COMPLETE_FAIL;
67 } else {
68 updateResult_ = UpdateState::UPDATE_STATE_COMPLETE_SUCCESS;
69 }
70
71 INTELL_VOICE_LOG_INFO("retry err, times %{public}d, result %{public}d", retryTimes_, updateResult_);
72 ClearRetryState();
73 ReleaseUpdateEngine();
74 TimerMgr::Stop();
75 return false;
76 }
77
UpdateArbitration(int priority)78 int UpdateEngineController::UpdateArbitration(int priority)
79 {
80 if (!GetUpdateState()) {
81 return 0;
82 }
83
84 if (curPriority_ > priority) {
85 INTELL_VOICE_LOG_INFO("cur priority is higher than new, can not create");
86 return -1;
87 }
88
89 if (curPriority_ == priority) {
90 INTELL_VOICE_LOG_INFO("same priority, retry update process");
91 return 0;
92 }
93
94 INTELL_VOICE_LOG_INFO("cur priority is lower than before, release old engine");
95 if (updateStrategy_ != nullptr) {
96 updateStrategy_->OnUpdateCompleteCallback(UPDATE_STATE_COMPLETE_FAIL, true);
97 }
98 ReleaseUpdateEngine();
99 ClearRetryState();
100 TimerMgr::Stop();
101 return 0;
102 }
103
CreateUpdateEngineUntilTime(std::shared_ptr<IUpdateStrategy> updateStrategy)104 int UpdateEngineController::CreateUpdateEngineUntilTime(std::shared_ptr<IUpdateStrategy> updateStrategy)
105 {
106 std::lock_guard<std::mutex> lock(updateEngineMutex_);
107 INTELL_VOICE_LOG_INFO("enter");
108 if (updateStrategy == nullptr) {
109 INTELL_VOICE_LOG_INFO("strategy is null");
110 return -1;
111 }
112
113 if (updateStrategy->UpdateRestrain()) {
114 INTELL_VOICE_LOG_INFO("update restrain, no need to update");
115 return -1;
116 }
117
118 if (UpdateArbitration(updateStrategy->GetUpdatePriority()) != 0) {
119 return -1;
120 }
121
122 if (!CreateUpdateEngine(updateStrategy->param_)) {
123 INTELL_VOICE_LOG_ERROR("create update engine error");
124 return -1;
125 }
126
127 retryTimesLimit_ = updateStrategy->GetRetryTimes();
128 if (retryTimes_ < retryTimesLimit_) {
129 TimerMgr::Start("UpdateThread", nullptr);
130 }
131
132 updateStrategy_ = updateStrategy;
133 curPriority_ = updateStrategy->GetUpdatePriority();
134 SetUpdateState(true);
135 isForceReleased_ = false;
136
137 INTELL_VOICE_LOG_INFO("create update engine success");
138 return 0;
139 }
140
StartUpdateTimer()141 bool UpdateEngineController::StartUpdateTimer()
142 {
143 if (timerId_ != INVALID_ID) {
144 INTELL_VOICE_LOG_INFO("timer already start %{public}d", timerId_);
145 return true;
146 }
147
148 timerId_ = SetTimer(0, delaySecond_ * MS_PER_S * US_PER_MS, 0, this);
149 if (timerId_ == INVALID_ID) {
150 INTELL_VOICE_LOG_ERROR("timerId %{public}d is invalid", timerId_);
151 return false;
152 }
153
154 retryTimes_++;
155 INTELL_VOICE_LOG_INFO("start update timer succeed");
156 return true;
157 }
158
StopUpdateTimer()159 void UpdateEngineController::StopUpdateTimer()
160 {
161 if (timerId_ == INVALID_ID) {
162 INTELL_VOICE_LOG_INFO("timer already stop");
163 return;
164 }
165
166 KillTimer(timerId_);
167 INTELL_VOICE_LOG_INFO("stop update timer");
168 }
169
IsNeedRetryUpdate()170 bool UpdateEngineController::IsNeedRetryUpdate()
171 {
172 if (updateResult_ == UpdateState::UPDATE_STATE_COMPLETE_SUCCESS) {
173 INTELL_VOICE_LOG_INFO("update success");
174 return false;
175 }
176
177 if (retryTimes_ >= retryTimesLimit_) {
178 INTELL_VOICE_LOG_INFO("retry times limit");
179 return false;
180 }
181
182 if ((updateStrategy_ == nullptr) || (updateStrategy_->UpdateRestrain())) {
183 INTELL_VOICE_LOG_INFO("no need to retry");
184 return false;
185 }
186
187 return true;
188 }
189
ClearRetryState()190 void UpdateEngineController::ClearRetryState()
191 {
192 retryTimes_ = 0;
193 updateResult_ = UpdateState::UPDATE_STATE_DEFAULT;
194 timerId_ = INVALID_ID;
195 updateStrategy_ = nullptr;
196 retryTimesLimit_ = 0;
197 curPriority_ = 0;
198 SetUpdateState(false);
199 }
200
UpdateCompleteProc(UpdateState result,const std::string & param,bool & isLast)201 void UpdateEngineController::UpdateCompleteProc(UpdateState result, const std::string ¶m, bool &isLast)
202 {
203 std::lock_guard<std::mutex> lock(updateEngineMutex_);
204 isLast = false;
205 if (isForceReleased_) {
206 HistoryInfoMgr::GetInstance().DeleteKey({ KEY_WAKEUP_VESRION });
207 INTELL_VOICE_LOG_WARN("already stop");
208 return;
209 }
210 if (updateStrategy_ == nullptr || param != updateStrategy_->param_) {
211 INTELL_VOICE_LOG_WARN("param is not equal");
212 return;
213 }
214 updateResult_ = result;
215 StopUpdateTimer();
216
217 if (IsNeedRetryUpdate() && StartUpdateTimer()) {
218 INTELL_VOICE_LOG_INFO("retry to update");
219 } else {
220 if (updateStrategy_ != nullptr) {
221 updateStrategy_->OnUpdateCompleteCallback(result != UpdateState::UPDATE_STATE_COMPLETE_SUCCESS, true);
222 }
223 ClearRetryState();
224 isLast = true;
225 TimerMgr::Stop();
226 }
227 ReleaseUpdateEngine();
228 }
229
ForceRelease()230 void UpdateEngineController::ForceRelease()
231 {
232 std::lock_guard<std::mutex> lock(updateEngineMutex_);
233 if (!GetUpdateState()) {
234 INTELL_VOICE_LOG_INFO("no need to stop update");
235 return;
236 }
237 ClearRetryState();
238 ReleaseUpdateEngine();
239 TimerMgr::Stop();
240 isForceReleased_ = true;
241 }
242 }
243 }