ISoundTriggerHwService.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. **
  3. ** Copyright 2014, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #define LOG_TAG "BpSoundTriggerHwService"
  18. //#define LOG_NDEBUG 0
  19. #include <utils/Log.h>
  20. #include <utils/Errors.h>
  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. #include <binder/IMemory.h>
  24. #include <binder/Parcel.h>
  25. #include <binder/IPCThreadState.h>
  26. #include <binder/IServiceManager.h>
  27. #include <soundtrigger/ISoundTriggerHwService.h>
  28. #include <soundtrigger/ISoundTrigger.h>
  29. #include <soundtrigger/ISoundTriggerClient.h>
  30. namespace android {
  31. enum {
  32. LIST_MODULES = IBinder::FIRST_CALL_TRANSACTION,
  33. ATTACH,
  34. SET_CAPTURE_STATE,
  35. };
  36. #define MAX_ITEMS_PER_LIST 1024
  37. class BpSoundTriggerHwService: public BpInterface<ISoundTriggerHwService>
  38. {
  39. public:
  40. explicit BpSoundTriggerHwService(const sp<IBinder>& impl)
  41. : BpInterface<ISoundTriggerHwService>(impl)
  42. {
  43. }
  44. virtual status_t listModules(const String16& opPackageName,
  45. struct sound_trigger_module_descriptor *modules,
  46. uint32_t *numModules)
  47. {
  48. if (numModules == NULL || (*numModules != 0 && modules == NULL)) {
  49. return BAD_VALUE;
  50. }
  51. Parcel data, reply;
  52. data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor());
  53. data.writeString16(opPackageName);
  54. unsigned int numModulesReq = (modules == NULL) ? 0 : *numModules;
  55. data.writeInt32(numModulesReq);
  56. status_t status = remote()->transact(LIST_MODULES, data, &reply);
  57. if (status == NO_ERROR) {
  58. status = (status_t)reply.readInt32();
  59. *numModules = (unsigned int)reply.readInt32();
  60. }
  61. ALOGV("listModules() status %d got *numModules %d", status, *numModules);
  62. if (status == NO_ERROR) {
  63. if (numModulesReq > *numModules) {
  64. numModulesReq = *numModules;
  65. }
  66. if (numModulesReq > 0) {
  67. reply.read(modules, numModulesReq * sizeof(struct sound_trigger_module_descriptor));
  68. }
  69. }
  70. return status;
  71. }
  72. virtual status_t attach(const String16& opPackageName,
  73. const sound_trigger_module_handle_t handle,
  74. const sp<ISoundTriggerClient>& client,
  75. sp<ISoundTrigger>& module)
  76. {
  77. Parcel data, reply;
  78. data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor());
  79. data.writeString16(opPackageName);
  80. data.write(&handle, sizeof(sound_trigger_module_handle_t));
  81. data.writeStrongBinder(IInterface::asBinder(client));
  82. status_t status = remote()->transact(ATTACH, data, &reply);
  83. if (status != NO_ERROR) {
  84. return status;
  85. }
  86. status = reply.readInt32();
  87. if (reply.readInt32() != 0) {
  88. module = interface_cast<ISoundTrigger>(reply.readStrongBinder());
  89. }
  90. return status;
  91. }
  92. virtual status_t setCaptureState(bool active)
  93. {
  94. Parcel data, reply;
  95. data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor());
  96. data.writeInt32(active);
  97. status_t status = remote()->transact(SET_CAPTURE_STATE, data, &reply);
  98. if (status == NO_ERROR) {
  99. status = reply.readInt32();
  100. }
  101. return status;
  102. }
  103. };
  104. IMPLEMENT_META_INTERFACE(SoundTriggerHwService, "android.hardware.ISoundTriggerHwService");
  105. // ----------------------------------------------------------------------
  106. status_t BnSoundTriggerHwService::onTransact(
  107. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  108. {
  109. switch(code) {
  110. case LIST_MODULES: {
  111. CHECK_INTERFACE(ISoundTriggerHwService, data, reply);
  112. String16 opPackageName;
  113. status_t status = data.readString16(&opPackageName);
  114. if (status != NO_ERROR) {
  115. return status;
  116. }
  117. unsigned int numModulesReq = data.readInt32();
  118. if (numModulesReq > MAX_ITEMS_PER_LIST) {
  119. numModulesReq = MAX_ITEMS_PER_LIST;
  120. }
  121. unsigned int numModules = numModulesReq;
  122. struct sound_trigger_module_descriptor *modules =
  123. (struct sound_trigger_module_descriptor *)calloc(numModulesReq,
  124. sizeof(struct sound_trigger_module_descriptor));
  125. if (modules == NULL) {
  126. reply->writeInt32(NO_MEMORY);
  127. reply->writeInt32(0);
  128. return NO_ERROR;
  129. }
  130. status = listModules(opPackageName, modules, &numModules);
  131. reply->writeInt32(status);
  132. reply->writeInt32(numModules);
  133. ALOGV("LIST_MODULES status %d got numModules %d", status, numModules);
  134. if (status == NO_ERROR) {
  135. if (numModulesReq > numModules) {
  136. numModulesReq = numModules;
  137. }
  138. reply->write(modules,
  139. numModulesReq * sizeof(struct sound_trigger_module_descriptor));
  140. }
  141. free(modules);
  142. return NO_ERROR;
  143. }
  144. case ATTACH: {
  145. CHECK_INTERFACE(ISoundTriggerHwService, data, reply);
  146. String16 opPackageName;
  147. status_t status = data.readString16(&opPackageName);
  148. if (status != NO_ERROR) {
  149. return status;
  150. }
  151. sound_trigger_module_handle_t handle;
  152. data.read(&handle, sizeof(sound_trigger_module_handle_t));
  153. sp<ISoundTriggerClient> client =
  154. interface_cast<ISoundTriggerClient>(data.readStrongBinder());
  155. sp<ISoundTrigger> module;
  156. status = attach(opPackageName, handle, client, module);
  157. reply->writeInt32(status);
  158. if (module != 0) {
  159. reply->writeInt32(1);
  160. reply->writeStrongBinder(IInterface::asBinder(module));
  161. } else {
  162. reply->writeInt32(0);
  163. }
  164. return NO_ERROR;
  165. } break;
  166. case SET_CAPTURE_STATE: {
  167. CHECK_INTERFACE(ISoundTriggerHwService, data, reply);
  168. reply->writeInt32(setCaptureState((bool)data.readInt32()));
  169. return NO_ERROR;
  170. } break;
  171. default:
  172. return BBinder::onTransact(code, data, reply, flags);
  173. }
  174. }
  175. // ----------------------------------------------------------------------------
  176. }; // namespace android