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_OPEN_FAIL_WHEN_CLOSING, ERROR_WIFI_BUSY},
31 {WIFI_OPT_OPEN_SUCC_WHEN_OPENED, ERROR_WIFI_BUSY},
32 {WIFI_OPT_CLOSE_FAIL_WHEN_OPENING, ERROR_WIFI_BUSY},
33 {WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED, ERROR_WIFI_BUSY},
34 {WIFI_OPT_STA_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
35 {WIFI_OPT_SCAN_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
36 {WIFI_OPT_AP_NOT_OPENED, ERROR_WIFI_NOT_STARTED},
37 {WIFI_OPT_INVALID_CONFIG, ERROR_WIFI_UNKNOWN}
38 };
39
GetCErrorCode(ErrCode errCode)40 WifiErrorCode GetCErrorCode(ErrCode errCode)
41 {
42 std::map<ErrCode, WifiErrorCode>::const_iterator iter = g_ErrCodeMap.find(errCode);
43 return iter == g_ErrCodeMap.end() ? ERROR_WIFI_UNKNOWN : iter->second;
44 }
45
IpStrToArray(const std::string & str,unsigned int ipAddr[IPV4_ARRAY_LEN])46 WifiErrorCode IpStrToArray(const std::string& str, unsigned int ipAddr[IPV4_ARRAY_LEN]) {
47 std::vector<std::string> vec = StrSplit(str, "\\.");
48 if (vec.size() != IPV4_ARRAY_LEN) {
49 return ERROR_WIFI_INVALID_ARGS;
50 }
51 for (int i = 0; i != IPV4_ARRAY_LEN && i != (int)vec.size(); ++i) {
52 ipAddr[i] = std::stoi(vec[i]);
53 }
54 return WIFI_SUCCESS;
55 }
56
IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN])57 std::string IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN]) {
58 std::string str = "";
59 for (int i = 0; i != IPV4_ARRAY_LEN; ++i) {
60 str += std::to_string(ipAddr[i]);
61 if (i != IPV4_ARRAY_LEN - 1) {
62 str += ".";
63 }
64 }
65 return str;
66 }
67 } // namespace Wifi
68 } // namespace OHOS
69