/services/common_time/common_clock_service.cpp

https://github.com/aizuzi/platform_frameworks_base · C++ · 157 lines · 112 code · 29 blank · 16 comment · 15 complexity · f03fc05ba6bde65b664620618b80073d MD5 · raw file

  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. #include <common_time/local_clock.h>
  17. #include <utils/String8.h>
  18. #include "common_clock_service.h"
  19. #include "common_clock.h"
  20. #include "common_time_server.h"
  21. namespace android {
  22. sp<CommonClockService> CommonClockService::instantiate(
  23. CommonTimeServer& timeServer) {
  24. sp<CommonClockService> tcc = new CommonClockService(timeServer);
  25. if (tcc == NULL)
  26. return NULL;
  27. defaultServiceManager()->addService(ICommonClock::kServiceName, tcc);
  28. return tcc;
  29. }
  30. status_t CommonClockService::dump(int fd, const Vector<String16>& args) {
  31. Mutex::Autolock lock(mRegistrationLock);
  32. return mTimeServer.dumpClockInterface(fd, args, mListeners.size());
  33. }
  34. status_t CommonClockService::isCommonTimeValid(bool* valid,
  35. uint32_t* timelineID) {
  36. return mTimeServer.isCommonTimeValid(valid, timelineID);
  37. }
  38. status_t CommonClockService::commonTimeToLocalTime(int64_t commonTime,
  39. int64_t* localTime) {
  40. return mTimeServer.getCommonClock().commonToLocal(commonTime, localTime);
  41. }
  42. status_t CommonClockService::localTimeToCommonTime(int64_t localTime,
  43. int64_t* commonTime) {
  44. return mTimeServer.getCommonClock().localToCommon(localTime, commonTime);
  45. }
  46. status_t CommonClockService::getCommonTime(int64_t* commonTime) {
  47. return localTimeToCommonTime(mTimeServer.getLocalClock().getLocalTime(), commonTime);
  48. }
  49. status_t CommonClockService::getCommonFreq(uint64_t* freq) {
  50. *freq = mTimeServer.getCommonClock().getCommonFreq();
  51. return OK;
  52. }
  53. status_t CommonClockService::getLocalTime(int64_t* localTime) {
  54. *localTime = mTimeServer.getLocalClock().getLocalTime();
  55. return OK;
  56. }
  57. status_t CommonClockService::getLocalFreq(uint64_t* freq) {
  58. *freq = mTimeServer.getLocalClock().getLocalFreq();
  59. return OK;
  60. }
  61. status_t CommonClockService::getEstimatedError(int32_t* estimate) {
  62. *estimate = mTimeServer.getEstimatedError();
  63. return OK;
  64. }
  65. status_t CommonClockService::getTimelineID(uint64_t* id) {
  66. *id = mTimeServer.getTimelineID();
  67. return OK;
  68. }
  69. status_t CommonClockService::getState(State* state) {
  70. *state = mTimeServer.getState();
  71. return OK;
  72. }
  73. status_t CommonClockService::getMasterAddr(struct sockaddr_storage* addr) {
  74. return mTimeServer.getMasterAddr(addr);
  75. }
  76. status_t CommonClockService::registerListener(
  77. const sp<ICommonClockListener>& listener) {
  78. Mutex::Autolock lock(mRegistrationLock);
  79. { // scoping for autolock pattern
  80. Mutex::Autolock lock(mCallbackLock);
  81. // check whether this is a duplicate
  82. for (size_t i = 0; i < mListeners.size(); i++) {
  83. if (mListeners[i]->asBinder() == listener->asBinder())
  84. return ALREADY_EXISTS;
  85. }
  86. }
  87. mListeners.add(listener);
  88. mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
  89. return listener->asBinder()->linkToDeath(this);
  90. }
  91. status_t CommonClockService::unregisterListener(
  92. const sp<ICommonClockListener>& listener) {
  93. Mutex::Autolock lock(mRegistrationLock);
  94. status_t ret_val = NAME_NOT_FOUND;
  95. { // scoping for autolock pattern
  96. Mutex::Autolock lock(mCallbackLock);
  97. for (size_t i = 0; i < mListeners.size(); i++) {
  98. if (mListeners[i]->asBinder() == listener->asBinder()) {
  99. mListeners[i]->asBinder()->unlinkToDeath(this);
  100. mListeners.removeAt(i);
  101. ret_val = OK;
  102. break;
  103. }
  104. }
  105. }
  106. mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
  107. return ret_val;
  108. }
  109. void CommonClockService::binderDied(const wp<IBinder>& who) {
  110. Mutex::Autolock lock(mRegistrationLock);
  111. { // scoping for autolock pattern
  112. Mutex::Autolock lock(mCallbackLock);
  113. for (size_t i = 0; i < mListeners.size(); i++) {
  114. if (mListeners[i]->asBinder() == who) {
  115. mListeners.removeAt(i);
  116. break;
  117. }
  118. }
  119. }
  120. mTimeServer.reevaluateAutoDisableState(0 != mListeners.size());
  121. }
  122. void CommonClockService::notifyOnTimelineChanged(uint64_t timelineID) {
  123. Mutex::Autolock lock(mCallbackLock);
  124. for (size_t i = 0; i < mListeners.size(); i++) {
  125. mListeners[i]->onTimelineChanged(timelineID);
  126. }
  127. }
  128. }; // namespace android