• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
40 
GetCErrorCode(ErrCode errCode)41 WifiErrorCode GetCErrorCode(ErrCode errCode)
42 {
43     std::map<ErrCode, WifiErrorCode>::const_iterator iter = g_ErrCodeMap.find(errCode);
44     return iter == g_ErrCodeMap.end() ? ERROR_WIFI_UNKNOWN : iter->second;
45 }
46 
IpStrToArray(const std::string & str,unsigned int ipAddr[IPV4_ARRAY_LEN])47 WifiErrorCode IpStrToArray(const std::string& str, unsigned int ipAddr[IPV4_ARRAY_LEN]) {
48     std::vector<std::string> vec = StrSplit(str, "\\.");
49     if (vec.size() != IPV4_ARRAY_LEN) {
50         return ERROR_WIFI_INVALID_ARGS;
51     }
52     for (int i = 0; i != IPV4_ARRAY_LEN && i != (int)vec.size(); ++i) {
53         ipAddr[i] = std::stoi(vec[i]);
54     }
55     return WIFI_SUCCESS;
56 }
57 
IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN])58 std::string IpArrayToStr(const unsigned int ipAddr[IPV4_ARRAY_LEN]) {
59     std::string str = "";
60     for (int i = 0; i != IPV4_ARRAY_LEN; ++i) {
61         str += std::to_string(ipAddr[i]);
62         if (i != IPV4_ARRAY_LEN - 1) {
63             str += ".";
64         }
65     }
66     return str;
67 }
68 }  // namespace Wifi
69 }  // namespace OHOS
70