view testugen2.c @ 34:2b8278ec5adb

Work around FreeBSD setting CPU flags based on architectures (-march=pentium-m doesn't work so well on AVR)
author darius
date Tue, 23 Oct 2007 10:52:50 +0930
parents d5a265299a4b
children
line wrap: on
line source

#include <sys/types.h>
#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Return codes for OWFirst()/OWNext() */
#define OW_BADWIRE		-3
#define OW_BADCRC		-2
#define OW_NOPRESENCE		-1
#define OW_NOMODULES		0
#define OW_FOUND		1

void	docmd(int fd, uint8_t *buffer, int rlen, int wlen);

int
main(int argc, char **argv) {
    int		endptfd, i;
    char	*endptname = "/dev/ugen0.2";
    uint8_t	buffer[9];
    
    if (argc < 2) {
	fprintf(stderr,
		"Bad usage:\n"
		"\t%s cmd\n", argv[0]);
	exit(1);
    }
    
    if ((endptfd = open(endptname, O_RDWR)) == -1) {
	fprintf(stderr, "Unable to open %s: %s\n", endptname, strerror(errno));
	exit(1);
    }

    i = 1;
    if (ioctl(endptfd, USB_SET_SHORT_XFER, &i) == -1) {
	fprintf(stderr, "Unable to set short xfer on end point 1: %s\n", strerror(errno));
	exit(1);
    }
    
    bzero(buffer, 9);

    if (!strcmp(argv[1], "reset")) {
	buffer[0] = 0x00;
	docmd(endptfd, buffer, 1, 1);
	printf("OWTouchReset() reported %d\n", buffer[0]);
    } else if (!strcmp(argv[1], "first")) {
	buffer[0] = 0x01;
	docmd(endptfd, buffer, 1, 9);
	printf("OWFirst() = %d\n", buffer[0]);
	if (buffer[0] == OW_FOUND)
	    printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
		   buffer[1], buffer[2], buffer[3], buffer[4],
		   buffer[5], buffer[6], buffer[7], buffer[8]);
    } else if (!strcmp(argv[1], "next")) {
	if (scanf(argv[2], "%hh:%hh:%hh:%hh:%hh:%hh:%hh:%hh", &buffer[1], &buffer[2], &buffer[3],
		  &buffer[4], &buffer[5], &buffer[6], &buffer[7], &buffer[8]) != 8) {
	    fprintf(stderr, "Unable to parse ROM ID\n");
	    exit(1);
	}
	buffer[0] = 0x02;
	fprintf(stderr, "About to OWNext()\n");
	docmd(endptfd, buffer, 9, 9);
	fprintf(stderr, "OWNext() = %d\n", buffer[0]);
	if (buffer[0] == OW_FOUND)
	    printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
		   buffer[1], buffer[2], buffer[3], buffer[4],
		   buffer[5], buffer[6], buffer[7], buffer[8]);
	
    } else if (!strcmp(argv[1], "scan")) {
	buffer[0] = 0x00;
	fprintf(stderr, "About to reset\n");
	docmd(endptfd, buffer, 1, 1);
	printf("OWReset() reported %d\n", buffer[0]);
	if (buffer[0] != 0)
	    exit(1);
	
	bzero(buffer, 9);
	buffer[0] = 0x01;
	fprintf(stderr, "About to OWFirst()\n");
	docmd(endptfd, buffer, 1, 9);
	printf("OWFirst() = %d\n", buffer[0]);

	do {
	    switch (buffer[0]) {
		case OW_BADWIRE:
		    printf("Bad wiring, either bus is held low, or a presence pulse was detected but no module found\n");
		    break;
		    
		case OW_NOPRESENCE:
		    printf("No presence pulse detected\n");
		    break;
		    
		case OW_BADCRC:
		    printf("Bad CRC\n");
		    break;
		    
		case OW_NOMODULES:
		case OW_FOUND:
		    break;
		    
		default:
		    printf("Unknown return code %d\n", buffer[0]);
		    break;
	    }
	    
	    if (buffer[0] != OW_FOUND) {
		printf("No more modules\n");
		break;
	    } else {
		printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
		       buffer[1], buffer[2], buffer[3], buffer[4],
		       buffer[5], buffer[6], buffer[7], buffer[8]);
	    }
	    
	    buffer[0] = 0x02;
	    fprintf(stderr, "About to OWNext()\n");
	    docmd(endptfd, buffer, 9, 9);
	} while(1);
    } else {
	printf("Unknown command: %s\n", argv[1]);
	exit(1);
    }
    
    
    exit(0);
}

void
docmd(int fd, uint8_t *buffer, int wlen, int rlen) {
    char blah[20];
    int	i;

    fprintf(stderr, "About to write\n");
    gets(blah);

    if ((i = write(fd, buffer, wlen)) == -1) {
	fprintf(stderr, "Unable to write end point: %s\n", strerror(errno));
	exit(1);
    }
    if (i != wlen)
	printf("Warning, only wrote %d of %d bytes\n", i, wlen);
    
    fprintf(stderr, "About to read\n");
    gets(blah);

    if ((i = read(fd, buffer, rlen)) == -1) {
	fprintf(stderr, "Unable to read from end point: %s\n", strerror(errno));
	exit(1);
    }
    if (i != rlen)
	printf("Warning, only read %d of %d bytes\n", i, wlen);
}