1589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian/*
2589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
4589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * you may not use this file except in compliance with the License.
6589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * You may obtain a copy of the License at
7589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
8589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
10589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Unless required by applicable law or agreed to in writing, software
11589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * See the License for the specific language governing permissions and
14589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * limitations under the License.
15589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian */
16589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
17589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <stdint.h>
18589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <sys/types.h>
19589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
20589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Errors.h>
21589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/RefBase.h>
22589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Vector.h>
23589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Timers.h>
24589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
25589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <binder/Parcel.h>
26589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <binder/IInterface.h>
27589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
28589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/Sensor.h>
29589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/ISensorServer.h>
30589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/ISensorEventConnection.h>
31589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
32589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiannamespace android {
33589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
34589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
35589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianenum {
36589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
37589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    CREATE_SENSOR_EVENT_CONNECTION,
38589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian};
39589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
40589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianclass BpSensorServer : public BpInterface<ISensorServer>
41589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
42589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianpublic:
43589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    BpSensorServer(const sp<IBinder>& impl)
44589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        : BpInterface<ISensorServer>(impl)
45589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    {
46589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    }
47589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
48589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    virtual Vector<Sensor> getSensorList()
49589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    {
50589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        Parcel data, reply;
51a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
52589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        remote()->transact(GET_SENSOR_LIST, data, &reply);
53589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        Sensor s;
54589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        Vector<Sensor> v;
55589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        int32_t n = reply.readInt32();
56589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        v.setCapacity(n);
57589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        while (n--) {
588683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian            reply.read(s);
59589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            v.add(s);
60589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        }
61589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        return v;
62589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    }
63589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
64589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    virtual sp<ISensorEventConnection> createSensorEventConnection()
65589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    {
66589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        Parcel data, reply;
67a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
68589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
69589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
70589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    }
71589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian};
72589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
73589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianIMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
74589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
75589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------
76589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
77589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatus_t BnSensorServer::onTransact(
78589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
79589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
80589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    switch(code) {
81589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        case GET_SENSOR_LIST: {
82589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            CHECK_INTERFACE(ISensorServer, data, reply);
83589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            Vector<Sensor> v(getSensorList());
84589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            size_t n = v.size();
85589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            reply->writeInt32(n);
86589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            for (size_t i=0 ; i<n ; i++) {
878683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian                reply->write(v[i]);
88589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            }
89589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            return NO_ERROR;
90589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        } break;
91589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        case CREATE_SENSOR_EVENT_CONNECTION: {
92589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            CHECK_INTERFACE(ISensorServer, data, reply);
93589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            sp<ISensorEventConnection> connection(createSensorEventConnection());
94589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            reply->writeStrongBinder(connection->asBinder());
95589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            return NO_ERROR;
96589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian        } break;
97589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    }
98589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return BBinder::onTransact(code, data, reply, flags);
99589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
100589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
101589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
102589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}; // namespace android
103