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_proxy.h"
17 #include "intell_voice_log.h"
18
19 #define LOG_TAG "IntellVoiceEngineProxy"
20
21 namespace OHOS {
22 namespace IntellVoiceEngine {
SetCallback(sptr<IRemoteObject> object)23 void IntellVoiceEngineProxy::SetCallback(sptr<IRemoteObject> object)
24 {
25 MessageParcel data;
26 MessageParcel reply;
27 MessageOption option;
28 if (object == nullptr) {
29 INTELL_VOICE_LOG_ERROR("object is null");
30 return;
31 }
32
33 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
34 data.WriteRemoteObject(object);
35 Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_CALLBACK, data, reply, option);
36 }
37
Attach(const IntellVoiceEngineInfo & info)38 int32_t IntellVoiceEngineProxy::Attach(const IntellVoiceEngineInfo &info)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option;
43
44 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
45 data.WriteString(info.wakeupPhrase);
46 data.WriteBool(info.isPcmFromExternal);
47 data.WriteInt32(info.minBufSize);
48 data.WriteInt32(info.sampleChannels);
49 data.WriteInt32(info.bitsPerSample);
50 data.WriteInt32(info.sampleRate);
51
52 Remote()->SendRequest(INTELL_VOICE_ENGINE_ATTACH, data, reply, option);
53
54 INTELL_VOICE_LOG_INFO("Attach call");
55 return reply.ReadInt32();
56 }
57
Detach(void)58 int32_t IntellVoiceEngineProxy::Detach(void)
59 {
60 MessageParcel data;
61 MessageParcel reply;
62 MessageOption option;
63
64 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
65 Remote()->SendRequest(INTELL_VOICE_ENGINE_DETACH, data, reply, option);
66 return reply.ReadInt32();
67 }
68
SetParameter(const std::string & keyValueList)69 int32_t IntellVoiceEngineProxy::SetParameter(const std::string &keyValueList)
70 {
71 MessageParcel data;
72 MessageParcel reply;
73 MessageOption option;
74
75 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
76 data.WriteString(keyValueList);
77 Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_PARAMETER, data, reply, option);
78 return reply.ReadInt32();
79 }
80
GetParameter(const std::string & key)81 std::string IntellVoiceEngineProxy::GetParameter(const std::string &key)
82 {
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option;
86
87 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
88 data.WriteString(key);
89
90 Remote()->SendRequest(INTELL_VOICE_ENGINE_GET_PARAMETER, data, reply, option);
91 return reply.ReadString();
92 }
93
Start(bool isLast)94 int32_t IntellVoiceEngineProxy::Start(bool isLast)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option;
99
100 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
101 data.WriteBool(isLast);
102
103 Remote()->SendRequest(INTELL_VOICE_ENGINE_START, data, reply, option);
104 return reply.ReadInt32();
105 }
106
Stop(void)107 int32_t IntellVoiceEngineProxy::Stop(void)
108 {
109 MessageParcel data;
110 MessageParcel reply;
111 MessageOption option;
112
113 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
114
115 Remote()->SendRequest(INTELL_VOICE_ENGINE_STOP, data, reply, option);
116 return reply.ReadInt32();
117 }
118
WriteAudio(const uint8_t * buffer,uint32_t size)119 int32_t IntellVoiceEngineProxy::WriteAudio(const uint8_t *buffer, uint32_t size)
120 {
121 MessageParcel data;
122 MessageParcel reply;
123 MessageOption option;
124
125 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
126 data.WriteUint32(size);
127 data.WriteBuffer(buffer, size);
128
129 Remote()->SendRequest(INTELL_VOICE_ENGINE_WRITE_AUDIO, data, reply, option);
130 return reply.ReadInt32();
131 }
132
StartCapturer(int32_t channels)133 int32_t IntellVoiceEngineProxy::StartCapturer(int32_t channels)
134 {
135 MessageParcel data;
136 MessageParcel reply;
137 MessageOption option;
138
139 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
140 data.WriteInt32(channels);
141 Remote()->SendRequest(INTELL_VOICE_ENGINE_STAET_CAPTURER, data, reply, option);
142 return reply.ReadInt32();
143 }
144
Read(std::vector<uint8_t> & data)145 int32_t IntellVoiceEngineProxy::Read(std::vector<uint8_t> &data)
146 {
147 MessageParcel parcelData;
148 MessageParcel reply;
149 MessageOption option;
150
151 parcelData.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
152 Remote()->SendRequest(INTELL_VOICE_ENGINE_READ, parcelData, reply, option);
153
154 int ret = reply.ReadInt32();
155 if (ret != 0) {
156 INTELL_VOICE_LOG_ERROR("failed to read wakeup pcm, ret:%{public}d", ret);
157 return ret;
158 }
159 uint32_t size = reply.ReadUint32();
160 if (size == 0) {
161 INTELL_VOICE_LOG_ERROR("buffer size is zero");
162 return -1;
163 }
164 const uint8_t *buff = reply.ReadBuffer(size);
165 if (buff == nullptr) {
166 INTELL_VOICE_LOG_ERROR("buffer is nullptr");
167 return -1;
168 }
169 data.resize(size);
170 std::copy(buff, buff + size, data.begin());
171 return ret;
172 }
173
StopCapturer()174 int32_t IntellVoiceEngineProxy::StopCapturer()
175 {
176 MessageParcel data;
177 MessageParcel reply;
178 MessageOption option;
179
180 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
181 Remote()->SendRequest(INTELL_VOICE_ENGINE_STOP_CAPTURER, data, reply, option);
182 return reply.ReadInt32();
183 }
184
GetWakeupPcm(std::vector<uint8_t> & data)185 int32_t IntellVoiceEngineProxy::GetWakeupPcm(std::vector<uint8_t> &data)
186 {
187 MessageParcel parcelData;
188 MessageParcel reply;
189 MessageOption option;
190
191 parcelData.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
192 Remote()->SendRequest(INTELL_VOICE_ENGINE_GET_WAKEUP_PCM, parcelData, reply, option);
193 int ret = reply.ReadInt32();
194 if (ret != 0) {
195 INTELL_VOICE_LOG_ERROR("failed to get wakeup pcm, ret:%{public}d", ret);
196 return ret;
197 }
198 uint32_t size = reply.ReadUint32();
199 if (size == 0) {
200 INTELL_VOICE_LOG_ERROR("buffer size is zero");
201 return -1;
202 }
203 const uint8_t *buff = reply.ReadBuffer(size);
204 if (buff == nullptr) {
205 INTELL_VOICE_LOG_ERROR("buffer is nullptr");
206 return -1;
207 }
208 data.resize(size);
209 std::copy(buff, buff + size, data.begin());
210 return ret;
211 }
212
Evaluate(const std::string & word,EvaluationResultInfo & info)213 int32_t IntellVoiceEngineProxy::Evaluate(const std::string &word, EvaluationResultInfo &info)
214 {
215 MessageParcel data;
216 MessageParcel reply;
217 MessageOption option;
218
219 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
220 data.WriteString(word);
221 Remote()->SendRequest(INTELL_VOICE_ENGINE_EVALUATE, data, reply, option);
222 int32_t ret = reply.ReadInt32();
223 if (ret != 0) {
224 INTELL_VOICE_LOG_ERROR("failed to evaluate");
225 return ret;
226 }
227
228 info.score = reply.ReadInt32();
229 info.resultCode = static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_2::EvaluationResultCode>(reply.ReadInt32());
230 return ret;
231 }
232
NotifyHeadsetWakeEvent()233 int32_t IntellVoiceEngineProxy::NotifyHeadsetWakeEvent()
234 {
235 INTELL_VOICE_LOG_ERROR("enter");
236 MessageParcel data;
237 MessageParcel reply;
238 MessageOption option;
239
240 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
241 Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT, data, reply, option);
242 return reply.ReadInt32();
243 }
244
NotifyHeadsetHostEvent(HeadsetHostEventType event)245 int32_t IntellVoiceEngineProxy::NotifyHeadsetHostEvent(HeadsetHostEventType event)
246 {
247 INTELL_VOICE_LOG_ERROR("enter");
248 MessageParcel data;
249 MessageParcel reply;
250 MessageOption option;
251
252 data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
253 data.WriteInt32(event);
254 Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT, data, reply, option);
255 return reply.ReadInt32();
256 }
257 }
258 }
259