blob: aea74036360945642c299ef233d3b1a043630f36 [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
17#include <stdint.h>
18#include <sys/types.h>
19
Peng Xue36e3472016-11-03 11:57:10 -070020#include <cutils/native_handle.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070021#include <utils/Errors.h>
22#include <utils/RefBase.h>
23#include <utils/Vector.h>
24#include <utils/Timers.h>
25
26#include <binder/Parcel.h>
27#include <binder/IInterface.h>
28
29#include <gui/Sensor.h>
30#include <gui/ISensorServer.h>
31#include <gui/ISensorEventConnection.h>
32
33namespace android {
34// ----------------------------------------------------------------------------
35
36enum {
37 GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
38 CREATE_SENSOR_EVENT_CONNECTION,
Peng Xu2576cb62016-01-20 00:22:09 -080039 ENABLE_DATA_INJECTION,
40 GET_DYNAMIC_SENSOR_LIST,
Peng Xue36e3472016-11-03 11:57:10 -070041 CREATE_SENSOR_DIRECT_CONNECTION,
Mathias Agopian589ce852010-07-13 22:21:56 -070042};
43
44class BpSensorServer : public BpInterface<ISensorServer>
45{
46public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070047 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070048 : BpInterface<ISensorServer>(impl)
49 {
50 }
51
Dan Stozad723bd72014-11-18 10:24:03 -080052 virtual ~BpSensorServer();
53
Svetoslavb412f6e2015-04-29 16:50:41 -070054 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070055 {
56 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070057 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070058 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070059 remote()->transact(GET_SENSOR_LIST, data, &reply);
60 Sensor s;
61 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080062 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070063 v.setCapacity(n);
64 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070065 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070066 v.add(s);
67 }
68 return v;
69 }
70
Peng Xu2576cb62016-01-20 00:22:09 -080071 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
72 {
73 Parcel data, reply;
74 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
75 data.writeString16(opPackageName);
76 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
77 Sensor s;
78 Vector<Sensor> v;
79 uint32_t n = reply.readUint32();
80 v.setCapacity(n);
81 while (n--) {
82 reply.read(s);
83 v.add(s);
84 }
85 return v;
86 }
87
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070088 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070089 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070090 {
91 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070092 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080093 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070094 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070095 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070096 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
97 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
98 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070099
Aravind Akella5c538052015-06-29 12:37:48 -0700100 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700101 Parcel data, reply;
102 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700103 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
104 return reply.readInt32();
105 }
Peng Xue36e3472016-11-03 11:57:10 -0700106
107 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
108 uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
109 Parcel data, reply;
110 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
111 data.writeString16(opPackageName);
112 data.writeUint32(size);
113 data.writeInt32(type);
114 data.writeInt32(format);
115 data.writeNativeHandle(resource);
116 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
117 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
118 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700119};
120
Dan Stozad723bd72014-11-18 10:24:03 -0800121// Out-of-line virtual method definition to trigger vtable emission in this
122// translation unit (see clang warning -Wweak-vtables)
123BpSensorServer::~BpSensorServer() {}
124
Mathias Agopian589ce852010-07-13 22:21:56 -0700125IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
126
127// ----------------------------------------------------------------------
128
129status_t BnSensorServer::onTransact(
130 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
131{
132 switch(code) {
133 case GET_SENSOR_LIST: {
134 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700135 const String16& opPackageName = data.readString16();
136 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700137 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800138 reply->writeUint32(static_cast<uint32_t>(n));
139 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700140 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700141 }
142 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800143 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700144 case CREATE_SENSOR_EVENT_CONNECTION: {
145 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800146 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700147 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700148 const String16& opPackageName = data.readString16();
149 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
150 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800151 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700152 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800153 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700154 case ENABLE_DATA_INJECTION: {
155 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700156 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700157 reply->writeInt32(static_cast<int32_t>(ret));
158 return NO_ERROR;
159 }
Peng Xu2576cb62016-01-20 00:22:09 -0800160 case GET_DYNAMIC_SENSOR_LIST: {
161 CHECK_INTERFACE(ISensorServer, data, reply);
162 const String16& opPackageName = data.readString16();
163 Vector<Sensor> v(getDynamicSensorList(opPackageName));
164 size_t n = v.size();
165 reply->writeUint32(static_cast<uint32_t>(n));
166 for (size_t i = 0; i < n; i++) {
167 reply->write(v[i]);
168 }
169 return NO_ERROR;
170 }
Peng Xue36e3472016-11-03 11:57:10 -0700171 case CREATE_SENSOR_DIRECT_CONNECTION: {
172 CHECK_INTERFACE(ISensorServer, data, reply);
173 const String16& opPackageName = data.readString16();
174 uint32_t size = data.readUint32();
175 int32_t type = data.readInt32();
176 int32_t format = data.readInt32();
177 native_handle_t *resource = data.readNativeHandle();
178 sp<ISensorEventConnection> ch =
179 createSensorDirectConnection(opPackageName, size, type, format, resource);
180 native_handle_close(resource);
181 native_handle_delete(resource);
182 reply->writeStrongBinder(IInterface::asBinder(ch));
183 return NO_ERROR;
184 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700185 }
186 return BBinder::onTransact(code, data, reply, flags);
187}
188
189// ----------------------------------------------------------------------------
190}; // namespace android