• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (servMgr_ == nullptr || devmgr_ == nullptr) {
48         DHLOGE("Get hdi service manager or device manager failed!");
49         return ERR_DH_AUDIO_NULLPTR;
50     }
51 
52     ::OHOS::sptr<IServStatListener> listener(
53         new DAudioHdfServStatListener(DAudioHdfServStatListener::StatusCallback([&](const ServiceStatus& status) {
54             DHLOGI("Load audio service status callback, serviceName: %s, status: %d",
55                 status.serviceName.c_str(), status.status);
56             std::unique_lock<std::mutex> lock(hdfOperateMutex_);
57             if (status.serviceName == AUDIO_SERVICE_NAME) {
58                 audioServStatus_ = status.status;
59                 hdfOperateCon_.notify_one();
60             } else if (status.serviceName == AUDIOEXT_SERVICE_NAME) {
61                 audioextServStatus_ = status.status;
62                 hdfOperateCon_.notify_one();
63             }
64     })));
65     if (servMgr_->RegisterServiceStatusListener(listener, DEVICE_CLASS_AUDIO) != HDF_SUCCESS) {
66         DHLOGE("Failed to register the service status listener.");
67         return ERR_DH_AUDIO_NULLPTR;
68     }
69 
70     if (devmgr_->LoadDevice(AUDIO_SERVICE_NAME) != HDF_SUCCESS) {
71         DHLOGE("Load audio service failed!");
72         return ERR_DH_AUDIO_FAILED;
73     }
74     if (WaitLoadService(audioServStatus_, AUDIO_SERVICE_NAME) != DH_SUCCESS) {
75         DHLOGE("Wait load audio service failed!");
76         return ERR_DH_AUDIO_FAILED;
77     }
78 
79     if (devmgr_->LoadDevice(AUDIOEXT_SERVICE_NAME) != HDF_SUCCESS) {
80         DHLOGE("Load provider service failed!");
81         return ERR_DH_AUDIO_FAILED;
82     }
83     if (WaitLoadService(audioextServStatus_, AUDIOEXT_SERVICE_NAME) != DH_SUCCESS) {
84         DHLOGE("Wait load provider service failed!");
85         return ERR_DH_AUDIO_FAILED;
86     }
87 
88     if (servMgr_->UnregisterServiceStatusListener(listener) != HDF_SUCCESS) {
89         DHLOGE("Failed to unregister the service status listener.");
90     }
91     return DH_SUCCESS;
92 }
93 
WaitLoadService(const uint16_t & servStatus,const std::string & servName)94 int32_t DaudioHdfOperate::WaitLoadService(const uint16_t& servStatus, const std::string& servName)
95 {
96     std::unique_lock<std::mutex> lock(hdfOperateMutex_);
97     hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME), [servStatus] {
98         return (servStatus == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START);
99     });
100 
101     if (servStatus != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) {
102         DHLOGE("Wait load service %s failed, status %d", servName.c_str(), servStatus);
103         return ERR_DH_AUDIO_FAILED;
104     }
105 
106     return DH_SUCCESS;
107 }
108 
UnLoadDaudioHDFImpl()109 int32_t DaudioHdfOperate::UnLoadDaudioHDFImpl()
110 {
111     DHLOGI("UnLoad daudio hdf impl begin!");
112     devmgr_ = IDeviceManager::Get();
113     if (devmgr_ == nullptr) {
114         DHLOGE("Get hdi device manager failed!");
115         return ERR_DH_AUDIO_NULLPTR;
116     }
117 
118     int32_t ret = devmgr_->UnloadDevice(AUDIO_SERVICE_NAME);
119     if (ret != HDF_SUCCESS) {
120         DHLOGE("Unload audio service failed, ret: %d", ret);
121     }
122     ret = devmgr_->UnloadDevice(AUDIOEXT_SERVICE_NAME);
123     if (ret != HDF_SUCCESS) {
124         DHLOGE("Unload provider service failed, ret: %d", ret);
125     }
126     audioServStatus_ = INVALID_VALUE;
127     audioextServStatus_ = INVALID_VALUE;
128     DHLOGI("UnLoad daudio hdf impl end!");
129     return DH_SUCCESS;
130 }
131 } // namespace DistributedHardware
132 } // namespace OHOS