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_engine_stub.h"
17 #include "securec.h"
18 #include "intell_voice_log.h"
19
20 #define LOG_TAG "IntellVoiceEngineStub"
21
22 namespace OHOS {
23 namespace IntellVoiceEngine {
IntellVoiceEngineStub()24 IntellVoiceEngineStub::IntellVoiceEngineStub()
25 {
26 processFuncMap_[INTELL_VOICE_ENGINE_SET_CALLBACK] = [this](MessageParcel &data,
27 MessageParcel &reply) -> int32_t { return this->SetCallbackInner(data, reply); };
28 processFuncMap_[INTELL_VOICE_ENGINE_ATTACH] = [this](MessageParcel &data,
29 MessageParcel &reply) -> int32_t { return this->AttachInner(data, reply); };
30 processFuncMap_[INTELL_VOICE_ENGINE_DETACH] = [this](MessageParcel &data,
31 MessageParcel &reply) -> int32_t { return this->DetachInner(data, reply); };
32 processFuncMap_[INTELL_VOICE_ENGINE_SET_PARAMETER] = [this](MessageParcel &data,
33 MessageParcel &reply) -> int32_t { return this->SetParameterInner(data, reply); };
34 processFuncMap_[INTELL_VOICE_ENGINE_GET_PARAMETER] = [this](MessageParcel &data,
35 MessageParcel &reply) -> int32_t { return this->GetParameterInner(data, reply); };
36 processFuncMap_[INTELL_VOICE_ENGINE_START] = [this](MessageParcel &data,
37 MessageParcel &reply) -> int32_t { return this->StartInner(data, reply); };
38 processFuncMap_[INTELL_VOICE_ENGINE_STOP] = [this](MessageParcel &data,
39 MessageParcel &reply) -> int32_t { return this->StopInner(data, reply); };
40 processFuncMap_[INTELL_VOICE_ENGINE_WRITE_AUDIO] = [this](MessageParcel &data,
41 MessageParcel &reply) -> int32_t { return this->WriteAudioInner(data, reply); };
42 processFuncMap_[INTELL_VOICE_ENGINE_STAET_CAPTURER] = [this](MessageParcel &data,
43 MessageParcel &reply) -> int32_t { return this->StartCapturerInner(data, reply); };
44 processFuncMap_[INTELL_VOICE_ENGINE_READ] = [this](MessageParcel &data,
45 MessageParcel &reply) -> int32_t { return this->ReadInner(data, reply); };
46 processFuncMap_[INTELL_VOICE_ENGINE_STOP_CAPTURER] = [this](MessageParcel &data,
47 MessageParcel &reply) -> int32_t { return this->StopCapturerInner(data, reply); };
48 processFuncMap_[INTELL_VOICE_ENGINE_GET_WAKEUP_PCM] = [this](MessageParcel &data,
49 MessageParcel &reply) -> int32_t { return this->GetWakeupPcmInner(data, reply); };
50 processFuncMap_[INTELL_VOICE_ENGINE_EVALUATE] = [this](MessageParcel &data,
51 MessageParcel &reply) -> int32_t { return this->EvaluateInner(data, reply); };
52 processFuncMap_[INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT] = [this](MessageParcel &data,
53 MessageParcel &reply) -> int32_t { return this->NotifyHeadSetWakeEventInner(data, reply); };
54 processFuncMap_[INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT] = [this](MessageParcel &data,
55 MessageParcel &reply) -> int32_t { return this->NotifyHeadSetHostEventInner(data, reply); };
56 }
57
~IntellVoiceEngineStub()58 IntellVoiceEngineStub::~IntellVoiceEngineStub()
59 {
60 processFuncMap_.clear();
61 }
62
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int32_t IntellVoiceEngineStub::OnRemoteRequest(uint32_t code,
64 MessageParcel &data, MessageParcel &reply, MessageOption &option)
65 {
66 if (data.ReadInterfaceToken() != IIntellVoiceEngine::GetDescriptor()) {
67 INTELL_VOICE_LOG_ERROR("token mismatch");
68 return -1;
69 }
70
71 auto it = processFuncMap_.find(code);
72 if ((it != processFuncMap_.end()) && (it->second != nullptr)) {
73 return it->second(data, reply);
74 }
75
76 INTELL_VOICE_LOG_WARN("invalid code:%{public}u", code);
77 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 }
79
SetCallbackInner(MessageParcel & data,MessageParcel &)80 int32_t IntellVoiceEngineStub::SetCallbackInner(MessageParcel &data, MessageParcel & /* reply */)
81 {
82 sptr<IRemoteObject> object = data.ReadRemoteObject();
83 SetCallback(object);
84 return 0;
85 }
86
AttachInner(MessageParcel & data,MessageParcel & reply)87 int32_t IntellVoiceEngineStub::AttachInner(MessageParcel &data, MessageParcel &reply)
88 {
89 IntellVoiceEngineInfo info = {
90 .wakeupPhrase = data.ReadString(),
91 .isPcmFromExternal = data.ReadBool(),
92 .minBufSize = data.ReadInt32(),
93 .sampleChannels = data.ReadInt32(),
94 .bitsPerSample = data.ReadInt32(),
95 .sampleRate = data.ReadInt32(),
96 };
97
98 int32_t ret = Attach(info);
99 reply.WriteInt32(ret);
100 return ret;
101 }
102
DetachInner(MessageParcel &,MessageParcel & reply)103 int32_t IntellVoiceEngineStub::DetachInner(MessageParcel & /* data */, MessageParcel &reply)
104 {
105 int32_t ret = Detach();
106 reply.WriteInt32(ret);
107 return ret;
108 }
109
SetParameterInner(MessageParcel & data,MessageParcel & reply)110 int32_t IntellVoiceEngineStub::SetParameterInner(MessageParcel &data, MessageParcel &reply)
111 {
112 int32_t ret = SetParameter(data.ReadString());
113 reply.WriteInt32(ret);
114 return ret;
115 }
116
GetParameterInner(MessageParcel & data,MessageParcel & reply)117 int32_t IntellVoiceEngineStub::GetParameterInner(MessageParcel &data, MessageParcel &reply)
118 {
119 std::string value = GetParameter(data.ReadString());
120 reply.WriteString(value);
121 return 0;
122 }
123
StartInner(MessageParcel & data,MessageParcel & reply)124 int32_t IntellVoiceEngineStub::StartInner(MessageParcel &data, MessageParcel &reply)
125 {
126 int32_t ret = Start(data.ReadBool());
127 reply.WriteInt32(ret);
128 return ret;
129 }
130
StopInner(MessageParcel &,MessageParcel & reply)131 int32_t IntellVoiceEngineStub::StopInner(MessageParcel & /* data */, MessageParcel &reply)
132 {
133 int32_t ret = Stop();
134 reply.WriteInt32(ret);
135 return ret;
136 }
137
WriteAudioInner(MessageParcel & data,MessageParcel & reply)138 int32_t IntellVoiceEngineStub::WriteAudioInner(MessageParcel &data, MessageParcel &reply)
139 {
140 int32_t size = data.ReadInt32();
141 if (size <= 0) {
142 INTELL_VOICE_LOG_ERROR("size(%{public}d) is invalid", size);
143 return -1;
144 }
145 const uint8_t *buffer = data.ReadBuffer(size);
146 if (buffer == nullptr) {
147 INTELL_VOICE_LOG_ERROR("buffer is nullptr");
148 return -1;
149 }
150
151 int32_t ret = WriteAudio(buffer, size);
152 reply.WriteInt32(ret);
153 return ret;
154 }
155
StartCapturerInner(MessageParcel & data,MessageParcel & reply)156 int32_t IntellVoiceEngineStub::StartCapturerInner(MessageParcel &data, MessageParcel &reply)
157 {
158 int32_t ret = StartCapturer(data.ReadInt32());
159 reply.WriteInt32(ret);
160 return ret;
161 }
162
ReadInner(MessageParcel & data,MessageParcel & reply)163 int32_t IntellVoiceEngineStub::ReadInner(MessageParcel &data, MessageParcel &reply)
164 {
165 std::vector<uint8_t> pcmData;
166 int32_t ret = Read(pcmData);
167 reply.WriteInt32(ret);
168 if (ret != 0) {
169 INTELL_VOICE_LOG_ERROR("failed to read wakeup pcm, ret:%{public}d", ret);
170 return ret;
171 }
172
173 reply.WriteUint32(pcmData.size());
174 reply.WriteBuffer(pcmData.data(), pcmData.size());
175 return ret;
176 }
177
StopCapturerInner(MessageParcel & data,MessageParcel & reply)178 int32_t IntellVoiceEngineStub::StopCapturerInner(MessageParcel &data, MessageParcel &reply)
179 {
180 int32_t ret = StopCapturer();
181 reply.WriteInt32(ret);
182 return ret;
183 }
184
GetWakeupPcmInner(MessageParcel & data,MessageParcel & reply)185 int32_t IntellVoiceEngineStub::GetWakeupPcmInner(MessageParcel &data, MessageParcel &reply)
186 {
187 std::vector<uint8_t> pcmData;
188 int32_t ret = GetWakeupPcm(pcmData);
189 reply.WriteInt32(ret);
190 if (ret != 0) {
191 INTELL_VOICE_LOG_ERROR("failed to get cloud pcm, ret:%{public}d", ret);
192 return ret;
193 }
194
195 reply.WriteUint32(pcmData.size());
196 reply.WriteBuffer(pcmData.data(), pcmData.size());
197 return ret;
198 }
199
EvaluateInner(MessageParcel & data,MessageParcel & reply)200 int32_t IntellVoiceEngineStub::EvaluateInner(MessageParcel &data, MessageParcel &reply)
201 {
202 EvaluationResult result;
203 int32_t ret = Evaluate(data.ReadString(), result);
204 reply.WriteInt32(ret);
205 if (ret != 0) {
206 INTELL_VOICE_LOG_ERROR("failed to evaluate");
207 return ret;
208 }
209 reply.WriteInt32(result.score);
210 reply.WriteInt32(result.resultCode);
211 return ret;
212 }
213
NotifyHeadSetWakeEventInner(MessageParcel & data,MessageParcel & reply)214 int32_t IntellVoiceEngineStub::NotifyHeadSetWakeEventInner(MessageParcel &data, MessageParcel &reply)
215 {
216 INTELL_VOICE_LOG_INFO("enter");
217 int32_t ret = NotifyHeadsetWakeEvent();
218 reply.WriteInt32(ret);
219 return ret;
220 }
221
NotifyHeadSetHostEventInner(MessageParcel & data,MessageParcel & reply)222 int32_t IntellVoiceEngineStub::NotifyHeadSetHostEventInner(MessageParcel &data, MessageParcel &reply)
223 {
224 INTELL_VOICE_LOG_INFO("enter");
225 int event = data.ReadInt32();
226 int32_t ret = NotifyHeadsetHostEvent(static_cast<OHOS::IntellVoiceEngine::HeadsetHostEventType>(event));
227 reply.WriteInt32(ret);
228 return ret;
229 }
230 }
231 }