blob: 5dde9f93ebae8c10d345a095fe6156c6fb2e3eed [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
Svetoslavb412f6e2015-04-29 16:50:41 -070051 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070052 {
53 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070054 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Svetoslavb412f6e2015-04-29 16:50:41 -070055 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070056 remote()->transact(GET_SENSOR_LIST, data, &reply);
57 Sensor s;
58 Vector<Sensor> v;
Dan Stozad723bd72014-11-18 10:24:03 -080059 uint32_t n = reply.readUint32();
Mathias Agopian589ce852010-07-13 22:21:56 -070060 v.setCapacity(n);
61 while (n--) {
Mathias Agopian8683fca2012-08-12 19:37:16 -070062 reply.read(s);
Mathias Agopian589ce852010-07-13 22:21:56 -070063 v.add(s);
64 }
65 return v;
66 }
67
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070068 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -070069 int mode, const String16& opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070070 {
71 Parcel data, reply;
Mathias Agopiana7352c92010-07-14 23:41:37 -070072 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
Aravind Akella4949c502015-02-11 15:54:35 -080073 data.writeString8(packageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070074 data.writeInt32(mode);
Svetoslavb412f6e2015-04-29 16:50:41 -070075 data.writeString16(opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -070076 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
77 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
78 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070079
80 virtual status_t enableDataInjection(int enable) {
81 Parcel data, reply;
82 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
83 data.writeInt32(enable);
84 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
85 return reply.readInt32();
86 }
Mathias Agopian589ce852010-07-13 22:21:56 -070087};
88
Dan Stozad723bd72014-11-18 10:24:03 -080089// Out-of-line virtual method definition to trigger vtable emission in this
90// translation unit (see clang warning -Wweak-vtables)
91BpSensorServer::~BpSensorServer() {}
92
Mathias Agopian589ce852010-07-13 22:21:56 -070093IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
94
95// ----------------------------------------------------------------------
96
97status_t BnSensorServer::onTransact(
98 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
99{
100 switch(code) {
101 case GET_SENSOR_LIST: {
102 CHECK_INTERFACE(ISensorServer, data, reply);
Svetoslavb412f6e2015-04-29 16:50:41 -0700103 const String16& opPackageName = data.readString16();
104 Vector<Sensor> v(getSensorList(opPackageName));
Mathias Agopian589ce852010-07-13 22:21:56 -0700105 size_t n = v.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800106 reply->writeUint32(static_cast<uint32_t>(n));
107 for (size_t i = 0; i < n; i++) {
Mathias Agopian8683fca2012-08-12 19:37:16 -0700108 reply->write(v[i]);
Mathias Agopian589ce852010-07-13 22:21:56 -0700109 }
110 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800111 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700112 case CREATE_SENSOR_EVENT_CONNECTION: {
113 CHECK_INTERFACE(ISensorServer, data, reply);
Aravind Akella4949c502015-02-11 15:54:35 -0800114 String8 packageName = data.readString8();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700115 int32_t mode = data.readInt32();
Svetoslavb412f6e2015-04-29 16:50:41 -0700116 const String16& opPackageName = data.readString16();
117 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
118 opPackageName));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800119 reply->writeStrongBinder(IInterface::asBinder(connection));
Mathias Agopian589ce852010-07-13 22:21:56 -0700120 return NO_ERROR;
Dan Stozad723bd72014-11-18 10:24:03 -0800121 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700122 case ENABLE_DATA_INJECTION: {
123 CHECK_INTERFACE(ISensorServer, data, reply);
124 int32_t enable = data.readInt32();
125 status_t ret = enableDataInjection(enable);
126 reply->writeInt32(static_cast<int32_t>(ret));
127 return NO_ERROR;
128 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700129 }
130 return BBinder::onTransact(code, data, reply, flags);
131}
132
133// ----------------------------------------------------------------------------
134}; // namespace android