aboutsummaryrefslogtreecommitdiffstats
path: root/examples/drivertest/commandline/runtest.c
blob: 3d04421f4171645244f03fa3285e4c1237675f1e (plain)
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
/* Name: runtest.c
 * Author: Christian Starkjohann
 * Creation Date: 2008-04-10
 * Tabsize: 4
 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
 */

/*
General Description:
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>        /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */

#include "../firmware/requests.h"   /* custom request numbers */
#include "../firmware/usbconfig.h"  /* device's VID/PID and names */

#define uchar   unsigned char

static void hexdump(char *_buffer, int len, FILE *fp)
{
int i;
uchar *buffer = (uchar *)_buffer;

    for(i = 0; i < len; i++){
        if(i != 0){
            if(i % 16 == 0){
                fprintf(fp, "\n");
            }else{
                fprintf(fp, " ");
            }
        }
        fprintf(fp, "%02x", buffer[i]);
    }
    if(i != 0)
        fprintf(fp, "\n");
}

static void fillBuffer(char *buffer, int len)
{
static int  type = 0;

    if(type == 0){          /* all 0 */
        bzero(buffer, len);
    }else if(type == 1){    /* all 0xff */
        memset(buffer, 0xff, len);
    }else{                  /* random */
        int i;
        for(i = 0; i < len; i++){
            buffer[i] = random() & 0xff;
        }
    }
    if(++type >= 1000)
        type = 0;
}

static int compareBuffers(char *txBuffer, char *rxBuffer, int len)
{
int i, rval = 0;

    for(i = 0; i < len; i++){
        if(rxBuffer[i] != txBuffer[i]){
            fprintf(stderr, "compare error at index %d: byte is 0x%x instead of 0x%x\n", i, rxBuffer[i], txBuffer[i]);
            rval = 1;
        }
    }
    if(rval){
        fprintf(stderr, "txBuffer was:\n");
        hexdump(txBuffer, len, stderr);
        fprintf(stderr, "rxBuffer is:\n");
        hexdump(rxBuffer, len, stderr);
    }
    return rval;
}

int main(int argc, char **argv)
{
usb_dev_handle  *handle = NULL;
const uchar     rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char            vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
char            txBuffer[64], rxBuffer[64];
int             cnt, vid, pid, i, j;

    usb_init();
    /* compute VID/PID from usbconfig.h so that there is a central source of information */
    vid = rawVid[1] * 256 + rawVid[0];
    pid = rawPid[1] * 256 + rawPid[0];
    /* The following function is in opendevice.c: */
    if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
        fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
        exit(1);
    }
    if(argc > 1 && strcasecmp(argv[1], "osccal") == 0){
        if(argc > 2){   /* set osccal */
            int osccal = atoi(argv[2]);
            printf("setting osccal to %d\n", osccal);
            cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_SET_OSCCAL, osccal, 0, txBuffer, 0, 5000);
            if(cnt < 0){
                fprintf(stderr, "\nUSB error setting osccal: %s\n", usb_strerror());
            }
        }else{
            cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_OSCCAL, 0, 0, rxBuffer, 1, 5000);
            if(cnt < 0){
                fprintf(stderr, "\nUSB error getting osccal: %s\n", usb_strerror());
            }else{
                printf("osccal = %d\n", (unsigned char)rxBuffer[0]);
            }
        }
    }else{
        srandomdev();
        for(i = 0; i <= 100000; i++){
            fillBuffer(txBuffer, sizeof(txBuffer));
            cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_DATA, 0, 0, txBuffer, sizeof(txBuffer), 5000);
            if(cnt < 0){
                fprintf(stderr, "\nUSB tx error in iteration %d: %s\n", i, usb_strerror());
                break;
            }else if(cnt != sizeof(txBuffer)){
                fprintf(stderr, "\nerror in iteration %d: %d bytes sent instead of %d\n", i, cnt, (int)sizeof(txBuffer));
                break;
            }
            for(j = 0; j < sizeof(rxBuffer); j++){
                rxBuffer[j] = ~txBuffer[j];
            }
            cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DATA, 0, 0, rxBuffer, sizeof(rxBuffer), 5000);
            if(cnt < 0){
                fprintf(stderr, "\nUSB rx error in iteration %d: %s\n", i, usb_strerror());
                break;
            }else if(cnt != sizeof(txBuffer)){
                fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of %d\n", i, cnt, (int)sizeof(rxBuffer));
                break;
            }
            if(compareBuffers(txBuffer, rxBuffer, sizeof(rxBuffer))){
                fprintf(stderr, "\ncompare error in iteration %d.\n", i);
                break;
            }
            if(i != 0 && i % 100 == 0){
                printf(".");
                fflush(stdout);
                if(i % 5000 == 0)
                    printf(" %6d\n", i);
            }
        }
        fprintf(stderr, "\nTest completed.\n");
    }
    usb_close(handle);
    return 0;
}