blob: 74186dfcb9392c9ad1de91830261d3da77123454 [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);
66 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070067 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070068 v.add(s);
69 }
70 return v;
71 }
72
Peng Xu2576cb62016-01-20 00:22:09 -080073 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
74 {
75 Parcel data, reply;
76 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
77 data.writeString16(opPackageName);
78 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
79 Sensor s;
80 Vector<Sensor> v;
81 uint32_t n = reply.readUint32();
82 v.setCapacity(n);
83 while (n--) {
84 reply.read(s);
85 v.add(s);
86 }
87 return v;
88 }
89
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070090 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070091 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070092 {
93 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070094 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080095 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070096 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070097 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070098 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
99 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
100 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700101
Aravind Akella5c538052015-06-29 12:37:48 -0700102 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700103 Parcel data, reply;
104 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700105 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
106 return reply.readInt32();
107 }
Peng Xue36e3472016-11-03 11:57:10 -0700108
109 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
110 uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
111 Parcel data, reply;
112 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
113 data.writeString16(opPackageName);
114 data.writeUint32(size);
115 data.writeInt32(type);
116 data.writeInt32(format);
117 data.writeNativeHandle(resource);
118 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
119 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
120 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700121
122 virtual int setOperationParameter(
123 int32_t type, const Vector<float> &floats, const Vector<int32_t> &ints) {
124 Parcel data, reply;
125 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
126 data.writeInt32(type);
127 data.writeUint32(static_cast<uint32_t>(floats.size()));
128 for (auto i : floats) {
129 data.writeFloat(i);
130 }
131 data.writeUint32(static_cast<uint32_t>(ints.size()));
132 for (auto i : ints) {
133 data.writeInt32(i);
134 }
135 remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
136 return reply.readInt32();
137 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700138};
139
Dan Stozad723bd72014-11-18 10:24:03 -0800140// Out-of-line virtual method definition to trigger vtable emission in this
141// translation unit (see clang warning -Wweak-vtables)
142BpSensorServer::~BpSensorServer() {}
143
Mathias Agopian589ce852010-07-13 22:21:56 -0700144IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
145
146// ----------------------------------------------------------------------
147
148status_t BnSensorServer::onTransact(
149 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
150{
151 switch(code) {
152 case GET_SENSOR_LIST: {
153 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700154 const String16& opPackageName = data.readString16();
155 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700156 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800157 reply->writeUint32(static_cast<uint32_t>(n));
158 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700159 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700160 }
161 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800162 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700163 case CREATE_SENSOR_EVENT_CONNECTION: {
164 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800165 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700166 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700167 const String16& opPackageName = data.readString16();
168 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
169 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800170 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700171 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800172 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700173 case ENABLE_DATA_INJECTION: {
174 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700175 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700176 reply->writeInt32(static_cast<int32_t>(ret));
177 return NO_ERROR;
178 }
Peng Xu2576cb62016-01-20 00:22:09 -0800179 case GET_DYNAMIC_SENSOR_LIST: {
180 CHECK_INTERFACE(ISensorServer, data, reply);
181 const String16& opPackageName = data.readString16();
182 Vector<Sensor> v(getDynamicSensorList(opPackageName));
183 size_t n = v.size();
184 reply->writeUint32(static_cast<uint32_t>(n));
185 for (size_t i = 0; i < n; i++) {
186 reply->write(v[i]);
187 }
188 return NO_ERROR;
189 }
Peng Xue36e3472016-11-03 11:57:10 -0700190 case CREATE_SENSOR_DIRECT_CONNECTION: {
191 CHECK_INTERFACE(ISensorServer, data, reply);
192 const String16& opPackageName = data.readString16();
193 uint32_t size = data.readUint32();
194 int32_t type = data.readInt32();
195 int32_t format = data.readInt32();
196 native_handle_t *resource = data.readNativeHandle();
197 sp<ISensorEventConnection> ch =
198 createSensorDirectConnection(opPackageName, size, type, format, resource);
199 native_handle_close(resource);
200 native_handle_delete(resource);
201 reply->writeStrongBinder(IInterface::asBinder(ch));
202 return NO_ERROR;
203 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700204 case SET_OPERATION_PARAMETER: {
205 CHECK_INTERFACE(ISensorServer, data, reply);
206 int32_t type;
207 Vector<float> floats;
208 Vector<int32_t> ints;
209
210 type = data.readInt32();
211 floats.resize(data.readUint32());
212 for (auto &i : floats) {
213 i = data.readFloat();
214 }
215 ints.resize(data.readUint32());
216 for (auto &i : ints) {
217 i = data.readInt32();
218 }
219
220 int32_t ret = setOperationParameter(type, floats, ints);
221 reply->writeInt32(ret);
222 return NO_ERROR;
223 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700224 }
225 return BBinder::onTransact(code, data, reply, flags);
226}
227
228// ----------------------------------------------------------------------------
229}; // namespace android