1 /*
2  * Copyright 2022, 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 #define WHD_ARRAY_SIZE(a)                                 (sizeof(a) / sizeof(a[0]) )
43 
44 /** Searches for a specific WiFi Information Element in a byte array
45  *
46  * Traverse a string of 1-byte tag/1-byte length/variable-length value
47  * triples, returning a pointer to the substring whose first element
48  * matches tag
49  *
50  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
51  *
52  *
53  * @param tlv_buf : The byte array containing the Information Elements (IEs)
54  * @param buflen  : The length of the tlv_buf byte array
55  * @param key     : The Information Element tag to search for
56  *
57  * @return    NULL : if no matching Information Element was found
58  *            Non-Null : Pointer to the start of the matching Information Element
59  */
60 
61 whd_tlv8_header_t *whd_parse_tlvs(const whd_tlv8_header_t *tlv_buf, uint32_t buflen, dot11_ie_id_t key);
62 
63 /** Checks if a WiFi Information Element is a WPA entry
64  *
65  * Is this body of this tlvs entry a WPA entry? If
66  * not update the tlvs buffer pointer/length
67  *
68  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
69  *
70  * @param wpaie    : The byte array containing the Information Element (IE)
71  * @param tlvs     : The larger IE array to be updated if not a WPA IE
72  * @param tlvs_len : The current length of larger IE array
73  *
74  * @return    WHD_TRUE  : if IE matches the WPA OUI (Organizationally Unique Identifier) and its type = 1
75  *            WHD_FALSE : otherwise
76  */
77 whd_bool_t whd_is_wpa_ie(vendor_specific_ie_header_t *wpaie, whd_tlv8_header_t **tlvs, uint32_t *tlvs_len);
78 
79 /** Searches for a specific WiFi Information Element in a byte array
80  *
81  * Traverse a string of 1-byte tag/1-byte length/variable-length value
82  * triples, returning a pointer to the substring whose first element
83  * matches tag
84  *
85  * @note : This function has been copied directly from the standard Broadcom host driver file wl/exe/wlu.c
86  *
87  *
88  * @param tlv_buf : The byte array containing the Information Elements (IEs)
89  * @param buflen  : The length of the tlv_buf byte array
90  * @param key     : The Information Element tag to search for
91  *
92  * @return    NULL : if no matching Information Element was found
93  *            Non-Null : Pointer to the start of the matching Information Element
94  */
95 whd_tlv8_header_t *whd_parse_dot11_tlvs(const whd_tlv8_header_t *tlv_buf, uint32_t buflen, dot11_ie_id_t key);
96 
97 /******************************************************
98 *             Debug helper functionality
99 ******************************************************/
100 #ifdef WPRINT_ENABLE_WHD_DEBUG
101 const char *whd_event_to_string(whd_event_num_t var);
102 char *whd_ssid_to_string(uint8_t *value, uint8_t length, char *ssid_buf, uint8_t ssid_buf_len);
103 const char *whd_status_to_string(whd_event_status_t status);
104 const char *whd_reason_to_string(whd_event_reason_t reason);
105 char *whd_ether_ntoa(const uint8_t *ea, char *buf, uint8_t buf_len);
106 const char *whd_ioctl_to_string(uint32_t ioctl);
107 #endif /* ifdef WPRINT_ENABLE_WHD_DEBUG */
108 
109 /**
110  ******************************************************************************
111  * Prints partial details of a scan result on a single line
112  * @param[in] record :  A pointer to the whd_scan_result_t record
113  *
114  */
115 extern void whd_print_scan_result(whd_scan_result_t *record);
116 
117 /**
118  ******************************************************************************
119  * Convert a security bitmap to string
120  * @param[in] security :  security of type whd_security_t
121  * @param[in] out_str :  a character array to store output
122  * @param[in] out_str_len :  length of out_str char array
123  *
124  */
125 extern void whd_convert_security_type_to_string(whd_security_t security, char *out_str, uint16_t out_str_len);
126 
127 /*!
128  ******************************************************************************
129  * Convert an IOCTL to a string.
130  *
131  * @param[in] cmd  The ioct_log value.
132  * @param[out] ioctl_str The string value after conversion.
133  * @param[out] ioctl_str_len The string length of the IOCTL string.
134  *
135  * @result
136  */
137 void whd_ioctl_info_to_string(uint32_t cmd, char *ioctl_str, uint16_t ioctl_str_len);
138 
139 /*!
140  ******************************************************************************
141  * Convert event, status and reason value coming from firmware to string.
142  *
143  * @param[in] cmd  The event value in numeric form.
144  * @param[in] flag  The status value in numeric form.
145  * @param[in] reason  The reson value in numeric form.
146  * @param[out] ioctl_str  The string representation of event, status and reason.
147  * @param[out] ioctl_str_len  The str_len of ioctl_str.
148  *
149  * @result
150  */
151 void whd_event_info_to_string(uint32_t cmd, uint16_t flag, uint32_t reason, char *ioctl_str, uint16_t ioctl_str_len);
152 
153 /*!
154  ******************************************************************************
155  * Prints Hexdump and ASCII dump for data passed as argument.
156  *
157  * @param[in] data  The data which has to be converted into hex and ascii format.
158  * @param[in] data_len The length of data.
159  *
160  * @result
161  */
162 void whd_hexdump(uint8_t *data, uint32_t data_len);
163 
164 extern wl_chanspec_t whd_channel_to_wl_band(whd_driver_t whd_driver, uint32_t channel);
165 
166 /*!
167  ******************************************************************************
168  * Convert an ipv4 string to a uint32_t.
169  *
170  * @param[in] ip4addr   : IP address in string format
171  * @param[in] len       : length of the ip address string
172  * @param[out] dest     : IP address format in uint32
173  *
174  * @return
175  */
176 bool whd_str_to_ip(const char *ip4addr, size_t len, void *dest);
177 
178 /*!
179  ******************************************************************************
180  * Print binary IPv4 address to a string.
181  * String must contain enough room for full address, 16 bytes exact.
182  * @param[in] ip4addr     : IPv4 address
183  * @param[out] p          : ipv4 address in string format
184  *
185  * @return
186  */
187 uint8_t whd_ip4_to_string(const void *ip4addr, char *p);
188 
189 #ifdef __cplusplus
190 } /* extern "C" */
191 #endif
192 #endif
193