-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.c
387 lines (345 loc) · 8.24 KB
/
stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* Copyright (c) 2016-2019 Intel Corporation
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "dwpal.h"
#include "dwpal_ext.h"
#include "stats.h"
#include "dwpal_log.h" //Logging
#if defined YOCTO
#include <slibc/string.h>
#else
#include "safe_str_lib.h"
#endif
/* global declaration */
void *goutData = NULL; // for traversing NL data
int gSpace = 0; // for indentation
int gEnum = 0; // to find enum stat
#define MAX_STR_SIZE 32
void convertMac(char *sMac, unsigned char *cMac)
{
int i;
for(i = 0; i < 6; i++) {
cMac[i] = (unsigned char) strtoul(sMac, &sMac, 16);
sMac++;
}
}
static unsigned int stat_ipow(unsigned int base, unsigned int power)
{
unsigned int i;
unsigned int res = 1;
for(i = 0; i < power; i++)
{
res *= base;
}
return res;
}
void print_type(type t,char *description,int len)
{
int i, tmp = gSpace;
if( t != BITFIELD && t != ENUM )
INDENTATION(tmp)
switch (t)
{
case BYTE:
{
printf("%20u : %s\n", *((unsigned char *)goutData) , description);
goutData += sizeof(unsigned char);
}
break;
case SLONG:
{
printf("%20i : %s\n", *((signed int *) goutData), description);
goutData += sizeof(signed int);
}
break;
case LONG:
{
printf("%20u : %s\n", *((unsigned int *) goutData), description);
goutData += sizeof(unsigned int);
}
break;
case SBYTEARRAY:
{
printf("%20s : %s\n","",description);
for(i = 0; i < len; i++)
{
tmp = gSpace;
INDENTATION(tmp)
printf("%20i : [%d]\n",*((signed char *) goutData),i);
goutData += sizeof(signed char);
}
}
break;
case SLONGARRAY:
{
printf("%20s : %s\n","",description);
for(i = 0; i < len; i++)
{
tmp = gSpace;
INDENTATION(tmp)
printf("%20i : [%d]\n",*((signed int *) goutData),i);
goutData += sizeof(signed int);
}
}
break;
case BITFIELD:
{
tmp = gSpace;
if( len )
goutData -= sizeof(unsigned int);
if( ( *((unsigned int *) goutData) & (0x01<<len) )) {
INDENTATION(tmp);
printf("%20s : \n",description);
}
}
break;
case FLAG:
{
printf("%20s : %s\n", *((unsigned int *) goutData)? "True" : "False",description);
goutData += sizeof(unsigned int);
}
break;
case ENUM:
{
if( len )
goutData -= sizeof(unsigned int);
if(*((unsigned int *) goutData) == len){
tmp = gSpace;
INDENTATION(tmp)
gEnum = 1;
printf("%20s : ", description);
}
}
break;
case TIMESTAMP:
{
tmp = gSpace;
INDENTATION(tmp)
printf("%11d msec ago : %s\n",*((unsigned int *)goutData),description);
goutData += sizeof(unsigned int);
}
break;
case LONGFRACT:
{
tmp = gSpace;
INDENTATION(tmp)
if (len != 0)
{
unsigned int base = stat_ipow(10, len);
unsigned int re_value = *(( unsigned int *)goutData) / base;
unsigned int fract_value = *(( unsigned int *)goutData) % base;
printf("%*u.%0*u : %s\n", 20 - len - 1, re_value, len, fract_value, description);
}
else
printf("%20u : %s\n", *(( unsigned int *)goutData), description);
goutData += sizeof(unsigned int);
}
break;
case SLONGFRACT:
{
tmp = gSpace;
INDENTATION(tmp)
if (len != 0)
{
unsigned int base = stat_ipow(10, len);
int re_value = *(( signed int *)goutData) / base;
int fract_value = *(( signed int *)goutData) % base;
printf("%*i.%0*i : %s\n", 20 - len - 1, re_value, len, fract_value, description);
}
else
printf("%20i : %s\n", *(( signed int *)goutData), description);
goutData += sizeof(signed int);
}
break;
case HUGE:
{
tmp = gSpace;
INDENTATION(tmp)
printf("%20lli : %s\n", *((signed long long *)goutData), description);
goutData += sizeof(signed long long);
}
break;
}
}
static int NlEventCallback(char* ifname, int event, int subevent, size_t len, unsigned char *data)
{
int i;
for(i=0;i<len;i++)
printf("%x ",data[i]);
return 0;
}
void help_print(stat_id c,bool original)
{
int i,tmp=0;
for(i = 0; i < gStat[c].size; i++)
{
if( !i && original )
gSpace++;
if( gStat[c].sts[i].t != NONE )
{
print_type(gStat[c].sts[i].t, (char *)gStat[c].sts[i].description, gStat[c].sts[i].element);
if(gStat[c].sts[i].t == BITFIELD || gStat[c].sts[i].t == ENUM)
goutData += sizeof(unsigned int);
}
else
{
tmp = gSpace;
if( gStat[c].sts[i].c == NETWORK_BITFIELD ) {
INDENTATION(tmp)
printf("%20s : %s\n","",gStat[c].sts[i].description);
}
else {
if ( ( gStat[c].sts[i].c != PHY_ENUM ) && ( gStat[c].sts[i].c != VENDOR_ENUM ) ) {
INDENTATION(tmp)
PRINT_DESCRIPTION(gStat[c].sts[i])
}
}
}
if( gStat[c].sts[i].c != c ) {
if( gStat[c].sts[i].c != NETWORK_BITFIELD )
gSpace++;
help_print(gStat[c].sts[i].c, false );
}
}
if ( !original )
gSpace--;
i--;
if( ( gStat[c].sts[i].t == ENUM ) ) {
char ptr[64]= {'\0'};
switch ( gStat[c].sts[i].c )
{
case VENDOR_ENUM:
snprintf(ptr,sizeof("Vendor"),"Vendor");
break;
case PHY_ENUM:
snprintf(ptr,sizeof("Network (Phy) Mode"),"Network (Phy) Mode");
break;
default:
;
}
if( !gEnum )
{
int tmp = gSpace;
INDENTATION(tmp);
printf("%20s : %s\n","Unknown value",ptr);
}
else {
printf("%s\n",ptr);
gEnum = 0;
}
}
}
void dump_sta_list(char *outData,unsigned int outLen)
{
unsigned int sta_number = ( outLen - NL_ATTR_HDR )/sizeof(peer_list_t);
peer_list_t *sta = (peer_list_t *)&outData[NL_ATTR_HDR];
if (sta_number > 0)
{
unsigned int i;
fprintf(stdout, "\n\n%u peer(s) connected:\n\n", sta_number);
for(i = 0; i < sta_number; i++)
{
if(sta->is_sta_auth)
{
fprintf(stdout, "\t" MAC_PRINTF_FMT "\n", MAC_PRINTF_ARG(&sta->addr));
}
else
{
fprintf(stdout, "\t" MAC_PRINTF_FMT " (not authorized)\n", MAC_PRINTF_ARG(&sta->addr));
}
sta++;
}
}
else
{
fprintf(stdout, "\n\nNo peers connected.\n\n");
}
fprintf(stdout, "\n");
}
void print_cmd_help( char *cmd )
{
int i, found = 0;
if( !cmd )
printf("\n\t Help for supported statistics are:");
for( i=0; i < sizeof(gCmd)/sizeof(gCmd[0]); i++ )
{
if( cmd )
{
if ( !strncmp(cmd,gCmd[i].cmd,strnlen_s(gCmd[i].cmd,MAX_STR_SIZE)) )
{
printf("\n\t Help for %s statistics is:",cmd);
printf("\n\t %s",gCmd[i].usage);
found = 1;
}
}
else
printf("\n\t %s",gCmd[i].usage);
}
if( !cmd || found )
{
printf("\n\t Note:\n\t\t INTERFACENAME can be wlan0,wlan0.0,wlan2,wlan2.0...");
printf("\n\t\t MACADDR corresponds to macaddr of connected station\n");
}
}
int check_stats_cmd(int num_arg, char *cmd[])
{
char outData[MAX_NL_REPLY] = { '\0' };
unsigned int outLen,i;
char Vendordata[6] = {'\0'};
int VendorDataLen = 0;
if( !strncmp(cmd[0],"help",sizeof("help")-1) ) {
if( num_arg > 1 )
print_cmd_help(cmd[1]);
else
print_cmd_help(NULL);
return 0;
}
if( num_arg < 2) {
PRINT_ERROR(" Need atleaset 2 arguments ie interfacename and command\n");
return -1;
}
for( i = 0; i < sizeof(gCmd)/sizeof(gCmd[0]); i++ )
{
if( !strncmp(cmd[1], gCmd[i].cmd, strnlen_s(gCmd[i].cmd,MAX_STR_SIZE)) ) {
if( num_arg -1 != gCmd[i].num_arg ) {
printf("%s\n",(char *)&gCmd[i].usage);
return -1;
}
if( num_arg > 2 ) {
convertMac(cmd[2],Vendordata);
VendorDataLen = sizeof(Vendordata);
PRINT_DEBUG("Vendordata %s\n",Vendordata);
}
if (dwpal_ext_driver_nl_attach(NlEventCallback) == DWPAL_FAILURE)
{
PRINT_ERROR("%s; dwpal_driver_nl_attach returned ERROR ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
if (dwpal_ext_driver_nl_get(cmd[0], NL80211_CMD_VENDOR, DWPAL_NETDEV_ID, gCmd[i].id ,\
Vendordata, VendorDataLen, &outLen, outData) != DWPAL_SUCCESS)
{
PRINT_ERROR("%s; dwpal_ext_driver_nl_get returned ERROR ==> Abort!\n", __FUNCTION__);
return -1;
}
if( outLen )
goutData = &outData[NL_ATTR_HDR];
else
return -1;
if ( gCmd[i].c != PEER_LIST )
help_print( gCmd[i].c, true );
else
dump_sta_list(outData,outLen);
break;
}
}
goutData = NULL;
return 0;
}