summaryrefslogtreecommitdiffstats
path: root/soundtrigger/ISoundTrigger.cpp
blob: eecc1ea43dbc0af80eff3b8abe043f0f648f4a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
**
** Copyright 2014, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#define LOG_TAG "ISoundTrigger"
#include <utils/Log.h>
#include <utils/Errors.h>
#include <binder/IMemory.h>
#include <soundtrigger/ISoundTrigger.h>
#include <soundtrigger/ISoundTriggerHwService.h>
#include <soundtrigger/ISoundTriggerClient.h>
#include <system/sound_trigger.h>

namespace android {

enum {
    DETACH = IBinder::FIRST_CALL_TRANSACTION,
    LOAD_SOUND_MODEL,
    UNLOAD_SOUND_MODEL,
    START_RECOGNITION,
    STOP_RECOGNITION,
};

class BpSoundTrigger: public BpInterface<ISoundTrigger>
{
public:
    BpSoundTrigger(const sp<IBinder>& impl)
        : BpInterface<ISoundTrigger>(impl)
    {
    }

    void detach()
    {
        ALOGV("detach");
        Parcel data, reply;
        data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
        remote()->transact(DETACH, data, &reply);
    }

    status_t loadSoundModel(const sp<IMemory>&  modelMemory,
                                    sound_model_handle_t *handle)
    {
        if (modelMemory == 0 || handle == NULL) {
            return BAD_VALUE;
        }
        Parcel data, reply;
        data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
        data.writeStrongBinder(IInterface::asBinder(modelMemory));
        status_t status = remote()->transact(LOAD_SOUND_MODEL, data, &reply);
        if (status != NO_ERROR ||
                (status = (status_t)reply.readInt32()) != NO_ERROR) {
            return status;
        }
        reply.read(handle, sizeof(sound_model_handle_t));
        return status;
    }

    virtual status_t unloadSoundModel(sound_model_handle_t handle)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
        data.write(&handle, sizeof(sound_model_handle_t));
        status_t status = remote()->transact(UNLOAD_SOUND_MODEL, data, &reply);
        if (status != NO_ERROR) {
            status = (status_t)reply.readInt32();
        }
        return status;
    }

    virtual status_t startRecognition(sound_model_handle_t handle,
                                      const sp<IMemory>& dataMemory)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
        data.write(&handle, sizeof(sound_model_handle_t));
        if (dataMemory == 0) {
            data.writeInt32(0);
        } else {
            data.writeInt32(dataMemory->size());
        }
        data.writeStrongBinder(IInterface::asBinder(dataMemory));
        status_t status = remote()->transact(START_RECOGNITION, data, &reply);
        if (status != NO_ERROR) {
            status = (status_t)reply.readInt32();
        }
        return status;
    }

    virtual status_t stopRecognition(sound_model_handle_t handle)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
        data.write(&handle, sizeof(sound_model_handle_t));
        status_t status = remote()->transact(STOP_RECOGNITION, data, &reply);
        if (status != NO_ERROR) {
            status = (status_t)reply.readInt32();
        }
        return status;
    }

};

IMPLEMENT_META_INTERFACE(SoundTrigger, "android.hardware.ISoundTrigger");

// ----------------------------------------------------------------------

status_t BnSoundTrigger::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case DETACH: {
            ALOGV("DETACH");
            CHECK_INTERFACE(ISoundTrigger, data, reply);
            detach();
            return NO_ERROR;
        } break;
        case LOAD_SOUND_MODEL: {
            CHECK_INTERFACE(ISoundTrigger, data, reply);
            sp<IMemory> modelMemory = interface_cast<IMemory>(
                data.readStrongBinder());
            sound_model_handle_t handle;
            status_t status = loadSoundModel(modelMemory, &handle);
            reply->writeInt32(status);
            if (status == NO_ERROR) {
                reply->write(&handle, sizeof(sound_model_handle_t));
            }
            return NO_ERROR;
        }
        case UNLOAD_SOUND_MODEL: {
            CHECK_INTERFACE(ISoundTrigger, data, reply);
            sound_model_handle_t handle;
            data.read(&handle, sizeof(sound_model_handle_t));
            status_t status = unloadSoundModel(handle);
            reply->writeInt32(status);
            return NO_ERROR;
        }
        case START_RECOGNITION: {
            CHECK_INTERFACE(ISoundTrigger, data, reply);
            sound_model_handle_t handle;
            data.read(&handle, sizeof(sound_model_handle_t));
            sp<IMemory> dataMemory;
            if (data.readInt32() != 0) {
                dataMemory = interface_cast<IMemory>(data.readStrongBinder());
            }
            status_t status = startRecognition(handle, dataMemory);
            reply->writeInt32(status);
            return NO_ERROR;
        }
        case STOP_RECOGNITION: {
            CHECK_INTERFACE(ISoundTrigger, data, reply);
            sound_model_handle_t handle;
            data.read(&handle, sizeof(sound_model_handle_t));
            status_t status = stopRecognition(handle);
            reply->writeInt32(status);
            return NO_ERROR;
        }
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

// ----------------------------------------------------------------------------

}; // namespace android