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 };
40
41 class BpSensorServer : public BpInterface<ISensorServer>
42 {
43 public:
BpSensorServer(const sp<IBinder> & impl)44 BpSensorServer(const sp<IBinder>& impl)
45 : BpInterface<ISensorServer>(impl)
46 {
47 }
48
49 virtual ~BpSensorServer();
50
getSensorList(const String16 & opPackageName)51 virtual Vector<Sensor> getSensorList(const String16& opPackageName)
52 {
53 Parcel data, reply;
54 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
55 data.writeString16(opPackageName);
56 remote()->transact(GET_SENSOR_LIST, data, &reply);
57 Sensor s;
58 Vector<Sensor> v;
59 uint32_t n = reply.readUint32();
60 v.setCapacity(n);
61 while (n--) {
62 reply.read(s);
63 v.add(s);
64 }
65 return v;
66 }
67
createSensorEventConnection(const String8 & packageName,int mode,const String16 & opPackageName)68 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
69 int mode, const String16& opPackageName)
70 {
71 Parcel data, reply;
72 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
73 data.writeString8(packageName);
74 data.writeInt32(mode);
75 data.writeString16(opPackageName);
76 remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
77 return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
78 }
79
isDataInjectionEnabled()80 virtual int isDataInjectionEnabled() {
81 Parcel data, reply;
82 data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
83 remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
84 return reply.readInt32();
85 }
86 };
87
88 // Out-of-line virtual method definition to trigger vtable emission in this
89 // translation unit (see clang warning -Wweak-vtables)
~BpSensorServer()90 BpSensorServer::~BpSensorServer() {}
91
92 IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
93
94 // ----------------------------------------------------------------------
95
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)96 status_t BnSensorServer::onTransact(
97 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
98 {
99 switch(code) {
100 case GET_SENSOR_LIST: {
101 CHECK_INTERFACE(ISensorServer, data, reply);
102 const String16& opPackageName = data.readString16();
103 Vector<Sensor> v(getSensorList(opPackageName));
104 size_t n = v.size();
105 reply->writeUint32(static_cast<uint32_t>(n));
106 for (size_t i = 0; i < n; i++) {
107 reply->write(v[i]);
108 }
109 return NO_ERROR;
110 }
111 case CREATE_SENSOR_EVENT_CONNECTION: {
112 CHECK_INTERFACE(ISensorServer, data, reply);
113 String8 packageName = data.readString8();
114 int32_t mode = data.readInt32();
115 const String16& opPackageName = data.readString16();
116 sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
117 opPackageName));
118 reply->writeStrongBinder(IInterface::asBinder(connection));
119 return NO_ERROR;
120 }
121 case ENABLE_DATA_INJECTION: {
122 CHECK_INTERFACE(ISensorServer, data, reply);
123 int32_t ret = isDataInjectionEnabled();
124 reply->writeInt32(static_cast<int32_t>(ret));
125 return NO_ERROR;
126 }
127 }
128 return BBinder::onTransact(code, data, reply, flags);
129 }
130
131 // ----------------------------------------------------------------------------
132 }; // namespace android
133