ISensorEventConnection.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2010 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 <stdint.h>
  17. #include <sys/types.h>
  18. #include <utils/Errors.h>
  19. #include <utils/RefBase.h>
  20. #include <utils/Timers.h>
  21. #include <binder/Parcel.h>
  22. #include <binder/IInterface.h>
  23. #include <gui/ISensorEventConnection.h>
  24. #include <gui/BitTube.h>
  25. namespace android {
  26. // ----------------------------------------------------------------------------
  27. enum {
  28. GET_SENSOR_CHANNEL = IBinder::FIRST_CALL_TRANSACTION,
  29. ENABLE_DISABLE,
  30. SET_EVENT_RATE,
  31. FLUSH_SENSOR
  32. };
  33. class BpSensorEventConnection : public BpInterface<ISensorEventConnection>
  34. {
  35. public:
  36. BpSensorEventConnection(const sp<IBinder>& impl)
  37. : BpInterface<ISensorEventConnection>(impl)
  38. {
  39. }
  40. virtual ~BpSensorEventConnection();
  41. virtual sp<BitTube> getSensorChannel() const
  42. {
  43. Parcel data, reply;
  44. data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
  45. remote()->transact(GET_SENSOR_CHANNEL, data, &reply);
  46. return new BitTube(reply);
  47. }
  48. virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
  49. nsecs_t maxBatchReportLatencyNs, int reservedFlags)
  50. {
  51. Parcel data, reply;
  52. data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
  53. data.writeInt32(handle);
  54. data.writeInt32(enabled);
  55. data.writeInt64(samplingPeriodNs);
  56. data.writeInt64(maxBatchReportLatencyNs);
  57. data.writeInt32(reservedFlags);
  58. remote()->transact(ENABLE_DISABLE, data, &reply);
  59. return reply.readInt32();
  60. }
  61. virtual status_t setEventRate(int handle, nsecs_t ns)
  62. {
  63. Parcel data, reply;
  64. data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
  65. data.writeInt32(handle);
  66. data.writeInt64(ns);
  67. remote()->transact(SET_EVENT_RATE, data, &reply);
  68. return reply.readInt32();
  69. }
  70. virtual status_t flush() {
  71. Parcel data, reply;
  72. data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
  73. remote()->transact(FLUSH_SENSOR, data, &reply);
  74. return reply.readInt32();
  75. }
  76. };
  77. // Out-of-line virtual method definition to trigger vtable emission in this
  78. // translation unit (see clang warning -Wweak-vtables)
  79. BpSensorEventConnection::~BpSensorEventConnection() {}
  80. IMPLEMENT_META_INTERFACE(SensorEventConnection, "android.gui.SensorEventConnection");
  81. // ----------------------------------------------------------------------------
  82. status_t BnSensorEventConnection::onTransact(
  83. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  84. {
  85. switch(code) {
  86. case GET_SENSOR_CHANNEL: {
  87. CHECK_INTERFACE(ISensorEventConnection, data, reply);
  88. sp<BitTube> channel(getSensorChannel());
  89. channel->writeToParcel(reply);
  90. return NO_ERROR;
  91. }
  92. case ENABLE_DISABLE: {
  93. CHECK_INTERFACE(ISensorEventConnection, data, reply);
  94. int handle = data.readInt32();
  95. int enabled = data.readInt32();
  96. nsecs_t samplingPeriodNs = data.readInt64();
  97. nsecs_t maxBatchReportLatencyNs = data.readInt64();
  98. int reservedFlags = data.readInt32();
  99. status_t result = enableDisable(handle, enabled, samplingPeriodNs,
  100. maxBatchReportLatencyNs, reservedFlags);
  101. reply->writeInt32(result);
  102. return NO_ERROR;
  103. }
  104. case SET_EVENT_RATE: {
  105. CHECK_INTERFACE(ISensorEventConnection, data, reply);
  106. int handle = data.readInt32();
  107. nsecs_t ns = data.readInt64();
  108. status_t result = setEventRate(handle, ns);
  109. reply->writeInt32(result);
  110. return NO_ERROR;
  111. }
  112. case FLUSH_SENSOR: {
  113. CHECK_INTERFACE(ISensorEventConnection, data, reply);
  114. status_t result = flush();
  115. reply->writeInt32(result);
  116. return NO_ERROR;
  117. }
  118. }
  119. return BBinder::onTransact(code, data, reply, flags);
  120. }
  121. // ----------------------------------------------------------------------------
  122. }; // namespace android