| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 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, | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 38 | ENABLE_DATA_INJECTION, | 
|  | 39 | GET_DYNAMIC_SENSOR_LIST, | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 40 | }; | 
|  | 41 |  | 
|  | 42 | class BpSensorServer : public BpInterface<ISensorServer> | 
|  | 43 | { | 
|  | 44 | public: | 
|  | 45 | BpSensorServer(const sp<IBinder>& impl) | 
|  | 46 | : BpInterface<ISensorServer>(impl) | 
|  | 47 | { | 
|  | 48 | } | 
|  | 49 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 50 | virtual ~BpSensorServer(); | 
|  | 51 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 52 | virtual Vector<Sensor> getSensorList(const String16& opPackageName) | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 53 | { | 
|  | 54 | Parcel data, reply; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 55 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 56 | data.writeString16(opPackageName); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 57 | remote()->transact(GET_SENSOR_LIST, data, &reply); | 
|  | 58 | Sensor s; | 
|  | 59 | Vector<Sensor> v; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 60 | uint32_t n = reply.readUint32(); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 61 | v.setCapacity(n); | 
|  | 62 | while (n--) { | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 63 | reply.read(s); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 64 | v.add(s); | 
|  | 65 | } | 
|  | 66 | return v; | 
|  | 67 | } | 
|  | 68 |  | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 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 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 86 | virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 87 | int mode, const String16& opPackageName) | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | Parcel data, reply; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 90 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 91 | data.writeString8(packageName); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 92 | data.writeInt32(mode); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 93 | data.writeString16(opPackageName); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 94 | remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); | 
|  | 95 | return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); | 
|  | 96 | } | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 97 |  | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 98 | virtual int isDataInjectionEnabled() { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 99 | Parcel data, reply; | 
|  | 100 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 101 | remote()->transact(ENABLE_DATA_INJECTION, data, &reply); | 
|  | 102 | return reply.readInt32(); | 
|  | 103 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 104 | }; | 
|  | 105 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 106 | // Out-of-line virtual method definition to trigger vtable emission in this | 
|  | 107 | // translation unit (see clang warning -Wweak-vtables) | 
|  | 108 | BpSensorServer::~BpSensorServer() {} | 
|  | 109 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 110 | IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer"); | 
|  | 111 |  | 
|  | 112 | // ---------------------------------------------------------------------- | 
|  | 113 |  | 
|  | 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); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 120 | const String16& opPackageName = data.readString16(); | 
|  | 121 | Vector<Sensor> v(getSensorList(opPackageName)); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 122 | size_t n = v.size(); | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 123 | reply->writeUint32(static_cast<uint32_t>(n)); | 
|  | 124 | for (size_t i = 0; i < n; i++) { | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 125 | reply->write(v[i]); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 126 | } | 
|  | 127 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 128 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 129 | case CREATE_SENSOR_EVENT_CONNECTION: { | 
|  | 130 | CHECK_INTERFACE(ISensorServer, data, reply); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 131 | String8 packageName = data.readString8(); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 132 | int32_t mode = data.readInt32(); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 133 | const String16& opPackageName = data.readString16(); | 
|  | 134 | sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode, | 
|  | 135 | opPackageName)); | 
| Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 136 | reply->writeStrongBinder(IInterface::asBinder(connection)); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 137 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 138 | } | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 139 | case ENABLE_DATA_INJECTION: { | 
|  | 140 | CHECK_INTERFACE(ISensorServer, data, reply); | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 141 | int32_t ret = isDataInjectionEnabled(); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 142 | reply->writeInt32(static_cast<int32_t>(ret)); | 
|  | 143 | return NO_ERROR; | 
|  | 144 | } | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 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 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 156 | } | 
|  | 157 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | // ---------------------------------------------------------------------------- | 
|  | 161 | }; // namespace android |