carlu: move from private carl9170_tx_status to public _carl9170_tx_status
[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 #include "cmd.h"
41
42 void debug_test(void)
43 {
44         err("This is an error.\n");
45         warn("This is a warnig.\n");
46         info("This is an informative message.\n");
47         dbg("This is just utter useless babble.\n");
48 }
49
50 void carlu_frame_test(struct carlu *ar)
51 {
52         struct frame *frame;
53
54         frame = carlu_alloc_frame(ar, 0x40);
55         frame_reserve(frame, 0x10);
56
57         memset(frame_put(frame, 0x10), 0x11, 0x10);
58         memset(frame_put(frame, 0x10), 0x22, 0x10);
59         memset(frame_push(frame, 0x10), 0x33, 0x10);
60         memset(frame_put(frame, 0x10), 0x44, 0x10);
61
62         print_hex_dump_bytes(INFO, "DATA:", frame->data, frame->len);
63
64         print_hex_dump_bytes(INFO, "PAYLOAD:", frame->payload, frame->alloced);
65
66         frame_free(frame);
67 }
68
69 static void carlu_loopback_tx_cb(struct carlu *ar __unused,
70                                     struct frame *frame __unused)
71 {
72 }
73
74 static int carlu_loopback_cmd(struct carlu *ar __unused,
75                               struct carl9170_rsp *cmd, void *buf __unused,
76                               unsigned int len __unused)
77 {
78         unsigned int i, n;
79
80         switch (cmd->hdr.cmd) {
81         case CARL9170_RSP_TXCOMP:
82                 n = cmd->hdr.ext;
83                 dbg("received tx feedback (%d).\n", n);
84
85                 for (i = 0; i < n; i++) {
86                         dbg("cookie:%x info:%x\n",
87                                 cmd->_tx_status[i].cookie,
88                                 cmd->_tx_status[i].info);
89                 }
90                 return -1;
91
92         default:
93                 return -1;
94         }
95 }
96
97 static void carlu_loopback_rx(struct carlu *ar,
98                                 void *buf __unused, unsigned int len)
99 {
100         ar->rxed++;
101         ar->rx_octets += len;
102 }
103
104 static void carlu_loopback_mark_tx_frames(struct frame *frame)
105 {
106         unsigned int i;
107
108         for (i = 0; i < frame->len; i++)
109                 frame->data[i] = (uint8_t) i;
110 }
111
112 void carlu_loopback_test(struct carlu *ar, const unsigned int total_runs,
113                           const unsigned int interval, const unsigned int min_len, const unsigned int max_len)
114 {
115         struct frame *frame;
116         uint32_t start_time, total_time = 0;
117         float moctets, dtime;
118         unsigned int runs = 0, i = 0, j = 0, len;
119         int ret;
120
121         if (min_len > max_len) {
122                 err("stresstest: invalid parameters => min_len:%d > max_len:%d",
123                     min_len, max_len);
124                 return;
125         }
126
127         if (min_len < 4) {
128                 err("stresstest: invalid parameters => min_len is smaller than 4");
129                 return;
130         }
131
132         len = min_len;
133         frame = carlu_alloc_frame(ar, len);
134         frame_put(frame, len);
135
136         carlu_loopback_mark_tx_frames(frame);
137
138         ar->rx_cb = carlu_loopback_rx;
139         ar->cmd_cb = carlu_loopback_cmd;
140         ar->tx_cb = carlu_loopback_tx_cb;
141
142         start_time = SDL_GetTicks();
143         while (runs <= total_runs) {
144                 if (frame && carlu_tx(ar, frame) == 0) {
145                         len = min_len;
146                         i++;
147                 } else {
148                         frame_free(frame);
149                 }
150
151                 frame = NULL;
152
153                 frame = carlu_alloc_frame(ar, len);
154                 frame_put(frame, len);
155
156                 carlu_loopback_mark_tx_frames(frame);
157                 j++;
158
159                 total_time = SDL_GetTicks() - start_time;
160
161                 if (total_time >= interval) {
162                         moctets = ((float)ar->tx_octets) / (1024.0f * 1024.0f);
163                         dtime = ((float)total_time) / 1000;
164                         info("%d: tx %d of %d => %.2f MiB in %.2f secs => %.4f MBits/s\n",
165                                 runs, i, j, moctets, dtime, (moctets * 8.0f) / dtime);
166
167                         moctets = ((float)ar->rx_octets) / (1024.0f * 1024.0f);
168                         info("%d: rx %d of %d => %.2f MiB in %.2f secs => %.4f MBits/s\n",
169                                 runs, ar->rxed, i, moctets, dtime, (moctets * 8.0f) / dtime);
170
171                         if ((ar->rxed == 0 && i) || !i) {
172                                 ret = carlu_cmd_echo(ar, 0xdeadbeef);
173                                 if (ret)
174                                         warn("firmware crashed... echo_cmd: (%d)\n", ret);
175                         }
176
177                         total_time = 0;
178                         i = 0;
179                         j = 0;
180                         ar->rxed = 0;
181                         ar->txed = 0;
182                         ar->rx_octets = 0;
183                         ar->tx_octets = 0;
184                         runs++;
185                         start_time = SDL_GetTicks();
186                 }
187         }
188
189         ar->rx_cb = NULL;
190         ar->cmd_cb = NULL;
191         ar->tx_cb = NULL;
192 }
193
194 int carlu_gpio_test(struct carlu *ar)
195 {
196         uint32_t gpio;
197
198 #define CHK(cmd)                                \
199         do {                                    \
200                 int __err;                      \
201                 if ((__err = cmd))              \
202                         return __err;           \
203         } while (0)
204
205         CHK(carlu_cmd_read_mem(ar, AR9170_GPIO_REG_PORT_DATA, &gpio));
206         info("GPIO state:%x\n", gpio);
207
208         /* turn both LEDs on */
209         CHK(carlu_cmd_write_mem(ar, AR9170_GPIO_REG_PORT_DATA,
210             AR9170_GPIO_PORT_LED_0 | AR9170_GPIO_PORT_LED_1));
211
212         SDL_Delay(700);
213
214         CHK(carlu_cmd_read_mem(ar, AR9170_GPIO_REG_PORT_DATA, &gpio));
215         info("GPIO state:%x\n", gpio);
216
217         /* turn LEDs off everything */
218         CHK(carlu_cmd_write_mem(ar, AR9170_GPIO_REG_PORT_DATA, 0));
219
220         CHK(carlu_cmd_read_mem(ar, AR9170_GPIO_REG_PORT_DATA, &gpio));
221         info("GPIO state:%x\n", gpio);
222 }