2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <common_time/local_clock.h>
18 #include <utils/String8.h>
20 #include "common_clock_service.h"
21 #include "common_clock.h"
22 #include "common_time_server.h"
26 sp<CommonClockService> CommonClockService::instantiate(
27 CommonTimeServer& timeServer) {
28 sp<CommonClockService> tcc = new CommonClockService(timeServer);
32 defaultServiceManager()->addService(ICommonClock::kServiceName, tcc);
36 status_t CommonClockService::dump(int fd, const Vector<String16>& args) {
37 Mutex::Autolock lock(mRegistrationLock);
38 return mTimeServer.dumpClockInterface(fd, args, mListeners.size());
41 status_t CommonClockService::isCommonTimeValid(bool* valid,
42 uint32_t* timelineID) {
43 return mTimeServer.isCommonTimeValid(valid, timelineID);
46 status_t CommonClockService::commonTimeToLocalTime(int64_t commonTime,
48 return mTimeServer.getCommonClock().commonToLocal(commonTime, localTime);
51 status_t CommonClockService::localTimeToCommonTime(int64_t localTime,
52 int64_t* commonTime) {
53 return mTimeServer.getCommonClock().localToCommon(localTime, commonTime);
56 status_t CommonClockService::getCommonTime(int64_t* commonTime) {
57 return localTimeToCommonTime(mTimeServer.getLocalClock().getLocalTime(), commonTime);
60 status_t CommonClockService::getCommonFreq(uint64_t* freq) {
61 *freq = mTimeServer.getCommonClock().getCommonFreq();
65 status_t CommonClockService::getLocalTime(int64_t* localTime) {
66 *localTime = mTimeServer.getLocalClock().getLocalTime();
70 status_t CommonClockService::getLocalFreq(uint64_t* freq) {
71 *freq = mTimeServer.getLocalClock().getLocalFreq();
75 status_t CommonClockService::getEstimatedError(int32_t* estimate) {
76 *estimate = mTimeServer.getEstimatedError();
80 status_t CommonClockService::getTimelineID(uint64_t* id) {
81 *id = mTimeServer.getTimelineID();
85 status_t CommonClockService::getState(State* state) {
86 *state = mTimeServer.getState();
90 status_t CommonClockService::getMasterAddr(struct sockaddr_storage* addr) {
91 return mTimeServer.getMasterAddr(addr);
94 status_t CommonClockService::registerListener(
95 const sp<ICommonClockListener>& listener) {
96 Mutex::Autolock lock(mRegistrationLock);
98 { // scoping for autolock pattern
99 Mutex::Autolock lock(mCallbackLock);
100 // check whether this is a duplicate
101 for (size_t i = 0; i < mListeners.size(); i++) {
102 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener))
103 return ALREADY_EXISTS;
107 mListeners.add(listener);
108 mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
109 return IInterface::asBinder(listener)->linkToDeath(this);
112 status_t CommonClockService::unregisterListener(
113 const sp<ICommonClockListener>& listener) {
114 Mutex::Autolock lock(mRegistrationLock);
115 status_t ret_val = NAME_NOT_FOUND;
117 { // scoping for autolock pattern
118 Mutex::Autolock lock(mCallbackLock);
119 for (size_t i = 0; i < mListeners.size(); i++) {
120 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
121 IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
122 mListeners.removeAt(i);
129 mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
133 void CommonClockService::binderDied(const wp<IBinder>& who) {
134 Mutex::Autolock lock(mRegistrationLock);
136 { // scoping for autolock pattern
137 Mutex::Autolock lock(mCallbackLock);
138 for (size_t i = 0; i < mListeners.size(); i++) {
139 if (IInterface::asBinder(mListeners[i]) == who) {
140 mListeners.removeAt(i);
146 mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
149 void CommonClockService::notifyOnTimelineChanged(uint64_t timelineID) {
150 Mutex::Autolock lock(mCallbackLock);
152 for (size_t i = 0; i < mListeners.size(); i++) {
153 mListeners[i]->onTimelineChanged(timelineID);
157 }; // namespace android