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 
16 #ifndef HDI_DEVICE_INTELL_VOICE_MANAGER_IMPL_H
17 #define HDI_DEVICE_INTELL_VOICE_MANAGER_IMPL_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 
23 #include "v1_2/intell_voice_engine_types.h"
24 #include "v1_1/intell_voice_engine_types.h"
25 #include "v1_2/iintell_voice_engine_manager.h"
26 #include "v1_1/iintell_voice_data_opr_callback.h"
27 #include "i_engine.h"
28 
29 namespace OHOS {
30 namespace IntelligentVoice {
31 namespace Engine {
32 using OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager;
33 using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter;
34 using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType;
35 using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor;
36 using OHOS::HDI::IntelligentVoice::Engine::V1_1::IIntellVoiceDataOprCallback;
37 using OHOS::HDI::IntelligentVoice::Engine::V1_1::IntellVoiceDataOprType;
38 using OHOS::HDI::IntelligentVoice::Engine::V1_2::UploadHdiFile;
39 
40 using GetEngineManagerHalInstFunc = IEngineManager *(*)();
41 
42 struct IntellVoiceEngineManagerPriv {
43     void *handle { nullptr };
44     GetEngineManagerHalInstFunc getEngineManagerHalInst { nullptr };
45 };
46 
47 class DataOprListener : public IDataOprListener {
48 public:
49     DataOprListener(sptr<IIntellVoiceDataOprCallback> cb);
50     ~DataOprListener();
51     int32_t OnDataOprEvent(IntellVoiceDataOprType type, const OprDataInfo &inData, OprDataInfo &outData) override;
52 private:
53     sptr<Ashmem> CreateAshmemFromOprData(const OprDataInfo &data, const std::string &name);
54     int32_t FillOprDataFromAshmem(const sptr<Ashmem> &ashmem, OprDataInfo &data);
55     sptr<IIntellVoiceDataOprCallback> cb_;
56 };
57 
58 class IntellVoiceEngineManagerImpl : public IIntellVoiceEngineManager {
59 public:
60     IntellVoiceEngineManagerImpl();
61     ~IntellVoiceEngineManagerImpl();
62 
63     int32_t GetAdapterDescriptors(std::vector<IntellVoiceEngineAdapterDescriptor> &descs) override;
64     int32_t CreateAdapter(const IntellVoiceEngineAdapterDescriptor &descriptor,
65         sptr<HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter> &adapter) override;
66     int32_t ReleaseAdapter(const IntellVoiceEngineAdapterDescriptor &descriptor) override;
67     int32_t SetDataOprCallback(const sptr<IIntellVoiceDataOprCallback> &dataOprCallback) override;
68     int32_t GetUploadFiles(int32_t numMax, std::vector<UploadHdiFile> &files) override;
69     int32_t CreateAdapter_V_2(const IntellVoiceEngineAdapterDescriptor &descriptor,
70         sptr<HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineAdapter> &adapter) override;
71     int32_t GetCloneFilesList(std::vector<std::string> &cloneFiles) override;
72     int32_t GetCloneFile(const std::string &filePath, std::vector<uint8_t> &buffer) override;
73     int32_t SendCloneFile(const std::string &filePath, const std::vector<uint8_t> &buffer) override;
74     int32_t ClearUserWakeupData(const std::string &wakeupPhrase) override;
75 
76 private:
77     int32_t LoadVendorLib();
78     void UnloadVendorLib();
79     template<typename T>
80     int32_t CreateAdapterInner(const IntellVoiceEngineAdapterDescriptor &descriptor, sptr<T> &adapter);
81 
82 private:
83     std::map<IntellVoiceEngineAdapterType, sptr<IIntellVoiceEngineAdapter>> adapters_;
84     std::mutex mutex_ {};
85     IntellVoiceEngineManagerPriv engineManagerPriv_;
86     IEngineManager *inst_ = nullptr;
87 };
88 }
89 }
90 }
91 #endif
92