blob: 634d35a5b8208895479306d472acfdc8dfc687a8 [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,
Mathias Agopian589ce852010-07-13 22:21:56 -070046};
47
48class BpSensorServer : public BpInterface<ISensorServer>
49{
50public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070051 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070052 : BpInterface<ISensorServer>(impl)
53 {
54 }
55
Dan Stozad723bd72014-11-18 10:24:03 -080056 virtual ~BpSensorServer();
57
Svetoslavb412f6e2015-04-29 16:50:41 -070058 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070059 {
60 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070061 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070062 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070063 remote()->transact(GET_SENSOR_LIST, data, &reply);
64 Sensor s;
65 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080066 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +000067 // The size of the n Sensor elements on the wire is what we really want, but
68 // this is better than nothing.
69 if (n > reply.dataAvail()) {
70 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
71 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
72 n, reply.dataAvail());
73 return v;
74 }
Mathias Agopian589ce852010-07-13 22:21:56 -070075 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -080076 while (n) {
77 n--;
Devin Moore91e97b82023-02-17 19:35:25 +000078 if(reply.read(s) != OK) {
79 ALOGE("Failed to read reply from getSensorList");
80 v.clear();
81 break;
82 }
Mathias Agopian589ce852010-07-13 22:21:56 -070083 v.add(s);
84 }
85 return v;
86 }
87
Peng Xu2576cb62016-01-20 00:22:09 -080088 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
89 {
90 Parcel data, reply;
91 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
92 data.writeString16(opPackageName);
93 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
94 Sensor s;
95 Vector<Sensor> v;
96 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +000097 // The size of the n Sensor elements on the wire is what we really want, but
98 // this is better than nothing.
99 if (n > reply.dataAvail()) {
100 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
101 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
102 n, reply.dataAvail());
103 return v;
104 }
Peng Xu2576cb62016-01-20 00:22:09 -0800105 v.setCapacity(n);
Ivan Lozano7acfd312017-11-07 12:23:26 -0800106 while (n) {
107 n--;
Devin Moore91e97b82023-02-17 19:35:25 +0000108 if(reply.read(s) != OK) {
109 ALOGE("Failed to read reply from getDynamicSensorList");
110 v.clear();
111 break;
112 }
Peng Xu2576cb62016-01-20 00:22:09 -0800113 v.add(s);
114 }
115 return v;
116 }
117
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200118 virtual Vector<Sensor> getRuntimeSensorList(const String16& opPackageName, int deviceId)
119 {
120 Parcel data, reply;
121 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
122 data.writeString16(opPackageName);
123 data.writeInt32(deviceId);
124 remote()->transact(GET_RUNTIME_SENSOR_LIST, data, &reply);
125 Sensor s;
126 Vector<Sensor> v;
127 uint32_t n = reply.readUint32();
Devin Moorebdc293a2023-06-06 22:09:02 +0000128 // The size of the n Sensor elements on the wire is what we really want, but
129 // this is better than nothing.
130 if (n > reply.dataAvail()) {
131 ALOGE("Failed to get a reasonable size of the sensor list. This is likely a "
132 "malformed reply parcel. Number of elements: %d, data available in reply: %zu",
133 n, reply.dataAvail());
134 return v;
135 }
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200136 v.setCapacity(n);
137 while (n) {
138 n--;
139 reply.read(s);
140 v.add(s);
141 }
142 return v;
143 }
144
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700145 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800146 int mode, const String16& opPackageName, const String16& attributionTag)
Mathias Agopian589ce852010-07-13 22:21:56 -0700147 {
148 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700149 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -0800150 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700151 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -0700152 data.writeString16(opPackageName);
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800153 data.writeString16(attributionTag);
Mathias Agopian589ce852010-07-13 22:21:56 -0700154 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
155 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
156 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700157
Aravind Akella5c538052015-06-29 12:37:48 -0700158 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700159 Parcel data, reply;
160 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700161 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
162 return reply.readInt32();
163 }
Peng Xue36e3472016-11-03 11:57:10 -0700164
165 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100166 int deviceId, uint32_t size, int32_t type, int32_t format,
167 const native_handle_t *resource) {
Peng Xue36e3472016-11-03 11:57:10 -0700168 Parcel data, reply;
169 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
170 data.writeString16(opPackageName);
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100171 data.writeInt32(deviceId);
Peng Xue36e3472016-11-03 11:57:10 -0700172 data.writeUint32(size);
173 data.writeInt32(type);
174 data.writeInt32(format);
175 data.writeNativeHandle(resource);
176 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
177 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
178 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700179
Alexey Polyudov88711e82017-05-23 19:54:04 -0700180 virtual int setOperationParameter(int32_t handle, int32_t type,
181 const Vector<float> &floats,
182 const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700183 Parcel data, reply;
184 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Alexey Polyudov88711e82017-05-23 19:54:04 -0700185 data.writeInt32(handle);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700186 data.writeInt32(type);
187 data.writeUint32(static_cast<uint32_t>(floats.size()));
188 for (auto i : floats) {
189 data.writeFloat(i);
190 }
191 data.writeUint32(static_cast<uint32_t>(ints.size()));
192 for (auto i : ints) {
193 data.writeInt32(i);
194 }
195 remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
196 return reply.readInt32();
197 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700198};
199
Dan Stozad723bd72014-11-18 10:24:03 -0800200// Out-of-line virtual method definition to trigger vtable emission in this
201// translation unit (see clang warning -Wweak-vtables)
202BpSensorServer::~BpSensorServer() {}
203
Mathias Agopian589ce852010-07-13 22:21:56 -0700204IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
205
206// ----------------------------------------------------------------------
207
208status_t BnSensorServer::onTransact(
209 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
210{
211 switch(code) {
212 case GET_SENSOR_LIST: {
213 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700214 const String16& opPackageName = data.readString16();
215 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700216 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800217 reply->writeUint32(static_cast<uint32_t>(n));
218 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700219 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700220 }
221 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800222 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700223 case CREATE_SENSOR_EVENT_CONNECTION: {
224 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800225 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700226 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700227 const String16& opPackageName = data.readString16();
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800228 const String16& attributionTag = data.readString16();
Svetoslavb412f6e2015-04-29 16:50:41 -0700229 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800230 opPackageName, attributionTag));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800231 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700232 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800233 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700234 case ENABLE_DATA_INJECTION: {
235 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700236 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700237 reply->writeInt32(static_cast<int32_t>(ret));
238 return NO_ERROR;
239 }
Peng Xu2576cb62016-01-20 00:22:09 -0800240 case GET_DYNAMIC_SENSOR_LIST: {
241 CHECK_INTERFACE(ISensorServer, data, reply);
242 const String16& opPackageName = data.readString16();
243 Vector<Sensor> v(getDynamicSensorList(opPackageName));
244 size_t n = v.size();
245 reply->writeUint32(static_cast<uint32_t>(n));
246 for (size_t i = 0; i < n; i++) {
247 reply->write(v[i]);
248 }
249 return NO_ERROR;
250 }
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200251 case GET_RUNTIME_SENSOR_LIST: {
252 CHECK_INTERFACE(ISensorServer, data, reply);
253 const String16& opPackageName = data.readString16();
254 const int deviceId = data.readInt32();
255 Vector<Sensor> v(getRuntimeSensorList(opPackageName, deviceId));
256 size_t n = v.size();
257 reply->writeUint32(static_cast<uint32_t>(n));
258 for (size_t i = 0; i < n; i++) {
259 reply->write(v[i]);
260 }
261 return NO_ERROR;
262 }
Peng Xue36e3472016-11-03 11:57:10 -0700263 case CREATE_SENSOR_DIRECT_CONNECTION: {
264 CHECK_INTERFACE(ISensorServer, data, reply);
265 const String16& opPackageName = data.readString16();
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100266 const int deviceId = data.readInt32();
Peng Xue36e3472016-11-03 11:57:10 -0700267 uint32_t size = data.readUint32();
268 int32_t type = data.readInt32();
269 int32_t format = data.readInt32();
270 native_handle_t *resource = data.readNativeHandle();
Stan Rokita2249c882019-07-30 14:23:49 -0700271 // Avoid a crash in native_handle_close if resource is nullptr
272 if (resource == nullptr) {
273 return BAD_VALUE;
274 }
Brian Duddie0a4cebb2022-08-25 19:20:18 +0000275 native_handle_set_fdsan_tag(resource);
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100276 sp<ISensorEventConnection> ch = createSensorDirectConnection(
277 opPackageName, deviceId, size, type, format, resource);
Brian Duddie0a4cebb2022-08-25 19:20:18 +0000278 native_handle_close_with_tag(resource);
Peng Xue36e3472016-11-03 11:57:10 -0700279 native_handle_delete(resource);
280 reply->writeStrongBinder(IInterface::asBinder(ch));
281 return NO_ERROR;
282 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700283 case SET_OPERATION_PARAMETER: {
284 CHECK_INTERFACE(ISensorServer, data, reply);
Alexey Polyudov88711e82017-05-23 19:54:04 -0700285 int32_t handle;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700286 int32_t type;
287 Vector<float> floats;
288 Vector<int32_t> ints;
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700289 uint32_t count;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700290
Alexey Polyudov88711e82017-05-23 19:54:04 -0700291 handle = data.readInt32();
Peng Xudd5c5cb2017-03-16 17:39:43 -0700292 type = data.readInt32();
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700293
294 count = data.readUint32();
295 if (count > (data.dataAvail() / sizeof(float))) {
296 return BAD_VALUE;
297 }
298 floats.resize(count);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700299 for (auto &i : floats) {
300 i = data.readFloat();
301 }
Arthur Ishiguro70e25ee2020-06-12 11:27:18 -0700302
303 count = data.readUint32();
304 if (count > (data.dataAvail() / sizeof(int32_t))) {
305 return BAD_VALUE;
306 }
307 ints.resize(count);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700308 for (auto &i : ints) {
309 i = data.readInt32();
310 }
311
Alexey Polyudov88711e82017-05-23 19:54:04 -0700312 int32_t ret = setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700313 reply->writeInt32(ret);
314 return NO_ERROR;
315 }
Svet Ganove752a5c2018-01-15 17:14:20 -0800316 case SHELL_COMMAND_TRANSACTION: {
317 int in = data.readFileDescriptor();
318 int out = data.readFileDescriptor();
319 int err = data.readFileDescriptor();
320 int argc = data.readInt32();
321 Vector<String16> args;
322 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
323 args.add(data.readString16());
324 }
325 sp<IBinder> unusedCallback;
326 sp<IResultReceiver> resultReceiver;
327 status_t status;
328 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
329 return status;
330 }
331 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
332 return status;
333 }
334 status = shellCommand(in, out, err, args);
335 if (resultReceiver != nullptr) {
336 resultReceiver->send(status);
337 }
338 return NO_ERROR;
339 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700340 }
341 return BBinder::onTransact(code, data, reply, flags);
342}
343
344// ----------------------------------------------------------------------------
345}; // namespace android