• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SERVICE_MANAGER_H
16 #define SERVICE_MANAGER_H
17 #include <mutex>
18 #include <map>
19 #include <atomic>
20 #include "switch_observer.h"
21 #include "switch_provider.h"
22 #include "task_executor.h"
23 #include "intell_voice_definitions.h"
24 #include "intell_voice_engine_registrar.h"
25 #include "intell_voice_trigger_registrar.h"
26 #include "trigger_base_type.h"
27 #include "service_manager_type.h"
28 namespace OHOS {
29 namespace IntellVoiceEngine {
30 
31 template<typename T, typename E>
32 class IntellVoiceServiceManager : private OHOS::IntellVoiceUtils::TaskExecutor, private T, private E,
33     private IntellVoiceTriggerRegistrar, private IntellVoiceEngineRegistrar {
34 public:
35     ~IntellVoiceServiceManager() override;
GetInstance()36     static IntellVoiceServiceManager &GetInstance()
37     {
38         static IntellVoiceServiceManager instance;
39         return instance;
40     }
41 
42     void HandleUnloadIntellVoiceService(bool isAsync);
43     bool HandleOnIdle();
44     void HandlePowerSaveModeChange();
45     sptr<IIntellVoiceEngine> HandleCreateEngine(IntellVoiceEngineType type);
46     int32_t HandleReleaseEngine(IntellVoiceEngineType type) override;
47     void HandleSilenceUpdate();
48     void HandleWhisperVprUpdate();
49     int32_t HandleCloneUpdate(const std::string &wakeupInfo, const sptr<IRemoteObject> &object);
50     void HandleServiceStop();
51 
52     void HandleSwitchOn(bool isAsync, int32_t uuid, bool needUpdateAdapter);
53     void HandleSwitchOff(bool isAsync, int32_t uuid);
54 
55     bool RegisterProxyDeathRecipient(IntellVoiceEngineType type, const sptr<IRemoteObject> &object);
56     bool DeregisterProxyDeathRecipient(IntellVoiceEngineType type);
57     int32_t GetUploadFiles(int numMax, std::vector<UploadFilesFromHdi> &files);
58     int32_t EngineSetParameter(const std::string &keyValueList);
59     std::string EngineGetParameter(const std::string &key);
60     int32_t GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles);
61     int32_t GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer);
62     int32_t SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer);
63     void SetScreenOff(bool value);
64 
65     void ProcBreathModel();
66     void ProcSingleLevelModel();
67     void CreateSwitchProvider();
68     void ReleaseSwitchProvider();
69     bool StartDetection(int32_t uuid);
70     void StopDetection(int32_t uuid);
71     bool QuerySwitchStatus(const std::string &key) override;
72     int32_t ClearUserData();
73     void OnServiceStart(std::map<int32_t, std::function<void(bool)>> &saChangeFuncMap);
74     void OnServiceStop();
75 
76     using TaskExecutor::AddAsyncTask;
77     using TaskExecutor::AddSyncTask;
78     using TaskExecutor::StopThread;
79 
80 private:
81     void HandleCloseWakeupSource(bool isNeedStop = false) override;
82     void HandleClearWakeupEngineCb() override;
83     void HandleHeadsetHostDie() override;
84     void HandleUpdateComplete(int32_t result, const std::string &param) override;
85     void HandleUpdateRetry() override;
86     int32_t ReleaseEngine(IntellVoiceEngineType type) override;
87     std::string TriggerGetParameter(const std::string &key) override;
88     int32_t TriggerSetParameter(const std::string &key, const std::string &value) override;
89     void TriggerMgrUpdateModel(std::vector<uint8_t> buffer, int32_t uuid,
90         IntellVoiceTrigger::TriggerModelType type) override;
91 
92 private:
93     IntellVoiceServiceManager();
IsSwitchKeyValid(const std::string & key)94     static bool IsSwitchKeyValid(const std::string &key)
95     {
96         if ((key == WAKEUP_KEY) || (key == WHISPER_KEY) || (key == SHORTWORD_KEY)) {
97             return true;
98         }
99         return false;
100     }
101 
QuerySwitchByUuid(int32_t uuid)102     bool QuerySwitchByUuid(int32_t uuid)
103     {
104         if (uuid == VOICE_WAKEUP_MODEL_UUID) {
105             return QuerySwitchStatus(WAKEUP_KEY);
106         }
107 
108         if (uuid == PROXIMAL_WAKEUP_MODEL_UUID) {
109             return QuerySwitchStatus(WHISPER_KEY);
110         }
111 
112         return false;
113     }
114     void OnSwitchChange(const std::string &switchKey);
115     void RegisterObserver(const std::string &switchKey);
116     bool IsNeedToUnloadService();
117     int32_t UnloadIntellVoiceService();
118     void NoiftySwitchOnToPowerChange();
119     void OnDetected(int32_t uuid);
120     void OnSingleLevelDetected();
121     int32_t SwitchOnProc(int32_t uuid, bool needUpdateAdapter);
122     int32_t SwitchOffProc(int32_t uuid);
123     void CreateAndStartServiceObject(int32_t uuid, bool needResetAdapter);
124     void ReleaseServiceObject(int32_t uuid);
125     bool AddStartDetectionTask(int32_t uuid);
126     void DelStartDetectionTask(int32_t uuid);
127     bool IsSwitchError(const std::string &key);
128     void StopWakeupSource();
129     void ResetSingleLevelWakeup(const std::string &value);
130     bool HasReceviedRecordStartMsg();
131     void NoiftyRecordStartInfoChange();
132     void HandleRecordStartInfoChange();
133     void OnTriggerConnectServiceStart() override;
134     void SetShortWordStatus();
135 
136 private:
137     bool notifyPowerModeChange_ = false;
138     std::mutex powerModeChangeMutex_;
139     std::condition_variable powerModeChangeCv_;
140     std::mutex switchMutex_;
141     std::map<const std::string, sptr<SwitchObserver>> switchObserver_;
142     IntellVoiceUtils::UniqueProductType<SwitchProvider> switchProvider_ =
143         IntellVoiceUtils::UniqueProductType<SwitchProvider> {nullptr, nullptr};
144     std::map<int32_t, bool> isStarted_;
145     int32_t recordStart_ = -1;
146     bool notifyRecordStartInfoChange_ = false;
147     std::mutex recordStartInfoChangeMutex_;
148     std::condition_variable recordStartInfoChangeCv_;
149 #ifdef USE_FFRT
150     std::shared_ptr<ffrt::queue> taskQueue_ = nullptr;
151     std::map<int32_t, ffrt::task_handle> taskHandle_;
152 #endif
153 };
154 }  // namespace IntellVoice
155 }  // namespace OHOS
156 #endif
157