• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Timers.h>
26 #include <utils/Vector.h>
27 
28 #include <binder/IInterface.h>
29 #include <binder/IResultReceiver.h>
30 #include <binder/Parcel.h>
31 
32 #include <sensor/Sensor.h>
33 #include <sensor/ISensorEventConnection.h>
34 
35 namespace android {
36 // ----------------------------------------------------------------------------
37 
38 enum {
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     GET_RUNTIME_SENSOR_LIST,
46     ENABLE_REPLAY_DATA_INJECTION,
47     ENABLE_HAL_BYPASS_REPLAY_DATA_INJECTION,
48 };
49 
50 class BpSensorServer : public BpInterface<ISensorServer>
51 {
52 public:
BpSensorServer(const sp<IBinder> & impl)53     explicit BpSensorServer(const sp<IBinder>& impl)
54         : BpInterface<ISensorServer>(impl)
55     {
56     }
57 
58     virtual ~BpSensorServer();
59 
getSensorList(const String16 & opPackageName)60     virtual Vector<Sensor> getSensorList(const String16& opPackageName)
61     {
62         Parcel data, reply;
63         data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
64         data.writeString16(opPackageName);
65         remote()->transact(GET_SENSOR_LIST, data, &reply);
66         Sensor s;
67         Vector<Sensor> v;
68         uint32_t n = reply.readUint32();
69         // 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         }
77         v.setCapacity(n);
78         while (n) {
79             n--;
80             if(reply.read(s) != OK) {
81                 ALOGE("Failed to read reply from getSensorList");
82                 v.clear();
83                 break;
84             }
85             v.add(s);
86         }
87         return v;
88     }
89 
getDynamicSensorList(const String16 & opPackageName)90     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();
99         // 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         }
107         v.setCapacity(n);
108         while (n) {
109             n--;
110             if(reply.read(s) != OK) {
111                 ALOGE("Failed to read reply from getDynamicSensorList");
112                 v.clear();
113                 break;
114             }
115             v.add(s);
116         }
117         return v;
118     }
119 
getRuntimeSensorList(const String16 & opPackageName,int deviceId)120     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();
130         // 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         }
138         v.setCapacity(n);
139         while (n) {
140             n--;
141             reply.read(s);
142             v.add(s);
143         }
144         return v;
145     }
146 
createSensorEventConnection(const String8 & packageName,int mode,const String16 & opPackageName,const String16 & attributionTag)147     virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
148              int mode, const String16& opPackageName, const String16& attributionTag)
149     {
150         Parcel data, reply;
151         data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
152         data.writeString8(packageName);
153         data.writeInt32(mode);
154         data.writeString16(opPackageName);
155         data.writeString16(attributionTag);
156         remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
157         return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
158     }
159 
isDataInjectionEnabled()160     virtual int isDataInjectionEnabled() {
161         Parcel data, reply;
162         data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
163         remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
164         return reply.readInt32();
165     }
166 
isReplayDataInjectionEnabled()167     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 
isHalBypassReplayDataInjectionEnabled()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 
createSensorDirectConnection(const String16 & opPackageName,int deviceId,uint32_t size,int32_t type,int32_t format,const native_handle_t * resource)181     virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
182             int deviceId, uint32_t size, int32_t type, int32_t format,
183             const native_handle_t *resource) {
184         Parcel data, reply;
185         data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
186         data.writeString16(opPackageName);
187         data.writeInt32(deviceId);
188         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     }
195 
setOperationParameter(int32_t handle,int32_t type,const Vector<float> & floats,const Vector<int32_t> & ints)196     virtual int setOperationParameter(int32_t handle, int32_t type,
197                                       const Vector<float> &floats,
198                                       const Vector<int32_t> &ints) {
199         Parcel data, reply;
200         data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
201         data.writeInt32(handle);
202         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     }
214 };
215 
216 // Out-of-line virtual method definition to trigger vtable emission in this
217 // translation unit (see clang warning -Wweak-vtables)
~BpSensorServer()218 BpSensorServer::~BpSensorServer() {}
219 
220 IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
221 
222 // ----------------------------------------------------------------------
223 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)224 status_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);
230             const String16& opPackageName = data.readString16();
231             Vector<Sensor> v(getSensorList(opPackageName));
232             size_t n = v.size();
233             reply->writeUint32(static_cast<uint32_t>(n));
234             for (size_t i = 0; i < n; i++) {
235                 reply->write(v[i]);
236             }
237             return NO_ERROR;
238         }
239         case CREATE_SENSOR_EVENT_CONNECTION: {
240             CHECK_INTERFACE(ISensorServer, data, reply);
241             String8 packageName = data.readString8();
242             int32_t mode = data.readInt32();
243             const String16& opPackageName = data.readString16();
244             const String16& attributionTag = data.readString16();
245             sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
246                     opPackageName, attributionTag));
247             reply->writeStrongBinder(IInterface::asBinder(connection));
248             return NO_ERROR;
249         }
250         case ENABLE_DATA_INJECTION: {
251             CHECK_INTERFACE(ISensorServer, data, reply);
252             int32_t ret = isDataInjectionEnabled();
253             reply->writeInt32(static_cast<int32_t>(ret));
254             return NO_ERROR;
255         }
256         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         }
268         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         }
279         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         }
291         case CREATE_SENSOR_DIRECT_CONNECTION: {
292             CHECK_INTERFACE(ISensorServer, data, reply);
293             const String16& opPackageName = data.readString16();
294             const int deviceId = data.readInt32();
295             uint32_t size = data.readUint32();
296             int32_t type = data.readInt32();
297             int32_t format = data.readInt32();
298             native_handle_t *resource = data.readNativeHandle();
299             // Avoid a crash in native_handle_close if resource is nullptr
300             if (resource == nullptr) {
301                 return BAD_VALUE;
302             }
303             native_handle_set_fdsan_tag(resource);
304             sp<ISensorEventConnection> ch = createSensorDirectConnection(
305                     opPackageName, deviceId, size, type, format, resource);
306             native_handle_close_with_tag(resource);
307             native_handle_delete(resource);
308             reply->writeStrongBinder(IInterface::asBinder(ch));
309             return NO_ERROR;
310         }
311         case SET_OPERATION_PARAMETER: {
312             CHECK_INTERFACE(ISensorServer, data, reply);
313             int32_t handle;
314             int32_t type;
315             Vector<float> floats;
316             Vector<int32_t> ints;
317             uint32_t count;
318 
319             handle = data.readInt32();
320             type = data.readInt32();
321 
322             count = data.readUint32();
323             if (count > (data.dataAvail() / sizeof(float))) {
324               return BAD_VALUE;
325             }
326             floats.resize(count);
327             for (auto &i : floats) {
328                 i = data.readFloat();
329             }
330 
331             count = data.readUint32();
332             if (count > (data.dataAvail() / sizeof(int32_t))) {
333               return BAD_VALUE;
334             }
335             ints.resize(count);
336             for (auto &i : ints) {
337                 i = data.readInt32();
338             }
339 
340             int32_t ret = setOperationParameter(handle, type, floats, ints);
341             reply->writeInt32(ret);
342             return NO_ERROR;
343         }
344         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         }
368     }
369     return BBinder::onTransact(code, data, reply, flags);
370 }
371 
372 // ----------------------------------------------------------------------------
373 }; // namespace android
374