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 "net_manager_tactics_call_back.h"
17
18 #include "cellular_data_error.h"
19 #include "cellular_data_service.h"
20 #include "core_manager_inner.h"
21 #include "telephony_common_utils.h"
22 #include "telephony_log_wrapper.h"
23 #include <charconv>
24
25 namespace OHOS {
26 namespace Telephony {
NetStrategySwitch(const std::string & simId,bool enable)27 int32_t NetManagerTacticsCallBack::NetStrategySwitch(const std::string &simId, bool enable)
28 {
29 if (simId.length() == 0) {
30 TELEPHONY_LOGI("StrategySwitch[The simd length is 0]");
31 return CELLULAR_DATA_INVALID_PARAM;
32 }
33 if (!IsValidDecValue(simId)) {
34 return CELLULAR_DATA_INVALID_PARAM;
35 }
36 int32_t tempNetSimId = 0;
37 ConvertStrToInt(simId, tempNetSimId);
38 const int32_t netSimId = tempNetSimId;
39 CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
40 for (int32_t i = 0; i < SIM_SLOT_COUNT; ++i) {
41 IccAccountInfo accountInfo;
42 if (coreInner.GetSimAccountInfo(i, accountInfo) != TELEPHONY_ERR_SUCCESS) {
43 continue;
44 }
45 if (netSimId == accountInfo.simId) {
46 int32_t result = DelayedRefSingleton<CellularDataService>::GetInstance().StrategySwitch(i, enable);
47 TELEPHONY_LOGI("StrategySwitch[%{public}s, %{public}d, %{public}d] result %{public}d", simId.c_str(),
48 i, enable, result);
49 return result;
50 }
51 }
52 TELEPHONY_LOGI("StrategySwitch fail[%{public}s, %{public}d]", simId.c_str(), enable);
53 return CELLULAR_DATA_INVALID_PARAM;
54 }
55
ConvertStrToInt(const std::string & str,int32_t & value)56 bool NetManagerTacticsCallBack::ConvertStrToInt(const std::string& str, int32_t& value)
57 {
58 if (str.empty()) {
59 return false;
60 }
61 auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value, 10); // 10: 十进制
62 bool succ = ec == std::errc{} && ptr == str.data() + str.size();
63 if (!succ) {
64 TELEPHONY_LOGE("ConvertStrToInt failed: str: %{public}s", str.c_str());
65 }
66 return succ;
67 }
68 } // Telephony
69 } // OHOS
70