blob: 12f600e97ef0e45f5b510025b8f77b7728de059d [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>
Mathias Agopian589ce852010-07-13 22:21:56 -070025#include <utils/Timers.h>
Brian Duddie0a4cebb2022-08-25 19:20:18 +000026#include <utils/Vector.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070027
Mathias Agopian589ce852010-07-13 22:21:56 -070028#include <binder/IInterface.h>
Svet Ganove752a5c2018-01-15 17:14:20 -080029#include <binder/IResultReceiver.h>
Brian Duddie0a4cebb2022-08-25 19:20:18 +000030#include <binder/Parcel.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070031
Mathias Agopian801ea092017-03-06 15:05:04 -080032#include <sensor/Sensor.h>
33#include <sensor/ISensorEventConnection.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070034
35namespace android {
36// ----------------------------------------------------------------------------
37
38enum {
39 GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
40 CREATE_SENSOR_EVENT_CONNECTION,
Peng Xu2576cb62016-01-20 00:22:09 -080041 ENABLE_DATA_INJECTION,
42 GET_DYNAMIC_SENSOR_LIST,
Peng Xue36e3472016-11-03 11:57:10 -070043 CREATE_SENSOR_DIRECT_CONNECTION,
Peng Xudd5c5cb2017-03-16 17:39:43 -070044 SET_OPERATION_PARAMETER,
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +020045 GET_RUNTIME_SENSOR_LIST,
Mark Wheatley8f285d92023-07-07 20:07:18 +000046 ENABLE_REPLAY_DATA_INJECTION,
47 ENABLE_HAL_BYPASS_REPLAY_DATA_INJECTION,
Mathias Agopian589ce852010-07-13 22:21:56 -070048};
49
50class BpSensorServer : public BpInterface<ISensorServer>
51{
52public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070053 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070054 : BpInterface<ISensorServer>(impl)
55 {
56 }
57
Dan Stozad723bd72014-11-18 10:24:03 -080058 virtual ~BpSensorServer();
59
Svetoslavb412f6e2015-04-29 16:50:41 -070060 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070061 {
62 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070063 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070064 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070065 remote()->transact(GET_SENSOR_LIST, data, &reply);
66 Sensor s;
67 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080068 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +000069 // The size of the n Sensor elements on the wire is what we really want, but
70 // this is better than nothing.
71 if (n > reply.dataAvail()) {
72 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
73 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
74 n, reply.dataAvail());
75 return v;
76 }
Mathias Agopian589ce852010-07-13 22:21:56 -070077 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -080078 while (n) {
79 n--;
Devin Moore91e97b82023-02-17 19:35:25 +000080 if(reply.read(s) != OK) {
81 ALOGE("Failed to read reply from getSensorList");
82 v.clear();
83 break;
84 }
Mathias Agopian589ce852010-07-13 22:21:56 -070085 v.add(s);
86 }
87 return v;
88 }
89
Peng Xu2576cb62016-01-20 00:22:09 -080090 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
91 {
92 Parcel data, reply;
93 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
94 data.writeString16(opPackageName);
95 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
96 Sensor s;
97 Vector<Sensor> v;
98 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +000099 // The size of the n Sensor elements on the wire is what we really want, but
100 // this is better than nothing.
101 if (n > reply.dataAvail()) {
102 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
103 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
104 n, reply.dataAvail());
105 return v;
106 }
Peng Xu2576cb62016-01-20 00:22:09 -0800107 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -0800108 while (n) {
109 n--;
Devin Moore91e97b82023-02-17 19:35:25 +0000110 if(reply.read(s) != OK) {
111 ALOGE("Failed to read reply from getDynamicSensorList");
112 v.clear();
113 break;
114 }
Peng Xu2576cb62016-01-20 00:22:09 -0800115 v.add(s);
116 }
117 return v;
118 }
119
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200120 virtual Vector<Sensor> getRuntimeSensorList(const String16& opPackageName, int deviceId)
121 {
122 Parcel data, reply;
123 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
124 data.writeString16(opPackageName);
125 data.writeInt32(deviceId);
126 remote()->transact(GET_RUNTIME_SENSOR_LIST, data, &reply);
127 Sensor s;
128 Vector<Sensor> v;
129 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +0000130 // The size of the n Sensor elements on the wire is what we really want, but
131 // this is better than nothing.
132 if (n > reply.dataAvail()) {
133 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
134 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
135 n, reply.dataAvail());
136 return v;
137 }
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200138 v.setCapacity(n);
139 while (n) {
140 n--;
141 reply.read(s);
142 v.add(s);
143 }
144 return v;
145 }
146
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700147 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800148 int mode, const String16& opPackageName, const String16& attributionTag)
Mathias Agopian589ce852010-07-13 22:21:56 -0700149 {
150 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700151 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -0800152 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700153 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -0700154 data.writeString16(opPackageName);
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800155 data.writeString16(attributionTag);
Mathias Agopian589ce852010-07-13 22:21:56 -0700156 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
157 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
158 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700159
Aravind Akella5c538052015-06-29 12:37:48 -0700160 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700161 Parcel data, reply;
162 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700163 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
164 return reply.readInt32();
165 }
Peng Xue36e3472016-11-03 11:57:10 -0700166
Mark Wheatley8f285d92023-07-07 20:07:18 +0000167 virtual int isReplayDataInjectionEnabled() {
168 Parcel data, reply;
169 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
170 remote()->transact(ENABLE_REPLAY_DATA_INJECTION, data, &reply);
171 return reply.readInt32();
172 }
173
174 virtual int isHalBypassReplayDataInjectionEnabled() {
175 Parcel data, reply;
176 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
177 remote()->transact(ENABLE_HAL_BYPASS_REPLAY_DATA_INJECTION, data, &reply);
178 return reply.readInt32();
179 }
180
Peng Xue36e3472016-11-03 11:57:10 -0700181 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100182 int deviceId, uint32_t size, int32_t type, int32_t format,
183 const native_handle_t *resource) {
Peng Xue36e3472016-11-03 11:57:10 -0700184 Parcel data, reply;
185 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
186 data.writeString16(opPackageName);
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100187 data.writeInt32(deviceId);
Peng Xue36e3472016-11-03 11:57:10 -0700188 data.writeUint32(size);
189 data.writeInt32(type);
190 data.writeInt32(format);
191 data.writeNativeHandle(resource);
192 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
193 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
194 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700195
Alexey Polyudov88711e82017-05-23 19:54:04 -0700196 virtual int setOperationParameter(int32_t handle, int32_t type,
197 const Vector<float> &floats,
198 const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700199 Parcel data, reply;
200 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Alexey Polyudov88711e82017-05-23 19:54:04 -0700201 data.writeInt32(handle);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700202 data.writeInt32(type);
203 data.writeUint32(static_cast<uint32_t>(floats.size()));
204 for (auto i : floats) {
205 data.writeFloat(i);
206 }
207 data.writeUint32(static_cast<uint32_t>(ints.size()));
208 for (auto i : ints) {
209 data.writeInt32(i);
210 }
211 remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
212 return reply.readInt32();
213 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700214};
215
Dan Stozad723bd72014-11-18 10:24:03 -0800216// Out-of-line virtual method definition to trigger vtable emission in this
217// translation unit (see clang warning -Wweak-vtables)
218BpSensorServer::~BpSensorServer() {}
219
Mathias Agopian589ce852010-07-13 22:21:56 -0700220IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
221
222// ----------------------------------------------------------------------
223
224status_t BnSensorServer::onTransact(
225 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
226{
227 switch(code) {
228 case GET_SENSOR_LIST: {
229 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700230 const String16& opPackageName = data.readString16();
231 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700232 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800233 reply->writeUint32(static_cast<uint32_t>(n));
234 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700235 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700236 }
237 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800238 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700239 case CREATE_SENSOR_EVENT_CONNECTION: {
240 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800241 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700242 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700243 const String16& opPackageName = data.readString16();
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800244 const String16& attributionTag = data.readString16();
Svetoslavb412f6e2015-04-29 16:50:41 -0700245 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800246 opPackageName, attributionTag));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800247 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700248 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800249 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700250 case ENABLE_DATA_INJECTION: {
251 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700252 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700253 reply->writeInt32(static_cast<int32_t>(ret));
254 return NO_ERROR;
255 }
Mark Wheatley8f285d92023-07-07 20:07:18 +0000256 case ENABLE_REPLAY_DATA_INJECTION: {
257 CHECK_INTERFACE(ISensorServer, data, reply);
258 int32_t ret = isReplayDataInjectionEnabled();
259 reply->writeInt32(static_cast<int32_t>(ret));
260 return NO_ERROR;
261 }
262 case ENABLE_HAL_BYPASS_REPLAY_DATA_INJECTION: {
263 CHECK_INTERFACE(ISensorServer, data, reply);
264 int32_t ret = isHalBypassReplayDataInjectionEnabled();
265 reply->writeInt32(static_cast<int32_t>(ret));
266 return NO_ERROR;
267 }
Peng Xu2576cb62016-01-20 00:22:09 -0800268 case GET_DYNAMIC_SENSOR_LIST: {
269 CHECK_INTERFACE(ISensorServer, data, reply);
270 const String16& opPackageName = data.readString16();
271 Vector<Sensor> v(getDynamicSensorList(opPackageName));
272 size_t n = v.size();
273 reply->writeUint32(static_cast<uint32_t>(n));
274 for (size_t i = 0; i < n; i++) {
275 reply->write(v[i]);
276 }
277 return NO_ERROR;
278 }
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200279 case GET_RUNTIME_SENSOR_LIST: {
280 CHECK_INTERFACE(ISensorServer, data, reply);
281 const String16& opPackageName = data.readString16();
282 const int deviceId = data.readInt32();
283 Vector<Sensor> v(getRuntimeSensorList(opPackageName, deviceId));
284 size_t n = v.size();
285 reply->writeUint32(static_cast<uint32_t>(n));
286 for (size_t i = 0; i < n; i++) {
287 reply->write(v[i]);
288 }
289 return NO_ERROR;
290 }
Peng Xue36e3472016-11-03 11:57:10 -0700291 case CREATE_SENSOR_DIRECT_CONNECTION: {
292 CHECK_INTERFACE(ISensorServer, data, reply);
293 const String16& opPackageName = data.readString16();
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100294 const int deviceId = data.readInt32();
Peng Xue36e3472016-11-03 11:57:10 -0700295 uint32_t size = data.readUint32();
296 int32_t type = data.readInt32();
297 int32_t format = data.readInt32();
298 native_handle_t *resource = data.readNativeHandle();
Stan Rokita2249c882019-07-30 14:23:49 -0700299 // Avoid a crash in native_handle_close if resource is nullptr
300 if (resource == nullptr) {
301 return BAD_VALUE;
302 }
Brian Duddie0a4cebb2022-08-25 19:20:18 +0000303 native_handle_set_fdsan_tag(resource);
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100304 sp<ISensorEventConnection> ch = createSensorDirectConnection(
305 opPackageName, deviceId, size, type, format, resource);
Brian Duddie0a4cebb2022-08-25 19:20:18 +0000306 native_handle_close_with_tag(resource);
Peng Xue36e3472016-11-03 11:57:10 -0700307 native_handle_delete(resource);
308 reply->writeStrongBinder(IInterface::asBinder(ch));
309 return NO_ERROR;
310 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700311 case SET_OPERATION_PARAMETER: {
312 CHECK_INTERFACE(ISensorServer, data, reply);
Alexey Polyudov88711e82017-05-23 19:54:04 -0700313 int32_t handle;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700314 int32_t type;
315 Vector<float> floats;
316 Vector<int32_t> ints;
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700317 uint32_t count;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700318
Alexey Polyudov88711e82017-05-23 19:54:04 -0700319 handle = data.readInt32();
Peng Xudd5c5cb2017-03-16 17:39:43 -0700320 type = data.readInt32();
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700321
322 count = data.readUint32();
323 if (count > (data.dataAvail() / sizeof(float))) {
324 return BAD_VALUE;
325 }
326 floats.resize(count);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700327 for (auto &i : floats) {
328 i = data.readFloat();
329 }
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700330
331 count = data.readUint32();
332 if (count > (data.dataAvail() / sizeof(int32_t))) {
333 return BAD_VALUE;
334 }
335 ints.resize(count);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700336 for (auto &i : ints) {
337 i = data.readInt32();
338 }
339
Alexey Polyudov88711e82017-05-23 19:54:04 -0700340 int32_t ret = setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700341 reply->writeInt32(ret);
342 return NO_ERROR;
343 }
Svet Ganove752a5c2018-01-15 17:14:20 -0800344 case SHELL_COMMAND_TRANSACTION: {
345 int in = data.readFileDescriptor();
346 int out = data.readFileDescriptor();
347 int err = data.readFileDescriptor();
348 int argc = data.readInt32();
349 Vector<String16> args;
350 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
351 args.add(data.readString16());
352 }
353 sp<IBinder> unusedCallback;
354 sp<IResultReceiver> resultReceiver;
355 status_t status;
356 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
357 return status;
358 }
359 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
360 return status;
361 }
362 status = shellCommand(in, out, err, args);
363 if (resultReceiver != nullptr) {
364 resultReceiver->send(status);
365 }
366 return NO_ERROR;
367 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700368 }
369 return BBinder::onTransact(code, data, reply, flags);
370}
371
372// ----------------------------------------------------------------------------
373}; // namespace android