blob: efbbf7d8266e8051da0e50904cad3fdf61365037 [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
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
Mathias Agopian801ea092017-03-06 15:05:04 -080017#include <sensor/ISensorServer.h>
18
Mathias Agopian589ce852010-07-13 22:21:56 -070019#include <stdint.h>
20#include <sys/types.h>
21
Peng Xue36e3472016-11-03 11:57:10 -070022#include <cutils/native_handle.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070023#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
Mathias Agopian801ea092017-03-06 15:05:04 -080031#include <sensor/Sensor.h>
32#include <sensor/ISensorEventConnection.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070033
34namespace android {
35// ----------------------------------------------------------------------------
36
37enum {
38 GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
39 CREATE_SENSOR_EVENT_CONNECTION,
Peng Xu2576cb62016-01-20 00:22:09 -080040 ENABLE_DATA_INJECTION,
41 GET_DYNAMIC_SENSOR_LIST,
Peng Xue36e3472016-11-03 11:57:10 -070042 CREATE_SENSOR_DIRECT_CONNECTION,
Peng Xudd5c5cb2017-03-16 17:39:43 -070043 SET_OPERATION_PARAMETER,
Mathias Agopian589ce852010-07-13 22:21:56 -070044};
45
46class BpSensorServer : public BpInterface<ISensorServer>
47{
48public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070049 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070050 : BpInterface<ISensorServer>(impl)
51 {
52 }
53
Dan Stozad723bd72014-11-18 10:24:03 -080054 virtual ~BpSensorServer();
55
Svetoslavb412f6e2015-04-29 16:50:41 -070056 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070057 {
58 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070059 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070060 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070061 remote()->transact(GET_SENSOR_LIST, data, &reply);
62 Sensor s;
63 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080064 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070065 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -080066 while (n) {
67 n--;
Mathias Agopian8683fca2012-08-12 19:37:16 -070068 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070069 v.add(s);
70 }
71 return v;
72 }
73
Peng Xu2576cb62016-01-20 00:22:09 -080074 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
75 {
76 Parcel data, reply;
77 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
78 data.writeString16(opPackageName);
79 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
80 Sensor s;
81 Vector<Sensor> v;
82 uint32_t n = reply.readUint32();
83 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -080084 while (n) {
85 n--;
Peng Xu2576cb62016-01-20 00:22:09 -080086 reply.read(s);
87 v.add(s);
88 }
89 return v;
90 }
91
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070092 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070093 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070094 {
95 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070096 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080097 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070098 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070099 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -0700100 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
101 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
102 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700103
Aravind Akella5c538052015-06-29 12:37:48 -0700104 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700105 Parcel data, reply;
106 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700107 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
108 return reply.readInt32();
109 }
Peng Xue36e3472016-11-03 11:57:10 -0700110
111 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
112 uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
113 Parcel data, reply;
114 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
115 data.writeString16(opPackageName);
116 data.writeUint32(size);
117 data.writeInt32(type);
118 data.writeInt32(format);
119 data.writeNativeHandle(resource);
120 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
121 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
122 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700123
Alexey Polyudov88711e82017-05-23 19:54:04 -0700124 virtual int setOperationParameter(int32_t handle, int32_t type,
125 const Vector<float> &floats,
126 const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700127 Parcel data, reply;
128 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Alexey Polyudov88711e82017-05-23 19:54:04 -0700129 data.writeInt32(handle);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700130 data.writeInt32(type);
131 data.writeUint32(static_cast<uint32_t>(floats.size()));
132 for (auto i : floats) {
133 data.writeFloat(i);
134 }
135 data.writeUint32(static_cast<uint32_t>(ints.size()));
136 for (auto i : ints) {
137 data.writeInt32(i);
138 }
139 remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
140 return reply.readInt32();
141 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700142};
143
Dan Stozad723bd72014-11-18 10:24:03 -0800144// Out-of-line virtual method definition to trigger vtable emission in this
145// translation unit (see clang warning -Wweak-vtables)
146BpSensorServer::~BpSensorServer() {}
147
Mathias Agopian589ce852010-07-13 22:21:56 -0700148IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
149
150// ----------------------------------------------------------------------
151
152status_t BnSensorServer::onTransact(
153 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
154{
155 switch(code) {
156 case GET_SENSOR_LIST: {
157 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700158 const String16& opPackageName = data.readString16();
159 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700160 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800161 reply->writeUint32(static_cast<uint32_t>(n));
162 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700163 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700164 }
165 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800166 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700167 case CREATE_SENSOR_EVENT_CONNECTION: {
168 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800169 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700170 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700171 const String16& opPackageName = data.readString16();
172 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
173 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800174 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700175 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800176 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700177 case ENABLE_DATA_INJECTION: {
178 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700179 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700180 reply->writeInt32(static_cast<int32_t>(ret));
181 return NO_ERROR;
182 }
Peng Xu2576cb62016-01-20 00:22:09 -0800183 case GET_DYNAMIC_SENSOR_LIST: {
184 CHECK_INTERFACE(ISensorServer, data, reply);
185 const String16& opPackageName = data.readString16();
186 Vector<Sensor> v(getDynamicSensorList(opPackageName));
187 size_t n = v.size();
188 reply->writeUint32(static_cast<uint32_t>(n));
189 for (size_t i = 0; i < n; i++) {
190 reply->write(v[i]);
191 }
192 return NO_ERROR;
193 }
Peng Xue36e3472016-11-03 11:57:10 -0700194 case CREATE_SENSOR_DIRECT_CONNECTION: {
195 CHECK_INTERFACE(ISensorServer, data, reply);
196 const String16& opPackageName = data.readString16();
197 uint32_t size = data.readUint32();
198 int32_t type = data.readInt32();
199 int32_t format = data.readInt32();
200 native_handle_t *resource = data.readNativeHandle();
201 sp<ISensorEventConnection> ch =
202 createSensorDirectConnection(opPackageName, size, type, format, resource);
203 native_handle_close(resource);
204 native_handle_delete(resource);
205 reply->writeStrongBinder(IInterface::asBinder(ch));
206 return NO_ERROR;
207 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700208 case SET_OPERATION_PARAMETER: {
209 CHECK_INTERFACE(ISensorServer, data, reply);
Alexey Polyudov88711e82017-05-23 19:54:04 -0700210 int32_t handle;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700211 int32_t type;
212 Vector<float> floats;
213 Vector<int32_t> ints;
214
Alexey Polyudov88711e82017-05-23 19:54:04 -0700215 handle = data.readInt32();
Peng Xudd5c5cb2017-03-16 17:39:43 -0700216 type = data.readInt32();
217 floats.resize(data.readUint32());
218 for (auto &i : floats) {
219 i = data.readFloat();
220 }
221 ints.resize(data.readUint32());
222 for (auto &i : ints) {
223 i = data.readInt32();
224 }
225
Alexey Polyudov88711e82017-05-23 19:54:04 -0700226 int32_t ret = setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700227 reply->writeInt32(ret);
228 return NO_ERROR;
229 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700230 }
231 return BBinder::onTransact(code, data, reply, flags);
232}
233
234// ----------------------------------------------------------------------------
235}; // namespace android