ISoundTrigger.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "ISoundTrigger"
  18. #include <utils/Log.h>
  19. #include <utils/Errors.h>
  20. #include <binder/IMemory.h>
  21. #include <soundtrigger/ISoundTrigger.h>
  22. #include <soundtrigger/ISoundTriggerHwService.h>
  23. #include <soundtrigger/ISoundTriggerClient.h>
  24. #include <system/sound_trigger.h>
  25. namespace android {
  26. enum {
  27. DETACH = IBinder::FIRST_CALL_TRANSACTION,
  28. LOAD_SOUND_MODEL,
  29. UNLOAD_SOUND_MODEL,
  30. START_RECOGNITION,
  31. STOP_RECOGNITION,
  32. GET_MODEL_STATE,
  33. };
  34. class BpSoundTrigger: public BpInterface<ISoundTrigger>
  35. {
  36. public:
  37. explicit BpSoundTrigger(const sp<IBinder>& impl)
  38. : BpInterface<ISoundTrigger>(impl)
  39. {
  40. }
  41. void detach()
  42. {
  43. ALOGV("detach");
  44. Parcel data, reply;
  45. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  46. remote()->transact(DETACH, data, &reply);
  47. }
  48. status_t loadSoundModel(const sp<IMemory>& modelMemory,
  49. sound_model_handle_t *handle)
  50. {
  51. if (modelMemory == 0 || handle == NULL) {
  52. return BAD_VALUE;
  53. }
  54. Parcel data, reply;
  55. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  56. data.writeStrongBinder(IInterface::asBinder(modelMemory));
  57. status_t status = remote()->transact(LOAD_SOUND_MODEL, data, &reply);
  58. if (status != NO_ERROR) {
  59. return status;
  60. }
  61. status = (status_t)reply.readInt32();
  62. if (status == NO_ERROR) {
  63. reply.read(handle, sizeof(sound_model_handle_t));
  64. }
  65. return status;
  66. }
  67. virtual status_t unloadSoundModel(sound_model_handle_t handle)
  68. {
  69. Parcel data, reply;
  70. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  71. data.write(&handle, sizeof(sound_model_handle_t));
  72. status_t status = remote()->transact(UNLOAD_SOUND_MODEL, data, &reply);
  73. if (status == NO_ERROR) {
  74. status = (status_t)reply.readInt32();
  75. }
  76. return status;
  77. }
  78. virtual status_t startRecognition(sound_model_handle_t handle,
  79. const sp<IMemory>& dataMemory)
  80. {
  81. Parcel data, reply;
  82. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  83. data.write(&handle, sizeof(sound_model_handle_t));
  84. if (dataMemory == 0) {
  85. data.writeInt32(0);
  86. } else {
  87. data.writeInt32(dataMemory->size());
  88. }
  89. data.writeStrongBinder(IInterface::asBinder(dataMemory));
  90. status_t status = remote()->transact(START_RECOGNITION, data, &reply);
  91. if (status == NO_ERROR) {
  92. status = (status_t)reply.readInt32();
  93. }
  94. return status;
  95. }
  96. virtual status_t stopRecognition(sound_model_handle_t handle)
  97. {
  98. Parcel data, reply;
  99. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  100. data.write(&handle, sizeof(sound_model_handle_t));
  101. status_t status = remote()->transact(STOP_RECOGNITION, data, &reply);
  102. if (status == NO_ERROR) {
  103. status = (status_t)reply.readInt32();
  104. }
  105. return status;
  106. }
  107. virtual status_t getModelState(sound_model_handle_t handle)
  108. {
  109. Parcel data, reply;
  110. data.writeInterfaceToken(ISoundTrigger::getInterfaceDescriptor());
  111. data.write(&handle, sizeof(sound_model_handle_t));
  112. status_t status = remote()->transact(GET_MODEL_STATE, data, &reply);
  113. if (status == NO_ERROR) {
  114. status = (status_t)reply.readInt32();
  115. }
  116. return status;
  117. }
  118. };
  119. IMPLEMENT_META_INTERFACE(SoundTrigger, "android.hardware.ISoundTrigger");
  120. // ----------------------------------------------------------------------
  121. status_t BnSoundTrigger::onTransact(
  122. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  123. {
  124. switch(code) {
  125. case DETACH: {
  126. ALOGV("DETACH");
  127. CHECK_INTERFACE(ISoundTrigger, data, reply);
  128. detach();
  129. return NO_ERROR;
  130. } break;
  131. case LOAD_SOUND_MODEL: {
  132. CHECK_INTERFACE(ISoundTrigger, data, reply);
  133. sp<IMemory> modelMemory = interface_cast<IMemory>(
  134. data.readStrongBinder());
  135. sound_model_handle_t handle;
  136. status_t status = loadSoundModel(modelMemory, &handle);
  137. reply->writeInt32(status);
  138. if (status == NO_ERROR) {
  139. reply->write(&handle, sizeof(sound_model_handle_t));
  140. }
  141. return NO_ERROR;
  142. }
  143. case UNLOAD_SOUND_MODEL: {
  144. CHECK_INTERFACE(ISoundTrigger, data, reply);
  145. sound_model_handle_t handle;
  146. data.read(&handle, sizeof(sound_model_handle_t));
  147. status_t status = unloadSoundModel(handle);
  148. reply->writeInt32(status);
  149. return NO_ERROR;
  150. }
  151. case START_RECOGNITION: {
  152. CHECK_INTERFACE(ISoundTrigger, data, reply);
  153. sound_model_handle_t handle;
  154. data.read(&handle, sizeof(sound_model_handle_t));
  155. sp<IMemory> dataMemory;
  156. if (data.readInt32() != 0) {
  157. dataMemory = interface_cast<IMemory>(data.readStrongBinder());
  158. }
  159. status_t status = startRecognition(handle, dataMemory);
  160. reply->writeInt32(status);
  161. return NO_ERROR;
  162. }
  163. case STOP_RECOGNITION: {
  164. CHECK_INTERFACE(ISoundTrigger, data, reply);
  165. sound_model_handle_t handle;
  166. data.read(&handle, sizeof(sound_model_handle_t));
  167. status_t status = stopRecognition(handle);
  168. reply->writeInt32(status);
  169. return NO_ERROR;
  170. }
  171. case GET_MODEL_STATE: {
  172. CHECK_INTERFACE(ISoundTrigger, data, reply);
  173. sound_model_handle_t handle;
  174. status_t status = UNKNOWN_ERROR;
  175. status_t ret = data.read(&handle, sizeof(sound_model_handle_t));
  176. if (ret == NO_ERROR) {
  177. status = getModelState(handle);
  178. }
  179. reply->writeInt32(status);
  180. return ret;
  181. }
  182. default:
  183. return BBinder::onTransact(code, data, reply, flags);
  184. }
  185. }
  186. // ----------------------------------------------------------------------------
  187. }; // namespace android