• 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     int32_t HandleCloneUpdate(const std::string &wakeupInfo, const sptr<IRemoteObject> &object);
49     void HandleServiceStop();
50 
51     void HandleSwitchOn(bool isAsync, int32_t uuid, bool needUpdateAdapter);
52     void HandleSwitchOff(bool isAsync, int32_t uuid);
53 
54     bool RegisterProxyDeathRecipient(IntellVoiceEngineType type, const sptr<IRemoteObject> &object);
55     bool DeregisterProxyDeathRecipient(IntellVoiceEngineType type);
56     int32_t GetUploadFiles(int numMax, std::vector<UploadFilesFromHdi> &files);
57     int32_t EngineSetParameter(const std::string &keyValueList);
58     std::string EngineGetParameter(const std::string &key);
59     int32_t GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles);
60     int32_t GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer);
61     int32_t SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer);
62     void SetScreenOff(bool value);
63 
64     void ProcBreathModel();
65     void ProcSingleLevelModel();
66     void CreateSwitchProvider();
67     void ReleaseSwitchProvider();
68     bool StartDetection(int32_t uuid);
69     void StopDetection(int32_t uuid);
70     bool QuerySwitchStatus(const std::string &key) override;
71     int32_t ClearUserData();
72     void OnServiceStart(std::map<int32_t, std::function<void(bool)>> &saChangeFuncMap);
73     void OnServiceStop();
74 
75     using TaskExecutor::AddAsyncTask;
76     using TaskExecutor::AddSyncTask;
77     using TaskExecutor::StopThread;
78 
79 private:
80     void HandleCloseWakeupSource(bool isNeedStop = false) override;
81     void HandleClearWakeupEngineCb() override;
82     void HandleHeadsetHostDie() override;
83     void HandleUpdateComplete(int32_t result, const std::string &param) override;
84     void HandleUpdateRetry() override;
85     int32_t ReleaseEngine(IntellVoiceEngineType type) override;
86     std::string TriggerGetParameter(const std::string &key) override;
87     int32_t TriggerSetParameter(const std::string &key, const std::string &value) override;
88     void TriggerMgrUpdateModel(std::vector<uint8_t> buffer, int32_t uuid,
89         IntellVoiceTrigger::TriggerModelType type) override;
90 
91 private:
92     IntellVoiceServiceManager();
IsSwitchKeyValid(const std::string & key)93     static bool IsSwitchKeyValid(const std::string &key)
94     {
95         if ((key == WAKEUP_KEY) || (key == WHISPER_KEY) || (key == IMPROVE_KEY) || (key == SHORTWORD_KEY)) {
96             return true;
97         }
98         return false;
99     }
100 
QuerySwitchByUuid(int32_t uuid)101     bool QuerySwitchByUuid(int32_t uuid)
102     {
103         if (uuid == VOICE_WAKEUP_MODEL_UUID) {
104             return QuerySwitchStatus(WAKEUP_KEY);
105         }
106 
107         if (uuid == PROXIMAL_WAKEUP_MODEL_UUID) {
108             return QuerySwitchStatus(WHISPER_KEY);
109         }
110 
111         return false;
112     }
113     void OnSwitchChange(const std::string &switchKey);
114     void RegisterObserver(const std::string &switchKey);
115     bool IsNeedToUnloadService();
116     int32_t UnloadIntellVoiceService();
117     void NoiftySwitchOnToPowerChange();
118     void OnDetected(int32_t uuid);
119     void OnSingleLevelDetected();
120     int32_t SwitchOnProc(int32_t uuid, bool needUpdateAdapter);
121     int32_t SwitchOffProc(int32_t uuid);
122     void CreateAndStartServiceObject(int32_t uuid, bool needResetAdapter);
123     void ReleaseServiceObject(int32_t uuid);
124     bool AddStartDetectionTask(int32_t uuid);
125     void DelStartDetectionTask(int32_t uuid);
126     bool IsSwitchError(const std::string &key);
127     void NotifyEvent(const std::string &eventType);
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