1 /*
2 * Copyright (C) 2021 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 "call_base.h"
17
18 #include <securec.h>
19
20 #include "audio_control_manager.h"
21 #include "bluetooth_call_manager.h"
22 #include "call_manager_errors.h"
23 #include "cellular_call_connection.h"
24 #include "common_type.h"
25 #include "telephony_log_wrapper.h"
26
27 namespace OHOS {
28 namespace Telephony {
CallBase(DialParaInfo & info)29 CallBase::CallBase(DialParaInfo &info)
30 : callId_(info.callId), callType_(info.callType), videoState_(info.videoState), accountNumber_(info.number),
31 bundleName_(info.bundleName), callRunningState_(CallRunningState::CALL_RUNNING_STATE_CREATE),
32 conferenceState_(TelConferenceState::TEL_CONFERENCE_IDLE), startTime_(0),
33 direction_(CallDirection::CALL_DIRECTION_IN), policyFlag_(0), callState_(info.callState),
34 isSpeakerphoneOn_(false), callEndedType_(CallEndedType::UNKNOWN), callBeginTime_(0), callEndTime_(0),
35 ringBeginTime_(0), ringEndTime_(0), answerType_(CallAnswerType::CALL_ANSWER_MISSED), accountId_(info.accountId)
36 {
37 (void)memset_s(&contactInfo_, sizeof(ContactInfo), 0, sizeof(ContactInfo));
38 }
39
CallBase(DialParaInfo & info,AppExecFwk::PacMap & extras)40 CallBase::CallBase(DialParaInfo &info, AppExecFwk::PacMap &extras)
41 : callId_(info.callId), callType_(info.callType), videoState_(info.videoState), accountNumber_(info.number),
42 bundleName_(info.bundleName), callRunningState_(CallRunningState::CALL_RUNNING_STATE_CREATE),
43 conferenceState_(TelConferenceState::TEL_CONFERENCE_IDLE), startTime_(0),
44 direction_(CallDirection::CALL_DIRECTION_OUT), policyFlag_(0), callState_(info.callState),
45 isSpeakerphoneOn_(false), callEndedType_(CallEndedType::UNKNOWN), callBeginTime_(0), callEndTime_(0),
46 ringBeginTime_(0), ringEndTime_(0), answerType_(CallAnswerType::CALL_ANSWER_MISSED), accountId_(info.accountId)
47 {
48 (void)memset_s(&contactInfo_, sizeof(ContactInfo), 0, sizeof(ContactInfo));
49 }
50
~CallBase()51 CallBase::~CallBase() {}
52
DialCallBase()53 int32_t CallBase::DialCallBase()
54 {
55 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_CONNECTING;
56 TELEPHONY_LOGI("start to set audio");
57 // Set audio, set hands-free
58 return TELEPHONY_SUCCESS;
59 }
60
IncomingCallBase()61 int32_t CallBase::IncomingCallBase()
62 {
63 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
64 return TELEPHONY_SUCCESS;
65 }
66
AnswerCallBase()67 int32_t CallBase::AnswerCallBase()
68 {
69 if (!IsCurrentRinging()) {
70 TELEPHONY_LOGW("the device is currently not ringing");
71 return CALL_ERR_PHONE_ANSWER_IS_BUSY;
72 }
73 DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
74 return TELEPHONY_SUCCESS;
75 }
76
RejectCallBase()77 int32_t CallBase::RejectCallBase()
78 {
79 answerType_ = CallAnswerType::CALL_ANSWER_REJECT;
80 return TELEPHONY_SUCCESS;
81 }
82
GetCallAttributeBaseInfo(CallAttributeInfo & info)83 void CallBase::GetCallAttributeBaseInfo(CallAttributeInfo &info)
84 {
85 std::lock_guard<std::mutex> lock(mutex_);
86 (void)memset_s(info.accountNumber, kMaxNumberLen, 0, kMaxNumberLen);
87 if (accountNumber_.length() > static_cast<size_t>(kMaxNumberLen)) {
88 TELEPHONY_LOGE("Number out of limit!");
89 return;
90 }
91 if (memcpy_s(info.accountNumber, kMaxNumberLen, accountNumber_.c_str(), accountNumber_.length()) == 0) {
92 info.speakerphoneOn = isSpeakerphoneOn_;
93 info.videoState = videoState_;
94 info.startTime = startTime_;
95 info.callType = callType_;
96 info.callId = callId_;
97 info.callState = callState_;
98 info.conferenceState = conferenceState_;
99 info.callBeginTime = callBeginTime_;
100 info.callEndTime = callEndTime_;
101 info.ringBeginTime = ringBeginTime_;
102 info.ringEndTime = ringEndTime_;
103 info.callDirection = direction_;
104 info.answerType = answerType_;
105 info.accountId = accountId_;
106 if (bundleName_.length() > static_cast<size_t>(kMaxBundleNameLen)) {
107 TELEPHONY_LOGE("Number out of limit!");
108 return;
109 }
110 errno_t result = memcpy_s(info.bundleName, kMaxBundleNameLen, bundleName_.c_str(), bundleName_.length());
111 if (result != EOK) {
112 TELEPHONY_LOGE("memcpy_s failed!");
113 }
114 }
115 }
116
GetCallID()117 int32_t CallBase::GetCallID()
118 {
119 std::lock_guard<std::mutex> lock(mutex_);
120 return callId_;
121 }
122
GetCallType()123 CallType CallBase::GetCallType()
124 {
125 return callType_;
126 }
127
GetCallRunningState()128 CallRunningState CallBase::GetCallRunningState()
129 {
130 std::lock_guard<std::mutex> lock(mutex_);
131 return callRunningState_;
132 }
133
134 // transfer from external call state to callmanager local state
SetTelCallState(TelCallState nextState)135 int32_t CallBase::SetTelCallState(TelCallState nextState)
136 {
137 std::lock_guard<std::mutex> lock(mutex_);
138 if (callRunningState_ != CallRunningState::CALL_RUNNING_STATE_CREATE && callState_ == nextState &&
139 nextState != TelCallState::CALL_STATUS_DIALING) {
140 TELEPHONY_LOGI("Call state duplication %{public}d", nextState);
141 return CALL_ERR_NOT_NEW_STATE;
142 }
143 callState_ = nextState;
144 switch (nextState) {
145 case TelCallState::CALL_STATUS_DIALING:
146 StateChangesToDialing();
147 break;
148 case TelCallState::CALL_STATUS_INCOMING:
149 StateChangesToIncoming();
150 break;
151 case TelCallState::CALL_STATUS_WAITING:
152 StateChangesToWaiting();
153 break;
154 case TelCallState::CALL_STATUS_ACTIVE:
155 StateChangesToActive();
156 break;
157 case TelCallState::CALL_STATUS_HOLDING:
158 StateChangesToHolding();
159 break;
160 case TelCallState::CALL_STATUS_DISCONNECTED:
161 StateChangesToDisconnected();
162 break;
163 case TelCallState::CALL_STATUS_DISCONNECTING:
164 StateChangesToDisconnecting();
165 break;
166 case TelCallState::CALL_STATUS_ALERTING:
167 StateChangesToAlerting();
168 break;
169 default:
170 break;
171 }
172 return TELEPHONY_SUCCESS;
173 }
174
StateChangesToDialing()175 void CallBase::StateChangesToDialing()
176 {
177 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING;
178 }
179
StateChangesToIncoming()180 void CallBase::StateChangesToIncoming()
181 {
182 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
183 ringBeginTime_ = time(nullptr);
184 }
185
StateChangesToWaiting()186 void CallBase::StateChangesToWaiting()
187 {
188 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
189 ringBeginTime_ = time(nullptr);
190 }
191
StateChangesToActive()192 void CallBase::StateChangesToActive()
193 {
194 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ACTIVE;
195 if (callBeginTime_ == 0) {
196 callBeginTime_ = ringEndTime_ = time(nullptr);
197 startTime_ = callBeginTime_;
198 answerType_ = CallAnswerType::CALL_ANSWER_ACTIVED;
199 }
200 }
201
StateChangesToHolding()202 void CallBase::StateChangesToHolding()
203 {
204 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_HOLD;
205 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
206 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED;
207 }
208 }
209
StateChangesToDisconnected()210 void CallBase::StateChangesToDisconnected()
211 {
212 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDED;
213 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_DISCONNECTING ||
214 conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
215 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED;
216 }
217 callEndTime_ = time(nullptr);
218 if (ringEndTime_ == 0) {
219 ringEndTime_ = time(nullptr);
220 }
221 }
222
StateChangesToDisconnecting()223 void CallBase::StateChangesToDisconnecting()
224 {
225 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDING;
226 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
227 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTING;
228 }
229 if (ringEndTime_ == 0) {
230 ringEndTime_ = time(nullptr);
231 }
232 }
233
StateChangesToAlerting()234 void CallBase::StateChangesToAlerting()
235 {
236 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING;
237 ringBeginTime_ = time(nullptr);
238 }
239
GetTelCallState()240 TelCallState CallBase::GetTelCallState()
241 {
242 std::lock_guard<std::mutex> lock(mutex_);
243 return callState_;
244 }
245
SetTelConferenceState(TelConferenceState state)246 void CallBase::SetTelConferenceState(TelConferenceState state)
247 {
248 std::lock_guard<std::mutex> lock(mutex_);
249 conferenceState_ = state;
250 TELEPHONY_LOGI("SetTelConferenceState, state:%{public}d", state);
251 }
252
GetTelConferenceState()253 TelConferenceState CallBase::GetTelConferenceState()
254 {
255 std::lock_guard<std::mutex> lock(mutex_);
256 return conferenceState_;
257 }
258
GetVideoStateType()259 VideoStateType CallBase::GetVideoStateType()
260 {
261 std::lock_guard<std::mutex> lock(mutex_);
262 return videoState_;
263 }
264
SetVideoStateType(VideoStateType mediaType)265 void CallBase::SetVideoStateType(VideoStateType mediaType)
266 {
267 std::lock_guard<std::mutex> lock(mutex_);
268 videoState_ = mediaType;
269 }
270
SetPolicyFlag(PolicyFlag flag)271 void CallBase::SetPolicyFlag(PolicyFlag flag)
272 {
273 std::lock_guard<std::mutex> lock(mutex_);
274 policyFlag_ |= flag;
275 }
276
GetPolicyFlag()277 uint64_t CallBase::GetPolicyFlag()
278 {
279 std::lock_guard<std::mutex> lock(mutex_);
280 return policyFlag_;
281 }
282
GetCallerInfo()283 ContactInfo CallBase::GetCallerInfo()
284 {
285 std::lock_guard<std::mutex> lock(mutex_);
286 return contactInfo_;
287 }
288
SetCallerInfo(const ContactInfo & info)289 void CallBase::SetCallerInfo(const ContactInfo &info)
290 {
291 std::lock_guard<std::mutex> lock(mutex_);
292 contactInfo_ = info;
293 }
294
SetCallRunningState(CallRunningState callRunningState)295 void CallBase::SetCallRunningState(CallRunningState callRunningState)
296 {
297 std::lock_guard<std::mutex> lock(mutex_);
298 callRunningState_ = callRunningState;
299 }
300
SetStartTime(int64_t startTime)301 void CallBase::SetStartTime(int64_t startTime)
302 {
303 std::lock_guard<std::mutex> lock(mutex_);
304 startTime_ = startTime;
305 }
306
SetCallBeginTime(time_t callBeginTime)307 void CallBase::SetCallBeginTime(time_t callBeginTime)
308 {
309 std::lock_guard<std::mutex> lock(mutex_);
310 callBeginTime_ = callBeginTime;
311 }
312
SetCallEndTime(time_t callEndTime)313 void CallBase::SetCallEndTime(time_t callEndTime)
314 {
315 std::lock_guard<std::mutex> lock(mutex_);
316 callEndTime_ = callEndTime;
317 }
318
SetRingBeginTime(time_t ringBeginTime)319 void CallBase::SetRingBeginTime(time_t ringBeginTime)
320 {
321 std::lock_guard<std::mutex> lock(mutex_);
322 ringBeginTime_ = ringBeginTime;
323 }
324
SetRingEndTime(time_t ringEndTime)325 void CallBase::SetRingEndTime(time_t ringEndTime)
326 {
327 std::lock_guard<std::mutex> lock(mutex_);
328 ringEndTime_ = ringEndTime;
329 }
330
SetAnswerType(CallAnswerType answerType)331 void CallBase::SetAnswerType(CallAnswerType answerType)
332 {
333 std::lock_guard<std::mutex> lock(mutex_);
334 answerType_ = answerType;
335 }
336
GetCallEndedType()337 CallEndedType CallBase::GetCallEndedType()
338 {
339 std::lock_guard<std::mutex> lock(mutex_);
340 return callEndedType_;
341 }
342
SetCallEndedType(CallEndedType callEndedType)343 int32_t CallBase::SetCallEndedType(CallEndedType callEndedType)
344 {
345 std::lock_guard<std::mutex> lock(mutex_);
346 callEndedType_ = callEndedType;
347 return TELEPHONY_SUCCESS;
348 }
349
SetCallId(int32_t callId)350 void CallBase::SetCallId(int32_t callId)
351 {
352 std::lock_guard<std::mutex> lock(mutex_);
353 callId_ = callId;
354 }
355
CheckVoicemailNumber(std::string phoneNumber)356 bool CallBase::CheckVoicemailNumber(std::string phoneNumber)
357 {
358 return false;
359 }
360
SetAudio()361 void CallBase::SetAudio()
362 {
363 // Get from the configuration file
364 bool useSpeakerWhenDocked = true;
365 /**
366 * Determine if hands-free should be enabled during docking.
367 * When the device is docked and the Bluetooth or wired headset is not in use,
368 * please turn on the hands-free
369 */
370 bool useSpeakerForDock = IsSpeakerphoneEnabled();
371 SetSpeakerphoneOn(useSpeakerWhenDocked && useSpeakerForDock);
372 // Confirm whether the speaker is turned on
373 if (isSpeakerphoneOn_) {
374 TELEPHONY_LOGI("set audio speaker");
375 AudioDevice device = {
376 .deviceType = AudioDeviceType::DEVICE_SPEAKER,
377 .address = { 0 },
378 };
379 DelayedSingleton<AudioControlManager>::GetInstance()->SetAudioDevice(device);
380 } else {
381 TELEPHONY_LOGI("set audio bluetooth");
382 std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
383 std::string address;
384 bluetoothCallManager->ConnectBtSco(address);
385 }
386 }
387
IsSpeakerphoneEnabled()388 bool CallBase::IsSpeakerphoneEnabled()
389 {
390 std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
391 // Gets whether the device can be started from the configuration
392 if (bluetoothCallManager->IsBtAvailble()) {
393 return false;
394 }
395 return true;
396 }
397
IsCurrentRinging()398 bool CallBase::IsCurrentRinging()
399 {
400 return (callRunningState_ == CallRunningState::CALL_RUNNING_STATE_RINGING) ? true : false;
401 }
402
GetAccountNumber()403 std::string CallBase::GetAccountNumber()
404 {
405 return accountNumber_;
406 }
407
SetSpeakerphoneOn(bool speakerphoneOn)408 int32_t CallBase::SetSpeakerphoneOn(bool speakerphoneOn)
409 {
410 isSpeakerphoneOn_ = speakerphoneOn;
411 return TELEPHONY_SUCCESS;
412 }
413
IsSpeakerphoneOn()414 bool CallBase::IsSpeakerphoneOn()
415 {
416 return isSpeakerphoneOn_;
417 }
418
IsAliveState()419 bool CallBase::IsAliveState()
420 {
421 return !(callState_ == TelCallState::CALL_STATUS_IDLE || callState_ == TelCallState::CALL_STATUS_DISCONNECTED ||
422 callState_ == TelCallState::CALL_STATUS_DISCONNECTING);
423 }
424
SetBundleName(const char * bundleName)425 void CallBase::SetBundleName(const char *bundleName)
426 {
427 bundleName_ = bundleName;
428 }
429
SetCallType(CallType callType)430 void CallBase::SetCallType(CallType callType)
431 {
432 callType_ = callType;
433 }
434 } // namespace Telephony
435 } // namespace OHOS
436