1 /*
2  * Copyright (C) 2023-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 #ifndef TELEPHONY_DISTRIBUTED_CALL_MANAGER_H
17 #define TELEPHONY_DISTRIBUTED_CALL_MANAGER_H
18 
19 #include <string>
20 #include <memory>
21 #include <map>
22 #include <mutex>
23 #include <atomic>
24 
25 #include "singleton.h"
26 #include "idcall_device_callback.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "system_ability_status_change_stub.h"
30 #include "call_manager_inner_type.h"
31 #include "distributed_call_proxy.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 class DistributedCallManager {
36     DECLARE_DELAYED_SINGLETON(DistributedCallManager)
37 public:
38     void Init();
39     int32_t AddDCallDevice(const std::string& devId);
40     int32_t RemoveDCallDevice(const std::string& devId);
41     void ClearDCallDevices();
42     void ClearConnectedDCallDevice();
43     std::string GetConnectedDCallDeviceAddr();
44     AudioDeviceType GetConnectedDCallDeviceType();
45     void SwitchOffDCallDeviceSync();
46     bool IsDCallDeviceSwitchedOn();
47     bool SwitchOnDCallDeviceSync(const AudioDevice& device);
48     void SwitchOnDCallDeviceAsync(const AudioDevice& device);
49     void SetCallState(bool isActive);
50     void DealDisconnectCall();
51 
52     int32_t OnDCallDeviceOnline(const std::string &devId);
53     int32_t OnDCallDeviceOffline(const std::string &devId);
54     void OnDCallSystemAbilityAdded(const std::string &deviceId);
55     void OnDCallSystemAbilityRemoved(const std::string &deviceId);
56 
57     void GetConnectedDCallDevice(AudioDevice& device);
58 
59     void OnDcCallSystemAbilityAdded();
60     void OnDcCallSystemAbilityRemoved();
61 
62 private:
63     class DistributedCallDeviceListener : public OHOS::DistributedHardware::IDCallDeviceCallback {
64     public:
65         DistributedCallDeviceListener() = default;
66         ~DistributedCallDeviceListener() = default;
67 
68         int32_t OnDCallDeviceOnline(const std::string &devId) override;
69         int32_t OnDCallDeviceOffline(const std::string &devId) override;
70     };
71 
72     bool CreateDAudioDevice(const std::string& devId, AudioDevice& device);
73     std::string GetDevIdFromAudioDevice(const AudioDevice& device);
74     void NotifyOnlineDCallDevices(std::vector<std::string> devices);
75     std::string GetConnectedDCallDeviceId();
76 
77     void SetConnectedDCallDevice(const AudioDevice& device);
78 
79     void InitDistributedCommunicationCall();
80 
81 private:
82     std::atomic<bool> isCallActived_ = false;
83     std::atomic<bool> dCallDeviceSwitchedOn_ = false;
84     std::mutex connectedDevMtx_;
85     std::mutex onlineDeviceMtx_;
86     std::string connectedDevId_;
87     AudioDevice connectedAudioDevice_;
88     AudioDevice currentAudioDevice_;
89     std::map<std::string, AudioDevice> onlineDCallDevices_;
90     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
91     std::shared_ptr<DistributedCallProxy> dcallProxy_ = nullptr;
92     std::shared_ptr<DistributedCallDeviceListener> dcallDeviceListener_ = nullptr;
93 
94     sptr<ISystemAbilityStatusChange> dcCallSaListener_ = nullptr;
95 };
96 
97 class DCallSystemAbilityListener : public SystemAbilityStatusChangeStub {
98 public:
99     DCallSystemAbilityListener() = default;
100     ~DCallSystemAbilityListener() = default;
101     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
103 };
104 
105 class DcCallSystemAbilityListener : public SystemAbilityStatusChangeStub {
106 public:
107     DcCallSystemAbilityListener() = default;
108     ~DcCallSystemAbilityListener() = default;
109     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
110     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
111 };
112 
113 } // namespace Telephony
114 } // namespace OHOS
115 #endif // TELEPHONY_DISTRIBUTED_CALL_MANAGER_H
116