blob: f20668d1e9de462055b6308ccb8277690f488646 [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
Alexey Polyudov88711e82017-05-23 19:54:04 -0700122 virtual int setOperationParameter(int32_t handle, int32_t type,
123 const Vector<float> &floats,
124 const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700125 Parcel data, reply;
126 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Alexey Polyudov88711e82017-05-23 19:54:04 -0700127 data.writeInt32(handle);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700128 data.writeInt32(type);
129 data.writeUint32(static_cast<uint32_t>(floats.size()));
130 for (auto i : floats) {
131 data.writeFloat(i);
132 }
133 data.writeUint32(static_cast<uint32_t>(ints.size()));
134 for (auto i : ints) {
135 data.writeInt32(i);
136 }
137 remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
138 return reply.readInt32();
139 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700140};
141
Dan Stozad723bd72014-11-18 10:24:03 -0800142// Out-of-line virtual method definition to trigger vtable emission in this
143// translation unit (see clang warning -Wweak-vtables)
144BpSensorServer::~BpSensorServer() {}
145
Mathias Agopian589ce852010-07-13 22:21:56 -0700146IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
147
148// ----------------------------------------------------------------------
149
150status_t BnSensorServer::onTransact(
151 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
152{
153 switch(code) {
154 case GET_SENSOR_LIST: {
155 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700156 const String16& opPackageName = data.readString16();
157 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700158 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800159 reply->writeUint32(static_cast<uint32_t>(n));
160 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700161 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700162 }
163 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800164 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700165 case CREATE_SENSOR_EVENT_CONNECTION: {
166 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800167 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700168 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700169 const String16& opPackageName = data.readString16();
170 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
171 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800172 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700173 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800174 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700175 case ENABLE_DATA_INJECTION: {
176 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700177 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700178 reply->writeInt32(static_cast<int32_t>(ret));
179 return NO_ERROR;
180 }
Peng Xu2576cb62016-01-20 00:22:09 -0800181 case GET_DYNAMIC_SENSOR_LIST: {
182 CHECK_INTERFACE(ISensorServer, data, reply);
183 const String16& opPackageName = data.readString16();
184 Vector<Sensor> v(getDynamicSensorList(opPackageName));
185 size_t n = v.size();
186 reply->writeUint32(static_cast<uint32_t>(n));
187 for (size_t i = 0; i < n; i++) {
188 reply->write(v[i]);
189 }
190 return NO_ERROR;
191 }
Peng Xue36e3472016-11-03 11:57:10 -0700192 case CREATE_SENSOR_DIRECT_CONNECTION: {
193 CHECK_INTERFACE(ISensorServer, data, reply);
194 const String16& opPackageName = data.readString16();
195 uint32_t size = data.readUint32();
196 int32_t type = data.readInt32();
197 int32_t format = data.readInt32();
198 native_handle_t *resource = data.readNativeHandle();
199 sp<ISensorEventConnection> ch =
200 createSensorDirectConnection(opPackageName, size, type, format, resource);
201 native_handle_close(resource);
202 native_handle_delete(resource);
203 reply->writeStrongBinder(IInterface::asBinder(ch));
204 return NO_ERROR;
205 }
Peng Xudd5c5cb2017-03-16 17:39:43 -0700206 case SET_OPERATION_PARAMETER: {
207 CHECK_INTERFACE(ISensorServer, data, reply);
Alexey Polyudov88711e82017-05-23 19:54:04 -0700208 int32_t handle;
Peng Xudd5c5cb2017-03-16 17:39:43 -0700209 int32_t type;
210 Vector<float> floats;
211 Vector<int32_t> ints;
212
Alexey Polyudov88711e82017-05-23 19:54:04 -0700213 handle = data.readInt32();
Peng Xudd5c5cb2017-03-16 17:39:43 -0700214 type = data.readInt32();
215 floats.resize(data.readUint32());
216 for (auto &i : floats) {
217 i = data.readFloat();
218 }
219 ints.resize(data.readUint32());
220 for (auto &i : ints) {
221 i = data.readInt32();
222 }
223
Alexey Polyudov88711e82017-05-23 19:54:04 -0700224 int32_t ret = setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700225 reply->writeInt32(ret);
226 return NO_ERROR;
227 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700228 }
229 return BBinder::onTransact(code, data, reply, flags);
230}
231
232// ----------------------------------------------------------------------------
233}; // namespace android