1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: oam log printing interface
15 */
16
17 #ifndef __WLAN_UTIL_COMMON_ROM_H__
18 #define __WLAN_UTIL_COMMON_ROM_H__
19
20 #include "securec.h"
21 #include "td_type.h"
22
23 #include "osal_types.h"
24
25 #include "soc_osal.h"
26 #include "oal_types_device_rom.h"
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32
33 #define OAL_RSSI_INIT_MARKER 0x320 /* RSSI平滑值初始值 */
34 #define OAL_RSSI_MAX_DELTA 24 /* 最大步长 24/8 = 3 */
35 #define OAL_RSSI_FRACTION_BITS 3
36 #define OAL_RSSI_SIGNAL_MIN (-103) /* 上报RSSI下边界 */
37 #define OAL_RSSI_SIGNAL_MAX 5 /* 上报RSSI上边界 */
38 #define OAL_SNR_INIT_VALUE 0x7F /* SNR上报的初始值 */
39 #define OAL_RSSI_INIT_VALUE (-128) /* RSSI的初始值 */
40
41 /* 位数定义 */
42 #define NUM_1_BITS 1
43 #define NUM_2_BITS 2
44 #define NUM_3_BITS 3
45 #define NUM_4_BITS 4
46 #define NUM_5_BITS 5
47 #define NUM_8_BITS 8
48 #define NUM_16_BITS 16
49 #define NUM_20_BITS 20
50 #define NUM_26_BITS 26
51
52
53 /* 位偏移定义 */
54 #define BIT_OFFSET_0 0
55 #define BIT_OFFSET_1 1
56 #define BIT_OFFSET_2 2
57 #define BIT_OFFSET_4 4
58 #define BIT_OFFSET_5 5
59 #define BIT_OFFSET_6 6
60 #define BIT_OFFSET_7 7
61 #define BIT_OFFSET_8 8
62 #define BIT_OFFSET_9 9
63 #define BIT_OFFSET_10 10
64 #define BIT_OFFSET_11 11
65 #define BIT_OFFSET_12 12
66 #define BIT_OFFSET_13 13
67 #define BIT_OFFSET_14 14
68 #define BIT_OFFSET_15 15
69 #define BIT_OFFSET_16 16
70 #define BIT_OFFSET_17 17
71 #define BIT_OFFSET_18 18
72 #define BIT_OFFSET_19 19
73 #define BIT_OFFSET_20 20
74 #define BIT_OFFSET_21 21
75 #define BIT_OFFSET_24 24
76 #define BIT_OFFSET_26 26
77
78 #ifdef BUILD_UT
79 #define OSAL_STATIC
80 #else
81 #define OSAL_STATIC static
82 #endif
83
84 #ifndef unref_param
85 #define unref_param(P) ((P) = (P))
86 #endif
87
88 #ifndef unref_param_prv
89 #define unref_param_prv(P) ((P) = (P))
90 #endif
91
92 /* 获取数组大小 */
93 #define oal_array_size(_ast_array) (sizeof(_ast_array) / sizeof((_ast_array)[0]))
94
95 #define OAL_BITS_PER_BYTE 8 /* 一个字节中包含的bit数目 */
96
97 #define SWAP_BYTEORDER_MSAK_8 8
98 #define SWAP_BYTEORDER_MSAK_24 24
99 #define SWAP_BYTEORDER_MSAK_40 40
100 #define SWAP_BYTEORDER_MSAK_56 56
101
102 /* 大小端转换 */
103 #define oal_swap_byteorder_16(value) ((((value) & 0xFF) << 8) + (((value) & 0xFF00) >> 8))
104
105 #define oal_swap_byteorder_32(value) \
106 ((osal_u32)(((value) & 0x000000FF) << 24) + \
107 (osal_u32)(((value) & 0x0000FF00) << 8) + \
108 (osal_u32)(((value) & 0x00FF0000) >> 8) + \
109 (osal_u32)(((value) & 0xFF000000) >> 24))
110
oal_swap_byteorder_64(osal_u64 value)111 static INLINE__ osal_u64 oal_swap_byteorder_64(osal_u64 value)
112 {
113 return ((((value) & 0x00000000000000ffULL) << SWAP_BYTEORDER_MSAK_56) +
114 (((value) & 0x000000000000ff00ULL) << SWAP_BYTEORDER_MSAK_40) +
115 (((value) & 0x0000000000ff0000ULL) << SWAP_BYTEORDER_MSAK_24) +
116 (((value) & 0x00000000ff000000ULL) << SWAP_BYTEORDER_MSAK_8) +
117 (((value) & 0x000000ff00000000ULL) >> SWAP_BYTEORDER_MSAK_8) +
118 (((value) & 0x0000ff0000000000ULL) >> SWAP_BYTEORDER_MSAK_24) +
119 (((value) & 0x00ff000000000000ULL) >> SWAP_BYTEORDER_MSAK_40) +
120 (((value) & 0xff00000000000000ULL) >> SWAP_BYTEORDER_MSAK_56));
121 }
122
123 /* 修改_PRE_BIG_CPU_ENDIAN保持HMAC&DMAC统一 */
124 #if defined(_PRE_BIG_CPU_ENDIAN) && defined(_PRE_BIG_CPU_ENDIAN) && defined(_PRE_CPU_ENDIAN) \
125 && (_PRE_BIG_CPU_ENDIAN == _PRE_CPU_ENDIAN) /* BIG_ENDIAN */
126 #define oal_byteorder_to_le32(_val) oal_swap_byteorder_32(_val)
127 #define oal_byteorder_to_le16(_val) oal_swap_byteorder_16(_val)
128 #define oal_mask_inverse(_len, _offset) ((osal_u32)(oal_swap_byteorder_32(~(((1 << (_len)) - 1) << (_offset)))))
129 #define oal_mask(_len, _offset) ((osal_u32)(oal_swap_byteorder_32(((1 << (_len)) - 1) << (_offset))))
130 #else /* LITTLE_ENDIAN */
131 #define oal_byteorder_to_le32(_val) (_val)
132 #define oal_byteorder_to_le16(_val) (_val)
133 #define oal_mask_inverse(_len, _offset) ((osal_u32)(~(((1UL << (_len)) - 1) << (_offset))))
134 #define oal_mask(_len, _offset) ((osal_u32)(((1UL << (_len)) - 1) << (_offset)))
135 #endif
136
137 /* 位操作 */
138 #define oal_set_bit(_val) (1 << (_val))
139 #define oal_left_shift(_data, _num) (osal_s64)((osal_u64)(_data) << (_num))
140 #define oal_rght_shift(_data, _num) ((_data) >> (_num))
141 #define oal_get_bits(_data, _bits, _pos) (((_data) >> (_pos)) & (((osal_u32)1 << (_bits)) - 1))
142
143 /* ****************************************************************************
144 函 数 名 : oal_sub
145 功能描述 : 求两数之差,限定差值大于0
146 **************************************************************************** */
oal_sub(osal_u16 a,osal_u16 b)147 static INLINE__ osal_u8 oal_sub(osal_u16 a, osal_u16 b)
148 {
149 return (a > b) ? (a - b) : 0;
150 }
151
152 /* ****************************************************************************
153 函 数 名 : oal_make_word16
154 功能描述 : 将两个8bit数拼接为16bit数
155 **************************************************************************** */
oal_make_word16(osal_u8 lsb,osal_u8 msb)156 static INLINE__ osal_u16 oal_make_word16(osal_u8 lsb, osal_u8 msb)
157 {
158 return ((((osal_u16)msb << 8) & 0xFF00) | lsb); /* msb 左移 8位 */
159 }
160
161 /* ****************************************************************************
162 函 数 名 : oal_make_word32
163 功能描述 : 将两个16bit数拼接为32bit数
164 **************************************************************************** */
oal_make_word32(osal_u16 lsw,osal_u16 msw)165 static INLINE__ osal_u32 oal_make_word32(osal_u16 lsw, osal_u16 msw)
166 {
167 return ((((osal_u32)msw << 16) & 0xFFFF0000) | lsw); /* msw 左移 16位 */
168 }
169
170 /* ****************************************************************************
171 函 数 名 : oal_join_word32
172 功能描述 : 将四个8bit数拼接为32bit数
173 **************************************************************************** */
oal_join_word32(osal_u8 lsb,osal_u8 ssb,osal_u8 asb,osal_u8 msb)174 static INLINE__ osal_u32 oal_join_word32(osal_u8 lsb, osal_u8 ssb, osal_u8 asb, osal_u8 msb)
175 {
176 return (((osal_u32)msb << 24) | ((osal_u32)asb << 16) | ((osal_u32)ssb << 8) | lsb); /* 分别左移24 16 8 位 */
177 }
178
179 /* ****************************************************************************
180 函 数 名 : oal_absolute_sub
181 功能描述 : 返回两数之差的绝对值
182 **************************************************************************** */
oal_absolute_sub(osal_u32 num_a,osal_u32 num_b)183 static INLINE__ osal_u32 oal_absolute_sub(osal_u32 num_a, osal_u32 num_b)
184 {
185 return ((num_a > num_b) ? (num_a - num_b) : (num_b - num_a));
186 }
187
188 /* ****************************************************************************
189 函 数 名 : round_pos
190 功能描述 : 定点数四舍五入,并取整数部分, fract_bits为小数位数
191 **************************************************************************** */
round_pos(osal_s32 fix_num,osal_s32 fract_bits)192 static INLINE__ osal_s32 round_pos(osal_s32 fix_num, osal_s32 fract_bits)
193 {
194 return ((fix_num + (1 << (fract_bits - 1))) >> fract_bits);
195 }
196
197 /* ****************************************************************************
198 函 数 名 : round_neg
199 功能描述 : 定点数四舍五入,并取整数部分, fract_bits为小数位数
200 **************************************************************************** */
round_neg(osal_s32 fix_num,osal_s32 fract_bits)201 static INLINE__ osal_s32 round_neg(osal_s32 fix_num, osal_s32 fract_bits)
202 {
203 return (osal_s32)(0 - round_pos(0 - fix_num, fract_bits));
204 }
205
206 /* ****************************************************************************
207 函 数 名 : oal_round
208 功能描述 : 定点数四舍五入,并取整数部分, fract_bits为小数位数
209 **************************************************************************** */
oal_round(osal_s32 fix_num,osal_s32 fract_bits)210 static INLINE__ osal_s32 oal_round(osal_s32 fix_num, osal_s32 fract_bits)
211 {
212 return (fix_num > 0 ? round_pos(fix_num, fract_bits) : round_neg(fix_num, fract_bits));
213 }
214
oal_pow(osal_s32 x,osal_u32 n)215 static INLINE__ osal_s32 oal_pow(osal_s32 x, osal_u32 n)
216 {
217 osal_s32 result = 1, pow_n = n, pow_x = x;
218 while (pow_n != 0) {
219 if ((pow_n & 0x1) != 0) {
220 result *= pow_x;
221 }
222 pow_n >>= 1;
223 pow_x *= pow_x;
224 }
225 return result;
226 }
227
228 /* ****************************************************************************
229 函 数 名 : oal_bit_get_num_four_byte
230 功能描述 : 获取4字节中bit1的个数
231 **************************************************************************** */
oal_bit_get_num_four_byte(osal_u32 byte)232 static INLINE__ osal_u32 oal_bit_get_num_four_byte(osal_u32 byte)
233 {
234 osal_u32 byte_data = byte;
235
236 byte_data = (byte_data & 0x55555555) + ((byte_data >> 1) & 0x55555555); /* 0x55555555掩码, 1移位数 */
237 byte_data = (byte_data & 0x33333333) + ((byte_data >> 2) & 0x33333333); /* 0x33333333掩码, 2移位数 */
238 byte_data = (byte_data & 0x0F0F0F0F) + ((byte_data >> 4) & 0x0F0F0F0F); /* 0x0F0F0F0F掩码, 4移位数 */
239 byte_data = (byte_data & 0x00FF00FF) + ((byte_data >> 8) & 0x00FF00FF); /* 0x00FF00FF掩码, 8移位数 */
240 byte_data = (byte_data & 0x0000FFFF) + ((byte_data >> 16) & 0x0000FFFF); /* 0x0000FFFF掩码, 16移位数 */
241
242 return byte_data;
243 }
244
245 /* ****************************************************************************
246 函 数 名 : oal_bit_set_bit_one_byte
247 功能描述 : 对1字节的指定位置一
248 **************************************************************************** */
oal_bit_set_bit_one_byte(osal_u8 * byte,osal_u32 nr)249 static INLINE__ osal_void oal_bit_set_bit_one_byte(osal_u8 *byte, osal_u32 nr)
250 {
251 *byte |= ((osal_u8)(1 << nr));
252 }
253
254 /* ****************************************************************************
255 函 数 名 : oal_bit_clear_bit_one_byte
256 功能描述 : 对1字节的指定位置零
257 **************************************************************************** */
oal_bit_clear_bit_one_byte(osal_u8 * byte,osal_u32 nr)258 static INLINE__ osal_void oal_bit_clear_bit_one_byte(osal_u8 *byte, osal_u32 nr)
259 {
260 *byte &= (~((osal_u8)(1 << nr)));
261 }
262
oal_bit_get_bit_one_byte(osal_u8 byte,osal_u32 nr)263 static INLINE__ osal_u8 oal_bit_get_bit_one_byte(osal_u8 byte, osal_u32 nr)
264 {
265 return ((byte >> nr) & 0x1);
266 }
267
268 /* ****************************************************************************
269 函 数 名 : oal_bit_set_bit_eight_byte
270 功能描述 : 对8字节的指定位置一
271 **************************************************************************** */
oal_bit_set_bit_eight_byte(osal_u64 * pull_byte,osal_u32 nr)272 static INLINE__ osal_void oal_bit_set_bit_eight_byte(osal_u64 *pull_byte, osal_u32 nr)
273 {
274 *pull_byte |= ((osal_u64)1 << nr);
275 }
276
277 /* ****************************************************************************
278 函 数 名 : oal_bit_find_first_bit_one_byte
279 功能描述 : 找到1字节右数第一个是1的位数
280 **************************************************************************** */
oal_bit_find_first_bit_one_byte(osal_u8 byte)281 static INLINE__ osal_u8 oal_bit_find_first_bit_one_byte(osal_u8 byte)
282 {
283 osal_u8 ret = 0, byte_data = byte;
284
285 byte_data = byte_data & (-byte_data);
286
287 while (byte_data != 1) {
288 ret++;
289 byte_data = (byte_data >> 1);
290
291 if (ret > 7) { /* 7表示超过字节最大bit数。退出循环 */
292 return ret;
293 }
294 }
295
296 return ret;
297 }
298
299 /*****************************************************************************
300 函 数 名 : oal_set_mac_addr
301 功能描述 : 将第二个mac地址 赋值给第一个mac地址
302 **************************************************************************** */
oal_set_mac_addr(osal_u8 * addr1,const osal_u8 * addr2)303 static INLINE__ osal_void oal_set_mac_addr(osal_u8 *addr1, const osal_u8 *addr2)
304 {
305 const osal_u32 mac_len = 6; /* 6字节mac地址长度 */
306 (osal_void)memcpy_s(addr1, mac_len, addr2, mac_len);
307 }
308
309 /*****************************************************************************
310 函 数 名 : oal_compare_mac_addr
311 功能描述 : 比较两个mac地址是否相等
312 **************************************************************************** */
oal_compare_mac_addr(const osal_u8 * mac_addr1,const osal_u8 * mac_addr2)313 static INLINE__ osal_u32 oal_compare_mac_addr(const osal_u8 *mac_addr1, const osal_u8 *mac_addr2)
314 {
315 const osal_u32 mac_len = 6; /* 6字节mac地址长度 */
316 return (osal_u32)(memcmp(mac_addr1, mac_addr2, mac_len));
317 }
318
319 /* ****************************************************************************
320 函 数 名 : oal_byteorder_host_to_net_uint16
321 功能描述 : 将16位本地字节序转换为网络字节序
322 **************************************************************************** */
oal_byteorder_host_to_net_uint16(osal_u16 byte)323 static INLINE__ osal_u16 oal_byteorder_host_to_net_uint16(osal_u16 byte)
324 {
325 osal_u16 byte_data = byte;
326 byte_data = ((byte_data & 0x00FF) << 8) + ((byte_data & 0xFF00) >> 8); /* 0x00FF,0xFF00高低字节掩码,8高低字节移位 */
327 return byte_data;
328 }
329
330 /* ****************************************************************************
331 函 数 名 : oal_del_lut_index
332 功能描述 : 在LUT index bitmap表中,删除一个LUT index (注:%可以作为优化项)
333 **************************************************************************** */
oal_del_lut_index(osal_u8 * lut_index_table,osal_u8 idx)334 static INLINE__ osal_void oal_del_lut_index(osal_u8 *lut_index_table, osal_u8 idx)
335 {
336 osal_u8 byte = idx / NUM_8_BITS;
337 osal_u8 bit = idx % NUM_8_BITS;
338
339 lut_index_table[byte] &= ~(osal_u8)(1 << bit); /* 1bit移位 */
340 }
341
oal_memcmp(const osal_void * buf1,const osal_void * buf2,osal_u32 count)342 static INLINE__ osal_s32 oal_memcmp(const osal_void *buf1, const osal_void *buf2, osal_u32 count)
343 {
344 return memcmp(buf1, buf2, count);
345 }
346
347 osal_u8 oal_bit_find_first_bit_four_byte(osal_u32 byte);
348 osal_u8 oal_get_lut_index(osal_u8 *lut_index_table, osal_u8 bmap_len, osal_u16 max_lut_size,
349 osal_u16 start, osal_u16 stop);
350
351 /* rssi平滑函数改为非内联函数,内联版本仅保证rom编译通过 */
352 osal_s8 oal_get_real_rssi(osal_s16 s_scaled_rssi);
353 osal_void oal_rssi_smooth(osal_s16 *old_rssi, osal_s8 new_rssi);
354
355 #ifdef __cplusplus
356 #if __cplusplus
357 }
358 #endif
359 #endif
360
361 #endif /* end of wlan_util_common.h */
362