1 /*
2 * Copyright (C) 2025-2025 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 "interoperable_communication_manager.h"
17 #include "interoperable_server_manager.h"
18 #include "interoperable_client_manager.h"
19 #include "parameters.h"
20 #include "telephony_log_wrapper.h"
21
22 namespace OHOS {
23 namespace Telephony {
InteroperableCommunicationManager()24 InteroperableCommunicationManager::InteroperableCommunicationManager()
25 {
26 devObserver_ = DelayedSingleton<InteroperableDeviceObserver>::GetInstance();
27 }
28
~InteroperableCommunicationManager()29 InteroperableCommunicationManager::~InteroperableCommunicationManager()
30 {
31 devObserver_ = nullptr;
32 }
33
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)34 void InteroperableCommunicationManager::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo)
35 {
36 TELEPHONY_LOGI("Interoperable device online, networkId = %{public}s, devrole = %{public}d",
37 deviceInfo.networkId, deviceInfo.deviceTypeId);
38 std::string networkId = deviceInfo.networkId;
39 std::string devName = deviceInfo.deviceName;
40 uint16_t devType = deviceInfo.deviceTypeId;
41 {
42 std::lock_guard<ffrt::mutex> lock(mutex_);
43 std::string ownType = system::GetParameter("const.product.devicetype", "");
44 if (devType == 0x0E && (ownType == "wearable" || ownType == "car")) { // 0x0E手机
45 role_ = InteroperableRole::OTHERS;
46 } else if (devType == 0x6D || devType == 0x83) { // 0x6D手表,0x83车机
47 role_ = InteroperableRole::PHONE;
48 } else {
49 TELEPHONY_LOGE("not interoperable device");
50 return;
51 }
52 auto iter = std::find(peerDevices_.begin(), peerDevices_.end(), networkId);
53 if (iter == peerDevices_.end()) {
54 peerDevices_.emplace_back(networkId);
55 }
56 if (dataController_ == nullptr) {
57 if (role_ == InteroperableRole::OTHERS) {
58 dataController_ = std::make_shared<InteroperableClientManager>();
59 } else {
60 dataController_ = std::make_shared<InteroperableServerManager>();
61 }
62 }
63 }
64 devObserver_->RegisterDevStatusCallback(dataController_);
65 devObserver_->OnDeviceOnline(networkId, devName, devType);
66 }
67
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)68 void InteroperableCommunicationManager::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo)
69 {
70 TELEPHONY_LOGI("Interoperable device offline, networkId = %{public}s, devrole = %{public}d",
71 deviceInfo.networkId, deviceInfo.deviceTypeId);
72 if (devObserver_ == nullptr) {
73 return;
74 }
75 std::string networkId = deviceInfo.networkId;
76 std::string devName = deviceInfo.deviceName;
77 uint16_t devType = deviceInfo.deviceTypeId;
78 std::string ownType = system::GetParameter("const.product.devicetype", "");
79 if (devType == 0x0E && ownType != "wearable" && ownType != "car") { // 0x0E手机
80 TELEPHONY_LOGE("not interoperable device");
81 return;
82 }
83 devObserver_->OnDeviceOffline(networkId, devName, devType);
84 devObserver_->UnRegisterDevStatusCallback(dataController_);
85
86 std::lock_guard<ffrt::mutex> lock(mutex_);
87 auto iter = std::find(peerDevices_.begin(), peerDevices_.end(), networkId);
88 if (iter != peerDevices_.end()) {
89 peerDevices_.erase(iter);
90 }
91 if (!peerDevices_.empty()) {
92 TELEPHONY_LOGI("device list not empty");
93 return;
94 }
95
96 role_ = InteroperableRole::UNKNOWN;
97 if (dataController_ != nullptr) {
98 dataController_.reset();
99 dataController_ = nullptr;
100 }
101 }
102
SetMuted(bool isMute)103 void InteroperableCommunicationManager::SetMuted(bool isMute)
104 {
105 std::lock_guard<ffrt::mutex> lock(mutex_);
106 if (dataController_ == nullptr) {
107 return;
108 }
109 TELEPHONY_LOGI("InteroperableCommunicationManager set muted %{public}d", isMute);
110 dataController_->SetMuted(isMute);
111 }
112
MuteRinger()113 void InteroperableCommunicationManager::MuteRinger()
114 {
115 std::lock_guard<ffrt::mutex> lock(mutex_);
116 if (dataController_ == nullptr) {
117 return;
118 }
119 TELEPHONY_LOGI("InteroperableCommunicationManager mute ringer");
120 dataController_->MuteRinger();
121 }
122
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)123 void InteroperableCommunicationManager::CallStateUpdated(
124 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
125 {
126 std::lock_guard<ffrt::mutex> lock(mutex_);
127 if (callObjectPtr == nullptr || dataController_ == nullptr) {
128 TELEPHONY_LOGE("callObjectPtr is null or dataController_ is null");
129 return;
130 }
131 if (nextState == TelCallState::CALL_STATUS_INCOMING) {
132 TELEPHONY_LOGI("interoperable new call created");
133 if (role_ == InteroperableRole::PHONE) {
134 TELEPHONY_LOGI("phone recv call");
135 return;
136 }
137 if (peerDevices_.empty()) {
138 TELEPHONY_LOGE("call created but no peer device");
139 return;
140 }
141 dataController_->OnCallCreated(callObjectPtr, peerDevices_.front());
142 } else if (nextState == TelCallState::CALL_STATUS_DISCONNECTED ||
143 nextState == TelCallState::CALL_STATUS_DISCONNECTING) {
144 TELEPHONY_LOGI("interoperable call destroyed");
145 dataController_->OnCallDestroyed();
146 }
147 }
148 }
149 }