1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sensor/ISensorServer.h>
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <cutils/native_handle.h>
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Vector.h>
26#include <utils/Timers.h>
27
28#include <binder/Parcel.h>
29#include <binder/IInterface.h>
30#include <binder/IResultReceiver.h>
31
32#include <sensor/Sensor.h>
33#include <sensor/ISensorEventConnection.h>
34
35namespace android {
36// ----------------------------------------------------------------------------
37
38enum {
39    GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
40    CREATE_SENSOR_EVENT_CONNECTION,
41    ENABLE_DATA_INJECTION,
42    GET_DYNAMIC_SENSOR_LIST,
43    CREATE_SENSOR_DIRECT_CONNECTION,
44    SET_OPERATION_PARAMETER,
45};
46
47class BpSensorServer : public BpInterface<ISensorServer>
48{
49public:
50    explicit BpSensorServer(const sp<IBinder>& impl)
51        : BpInterface<ISensorServer>(impl)
52    {
53    }
54
55    virtual ~BpSensorServer();
56
57    virtual Vector<Sensor> getSensorList(const String16& opPackageName)
58    {
59        Parcel data, reply;
60        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
61        data.writeString16(opPackageName);
62        remote()->transact(GET_SENSOR_LIST, data, &reply);
63        Sensor s;
64        Vector<Sensor> v;
65        uint32_t n = reply.readUint32();
66        v.setCapacity(n);
67        while (n) {
68            n--;
69            reply.read(s);
70            v.add(s);
71        }
72        return v;
73    }
74
75    virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
76    {
77        Parcel data, reply;
78        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
79        data.writeString16(opPackageName);
80        remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
81        Sensor s;
82        Vector<Sensor> v;
83        uint32_t n = reply.readUint32();
84        v.setCapacity(n);
85        while (n) {
86            n--;
87            reply.read(s);
88            v.add(s);
89        }
90        return v;
91    }
92
93    virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
94             int mode, const String16& opPackageName)
95    {
96        Parcel data, reply;
97        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
98        data.writeString8(packageName);
99        data.writeInt32(mode);
100        data.writeString16(opPackageName);
101        remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
102        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
103    }
104
105    virtual int isDataInjectionEnabled() {
106        Parcel data, reply;
107        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
108        remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
109        return reply.readInt32();
110    }
111
112    virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
113            uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
114        Parcel data, reply;
115        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
116        data.writeString16(opPackageName);
117        data.writeUint32(size);
118        data.writeInt32(type);
119        data.writeInt32(format);
120        data.writeNativeHandle(resource);
121        remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
122        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
123    }
124
125    virtual int setOperationParameter(int32_t handle, int32_t type,
126                                      const Vector<float> &floats,
127                                      const Vector<int32_t> &ints) {
128        Parcel data, reply;
129        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
130        data.writeInt32(handle);
131        data.writeInt32(type);
132        data.writeUint32(static_cast<uint32_t>(floats.size()));
133        for (auto i : floats) {
134            data.writeFloat(i);
135        }
136        data.writeUint32(static_cast<uint32_t>(ints.size()));
137        for (auto i : ints) {
138            data.writeInt32(i);
139        }
140        remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
141        return reply.readInt32();
142    }
143};
144
145// Out-of-line virtual method definition to trigger vtable emission in this
146// translation unit (see clang warning -Wweak-vtables)
147BpSensorServer::~BpSensorServer() {}
148
149IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
150
151// ----------------------------------------------------------------------
152
153status_t BnSensorServer::onTransact(
154    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
155{
156    switch(code) {
157        case GET_SENSOR_LIST: {
158            CHECK_INTERFACE(ISensorServer, data, reply);
159            const String16& opPackageName = data.readString16();
160            Vector<Sensor> v(getSensorList(opPackageName));
161            size_t n = v.size();
162            reply->writeUint32(static_cast<uint32_t>(n));
163            for (size_t i = 0; i < n; i++) {
164                reply->write(v[i]);
165            }
166            return NO_ERROR;
167        }
168        case CREATE_SENSOR_EVENT_CONNECTION: {
169            CHECK_INTERFACE(ISensorServer, data, reply);
170            String8 packageName = data.readString8();
171            int32_t mode = data.readInt32();
172            const String16& opPackageName = data.readString16();
173            sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
174                    opPackageName));
175            reply->writeStrongBinder(IInterface::asBinder(connection));
176            return NO_ERROR;
177        }
178        case ENABLE_DATA_INJECTION: {
179            CHECK_INTERFACE(ISensorServer, data, reply);
180            int32_t ret = isDataInjectionEnabled();
181            reply->writeInt32(static_cast<int32_t>(ret));
182            return NO_ERROR;
183        }
184        case GET_DYNAMIC_SENSOR_LIST: {
185            CHECK_INTERFACE(ISensorServer, data, reply);
186            const String16& opPackageName = data.readString16();
187            Vector<Sensor> v(getDynamicSensorList(opPackageName));
188            size_t n = v.size();
189            reply->writeUint32(static_cast<uint32_t>(n));
190            for (size_t i = 0; i < n; i++) {
191                reply->write(v[i]);
192            }
193            return NO_ERROR;
194        }
195        case CREATE_SENSOR_DIRECT_CONNECTION: {
196            CHECK_INTERFACE(ISensorServer, data, reply);
197            const String16& opPackageName = data.readString16();
198            uint32_t size = data.readUint32();
199            int32_t type = data.readInt32();
200            int32_t format = data.readInt32();
201            native_handle_t *resource = data.readNativeHandle();
202            sp<ISensorEventConnection> ch =
203                    createSensorDirectConnection(opPackageName, size, type, format, resource);
204            native_handle_close(resource);
205            native_handle_delete(resource);
206            reply->writeStrongBinder(IInterface::asBinder(ch));
207            return NO_ERROR;
208        }
209        case SET_OPERATION_PARAMETER: {
210            CHECK_INTERFACE(ISensorServer, data, reply);
211            int32_t handle;
212            int32_t type;
213            Vector<float> floats;
214            Vector<int32_t> ints;
215
216            handle = data.readInt32();
217            type = data.readInt32();
218            floats.resize(data.readUint32());
219            for (auto &i : floats) {
220                i = data.readFloat();
221            }
222            ints.resize(data.readUint32());
223            for (auto &i : ints) {
224                i = data.readInt32();
225            }
226
227            int32_t ret = setOperationParameter(handle, type, floats, ints);
228            reply->writeInt32(ret);
229            return NO_ERROR;
230        }
231        case SHELL_COMMAND_TRANSACTION: {
232            int in = data.readFileDescriptor();
233            int out = data.readFileDescriptor();
234            int err = data.readFileDescriptor();
235            int argc = data.readInt32();
236            Vector<String16> args;
237            for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
238               args.add(data.readString16());
239            }
240            sp<IBinder> unusedCallback;
241            sp<IResultReceiver> resultReceiver;
242            status_t status;
243            if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
244                return status;
245            }
246            if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
247                return status;
248            }
249            status = shellCommand(in, out, err, args);
250            if (resultReceiver != nullptr) {
251                resultReceiver->send(status);
252            }
253            return NO_ERROR;
254        }
255    }
256    return BBinder::onTransact(code, data, reply, flags);
257}
258
259// ----------------------------------------------------------------------------
260}; // namespace android
261