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