1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /**
8  * @brief File containing utility function definitions for the
9  * Wi-Fi driver.
10  */
11 
12 #include <util.h>
13 #include "host_rpu_data_if.h"
14 
nrf_wifi_utils_hex_str_to_val(unsigned char * hex_arr,unsigned int hex_arr_sz,unsigned char * str)15 int nrf_wifi_utils_hex_str_to_val(unsigned char *hex_arr,
16 				  unsigned int hex_arr_sz,
17 				  unsigned char *str)
18 {
19 	unsigned int i = 0;
20 	unsigned int j = 0;
21 	unsigned char ch = 0;
22 	unsigned char val = 0;
23 	unsigned int len = 0;
24 	int ret = -1;
25 
26 	len = nrf_wifi_osal_strlen(str);
27 
28 	if (len / 2 > hex_arr_sz) {
29 		nrf_wifi_osal_log_err("%s: String length (%d) greater than array size (%d)",
30 				      __func__,
31 				      len,
32 				      hex_arr_sz);
33 		goto out;
34 	}
35 
36 	if (len % 2) {
37 		nrf_wifi_osal_log_err("%s:String length = %d, is not a multiple of 2",
38 				      __func__,
39 				      len);
40 		goto out;
41 	}
42 
43 	for (i = 0; i < len; i++) {
44 		/* Convert each character to lower case */
45 		ch = ((str[i] >= 'A' && str[i] <= 'Z') ? str[i] + 32 : str[i]);
46 
47 		if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) {
48 			nrf_wifi_osal_log_err("%s: Invalid hex character in string %d",
49 					      __func__,
50 					      ch);
51 			goto out;
52 		}
53 
54 		if (ch >= '0' && ch <= '9') {
55 			ch = ch - '0';
56 		} else {
57 			ch = ch - 'a' + 10;
58 		}
59 
60 		val += ch;
61 
62 		if (!(i % 2)) {
63 			val <<= 4;
64 		} else {
65 			hex_arr[j] = val;
66 			j++;
67 			val = 0;
68 		}
69 	}
70 
71 	ret = j;
72 out:
73 	return ret;
74 }
75 
76 
nrf_wifi_utils_is_mac_addr_valid(const char * mac_addr)77 bool nrf_wifi_utils_is_mac_addr_valid(const char *mac_addr)
78 {
79 	unsigned char zero_addr[NRF_WIFI_ETH_ADDR_LEN] = {0};
80 
81 	return (mac_addr &&
82 		(nrf_wifi_osal_mem_cmp(mac_addr,
83 				       zero_addr,
84 				       sizeof(zero_addr)) != 0) &&
85 		!(mac_addr[0] & 0x1));
86 }
87 
88 
nrf_wifi_utils_chan_to_freq(enum nrf_wifi_band band,unsigned short chan)89 int nrf_wifi_utils_chan_to_freq(enum nrf_wifi_band band,
90 				unsigned short chan)
91 {
92 	int freq = -1;
93 	unsigned short valid_5g_chans[] = {32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 96, 100, 104,
94 		108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 149, 153, 157, 159, 161, 163,
95 		165, 167, 169, 171, 173, 175, 177};
96 	unsigned char i = 0;
97 
98 	switch (band) {
99 	case NRF_WIFI_BAND_2GHZ:
100 		if ((chan >= 1) && (chan <= 13)) {
101 			freq = (((chan - 1) * 5) + 2412);
102 		} else if (chan == 14) {
103 			freq = 2484;
104 		} else {
105 			nrf_wifi_osal_log_err("%s: Invalid channel value %d",
106 					      __func__,
107 					      chan);
108 			goto out;
109 		}
110 		break;
111 	case NRF_WIFI_BAND_5GHZ:
112 		for (i = 0; i < ARRAY_SIZE(valid_5g_chans); i++) {
113 			if (chan == valid_5g_chans[i]) {
114 				freq = (chan * 5) + 5000;
115 				break;
116 			}
117 		}
118 
119 		break;
120 	default:
121 		nrf_wifi_osal_log_err("%s: Invalid band value %d",
122 				      __func__,
123 				      band);
124 		goto out;
125 	}
126 out:
127 	return freq;
128 
129 }
130