blob: 32882f1ef230cb262d54acf7bfe8896fe7afcb50 [file] [log] [blame]
Eric Laurentb7a11d82014-04-18 17:40:41 -07001/*
2**
3** Copyright 2014, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "ISoundTrigger"
19#include <utils/Log.h>
20#include <utils/Errors.h>
21#include <binder/IMemory.h>
22#include <soundtrigger/ISoundTrigger.h>
23#include <soundtrigger/ISoundTriggerHwService.h>
24#include <soundtrigger/ISoundTriggerClient.h>
25#include <system/sound_trigger.h>
26
27namespace android {
28
29enum {
30 DETACH = IBinder::FIRST_CALL_TRANSACTION,
31 LOAD_SOUND_MODEL,
32 UNLOAD_SOUND_MODEL,
33 START_RECOGNITION,
34 STOP_RECOGNITION,
Michael Dooley67e3d412018-10-16 19:51:16 +000035 GET_MODEL_STATE,
Eric Laurentb7a11d82014-04-18 17:40:41 -070036};
37
38class BpSoundTrigger: public BpInterface<ISoundTrigger>
39{
40public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070041 explicit BpSoundTrigger(const sp<IBinder>& impl)
Eric Laurentb7a11d82014-04-18 17:40:41 -070042 : BpInterface<ISoundTrigger>(impl)
43 {
44 }
45
46 void detach()
47 {
48 ALOGV("detach");
49 Parcel data, reply;
50 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
51 remote()->transact(DETACH, data, &reply);
52 }
53
54 status_t loadSoundModel(const sp<IMemory>& modelMemory,
55 sound_model_handle_t *handle)
56 {
57 if (modelMemory == 0 || handle == NULL) {
58 return BAD_VALUE;
59 }
60 Parcel data, reply;
61 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
Marco Nelissen06b46062014-11-14 07:58:25 -080062 data.writeStrongBinder(IInterface::asBinder(modelMemory));
Eric Laurentb7a11d82014-04-18 17:40:41 -070063 status_t status = remote()->transact(LOAD_SOUND_MODEL, data, &reply);
Eric Laurent014620f2015-10-05 17:21:00 -070064 if (status != NO_ERROR) {
Eric Laurentb7a11d82014-04-18 17:40:41 -070065 return status;
66 }
Eric Laurent014620f2015-10-05 17:21:00 -070067 status = (status_t)reply.readInt32();
68 if (status == NO_ERROR) {
69 reply.read(handle, sizeof(sound_model_handle_t));
70 }
Eric Laurentb7a11d82014-04-18 17:40:41 -070071 return status;
72 }
73
74 virtual status_t unloadSoundModel(sound_model_handle_t handle)
75 {
76 Parcel data, reply;
77 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
78 data.write(&handle, sizeof(sound_model_handle_t));
79 status_t status = remote()->transact(UNLOAD_SOUND_MODEL, data, &reply);
Eric Laurent014620f2015-10-05 17:21:00 -070080 if (status == NO_ERROR) {
Eric Laurentb7a11d82014-04-18 17:40:41 -070081 status = (status_t)reply.readInt32();
82 }
83 return status;
84 }
85
86 virtual status_t startRecognition(sound_model_handle_t handle,
87 const sp<IMemory>& dataMemory)
88 {
89 Parcel data, reply;
90 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
91 data.write(&handle, sizeof(sound_model_handle_t));
92 if (dataMemory == 0) {
93 data.writeInt32(0);
94 } else {
95 data.writeInt32(dataMemory->size());
96 }
Marco Nelissen06b46062014-11-14 07:58:25 -080097 data.writeStrongBinder(IInterface::asBinder(dataMemory));
Eric Laurentb7a11d82014-04-18 17:40:41 -070098 status_t status = remote()->transact(START_RECOGNITION, data, &reply);
Eric Laurent014620f2015-10-05 17:21:00 -070099 if (status == NO_ERROR) {
Eric Laurentb7a11d82014-04-18 17:40:41 -0700100 status = (status_t)reply.readInt32();
101 }
102 return status;
103 }
104
105 virtual status_t stopRecognition(sound_model_handle_t handle)
106 {
107 Parcel data, reply;
108 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
109 data.write(&handle, sizeof(sound_model_handle_t));
110 status_t status = remote()->transact(STOP_RECOGNITION, data, &reply);
Eric Laurent014620f2015-10-05 17:21:00 -0700111 if (status == NO_ERROR) {
Eric Laurentb7a11d82014-04-18 17:40:41 -0700112 status = (status_t)reply.readInt32();
113 }
114 return status;
115 }
116
Michael Dooley67e3d412018-10-16 19:51:16 +0000117 virtual status_t getModelState(sound_model_handle_t handle,
118 sp<IMemory>& eventMemory)
119 {
120 Parcel data, reply;
121 data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
122 data.write(&handle, sizeof(sound_model_handle_t));
123 status_t status = remote()->transact(GET_MODEL_STATE, data, &reply);
124 if (status == NO_ERROR) {
125 status = (status_t)reply.readInt32();
126 if (status == NO_ERROR) {
127 eventMemory = interface_cast<IMemory>(reply.readStrongBinder());
128 }
129 }
130 return status;
131 }
132
Eric Laurentb7a11d82014-04-18 17:40:41 -0700133};
134
135IMPLEMENT_META_INTERFACE(SoundTrigger, "android.hardware.ISoundTrigger");
136
137// ----------------------------------------------------------------------
138
139status_t BnSoundTrigger::onTransact(
140 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
141{
142 switch(code) {
143 case DETACH: {
144 ALOGV("DETACH");
145 CHECK_INTERFACE(ISoundTrigger, data, reply);
146 detach();
147 return NO_ERROR;
148 } break;
149 case LOAD_SOUND_MODEL: {
150 CHECK_INTERFACE(ISoundTrigger, data, reply);
151 sp<IMemory> modelMemory = interface_cast<IMemory>(
152 data.readStrongBinder());
153 sound_model_handle_t handle;
154 status_t status = loadSoundModel(modelMemory, &handle);
155 reply->writeInt32(status);
156 if (status == NO_ERROR) {
157 reply->write(&handle, sizeof(sound_model_handle_t));
158 }
159 return NO_ERROR;
160 }
161 case UNLOAD_SOUND_MODEL: {
162 CHECK_INTERFACE(ISoundTrigger, data, reply);
163 sound_model_handle_t handle;
164 data.read(&handle, sizeof(sound_model_handle_t));
165 status_t status = unloadSoundModel(handle);
166 reply->writeInt32(status);
167 return NO_ERROR;
168 }
169 case START_RECOGNITION: {
170 CHECK_INTERFACE(ISoundTrigger, data, reply);
171 sound_model_handle_t handle;
172 data.read(&handle, sizeof(sound_model_handle_t));
173 sp<IMemory> dataMemory;
174 if (data.readInt32() != 0) {
175 dataMemory = interface_cast<IMemory>(data.readStrongBinder());
176 }
177 status_t status = startRecognition(handle, dataMemory);
178 reply->writeInt32(status);
179 return NO_ERROR;
180 }
181 case STOP_RECOGNITION: {
182 CHECK_INTERFACE(ISoundTrigger, data, reply);
183 sound_model_handle_t handle;
184 data.read(&handle, sizeof(sound_model_handle_t));
185 status_t status = stopRecognition(handle);
186 reply->writeInt32(status);
187 return NO_ERROR;
188 }
Michael Dooley67e3d412018-10-16 19:51:16 +0000189 case GET_MODEL_STATE: {
190 CHECK_INTERFACE(ISoundTrigger, data, reply);
191 sound_model_handle_t handle;
192 status_t status = UNKNOWN_ERROR;
193 status_t ret = data.read(&handle, sizeof(sound_model_handle_t));
194 if (ret == NO_ERROR) {
195 sp<IMemory> eventMemory;
196 status = getModelState(handle, eventMemory);
197 if (eventMemory != NULL) {
198 ret = reply->writeStrongBinder(
199 IInterface::asBinder(eventMemory));
200 } else {
201 ret = NO_MEMORY;
202 }
203 }
204 reply->writeInt32(status);
205 return ret;
206 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700207 default:
208 return BBinder::onTransact(code, data, reply, flags);
209 }
210}
211
212// ----------------------------------------------------------------------------
213
214}; // namespace android