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 #include "intell_voice_service_proxy.h"
17
18 #include <cinttypes>
19 #include "intell_voice_log.h"
20 #include "intell_voice_death_recipient_stub.h"
21
22 #define LOG_TAG "IntellVoiceServiceProxy"
23 namespace OHOS {
24 namespace IntellVoiceEngine {
25 #define CROSS_PROCESS_BUF_SIZE_LIMIT (256 *1024)
26
CreateIntellVoiceEngine(IntellVoiceEngineType type,sptr<IIntellVoiceEngine> & inst)27 int32_t IntellVoiceServiceProxy::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
28 {
29 MessageParcel data;
30 MessageParcel reply;
31 MessageOption option;
32
33 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
34 data.WriteInt32(static_cast<int>(type));
35
36 auto stub = sptr<IntellVoiceDeathRecipientStub>(new (std::nothrow) IntellVoiceDeathRecipientStub());
37 data.WriteRemoteObject(stub);
38
39 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE, data, reply, option);
40 int32_t ret = reply.ReadInt32();
41 if (ret != 0) {
42 INTELL_VOICE_LOG_ERROR("failed to create intell voice engine, type:%{public}d", type);
43 return ret;
44 }
45
46 sptr<IRemoteObject> remote = reply.ReadRemoteObject();
47 if (remote == nullptr) {
48 INTELL_VOICE_LOG_ERROR("create engine failed, engine is null");
49 return -1;
50 }
51 inst = iface_cast<IIntellVoiceEngine>(remote);
52 return 0;
53 }
54
ReleaseIntellVoiceEngine(IntellVoiceEngineType type)55 int32_t IntellVoiceServiceProxy::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
56 {
57 MessageParcel data;
58 MessageParcel reply;
59 MessageOption option;
60
61 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
62 data.WriteInt32(type);
63
64 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE, data, reply, option);
65 return reply.ReadInt32();
66 }
67
GetUploadFiles(int numMax,std::vector<UploadHdiFile> & files)68 int32_t IntellVoiceServiceProxy::GetUploadFiles(int numMax, std::vector<UploadHdiFile> &files)
69 {
70 MessageParcel data;
71 MessageParcel reply;
72 MessageOption option;
73
74 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
75
76 data.WriteInt32(numMax);
77 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_REPORTED_FILES, data, reply, option);
78 int32_t ret = reply.ReadInt32();
79 if (ret != 0) {
80 INTELL_VOICE_LOG_ERROR("failed to get reported files, ret:%{public}d", ret);
81 return ret;
82 }
83 int32_t size = reply.ReadInt32();
84 INTELL_VOICE_LOG_INFO("reported files info size:%{public}d", size);
85 if (size <= 0) {
86 return -1;
87 }
88
89 for (int32_t i = 0; i < size; i++) {
90 UploadHdiFile file;
91 file.type = static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_2::UploadHdiFileType>(reply.ReadInt32());
92 file.filesDescription = reply.ReadString();
93 int32_t filesContentSize = reply.ReadInt32();
94 INTELL_VOICE_LOG_INFO("filesContentSize:%{public}d", filesContentSize);
95 for (int32_t j = 0; j < filesContentSize; j++) {
96 file.filesContent.push_back(reply.ReadAshmem());
97 }
98 files.push_back(file);
99 }
100 return ret;
101 }
102
GetParameter(const std::string & key)103 std::string IntellVoiceServiceProxy::GetParameter(const std::string &key)
104 {
105 MessageParcel data;
106 MessageParcel reply;
107 MessageOption option;
108
109 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
110 data.WriteString(key);
111 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_PARAMETER, data, reply, option);
112 return reply.ReadString();
113 }
114
SetParameter(const std::string & keyValueList)115 int32_t IntellVoiceServiceProxy::SetParameter(const std::string &keyValueList)
116 {
117 MessageParcel data;
118 MessageParcel reply;
119 MessageOption option;
120
121 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
122 data.WriteString(keyValueList);
123 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_SET_PARAMETER, data, reply, option);
124 return reply.ReadInt32();
125 }
126
GetWakeupSourceFilesList(std::vector<std::string> & cloneFiles)127 int32_t IntellVoiceServiceProxy::GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 MessageOption option;
132
133 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
134 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILES_LIST, data, reply, option);
135 int32_t ret = reply.ReadInt32();
136 if (ret != 0) {
137 INTELL_VOICE_LOG_ERROR("get wakeup source files list failed, ret:%{public}d", ret);
138 return ret;
139 }
140
141 uint32_t size = reply.ReadUint32();
142 cloneFiles.reserve(size);
143 for (uint32_t i = 0; i < size; i++) {
144 cloneFiles.push_back(reply.ReadString());
145 }
146
147 return ret;
148 }
149
GetWakeupSourceFile(const std::string & filePath,std::vector<uint8_t> & buffer)150 int32_t IntellVoiceServiceProxy::GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer)
151 {
152 MessageParcel data;
153 MessageParcel reply;
154 MessageOption option;
155
156 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
157 data.WriteString(filePath);
158 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILE, data, reply, option);
159 int32_t ret = reply.ReadInt32();
160 if (ret != 0) {
161 INTELL_VOICE_LOG_ERROR("get wakeup source file failed, ret:%{public}d", ret);
162 return ret;
163 }
164
165 uint32_t size = reply.ReadUint32();
166 const uint8_t *buff = reply.ReadBuffer(size);
167 if (buff == nullptr) {
168 INTELL_VOICE_LOG_ERROR("buf is null");
169 return -1;
170 }
171
172 if (size == 0 || size > CROSS_PROCESS_BUF_SIZE_LIMIT) {
173 INTELL_VOICE_LOG_ERROR("buf size is invalid %{public}u", size);
174 return -1;
175 }
176
177 buffer.resize(size);
178 std::copy(buff, buff + size, buffer.data());
179
180 return ret;
181 }
182
SendWakeupFile(const std::string & filePath,const std::vector<uint8_t> & buffer)183 int32_t IntellVoiceServiceProxy::SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer)
184 {
185 MessageParcel data;
186 MessageParcel reply;
187 MessageOption option;
188
189 if (buffer.data() == nullptr) {
190 INTELL_VOICE_LOG_ERROR("buf is null");
191 return -1;
192 }
193
194 if (buffer.size() == 0 || buffer.size() > CROSS_PROCESS_BUF_SIZE_LIMIT) {
195 INTELL_VOICE_LOG_ERROR("buffer size is invalid %{public}u", static_cast<uint32_t>(buffer.size()));
196 return -1;
197 }
198
199 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
200 data.WriteString(filePath);
201 data.WriteUint32(buffer.size());
202 data.WriteBuffer(buffer.data(), buffer.size());
203 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_SEND_CLONE_FILE, data, reply, option);
204 return reply.ReadInt32();
205 }
206
EnrollWithWakeupFilesForResult(const std::string & wakeupInfo,sptr<IRemoteObject> object)207 int32_t IntellVoiceServiceProxy::EnrollWithWakeupFilesForResult(const std::string &wakeupInfo,
208 sptr<IRemoteObject> object)
209 {
210 MessageParcel data;
211 MessageParcel reply;
212 MessageOption option;
213
214 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
215 data.WriteString(wakeupInfo);
216 data.WriteRemoteObject(object);
217 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CLONE_FOR_RESULT, data, reply, option);
218 return reply.ReadInt32();
219 }
220
ClearUserData()221 int32_t IntellVoiceServiceProxy::ClearUserData()
222 {
223 MessageParcel data;
224 MessageParcel reply;
225 MessageOption option;
226
227 data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
228 Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CLEAR_USER_DATA, data, reply, option);
229 return reply.ReadInt32();
230 }
231 } // namespace IntellVoiceEngine
232 } // namespace OHOS
233