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 "base_connection.h"
17
18#include "securec.h"
19#include "standardize_utils.h"
20
21namespace OHOS {
22namespace Telephony {
23BaseConnection::BaseConnection()
24{
25    if (memset_s(&callReportInfo_, sizeof(CallReportInfo), 0, sizeof(callReportInfo_)) != EOK) {
26        TELEPHONY_LOGE("return, memset_s error.");
27    }
28}
29
30void BaseConnection::SetOrUpdateCallReportInfo(CallReportInfo &callReportInfo)
31{
32    callReportInfo_ = callReportInfo;
33}
34
35CallReportInfo BaseConnection::GetCallReportInfo()
36{
37    return callReportInfo_;
38}
39
40void BaseConnection::SetStatus(TelCallState state)
41{
42    callReportInfo_.state = state;
43}
44
45TelCallState BaseConnection::GetStatus() const
46{
47    return callReportInfo_.state;
48}
49
50bool BaseConnection::IsRingingState() const
51{
52    return this->GetStatus() == TelCallState::CALL_STATUS_INCOMING ||
53        this->GetStatus() == TelCallState::CALL_STATUS_WAITING ||
54        this->GetStatus() == TelCallState::CALL_STATUS_ALERTING;
55}
56
57void BaseConnection::SetFlag(bool flag)
58{
59    flag_ = flag;
60}
61
62bool BaseConnection::GetFlag() const
63{
64    return flag_;
65}
66
67void BaseConnection::SetIndex(int32_t index)
68{
69    index_ = index;
70}
71
72int32_t BaseConnection::GetIndex() const
73{
74    return index_;
75}
76
77void BaseConnection::SetNumber(const std::string &number)
78{
79    number_ = number;
80}
81
82std::string BaseConnection::GetNumber() const
83{
84    return number_;
85}
86
87PostDialCallState BaseConnection::ProcessNextChar(int32_t slotId, char &c)
88{
89    if (postDialCalltate_ == PostDialCallState::POST_DIAL_CALL_CANCELED) {
90        return PostDialCallState::POST_DIAL_CALL_CANCELED;
91    }
92    if (postDialCallString_.empty() || postDialCallString_.length() <= static_cast<size_t>(nextPostDialCallCode_)) {
93        SetPostDialCallState(PostDialCallState::POST_DIAL_CALL_COMPLETE);
94        return PostDialCallState::POST_DIAL_CALL_COMPLETE;
95    } else {
96        SetPostDialCallState(PostDialCallState::POST_DIAL_CALL_STARTED);
97        c = postDialCallString_.at(nextPostDialCallCode_++);
98        ProcessPostDialCallChar(slotId, c);
99    }
100    return postDialCalltate_;
101}
102
103void BaseConnection::SetPostDialCallState(PostDialCallState s)
104{
105    postDialCalltate_ = s;
106}
107
108void BaseConnection::UpdateCallNumber(std::string phoneNum)
109{
110    StandardizeUtils standardizeUtils;
111    standardizeUtils.ExtractAddressAndPostDial(phoneNum, phoneNumber_, postDialCallString_);
112}
113
114std::string BaseConnection::GetLeftPostDialCallString()
115{
116    if (postDialCallString_.empty() || postDialCallString_.length() <= static_cast<size_t>(nextPostDialCallCode_)) {
117        return "";
118    }
119    return postDialCallString_.substr(nextPostDialCallCode_);
120}
121
122int32_t BaseConnection::ProcessPostDialCallChar(int32_t slotId, char c)
123{
124    return TELEPHONY_SUCCESS;
125}
126
127void BaseConnection::UpdatePendingHoldFlag(bool isPendingHold)
128{
129    callReportInfo_.isPendingHold = isPendingHold;
130}
131} // namespace Telephony
132} // namespace OHOS