1 /*
2  * Copyright (C) 2022 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 "wifi_msg.h"
17 #include <string>
18 
19 namespace OHOS {
20 namespace Wifi {
21 static const std::string PREFIX_AUTH = "auth=";
22 static const std::string PREFIX_AUTHEAP = "autheap=";
23 static const std::string METHOD_STRS[] = { "NONE", "PAP", "MSCHAP", "MSCHAPV2", "GTC", "SIM", "AKA", "AKA'" };
24 
Phase2MethodToStr(const std::string & eap,const int & method)25 std::string WifiEapConfig::Phase2MethodToStr(const std::string& eap, const int& method)
26 {
27     if (method < 0 || method >= static_cast<int>(sizeof(METHOD_STRS) / sizeof(METHOD_STRS[0]))) {
28         return "auth=NONE";
29     }
30     std::string prefix = (eap == EAP_METHOD_TTLS && method == static_cast<int>(Phase2Method::GTC)) ?
31         PREFIX_AUTHEAP : PREFIX_AUTH;
32     return prefix + METHOD_STRS[method];
33 }
34 
Phase2MethodFromStr(const std::string & str)35 Phase2Method WifiEapConfig::Phase2MethodFromStr(const std::string& str)
36 {
37     std::string methodStr;
38     if (str.find(PREFIX_AUTH) == 0) {
39         methodStr = str.substr(PREFIX_AUTH.length());
40     } else if (str.find(PREFIX_AUTHEAP) == 0) {
41         methodStr = str.substr(PREFIX_AUTHEAP.length());
42     } else {
43         return Phase2Method::NONE;
44     }
45     int len = sizeof(METHOD_STRS) / sizeof(METHOD_STRS[0]);
46     for (int i = 0; i < len; i++) {
47         if (METHOD_STRS[i] == methodStr) {
48             return Phase2Method(i);
49         }
50     }
51     return Phase2Method::NONE;
52 }
Str2EapMethod(const std::string & str)53 EapMethod WifiEapConfig::Str2EapMethod(const std::string& str)
54 {
55     const std::string eapMethod[] = { "NONE", "PEAP", "TLS", "TTLS", "PWD", "SIM", "AKA", "AKA'" };
56     int len = sizeof(eapMethod) / sizeof(eapMethod[0]);
57     for (int i = 0; i < len; i++) {
58         if (eapMethod[i] == str) {
59             return EapMethod(i);
60         }
61     }
62     return EapMethod::EAP_NONE;
63 }
64 } // namespace Wifi
65 } // namespace OHOS
66