1 /*
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
32 namespace android {
33 // ----------------------------------------------------------------------------
34
35 enum {
36 GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
37 CREATE_SENSOR_EVENT_CONNECTION,
38 ENABLE_DATA_INJECTION,
39 GET_DYNAMIC_SENSOR_LIST,
40 };
41
42 class BpSensorServer : public BpInterface<ISensorServer>
43 {
44 public:
BpSensorServer(const sp<IBinder> & impl)45 BpSensorServer(const sp<IBinder>& impl)
46 : BpInterface<ISensorServer>(impl)
47 {
48 }
49
50 virtual ~BpSensorServer();
51
getSensorList(const String16 & opPackageName)52 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
53 {
54 Parcel data, reply;
55 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
56 data.writeString16(opPackageName);
57 remote()->transact(GET_SENSOR_LIST, data, &reply);
58 Sensor s;
59 Vector<Sensor> v;
60 uint32_t n = reply.readUint32();
61 v.setCapacity(n);
62 while (n--) {
63 reply.read(s);
64 v.add(s);
65 }
66 return v;
67 }
68
getDynamicSensorList(const String16 & opPackageName)69 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
createSensorEventConnection(const String8 & packageName,int mode,const String16 & opPackageName)86 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
87 int mode, const String16& opPackageName)
88 {
89 Parcel data, reply;
90 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
91 data.writeString8(packageName);
92 data.writeInt32(mode);
93 data.writeString16(opPackageName);
94 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
95 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
96 }
97
isDataInjectionEnabled()98 virtual int isDataInjectionEnabled() {
99 Parcel data, reply;
100 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
101 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
102 return reply.readInt32();
103 }
104 };
105
106 // Out-of-line virtual method definition to trigger vtable emission in this
107 // translation unit (see clang warning -Wweak-vtables)
~BpSensorServer()108 BpSensorServer::~BpSensorServer() {}
109
110 IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
111
112 // ----------------------------------------------------------------------
113
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)114 status_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);
120 const String16& opPackageName = data.readString16();
121 Vector<Sensor> v(getSensorList(opPackageName));
122 size_t n = v.size();
123 reply->writeUint32(static_cast<uint32_t>(n));
124 for (size_t i = 0; i < n; i++) {
125 reply->write(v[i]);
126 }
127 return NO_ERROR;
128 }
129 case CREATE_SENSOR_EVENT_CONNECTION: {
130 CHECK_INTERFACE(ISensorServer, data, reply);
131 String8 packageName = data.readString8();
132 int32_t mode = data.readInt32();
133 const String16& opPackageName = data.readString16();
134 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
135 opPackageName));
136 reply->writeStrongBinder(IInterface::asBinder(connection));
137 return NO_ERROR;
138 }
139 case ENABLE_DATA_INJECTION: {
140 CHECK_INTERFACE(ISensorServer, data, reply);
141 int32_t ret = isDataInjectionEnabled();
142 reply->writeInt32(static_cast<int32_t>(ret));
143 return NO_ERROR;
144 }
145 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 }
156 }
157 return BBinder::onTransact(code, data, reply, flags);
158 }
159
160 // ----------------------------------------------------------------------------
161 }; // namespace android
162