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 #include "trigger_connector_internal_validation.h"
16 #include "intell_voice_log.h"
17
18 #define LOG_TAG "TriggerConnectorValidation"
19
20 namespace OHOS {
21 namespace IntellVoiceTrigger {
TriggerConnectorInternalValidation(std::unique_ptr<IIntellVoiceTriggerConnectorInternal> delegate)22 TriggerConnectorInternalValidation::TriggerConnectorInternalValidation(
23 std::unique_ptr<IIntellVoiceTriggerConnectorInternal> delegate) : delegate_(std::move(delegate))
24 {
25 }
26
ListModuleDescriptors()27 std::vector<TriggerConnectorModuleDesc> TriggerConnectorInternalValidation::ListModuleDescriptors()
28 {
29 std::lock_guard<std::mutex> lock(mutex_);
30 std::vector<TriggerConnectorModuleDesc> ret = delegate_->ListModuleDescriptors();
31 if (ret.empty()) {
32 INTELL_VOICE_LOG_ERROR("no trigger connector module desc");
33 return ret;
34 }
35
36 if (moduleDescs_.empty()) {
37 for (auto it : ret) {
38 moduleDescs_.insert(it.adapterName);
39 }
40 return ret;
41 }
42
43 if (ret.size() != moduleDescs_.size()) {
44 INTELL_VOICE_LOG_ERROR("size different, ret size:%zu, module descs size:%zu", ret.size(),
45 moduleDescs_.size());
46 return {};
47 }
48
49 for (auto it : ret) {
50 if (moduleDescs_.count(it.adapterName) == 0) {
51 INTELL_VOICE_LOG_ERROR("adapter name:%{public}s does not exist", it.adapterName.c_str());
52 return {};
53 }
54 }
55
56 return ret;
57 }
58
GetModule(const std::string & adapterName,std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)59 std::shared_ptr<IIntellVoiceTriggerConnectorModule> TriggerConnectorInternalValidation::GetModule(
60 const std::string &adapterName, std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)
61 {
62 std::lock_guard<std::mutex> lock(mutex_);
63 if (callback == nullptr) {
64 INTELL_VOICE_LOG_ERROR("callback is nullptr");
65 return nullptr;
66 }
67 if (moduleDescs_.count(adapterName) == 0) {
68 INTELL_VOICE_LOG_ERROR("adapter name:%{public}s does not exist", adapterName.c_str());
69 return nullptr;
70 }
71 std::shared_ptr<TriggerConnectorModuleValidation> moduleValidation =
72 std::make_shared<TriggerConnectorModuleValidation>(callback);
73 if (moduleValidation == nullptr) {
74 INTELL_VOICE_LOG_ERROR("failed to malloc connector module validation");
75 return nullptr;
76 }
77
78 auto delegate = delegate_->GetModule(adapterName, moduleValidation->GetCallbackWrapper());
79 if (delegate == nullptr) {
80 INTELL_VOICE_LOG_ERROR("failed to get delegate");
81 return nullptr;
82 }
83 moduleValidation->SetDelegate(delegate);
84 return moduleValidation;
85 }
86
ValidateGenericModel(std::shared_ptr<GenericTriggerModel> model)87 bool TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::ValidationUtils::ValidateGenericModel(
88 std::shared_ptr<GenericTriggerModel> model)
89 {
90 if (model == nullptr) {
91 INTELL_VOICE_LOG_ERROR("generic model is nullptr");
92 return false;
93 }
94
95 if ((model->GetType() != TriggerModel::TriggerModelType::VOICE_WAKEUP_TYPE) &&
96 (model->GetType() != TriggerModel::TriggerModelType::PROXIMAL_WAKEUP_TYPE)) {
97 INTELL_VOICE_LOG_ERROR("generic model type:%{public}d is invalid", model->GetType());
98 return false;
99 }
100
101 if (model->GetData().size() == 0) {
102 INTELL_VOICE_LOG_ERROR("generic model data size is zero");
103 return false;
104 }
105
106 return true;
107 }
108
TriggerConnectorModuleValidation(std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)109 TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::TriggerConnectorModuleValidation(
110 std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)
111 {
112 callbackWrapper_ = std::make_shared<TriggerConnectorCallbackValidation>(callback);
113 if (callbackWrapper_ == nullptr) {
114 INTELL_VOICE_LOG_ERROR("failed to malloc callback wrapper");
115 }
116 }
117
LoadModel(std::shared_ptr<GenericTriggerModel> model,int32_t & modelHandle)118 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::LoadModel(
119 std::shared_ptr<GenericTriggerModel> model, int32_t &modelHandle)
120 {
121 if (!ValidationUtils::ValidateGenericModel(model)) {
122 INTELL_VOICE_LOG_ERROR();
123 return -1;
124 }
125 return delegate_->LoadModel(model, modelHandle);
126 }
127
UnloadModel(int32_t modelHandle)128 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::UnloadModel(
129 int32_t modelHandle)
130 {
131 return delegate_->UnloadModel(modelHandle);
132 }
133
Start(int32_t modelHandle)134 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::Start(int32_t modelHandle)
135 {
136 return delegate_->Start(modelHandle);
137 }
138
Stop(int32_t modelHandle)139 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::Stop(int32_t modelHandle)
140 {
141 return delegate_->Stop(modelHandle);
142 }
143
SetParams(const std::string & key,const std::string & value)144 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::SetParams(const std::string &key,
145 const std::string &value)
146 {
147 return delegate_->SetParams(key, value);
148 }
149
GetParams(const std::string & key,std::string & value)150 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::GetParams(const std::string &key,
151 std::string &value)
152 {
153 return delegate_->GetParams(key, value);
154 }
155 }
156 }
157