IBatteryPropertiesRegistrar.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2013 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. #define LOG_TAG "IBatteryPropertiesRegistrar"
  17. //#define LOG_NDEBUG 0
  18. #include <utils/Log.h>
  19. #include <batteryservice/IBatteryPropertiesListener.h>
  20. #include <batteryservice/IBatteryPropertiesRegistrar.h>
  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. #include <binder/Parcel.h>
  24. namespace android {
  25. class BpBatteryPropertiesRegistrar : public BpInterface<IBatteryPropertiesRegistrar> {
  26. public:
  27. BpBatteryPropertiesRegistrar(const sp<IBinder>& impl)
  28. : BpInterface<IBatteryPropertiesRegistrar>(impl) {}
  29. void registerListener(const sp<IBatteryPropertiesListener>& listener) {
  30. Parcel data;
  31. data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
  32. data.writeStrongBinder(IInterface::asBinder(listener));
  33. remote()->transact(REGISTER_LISTENER, data, NULL);
  34. }
  35. void unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
  36. Parcel data;
  37. data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
  38. data.writeStrongBinder(IInterface::asBinder(listener));
  39. remote()->transact(UNREGISTER_LISTENER, data, NULL);
  40. }
  41. status_t getProperty(int id, struct BatteryProperty *val) {
  42. Parcel data, reply;
  43. data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
  44. data.writeInt32(id);
  45. remote()->transact(GET_PROPERTY, data, &reply);
  46. int32_t ret = reply.readExceptionCode();
  47. if (ret != 0) {
  48. return ret;
  49. }
  50. ret = reply.readInt32();
  51. int parcelpresent = reply.readInt32();
  52. if (parcelpresent)
  53. val->readFromParcel(&reply);
  54. return ret;
  55. }
  56. status_t getDockProperty(int id, struct BatteryProperty *val) {
  57. Parcel data, reply;
  58. data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
  59. data.writeInt32(id);
  60. remote()->transact(GET_DOCK_PROPERTY, data, &reply);
  61. int32_t ret = reply.readExceptionCode();
  62. if (ret != 0) {
  63. return ret;
  64. }
  65. ret = reply.readInt32();
  66. int parcelpresent = reply.readInt32();
  67. if (parcelpresent)
  68. val->readFromParcel(&reply);
  69. return ret;
  70. }
  71. };
  72. IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar");
  73. status_t BnBatteryPropertiesRegistrar::onTransact(uint32_t code,
  74. const Parcel& data,
  75. Parcel* reply,
  76. uint32_t flags)
  77. {
  78. switch(code) {
  79. case REGISTER_LISTENER: {
  80. CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
  81. sp<IBatteryPropertiesListener> listener =
  82. interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
  83. registerListener(listener);
  84. return OK;
  85. }
  86. case UNREGISTER_LISTENER: {
  87. CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
  88. sp<IBatteryPropertiesListener> listener =
  89. interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
  90. unregisterListener(listener);
  91. return OK;
  92. }
  93. case GET_PROPERTY: {
  94. CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
  95. int id = data.readInt32();
  96. struct BatteryProperty val;
  97. status_t result = getProperty(id, &val);
  98. reply->writeNoException();
  99. reply->writeInt32(result);
  100. reply->writeInt32(1);
  101. val.writeToParcel(reply);
  102. return OK;
  103. }
  104. case GET_DOCK_PROPERTY: {
  105. CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
  106. int id = data.readInt32();
  107. struct BatteryProperty val;
  108. status_t result = getDockProperty(id, &val);
  109. reply->writeNoException();
  110. reply->writeInt32(result);
  111. reply->writeInt32(1);
  112. val.writeToParcel(reply);
  113. return OK;
  114. }
  115. }
  116. return BBinder::onTransact(code, data, reply, flags);
  117. };
  118. // ----------------------------------------------------------------------------
  119. }; // namespace android