• 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 "apn_holder.h"
17 
18 #include "cellular_data_event_code.h"
19 #include "cellular_data_state_machine.h"
20 #include "data_disconnect_params.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 const std::map<std::string, int32_t> ApnHolder::apnTypeDataProfileMap_ {
25     {DATA_CONTEXT_ROLE_DEFAULT, DATA_PROFILE_DEFAULT},
26     {DATA_CONTEXT_ROLE_MMS,     DATA_PROFILE_MMS}
27 };
28 
ApnHolder(const std::string & apnType,const int32_t priority)29 ApnHolder::ApnHolder(const std::string &apnType, const int32_t priority) : apnType_(apnType), priority_(priority) {}
30 
31 ApnHolder::~ApnHolder() = default;
32 
GetNextRetryApn() const33 sptr<ApnItem> ApnHolder::GetNextRetryApn() const
34 {
35     return retryPolicy_.GetNextRetryApnItem();
36 }
37 
SetAllMatchedApns(std::vector<sptr<ApnItem>> & matchedApns)38 void ApnHolder::SetAllMatchedApns(std::vector<sptr<ApnItem>> &matchedApns)
39 {
40     retryPolicy_.SetMatchedApns(matchedApns);
41 }
42 
GetRetryDelay() const43 int64_t ApnHolder::GetRetryDelay() const
44 {
45     return retryPolicy_.GetNextRetryDelay();
46 }
47 
SetCurrentApn(sptr<ApnItem> & apnItem)48 void ApnHolder::SetCurrentApn(sptr<ApnItem> &apnItem)
49 {
50     apnItem_ = apnItem;
51 }
52 
GetCurrentApn() const53 sptr<ApnItem> ApnHolder::GetCurrentApn() const
54 {
55     return apnItem_;
56 }
57 
SetApnState(ApnProfileState state)58 void ApnHolder::SetApnState(ApnProfileState state)
59 {
60     if (apnState_ != state) {
61         apnState_ = state;
62     }
63     if (apnState_ == PROFILE_STATE_FAILED) {
64         retryPolicy_.ClearRetryApns();
65     }
66 }
67 
GetApnState() const68 ApnProfileState ApnHolder::GetApnState() const
69 {
70     return apnState_;
71 }
72 
IsDataCallEnabled() const73 bool ApnHolder::IsDataCallEnabled() const
74 {
75     return dataCallEnabled_;
76 }
77 
IsDataCallConnectable() const78 bool ApnHolder::IsDataCallConnectable() const
79 {
80     return dataCallEnabled_ && ((apnState_ == PROFILE_STATE_IDLE)
81         || (apnState_ == PROFILE_STATE_RETRYING) || (apnState_ == PROFILE_STATE_FAILED));
82 }
83 
GetApnType() const84 std::string ApnHolder::GetApnType() const
85 {
86     return apnType_;
87 }
88 
ReleaseDataConnection()89 void ApnHolder::ReleaseDataConnection()
90 {
91     if (cellularDataStateMachine_ == nullptr) {
92         TELEPHONY_LOGE("cellularDataStateMachine_ is null");
93         return;
94     }
95     std::unique_ptr<DataDisconnectParams> object =
96         std::make_unique<DataDisconnectParams>(apnType_, DisConnectionReason::REASON_CLEAR_CONNECTION);
97     if (object == nullptr) {
98         TELEPHONY_LOGE("ClearConnection fail, object is null");
99         return;
100     }
101     AppExecFwk::InnerEvent::Pointer event =
102         AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_SM_DISCONNECT, object);
103     cellularDataStateMachine_->SendEvent(event);
104     apnState_ = PROFILE_STATE_IDLE;
105     cellularDataStateMachine_ = nullptr;
106 }
107 
GetProfileId(const std::string & apnType) const108 int32_t ApnHolder::GetProfileId(const std::string &apnType) const
109 {
110     std::map<std::string, int32_t>::const_iterator it = apnTypeDataProfileMap_.find(apnType);
111     if (it != apnTypeDataProfileMap_.end()) {
112         return it->second;
113     }
114     TELEPHONY_LOGI("this apnType is not in apnTypeDataProfileMap.");
115     return DATA_PROFILE_DEFAULT;
116 }
117 
SetCellularDataStateMachine(const std::shared_ptr<CellularDataStateMachine> & stateMachine)118 void ApnHolder::SetCellularDataStateMachine(const std::shared_ptr<CellularDataStateMachine> &stateMachine)
119 {
120     cellularDataStateMachine_ = stateMachine;
121 }
122 
GetCellularDataStateMachine() const123 std::shared_ptr<CellularDataStateMachine> ApnHolder::GetCellularDataStateMachine() const
124 {
125     return cellularDataStateMachine_;
126 }
127 
GetCapability() const128 uint64_t ApnHolder::GetCapability() const
129 {
130     return capability_;
131 }
132 
GetPriority() const133 int32_t ApnHolder::GetPriority() const
134 {
135     return priority_;
136 }
137 
RequestCellularData(const NetRequest & netRequest)138 void ApnHolder::RequestCellularData(const NetRequest &netRequest)
139 {
140     for (const NetRequest &request : netRequests_) {
141         if ((netRequest.capability == request.capability) && (netRequest.ident == request.ident)) {
142             return;
143         }
144     }
145     netRequests_.push_back(netRequest);
146     capability_ = netRequest.capability;
147     dataCallEnabled_ = true;
148 }
149 
ReleaseCellularData(const NetRequest & netRequest)150 void ApnHolder::ReleaseCellularData(const NetRequest &netRequest)
151 {
152     for (std::vector<NetRequest>::const_iterator it = netRequests_.begin(); it != netRequests_.end(); it++) {
153         if ((netRequest.capability == it->capability) && (netRequest.ident == it->ident)) {
154             netRequests_.erase(it);
155             if (netRequests_.empty()) {
156                 dataCallEnabled_ = false;
157             }
158             return;
159         }
160     }
161 }
162 
IsEmergencyType() const163 bool ApnHolder::IsEmergencyType() const
164 {
165     return apnType_ == DATA_CONTEXT_ROLE_EMERGENCY;
166 }
167 
IsMmsType() const168 bool ApnHolder::IsMmsType() const
169 {
170     return apnType_ == DATA_CONTEXT_ROLE_MMS;
171 }
172 
InitialApnRetryCount()173 void ApnHolder::InitialApnRetryCount()
174 {
175     retryPolicy_.InitialRetryCountValue();
176 }
177 
IsSameMatchedApns(std::vector<sptr<ApnItem>> newMatchedApns,bool roamingState)178 bool ApnHolder::IsSameMatchedApns(std::vector<sptr<ApnItem>> newMatchedApns, bool roamingState)
179 {
180     std::vector<sptr<ApnItem>> currentMatchedApns = retryPolicy_.GetMatchedApns();
181     if (currentMatchedApns.empty() || newMatchedApns.empty()) {
182         TELEPHONY_LOGE("newMatchedApns or oldMatchedApns is empty");
183         return false;
184     }
185     if (currentMatchedApns.size() != newMatchedApns.size()) {
186         TELEPHONY_LOGI("newMatchedApns and oldMatchedApns are not equal in size");
187         return false;
188     }
189     for (const sptr<ApnItem> &newApnItem : newMatchedApns) {
190         bool canHandle = false;
191         for (const sptr<ApnItem> &oldApnItem : currentMatchedApns) {
192             if (IsSameApnItem(newApnItem, oldApnItem, roamingState)) {
193                 canHandle = true;
194                 break;
195             }
196         }
197         if (!canHandle) {
198             return false;
199         }
200     }
201     return true;
202 }
203 
IsSameApnItem(const sptr<ApnItem> & newApnItem,const sptr<ApnItem> & oldApnItem,bool roamingState)204 bool ApnHolder::IsSameApnItem(const sptr<ApnItem> &newApnItem,
205                               const sptr<ApnItem> &oldApnItem,
206                               bool roamingState)
207 {
208     if (newApnItem == nullptr || oldApnItem == nullptr) {
209         TELEPHONY_LOGE("newApnItem or oldApnItem is null");
210         return false;
211     }
212     bool isSameProtocol = false;
213     if (roamingState) {
214         isSameProtocol = std::strcmp(newApnItem->attr_.roamingProtocol_, oldApnItem->attr_.roamingProtocol_) == 0;
215     } else {
216         isSameProtocol = std::strcmp(newApnItem->attr_.protocol_, oldApnItem->attr_.protocol_) == 0;
217     }
218     return isSameProtocol && newApnItem->attr_.profileId_ == oldApnItem->attr_.profileId_ &&
219         newApnItem->attr_.authType_ == oldApnItem->attr_.authType_ &&
220         newApnItem->attr_.isRoamingApn_ == oldApnItem->attr_.isRoamingApn_ &&
221         newApnItem->attr_.isEdited_ == oldApnItem->attr_.isEdited_ &&
222         std::strcmp(newApnItem->attr_.types_, oldApnItem->attr_.types_) == 0 &&
223         std::strcmp(newApnItem->attr_.numeric_, oldApnItem->attr_.numeric_) == 0 &&
224         std::strcmp(newApnItem->attr_.apn_, oldApnItem->attr_.apn_) == 0 &&
225         std::strcmp(newApnItem->attr_.apnName_, oldApnItem->attr_.apnName_) == 0 &&
226         std::strcmp(newApnItem->attr_.user_, oldApnItem->attr_.user_) == 0 &&
227         std::strcmp(newApnItem->attr_.password_, oldApnItem->attr_.password_) == 0 &&
228         std::strcmp(newApnItem->attr_.homeUrl_, oldApnItem->attr_.homeUrl_) == 0 &&
229         std::strcmp(newApnItem->attr_.proxyIpAddress_, oldApnItem->attr_.proxyIpAddress_) == 0 &&
230         std::strcmp(newApnItem->attr_.mmsIpAddress_, oldApnItem->attr_.mmsIpAddress_) == 0;
231 }
232 } // namespace Telephony
233 } // namespace OHOS