• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
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)
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     SetAudio();
59     return TELEPHONY_SUCCESS;
60 }
61 
IncomingCallBase()62 int32_t CallBase::IncomingCallBase()
63 {
64     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
65     return TELEPHONY_SUCCESS;
66 }
67 
AnswerCallBase()68 int32_t CallBase::AnswerCallBase()
69 {
70     if (!IsCurrentRinging()) {
71         TELEPHONY_LOGW("the device is currently not ringing");
72         return CALL_ERR_PHONE_ANSWER_IS_BUSY;
73     }
74     DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
75     return TELEPHONY_SUCCESS;
76 }
77 
RejectCallBase()78 int32_t CallBase::RejectCallBase()
79 {
80     answerType_ = CallAnswerType::CALL_ANSWER_REJECT;
81     return TELEPHONY_SUCCESS;
82 }
83 
GetCallAttributeBaseInfo(CallAttributeInfo & info)84 void CallBase::GetCallAttributeBaseInfo(CallAttributeInfo &info)
85 {
86     std::lock_guard<std::mutex> lock(mutex_);
87     (void)memset_s(info.accountNumber, kMaxNumberLen, 0, kMaxNumberLen);
88     if (accountNumber_.length() > static_cast<size_t>(kMaxNumberLen)) {
89         TELEPHONY_LOGE("Number out of limit!");
90         return;
91     }
92     if (memcpy_s(info.accountNumber, kMaxNumberLen, accountNumber_.c_str(), accountNumber_.length()) == 0) {
93         info.speakerphoneOn = isSpeakerphoneOn_;
94         info.videoState = videoState_;
95         info.startTime = startTime_;
96         info.callType = callType_;
97         info.callId = callId_;
98         info.callState = callState_;
99         info.conferenceState = conferenceState_;
100         info.callBeginTime = callBeginTime_;
101         info.callEndTime = callEndTime_;
102         info.ringBeginTime = ringBeginTime_;
103         info.ringEndTime = ringEndTime_;
104         info.callDirection = direction_;
105         info.answerType = answerType_;
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         TELEPHONY_LOGI("Call state duplication %{public}d", nextState);
140         return CALL_ERR_NOT_NEW_STATE;
141     }
142     callState_ = nextState;
143     switch (nextState) {
144         case TelCallState::CALL_STATUS_DIALING:
145             StateChangesToDialing();
146             break;
147         case TelCallState::CALL_STATUS_INCOMING:
148             StateChangesToIncoming();
149             break;
150         case TelCallState::CALL_STATUS_WAITING:
151             StateChangesToWaiting();
152             break;
153         case TelCallState::CALL_STATUS_ACTIVE:
154             StateChangesToActive();
155             break;
156         case TelCallState::CALL_STATUS_HOLDING:
157             StateChangesToHolding();
158             break;
159         case TelCallState::CALL_STATUS_DISCONNECTED:
160             StateChangesToDisconnected();
161             break;
162         case TelCallState::CALL_STATUS_DISCONNECTING:
163             StateChangesToDisconnecting();
164             break;
165         case TelCallState::CALL_STATUS_ALERTING:
166             StateChangesToAlerting();
167             break;
168         default:
169             break;
170     }
171     return TELEPHONY_SUCCESS;
172 }
173 
StateChangesToDialing()174 void CallBase::StateChangesToDialing()
175 {
176     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING;
177 }
178 
StateChangesToIncoming()179 void CallBase::StateChangesToIncoming()
180 {
181     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
182     ringBeginTime_ = time(nullptr);
183 }
184 
StateChangesToWaiting()185 void CallBase::StateChangesToWaiting()
186 {
187     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING;
188     ringBeginTime_ = time(nullptr);
189 }
190 
StateChangesToActive()191 void CallBase::StateChangesToActive()
192 {
193     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ACTIVE;
194     if (callBeginTime_ == 0) {
195         callBeginTime_ = ringEndTime_ = time(nullptr);
196         startTime_ = callBeginTime_;
197         answerType_ = CallAnswerType::CALL_ANSWER_ACTIVED;
198     }
199 }
200 
StateChangesToHolding()201 void CallBase::StateChangesToHolding()
202 {
203     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_HOLD;
204     if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
205         conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED;
206     }
207 }
208 
StateChangesToDisconnected()209 void CallBase::StateChangesToDisconnected()
210 {
211     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDED;
212     if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_DISCONNECTING ||
213         conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
214         conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED;
215     }
216     callEndTime_ = time(nullptr);
217     if (ringEndTime_ == 0) {
218         ringEndTime_ = time(nullptr);
219     }
220 }
221 
StateChangesToDisconnecting()222 void CallBase::StateChangesToDisconnecting()
223 {
224     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDING;
225     if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) {
226         conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTING;
227     }
228     if (ringEndTime_ == 0) {
229         ringEndTime_ = time(nullptr);
230     }
231 }
232 
StateChangesToAlerting()233 void CallBase::StateChangesToAlerting()
234 {
235     callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING;
236     ringBeginTime_ = time(nullptr);
237 }
238 
GetTelCallState()239 TelCallState CallBase::GetTelCallState()
240 {
241     std::lock_guard<std::mutex> lock(mutex_);
242     return callState_;
243 }
244 
SetTelConferenceState(TelConferenceState state)245 void CallBase::SetTelConferenceState(TelConferenceState state)
246 {
247     std::lock_guard<std::mutex> lock(mutex_);
248     conferenceState_ = state;
249     TELEPHONY_LOGI("SetTelConferenceState, state:%{public}d", state);
250 }
251 
GetTelConferenceState()252 TelConferenceState CallBase::GetTelConferenceState()
253 {
254     std::lock_guard<std::mutex> lock(mutex_);
255     return conferenceState_;
256 }
257 
GetVideoStateType()258 VideoStateType CallBase::GetVideoStateType()
259 {
260     std::lock_guard<std::mutex> lock(mutex_);
261     return videoState_;
262 }
263 
SetVideoStateType(VideoStateType mediaType)264 void CallBase::SetVideoStateType(VideoStateType mediaType)
265 {
266     std::lock_guard<std::mutex> lock(mutex_);
267     videoState_ = mediaType;
268 }
269 
SetPolicyFlag(PolicyFlag flag)270 void CallBase::SetPolicyFlag(PolicyFlag flag)
271 {
272     std::lock_guard<std::mutex> lock(mutex_);
273     policyFlag_ |= flag;
274 }
275 
GetPolicyFlag()276 uint64_t CallBase::GetPolicyFlag()
277 {
278     std::lock_guard<std::mutex> lock(mutex_);
279     return policyFlag_;
280 }
281 
GetCallerInfo()282 ContactInfo CallBase::GetCallerInfo()
283 {
284     std::lock_guard<std::mutex> lock(mutex_);
285     return contactInfo_;
286 }
287 
SetCallerInfo(const ContactInfo & info)288 void CallBase::SetCallerInfo(const ContactInfo &info)
289 {
290     std::lock_guard<std::mutex> lock(mutex_);
291     contactInfo_ = info;
292 }
293 
SetCallRunningState(CallRunningState callRunningState)294 void CallBase::SetCallRunningState(CallRunningState callRunningState)
295 {
296     std::lock_guard<std::mutex> lock(mutex_);
297     callRunningState_ = callRunningState;
298 }
299 
SetStartTime(int64_t startTime)300 void CallBase::SetStartTime(int64_t startTime)
301 {
302     std::lock_guard<std::mutex> lock(mutex_);
303     startTime_ = startTime;
304 }
305 
SetCallBeginTime(time_t callBeginTime)306 void CallBase::SetCallBeginTime(time_t callBeginTime)
307 {
308     std::lock_guard<std::mutex> lock(mutex_);
309     callBeginTime_ = callBeginTime;
310 }
311 
SetCallEndTime(time_t callEndTime)312 void CallBase::SetCallEndTime(time_t callEndTime)
313 {
314     std::lock_guard<std::mutex> lock(mutex_);
315     callEndTime_ = callEndTime;
316 }
317 
SetRingBeginTime(time_t ringBeginTime)318 void CallBase::SetRingBeginTime(time_t ringBeginTime)
319 {
320     std::lock_guard<std::mutex> lock(mutex_);
321     ringBeginTime_ = ringBeginTime;
322 }
323 
SetRingEndTime(time_t ringEndTime)324 void CallBase::SetRingEndTime(time_t ringEndTime)
325 {
326     std::lock_guard<std::mutex> lock(mutex_);
327     ringEndTime_ = ringEndTime;
328 }
329 
SetAnswerType(CallAnswerType answerType)330 void CallBase::SetAnswerType(CallAnswerType answerType)
331 {
332     std::lock_guard<std::mutex> lock(mutex_);
333     answerType_ = answerType;
334 }
335 
GetCallEndedType()336 CallEndedType CallBase::GetCallEndedType()
337 {
338     std::lock_guard<std::mutex> lock(mutex_);
339     return callEndedType_;
340 }
341 
SetCallEndedType(CallEndedType callEndedType)342 int32_t CallBase::SetCallEndedType(CallEndedType callEndedType)
343 {
344     std::lock_guard<std::mutex> lock(mutex_);
345     callEndedType_ = callEndedType;
346     return TELEPHONY_SUCCESS;
347 }
348 
SetCallId(int32_t callId)349 void CallBase::SetCallId(int32_t callId)
350 {
351     std::lock_guard<std::mutex> lock(mutex_);
352     callId_ = callId;
353 }
354 
CheckVoicemailNumber(std::string phoneNumber)355 bool CallBase::CheckVoicemailNumber(std::string phoneNumber)
356 {
357     return false;
358 }
359 
SetAudio()360 void CallBase::SetAudio()
361 {
362     // Get from the configuration file
363     bool useSpeakerWhenDocked = true;
364     /**
365      * Determine if hands-free should be enabled during docking.
366      * When the device is docked and the Bluetooth or wired headset is not in use,
367      * please turn on the hands-free
368      */
369     bool useSpeakerForDock = IsSpeakerphoneEnabled();
370     SetSpeakerphoneOn(useSpeakerWhenDocked && useSpeakerForDock);
371     // Confirm whether the speaker is turned on
372     if (isSpeakerphoneOn_) {
373         TELEPHONY_LOGI("set audio speaker");
374         DelayedSingleton<AudioControlManager>::GetInstance()->SetAudioDevice(AudioDevice::DEVICE_SPEAKER);
375     } else {
376         TELEPHONY_LOGI("set audio bluetooth");
377         std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
378         std::string address;
379         bluetoothCallManager->ConnectBtSco(address);
380     }
381 }
382 
IsSpeakerphoneEnabled()383 bool CallBase::IsSpeakerphoneEnabled()
384 {
385     std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
386     // Gets whether the device can be started from the configuration
387     if (bluetoothCallManager->IsBtAvailble()) {
388         return false;
389     }
390     return true;
391 }
392 
IsCurrentRinging()393 bool CallBase::IsCurrentRinging()
394 {
395     return (callRunningState_ == CallRunningState::CALL_RUNNING_STATE_RINGING) ? true : false;
396 }
397 
GetAccountNumber()398 std::string CallBase::GetAccountNumber()
399 {
400     return accountNumber_;
401 }
402 
SetSpeakerphoneOn(bool speakerphoneOn)403 int32_t CallBase::SetSpeakerphoneOn(bool speakerphoneOn)
404 {
405     isSpeakerphoneOn_ = speakerphoneOn;
406     return TELEPHONY_SUCCESS;
407 }
408 
IsSpeakerphoneOn()409 bool CallBase::IsSpeakerphoneOn()
410 {
411     return isSpeakerphoneOn_;
412 }
413 
IsAliveState()414 bool CallBase::IsAliveState()
415 {
416     return !(callState_ == TelCallState::CALL_STATUS_IDLE || callState_ == TelCallState::CALL_STATUS_DISCONNECTED ||
417         callState_ == TelCallState::CALL_STATUS_DISCONNECTING);
418 }
419 } // namespace Telephony
420 } // namespace OHOS
421