carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / carlu / src / test.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * Various tests
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include "libusb.h"
34 #include "SDL.h"
35
36 #include "carlu.h"
37 #include "debug.h"
38 #include "frame.h"
39 #include "usb.h"
40
41 void debug_test(void)
42 {
43         err("This is an error.\n");
44         warn("This is a warnig.\n");
45         info("This is an informative message.\n");
46         dbg("This is just utter useless babble.\n");
47 }
48
49 void carlu_frame_test(struct carlu *ar)
50 {
51         struct frame *frame;
52
53         frame = carlu_alloc_frame(ar, 0x40);
54         frame_reserve(frame, 0x10);
55
56         memset(frame_put(frame, 0x10), 0x11, 0x10);
57         memset(frame_put(frame, 0x10), 0x22, 0x10);
58         memset(frame_push(frame, 0x10), 0x33, 0x10);
59         memset(frame_put(frame, 0x10), 0x44, 0x10);
60
61         print_hex_dump_bytes(INFO, "DATA:", frame->data, frame->len);
62
63         print_hex_dump_bytes(INFO, "PAYLOAD:", frame->payload, frame->alloced);
64
65         frame_free(frame);
66 }
67
68 static void carlu_loopback_tx_cb(struct carlu *ar __unused,
69                                     struct frame *frame __unused)
70 {
71 }
72
73 static int carlu_loopback_cmd(struct carlu *ar __unused,
74                               struct carl9170_rsp *cmd, void *buf __unused,
75                               unsigned int len __unused)
76 {
77         unsigned int i, n;
78
79         switch (cmd->hdr.cmd) {
80         case CARL9170_RSP_TXCOMP:
81                 n = cmd->hdr.ext;
82                 dbg("received tx feedback (%d).\n", n);
83
84                 for (i = 0; i < n; i++) {
85                         dbg("cookie:%x success:%d rix:%d tries:%d queue:%d\n",
86                                 cmd->tx_status[i].cookie,
87                                 cmd->tx_status[i].success,
88                                 cmd->tx_status[i].rix,
89                                 cmd->tx_status[i].tries,
90                                 cmd->tx_status[i].queue);
91                 }
92                 return -1;
93
94         default:
95                 return -1;
96         }
97 }
98
99 static void carlu_loopback_rx(struct carlu *ar,
100                                 void *buf __unused, unsigned int len)
101 {
102         ar->rxed++;
103         ar->rx_octets += len;
104 }
105
106 static void carlu_loopback_mark_tx_frames(struct frame *frame)
107 {
108         unsigned int i;
109
110         for (i = 0; i < frame->len; i++)
111                 frame->data[i] = (uint8_t) i;
112 }
113
114 void carlu_loopback_test(struct carlu *ar, const unsigned int total_runs,
115                           const unsigned int interval, const unsigned int min_len, const unsigned int max_len)
116 {
117         struct frame *frame;
118         uint32_t start_time, total_time = 0;
119         float moctets, dtime;
120         unsigned int runs = 0, i = 0, j = 0, len;
121         int ret;
122
123         if (min_len > max_len) {
124                 err("stresstest: invalid parameters => min_len:%d > max_len:%d",
125                     min_len, max_len);
126                 return;
127         }
128
129         if (min_len < 4) {
130                 err("stresstest: invalid parameters => min_len is smaller than 4");
131                 return;
132         }
133
134         len = min_len;
135         frame = carlu_alloc_frame(ar, len);
136         frame_put(frame, len);
137
138         carlu_loopback_mark_tx_frames(frame);
139
140         ar->rx_cb = carlu_loopback_rx;
141         ar->cmd_cb = carlu_loopback_cmd;
142         ar->tx_cb = carlu_loopback_tx_cb;
143
144         start_time = SDL_GetTicks();
145         while (runs <= total_runs) {
146                 if (frame && carlu_tx(ar, frame) == 0) {
147                         len = min_len;
148                         i++;
149                 } else {
150                         frame_free(frame);
151                 }
152
153                 frame = NULL;
154
155                 frame = carlu_alloc_frame(ar, len);
156                 frame_put(frame, len);
157
158                 carlu_loopback_mark_tx_frames(frame);
159                 j++;
160
161                 total_time = SDL_GetTicks() - start_time;
162
163                 if (total_time >= interval) {
164                         moctets = ((float)ar->tx_octets) / (1024.0f * 1024.0f);
165                         dtime = ((float)total_time) / 1000;
166                         info("%d: tx %d of %d => %.2f MiB in %.2f secs => %.4f MBits/s\n",
167                                 runs, i, j, moctets, dtime, (moctets * 8.0f) / dtime);
168
169                         moctets = ((float)ar->rx_octets) / (1024.0f * 1024.0f);
170                         info("%d: rx %d of %d => %.2f MiB in %.2f secs => %.4f MBits/s\n",
171                                 runs, ar->rxed, i, moctets, dtime, (moctets * 8.0f) / dtime);
172
173                         if ((ar->rxed == 0 && i) || !i) {
174                                 ret = carlu_cmd_echo(ar, 0xdeadbeef);
175                                 if (ret)
176                                         warn("firmware crashed... echo_cmd: (%d)\n", ret);
177                         }
178
179                         total_time = 0;
180                         i = 0;
181                         j = 0;
182                         ar->rxed = 0;
183                         ar->txed = 0;
184                         ar->rx_octets = 0;
185                         ar->tx_octets = 0;
186                         runs++;
187                         start_time = SDL_GetTicks();
188                 }
189         }
190
191         ar->rx_cb = NULL;
192         ar->cmd_cb = NULL;
193         ar->tx_cb = NULL;
194 }