1951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen/*
2951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Copyright (C) 2011 The Android Open Source Project
3951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
4951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Licensed under the Apache License, Version 2.0 (the "License");
5951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * you may not use this file except in compliance with the License.
6951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * You may obtain a copy of the License at
7951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
8951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *      http://www.apache.org/licenses/LICENSE-2.0
9951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
10951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Unless required by applicable law or agreed to in writing, software
11951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * distributed under the License is distributed on an "AS IS" BASIS,
12951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * See the License for the specific language governing permissions and
14951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * limitations under the License.
15951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen */
16951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
17951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#ifndef ANDROID_ICOMMONCLOCK_H
18951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#define ANDROID_ICOMMONCLOCK_H
19951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
20951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <stdint.h>
21951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <linux/socket.h>
22951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
23951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <binder/IInterface.h>
24951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <binder/IServiceManager.h>
25951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
26951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chennamespace android {
27951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
28951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chenclass ICommonClockListener : public IInterface {
29951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  public:
30951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    DECLARE_META_INTERFACE(CommonClockListener);
31951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
32951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual void onTimelineChanged(uint64_t timelineID) = 0;
33951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};
34951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
35951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chenclass BnCommonClockListener : public BnInterface<ICommonClockListener> {
36951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  public:
37951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t onTransact(uint32_t code, const Parcel& data,
38951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen                                Parcel* reply, uint32_t flags = 0);
39951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};
40951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
41951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chenclass ICommonClock : public IInterface {
42951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  public:
43951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    DECLARE_META_INTERFACE(CommonClock);
44951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
45951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    // Name of the ICommonClock service registered with the service manager.
46951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static const String16 kServiceName;
47951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
48951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    // a reserved invalid timeline ID
49951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static const uint64_t kInvalidTimelineID;
50951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
51951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    // a reserved invalid error estimate
52951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static const int32_t kErrorEstimateUnknown;
53951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
54951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    enum State {
55951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // the device just came up and is trying to discover the master
56951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        STATE_INITIAL,
57951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
58951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // the device is a client of a master
59951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        STATE_CLIENT,
60951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
61951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // the device is acting as master
62951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        STATE_MASTER,
63951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
64951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // the device has lost contact with its master and needs to participate
65951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // in the election of a new master
66951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        STATE_RONIN,
67951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
68951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        // the device is waiting for announcement of the newly elected master
69951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        STATE_WAIT_FOR_ELECTION,
70951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    };
71951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
72951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t isCommonTimeValid(bool* valid, uint32_t* timelineID) = 0;
73951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t commonTimeToLocalTime(int64_t commonTime,
74951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen                                           int64_t* localTime) = 0;
75951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t localTimeToCommonTime(int64_t localTime,
76951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen                                           int64_t* commonTime) = 0;
77951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getCommonTime(int64_t* commonTime) = 0;
78951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getCommonFreq(uint64_t* freq) = 0;
79951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getLocalTime(int64_t* localTime) = 0;
80951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getLocalFreq(uint64_t* freq) = 0;
81951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getEstimatedError(int32_t* estimate) = 0;
82951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getTimelineID(uint64_t* id) = 0;
83951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getState(State* state) = 0;
84951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t getMasterAddr(struct sockaddr_storage* addr) = 0;
85951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
86951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t registerListener(
87951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen            const sp<ICommonClockListener>& listener) = 0;
88951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t unregisterListener(
89951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen            const sp<ICommonClockListener>& listener) = 0;
90951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
91951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    // Simple helper to make it easier to connect to the CommonClock service.
92951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static inline sp<ICommonClock> getInstance() {
93951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        sp<IBinder> binder = defaultServiceManager()->checkService(
94951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen                ICommonClock::kServiceName);
95951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        sp<ICommonClock> clk = interface_cast<ICommonClock>(binder);
96951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        return clk;
97951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    }
98951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};
99951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
100951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chenclass BnCommonClock : public BnInterface<ICommonClock> {
101951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  public:
102951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    virtual status_t onTransact(uint32_t code, const Parcel& data,
103951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen                                Parcel* reply, uint32_t flags = 0);
104951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};
105951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
106951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};  // namespace android
107951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
108951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#endif  // ANDROID_ICOMMONCLOCK_H
109