1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "wakeup_adapter_listener.h" 16 17 #include <thread> 18 #include "intell_voice_log.h" 19 #include "v1_2/intell_voice_engine_types.h" 20 21 #define LOG_TAG "WakeupAdapterListener" 22 23 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_2; 24 25 namespace OHOS { 26 namespace IntellVoiceEngine { WakeupAdapterListener(OnWakeupEventCb wakeupEventCb)27WakeupAdapterListener::WakeupAdapterListener(OnWakeupEventCb wakeupEventCb) : wakeupEventCb_(wakeupEventCb) 28 { 29 INTELL_VOICE_LOG_INFO("constructor"); 30 } 31 ~WakeupAdapterListener()32WakeupAdapterListener::~WakeupAdapterListener() 33 { 34 INTELL_VOICE_LOG_INFO("destructor"); 35 } 36 SetCallback(const sptr<IIntelligentVoiceEngineCallback> & cb)37void WakeupAdapterListener::SetCallback(const sptr<IIntelligentVoiceEngineCallback> &cb) 38 { 39 INTELL_VOICE_LOG_INFO("enter"); 40 std::lock_guard<std::mutex> lock(mutex_); 41 if (cb == nullptr) { 42 INTELL_VOICE_LOG_INFO("clear callback"); 43 cb_ = nullptr; 44 return; 45 } 46 cb_ = cb; 47 if (historyEvent_ != nullptr) { 48 cb_->OnIntellVoiceEngineEvent(*(historyEvent_.get())); 49 historyEvent_ = nullptr; 50 } 51 } 52 GetCallbackStatus()53std::string WakeupAdapterListener::GetCallbackStatus() 54 { 55 INTELL_VOICE_LOG_INFO("enter"); 56 std::lock_guard<std::mutex> lock(mutex_); 57 if (cb_ == nullptr) { 58 INTELL_VOICE_LOG_WARN("cb_ is nullptr"); 59 return "false"; 60 } 61 return "true"; 62 } 63 OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent & event)64void WakeupAdapterListener::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent &event) 65 { 66 INTELL_VOICE_LOG_INFO("enter"); 67 wakeupEventCb_(event); 68 } 69 Notify(const IntellVoiceEngineCallBackEvent & event)70void WakeupAdapterListener::Notify(const IntellVoiceEngineCallBackEvent &event) 71 { 72 std::lock_guard<std::mutex> lock(mutex_); 73 if (cb_ == nullptr) { 74 INTELL_VOICE_LOG_WARN("cb_ is nullptr"); 75 BackupCallBackEvent(event); 76 return; 77 } 78 79 historyEvent_ = nullptr; 80 if ((event.msgId == OHOS::HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) || 81 (event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>( 82 INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE))) { 83 cb_->OnIntellVoiceEngineEvent(event); 84 } 85 } 86 BackupCallBackEvent(const IntellVoiceEngineCallBackEvent & event)87void WakeupAdapterListener::BackupCallBackEvent(const IntellVoiceEngineCallBackEvent &event) 88 { 89 if ((event.msgId == OHOS::HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) || ( 90 event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>( 91 INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE))) { 92 INTELL_VOICE_LOG_INFO("backup callBackEvent, msg id:%{public}d", event.msgId); 93 94 historyEvent_ = std::make_shared<IntellVoiceEngineCallBackEvent>(); 95 if (historyEvent_ == nullptr) { 96 INTELL_VOICE_LOG_INFO("historyEvent_ is nullptr"); 97 return; 98 } 99 100 historyEvent_->msgId = event.msgId; 101 historyEvent_->result = event.result; 102 historyEvent_->info = event.info; 103 } else { 104 INTELL_VOICE_LOG_WARN("unknow msg id:%{public}d", event.msgId); 105 } 106 } 107 } 108 }