• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <locale>
17 #include <codecvt>
18 #include "wifi_telephony_utils.h"
19 #include "wifi_logger.h"
20 DEFINE_WIFILOG_LABEL("WifiTelephonyUtils");
21 
22 namespace OHOS {
23 namespace Wifi {
24 namespace WifiTelephonyUtils {
25 
26 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
27     using namespace OHOS::Telephony;
28 #endif
29 
ConvertString(const std::u16string & wideText)30 std::string ConvertString(const std::u16string &wideText)
31 {
32     return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(wideText);
33 }
34 
SimAkaAuth(const std::string & nonce,AuthType authType,int32_t eapSubId)35 std::string SimAkaAuth(const std::string &nonce, AuthType authType, int32_t eapSubId)
36 {
37 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
38     SimAuthenticationResponse response;
39     Telephony::AuthType type;
40     switch (authType) {
41         case AuthType::SIM_TYPE:
42             type = Telephony::AuthType::SIM_AUTH_EAP_SIM_TYPE;
43             break;
44         case AuthType::AKA_TYPE:
45             type = Telephony::AuthType::SIM_AUTH_EAP_AKA_TYPE;
46             break;
47         default:
48             break;
49     }
50     WIFI_LOGD("StaStateMachine::SimAkaAuth in, authType:%{public}d, nonce:%{private}s", type, nonce.c_str());
51     auto slotId = CoreServiceClient::GetInstance().GetSlotId(eapSubId);
52     int32_t result = CoreServiceClient::GetInstance().SimAuthentication(slotId, type, nonce, response);
53     if (result != WIFI_OPT_SUCCESS) {
54         WIFI_LOGE("StaStateMachine::SimAkaAuth: errCode=%{public}d", result);
55         return "";
56     }
57     return response.response;
58 #else
59     WIFI_LOGW("telephony subsystem is disabled, sim auth is not supported");
60     return "";
61 #endif
62 }
63 
64 #ifndef OHOS_ARCH_LITE
GetDataSlotId(int32_t slotId)65 int32_t GetDataSlotId(int32_t slotId)
66 {
67 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
68     int32_t simCount = CoreServiceClient::GetInstance().GetMaxSimCount();
69     if (slotId >= 0 && slotId < simCount) {
70         WIFI_LOGI("slotId: %{public}d, simCount:%{public}d", slotId, simCount);
71         return slotId;
72     }
73     auto slotDefaultID = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
74     if ((slotDefaultID < 0) || (slotDefaultID >= simCount)) {
75         WIFI_LOGE("failed to get default slotId, slotId:%{public}d, simCount:%{public}d", slotDefaultID, simCount);
76         return -1;
77     }
78     WIFI_LOGI("slotDefaultID: %{public}d, simCount:%{public}d", slotDefaultID, simCount);
79     return slotDefaultID;
80 #else
81     WIFI_LOGW("telephony subsystem is disabled, query slotId is not supported");
82     return -1;
83 #endif
84 }
85 
GetImsi(int32_t slotId)86 std::string GetImsi(int32_t slotId)
87 {
88 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
89     std::u16string imsi;
90     int32_t errCode = CoreServiceClient::GetInstance().GetIMSI(slotId, imsi);
91     if (errCode != 0) {
92         WIFI_LOGE("failed to get imsi, errCode: %{public}d", errCode);
93         return "";
94     }
95     return ConvertString(imsi);
96 #else
97     WIFI_LOGW("telephony subsystem is disabled, query imsi is not supported");
98     return "";
99 #endif
100 }
101 
GetPlmn(int32_t slotId)102 std::string GetPlmn(int32_t slotId)
103 {
104 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
105     std::u16string plmn;
106     int32_t errCode = CoreServiceClient::GetInstance().GetSimOperatorNumeric(slotId, plmn);
107     if (errCode != 0) {
108         WIFI_LOGE("failed to get plmn, errCode: %{public}d", errCode);
109         return "";
110     }
111     return ConvertString(plmn);
112 #else
113     WIFI_LOGW("telephony subsystem is disabled, query plmn is not supported");
114     return "";
115 #endif
116 }
117 #endif
118 
GetDefaultId(int32_t slotId)119 int32_t GetDefaultId(int32_t slotId)
120 {
121 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
122     WIFI_LOGI("StaStateMachine::GetDefaultId in, slotId: %{public}d", slotId);
123     constexpr int32_t WIFI_INVALID_SIM_ID = 0;
124     if (slotId == WIFI_INVALID_SIM_ID) {
125         return GetDataSlotId(slotId);
126     }
127     return slotId;
128 #else
129     WIFI_LOGW("telephony subsystem is disabled, query defaultId is not supported");
130     return -1;
131 #endif
132 }
133 
GetSimCardState(int32_t slotId)134 int32_t GetSimCardState(int32_t slotId)
135 {
136 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
137     WIFI_LOGI("StaStateMachine::GetSimCardState in, slotId: %{public}d", slotId);
138     slotId = GetDefaultId(slotId);
139     WIFI_LOGI("slotId: %{public}d", slotId);
140     SimState simState = SimState::SIM_STATE_UNKNOWN;
141     int32_t result = CoreServiceClient::GetInstance().GetSimState(slotId, simState);
142     if (result != Wifi::WIFI_OPT_SUCCESS) {
143         WIFI_LOGE("StaStateMachine::GetSimCardState result:%{public}d, simState:%{public}d", result, simState);
144         return static_cast<int32_t>(simState);
145     }
146     WIFI_LOGI("StaStateMachine::GetSimCardState out, simState:%{public}d", simState);
147     return static_cast<int32_t>(simState);
148 #else
149     WIFI_LOGW("telephony subsystem is disabled, query sim card state is not supported");
150     return -1;
151 #endif
152 }
153 
IsMultiSimEnabled()154 bool IsMultiSimEnabled()
155 {
156 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
157     int32_t simCount = CoreServiceClient::GetInstance().GetMaxSimCount();
158     WIFI_LOGI("StaStateMachine::IsMultiSimEnabled simCount:%{public}d", simCount);
159     if (simCount > 1) {
160         return true;
161     }
162 #endif
163     return false;
164 }
165 
GetSlotId(int32_t eapSubId)166 int32_t GetSlotId(int32_t eapSubId)
167 {
168     int32_t slotId;
169 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
170     slotId = CoreServiceClient::GetInstance().GetSlotId(eapSubId);
171 #else
172     slotId = -1;
173 #endif
174     return slotId;
175 }
176 
IsSupportCardType(int32_t eapSubId)177 bool IsSupportCardType(int32_t eapSubId)
178 {
179 #ifdef TELEPHONE_CORE_SERVICE_ENABLE
180     CardType cardType;
181     int32_t ret =  CoreServiceClient::GetInstance().GetCardType(
182         CoreServiceClient::GetInstance().GetSlotId(eapSubId), cardType);
183     if (ret != 0) {
184         WIFI_LOGE("failed to get cardType: %{public}d", ret);
185         return false;
186     }
187     if (cardType == CardType::SINGLE_MODE_SIM_CARD) {
188         WIFI_LOGE("invalid cardType: %{public}d", cardType);
189         return false;
190     }
191     return true;
192 #else
193     WIFI_LOGW("telephony subsystem is disabled, sim card is not supported");
194     return false;
195 #endif
196 }
197 } // WifiTelephonyUtils
198 } // Wifi
199 } // OHOS