blob: 07c507a082ca465dd50177465c0122a83d061ef8 [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
20#include <utils/Errors.h>
21#include <utils/RefBase.h>
22#include <utils/Vector.h>
23#include <utils/Timers.h>
24
25#include <binder/Parcel.h>
26#include <binder/IInterface.h>
27
28#include <gui/Sensor.h>
29#include <gui/ISensorServer.h>
30#include <gui/ISensorEventConnection.h>
31
32namespace android {
33// ----------------------------------------------------------------------------
34
35enum {
36 GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
37 CREATE_SENSOR_EVENT_CONNECTION,
Peng Xu2576cb62016-01-20 00:22:09 -080038 ENABLE_DATA_INJECTION,
39 GET_DYNAMIC_SENSOR_LIST,
Mathias Agopian589ce852010-07-13 22:21:56 -070040};
41
42class BpSensorServer : public BpInterface<ISensorServer>
43{
44public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070045 explicit BpSensorServer(const sp<IBinder>& impl)
Mathias Agopian589ce852010-07-13 22:21:56 -070046 : BpInterface<ISensorServer>(impl)
47 {
48 }
49
Dan Stozad723bd72014-11-18 10:24:03 -080050 virtual ~BpSensorServer();
51
Svetoslavb412f6e2015-04-29 16:50:41 -070052 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070053 {
54 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070055 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070056 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070057 remote()->transact(GET_SENSOR_LIST, data, &reply);
58 Sensor s;
59 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080060 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070061 v.setCapacity(n);
62 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070063 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070064 v.add(s);
65 }
66 return v;
67 }
68
Peng Xu2576cb62016-01-20 00:22:09 -080069 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
70 {
71 Parcel data, reply;
72 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
73 data.writeString16(opPackageName);
74 remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
75 Sensor s;
76 Vector<Sensor> v;
77 uint32_t n = reply.readUint32();
78 v.setCapacity(n);
79 while (n--) {
80 reply.read(s);
81 v.add(s);
82 }
83 return v;
84 }
85
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070086 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070087 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070088 {
89 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070090 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080091 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070092 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070093 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070094 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
95 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
96 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070097
Aravind Akella5c538052015-06-29 12:37:48 -070098 virtual int isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070099 Parcel data, reply;
100 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700101 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
102 return reply.readInt32();
103 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700104};
105
Dan Stozad723bd72014-11-18 10:24:03 -0800106// Out-of-line virtual method definition to trigger vtable emission in this
107// translation unit (see clang warning -Wweak-vtables)
108BpSensorServer::~BpSensorServer() {}
109
Mathias Agopian589ce852010-07-13 22:21:56 -0700110IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
111
112// ----------------------------------------------------------------------
113
114status_t BnSensorServer::onTransact(
115 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
116{
117 switch(code) {
118 case GET_SENSOR_LIST: {
119 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700120 const String16& opPackageName = data.readString16();
121 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700122 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800123 reply->writeUint32(static_cast<uint32_t>(n));
124 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700125 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700126 }
127 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800128 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700129 case CREATE_SENSOR_EVENT_CONNECTION: {
130 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800131 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700132 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700133 const String16& opPackageName = data.readString16();
134 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
135 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800136 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700137 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800138 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700139 case ENABLE_DATA_INJECTION: {
140 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella5c538052015-06-29 12:37:48 -0700141 int32_t ret = isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700142 reply->writeInt32(static_cast<int32_t>(ret));
143 return NO_ERROR;
144 }
Peng Xu2576cb62016-01-20 00:22:09 -0800145 case GET_DYNAMIC_SENSOR_LIST: {
146 CHECK_INTERFACE(ISensorServer, data, reply);
147 const String16& opPackageName = data.readString16();
148 Vector<Sensor> v(getDynamicSensorList(opPackageName));
149 size_t n = v.size();
150 reply->writeUint32(static_cast<uint32_t>(n));
151 for (size_t i = 0; i < n; i++) {
152 reply->write(v[i]);
153 }
154 return NO_ERROR;
155 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700156 }
157 return BBinder::onTransact(code, data, reply, flags);
158}
159
160// ----------------------------------------------------------------------------
161}; // namespace android