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 #ifndef OHOS_DAUDIO_HDF_OPERATE_H 17 #define OHOS_DAUDIO_HDF_OPERATE_H 18 19 #include <condition_variable> 20 #include <mutex> 21 22 #include "iservstat_listener_hdi.h" 23 #include "idevmgr_hdi.h" 24 #include "iservmgr_hdi.h" 25 #include "single_instance.h" 26 27 const std::string AUDIO_SERVICE_NAME = "daudio_primary_service"; 28 const std::string AUDIOEXT_SERVICE_NAME = "daudio_ext_service"; 29 constexpr uint16_t INVALID_VALUE = 0xffff; 30 constexpr int32_t WAIT_TIME = 5000; 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; 35 using OHOS::HDI::ServiceManager::V1_0::IServiceManager; 36 using OHOS::HDI::ServiceManager::V1_0::IServStatListener; 37 using OHOS::HDI::ServiceManager::V1_0::ServiceStatus; 38 using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub; 39 40 class DaudioHdfOperate { 41 DECLARE_SINGLE_INSTANCE(DaudioHdfOperate); 42 43 public: 44 int32_t LoadDaudioHDFImpl(); 45 int32_t UnLoadDaudioHDFImpl(); 46 47 private: 48 int32_t WaitLoadService(const uint16_t& servStatus, const std::string& servName); 49 50 private: 51 OHOS::sptr<IDeviceManager> devmgr_; 52 OHOS::sptr<IServiceManager> servMgr_; 53 uint16_t audioServStatus_ = INVALID_VALUE; 54 uint16_t audioextServStatus_ = INVALID_VALUE; 55 std::condition_variable hdfOperateCon_; 56 std::mutex hdfOperateMutex_; 57 }; 58 59 class DAudioHdfServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub { 60 public: 61 using StatusCallback = std::function<void(const ServiceStatus &)>; DAudioHdfServStatListener(StatusCallback callback)62 explicit DAudioHdfServStatListener(StatusCallback callback) : callback_(std::move(callback)) 63 { 64 } 65 ~DAudioHdfServStatListener() override = default; 66 void OnReceive(const ServiceStatus& status) override; 67 68 private: 69 StatusCallback callback_; 70 }; 71 } // namespace DistributedHardware 72 } // namespace OHOS 73 #endif // OHOS_DAUDIO_HDF_OPERATE_H