ISensorServer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/Vector.h>
  21. #include <utils/Timers.h>
  22. #include <binder/Parcel.h>
  23. #include <binder/IInterface.h>
  24. #include <gui/Sensor.h>
  25. #include <gui/ISensorServer.h>
  26. #include <gui/ISensorEventConnection.h>
  27. namespace android {
  28. // ----------------------------------------------------------------------------
  29. enum {
  30. GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
  31. CREATE_SENSOR_EVENT_CONNECTION,
  32. ENABLE_DATA_INJECTION
  33. };
  34. class BpSensorServer : public BpInterface<ISensorServer>
  35. {
  36. public:
  37. BpSensorServer(const sp<IBinder>& impl)
  38. : BpInterface<ISensorServer>(impl)
  39. {
  40. }
  41. virtual ~BpSensorServer();
  42. virtual Vector<Sensor> getSensorList(const String16& opPackageName)
  43. {
  44. Parcel data, reply;
  45. data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
  46. data.writeString16(opPackageName);
  47. remote()->transact(GET_SENSOR_LIST, data, &reply);
  48. Sensor s;
  49. Vector<Sensor> v;
  50. uint32_t n = reply.readUint32();
  51. v.setCapacity(n);
  52. while (n--) {
  53. reply.read(s);
  54. v.add(s);
  55. }
  56. return v;
  57. }
  58. virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
  59. int mode, const String16& opPackageName)
  60. {
  61. Parcel data, reply;
  62. data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
  63. data.writeString8(packageName);
  64. data.writeInt32(mode);
  65. data.writeString16(opPackageName);
  66. remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
  67. return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
  68. }
  69. virtual int isDataInjectionEnabled() {
  70. Parcel data, reply;
  71. data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
  72. remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
  73. return reply.readInt32();
  74. }
  75. };
  76. // Out-of-line virtual method definition to trigger vtable emission in this
  77. // translation unit (see clang warning -Wweak-vtables)
  78. BpSensorServer::~BpSensorServer() {}
  79. IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
  80. // ----------------------------------------------------------------------
  81. status_t BnSensorServer::onTransact(
  82. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  83. {
  84. switch(code) {
  85. case GET_SENSOR_LIST: {
  86. CHECK_INTERFACE(ISensorServer, data, reply);
  87. const String16& opPackageName = data.readString16();
  88. Vector<Sensor> v(getSensorList(opPackageName));
  89. size_t n = v.size();
  90. reply->writeUint32(static_cast<uint32_t>(n));
  91. for (size_t i = 0; i < n; i++) {
  92. reply->write(v[i]);
  93. }
  94. return NO_ERROR;
  95. }
  96. case CREATE_SENSOR_EVENT_CONNECTION: {
  97. CHECK_INTERFACE(ISensorServer, data, reply);
  98. String8 packageName = data.readString8();
  99. int32_t mode = data.readInt32();
  100. const String16& opPackageName = data.readString16();
  101. sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
  102. opPackageName));
  103. reply->writeStrongBinder(IInterface::asBinder(connection));
  104. return NO_ERROR;
  105. }
  106. case ENABLE_DATA_INJECTION: {
  107. CHECK_INTERFACE(ISensorServer, data, reply);
  108. int32_t ret = isDataInjectionEnabled();
  109. reply->writeInt32(static_cast<int32_t>(ret));
  110. return NO_ERROR;
  111. }
  112. }
  113. return BBinder::onTransact(code, data, reply, flags);
  114. }
  115. // ----------------------------------------------------------------------------
  116. }; // namespace android