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 "intell_voice_trigger_adapter_impl.h"
16 #include "hdf_base.h"
17 #include "intell_voice_log.h"
18 #include "securec.h"
19 #include "scope_guard.h"
20 #include "memory_guard.h"
21
22 #undef HDF_LOG_TAG
23 #define HDF_LOG_TAG "TriggerAdapterImpl"
24
25 using namespace OHOS::HDI::IntelligentVoice::Trigger::V1_0;
26 using namespace OHOS::IntelligentVoice::Utils;
27
28 namespace OHOS {
29 namespace IntelligentVoice {
30 namespace Trigger {
IntellVoiceTriggerCallbackDevice(OHOS::sptr<IIntellVoiceTriggerCallback> callback)31 IntellVoiceTriggerCallbackDevice::IntellVoiceTriggerCallbackDevice(OHOS::sptr<IIntellVoiceTriggerCallback> callback)
32 : callback_(callback)
33 {
34 }
35
~IntellVoiceTriggerCallbackDevice()36 IntellVoiceTriggerCallbackDevice::~IntellVoiceTriggerCallbackDevice()
37 {
38 callback_ = nullptr;
39 }
40
OnRecognitionHdiEvent(const IntellVoiceRecognitionEvent & event,int32_t cookie)41 void IntellVoiceTriggerCallbackDevice::OnRecognitionHdiEvent(const IntellVoiceRecognitionEvent &event, int32_t cookie)
42 {
43 if (callback_ == nullptr) {
44 INTELLIGENT_VOICE_LOGE("callback_ is nullptr");
45 return;
46 }
47 callback_->OnRecognitionHdiEvent(event, cookie);
48 }
49
IntellVoiceTriggerAdapterImpl(std::unique_ptr<ITrigger> adapter)50 IntellVoiceTriggerAdapterImpl::IntellVoiceTriggerAdapterImpl(std::unique_ptr<ITrigger> adapter)
51 : adapter_(std::move(adapter))
52 {
53 }
54
~IntellVoiceTriggerAdapterImpl()55 IntellVoiceTriggerAdapterImpl::~IntellVoiceTriggerAdapterImpl()
56 {
57 adapter_ = nullptr;
58 }
59
GetProperties(IntellVoiceTriggerProperties & properties)60 int32_t IntellVoiceTriggerAdapterImpl::GetProperties(IntellVoiceTriggerProperties& properties)
61 {
62 return adapter_->GetProperties(properties);
63 }
64
LoadModel(const IntellVoiceTriggerModel & model,const sptr<IIntellVoiceTriggerCallback> & triggerCallback,int32_t cookie,int32_t & handle)65 int32_t IntellVoiceTriggerAdapterImpl::LoadModel(const IntellVoiceTriggerModel &model,
66 const sptr<IIntellVoiceTriggerCallback> &triggerCallback, int32_t cookie, int32_t &handle)
67 {
68 MemoryGuard memoryGuard;
69 std::shared_ptr<ITriggerCallback> cb = std::make_shared<IntellVoiceTriggerCallbackDevice>(triggerCallback);
70 if (cb == nullptr) {
71 INTELLIGENT_VOICE_LOGE("callback is nullptr");
72 return HDF_ERR_MALLOC_FAIL;
73 }
74
75 if (model.data == nullptr) {
76 INTELLIGENT_VOICE_LOGE("model data is nullptr");
77 return HDF_ERR_INVALID_PARAM;
78 }
79
80 ON_SCOPE_EXIT {
81 INTELLIGENT_VOICE_LOGI("close ashmem");
82 model.data->UnmapAshmem();
83 model.data->CloseAshmem();
84 };
85
86 std::vector<uint8_t> modelData;
87 if (GetModelDataFromAshmem(model.data, modelData) != static_cast<int32_t>(HDF_SUCCESS)) {
88 return HDF_ERR_INVALID_PARAM;
89 }
90
91 TriggerModel triggerModel(model.type, model.uid, modelData);
92 return adapter_->LoadIntellVoiceTriggerModel(triggerModel, cb, cookie, handle);
93 }
94
UnloadModel(int32_t handle)95 int32_t IntellVoiceTriggerAdapterImpl::UnloadModel(int32_t handle)
96 {
97 MemoryGuard memoryGuard;
98 return adapter_->UnloadIntellVoiceTriggerModel(handle);
99 }
100
Start(int32_t handle)101 int32_t IntellVoiceTriggerAdapterImpl::Start(int32_t handle)
102 {
103 MemoryGuard memoryGuard;
104 return adapter_->Start(handle);
105 }
106
Stop(int32_t handle)107 int32_t IntellVoiceTriggerAdapterImpl::Stop(int32_t handle)
108 {
109 MemoryGuard memoryGuard;
110 return adapter_->Stop(handle);
111 }
112
GetModelDataFromAshmem(sptr<Ashmem> ashmem,std::vector<uint8_t> & modelData)113 int32_t IntellVoiceTriggerAdapterImpl::GetModelDataFromAshmem(sptr<Ashmem> ashmem, std::vector<uint8_t> &modelData)
114 {
115 if (ashmem == nullptr) {
116 INTELLIGENT_VOICE_LOGE("ashmem is nullptr");
117 return HDF_ERR_INVALID_OBJECT;
118 }
119
120 uint32_t size = static_cast<uint32_t>(ashmem->GetAshmemSize());
121 if (size == 0) {
122 INTELLIGENT_VOICE_LOGE("size is zero");
123 return HDF_ERR_INVALID_PARAM;
124 }
125
126 if (!ashmem->MapReadOnlyAshmem()) {
127 INTELLIGENT_VOICE_LOGE("map ashmem failed");
128 return HDF_FAILURE;
129 }
130
131 const uint8_t *buffer = static_cast<const uint8_t *>(ashmem->ReadFromAshmem(size, 0));
132 if (buffer == nullptr) {
133 INTELLIGENT_VOICE_LOGE("read from ashmem failed");
134 return HDF_ERR_MALLOC_FAIL;
135 }
136
137 modelData.insert(modelData.begin(), buffer, buffer + size);
138 return HDF_SUCCESS;
139 }
140 }
141 }
142 }