1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 2023-2023. 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 #ifndef UPDATE_ENGINE_CONTROLLER_H 16 #define UPDATE_ENGINE_CONTROLLER_H 17 #include <memory> 18 #include <string> 19 #include <atomic> 20 #include "intell_voice_generic_factory.h" 21 #include "timer_mgr.h" 22 #include "update_state.h" 23 #include "update_strategy.h" 24 25 namespace OHOS { 26 namespace IntellVoiceEngine { 27 class UpdateEngineController : public OHOS::IntellVoiceUtils::ITimerObserver, 28 private OHOS::IntellVoiceUtils::TimerMgr { 29 public: 30 virtual ~UpdateEngineController(); 31 UpdateEngineController(); 32 33 virtual bool CreateUpdateEngine(const std::string ¶m, bool reEnroll = false) 34 { 35 return false; 36 } ReleaseUpdateEngine()37 virtual void ReleaseUpdateEngine() {}; 38 int CreateUpdateEngineUntilTime(std::shared_ptr<IUpdateStrategy> updateStrategy, bool reEnroll = false); 39 GetUpdateState()40 static bool GetUpdateState() 41 { 42 return isUpdating_.load(); 43 } 44 45 protected: 46 void UpdateCompleteProc(UpdateState result, const std::string ¶m, bool &isLast); 47 bool UpdateRetryProc(); 48 void ForceRelease(); 49 50 private: 51 52 void OnTimerEvent(OHOS::IntellVoiceUtils::TimerEvent &info) override; 53 bool StartUpdateTimer(); 54 void StopUpdateTimer(); 55 bool IsNeedRetryUpdate(); SetUpdateState(bool state)56 static void SetUpdateState(bool state) 57 { 58 isUpdating_.store(state); 59 } 60 void ClearRetryState(void); 61 int UpdateArbitration(int priority); 62 63 private: 64 static std::atomic<bool> isUpdating_; 65 int timerId_ = OHOS::IntellVoiceUtils::INVALID_ID; 66 int retryTimes_ = 0; 67 int retryTimesLimit_ = 0; 68 int delaySecond_ = UPDATE_DELAY_TIME_SECONDS; 69 std::mutex updateEngineMutex_; 70 UpdateState updateResult_ = UpdateState::UPDATE_STATE_DEFAULT; 71 std::shared_ptr<IUpdateStrategy> updateStrategy_ = nullptr; 72 int curPriority_ = UPDATE_PRIORITY_DEFAULT; 73 bool isForceReleased_ = false; 74 }; 75 } 76 } 77 #endif 78