1 /*
2  * Copyright 2023, Cypress Semiconductor Corporation (an Infineon company)
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /**
19  * @file WHD utilities
20  *
21  * Utilities to help do specialized (not general purpose) WHD specific things
22  */
23 #include "whd_chip.h"
24 #include "whd_events_int.h"
25 #include "whd_types_int.h"
26 
27 #ifndef INCLUDED_WHD_UTILS_H_
28 #define INCLUDED_WHD_UTILS_H_
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * Get the offset (in bytes) of a member within a structure
36  */
37 #define OFFSET(type, member)                          ( (uint32_t)&( (type *)0 )->member )
38 
39 /**
40  * determine size (number of elements) in an array
41  */
42 #ifndef ARRAY_SIZE
43 #define ARRAY_SIZE(a)                                 (sizeof(a) / sizeof(a[0]) )
44 #endif
45 
46 /** Searches for a specific WiFi Information Element in a byte array
47  *
48  * Traverse a string of 1-byte tag/1-byte length/variable-length value
49  * triples, returning a pointer to the substring whose first element
50  * matches tag
51  *
52  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
53  *
54  *
55  * @param tlv_buf : The byte array containing the Information Elements (IEs)
56  * @param buflen  : The length of the tlv_buf byte array
57  * @param key     : The Information Element tag to search for
58  *
59  * @return    NULL : if no matching Information Element was found
60  *            Non-Null : Pointer to the start of the matching Information Element
61  */
62 
63 whd_tlv8_header_t *whd_parse_tlvs(const whd_tlv8_header_t *tlv_buf, uint32_t buflen, dot11_ie_id_t key);
64 
65 /** Checks if a WiFi Information Element is a WPA entry
66  *
67  * Is this body of this tlvs entry a WPA entry? If
68  * not update the tlvs buffer pointer/length
69  *
70  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
71  *
72  * @param wpaie    : The byte array containing the Information Element (IE)
73  * @param tlvs     : The larger IE array to be updated if not a WPA IE
74  * @param tlvs_len : The current length of larger IE array
75  *
76  * @return    WHD_TRUE  : if IE matches the WPA OUI (Organizationally Unique Identifier) and its type = 1
77  *            WHD_FALSE : otherwise
78  */
79 whd_bool_t whd_is_wpa_ie(vendor_specific_ie_header_t *wpaie, whd_tlv8_header_t **tlvs, uint32_t *tlvs_len);
80 
81 /** Searches for a specific WiFi Information Element in a byte array
82  *
83  * Traverse a string of 1-byte tag/1-byte length/variable-length value
84  * triples, returning a pointer to the substring whose first element
85  * matches tag
86  *
87  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
88  *
89  *
90  * @param tlv_buf : The byte array containing the Information Elements (IEs)
91  * @param buflen  : The length of the tlv_buf byte array
92  * @param key     : The Information Element tag to search for
93  *
94  * @return    NULL : if no matching Information Element was found
95  *            Non-Null : Pointer to the start of the matching Information Element
96  */
97 whd_tlv8_header_t *whd_parse_dot11_tlvs(const whd_tlv8_header_t *tlv_buf, uint32_t buflen, dot11_ie_id_t key);
98 
99 /******************************************************
100 *             Debug helper functionality
101 ******************************************************/
102 #ifdef WPRINT_ENABLE_WHD_DEBUG
103 const char *whd_event_to_string(whd_event_num_t var);
104 char *whd_ssid_to_string(uint8_t *value, uint8_t length, char *ssid_buf, uint8_t ssid_buf_len);
105 const char *whd_status_to_string(whd_event_status_t status);
106 const char *whd_reason_to_string(whd_event_reason_t reason);
107 char *whd_ether_ntoa(const uint8_t *ea, char *buf, uint8_t buf_len);
108 const char *whd_ioctl_to_string(uint32_t ioctl);
109 #endif /* ifdef WPRINT_ENABLE_WHD_DEBUG */
110 
111 /**
112  ******************************************************************************
113  * Prints partial details of a scan result on a single line
114  * @param[in] record :  A pointer to the whd_scan_result_t record
115  *
116  */
117 extern void whd_print_scan_result(whd_scan_result_t *record);
118 
119 /**
120  ******************************************************************************
121  * Convert a security bitmap to string
122  * @param[in] security :  security of type whd_security_t
123  * @param[in] out_str :  a character array to store output
124  * @param[in] out_str_len :  length of out_str char array
125  *
126  */
127 extern void whd_convert_security_type_to_string(whd_security_t security, char *out_str, uint16_t out_str_len);
128 
129 /*!
130  ******************************************************************************
131  * Convert an IOCTL to a string.
132  *
133  * @param[in] cmd  The ioct_log value.
134  * @param[out] ioctl_str The string value after conversion.
135  * @param[out] ioctl_str_len The string length of the IOCTL string.
136  *
137  * @result
138  */
139 void whd_ioctl_info_to_string(uint32_t cmd, char *ioctl_str, uint16_t ioctl_str_len);
140 
141 /*!
142  ******************************************************************************
143  * Convert event, status and reason value coming from firmware to string.
144  *
145  * @param[in] cmd  The event value in numeric form.
146  * @param[in] flag  The status value in numeric form.
147  * @param[in] reason  The reson value in numeric form.
148  * @param[out] ioctl_str  The string representation of event, status and reason.
149  * @param[out] ioctl_str_len  The str_len of ioctl_str.
150  *
151  * @result
152  */
153 void whd_event_info_to_string(uint32_t cmd, uint16_t flag, uint32_t reason, char *ioctl_str, uint16_t ioctl_str_len);
154 
155 /*!
156  ******************************************************************************
157  * Prints Hexdump and ASCII dump for data passed as argument.
158  *
159  * @param[in] data  The data which has to be converted into hex and ascii format.
160  * @param[in] data_len The length of data.
161  *
162  * @result
163  */
164 void whd_hexdump(uint8_t *data, uint32_t data_len);
165 
166 extern wl_chanspec_t whd_channel_to_wl_band(whd_driver_t whd_driver, uint32_t channel);
167 
168 /*!
169  ******************************************************************************
170  * Convert an ipv4 string to a uint32_t.
171  *
172  * @param[in] ip4addr   : IP address in string format
173  * @param[in] len       : length of the ip address string
174  * @param[out] dest     : IP address format in uint32
175  *
176  * @return
177  */
178 bool whd_str_to_ip(const char *ip4addr, size_t len, void *dest);
179 
180 /*!
181  ******************************************************************************
182  * Print binary IPv4 address to a string.
183  * String must contain enough room for full address, 16 bytes exact.
184  * @param[in] ip4addr     : IPv4 address
185  * @param[out] p          : ipv4 address in string format
186  *
187  * @return
188  */
189 uint8_t whd_ip4_to_string(const void *ip4addr, char *p);
190 
191 
192 /*!
193  ******************************************************************************
194  * The wrapper function for memory allocation.
195  * It allocates the requested memory and returns a pointer to it.
196  * In default implementation it uses The C library function malloc().
197  *
198  * Use macro WHD_USE_CUSTOM_MALLOC_IMPL (-D) for custom whd_mem_malloc/
199  * whd_mem_calloc/whd_mem_free inplemetation.
200  *
201  * @param[in] size     :  This is the size of the memory block, in bytes.
202  *
203  * @return
204  *  This function returns a pointer to the allocated memory, or NULL if the
205  *  request fails.
206  */
207 void *whd_mem_malloc(size_t size);
208 
209 /*!
210  ******************************************************************************
211  * The wrapper function for memory allocation.
212  * It allocates the requested memory and sets allocated memory to zero.
213  * In default implementation it uses The C library function calloc().
214  *
215  * Use macro WHD_USE_CUSTOM_MALLOC_IMPL (-D) for custom whd_mem_malloc/
216  * whd_mem_calloc/whd_mem_free inplemetation.
217  *
218  * @param[in] nitems   :  This is the number of elements to be allocated.
219  * @param[in] size     :  This is the size of elements.
220  *
221  * @return
222  *  This function returns a pointer to the allocated memory, or NULL if the
223  *  request fails.
224  */
225 void *whd_mem_calloc(size_t nitems, size_t size);
226 
227 /*!
228  ******************************************************************************
229  * The wrapper function for free allocated memory.
230  * In default implementation it uses The C library function free().
231  *
232  * Use macro WHD_USE_CUSTOM_MALLOC_IMPL (-D) for custom whd_mem_malloc/
233  * whd_mem_calloc/whd_mem_free inplemetation.
234  *
235  * @param[in] ptr     :  pointer to a memory block previously allocated
236  *                       with whd_mem_malloc, whd_mem_calloc
237  * @return
238  */
239 void whd_mem_free(void *ptr);
240 
241 #ifdef __cplusplus
242 } /* extern "C" */
243 #endif
244 #endif
245 
246