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 "default.h"
17
18#include "cellular_data_utils.h"
19#include "core_manager_inner.h"
20#include "telephony_log_wrapper.h"
21
22namespace OHOS {
23namespace Telephony {
24void Default::StateBegin()
25{
26    TELEPHONY_LOGD("Enter default state");
27    isActive_ = true;
28}
29
30void Default::StateEnd()
31{
32    TELEPHONY_LOGD("Exit default state");
33    isActive_ = false;
34}
35
36bool Default::StateProcess(const AppExecFwk::InnerEvent::Pointer &event)
37{
38    if (event == nullptr) {
39        TELEPHONY_LOGE("event is null");
40        return false;
41    }
42    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
43    if (stateMachine == nullptr) {
44        TELEPHONY_LOGE("stateMachine is null");
45        return false;
46    }
47    uint32_t eventCode = event->GetInnerEventId();
48    std::map<uint32_t, Fun>::iterator it = eventIdFunMap_.find(eventCode);
49    if (it != eventIdFunMap_.end()) {
50        return it->second(event);
51    }
52    return false;
53}
54
55bool Default::ProcessConnectDone(const AppExecFwk::InnerEvent::Pointer &event)
56{
57    TELEPHONY_LOGI("Default::MSG_SM_CONNECT");
58    return false;
59}
60
61bool Default::ProcessDisconnectDone(const AppExecFwk::InnerEvent::Pointer &event)
62{
63    if (event == nullptr) {
64        TELEPHONY_LOGE("event is null");
65        return false;
66    }
67    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
68    if (stateMachine == nullptr) {
69        TELEPHONY_LOGE("The state machine pointer is null");
70        return false;
71    }
72    TELEPHONY_LOGI("The data connection is disconnected by default");
73    stateMachine->DeferEvent(std::move(event));
74    return true;
75}
76
77bool Default::ProcessDisconnectAllDone(const AppExecFwk::InnerEvent::Pointer &event)
78{
79    if (event == nullptr) {
80        TELEPHONY_LOGE("event is null");
81        return false;
82    }
83    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
84    if (stateMachine == nullptr) {
85        TELEPHONY_LOGE("The state machine pointer is null");
86        return false;
87    }
88    TELEPHONY_LOGI("All data connections are disconnected by default");
89    stateMachine->DeferEvent(std::move(event));
90    return true;
91}
92
93bool Default::ProcessDataConnectionDrsOrRatChanged(const AppExecFwk::InnerEvent::Pointer &event)
94{
95    if (event == nullptr) {
96        TELEPHONY_LOGE("event is null");
97        return false;
98    }
99    std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
100    if (stateMachine == nullptr) {
101        TELEPHONY_LOGE("stateMachine is null");
102        return false;
103    }
104    TELEPHONY_LOGI("The RAT changes by default");
105    CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
106    int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
107    netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
108    if (stateMachine->IsActiveState() || stateMachine->IsActivatingState()) {
109        netAgent.UpdateNetLinkInfo(supplierId, stateMachine->netLinkInfo_);
110    }
111    int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
112    CoreManagerInner::GetInstance().GetPsRadioTech(stateMachine->GetSlotId(), radioTech);
113    netAgent.RegisterSlotType(supplierId, radioTech);
114    TELEPHONY_LOGI("RegisterSlotType: supplierId[%{public}d] slotId[%{public}d] radioTech[%{public}d]",
115        supplierId, stateMachine->GetSlotId(), radioTech);
116    return false;
117}
118
119bool Default::ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer &event)
120{
121    TELEPHONY_LOGI("Default::EVENT_DATA_CONNECTION_ROAM_ON");
122    return false;
123}
124
125bool Default::ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer &event)
126{
127    TELEPHONY_LOGI("Default::EVENT_DATA_CONNECTION_ROAM_OFF");
128    return false;
129}
130
131bool Default::ProcessUpdateNetworkInfo(const AppExecFwk::InnerEvent::Pointer &event)
132{
133    std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
134    if (shareStateMachine == nullptr) {
135        TELEPHONY_LOGE("shareStateMachine is null");
136        return false;
137    }
138    shareStateMachine->UpdateNetworkInfo();
139    return true;
140}
141} // namespace Telephony
142} // namespace OHOS
143