1 /*
2 * Copyright (c) 2021-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 "device_status_listener.h"
17
18 #include <hdf_io_service_if.h>
19 #include <securec.h>
20
21 #include "audio_bluetooth_manager.h"
22 #include "audio_errors.h"
23 #include "audio_events.h"
24 #include "media_log.h"
25
26 namespace OHOS {
27 namespace AudioStandard {
28 namespace {
29 #ifdef PRODUCT_M40
30 const std::string AUDIO_HDI_SERVICE_NAME = "audio_adapter_service";
31 #else
32 const std::string AUDIO_HDI_SERVICE_NAME = "audio_hdi_service";
33 #endif
34 }
35
36 const uint8_t EVENT_PARAMS = 2;
37
GetInternalDeviceType(AudioDeviceType hdiDeviceType)38 static DeviceType GetInternalDeviceType(AudioDeviceType hdiDeviceType)
39 {
40 DeviceType internalDeviceType = DEVICE_TYPE_NONE;
41
42 switch (hdiDeviceType) {
43 case AudioDeviceType::HDF_AUDIO_HEADSET:
44 internalDeviceType = DEVICE_TYPE_WIRED_HEADSET;
45 break;
46 case AudioDeviceType::HDF_AUDIO_USB_HEADSET:
47 internalDeviceType = DEVICE_TYPE_USB_HEADSET;
48 break;
49 default:
50 internalDeviceType = DEVICE_TYPE_NONE;
51 break;
52 }
53
54 return internalDeviceType;
55 }
56
OnServiceStatusReceived(struct ServiceStatusListener * listener,struct ServiceStatus * serviceStatus)57 static void OnServiceStatusReceived(struct ServiceStatusListener *listener, struct ServiceStatus *serviceStatus)
58 {
59 CHECK_AND_RETURN_LOG(serviceStatus != nullptr, "Invalid ServiceStatus");
60 std::string info = serviceStatus->info;
61 MEDIA_DEBUG_LOG("OnServiceStatusReceived: [service name:%{public}s] [status:%{public}d] [info:%{public}s]",
62 serviceStatus->serviceName, serviceStatus->status, info.c_str());
63
64 if (serviceStatus->serviceName == AUDIO_HDI_SERVICE_NAME) {
65 DeviceStatusListener *devListener = reinterpret_cast<DeviceStatusListener *>(listener->priv);
66 CHECK_AND_RETURN_LOG(devListener != nullptr, "Invalid deviceStatusListener");
67
68 if (serviceStatus->status == SERVIE_STATUS_START) {
69 devListener->deviceObserver_.OnServiceConnected(AudioServiceIndex::HDI_SERVICE_INDEX);
70 } else if (serviceStatus->status == SERVIE_STATUS_CHANGE && !info.empty()) {
71 AudioDeviceType hdiDeviceType = HDF_AUDIO_DEVICE_UNKOWN;
72 AudioEventType hdiEventType = HDF_AUDIO_EVENT_UNKOWN;
73 if (sscanf_s(info.c_str(), "EVENT_TYPE=%d;DEVICE_TYPE=%d", &hdiEventType, &hdiDeviceType) < EVENT_PARAMS) {
74 MEDIA_ERR_LOG("[DeviceStatusListener]: Failed to scan info string");
75 return;
76 }
77
78 DeviceType internalDevice = GetInternalDeviceType(hdiDeviceType);
79 bool isConnected = (hdiEventType == HDF_AUDIO_DEVICE_ADD) ? true : false;
80 devListener->deviceObserver_.OnDeviceStatusUpdated(internalDevice, isConnected, devListener->privData_);
81 }
82 }
83 }
84
DeviceStatusListener(IDeviceStatusObserver & observer)85 DeviceStatusListener::DeviceStatusListener(IDeviceStatusObserver &observer)
86 : deviceObserver_(observer), hdiServiceManager_(nullptr), listener_(nullptr) {}
87
88 DeviceStatusListener::~DeviceStatusListener() = default;
89
RegisterDeviceStatusListener(void * privData)90 int32_t DeviceStatusListener::RegisterDeviceStatusListener(void *privData)
91 {
92 hdiServiceManager_ = HDIServiceManagerGet();
93 if (hdiServiceManager_ == nullptr) {
94 MEDIA_ERR_LOG("[DeviceStatusListener]: Get HDI service manager failed");
95 return ERR_OPERATION_FAILED;
96 }
97
98 privData_ = privData;
99 listener_ = HdiServiceStatusListenerNewInstance();
100 listener_->callback = OnServiceStatusReceived;
101 listener_->priv = (void *)this;
102 int32_t status = hdiServiceManager_->RegisterServiceStatusListener(hdiServiceManager_, listener_,
103 DeviceClass::DEVICE_CLASS_AUDIO);
104 if (status != HDF_SUCCESS) {
105 MEDIA_ERR_LOG("[DeviceStatusListener]: Register service status listener failed");
106 return ERR_OPERATION_FAILED;
107 }
108
109 MEDIA_DEBUG_LOG("call bluetooth interface");
110 OHOS::Bluetooth::GetProxy();
111 OHOS::Bluetooth::RegisterObserver(deviceObserver_);
112
113 return SUCCESS;
114 }
115
UnRegisterDeviceStatusListener()116 int32_t DeviceStatusListener::UnRegisterDeviceStatusListener()
117 {
118 if ((hdiServiceManager_ == nullptr) || (listener_ == nullptr)) {
119 return ERR_ILLEGAL_STATE;
120 }
121
122 int32_t status = hdiServiceManager_->UnregisterServiceStatusListener(hdiServiceManager_, listener_);
123 if (status != HDF_SUCCESS) {
124 MEDIA_ERR_LOG("[DeviceStatusListener]: UnRegister service status listener failed");
125 return ERR_OPERATION_FAILED;
126 }
127
128 hdiServiceManager_ = nullptr;
129 listener_ = nullptr;
130 privData_ = nullptr;
131
132 return SUCCESS;
133 }
134 } // namespace AudioStandard
135 } // namespace OHOS
136