• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "voip_call_connection.h"
17 
18 #include "bluetooth_call_connection.h"
19 #include "telephony_log_wrapper.h"
20 #include "call_control_manager.h"
21 #include "settings_datashare_helper.h"
22 #include "report_call_info_handler.h"
23 
24 namespace OHOS {
25 namespace Telephony {
BluetoothCallConnection()26 BluetoothCallConnection::BluetoothCallConnection() {}
~BluetoothCallConnection()27 BluetoothCallConnection::~BluetoothCallConnection() {}
28 
Dial(DialParaInfo & info)29 int32_t BluetoothCallConnection::Dial(DialParaInfo &info)
30 {
31     TELEPHONY_LOGI("bluetooth dial start!");
32     if (!GetSupportBtCall()) {
33         return CALL_ERR_BLUETOOTH_CONNECTION_FAILED;
34     }
35     std::string number = info.number;
36     if (number.empty()) {
37         TELEPHONY_LOGE("bluetooth call number is null!");
38         return CALL_ERR_DIAL_FAILED;
39     }
40     if (macAddress_.empty()) {
41         TELEPHONY_LOGE("bluetooth call macaddress is empty");
42         return CALL_ERR_DIAL_FAILED;
43     }
44     Bluetooth::HandsFreeUnit *profile = Bluetooth::HandsFreeUnit::GetProfile();
45     if (profile == nullptr) {
46         TELEPHONY_LOGE("profile is nullptr");
47         return CALL_ERR_DIAL_FAILED;
48     }
49 
50     Bluetooth::BluetoothRemoteDevice device(macAddress_);
51     profile->StartDial(device, number);
52     return TELEPHONY_SUCCESS;
53 }
54 
SetMacAddress(const std::string & macAddress)55 void BluetoothCallConnection::SetMacAddress(const std::string &macAddress)
56 {
57     macAddress_ = macAddress;
58     if (macAddress_.empty()) {
59         TELEPHONY_LOGE("BluetoothCallConnection macAddress is empty");
60     }
61 }
62 
GetMacAddress()63 std::string BluetoothCallConnection::GetMacAddress()
64 {
65     return macAddress_;
66 }
67 
ConnectBtSco()68 int32_t BluetoothCallConnection::ConnectBtSco()
69 {
70     TELEPHONY_LOGI("connectBtSco event");
71     Bluetooth::HandsFreeUnit *profile = Bluetooth::HandsFreeUnit::GetProfile();
72     if (profile != nullptr) {
73         Bluetooth::BluetoothRemoteDevice device(macAddress_, 1);
74         bool isOK = profile->ConnectSco(device);
75         if (!isOK) {
76             TELEPHONY_LOGE("connectBtSco failed!");
77             return TELEPHONY_ERR_FAIL;
78         }
79         return TELEPHONY_SUCCESS;
80     } else {
81         TELEPHONY_LOGE("profile is nullptr");
82     }
83     return TELEPHONY_ERR_FAIL;
84 }
85 
DisConnectBtSco()86 int32_t BluetoothCallConnection::DisConnectBtSco()
87 {
88     TELEPHONY_LOGI("disconnectBtSco event");
89     Bluetooth::HandsFreeUnit *profile = Bluetooth::HandsFreeUnit::GetProfile();
90     if (profile != nullptr) {
91         Bluetooth::BluetoothRemoteDevice device(macAddress_, 1);
92         bool isOK = profile->DisconnectSco(device);
93         if (!isOK) {
94             TELEPHONY_LOGE("disconnectBtSco failed!");
95             return TELEPHONY_ERR_FAIL;
96         }
97         return TELEPHONY_SUCCESS;
98     } else {
99         TELEPHONY_LOGE("profile is nullptr");
100     }
101     return TELEPHONY_ERR_FAIL;
102 }
103 
GetBtScoIsConnected()104 bool BluetoothCallConnection::GetBtScoIsConnected()
105 {
106     TELEPHONY_LOGI("getBtScoconnect state event");
107     Bluetooth::HandsFreeUnit *profile = Bluetooth::HandsFreeUnit::GetProfile();
108     if (profile != nullptr) {
109         Bluetooth::BluetoothRemoteDevice device(macAddress_, 1);
110         int state = profile->GetScoState(device);
111         if (state == static_cast<int>(Bluetooth::HfpScoConnectState::SCO_CONNECTED)) {
112             TELEPHONY_LOGE("BtScoconnect on!");
113             return true;
114         }
115     } else {
116         TELEPHONY_LOGE("profile is nullptr");
117     }
118     TELEPHONY_LOGI("BtScoconnect off!");
119     return false;
120 }
121 
SetHfpConnected(bool isHfpConnected)122 void BluetoothCallConnection::SetHfpConnected(bool isHfpConnected)
123 {
124     isHfpConnected_ = isHfpConnected;
125     TELEPHONY_LOGI("Set hfpCconnectd=%{public}d", isHfpConnected_);
126     if (!isHfpConnected_) {
127         HfpDisConnectedEndBtCall();
128     }
129 }
130 
GetSupportBtCall()131 bool BluetoothCallConnection::GetSupportBtCall()
132 {
133     Bluetooth::BluetoothRemoteDevice device(macAddress_);
134     bool isAclConnected = device.IsAclConnected();
135     if (isAclConnected) {
136         TELEPHONY_LOGI("Watch Support Bluetooth Call.");
137     } else {
138         TELEPHONY_LOGE("Watch not Support Bluetooth Call.");
139     }
140     return isAclConnected;
141 }
142 
SetBtCallScoConnected(bool isBtCallScoConnected)143 void BluetoothCallConnection::SetBtCallScoConnected(bool isBtCallScoConnected)
144 {
145     isBtCallScoConnected_ = isBtCallScoConnected;
146     TELEPHONY_LOGI("Set BtCallScoConnected=%{public}d", isBtCallScoConnected_);
147 }
148 
GetBtCallScoConnected()149 bool BluetoothCallConnection::GetBtCallScoConnected()
150 {
151     TELEPHONY_LOGI("Get BtCallScoConnected=%{public}d", isBtCallScoConnected_);
152     return isBtCallScoConnected_;
153 }
154 
HfpDisConnectedEndBtCall()155 void BluetoothCallConnection::HfpDisConnectedEndBtCall()
156 {
157     TELEPHONY_LOGI("hfp disconnected, hangup all bt call.");
158     std::list<sptr<CallBase>> allCallList = CallObjectManager::GetAllCallList();
159     for (auto call : allCallList) {
160         if (call == nullptr || call->GetCallType() != CallType::TYPE_BLUETOOTH) {
161             continue;
162         }
163         CallAttributeInfo info;
164         (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
165         call->GetCallAttributeBaseInfo(info);
166         CallDetailInfo detailInfo;
167         detailInfo.callType = info.callType;
168         detailInfo.accountId = info.accountId;
169         detailInfo.index = call->GetCallIndex();
170         detailInfo.state = TelCallState::CALL_STATUS_DISCONNECTED;
171         (void)memcpy_s(detailInfo.phoneNum, kMaxNumberLen, info.accountNumber, kMaxNumberLen);
172         (void)memset_s(detailInfo.bundleName, kMaxBundleNameLen  + 1, 0, kMaxBundleNameLen + 1);
173         int32_t ret = DelayedSingleton<ReportCallInfoHandler>::GetInstance()->UpdateCallReportInfo(detailInfo);
174         if (ret != TELEPHONY_SUCCESS) {
175             TELEPHONY_LOGE("UpdateCallReportInfo failed! errCode:%{public}d", ret);
176         } else {
177             TELEPHONY_LOGI("UpdateCallReportInfo success! state:%{public}d, index:%{public}d",
178                 detailInfo.state, detailInfo.index);
179         }
180     }
181 }
182 
SetHfpContactName(const std::string & hfpPhoneNumber,const std::string & hfpContactName)183 void BluetoothCallConnection::SetHfpContactName(const std::string &hfpPhoneNumber, const std::string &hfpContactName)
184 {
185     TELEPHONY_LOGI("hfpPhoneNumber length = %{public}zu, hfpContactName length = %{public}zu",
186         hfpPhoneNumber.length(), hfpContactName.length());
187     hfpPhoneNumber_ = hfpPhoneNumber;
188     hfpContactName_ = hfpContactName;
189 }
190 
GetHfpContactName(const std::string & hfpPhoneNumber)191 std::string BluetoothCallConnection::GetHfpContactName(const std::string &hfpPhoneNumber)
192 {
193     if (!hfpPhoneNumber_.empty() && hfpPhoneNumber == hfpPhoneNumber_) {
194         TELEPHONY_LOGI("got name.");
195         return hfpContactName_;
196     }
197     return "";
198 }
199 } // namespace Telephony
200 } // namespace OHOS
201