Home | History | Annotate | Download | only in default
      1 /*
      2  * Copyright (C) 2018 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 
     17 #define LOG_TAG "SoundTriggerHw"
     18 
     19 #include "SoundTriggerHw.h"
     20 
     21 #include <utility>
     22 
     23 #include <android/hidl/allocator/1.0/IAllocator.h>
     24 #include <android/log.h>
     25 #include <hidlmemory/mapping.h>
     26 
     27 using android::hardware::hidl_memory;
     28 using android::hidl::allocator::V1_0::IAllocator;
     29 using android::hidl::memory::V1_0::IMemory;
     30 
     31 namespace android {
     32 namespace hardware {
     33 namespace soundtrigger {
     34 namespace V2_1 {
     35 namespace implementation {
     36 
     37 namespace {
     38 
     39 // Backs up by the vector with the contents of shared memory.
     40 // It is assumed that the passed hidl_vector is empty, so it's
     41 // not cleared if the memory is a null object.
     42 // The caller needs to keep the returned sp<IMemory> as long as
     43 // the data is needed.
     44 std::pair<bool, sp<IMemory>> memoryAsVector(const hidl_memory& m, hidl_vec<uint8_t>* vec) {
     45     sp<IMemory> memory;
     46     if (m.size() == 0) {
     47         return std::make_pair(true, memory);
     48     }
     49     memory = mapMemory(m);
     50     if (memory != nullptr) {
     51         memory->read();
     52         vec->setToExternal(static_cast<uint8_t*>(static_cast<void*>(memory->getPointer())),
     53                            memory->getSize());
     54         return std::make_pair(true, memory);
     55     }
     56     ALOGE("%s: Could not map HIDL memory to IMemory", __func__);
     57     return std::make_pair(false, memory);
     58 }
     59 
     60 // Moves the data from the vector into allocated shared memory,
     61 // emptying the vector.
     62 // It is assumed that the passed hidl_memory is a null object, so it's
     63 // not reset if the vector is empty.
     64 // The caller needs to keep the returned sp<IMemory> as long as
     65 // the data is needed.
     66 std::pair<bool, sp<IMemory>> moveVectorToMemory(hidl_vec<uint8_t>* v, hidl_memory* mem) {
     67     sp<IMemory> memory;
     68     if (v->size() == 0) {
     69         return std::make_pair(true, memory);
     70     }
     71     sp<IAllocator> ashmem = IAllocator::getService("ashmem");
     72     if (ashmem == 0) {
     73         ALOGE("Failed to retrieve ashmem allocator service");
     74         return std::make_pair(false, memory);
     75     }
     76     bool success = false;
     77     Return<void> r = ashmem->allocate(v->size(), [&](bool s, const hidl_memory& m) {
     78         success = s;
     79         if (success) *mem = m;
     80     });
     81     if (r.isOk() && success) {
     82         memory = hardware::mapMemory(*mem);
     83         if (memory != 0) {
     84             memory->update();
     85             memcpy(memory->getPointer(), v->data(), v->size());
     86             memory->commit();
     87             v->resize(0);
     88             return std::make_pair(true, memory);
     89         } else {
     90             ALOGE("Failed to map allocated ashmem");
     91         }
     92     } else {
     93         ALOGE("Failed to allocate %llu bytes from ashmem", (unsigned long long)v->size());
     94     }
     95     return std::make_pair(false, memory);
     96 }
     97 
     98 }  // namespace
     99 
    100 Return<void> SoundTriggerHw::loadSoundModel_2_1(
    101     const V2_1::ISoundTriggerHw::SoundModel& soundModel,
    102     const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
    103     V2_1::ISoundTriggerHw::loadSoundModel_2_1_cb _hidl_cb) {
    104     // It is assumed that legacy data vector is empty, thus making copy is cheap.
    105     V2_0::ISoundTriggerHw::SoundModel soundModel_2_0(soundModel.header);
    106     auto result = memoryAsVector(soundModel.data, &soundModel_2_0.data);
    107     if (result.first) {
    108         sp<SoundModelClient> client =
    109             new SoundModelClient_2_1(nextUniqueModelId(), cookie, callback);
    110         _hidl_cb(doLoadSoundModel(soundModel_2_0, client), client->getId());
    111         return Void();
    112     }
    113     _hidl_cb(-ENOMEM, 0);
    114     return Void();
    115 }
    116 
    117 Return<void> SoundTriggerHw::loadPhraseSoundModel_2_1(
    118     const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
    119     const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
    120     V2_1::ISoundTriggerHw::loadPhraseSoundModel_2_1_cb _hidl_cb) {
    121     V2_0::ISoundTriggerHw::PhraseSoundModel soundModel_2_0;
    122     // It is assumed that legacy data vector is empty, thus making copy is cheap.
    123     soundModel_2_0.common = soundModel.common.header;
    124     // Avoid copying phrases data.
    125     soundModel_2_0.phrases.setToExternal(
    126         const_cast<V2_0::ISoundTriggerHw::Phrase*>(soundModel.phrases.data()),
    127         soundModel.phrases.size());
    128     auto result = memoryAsVector(soundModel.common.data, &soundModel_2_0.common.data);
    129     if (result.first) {
    130         sp<SoundModelClient> client =
    131             new SoundModelClient_2_1(nextUniqueModelId(), cookie, callback);
    132         _hidl_cb(doLoadSoundModel((const V2_0::ISoundTriggerHw::SoundModel&)soundModel_2_0, client),
    133                  client->getId());
    134         return Void();
    135     }
    136     _hidl_cb(-ENOMEM, 0);
    137     return Void();
    138 }
    139 
    140 Return<int32_t> SoundTriggerHw::startRecognition_2_1(
    141     int32_t modelHandle, const V2_1::ISoundTriggerHw::RecognitionConfig& config) {
    142     // It is assumed that legacy data vector is empty, thus making copy is cheap.
    143     V2_0::ISoundTriggerHw::RecognitionConfig config_2_0(config.header);
    144     auto result = memoryAsVector(config.data, &config_2_0.data);
    145     return result.first ? startRecognition(modelHandle, config_2_0) : Return<int32_t>(-ENOMEM);
    146 }
    147 
    148 void SoundTriggerHw::SoundModelClient_2_1::recognitionCallback(
    149     struct sound_trigger_recognition_event* halEvent) {
    150     if (halEvent->type == SOUND_MODEL_TYPE_KEYPHRASE) {
    151         V2_0::ISoundTriggerHwCallback::PhraseRecognitionEvent event_2_0;
    152         convertPhaseRecognitionEventFromHal(
    153             &event_2_0, reinterpret_cast<sound_trigger_phrase_recognition_event*>(halEvent));
    154         event_2_0.common.model = mId;
    155         V2_1::ISoundTriggerHwCallback::PhraseRecognitionEvent event;
    156         event.phraseExtras.setToExternal(event_2_0.phraseExtras.data(),
    157                                          event_2_0.phraseExtras.size());
    158         auto result = moveVectorToMemory(&event_2_0.common.data, &event.common.data);
    159         if (result.first) {
    160             // The data vector is now empty, thus copying is cheap.
    161             event.common.header = event_2_0.common;
    162             mCallback->phraseRecognitionCallback_2_1(event, mCookie);
    163         }
    164     } else {
    165         V2_1::ISoundTriggerHwCallback::RecognitionEvent event;
    166         convertRecognitionEventFromHal(&event.header, halEvent);
    167         event.header.model = mId;
    168         auto result = moveVectorToMemory(&event.header.data, &event.data);
    169         if (result.first) {
    170             mCallback->recognitionCallback_2_1(event, mCookie);
    171         }
    172     }
    173 }
    174 
    175 void SoundTriggerHw::SoundModelClient_2_1::soundModelCallback(
    176     struct sound_trigger_model_event* halEvent) {
    177     V2_1::ISoundTriggerHwCallback::ModelEvent event;
    178     convertSoundModelEventFromHal(&event.header, halEvent);
    179     event.header.model = mId;
    180     auto result = moveVectorToMemory(&event.header.data, &event.data);
    181     if (result.first) {
    182         mCallback->soundModelCallback_2_1(event, mCookie);
    183     }
    184 }
    185 
    186 ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* /* name */) {
    187     return (new SoundTriggerHw())->getInterface();
    188 }
    189 
    190 }  // namespace implementation
    191 }  // namespace V2_1
    192 }  // namespace soundtrigger
    193 }  // namespace hardware
    194 }  // namespace android
    195