1 /*
2 * Copyright (c) 2024 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 "headset_wakeup_wrapper.h"
16
17 #include <dlfcn.h>
18 #include "intell_voice_log.h"
19
20 #define LOG_TAG "HeadsetWakeupWrapper"
21
22 namespace OHOS {
23 namespace IntellVoiceEngine {
24 static const std::string HEADSET_SO_PATH = "libaam_connection_inner_client.z.so";
25
LoadHeadsetLib()26 int32_t HeadsetWakeupWrapper::LoadHeadsetLib()
27 {
28 const char *strError = nullptr;
29 headsetWakeupPriv_.handle = dlopen(HEADSET_SO_PATH.c_str(), RTLD_LAZY);
30 if (headsetWakeupPriv_.handle == nullptr) {
31 strError = dlerror();
32 INTELL_VOICE_LOG_ERROR("dlopen err:%{public}s", strError);
33 return -1;
34 }
35
36 (void)dlerror(); // clear existing error
37 headsetWakeupPriv_.getHeadsetWakeupInst = reinterpret_cast<GetHeadsetWakeupInstFunc>(dlsym(
38 headsetWakeupPriv_.handle, "GetHeadsetWakeupInst"));
39 if (headsetWakeupPriv_.getHeadsetWakeupInst == nullptr) {
40 strError = dlerror();
41 INTELL_VOICE_LOG_ERROR("dlsym GetHeadSetWakeupInst err:%{public}s", strError);
42 dlclose(headsetWakeupPriv_.handle);
43 headsetWakeupPriv_.handle = nullptr;
44 return -1;
45 }
46
47 INTELL_VOICE_LOG_INFO("load headset lib success");
48 return 0;
49 }
50
UnloadHeadsetLib()51 void HeadsetWakeupWrapper::UnloadHeadsetLib()
52 {
53 if (headsetWakeupPriv_.handle != nullptr) {
54 dlclose(headsetWakeupPriv_.handle);
55 headsetWakeupPriv_.handle = nullptr;
56 }
57 }
58
HeadsetWakeupWrapper()59 HeadsetWakeupWrapper::HeadsetWakeupWrapper()
60 {
61 INTELL_VOICE_LOG_INFO("enter");
62 if (LoadHeadsetLib() == 0) {
63 inst_ = headsetWakeupPriv_.getHeadsetWakeupInst();
64 if (inst_ == nullptr) {
65 INTELL_VOICE_LOG_ERROR("failed to get headset inst");
66 }
67 }
68 }
69
~HeadsetWakeupWrapper()70 HeadsetWakeupWrapper::~HeadsetWakeupWrapper()
71 {
72 INTELL_VOICE_LOG_INFO("enter");
73 UnloadHeadsetLib();
74 inst_ = nullptr;
75 }
76
ReadHeadsetStream(std::vector<uint8_t> & audioStream,bool & hasAwakeWord)77 int32_t HeadsetWakeupWrapper::ReadHeadsetStream(std::vector<uint8_t> &audioStream, bool &hasAwakeWord)
78 {
79 INTELL_VOICE_LOG_INFO("enter");
80 std::lock_guard<std::mutex> lock(mutex_);
81 if (inst_ == nullptr) {
82 INTELL_VOICE_LOG_ERROR("inst is nullptr");
83 return -1;
84 }
85
86 return inst_->ReadHeadsetStream(audioStream, hasAwakeWord);
87 }
88
NotifyVerifyResult(bool result)89 int32_t HeadsetWakeupWrapper::NotifyVerifyResult(bool result)
90 {
91 INTELL_VOICE_LOG_INFO("enter");
92 std::lock_guard<std::mutex> lock(mutex_);
93 if (inst_ == nullptr) {
94 INTELL_VOICE_LOG_ERROR("inst is nullptr");
95 return -1;
96 }
97
98 return inst_->NotifyVerifyResult(result);
99 }
100
StopReadingStream()101 int32_t HeadsetWakeupWrapper::StopReadingStream()
102 {
103 INTELL_VOICE_LOG_INFO("enter");
104 std::lock_guard<std::mutex> lock(mutex_);
105 if (inst_ == nullptr) {
106 INTELL_VOICE_LOG_ERROR("inst is nullptr");
107 return -1;
108 }
109
110 return inst_->StopReadingStream();
111 }
112
GetHeadsetAwakeState()113 int32_t HeadsetWakeupWrapper::GetHeadsetAwakeState()
114 {
115 INTELL_VOICE_LOG_INFO("enter");
116 std::lock_guard<std::mutex> lock(mutex_);
117 if (inst_ == nullptr) {
118 INTELL_VOICE_LOG_ERROR("inst is nullptr");
119 return -1;
120 }
121
122 return inst_->GetHeadsetAwakeState();
123 }
124 } // namespace IntellVoice
125 } // namespace OHOS
126