1 /*
2  * Copyright (C) 2011 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 #ifndef ANDROID_ICOMMONCLOCK_H
18 #define ANDROID_ICOMMONCLOCK_H
19 
20 #include <stdint.h>
21 #include <linux/socket.h>
22 
23 #include <binder/IInterface.h>
24 #include <binder/IServiceManager.h>
25 
26 namespace android {
27 
28 class ICommonClockListener : public IInterface {
29   public:
30     DECLARE_META_INTERFACE(CommonClockListener);
31 
32     virtual void onTimelineChanged(uint64_t timelineID) = 0;
33 };
34 
35 class BnCommonClockListener : public BnInterface<ICommonClockListener> {
36   public:
37     virtual status_t onTransact(uint32_t code, const Parcel& data,
38                                 Parcel* reply, uint32_t flags = 0);
39 };
40 
41 class ICommonClock : public IInterface {
42   public:
43     DECLARE_META_INTERFACE(CommonClock);
44 
45     // Name of the ICommonClock service registered with the service manager.
46     static const String16 kServiceName;
47 
48     // a reserved invalid timeline ID
49     static const uint64_t kInvalidTimelineID;
50 
51     // a reserved invalid error estimate
52     static const int32_t kErrorEstimateUnknown;
53 
54     enum State {
55         // the device just came up and is trying to discover the master
56         STATE_INITIAL,
57 
58         // the device is a client of a master
59         STATE_CLIENT,
60 
61         // the device is acting as master
62         STATE_MASTER,
63 
64         // the device has lost contact with its master and needs to participate
65         // in the election of a new master
66         STATE_RONIN,
67 
68         // the device is waiting for announcement of the newly elected master
69         STATE_WAIT_FOR_ELECTION,
70     };
71 
72     virtual status_t isCommonTimeValid(bool* valid, uint32_t* timelineID) = 0;
73     virtual status_t commonTimeToLocalTime(int64_t commonTime,
74                                            int64_t* localTime) = 0;
75     virtual status_t localTimeToCommonTime(int64_t localTime,
76                                            int64_t* commonTime) = 0;
77     virtual status_t getCommonTime(int64_t* commonTime) = 0;
78     virtual status_t getCommonFreq(uint64_t* freq) = 0;
79     virtual status_t getLocalTime(int64_t* localTime) = 0;
80     virtual status_t getLocalFreq(uint64_t* freq) = 0;
81     virtual status_t getEstimatedError(int32_t* estimate) = 0;
82     virtual status_t getTimelineID(uint64_t* id) = 0;
83     virtual status_t getState(State* state) = 0;
84     virtual status_t getMasterAddr(struct sockaddr_storage* addr) = 0;
85 
86     virtual status_t registerListener(
87             const sp<ICommonClockListener>& listener) = 0;
88     virtual status_t unregisterListener(
89             const sp<ICommonClockListener>& listener) = 0;
90 
91     // Simple helper to make it easier to connect to the CommonClock service.
getInstance()92     static inline sp<ICommonClock> getInstance() {
93         sp<IBinder> binder = defaultServiceManager()->checkService(
94                 ICommonClock::kServiceName);
95         sp<ICommonClock> clk = interface_cast<ICommonClock>(binder);
96         return clk;
97     }
98 };
99 
100 class BnCommonClock : public BnInterface<ICommonClock> {
101   public:
102     virtual status_t onTransact(uint32_t code, const Parcel& data,
103                                 Parcel* reply, uint32_t flags = 0);
104 };
105 
106 };  // namespace android
107 
108 #endif  // ANDROID_ICOMMONCLOCK_H
109