blob: f41f187136993c96357a54cf883341e013637980 [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,
Mathias Agopian589ce852010-07-13 22:21:56 -070043};
44
45class BpSensorServer : public BpInterface<ISensorServer>
46{
47public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070048 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070049 : BpInterface<ISensorServer>(impl)
50 {
51 }
52
Dan Stozad723bd72014-11-18 10:24:03 -080053 virtual ~BpSensorServer();
54
Svetoslavb412f6e2015-04-29 16:50:41 -070055 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070056 {
57 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070058 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070059 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070060 remote()->transact(GET_SENSOR_LIST, data, &reply);
61 Sensor s;
62 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080063 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070064 v.setCapacity(n);
65 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070066 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070067 v.add(s);
68 }
69 return v;
70 }
71
Peng Xu2576cb62016-01-20 00:22:09 -080072 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
73 {
74 Parcel data, reply;
75 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
76 data.writeString16(opPackageName);
77 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
78 Sensor s;
79 Vector<Sensor> v;
80 uint32_t n = reply.readUint32();
81 v.setCapacity(n);
82 while (n--) {
83 reply.read(s);
84 v.add(s);
85 }
86 return v;
87 }
88
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070089 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070090 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070091 {
92 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070093 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080094 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070095 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070096 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070097 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
98 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
99 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700100
Aravind Akella5c538052015-06-29 12:37:48 -0700101 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700102 Parcel data, reply;
103 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700104 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
105 return reply.readInt32();
106 }
Peng Xue36e3472016-11-03 11:57:10 -0700107
108 virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
109 uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
110 Parcel data, reply;
111 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
112 data.writeString16(opPackageName);
113 data.writeUint32(size);
114 data.writeInt32(type);
115 data.writeInt32(format);
116 data.writeNativeHandle(resource);
117 remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
118 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
119 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700120};
121
Dan Stozad723bd72014-11-18 10:24:03 -0800122// Out-of-line virtual method definition to trigger vtable emission in this
123// translation unit (see clang warning -Wweak-vtables)
124BpSensorServer::~BpSensorServer() {}
125
Mathias Agopian589ce852010-07-13 22:21:56 -0700126IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
127
128// ----------------------------------------------------------------------
129
130status_t BnSensorServer::onTransact(
131 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
132{
133 switch(code) {
134 case GET_SENSOR_LIST: {
135 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700136 const String16& opPackageName = data.readString16();
137 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700138 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800139 reply->writeUint32(static_cast<uint32_t>(n));
140 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700141 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700142 }
143 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800144 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700145 case CREATE_SENSOR_EVENT_CONNECTION: {
146 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800147 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700148 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700149 const String16& opPackageName = data.readString16();
150 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
151 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800152 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700153 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800154 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700155 case ENABLE_DATA_INJECTION: {
156 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700157 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700158 reply->writeInt32(static_cast<int32_t>(ret));
159 return NO_ERROR;
160 }
Peng Xu2576cb62016-01-20 00:22:09 -0800161 case GET_DYNAMIC_SENSOR_LIST: {
162 CHECK_INTERFACE(ISensorServer, data, reply);
163 const String16& opPackageName = data.readString16();
164 Vector<Sensor> v(getDynamicSensorList(opPackageName));
165 size_t n = v.size();
166 reply->writeUint32(static_cast<uint32_t>(n));
167 for (size_t i = 0; i < n; i++) {
168 reply->write(v[i]);
169 }
170 return NO_ERROR;
171 }
Peng Xue36e3472016-11-03 11:57:10 -0700172 case CREATE_SENSOR_DIRECT_CONNECTION: {
173 CHECK_INTERFACE(ISensorServer, data, reply);
174 const String16& opPackageName = data.readString16();
175 uint32_t size = data.readUint32();
176 int32_t type = data.readInt32();
177 int32_t format = data.readInt32();
178 native_handle_t *resource = data.readNativeHandle();
179 sp<ISensorEventConnection> ch =
180 createSensorDirectConnection(opPackageName, size, type, format, resource);
181 native_handle_close(resource);
182 native_handle_delete(resource);
183 reply->writeStrongBinder(IInterface::asBinder(ch));
184 return NO_ERROR;
185 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700186 }
187 return BBinder::onTransact(code, data, reply, flags);
188}
189
190// ----------------------------------------------------------------------------
191}; // namespace android