1 /*
2 * Copyright (c) 2022 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
16 #include "daudio_hdf_operate.h"
17
18 #include <hdf_io_service_if.h>
19 #include <hdf_base.h>
20
21 #include "daudio_errorcode.h"
22 #include "daudio_log.h"
23
24 #undef DH_LOG_TAG
25 #define DH_LOG_TAG "DAudioHdfServStatListener"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 IMPLEMENT_SINGLE_INSTANCE(DaudioHdfOperate);
OnReceive(const ServiceStatus & status)30 void DAudioHdfServStatListener::OnReceive(const ServiceStatus& status)
31 {
32 DHLOGI("Service status on receive.");
33 if (status.serviceName == AUDIO_SERVICE_NAME || status.serviceName == AUDIOEXT_SERVICE_NAME) {
34 callback_(status);
35 }
36 }
37
LoadDaudioHDFImpl()38 int32_t DaudioHdfOperate::LoadDaudioHDFImpl()
39 {
40 if (audioServStatus_ == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START &&
41 audioextServStatus_ == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) {
42 DHLOGD("Service has already start.");
43 return DH_SUCCESS;
44 }
45 servMgr_ = IServiceManager::Get();
46 devmgr_ = IDeviceManager::Get();
47 CHECK_NULL_RETURN(servMgr_, ERR_DH_AUDIO_NULLPTR);
48 CHECK_NULL_RETURN(devmgr_, ERR_DH_AUDIO_NULLPTR);
49
50 ::OHOS::sptr<IServStatListener> listener(
51 new DAudioHdfServStatListener(DAudioHdfServStatListener::StatusCallback([&](const ServiceStatus& status) {
52 DHLOGI("Load audio service status callback, serviceName: %s, status: %d",
53 status.serviceName.c_str(), status.status);
54 std::unique_lock<std::mutex> lock(hdfOperateMutex_);
55 if (status.serviceName == AUDIO_SERVICE_NAME) {
56 audioServStatus_ = status.status;
57 hdfOperateCon_.notify_one();
58 } else if (status.serviceName == AUDIOEXT_SERVICE_NAME) {
59 audioextServStatus_ = status.status;
60 hdfOperateCon_.notify_one();
61 }
62 })));
63 if (servMgr_->RegisterServiceStatusListener(listener, DEVICE_CLASS_AUDIO) != HDF_SUCCESS) {
64 DHLOGE("Failed to register the service status listener.");
65 return ERR_DH_AUDIO_NULLPTR;
66 }
67
68 if (devmgr_->LoadDevice(AUDIO_SERVICE_NAME) != HDF_SUCCESS) {
69 DHLOGE("Load audio service failed!");
70 return ERR_DH_AUDIO_FAILED;
71 }
72 if (WaitLoadService(audioServStatus_, AUDIO_SERVICE_NAME) != DH_SUCCESS) {
73 DHLOGE("Wait load audio service failed!");
74 return ERR_DH_AUDIO_FAILED;
75 }
76
77 if (devmgr_->LoadDevice(AUDIOEXT_SERVICE_NAME) != HDF_SUCCESS) {
78 DHLOGE("Load provider service failed!");
79 return ERR_DH_AUDIO_FAILED;
80 }
81 if (WaitLoadService(audioextServStatus_, AUDIOEXT_SERVICE_NAME) != DH_SUCCESS) {
82 DHLOGE("Wait load provider service failed!");
83 return ERR_DH_AUDIO_FAILED;
84 }
85
86 if (servMgr_->UnregisterServiceStatusListener(listener) != HDF_SUCCESS) {
87 DHLOGE("Failed to unregister the service status listener.");
88 }
89 return DH_SUCCESS;
90 }
91
WaitLoadService(const uint16_t & servStatus,const std::string & servName)92 int32_t DaudioHdfOperate::WaitLoadService(const uint16_t& servStatus, const std::string& servName)
93 {
94 std::unique_lock<std::mutex> lock(hdfOperateMutex_);
95 hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME), [servStatus] {
96 return (servStatus == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START);
97 });
98
99 if (servStatus != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) {
100 DHLOGE("Wait load service %s failed, status %d", servName.c_str(), servStatus);
101 return ERR_DH_AUDIO_FAILED;
102 }
103
104 return DH_SUCCESS;
105 }
106
UnLoadDaudioHDFImpl()107 int32_t DaudioHdfOperate::UnLoadDaudioHDFImpl()
108 {
109 DHLOGI("UnLoad daudio hdf impl begin!");
110 devmgr_ = IDeviceManager::Get();
111 CHECK_NULL_RETURN(devmgr_, ERR_DH_AUDIO_NULLPTR);
112
113 int32_t ret = devmgr_->UnloadDevice(AUDIO_SERVICE_NAME);
114 if (ret != HDF_SUCCESS) {
115 DHLOGE("Unload audio service failed, ret: %d", ret);
116 }
117 ret = devmgr_->UnloadDevice(AUDIOEXT_SERVICE_NAME);
118 if (ret != HDF_SUCCESS) {
119 DHLOGE("Unload provider service failed, ret: %d", ret);
120 }
121 audioServStatus_ = INVALID_VALUE;
122 audioextServStatus_ = INVALID_VALUE;
123 DHLOGI("UnLoad daudio hdf impl end!");
124 return DH_SUCCESS;
125 }
126 } // namespace DistributedHardware
127 } // namespace OHOS