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 "disconnecting.h"
17
18#include "tel_ril_data_parcel.h"
19#include "telephony_log_wrapper.h"
20#include "radio_event.h"
21
22#include "cellular_data_event_code.h"
23#include "inactive.h"
24
25namespace OHOS {
26namespace Telephony {
27void Disconnecting::StateBegin()
28{
29    TELEPHONY_LOGI("Enter disconnecting state");
30    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
31    if (stateMachine == nullptr) {
32        TELEPHONY_LOGE("stateMachine is null");
33        return;
34    }
35    isActive_ = true;
36    stateMachine->SetCurrentState(sptr<State>(this));
37}
38
39void Disconnecting::StateEnd()
40{
41    TELEPHONY_LOGI("Disconnecting::exit");
42    isActive_ = false;
43}
44
45void Disconnecting::ProcessDisconnectTimeout(const AppExecFwk::InnerEvent::Pointer &event)
46{
47    if (event == nullptr) {
48        TELEPHONY_LOGE("event is null");
49        return;
50    }
51    int32_t connectId = event->GetParam();
52    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
53    if (stateMachine == nullptr) {
54        TELEPHONY_LOGE("stateMachine is null");
55        return;
56    }
57    if (connectId != stateMachine->connectId_) {
58        return;
59    }
60    Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
61    if (inActive == nullptr) {
62        TELEPHONY_LOGE("inActive is null");
63        return;
64    }
65    inActive->SetDataCallResultInfoToRetry();
66    inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
67    stateMachine->TransitionTo(stateMachine->inActiveState_);
68    TELEPHONY_LOGI("ProcessDisconnectTimeout");
69}
70
71void Disconnecting::ProcessRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer &event)
72{
73    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
74    if (stateMachine == nullptr) {
75        TELEPHONY_LOGE("stateMachine is null");
76        return;
77    }
78    Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
79    if (inActive == nullptr) {
80        TELEPHONY_LOGE("inActive is null");
81        return;
82    }
83    inActive->SetDataCallResultInfoToRetry();
84    inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
85    stateMachine->TransitionTo(stateMachine->inActiveState_);
86    TELEPHONY_LOGI("ProcessRilAdapterHostDied");
87}
88
89void Disconnecting::ProcessRilDeactivateDataCall(const AppExecFwk::InnerEvent::Pointer &event)
90{
91    if (event == nullptr) {
92        TELEPHONY_LOGE("event is null");
93        return;
94    }
95    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
96    if (stateMachine == nullptr || stateMachine->stateMachineEventHandler_ == nullptr) {
97        TELEPHONY_LOGE("stateMachine is null");
98        return;
99    }
100    Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
101    if (inActive == nullptr) {
102        TELEPHONY_LOGE("inActive is null");
103        return;
104    }
105    std::shared_ptr<RadioResponseInfo> rilInfo = event->GetSharedObject<RadioResponseInfo>();
106    if (rilInfo == nullptr) {
107        TELEPHONY_LOGE("SetupDataCallResultInfo and RadioResponseInfo is null");
108        stateMachine->stateMachineEventHandler_->RemoveEvent(CellularDataEventCode::MSG_CONNECT_TIMEOUT_CHECK);
109        inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
110        stateMachine->TransitionTo(stateMachine->inActiveState_);
111        return;
112    }
113    if (stateMachine->connectId_ != rilInfo->flag) {
114        TELEPHONY_LOGE("connectId is %{public}d, flag is %{public}d", stateMachine->connectId_, rilInfo->flag);
115        return;
116    }
117    TELEPHONY_LOGI("RadioResponseInfo error is %{public}d", static_cast<int32_t>(rilInfo->error));
118    stateMachine->stateMachineEventHandler_->RemoveEvent(CellularDataEventCode::MSG_CONNECT_TIMEOUT_CHECK);
119    inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
120    stateMachine->TransitionTo(stateMachine->inActiveState_);
121    TELEPHONY_LOGI("ProcessRilDeactivateDataCall");
122}
123
124bool Disconnecting::StateProcess(const AppExecFwk::InnerEvent::Pointer &event)
125{
126    if (event == nullptr) {
127        TELEPHONY_LOGE("event is null");
128        return false;
129    }
130    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
131    if (stateMachine == nullptr) {
132        TELEPHONY_LOGE("stateMachine is null");
133        return false;
134    }
135    bool retVal = false;
136    switch (event->GetInnerEventId()) {
137        case CellularDataEventCode::MSG_SM_CONNECT:
138            TELEPHONY_LOGI("Disconnecting::MSG_SM_CONNECT");
139            stateMachine->DeferEvent(std::move(event));
140            retVal = PROCESSED;
141            break;
142        case RadioEvent::RADIO_RIL_DEACTIVATE_DATA_CALL: {
143            ProcessRilDeactivateDataCall(event);
144            retVal = PROCESSED;
145            break;
146        }
147        case CellularDataEventCode::MSG_DISCONNECT_TIMEOUT_CHECK:
148            ProcessDisconnectTimeout(event);
149            retVal = PROCESSED;
150            break;
151        case CellularDataEventCode::MSG_SM_RIL_ADAPTER_HOST_DIED:
152            ProcessRilAdapterHostDied(event);
153            retVal = PROCESSED;
154            break;
155        default:
156            TELEPHONY_LOGE("disconnecting StateProcess do nothing!");
157            break;
158    }
159    return retVal;
160}
161} // namespace Telephony
162} // namespace OHOS
163