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_stub.h"
17 #include "intell_voice_log.h"
18
19 #define LOG_TAG "IntellVoiceServiceStub"
20
21 namespace OHOS {
22 namespace IntellVoiceEngine {
IntellVoiceServiceStub()23 IntellVoiceServiceStub::IntellVoiceServiceStub()
24 {
25 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE] = [this](MessageParcel &data,
26 MessageParcel &reply) -> int32_t { return this->CreateEngineInner(data, reply); };
27 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE] = [this](MessageParcel &data,
28 MessageParcel &reply) -> int32_t { return this->ReleaseEngineInner(data, reply); };
29 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_GET_PARAMETER] = [this](MessageParcel &data,
30 MessageParcel &reply) -> int32_t { return this->GetParameterInner(data, reply); };
31 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_GET_REPORTED_FILES] = [this](MessageParcel &data,
32 MessageParcel &reply) -> int32_t { return this->GetReportedFilesInner(data, reply); };
33 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILES_LIST] = [this](MessageParcel &data,
34 MessageParcel &reply) -> int32_t { return this->GetCloneFileListInner(data, reply); };
35 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_GET_CLONE_FILE] = [this](MessageParcel &data,
36 MessageParcel &reply) -> int32_t { return this->GetCloneFileInner(data, reply); };
37 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_SEND_CLONE_FILE] = [this](MessageParcel &data,
38 MessageParcel &reply) -> int32_t { return this->SendCloneFileInner(data, reply); };
39 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_CLONE_FOR_RESULT] = [this](MessageParcel &data,
40 MessageParcel &reply) -> int32_t { return this->CloneForResultInner(data, reply); };
41 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_SET_PARAMETER] = [this](MessageParcel &data,
42 MessageParcel &reply) -> int32_t { return this->SetParameterInner(data, reply); };
43 processServiceFuncMap_[HDI_INTELL_VOICE_SERVICE_CLEAR_USER_DATA] = [this](MessageParcel &data,
44 MessageParcel &reply) -> int32_t { return this->ClearUserDataInner(data, reply); };
45 }
46
~IntellVoiceServiceStub()47 IntellVoiceServiceStub::~IntellVoiceServiceStub()
48 {
49 processServiceFuncMap_.clear();
50 }
51
CreateEngineInner(MessageParcel & data,MessageParcel & reply)52 int32_t IntellVoiceServiceStub::CreateEngineInner(MessageParcel &data, MessageParcel &reply)
53 {
54 int32_t ret = 0;
55 sptr<IIntellVoiceEngine> engine = nullptr;
56
57 IntellVoiceEngineType type = static_cast<IntellVoiceEngineType>(data.ReadInt32());
58 auto obj = data.ReadRemoteObject();
59 RegisterDeathRecipient(type, obj);
60 if (type == INTELL_VOICE_HEADSET_WAKEUP) {
61 type = INTELL_VOICE_WAKEUP;
62 }
63 ret = CreateIntellVoiceEngine(type, engine);
64 reply.WriteInt32(ret);
65 if (ret != 0) {
66 INTELL_VOICE_LOG_ERROR("failed to create engine, type:%{public}d", type);
67 return ret;
68 }
69 if (engine != nullptr) {
70 reply.WriteRemoteObject(engine->AsObject());
71 }
72
73 return ret;
74 }
75
ReleaseEngineInner(MessageParcel & data,MessageParcel & reply)76 int32_t IntellVoiceServiceStub::ReleaseEngineInner(MessageParcel &data, MessageParcel &reply)
77 {
78 int32_t ret = 0;
79
80 IntellVoiceEngineType type = static_cast<IntellVoiceEngineType>(data.ReadInt32());
81 ret = ReleaseIntellVoiceEngine(type);
82 reply.WriteInt32(ret);
83 DeregisterDeathRecipient(type);
84 return ret;
85 }
86
GetReportedFilesInner(MessageParcel & data,MessageParcel & reply)87 int32_t IntellVoiceServiceStub::GetReportedFilesInner(MessageParcel &data, MessageParcel &reply)
88 {
89 int32_t ret = 0;
90 int numMax = data.ReadInt32();
91 std::vector<UploadFilesFromHdi> Files;
92 ret = GetUploadFiles(numMax, Files);
93 reply.WriteInt32(ret);
94 if (ret != 0) {
95 INTELL_VOICE_LOG_ERROR("get upload files failed, ret:%{public}d", ret);
96 return ret;
97 }
98 int size = static_cast<int>(Files.size());
99 reply.WriteInt32(size);
100 INTELL_VOICE_LOG_INFO("reported hdi files size:%{public}d", size);
101 if (size <= 0) {
102 return -1;
103 }
104 for (auto file : Files) {
105 reply.WriteInt32(file.type);
106 reply.WriteString(file.filesDescription);
107 reply.WriteInt32(file.filesContent.size());
108 for (auto ashmem : file.filesContent) {
109 reply.WriteAshmem(ashmem);
110 }
111 }
112 return ret;
113 }
114
GetParameterInner(MessageParcel & data,MessageParcel & reply)115 int32_t IntellVoiceServiceStub::GetParameterInner(MessageParcel &data, MessageParcel &reply)
116 {
117 std::string val = GetParameter(data.ReadString());
118 reply.WriteString(val);
119
120 return 0;
121 }
122
SetParameterInner(MessageParcel & data,MessageParcel & reply)123 int32_t IntellVoiceServiceStub::SetParameterInner(MessageParcel &data, MessageParcel &reply)
124 {
125 int32_t ret = SetParameter(data.ReadString());
126 reply.WriteInt32(ret);
127 return ret;
128 }
129
GetCloneFileListInner(MessageParcel & data,MessageParcel & reply)130 int32_t IntellVoiceServiceStub::GetCloneFileListInner(MessageParcel &data, MessageParcel &reply)
131 {
132 int32_t ret = 0;
133 std::vector<std::string> cloneFiles;
134 ret = GetWakeupSourceFilesList(cloneFiles);
135 reply.WriteInt32(ret);
136 if (ret != 0) {
137 INTELL_VOICE_LOG_ERROR("get clone files list failed, ret:%{public}d", ret);
138 return ret;
139 }
140
141 reply.WriteUint32(cloneFiles.size());
142 for (auto i : cloneFiles) {
143 reply.WriteString(i);
144 }
145
146 return ret;
147 }
148
GetCloneFileInner(MessageParcel & data,MessageParcel & reply)149 int32_t IntellVoiceServiceStub::GetCloneFileInner(MessageParcel &data, MessageParcel &reply)
150 {
151 std::string filePath = data.ReadString();
152 std::vector<uint8_t> buffer;
153 int32_t ret = GetWakeupSourceFile(filePath, buffer);
154 reply.WriteInt32(ret);
155 if (ret != 0) {
156 INTELL_VOICE_LOG_ERROR("get clone file failed, ret:%{public}d", ret);
157 return ret;
158 }
159
160 reply.WriteUint32(buffer.size());
161 reply.WriteBuffer(buffer.data(), buffer.size());
162 return ret;
163 }
164
SendCloneFileInner(MessageParcel & data,MessageParcel & reply)165 int32_t IntellVoiceServiceStub::SendCloneFileInner(MessageParcel &data, MessageParcel &reply)
166 {
167 std::string filePath = data.ReadString();
168 std::vector<uint8_t> buffer;
169 uint32_t size = data.ReadUint32();
170 if (size == 0) {
171 INTELL_VOICE_LOG_ERROR("invalid size");
172 return -1;
173 }
174
175 const uint8_t *readBuf = data.ReadBuffer(size);
176 if (readBuf == nullptr) {
177 INTELL_VOICE_LOG_ERROR("read buffer is nullptr");
178 return -1;
179 }
180
181 buffer.resize(size);
182 std::copy(readBuf, readBuf + size, buffer.data());
183 int32_t ret = SendWakeupFile(filePath, buffer);
184 reply.WriteInt32(ret);
185 return ret;
186 }
187
CloneForResultInner(MessageParcel & data,MessageParcel & reply)188 int32_t IntellVoiceServiceStub::CloneForResultInner(MessageParcel &data, MessageParcel &reply)
189 {
190 int32_t ret = 0;
191 std::string wakeupInfo = data.ReadString();
192 sptr<IRemoteObject> object = data.ReadRemoteObject();
193
194 ret = EnrollWithWakeupFilesForResult(wakeupInfo, object);
195 reply.WriteInt32(ret);
196 return ret;
197 }
198
ClearUserDataInner(MessageParcel & data,MessageParcel & reply)199 int32_t IntellVoiceServiceStub::ClearUserDataInner(MessageParcel &data, MessageParcel &reply)
200 {
201 int32_t ret = ClearUserData();
202 reply.WriteInt32(ret);
203 return ret;
204 }
205
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)206 int32_t IntellVoiceServiceStub::OnRemoteRequest(
207 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
208 {
209 if (data.ReadInterfaceToken() != IIntellVoiceService::GetDescriptor()) {
210 INTELL_VOICE_LOG_ERROR("token mismatch");
211 return -1;
212 }
213
214 auto it = processServiceFuncMap_.find(code);
215 if ((it != processServiceFuncMap_.end()) && (it->second != nullptr)) {
216 return it->second(data, reply);
217 }
218
219 INTELL_VOICE_LOG_WARN("sevice stub invalid code:%{public}u", code);
220 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
221 }
222 } // namespace IntellVoiceEngine
223 } // namespace OHOS
224