variuos old test code
[pandora-misc.git] / tests / wlstat.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <pthread.h>
4 #include <time.h>
5
6 #define ts_add_nsec(ts, ns) { \
7   ts.tv_nsec += ns; \
8   if (ts.tv_nsec >= 1000000000) { \
9     ts.tv_sec++; \
10     ts.tv_nsec -= 1000000000; \
11   } \
12 }
13
14 static FILE *open_wl_stats(void)
15 {
16         char buf[256];
17         FILE *f;
18         int i;
19
20         for (i = 0; i < 100; i++) {
21                 snprintf(buf, sizeof(buf),
22                          "/sys/class/ieee80211/phy%d/device/wl_stats", i);
23                 f = fopen(buf, "r");
24                 if (f != NULL)
25                         break;
26         }
27
28         if (f == NULL)
29                 perror("open wl_stats");
30
31         return f;
32 }
33
34 #define VALS 8
35 static const char *names[VALS] = {
36         "fcs_err", "plcp_er", "seq_err", "  valid",
37         "  retry", "  touts", "oth_err", "   reqs"
38 };
39 static const unsigned char need_diff[VALS] = { 0, 0, 1, 0, 1, 1, 1, 1 };
40
41 static int get_vals(FILE *f, unsigned int *vals)
42 {
43         return fscanf(f, "%u %u %u %u %u %u %u %u\n",
44                 &vals[0], &vals[1], &vals[2], &vals[3],
45                 &vals[4], &vals[5], &vals[6], &vals[7]);
46 }
47
48 int main(int argc, char *argv[])
49 {
50         pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
51         pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
52         unsigned int vals[VALS], vals_old[VALS];
53         struct timespec ts;
54         int l, i, ret;
55         FILE *f;
56
57         f = open_wl_stats();
58         if (f == NULL)
59                 return 1;
60
61         // initial read
62         get_vals(f, vals_old);
63
64         clock_gettime(CLOCK_REALTIME, &ts);
65
66         for (l = 0; ; l++)
67         {
68                 rewind(f);
69                 ret = get_vals(f, vals);
70                 if (ret != VALS) {
71                         fprintf(stderr, "scan error (%d)\n", ret);
72                         return 1;
73                 }
74
75                 if (l % 25 == 0) {
76                         // printf("-------------  rx  ------------- ------------  tx  -------------\n");
77                         for (i = 0; i < VALS; i++)
78                                 printf("%s ", names[i]);
79                         printf("\n");
80                 }
81
82                 for (i = 0; i < VALS; i++)
83                         printf("%7u ", need_diff[i] ? (vals[i] - vals_old[i]) : vals[i]);
84                 printf("\n");
85                 fflush(stdout);
86
87                 memcpy(vals_old, vals, sizeof(vals_old));
88
89                 ts_add_nsec(ts, 1000000000);
90                 pthread_mutex_lock(&dummy_mutex);
91                 pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts);
92                 pthread_mutex_unlock(&dummy_mutex);
93         }
94
95         fclose(f);
96         return 0;
97 }
98