| 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/ISensorEventConnection.h> | 
|  | 18 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 |  | 
|  | 22 | #include <utils/Errors.h> | 
|  | 23 | #include <utils/RefBase.h> | 
|  | 24 | #include <utils/Timers.h> | 
|  | 25 |  | 
|  | 26 | #include <binder/Parcel.h> | 
|  | 27 | #include <binder/IInterface.h> | 
|  | 28 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 29 | #include <sensor/BitTube.h> | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 | // ---------------------------------------------------------------------------- | 
|  | 33 |  | 
|  | 34 | enum { | 
|  | 35 | GET_SENSOR_CHANNEL = IBinder::FIRST_CALL_TRANSACTION, | 
|  | 36 | ENABLE_DISABLE, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 37 | SET_EVENT_RATE, | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 38 | FLUSH_SENSOR, | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 39 | CONFIGURE_CHANNEL, | 
|  | 40 | DESTROY, | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 41 | }; | 
|  | 42 |  | 
|  | 43 | class BpSensorEventConnection : public BpInterface<ISensorEventConnection> | 
|  | 44 | { | 
|  | 45 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 46 | explicit BpSensorEventConnection(const sp<IBinder>& impl) | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 47 | : BpInterface<ISensorEventConnection>(impl) | 
|  | 48 | { | 
|  | 49 | } | 
|  | 50 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 51 | virtual ~BpSensorEventConnection(); | 
|  | 52 |  | 
| Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 53 | virtual sp<BitTube> getSensorChannel() const | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 54 | { | 
|  | 55 | Parcel data, reply; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 56 | data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 57 | remote()->transact(GET_SENSOR_CHANNEL, data, &reply); | 
| Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 58 | return new BitTube(reply); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 61 | virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, | 
|  | 62 | nsecs_t maxBatchReportLatencyNs, int reservedFlags) | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 63 | { | 
|  | 64 | Parcel data, reply; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 65 | data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 66 | data.writeInt32(handle); | 
|  | 67 | data.writeInt32(enabled); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 68 | data.writeInt64(samplingPeriodNs); | 
|  | 69 | data.writeInt64(maxBatchReportLatencyNs); | 
|  | 70 | data.writeInt32(reservedFlags); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 71 | remote()->transact(ENABLE_DISABLE, data, &reply); | 
|  | 72 | return reply.readInt32(); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | virtual status_t setEventRate(int handle, nsecs_t ns) | 
|  | 76 | { | 
|  | 77 | Parcel data, reply; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 78 | data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 79 | data.writeInt32(handle); | 
|  | 80 | data.writeInt64(ns); | 
|  | 81 | remote()->transact(SET_EVENT_RATE, data, &reply); | 
|  | 82 | return reply.readInt32(); | 
|  | 83 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 84 |  | 
| Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 85 | virtual status_t flush() { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 86 | Parcel data, reply; | 
|  | 87 | data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 88 | remote()->transact(FLUSH_SENSOR, data, &reply); | 
|  | 89 | return reply.readInt32(); | 
|  | 90 | } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | virtual int32_t configureChannel(int32_t handle, int32_t rateLevel) { | 
|  | 93 | Parcel data, reply; | 
|  | 94 | data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); | 
|  | 95 | data.writeInt32(handle); | 
|  | 96 | data.writeInt32(rateLevel); | 
|  | 97 | remote()->transact(CONFIGURE_CHANNEL, data, &reply); | 
|  | 98 | return reply.readInt32(); | 
|  | 99 | } | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | virtual void onLastStrongRef(const void* id) { | 
|  | 102 | destroy(); | 
|  | 103 | BpInterface<ISensorEventConnection>::onLastStrongRef(id); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | protected: | 
|  | 107 | virtual void destroy() { | 
|  | 108 | Parcel data, reply; | 
|  | 109 | remote()->transact(DESTROY, data, &reply); | 
|  | 110 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 111 | }; | 
|  | 112 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 113 | // Out-of-line virtual method definition to trigger vtable emission in this | 
|  | 114 | // translation unit (see clang warning -Wweak-vtables) | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 115 | BpSensorEventConnection::~BpSensorEventConnection() { } | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 116 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 117 | IMPLEMENT_META_INTERFACE(SensorEventConnection, "android.gui.SensorEventConnection"); | 
|  | 118 |  | 
|  | 119 | // ---------------------------------------------------------------------------- | 
|  | 120 |  | 
|  | 121 | status_t BnSensorEventConnection::onTransact( | 
|  | 122 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 123 | { | 
|  | 124 | switch(code) { | 
|  | 125 | case GET_SENSOR_CHANNEL: { | 
|  | 126 | CHECK_INTERFACE(ISensorEventConnection, data, reply); | 
| Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 127 | sp<BitTube> channel(getSensorChannel()); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 128 | channel->writeToParcel(reply); | 
|  | 129 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 130 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 131 | case ENABLE_DISABLE: { | 
|  | 132 | CHECK_INTERFACE(ISensorEventConnection, data, reply); | 
|  | 133 | int handle = data.readInt32(); | 
|  | 134 | int enabled = data.readInt32(); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 135 | nsecs_t samplingPeriodNs = data.readInt64(); | 
|  | 136 | nsecs_t maxBatchReportLatencyNs = data.readInt64(); | 
|  | 137 | int reservedFlags = data.readInt32(); | 
|  | 138 | status_t result = enableDisable(handle, enabled, samplingPeriodNs, | 
|  | 139 | maxBatchReportLatencyNs, reservedFlags); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 140 | reply->writeInt32(result); | 
|  | 141 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 142 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 143 | case SET_EVENT_RATE: { | 
|  | 144 | CHECK_INTERFACE(ISensorEventConnection, data, reply); | 
|  | 145 | int handle = data.readInt32(); | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 146 | nsecs_t ns = data.readInt64(); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 147 | status_t result = setEventRate(handle, ns); | 
|  | 148 | reply->writeInt32(result); | 
|  | 149 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 150 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 151 | case FLUSH_SENSOR: { | 
|  | 152 | CHECK_INTERFACE(ISensorEventConnection, data, reply); | 
| Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 153 | status_t result = flush(); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 154 | reply->writeInt32(result); | 
|  | 155 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 156 | } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 157 | case CONFIGURE_CHANNEL: { | 
|  | 158 | CHECK_INTERFACE(ISensorEventConnection, data, reply); | 
|  | 159 | int handle = data.readInt32(); | 
|  | 160 | int rateLevel = data.readInt32(); | 
|  | 161 | status_t result = configureChannel(handle, rateLevel); | 
|  | 162 | reply->writeInt32(result); | 
|  | 163 | return NO_ERROR; | 
|  | 164 | } | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 165 | case DESTROY: { | 
|  | 166 | destroy(); | 
|  | 167 | return NO_ERROR; | 
|  | 168 | } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 169 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 170 | } | 
|  | 171 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | // ---------------------------------------------------------------------------- | 
|  | 175 | }; // namespace android |