Home | History | Annotate | Download | only in gui
      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/Timers.h>
     23 
     24 #include <binder/Parcel.h>
     25 #include <binder/IInterface.h>
     26 
     27 #include <gui/ISensorEventConnection.h>
     28 #include <gui/BitTube.h>
     29 
     30 namespace android {
     31 // ----------------------------------------------------------------------------
     32 
     33 enum {
     34     GET_SENSOR_CHANNEL = IBinder::FIRST_CALL_TRANSACTION,
     35     ENABLE_DISABLE,
     36     SET_EVENT_RATE
     37 };
     38 
     39 class BpSensorEventConnection : public BpInterface<ISensorEventConnection>
     40 {
     41 public:
     42     BpSensorEventConnection(const sp<IBinder>& impl)
     43         : BpInterface<ISensorEventConnection>(impl)
     44     {
     45     }
     46 
     47     virtual sp<BitTube> getSensorChannel() const
     48     {
     49         Parcel data, reply;
     50         data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
     51         remote()->transact(GET_SENSOR_CHANNEL, data, &reply);
     52         return new BitTube(reply);
     53     }
     54 
     55     virtual status_t enableDisable(int handle, bool enabled)
     56     {
     57         Parcel data, reply;
     58         data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
     59         data.writeInt32(handle);
     60         data.writeInt32(enabled);
     61         remote()->transact(ENABLE_DISABLE, data, &reply);
     62         return reply.readInt32();
     63     }
     64 
     65     virtual status_t setEventRate(int handle, nsecs_t ns)
     66     {
     67         Parcel data, reply;
     68         data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
     69         data.writeInt32(handle);
     70         data.writeInt64(ns);
     71         remote()->transact(SET_EVENT_RATE, data, &reply);
     72         return reply.readInt32();
     73     }
     74 };
     75 
     76 IMPLEMENT_META_INTERFACE(SensorEventConnection, "android.gui.SensorEventConnection");
     77 
     78 // ----------------------------------------------------------------------------
     79 
     80 status_t BnSensorEventConnection::onTransact(
     81     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
     82 {
     83     switch(code) {
     84         case GET_SENSOR_CHANNEL: {
     85             CHECK_INTERFACE(ISensorEventConnection, data, reply);
     86             sp<BitTube> channel(getSensorChannel());
     87             channel->writeToParcel(reply);
     88             return NO_ERROR;
     89         } break;
     90         case ENABLE_DISABLE: {
     91             CHECK_INTERFACE(ISensorEventConnection, data, reply);
     92             int handle = data.readInt32();
     93             int enabled = data.readInt32();
     94             status_t result = enableDisable(handle, enabled);
     95             reply->writeInt32(result);
     96             return NO_ERROR;
     97         } break;
     98         case SET_EVENT_RATE: {
     99             CHECK_INTERFACE(ISensorEventConnection, data, reply);
    100             int handle = data.readInt32();
    101             int ns = data.readInt64();
    102             status_t result = setEventRate(handle, ns);
    103             reply->writeInt32(result);
    104             return NO_ERROR;
    105         } break;
    106     }
    107     return BBinder::onTransact(code, data, reply, flags);
    108 }
    109 
    110 // ----------------------------------------------------------------------------
    111 }; // namespace android
    112