blob: 140712eb219743409944b441c2cef74cc726223e [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,
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070038 ENABLE_DATA_INJECTION
Mathias Agopian589ce852010-07-13 22:21:56 -070039};
40
41class BpSensorServer : public BpInterface<ISensorServer>
42{
43public:
44 BpSensorServer(const sp<IBinder>& impl)
45 : BpInterface<ISensorServer>(impl)
46 {
47 }
48
Dan Stozad723bd72014-11-18 10:24:03 -080049 virtual ~BpSensorServer();
50
Mathias Agopian589ce852010-07-13 22:21:56 -070051 virtual Vector<Sensor> getSensorList()
52 {
53 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070054 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Mathias Agopian589ce852010-07-13 22:21:56 -070055 remote()->transact(GET_SENSOR_LIST, data, &reply);
56 Sensor s;
57 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080058 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070059 v.setCapacity(n);
60 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070061 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070062 v.add(s);
63 }
64 return v;
65 }
66
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070067 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
68 int mode)
Mathias Agopian589ce852010-07-13 22:21:56 -070069 {
70 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070071 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080072 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070073 data.writeInt32(mode);
Mathias Agopian589ce852010-07-13 22:21:56 -070074 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
75 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
76 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070077
78 virtual status_t enableDataInjection(int enable) {
79 Parcel data, reply;
80 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
81 data.writeInt32(enable);
82 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
83 return reply.readInt32();
84 }
Mathias Agopian589ce852010-07-13 22:21:56 -070085};
86
Dan Stozad723bd72014-11-18 10:24:03 -080087// Out-of-line virtual method definition to trigger vtable emission in this
88// translation unit (see clang warning -Wweak-vtables)
89BpSensorServer::~BpSensorServer() {}
90
Mathias Agopian589ce852010-07-13 22:21:56 -070091IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
92
93// ----------------------------------------------------------------------
94
95status_t BnSensorServer::onTransact(
96 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
97{
98 switch(code) {
99 case GET_SENSOR_LIST: {
100 CHECK_INTERFACE(ISensorServer, data, reply);
101 Vector<Sensor> v(getSensorList());
102 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800103 reply->writeUint32(static_cast<uint32_t>(n));
104 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700105 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700106 }
107 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800108 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700109 case CREATE_SENSOR_EVENT_CONNECTION: {
110 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800111 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700112 int32_t mode = data.readInt32();
113 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800114 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700115 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800116 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700117 case ENABLE_DATA_INJECTION: {
118 CHECK_INTERFACE(ISensorServer, data, reply);
119 int32_t enable = data.readInt32();
120 status_t ret = enableDataInjection(enable);
121 reply->writeInt32(static_cast<int32_t>(ret));
122 return NO_ERROR;
123 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700124 }
125 return BBinder::onTransact(code, data, reply, flags);
126}
127
128// ----------------------------------------------------------------------------
129}; // namespace android