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 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 17 | #include <sensor/ISensorServer.h> |
| 18 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
| 21 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <utils/RefBase.h> |
| 25 | #include <utils/Vector.h> |
| 26 | #include <utils/Timers.h> |
| 27 | |
| 28 | #include <binder/Parcel.h> |
| 29 | #include <binder/IInterface.h> |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 30 | #include <binder/IResultReceiver.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 32 | #include <sensor/Sensor.h> |
| 33 | #include <sensor/ISensorEventConnection.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | enum { |
| 39 | GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION, |
| 40 | CREATE_SENSOR_EVENT_CONNECTION, |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 41 | ENABLE_DATA_INJECTION, |
| 42 | GET_DYNAMIC_SENSOR_LIST, |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 43 | CREATE_SENSOR_DIRECT_CONNECTION, |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 44 | SET_OPERATION_PARAMETER, |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class BpSensorServer : public BpInterface<ISensorServer> |
| 48 | { |
| 49 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 50 | explicit BpSensorServer(const sp<IBinder>& impl) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 51 | : BpInterface<ISensorServer>(impl) |
| 52 | { |
| 53 | } |
| 54 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 55 | virtual ~BpSensorServer(); |
| 56 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 57 | virtual Vector<Sensor> getSensorList(const String16& opPackageName) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 58 | { |
| 59 | Parcel data, reply; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 60 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 61 | data.writeString16(opPackageName); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 62 | remote()->transact(GET_SENSOR_LIST, data, &reply); |
| 63 | Sensor s; |
| 64 | Vector<Sensor> v; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 65 | uint32_t n = reply.readUint32(); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 66 | v.setCapacity(n); |
Ivan Lozano | 7acfd31 | 2017-11-07 12:23:26 -0800 | [diff] [blame] | 67 | while (n) { |
| 68 | n--; |
Devin Moore | 2f8fa13 | 2023-02-17 19:35:25 +0000 | [diff] [blame] | 69 | if(reply.read(s) != OK) { |
| 70 | ALOGE("Failed to read reply from getSensorList"); |
| 71 | v.clear(); |
| 72 | break; |
| 73 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 74 | v.add(s); |
| 75 | } |
| 76 | return v; |
| 77 | } |
| 78 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 79 | virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName) |
| 80 | { |
| 81 | Parcel data, reply; |
| 82 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
| 83 | data.writeString16(opPackageName); |
| 84 | remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply); |
| 85 | Sensor s; |
| 86 | Vector<Sensor> v; |
| 87 | uint32_t n = reply.readUint32(); |
| 88 | v.setCapacity(n); |
Ivan Lozano | 7acfd31 | 2017-11-07 12:23:26 -0800 | [diff] [blame] | 89 | while (n) { |
| 90 | n--; |
Devin Moore | 2f8fa13 | 2023-02-17 19:35:25 +0000 | [diff] [blame] | 91 | if(reply.read(s) != OK) { |
| 92 | ALOGE("Failed to read reply from getDynamicSensorList"); |
| 93 | v.clear(); |
| 94 | break; |
| 95 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 96 | v.add(s); |
| 97 | } |
| 98 | return v; |
| 99 | } |
| 100 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 101 | virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 102 | int mode, const String16& opPackageName, const String16& attributionTag) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 103 | { |
| 104 | Parcel data, reply; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 105 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 106 | data.writeString8(packageName); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 107 | data.writeInt32(mode); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 108 | data.writeString16(opPackageName); |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 109 | data.writeString16(attributionTag); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 110 | remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); |
| 111 | return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); |
| 112 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 113 | |
Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 114 | virtual int isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 115 | Parcel data, reply; |
| 116 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 117 | remote()->transact(ENABLE_DATA_INJECTION, data, &reply); |
| 118 | return reply.readInt32(); |
| 119 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 120 | |
| 121 | virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName, |
| 122 | uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) { |
| 123 | Parcel data, reply; |
| 124 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
| 125 | data.writeString16(opPackageName); |
| 126 | data.writeUint32(size); |
| 127 | data.writeInt32(type); |
| 128 | data.writeInt32(format); |
| 129 | data.writeNativeHandle(resource); |
| 130 | remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply); |
| 131 | return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); |
| 132 | } |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 133 | |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 134 | virtual int setOperationParameter(int32_t handle, int32_t type, |
| 135 | const Vector<float> &floats, |
| 136 | const Vector<int32_t> &ints) { |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 137 | Parcel data, reply; |
| 138 | data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 139 | data.writeInt32(handle); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 140 | data.writeInt32(type); |
| 141 | data.writeUint32(static_cast<uint32_t>(floats.size())); |
| 142 | for (auto i : floats) { |
| 143 | data.writeFloat(i); |
| 144 | } |
| 145 | data.writeUint32(static_cast<uint32_t>(ints.size())); |
| 146 | for (auto i : ints) { |
| 147 | data.writeInt32(i); |
| 148 | } |
| 149 | remote()->transact(SET_OPERATION_PARAMETER, data, &reply); |
| 150 | return reply.readInt32(); |
| 151 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 154 | // Out-of-line virtual method definition to trigger vtable emission in this |
| 155 | // translation unit (see clang warning -Wweak-vtables) |
| 156 | BpSensorServer::~BpSensorServer() {} |
| 157 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 158 | IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer"); |
| 159 | |
| 160 | // ---------------------------------------------------------------------- |
| 161 | |
| 162 | status_t BnSensorServer::onTransact( |
| 163 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 164 | { |
| 165 | switch(code) { |
| 166 | case GET_SENSOR_LIST: { |
| 167 | CHECK_INTERFACE(ISensorServer, data, reply); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 168 | const String16& opPackageName = data.readString16(); |
| 169 | Vector<Sensor> v(getSensorList(opPackageName)); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 170 | size_t n = v.size(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 171 | reply->writeUint32(static_cast<uint32_t>(n)); |
| 172 | for (size_t i = 0; i < n; i++) { |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 173 | reply->write(v[i]); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 174 | } |
| 175 | return NO_ERROR; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 176 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 177 | case CREATE_SENSOR_EVENT_CONNECTION: { |
| 178 | CHECK_INTERFACE(ISensorServer, data, reply); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 179 | String8 packageName = data.readString8(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 180 | int32_t mode = data.readInt32(); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 181 | const String16& opPackageName = data.readString16(); |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 182 | const String16& attributionTag = data.readString16(); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 183 | sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode, |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 184 | opPackageName, attributionTag)); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 185 | reply->writeStrongBinder(IInterface::asBinder(connection)); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 186 | return NO_ERROR; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 187 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 188 | case ENABLE_DATA_INJECTION: { |
| 189 | CHECK_INTERFACE(ISensorServer, data, reply); |
Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 190 | int32_t ret = isDataInjectionEnabled(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 191 | reply->writeInt32(static_cast<int32_t>(ret)); |
| 192 | return NO_ERROR; |
| 193 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 194 | case GET_DYNAMIC_SENSOR_LIST: { |
| 195 | CHECK_INTERFACE(ISensorServer, data, reply); |
| 196 | const String16& opPackageName = data.readString16(); |
| 197 | Vector<Sensor> v(getDynamicSensorList(opPackageName)); |
| 198 | size_t n = v.size(); |
| 199 | reply->writeUint32(static_cast<uint32_t>(n)); |
| 200 | for (size_t i = 0; i < n; i++) { |
| 201 | reply->write(v[i]); |
| 202 | } |
| 203 | return NO_ERROR; |
| 204 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 205 | case CREATE_SENSOR_DIRECT_CONNECTION: { |
| 206 | CHECK_INTERFACE(ISensorServer, data, reply); |
| 207 | const String16& opPackageName = data.readString16(); |
| 208 | uint32_t size = data.readUint32(); |
| 209 | int32_t type = data.readInt32(); |
| 210 | int32_t format = data.readInt32(); |
| 211 | native_handle_t *resource = data.readNativeHandle(); |
Stan Rokita | 2249c88 | 2019-07-30 14:23:49 -0700 | [diff] [blame] | 212 | // Avoid a crash in native_handle_close if resource is nullptr |
| 213 | if (resource == nullptr) { |
| 214 | return BAD_VALUE; |
| 215 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 216 | sp<ISensorEventConnection> ch = |
| 217 | createSensorDirectConnection(opPackageName, size, type, format, resource); |
| 218 | native_handle_close(resource); |
| 219 | native_handle_delete(resource); |
| 220 | reply->writeStrongBinder(IInterface::asBinder(ch)); |
| 221 | return NO_ERROR; |
| 222 | } |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 223 | case SET_OPERATION_PARAMETER: { |
| 224 | CHECK_INTERFACE(ISensorServer, data, reply); |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 225 | int32_t handle; |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 226 | int32_t type; |
| 227 | Vector<float> floats; |
| 228 | Vector<int32_t> ints; |
Arthur Ishiguro | 70e25ee | 2020-06-12 11:27:18 -0700 | [diff] [blame] | 229 | uint32_t count; |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 230 | |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 231 | handle = data.readInt32(); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 232 | type = data.readInt32(); |
Arthur Ishiguro | 70e25ee | 2020-06-12 11:27:18 -0700 | [diff] [blame] | 233 | |
| 234 | count = data.readUint32(); |
| 235 | if (count > (data.dataAvail() / sizeof(float))) { |
| 236 | return BAD_VALUE; |
| 237 | } |
| 238 | floats.resize(count); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 239 | for (auto &i : floats) { |
| 240 | i = data.readFloat(); |
| 241 | } |
Arthur Ishiguro | 70e25ee | 2020-06-12 11:27:18 -0700 | [diff] [blame] | 242 | |
| 243 | count = data.readUint32(); |
| 244 | if (count > (data.dataAvail() / sizeof(int32_t))) { |
| 245 | return BAD_VALUE; |
| 246 | } |
| 247 | ints.resize(count); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 248 | for (auto &i : ints) { |
| 249 | i = data.readInt32(); |
| 250 | } |
| 251 | |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 252 | int32_t ret = setOperationParameter(handle, type, floats, ints); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 253 | reply->writeInt32(ret); |
| 254 | return NO_ERROR; |
| 255 | } |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 256 | case SHELL_COMMAND_TRANSACTION: { |
| 257 | int in = data.readFileDescriptor(); |
| 258 | int out = data.readFileDescriptor(); |
| 259 | int err = data.readFileDescriptor(); |
| 260 | int argc = data.readInt32(); |
| 261 | Vector<String16> args; |
| 262 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 263 | args.add(data.readString16()); |
| 264 | } |
| 265 | sp<IBinder> unusedCallback; |
| 266 | sp<IResultReceiver> resultReceiver; |
| 267 | status_t status; |
| 268 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 269 | return status; |
| 270 | } |
| 271 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 272 | return status; |
| 273 | } |
| 274 | status = shellCommand(in, out, err, args); |
| 275 | if (resultReceiver != nullptr) { |
| 276 | resultReceiver->send(status); |
| 277 | } |
| 278 | return NO_ERROR; |
| 279 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 280 | } |
| 281 | return BBinder::onTransact(code, data, reply, flags); |
| 282 | } |
| 283 | |
| 284 | // ---------------------------------------------------------------------------- |
| 285 | }; // namespace android |