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 "wifi_c_utils.h"
17 #include <map>
18 #include "wifi_common_util.h"
19 
20 namespace OHOS {
21 namespace Wifi {
22 static std::map<ErrCode, WifiErrorCode> g_ErrCodeMap = {
23     {WIFI_OPT_SUCCESS, WIFI_SUCCESS},
24     {WIFI_OPT_FAILED, ERROR_WIFI_UNKNOWN},
25     {WIFI_OPT_NOT_SUPPORTED, ERROR_WIFI_NOT_SUPPORTED},
26     {WIFI_OPT_INVALID_PARAM, ERROR_WIFI_INVALID_ARGS},
27     {WIFI_OPT_FORBID_AIRPLANE, ERROR_WIFI_NOT_AVAILABLE},
28     {WIFI_OPT_FORBID_POWSAVING, ERROR_WIFI_NOT_AVAILABLE},
29     {WIFI_OPT_PERMISSION_DENIED, ERROR_WIFI_UNKNOWN},
30     {WIFI_OPT_NON_SYSTEMAPP, ERROR_WIFI_UNKNOWN},
31     {WIFI_OPT_OPEN_FAIL_WHEN_CLOSING, ERROR_WIFI_BUSY},
32     {WIFI_OPT_OPEN_SUCC_WHEN_OPENED, ERROR_WIFI_BUSY},
33     {WIFI_OPT_CLOSE_FAIL_WHEN_OPENING, ERROR_WIFI_BUSY},
34     {WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED, ERROR_WIFI_BUSY},
35     {WIFI_OPT_STA_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
36     {WIFI_OPT_SCAN_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
37     {WIFI_OPT_AP_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
38     {WIFI_OPT_INVALID_CONFIG, ERROR_WIFI_UNKNOWN},
39     {WIFI_OPT_GET_ENHANCE_SVC_FAILED, ERROR_WIFI_ENHANCE_SVC}
40 };
41 
GetCErrorCode(ErrCode errCode)42 WifiErrorCode GetCErrorCode(ErrCode errCode)
43 {
44     std::map<ErrCode, WifiErrorCode>::const_iterator iter = g_ErrCodeMap.find(errCode);
45     return iter == g_ErrCodeMap.end() ? ERROR_WIFI_UNKNOWN : iter->second;
46 }
47 
IpStrToArray(const std::string & str,unsigned int ipAddr[IPV4_ARRAY_LEN])48 WifiErrorCode IpStrToArray(const std::string& str, unsigned int ipAddr[IPV4_ARRAY_LEN]) {
49     std::vector<std::string> vec = StrSplit(str, "\\.");
50     if (vec.size() != IPV4_ARRAY_LEN) {
51         return ERROR_WIFI_INVALID_ARGS;
52     }
53     for (int i = 0; i != IPV4_ARRAY_LEN && i != (int)vec.size(); ++i) {
54         ipAddr[i] = static_cast<size_t>(std::stoi(vec[i]));
55     }
56     return WIFI_SUCCESS;
57 }
58 
IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN])59 std::string IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN]) {
60     std::string str = "";
61     for (int i = 0; i != IPV4_ARRAY_LEN; ++i) {
62         str += std::to_string(ipAddr[i]);
63         if (i != IPV4_ARRAY_LEN - 1) {
64             str += ".";
65         }
66     }
67     return str;
68 }
69 }  // namespace Wifi
70 }  // namespace OHOS
71