• 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 
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     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE, data, reply, option);
40     if (error != 0) {
41         INTELL_VOICE_LOG_ERROR("create intell voice engine error: %{public}d", error);
42         return -1;
43     }
44     int32_t ret = reply.ReadInt32();
45     if (ret != 0) {
46         INTELL_VOICE_LOG_ERROR("failed to create intell voice engine, type:%{public}d", type);
47         return ret;
48     }
49 
50     sptr<IRemoteObject> remote = reply.ReadRemoteObject();
51     if (remote == nullptr) {
52         INTELL_VOICE_LOG_ERROR("create engine failed, engine is null");
53         return -1;
54     }
55     inst = iface_cast<IIntellVoiceEngine>(remote);
56     return 0;
57 }
58 
ReleaseIntellVoiceEngine(IntellVoiceEngineType type)59 int32_t IntellVoiceServiceProxy::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
60 {
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option;
64 
65     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
66     data.WriteInt32(type);
67 
68     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE, data, reply, option);
69     if (error != 0) {
70         INTELL_VOICE_LOG_ERROR("release intell voice engine error: %{public}d", error);
71         return -1;
72     }
73     return reply.ReadInt32();
74 }
75 
GetUploadFiles(int numMax,std::vector<UploadFilesFromHdi> & files)76 int32_t IntellVoiceServiceProxy::GetUploadFiles(int numMax, std::vector<UploadFilesFromHdi> &files)
77 {
78     MessageParcel data;
79     MessageParcel reply;
80     MessageOption option;
81 
82     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
83 
84     data.WriteInt32(numMax);
85     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_REPORTED_FILES, data, reply, option);
86     if (error != 0) {
87         INTELL_VOICE_LOG_ERROR("get upload files error: %{public}d", error);
88         return -1;
89     }
90     int32_t ret = reply.ReadInt32();
91     if (ret != 0) {
92         INTELL_VOICE_LOG_ERROR("failed to get reported files, ret:%{public}d", ret);
93         return ret;
94     }
95     int32_t size = reply.ReadInt32();
96     INTELL_VOICE_LOG_INFO("reported files info size:%{public}d", size);
97     if (size <= 0) {
98         return -1;
99     }
100 
101     for (int32_t i = 0; i < size; i++) {
102         UploadFilesFromHdi file;
103         file.type = reply.ReadInt32();
104         file.filesDescription = reply.ReadString();
105         int32_t filesContentSize = reply.ReadInt32();
106         INTELL_VOICE_LOG_INFO("filesContentSize:%{public}d", filesContentSize);
107         for (int32_t j = 0; j < filesContentSize; j++) {
108             file.filesContent.push_back(reply.ReadAshmem());
109         }
110         files.push_back(file);
111     }
112     return ret;
113 }
114 
GetParameter(const std::string & key)115 std::string IntellVoiceServiceProxy::GetParameter(const std::string &key)
116 {
117     MessageParcel data;
118     MessageParcel reply;
119     MessageOption option;
120 
121     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
122     data.WriteString(key);
123     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_PARAMETER, data, reply, option);
124     if (error != 0) {
125         INTELL_VOICE_LOG_ERROR("get parameter error: %{public}d", error);
126         return "";
127     }
128     return reply.ReadString();
129 }
130 
SetParameter(const std::string & keyValueList)131 int32_t IntellVoiceServiceProxy::SetParameter(const std::string &keyValueList)
132 {
133     MessageParcel data;
134     MessageParcel reply;
135     MessageOption option;
136 
137     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
138     data.WriteString(keyValueList);
139     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_SET_PARAMETER, data, reply, option);
140     if (error != 0) {
141         INTELL_VOICE_LOG_ERROR("set parameter error: %{public}d", error);
142         return -1;
143     }
144     return reply.ReadInt32();
145 }
146 
GetWakeupSourceFilesList(std::vector<std::string> & cloneFiles)147 int32_t IntellVoiceServiceProxy::GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles)
148 {
149     MessageParcel data;
150     MessageParcel reply;
151     MessageOption option;
152 
153     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
154     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILES_LIST, data, reply, option);
155     if (error != 0) {
156         INTELL_VOICE_LOG_ERROR("get wakeup source files list error: %{public}d", error);
157         return -1;
158     }
159     int32_t ret = reply.ReadInt32();
160     if (ret != 0) {
161         INTELL_VOICE_LOG_ERROR("get wakeup source files list failed, ret:%{public}d", ret);
162         return ret;
163     }
164 
165     uint32_t size = reply.ReadUint32();
166     cloneFiles.reserve(size);
167     for (uint32_t i = 0; i < size; i++) {
168         cloneFiles.push_back(reply.ReadString());
169     }
170 
171     return ret;
172 }
173 
GetWakeupSourceFile(const std::string & filePath,std::vector<uint8_t> & buffer)174 int32_t IntellVoiceServiceProxy::GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer)
175 {
176     MessageParcel data;
177     MessageParcel reply;
178     MessageOption option;
179 
180     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
181     data.WriteString(filePath);
182     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILE, data, reply, option);
183     if (error != 0) {
184         INTELL_VOICE_LOG_ERROR("get wakeup source file error: %{public}d", error);
185         return -1;
186     }
187     int32_t ret = reply.ReadInt32();
188     if (ret != 0) {
189         INTELL_VOICE_LOG_ERROR("get wakeup source file failed, ret:%{public}d", ret);
190         return ret;
191     }
192 
193     uint32_t size = reply.ReadUint32();
194     const uint8_t *buff = reply.ReadBuffer(size);
195     if (buff == nullptr) {
196         INTELL_VOICE_LOG_ERROR("buf is null");
197         return -1;
198     }
199 
200     if (size == 0 || size > CROSS_PROCESS_BUF_SIZE_LIMIT) {
201         INTELL_VOICE_LOG_ERROR("buf size is invalid %{public}u", size);
202         return -1;
203     }
204 
205     buffer.resize(size);
206     std::copy(buff, buff + size, buffer.data());
207 
208     return ret;
209 }
210 
SendWakeupFile(const std::string & filePath,const std::vector<uint8_t> & buffer)211 int32_t IntellVoiceServiceProxy::SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer)
212 {
213     MessageParcel data;
214     MessageParcel reply;
215     MessageOption option;
216 
217     if (buffer.data() == nullptr) {
218         INTELL_VOICE_LOG_ERROR("buf is null");
219         return -1;
220     }
221 
222     if (buffer.size() == 0 || buffer.size() > CROSS_PROCESS_BUF_SIZE_LIMIT) {
223         INTELL_VOICE_LOG_ERROR("buffer size is invalid %{public}u", static_cast<uint32_t>(buffer.size()));
224         return -1;
225     }
226 
227     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
228     data.WriteString(filePath);
229     data.WriteUint32(buffer.size());
230     data.WriteBuffer(buffer.data(), buffer.size());
231     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_SEND_CLONE_FILE, data, reply, option);
232     if (error != 0) {
233         INTELL_VOICE_LOG_ERROR("send wakeup file error: %{public}d", error);
234         return -1;
235     }
236     return reply.ReadInt32();
237 }
238 
EnrollWithWakeupFilesForResult(const std::string & wakeupInfo,sptr<IRemoteObject> object)239 int32_t IntellVoiceServiceProxy::EnrollWithWakeupFilesForResult(const std::string &wakeupInfo,
240     sptr<IRemoteObject> object)
241 {
242     MessageParcel data;
243     MessageParcel reply;
244     MessageOption option;
245 
246     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
247     data.WriteString(wakeupInfo);
248     data.WriteRemoteObject(object);
249     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CLONE_FOR_RESULT, data, reply, option);
250     if (error != 0) {
251         INTELL_VOICE_LOG_ERROR("enroll with wakeup files for result error: %{public}d", error);
252         return -1;
253     }
254     return reply.ReadInt32();
255 }
256 
ClearUserData()257 int32_t IntellVoiceServiceProxy::ClearUserData()
258 {
259     MessageParcel data;
260     MessageParcel reply;
261     MessageOption option;
262 
263     data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
264     int32_t error = Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CLEAR_USER_DATA, data, reply, option);
265     if (error != 0) {
266         INTELL_VOICE_LOG_ERROR("clear user data error: %{public}d", error);
267         return -1;
268     }
269     return reply.ReadInt32();
270 }
271 }  // namespace IntellVoiceEngine
272 }  // namespace OHOS
273