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