variuos old test code
[pandora-misc.git] / tests / wlstat.c
diff --git a/tests/wlstat.c b/tests/wlstat.c
new file mode 100644 (file)
index 0000000..3b29ee3
--- /dev/null
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+#include <time.h>
+
+#define ts_add_nsec(ts, ns) { \
+  ts.tv_nsec += ns; \
+  if (ts.tv_nsec >= 1000000000) { \
+    ts.tv_sec++; \
+    ts.tv_nsec -= 1000000000; \
+  } \
+}
+
+static FILE *open_wl_stats(void)
+{
+       char buf[256];
+       FILE *f;
+       int i;
+
+       for (i = 0; i < 100; i++) {
+               snprintf(buf, sizeof(buf),
+                        "/sys/class/ieee80211/phy%d/device/wl_stats", i);
+               f = fopen(buf, "r");
+               if (f != NULL)
+                       break;
+       }
+
+       if (f == NULL)
+               perror("open wl_stats");
+
+       return f;
+}
+
+#define VALS 8
+static const char *names[VALS] = {
+       "fcs_err", "plcp_er", "seq_err", "  valid",
+       "  retry", "  touts", "oth_err", "   reqs"
+};
+static const unsigned char need_diff[VALS] = { 0, 0, 1, 0, 1, 1, 1, 1 };
+
+static int get_vals(FILE *f, unsigned int *vals)
+{
+       return fscanf(f, "%u %u %u %u %u %u %u %u\n",
+               &vals[0], &vals[1], &vals[2], &vals[3],
+               &vals[4], &vals[5], &vals[6], &vals[7]);
+}
+
+int main(int argc, char *argv[])
+{
+       pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
+       pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
+       unsigned int vals[VALS], vals_old[VALS];
+       struct timespec ts;
+       int l, i, ret;
+       FILE *f;
+
+       f = open_wl_stats();
+       if (f == NULL)
+               return 1;
+
+       // initial read
+       get_vals(f, vals_old);
+
+       clock_gettime(CLOCK_REALTIME, &ts);
+
+       for (l = 0; ; l++)
+       {
+               rewind(f);
+               ret = get_vals(f, vals);
+               if (ret != VALS) {
+                       fprintf(stderr, "scan error (%d)\n", ret);
+                       return 1;
+               }
+
+               if (l % 25 == 0) {
+                       // printf("-------------  rx  ------------- ------------  tx  -------------\n");
+                       for (i = 0; i < VALS; i++)
+                               printf("%s ", names[i]);
+                       printf("\n");
+               }
+
+               for (i = 0; i < VALS; i++)
+                       printf("%7u ", need_diff[i] ? (vals[i] - vals_old[i]) : vals[i]);
+               printf("\n");
+               fflush(stdout);
+
+               memcpy(vals_old, vals, sizeof(vals_old));
+
+               ts_add_nsec(ts, 1000000000);
+               pthread_mutex_lock(&dummy_mutex);
+               pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts);
+               pthread_mutex_unlock(&dummy_mutex);
+       }
+
+       fclose(f);
+       return 0;
+}
+