1b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/*
2b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3b957b9d63c88efd3a961759424987b99219adeedMathias Agopian *
4b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * you may not use this file except in compliance with the License.
6b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * You may obtain a copy of the License at
7b957b9d63c88efd3a961759424987b99219adeedMathias Agopian *
8b957b9d63c88efd3a961759424987b99219adeedMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9b957b9d63c88efd3a961759424987b99219adeedMathias Agopian *
10b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Unless required by applicable law or agreed to in writing, software
11b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * See the License for the specific language governing permissions and
14b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * limitations under the License.
15b957b9d63c88efd3a961759424987b99219adeedMathias Agopian */
16b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
17b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <stdint.h>
18b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <sys/types.h>
19b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
20b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Errors.h>
21b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/RefBase.h>
22b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Vector.h>
23b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Timers.h>
24b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
25b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <binder/Parcel.h>
26b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <binder/IInterface.h>
27b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
28b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/Sensor.h>
29b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/ISensorServer.h>
30b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/ISensorEventConnection.h>
31b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
32b957b9d63c88efd3a961759424987b99219adeedMathias Agopiannamespace android {
33b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ----------------------------------------------------------------------------
34b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
35b957b9d63c88efd3a961759424987b99219adeedMathias Agopianenum {
36b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
37b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    CREATE_SENSOR_EVENT_CONNECTION,
38b957b9d63c88efd3a961759424987b99219adeedMathias Agopian};
39b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
40b957b9d63c88efd3a961759424987b99219adeedMathias Agopianclass BpSensorServer : public BpInterface<ISensorServer>
41b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
42b957b9d63c88efd3a961759424987b99219adeedMathias Agopianpublic:
43b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    BpSensorServer(const sp<IBinder>& impl)
44b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        : BpInterface<ISensorServer>(impl)
45b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    {
46b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
47b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
48b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    virtual Vector<Sensor> getSensorList()
49b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    {
50b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        Parcel data, reply;
511bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
52b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        remote()->transact(GET_SENSOR_LIST, data, &reply);
53b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        Sensor s;
54b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        Vector<Sensor> v;
55b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        int32_t n = reply.readInt32();
56b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        v.setCapacity(n);
57b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        while (n--) {
58b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            reply.read(static_cast<Flattenable&>(s));
59b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            v.add(s);
60b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        }
61b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        return v;
62b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
63b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
64b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    virtual sp<ISensorEventConnection> createSensorEventConnection()
65b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    {
66b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        Parcel data, reply;
671bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
68b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
69b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
70b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
71b957b9d63c88efd3a961759424987b99219adeedMathias Agopian};
72b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
73b957b9d63c88efd3a961759424987b99219adeedMathias AgopianIMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
74b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
75b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ----------------------------------------------------------------------
76b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
77b957b9d63c88efd3a961759424987b99219adeedMathias Agopianstatus_t BnSensorServer::onTransact(
78b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
79b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
80b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    switch(code) {
81b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        case GET_SENSOR_LIST: {
82b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            CHECK_INTERFACE(ISensorServer, data, reply);
83b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            Vector<Sensor> v(getSensorList());
84b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            size_t n = v.size();
85b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            reply->writeInt32(n);
86b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            for (size_t i=0 ; i<n ; i++) {
87b957b9d63c88efd3a961759424987b99219adeedMathias Agopian                reply->write(static_cast<const Flattenable&>(v[i]));
88b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            }
89b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            return NO_ERROR;
90b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        } break;
91b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        case CREATE_SENSOR_EVENT_CONNECTION: {
92b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            CHECK_INTERFACE(ISensorServer, data, reply);
93b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            sp<ISensorEventConnection> connection(createSensorEventConnection());
94b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            reply->writeStrongBinder(connection->asBinder());
95b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            return NO_ERROR;
96b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        } break;
97b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
98b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return BBinder::onTransact(code, data, reply, flags);
99b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
100b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
101b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ----------------------------------------------------------------------------
102b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}; // namespace android
103