• 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 #include "trigger_connector_internal_validation.h"
16 #include "intell_voice_log.h"
17 
18 #define LOG_TAG "TriggerConnectorInternalValidation"
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 (moduleDescs_.count(adapterName) == 0) {
64         INTELL_VOICE_LOG_ERROR("adapter name:%{public}s does not exist", adapterName.c_str());
65         return nullptr;
66     }
67     std::shared_ptr<TriggerConnectorModuleValidation> moduleValidation =
68         std::make_shared<TriggerConnectorModuleValidation>(callback);
69     if (moduleValidation == nullptr) {
70         INTELL_VOICE_LOG_ERROR("failed to malloc connector module validation");
71         return nullptr;
72     }
73 
74     auto delegate = delegate_->GetModule(adapterName, moduleValidation->GetCallbackWrapper());
75     if (delegate == nullptr) {
76         INTELL_VOICE_LOG_ERROR("failed to get delegate");
77         return nullptr;
78     }
79     moduleValidation->SetDelegate(delegate);
80     return moduleValidation;
81 }
82 
ValidateGenericModel(std::shared_ptr<GenericTriggerModel> model)83 bool TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::ValidationUtils::ValidateGenericModel(
84     std::shared_ptr<GenericTriggerModel> model)
85 {
86     if (model == nullptr) {
87         INTELL_VOICE_LOG_ERROR("generic model is nullptr");
88         return false;
89     }
90 
91     if (model->GetType() != TriggerModel::TriggerModelType::GENERIC_TYPE) {
92         INTELL_VOICE_LOG_ERROR("generic model type:%{public}d is invalid", model->GetType());
93         return false;
94     }
95 
96     if (model->GetData().size() == 0) {
97         INTELL_VOICE_LOG_ERROR("generic model data size is zero");
98         return false;
99     }
100 
101     return true;
102 }
103 
TriggerConnectorModuleValidation(std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)104 TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::TriggerConnectorModuleValidation(
105     std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)
106 {
107     callbackWrapper_ = std::make_shared<TriggerConnectorCallbackValidation>(callback);
108     if (callbackWrapper_ == nullptr) {
109         INTELL_VOICE_LOG_ERROR("failed to malloc callback wrapper");
110     }
111 }
112 
LoadModel(std::shared_ptr<GenericTriggerModel> model,int32_t & modelHandle)113 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::LoadModel(
114     std::shared_ptr<GenericTriggerModel> model, int32_t &modelHandle)
115 {
116     if (!ValidationUtils::ValidateGenericModel(model)) {
117         INTELL_VOICE_LOG_ERROR();
118         return -1;
119     }
120     return delegate_->LoadModel(model, modelHandle);
121 }
122 
UnloadModel(int32_t modelHandle)123 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::UnloadModel(
124     int32_t modelHandle)
125 {
126     return delegate_->UnloadModel(modelHandle);
127 }
128 
Start(int32_t modelHandle)129 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::Start(int32_t modelHandle)
130 {
131     return delegate_->Start(modelHandle);
132 }
133 
Stop(int32_t modelHandle)134 int32_t TriggerConnectorInternalValidation::TriggerConnectorModuleValidation::Stop(int32_t modelHandle)
135 {
136     return delegate_->Stop(modelHandle);
137 }
138 }
139 }
140