• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_direct_utils.h"
17 #include <endian.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include "securec.h"
21 #include "softbus_log.h"
22 #include "wifi_direct_types.h"
23 #include "data/link_info.h"
24 
25 #define LOG_LABEL "[WifiDirect] WifiDirectUtils: "
26 
27 #define HEX_DUMP_LINE_NUM 16
28 #define PRINT_BUFFER_LEN 128
29 
TransferModeToRole(enum WifiDirectApiRole mode)30 static enum WifiDirectRole TransferModeToRole(enum WifiDirectApiRole mode)
31 {
32     switch (mode) {
33         case WIFI_DIRECT_API_ROLE_NONE:
34             return WIFI_DIRECT_ROLE_NONE;
35         case WIFI_DIRECT_API_ROLE_GC:
36             return WIFI_DIRECT_ROLE_GC;
37         case WIFI_DIRECT_API_ROLE_GO:
38             return WIFI_DIRECT_ROLE_GO;
39         case WIFI_DIRECT_API_ROLE_HML:
40             return WIFI_DIRECT_ROLE_HML;
41         default:
42             return WIFI_DIRECT_ROLE_INVALID;
43     }
44 }
45 
TransferRoleToPreferLinkMode(enum WifiDirectRole role)46 static enum WifiDirectApiRole TransferRoleToPreferLinkMode(enum WifiDirectRole role)
47 {
48     switch (role) {
49         case WIFI_DIRECT_ROLE_NONE:
50             return WIFI_DIRECT_API_ROLE_NONE;
51         case WIFI_DIRECT_ROLE_GC:
52             return WIFI_DIRECT_API_ROLE_GC | WIFI_DIRECT_API_ROLE_HML;
53         case WIFI_DIRECT_ROLE_GO:
54             return WIFI_DIRECT_API_ROLE_GO | WIFI_DIRECT_API_ROLE_HML;
55         case WIFI_DIRECT_ROLE_HML:
56             return WIFI_DIRECT_API_ROLE_HML;
57         default:
58             return WIFI_DIRECT_API_ROLE_GC | WIFI_DIRECT_API_ROLE_GO | WIFI_DIRECT_API_ROLE_HML;
59     }
60 }
61 
BytesToInt(const uint8_t * data,uint32_t len)62 static uint32_t BytesToInt(const uint8_t *data, uint32_t len)
63 {
64     CONN_CHECK_AND_RETURN_RET_LOG(len <= sizeof(uint32_t), 0, LOG_LABEL "len=%u invalid", len);
65     uint32_t res = 0;
66     CONN_CHECK_AND_RETURN_RET_LOG(memcpy_s(&res, sizeof(res), data, len) == EOK, 0, LOG_LABEL "memcpy_s failed");
67     return le32toh(res);
68 }
69 
IntToBytes(uint32_t data,uint32_t len,uint8_t * out,uint32_t outSize)70 static void IntToBytes(uint32_t data, uint32_t len, uint8_t *out, uint32_t outSize)
71 {
72     if (len > sizeof(uint32_t)) {
73         CLOGE(LOG_LABEL "len=%u invalid", len);
74         return;
75     }
76 
77     data = htole32(data);
78     CONN_CHECK_AND_RETURN_LOG(memcpy_s(out, outSize, &data, len) == EOK, "memcpy_s failed");
79 }
80 
HexDump(const char * banana,const uint8_t * data,size_t size)81 static void HexDump(const char *banana, const uint8_t *data, size_t size)
82 {
83     CLOGI(LOG_LABEL "%s size=%d", banana, size);
84     char line[64];
85     int32_t pos = 0;
86     bool isLastPrinted = false;
87     for (size_t i = 1; i <= size; i++) {
88         isLastPrinted = false;
89         int32_t ret;
90         if (i % HEX_DUMP_LINE_NUM == 1) {
91             ret = sprintf_s(line + pos, sizeof(line) - pos, "%02x", data[i - 1]);
92         } else {
93             ret = sprintf_s(line + pos, sizeof(line) - pos, " %02x", data[i - 1]);
94         }
95         if (ret <= 0) {
96             CLOGI(LOG_LABEL "sprintf failed");
97             return;
98         }
99         pos += ret;
100         if (i % HEX_DUMP_LINE_NUM == 0) {
101             pos = 0;
102             isLastPrinted = true;
103             CLOGI(LOG_LABEL "%s", line);
104         }
105     }
106     if (!isLastPrinted) {
107         CLOGI(LOG_LABEL "%s", line);
108     }
109 }
110 
ShowLinkInfoList(const char * banana,ListNode * list)111 static void ShowLinkInfoList(const char *banana, ListNode *list)
112 {
113     CLOGI(LOG_LABEL "%s", banana);
114     struct LinkInfo *info = NULL;
115     LIST_FOR_EACH_ENTRY(info, list, struct LinkInfo, node) {
116         CLOGI(LOG_LABEL "interface=%s mode=%d", info->getString(info, LI_KEY_LOCAL_INTERFACE, ""),
117               info->getInt(info, LI_KEY_LOCAL_LINK_MODE, -1));
118     }
119 }
120 
PrintLargeString(const char * string)121 static void PrintLargeString(const char *string)
122 {
123     char buffer[PRINT_BUFFER_LEN + 1] = {0};
124     size_t stringLen = strlen(string);
125     size_t printLen = 0;
126     while (printLen < stringLen) {
127         size_t copyLen = MIN(PRINT_BUFFER_LEN, stringLen - printLen);
128         if (memcpy_s(buffer, copyLen, string + printLen, copyLen) != EOK) {
129             CLOGE("buffer memcpy fail");
130             return;
131         }
132         buffer[copyLen] = 0;
133         printLen += copyLen;
134         CLOGI(LOG_LABEL "%s", buffer);
135     }
136 }
137 
StrCompareIgnoreCase(const char * str1,const char * str2)138 static int32_t StrCompareIgnoreCase(const char *str1, const char *str2)
139 {
140     while (*str1 && *str2) {
141         int c1 = *str1;
142         int c2 = *str2;
143         if (isupper(c1)) {
144             c1 = c1 + 'a' - 'A';
145         }
146         if (isupper(c2)) {
147             c2 = c2 + 'a' - 'A';
148         }
149         if (c1 != c2) {
150             return c1 - c2;
151         }
152         str1++;
153         str2++;
154     }
155     return *str1 - *str2;
156 }
157 
158 static struct WifiDirectUtils g_utils = {
159     .transferModeToRole = TransferModeToRole,
160     .transferRoleToPreferLinkMode = TransferRoleToPreferLinkMode,
161     .bytesToInt = BytesToInt,
162     .intToBytes = IntToBytes,
163     .hexDump = HexDump,
164     .showLinkInfoList = ShowLinkInfoList,
165     .printLargeString = PrintLargeString,
166     .strCompareIgnoreCase = StrCompareIgnoreCase,
167 };
168 
GetWifiDirectUtils(void)169 struct WifiDirectUtils* GetWifiDirectUtils(void)
170 {
171     return &g_utils;
172 }