• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 /* *****************************************************************************
20   1 头文件包含
21 ***************************************************************************** */
22 #include "oal_util.h"
23 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
24 #include <linux/etherdevice.h>
25 #endif
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32 
33 /* ****************************************************************************
34 1 全局变量定义
35 **************************************************************************** */
36 hi_u32 g_level_log = 1;
37 
38 /* ****************************************************************************
39 2 函数实现
40 **************************************************************************** */
41 /* ****************************************************************************
42  功能描述  : 字符串转MAC地址
43  输入参数  : param: MAC地址字符串, 格式 xx:xx:xx:xx:xx:xx  分隔符支持':'与'-'
44  输出参数  : mac_addr: 转换成16进制后的MAC地址
45  返 回 值  :
46 **************************************************************************** */
oal_strtoaddr(const hi_char * param,hi_u8 * mac_addr,hi_u8 mac_addr_len)47 WIFI_ROM_TEXT hi_void oal_strtoaddr(const hi_char *param, hi_u8 *mac_addr, hi_u8 mac_addr_len)
48 {
49     hi_u8 index;
50 
51     /* 获取mac地址,16进制转换 */
52     for (index = 0; index < 12; index++) { /* 循环12次 */
53         if ((*param == ':') || (*param == '-')) {
54             param++;
55             if (index != 0) {
56                 index--;
57             }
58             continue;
59         }
60         if ((index / 2) >= mac_addr_len) { /* 除2 以找到正确的MAC地址 */
61             break;                         /* 防止mac_addr 数组越界 */
62         }
63         mac_addr[index / 2] = /* 除2 以找到正确的MAC地址 */
64             (hi_u8)(mac_addr[index / 2] * 16 * (index % 2) + oal_strtohex(param)); /* 除2 乘16以找到正确的MAC地址 */
65         param++;
66     }
67 }
68 
69 /* ****************************************************************************
70  功能描述  : 找到1字节右数第一个是1的位数
71  输入参数  : pbyte: 要查找的字节
72  输出参数  : 无
73  返 回 值  : 右数第一个是1的位数
74 **************************************************************************** */
oal_bit_find_first_bit_four_byte(hi_u32 word)75 WIFI_ROM_TEXT hi_u8 oal_bit_find_first_bit_four_byte(hi_u32 word)
76 {
77     hi_u8 ret = 0;
78 
79     if (word == 0) {
80         return ret;
81     }
82 
83     if (!(word & 0xffff)) {
84         word >>= 16; /* 右移16bit */
85         ret += 16;   /* bit数加16 */
86     }
87 
88     if (!(word & 0xff)) {
89         word >>= 8; /* 右移8bit */
90         ret += 8;   /* bit数加8 */
91     }
92 
93     if (!(word & 0xf)) {
94         word >>= 4; /* 右移4bit */
95         ret += 4;   /* bit数加4 */
96     }
97 
98     if (!(word & 0x3)) {
99         word >>= 2; /* 右移2bit */
100         ret += 2;   /* bit数加2 */
101     }
102 
103     if (!(word & 1)) {
104         ret += 1;
105     }
106 
107     return ret;
108 }
109 
110 /* ****************************************************************************
111  功能描述  : 从LUT index bitmap表中,获取一个没有被使用的索引,没有找到的话,
112              返回不可用的索引标识(非关键路径,未进行优化,有时间可以优化)
113 **************************************************************************** */
oal_get_lut_index(hi_u8 * lut_index_table,hi_u8 bitmap_len,hi_u16 max_lut_size)114 WIFI_ROM_TEXT hi_u8 oal_get_lut_index(hi_u8 *lut_index_table, hi_u8 bitmap_len, hi_u16 max_lut_size)
115 {
116     hi_u8       byte;
117     hi_u8       bit_idx;
118     hi_u8       temp;
119     hi_u16      index;
120 
121     for (byte = 0; byte < bitmap_len; byte++) {
122         temp = lut_index_table[byte];
123 
124         if (temp == 0xFF) {
125             continue;
126         }
127 
128         for (bit_idx = 0; bit_idx < 8; bit_idx++) { /* 8 bit循环处理 */
129             if ((temp & (1 << bit_idx)) != 0) {
130                 continue;
131             }
132 
133             index = (byte * 8 + bit_idx); /* 乘8转成bit index */
134 
135             if (index < max_lut_size) {
136                 lut_index_table[byte] |= (hi_u8)(1 << bit_idx);
137 
138                 return (hi_u8)index;
139             } else {
140                 return (hi_u8)max_lut_size;
141             }
142         }
143     }
144 
145     return (hi_u8)max_lut_size;
146 }
147 
148 /* ****************************************************************************
149  功能描述  : RSSI低通滤波,注RSSI一定是小于0的数
150  输入参数  : c_old, 老的RSSI;c_new,新的RSSI
151  输出参数  : 滤波后的RSSI
152 **************************************************************************** */
wlan_rssi_lpf(hi_s8 old,hi_s8 new)153 WIFI_ROM_TEXT hi_s8 wlan_rssi_lpf(hi_s8 old, hi_s8 new)
154 {
155     hi_u8   oldval;
156     hi_u8   newval;
157     hi_u16  us_sum;
158 
159     /* 如果c_new是正数或0,则说明该RSSI有问题,不需要往下计算 */
160     if (new >= 0) {
161         return old;
162     }
163 
164     /* 如果是第一次,则直接返回新的RSSI */
165     if (old == WLAN_RSSI_DUMMY_MARKER) {
166         return new;
167     }
168 
169     /* 先获取绝对值,变成正数 */
170     oldval = (hi_u8)oal_abs(old);
171     newval = (hi_u8)oal_abs(new);
172 
173     /* 公式: (uc_old x 7/8 + uc_new x 1/8) */
174     us_sum = (((oldval) << 3) + (newval) - (oldval));
175     newval = (us_sum >> 3) & 0xFF;
176 
177     /* 返回相反数 */
178     return -(newval & WLAN_RSSI_DUMMY_MARKER);
179 }
180 
181 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
oal_atoi(const hi_char * c_string)182 hi_s32 oal_atoi(const hi_char *c_string)
183 {
184     hi_s32 l_ret = 0;
185     hi_s32 flag = 0;
186 
187     for (;; c_string++) {
188         switch (*c_string) {
189             case '0' ... '9':
190                 l_ret = 10 * l_ret + (*c_string - '0'); /* 10:十进制数 */
191                 break;
192             case '-':
193                 flag = 1;
194                 break;
195             case ' ':
196                 continue;
197             default:
198                 return ((flag == 0) ? l_ret : (-l_ret));
199         }
200     }
201 }
202 #endif
203 
204 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
oal_random_ether_addr(hi_u8 * mac_addr,hi_u8 mac_addr_len)205 hi_void oal_random_ether_addr(hi_u8 *mac_addr, hi_u8 mac_addr_len)
206 {
207     struct timeval tv1;
208     struct timeval tv2;
209 
210     hi_unref_param(mac_addr_len);
211     /* 获取随机种子 */
212     gettimeofday(&tv1, NULL);
213 
214     /* 防止秒级种子为0 */
215     tv1.tv_sec += 2; /* 加2 */
216 
217     tv2.tv_sec = (hi_u32)((hi_u32)((hi_u64)tv1.tv_sec * tv1.tv_sec) * (hi_u64)tv1.tv_usec);
218     tv2.tv_usec = (hi_u32)((hi_u32)((hi_u64)tv1.tv_sec * tv1.tv_usec) * (hi_u64)tv1.tv_usec);
219 
220     /* 生成随机的mac地址 */
221     mac_addr[0] = ((hi_u32)tv2.tv_sec & 0xff) & 0xfe;
222     mac_addr[1] = (hi_u32)tv2.tv_usec & 0xff;
223     mac_addr[2] = ((hi_u32)tv2.tv_sec & 0xff0) >> 4;   /* mac_addr[2]右移4 bit */
224     mac_addr[3] = ((hi_u32)tv2.tv_usec & 0xff0) >> 4;  /* mac_addr[3]右移4 bit */
225     mac_addr[4] = ((hi_u32)tv2.tv_sec & 0xff00) >> 8;  /* mac_addr[4]右移8 bit */
226     mac_addr[5] = ((hi_u32)tv2.tv_usec & 0xff00) >> 8; /* mac_addr[5]右移8 bit */
227 }
228 #else
oal_random_ether_addr(hi_u8 * mac_addr,hi_u8 mac_addr_len)229 hi_void oal_random_ether_addr(hi_u8 *mac_addr, hi_u8 mac_addr_len)
230 {
231     hi_unref_param(mac_addr_len);
232     random_ether_addr(mac_addr);
233 }
234 #endif /* #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION) */
235 
236 /* ****************************************************************************
237  功能描述  : 打印对应的内存值
238 **************************************************************************** */
oal_print_hex_dump(const hi_u8 * addr,hi_s32 len,hi_s32 group_size,hi_char * pre_str)239 hi_void oal_print_hex_dump(const hi_u8 *addr, hi_s32 len, hi_s32 group_size, hi_char *pre_str)
240 {
241 #ifdef CONFIG_PRINTK
242 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
243     hi_unref_param(group_size);
244     printk(KERN_DEBUG "buf %p,len:%d\n", addr, len);
245     print_hex_dump(KERN_DEBUG, pre_str, DUMP_PREFIX_ADDRESS, 16, 1, /* 16 */
246         addr, len, true);
247     printk(KERN_DEBUG "\n");
248 #else
249     hi_unref_param(addr);
250     hi_unref_param(group_size);
251     hi_unref_param(pre_str);
252 #endif
253 #endif
254     hi_diag_log_msg_i0(0, "---start--\n");
255     hi_s32 i = 0;
256     for (i = 0; i < len; i++) {
257         hi_diag_log_msg_i2(0, "netbuf[%d]=%02x\n", (hi_u32)i, addr[i]);
258     }
259     hi_diag_log_msg_i0(0, "---end---\n");
260 }
261 
262 #ifdef __cplusplus
263 #if __cplusplus
264 }
265 #endif
266 #endif
267