1 /*
2 * Copyright (c) 2025 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 "swing_service_wrapper.h"
16
17 #include <dlfcn.h>
18 #include "intell_voice_log.h"
19
20 #define LOG_TAG "SwingServiceWrapper"
21
22 namespace OHOS {
23 namespace IntellVoiceEngine {
24 static const std::string SWINGSERVICE_SO_PATH = "libswing_client.z.so";
25
LoadSwingServiceLib()26 int32_t SwingServiceWrapper::LoadSwingServiceLib()
27 {
28 if (swingServicePriv_.handle != nullptr) {
29 INTELL_VOICE_LOG_INFO("Npu lib has loaded");
30 return 0;
31 }
32
33 const char *strError = nullptr;
34 swingServicePriv_.handle = dlopen(SWINGSERVICE_SO_PATH.c_str(), RTLD_LAZY);
35 if (swingServicePriv_.handle == nullptr) {
36 strError = dlerror();
37 INTELL_VOICE_LOG_ERROR("dlopen err:%{public}s", strError);
38 return -1;
39 }
40
41 if (LoadLibFunction() > 0) {
42 UnloadSwingServiceLib();
43 return -1;
44 }
45
46 INTELL_VOICE_LOG_INFO("load swingService lib success");
47 return 0;
48 }
49
LoadLibFunction()50 int32_t SwingServiceWrapper::LoadLibFunction()
51 {
52 int32_t errCount = 0;
53 dlerror(); // clear existing error
54 LOAD_FUNCTION(swingServicePriv_.subscribeSwingEvent, SubscribeSwingEventPtr,
55 "SubscribeSwingEvent", swingServicePriv_, errCount);
56 LOAD_FUNCTION(swingServicePriv_.unSubscribeSwingEvent, UnSubscribeSwingEventPtr,
57 "UnSubscribeSwingEvent", swingServicePriv_, errCount);
58 return errCount;
59 }
60
UnloadSwingServiceLib()61 void SwingServiceWrapper::UnloadSwingServiceLib()
62 {
63 if (swingServicePriv_.handle != nullptr) {
64 dlclose(swingServicePriv_.handle);
65 swingServicePriv_.handle = nullptr;
66 }
67 swingServicePriv_.subscribeSwingEvent = nullptr;
68 swingServicePriv_.unSubscribeSwingEvent = nullptr;
69 }
70
SwingServiceWrapper()71 SwingServiceWrapper::SwingServiceWrapper()
72 {
73 INTELL_VOICE_LOG_INFO("enter");
74 if (LoadSwingServiceLib() != 0) {
75 INTELL_VOICE_LOG_ERROR("failed to get swingServicePriv_");
76 }
77 }
78
~SwingServiceWrapper()79 SwingServiceWrapper::~SwingServiceWrapper()
80 {
81 INTELL_VOICE_LOG_INFO("enter");
82 UnloadSwingServiceLib();
83 }
84
SubscribeSwingEvent(std::string swingEventType,std::string eventParams)85 int32_t SwingServiceWrapper::SubscribeSwingEvent(std::string swingEventType, std::string eventParams)
86 {
87 INTELL_VOICE_LOG_INFO("enter");
88 std::lock_guard<std::mutex> lock(mutex_);
89 if (swingServicePriv_.subscribeSwingEvent == nullptr) {
90 INTELL_VOICE_LOG_ERROR("failed to get subscribeSwingEvent");
91 return -1;
92 }
93 return swingServicePriv_.subscribeSwingEvent(swingEventType.c_str(), eventParams.c_str());
94 }
95
UnSubscribeSwingEvent(std::string swingEventType,std::string eventParams)96 int32_t SwingServiceWrapper::UnSubscribeSwingEvent(std::string swingEventType, std::string eventParams)
97 {
98 INTELL_VOICE_LOG_INFO("enter");
99 std::lock_guard<std::mutex> lock(mutex_);
100 if (swingServicePriv_.unSubscribeSwingEvent == nullptr) {
101 INTELL_VOICE_LOG_ERROR("failed to get unSubscribeSwingEvent");
102 return -1;
103 }
104 return swingServicePriv_.unSubscribeSwingEvent(swingEventType.c_str(), eventParams.c_str());
105 }
106 } // namespace IntellVoice
107 } // namespace OHOS
108