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_client_manager.h"
17 #include "telephony_log_wrapper.h"
18 #include "call_object_manager.h"
19 #include "transmission_manager.h"
20
21 namespace OHOS {
22 namespace Telephony {
23 constexpr const char* SOFTNET_SESSION_NAME = "ohos.telephony.callmanager.interoperable_communication";
24
OnCallCreated(const sptr<CallBase> & call,const std::string & networkId)25 void InteroperableClientManager::OnCallCreated(const sptr<CallBase> &call, const std::string &networkId)
26 {
27 ConnectRemote(networkId);
28 }
29
OnCallDestroyed()30 void InteroperableClientManager::OnCallDestroyed()
31 {
32 if (session_ != nullptr) {
33 TELEPHONY_LOGI("disconnect session_");
34 session_->Disconnect();
35 session_.reset();
36 session_ = nullptr;
37 }
38 std::unique_lock<ffrt::mutex> lock(mutex_);
39 phoneNum_ = "";
40 }
41
ConnectRemote(const std::string & networkId)42 void InteroperableClientManager::ConnectRemote(const std::string &networkId)
43 {
44 if (session_ != nullptr) {
45 return;
46 }
47 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
48 session_ = transMgr->CreateClientSession(shared_from_this());
49 if (session_ != nullptr) {
50 TELEPHONY_LOGI("connect session_");
51 session_->Connect(networkId, SOFTNET_SESSION_NAME, SOFTNET_SESSION_NAME, QOS_BW_BT);
52 }
53 }
54
OnConnected()55 void InteroperableClientManager::OnConnected()
56 {
57 TELEPHONY_LOGI("client on connected");
58 std::string phoneNum = "";
59 std::unique_lock<ffrt::mutex> lock(mutex_);
60 phoneNum = phoneNum_;
61 lock.unlock();
62
63 sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(phoneNum);
64 if (callPtr == nullptr) {
65 TELEPHONY_LOGE("client connect, get call failed");
66 return;
67 }
68 TELEPHONY_LOGI("phoneOrWatchDial[%{public}d]", callPtr->GetPhoneOrWatchDial());
69 if (callPtr->GetCallDirection() == CallDirection::CALL_DIRECTION_OUT &&
70 callPtr->GetPhoneOrWatchDial() == static_cast<int32_t>(PhoneOrWatchDial::WATCH_DIAL)) {
71 SendRequisiteDataToPeer(callPtr->GetAccountId(), callPtr->GetAccountNumber());
72 } else {
73 SendRequisiteDataQueryToPeer(phoneNum);
74 }
75 }
76
CallCreated(const sptr<CallBase> & call,const std::string & networkId)77 void InteroperableClientManager::CallCreated(const sptr<CallBase> &call, const std::string &networkId)
78 {
79 TELEPHONY_LOGI("client mgr call created");
80 std::unique_lock<ffrt::mutex> lock(mutex_);
81 phoneNum_ = call->GetAccountNumber();
82 lock.unlock();
83 ConnectRemote(networkId);
84 if (call->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING ||
85 call->GetTelCallState() == TelCallState::CALL_STATUS_WAITING) {
86 SendRequisiteDataQueryToPeer(call->GetAccountNumber()); // multi-call
87 }
88 }
89 } // namespace Telephony
90 } // namespace OHOS