1 /** @file  wifi_common.h
2  *
3  *  @brief Header file for wifi common functions
4  *
5  *  Copyright 2008-2024 NXP
6  *
7  *  SPDX-License-Identifier: BSD-3-Clause
8  *
9  */
10 #ifndef _WIFI_COMMON_H
11 #define _WIFI_COMMON_H
12 #include <wifi.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 
17 #define wfd_e(...) wmlog_e("p2p", ##__VA_ARGS__)
18 #define wfd_w(...) wmlog_w("p2p", ##__VA_ARGS__)
19 
20 #if CONFIG_P2P_DEBUG
21 #define wfd_d(...) wmlog("p2p", ##__VA_ARGS__)
22 /**
23  *  @brief Dump hex data
24  *
25  *  @param p        A pointer to data buffer
26  *  @param len      The len of data buffer
27  *  @param delim    Deliminator character
28  *  @return         Hex integer
29  */
hexdump(void * p,t_s32 len,t_s8 delim)30 static void hexdump(void *p, t_s32 len, t_s8 delim)
31 {
32     t_s32 i;
33     t_u8 *s = p;
34     wmprintf("HexDump: len=%d\r\n", (int)len);
35     for (i = 0; i < len; i++)
36     {
37         if (i != len - 1)
38             wmprintf("%02x%c", *s++, delim);
39         else
40             wmprintf("%02x\r\n", *s);
41         if ((i + 1) % 16 == 0)
42             wmprintf("\r\n");
43     }
44 }
45 
46 #else
47 #define wfd_d(...)
48 #endif /* ! CONFIG_P2P_DEBUG */
49 
50 /** Success */
51 #define SUCCESS 1
52 /** Failure */
53 #define FAILURE 0
54 /** MAC BROADCAST */
55 #define WIFIDIRECT_RET_MAC_BROADCAST 0x1FF
56 /** MAC MULTICAST */
57 #define WIFIDIRECT_RET_MAC_MULTICAST 0x1FE
58 
59 /* fixme: remove this after mlan integration complete
60  */
61 /* Definition of firmware host command */
62 /** HostCmd_DS_GEN */
63 typedef PACK_START struct
64 {
65     /** Command */
66     t_u16 command;
67     /** Size */
68     t_u16 size;
69     /** Sequence number */
70     t_u16 seq_num;
71     /** Result */
72     t_u16 result;
73 } PACK_END HostCmd_DS_GEN;
74 
75 /**
76  *  @brief  Detects duplicates channel in array of strings
77  *
78  *  @param  argc    Number of elements
79  *  @param  argv    Array of strings
80  *  @return UAP_FAILURE or UAP_SUCCESS
81  */
has_dup_channel(int argc,char * argv[])82 static inline int has_dup_channel(int argc, char *argv[])
83 {
84     int i, j;
85     /* Check for duplicate */
86     for (i = 0; i < (argc - 1); i++)
87     {
88         for (j = i + 1; j < argc; j++)
89         {
90             if (atoi(argv[i]) == atoi(argv[j]))
91                 return FAILURE;
92         }
93     }
94     return SUCCESS;
95 }
96 
97 /**
98  *  @brief Converts colon separated MAC address to hex value
99  *
100  *  @param mac      A pointer to the colon separated MAC string
101  *  @param raw      A pointer to the hex data buffer
102  *  @return         SUCCESS or FAILURE
103  *                  WIFIDIRECT_RET_MAC_BROADCAST  - if broadcast mac
104  *                  WIFIDIRECT_RET_MAC_MULTICAST - if multicast mac
105  */
mac2raw(char * mac,t_u8 * raw)106 static int mac2raw(char *mac, t_u8 *raw)
107 {
108     unsigned int temp_raw[ETH_ALEN];
109     int num_tokens = 0;
110     int i;
111     if (strlen(mac) != ((2 * ETH_ALEN) + (ETH_ALEN - 1)))
112         return FAILURE;
113     num_tokens = sscanf(mac, "%2x:%2x:%2x:%2x:%2x:%2x", temp_raw + 0, temp_raw + 1, temp_raw + 2, temp_raw + 3,
114                         temp_raw + 4, temp_raw + 5);
115     if (num_tokens != ETH_ALEN)
116         return FAILURE;
117 
118     for (i = 0; i < num_tokens; i++)
119         raw[i] = (t_u8)temp_raw[i];
120 
121     if (memcmp(raw, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
122         return WIFIDIRECT_RET_MAC_BROADCAST;
123     else if (raw[0] & 0x01)
124         return WIFIDIRECT_RET_MAC_MULTICAST;
125     return SUCCESS;
126 }
127 
128 /**
129  *  @brief Parses a command line
130  *
131  *  @param line     The line to parse
132  *  @param args     Pointer to the argument buffer to be filled in
133  *  @return         Number of arguments in the line or EOF
134  */
parse_line(char * line,char * args[])135 static int parse_line(char *line, char *args[])
136 {
137     int arg_num  = 0;
138     int is_start = 0;
139     int is_quote = 0;
140     int length   = 0;
141     int i        = 0;
142 
143     arg_num = 0;
144     length  = strlen(line);
145     /* Process line */
146 
147     /* Find number of arguments */
148     is_start = 0;
149     is_quote = 0;
150     for (i = 0; i < length; i++)
151     {
152         /* Ignore leading spaces */
153         if (is_start == 0)
154         {
155             if (line[i] == ' ')
156             {
157                 continue;
158             }
159             else if (line[i] == '\t')
160             {
161                 continue;
162             }
163             else if (line[i] == '\n')
164             {
165                 break;
166             }
167             else
168             {
169                 is_start      = 1;
170                 args[arg_num] = &line[i];
171                 arg_num++;
172             }
173         }
174         if (is_start == 1)
175         {
176             /* Ignore comments */
177             if (line[i] == '#')
178             {
179                 if (is_quote == 0)
180                 {
181                     line[i] = '\0';
182                     arg_num--;
183                 }
184                 break;
185             }
186             /* Separate by '=' */
187             if (line[i] == '=')
188             {
189                 line[i]  = '\0';
190                 is_start = 0;
191                 continue;
192             }
193             /* Separate by ',' */
194             if (line[i] == ',')
195             {
196                 line[i]  = '\0';
197                 is_start = 0;
198                 continue;
199             }
200             /* Change ',' to ' ', but not inside quotes */
201             if ((line[i] == ',') && (is_quote == 0))
202             {
203                 line[i] = ' ';
204                 continue;
205             }
206         }
207         /* Remove newlines */
208         if (line[i] == '\n')
209             line[i] = '\0';
210         /* Check for quotes */
211         if (line[i] == '"')
212         {
213             is_quote = (is_quote == 1) ? 0 : 1;
214             continue;
215         }
216         if (((line[i] == ' ') || (line[i] == '\t')) && (is_quote == 0))
217         {
218             line[i]  = '\0';
219             is_start = 0;
220             continue;
221         }
222     }
223     return arg_num;
224 }
225 
226 #endif /* _WIFI_COMMON_H */
227