1 /*
2  * Copyright (c) 2022-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 
16 #include "daudio_manager_callback.h"
17 
18 #include <string>
19 #include <hdf_base.h>
20 #include <cstdlib>
21 #include "iservice_registry.h"
22 #include "iservmgr_hdi.h"
23 #include "iproxy_broker.h"
24 
25 #include "daudio_constants.h"
26 #include "daudio_errorcode.h"
27 #include "daudio_hdf_operate.h"
28 #include "daudio_hdi_handler.h"
29 #include "daudio_hitrace.h"
30 #include "daudio_log.h"
31 #include "daudio_util.h"
32 
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "DAudioHdiHandler"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 IMPLEMENT_SINGLE_INSTANCE(DAudioHdiHandler);
39 
DAudioHdiHandler()40 DAudioHdiHandler::DAudioHdiHandler()
41 {
42     DHLOGD("Distributed audio hdi handler construct.");
43     audioHdiRecipient_ = new AudioHdiRecipient();
44 }
45 
~DAudioHdiHandler()46 DAudioHdiHandler::~DAudioHdiHandler()
47 {
48     DHLOGD("Distributed audio hdi handler deconstructed.");
49 }
50 
InitHdiHandler()51 int32_t DAudioHdiHandler::InitHdiHandler()
52 {
53     DHLOGI("Init hdi handler.");
54     if (audioSrvHdf_ != nullptr) {
55         return DH_SUCCESS;
56     }
57 
58     DHLOGD("Load hdf driver start.");
59     int32_t ret = DaudioHdfOperate::GetInstance().LoadDaudioHDFImpl();
60     if (ret != DH_SUCCESS) {
61         DHLOGE("Load hdf driver failed, ret: %{public}d", ret);
62         return ret;
63     }
64     DHLOGI("Load hdf driver end.");
65 
66     audioSrvHdf_ = IDAudioManager::Get(HDF_AUDIO_SERVICE_NAME.c_str(), false);
67     CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
68     remote_ = OHOS::HDI::hdi_objcast<IDAudioManager>(audioSrvHdf_);
69     CHECK_NULL_RETURN(remote_, ERR_DH_AUDIO_NULLPTR);
70     remote_->AddDeathRecipient(audioHdiRecipient_);
71     DHLOGD("Init hdi handler success.");
72     return DH_SUCCESS;
73 }
74 
UninitHdiHandler()75 int32_t DAudioHdiHandler::UninitHdiHandler()
76 {
77     DHLOGI("Unload hdf driver start.");
78     CHECK_NULL_RETURN(remote_, ERR_DH_AUDIO_NULLPTR);
79     remote_->RemoveDeathRecipient(audioHdiRecipient_);
80     CHECK_NULL_RETURN(audioSrvHdf_, DH_SUCCESS);
81 
82     int32_t ret = DaudioHdfOperate::GetInstance().UnLoadDaudioHDFImpl();
83     if (ret != DH_SUCCESS) {
84         DHLOGE("Unload hdf driver failed, ret: %{public}d", ret);
85         return ret;
86     }
87     DHLOGD("Uninit hdi handler success.");
88     return DH_SUCCESS;
89 }
90 
RegisterAudioDevice(const std::string & devId,const int32_t dhId,const std::string & capability,const std::shared_ptr<IDAudioHdiCallback> & callbackObjParam)91 int32_t DAudioHdiHandler::RegisterAudioDevice(const std::string &devId, const int32_t dhId,
92     const std::string &capability, const std::shared_ptr<IDAudioHdiCallback> &callbackObjParam)
93 {
94     DHLOGI("Register audio device, adpname: %{public}s, dhId: %{public}d", GetAnonyString(devId).c_str(), dhId);
95     CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
96     std::string searchKey;
97     switch (GetDevTypeByDHId(dhId)) {
98         case AUDIO_DEVICE_TYPE_SPEAKER:
99             searchKey = devId + "Speaker" + std::to_string(dhId);
100             break;
101         case AUDIO_DEVICE_TYPE_MIC:
102             searchKey = devId + "Mic" + std::to_string(dhId);
103             break;
104         case AUDIO_DEVICE_TYPE_UNKNOWN:
105         default:
106             DHLOGE("Unknown audio device.");
107             return ERR_DH_AUDIO_NOT_SUPPORT;
108     }
109     {
110         std::lock_guard<std::mutex> devLck(devMapMtx_);
111         auto call = mapAudioMgrCallback_.find(searchKey);
112         if (call == mapAudioMgrCallback_.end()) {
113             const sptr<DAudioManagerCallback> callbackptr(new DAudioManagerCallback(callbackObjParam));
114             mapAudioMgrCallback_.emplace(searchKey, callbackptr);
115         }
116         auto dhIds = mapAudioMgrDhIds_.find(devId);
117         if (dhIds != mapAudioMgrDhIds_.end()) {
118             dhIds->second.insert(dhId);
119         } else {
120             std::set<int32_t> newDhIds;
121             newDhIds.insert(dhId);
122             mapAudioMgrDhIds_.emplace(devId, newDhIds);
123         }
124     }
125 
126     auto iter = mapAudioMgrCallback_.find(searchKey);
127     if (iter != mapAudioMgrCallback_.end()) {
128         int32_t res = audioSrvHdf_->RegisterAudioDevice(devId, dhId, capability, iter->second);
129         if (res != HDF_SUCCESS) {
130             DHLOGE("Call hdf proxy register failed, res: %{public}d", res);
131             return ERR_DH_AUDIO_HDI_CALL_FAILED;
132         }
133     }
134     return DH_SUCCESS;
135 }
136 
UnRegisterAudioDevice(const std::string & devId,const int32_t dhId)137 int32_t DAudioHdiHandler::UnRegisterAudioDevice(const std::string &devId, const int32_t dhId)
138 {
139     DHLOGI("Unregister audio device, adpname: %{public}s, dhId: %{public}d", GetAnonyString(devId).c_str(), dhId);
140     CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
141     int32_t res = audioSrvHdf_->UnRegisterAudioDevice(devId, dhId);
142     if (res != HDF_SUCCESS) {
143         DHLOGE("Call hdf proxy unregister failed, res: %{public}d", res);
144         return ERR_DH_AUDIO_HDI_CALL_FAILED;
145     }
146 
147     {
148         std::lock_guard<std::mutex> devLck(devMapMtx_);
149         auto iter = mapAudioMgrDhIds_.find(devId);
150         if (iter == mapAudioMgrDhIds_.end()) {
151             DHLOGE("Can not find register devId. devId: %{public}s", GetAnonyString(devId).c_str());
152             return ERR_DH_AUDIO_SA_CALLBACK_NOT_FOUND;
153         }
154 
155         iter->second.erase(dhId);
156         if (iter->second.empty()) {
157             mapAudioMgrDhIds_.erase(devId);
158         }
159     }
160     return DH_SUCCESS;
161 }
162 
ProcessEventMsg(const AudioEvent & audioEvent,DAudioEvent & newEvent)163 void DAudioHdiHandler::ProcessEventMsg(const AudioEvent &audioEvent, DAudioEvent &newEvent)
164 {
165     switch (audioEvent.type) {
166         case AudioEventType::NOTIFY_OPEN_SPEAKER_RESULT:
167             newEvent.type = AUDIO_EVENT_OPEN_SPK_RESULT;
168             break;
169         case AudioEventType::NOTIFY_CLOSE_SPEAKER_RESULT:
170             newEvent.type = AUDIO_EVENT_CLOSE_SPK_RESULT;
171             break;
172         case AudioEventType::NOTIFY_OPEN_MIC_RESULT:
173             newEvent.type = AUDIO_EVENT_OPEN_MIC_RESULT;
174             break;
175         case AudioEventType::NOTIFY_CLOSE_MIC_RESULT:
176             newEvent.type = AUDIO_EVENT_CLOSE_MIC_RESULT;
177             break;
178         case AudioEventType::VOLUME_CHANGE:
179             newEvent.type = AUDIO_EVENT_VOLUME_CHANGE;
180             break;
181         case AudioEventType::SPEAKER_CLOSED:
182             newEvent.type = AUDIO_EVENT_SPK_CLOSED;
183             break;
184         case AudioEventType::MIC_CLOSED:
185             newEvent.type = AUDIO_EVENT_MIC_CLOSED;
186             break;
187         case AudioEventType::AUDIO_FOCUS_CHANGE:
188             newEvent.type = AUDIO_EVENT_FOCUS_CHANGE;
189             break;
190         case AudioEventType::AUDIO_RENDER_STATE_CHANGE:
191             newEvent.type = AUDIO_EVENT_RENDER_STATE_CHANGE;
192             break;
193         case AudioEventType::NOTIFY_HDF_SPK_DUMP:
194             newEvent.type = AUDIO_EVENT_SPK_DUMP;
195             break;
196         case AudioEventType::NOTIFY_HDF_MIC_DUMP:
197             newEvent.type = AUDIO_EVENT_MIC_DUMP;
198             break;
199         default:
200             DHLOGE("Unsupport audio event.");
201             break;
202     }
203 }
204 
NotifyEvent(const std::string & devId,const int32_t dhId,const int32_t streamId,const AudioEvent & audioEvent)205 int32_t DAudioHdiHandler::NotifyEvent(const std::string &devId, const int32_t dhId,
206     const int32_t streamId, const AudioEvent &audioEvent)
207 {
208     DHLOGD("Notify event adpname: %{public}s, dhId: %{public}d, event type: %{public}d, event content: %{public}s.",
209         GetAnonyString(devId).c_str(), dhId, audioEvent.type, audioEvent.content.c_str());
210     DAudioEvent newEvent = {AUDIO_EVENT_UNKNOWN, audioEvent.content};
211     ProcessEventMsg(audioEvent, newEvent);
212 
213     CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
214     if (audioSrvHdf_->NotifyEvent(devId, dhId, streamId, newEvent) != HDF_SUCCESS) {
215         DHLOGE("Call hdf proxy NotifyEvent failed.");
216         return ERR_DH_AUDIO_HDI_CALL_FAILED;
217     }
218     return DH_SUCCESS;
219 }
220 
OnRemoteDied(const wptr<IRemoteObject> & remote)221 void DAudioHdiHandler::AudioHdiRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
222 {
223     DHLOGE("Exit the current process remote died.");
224     _Exit(0);
225 }
226 } // namespace DistributedHardware
227 } // namespace OHOS
228