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