int main(int argc, char *argv[])
{
	int opt;
	int r = 0;
	ubertooth_t* ut = NULL;
	rangetest_result rr;
	int do_stop, do_flash, do_isp, do_leds, do_part, do_reset;
	int do_serial, do_tx, do_palevel, do_channel, do_led_specan;
	int do_range_test, do_repeater, do_firmware, do_board_id;
	int do_range_result, do_all_leds, do_identify;
	int do_set_squelch, do_get_squelch, squelch_level;
	int do_something, do_compile_info;
	char ubertooth_device = -1;

	/* set command states to negative as a starter
	 * setting to 0 means 'do it'
	 * setting to positive is value of specified argument */
	do_stop= do_flash= do_isp= do_leds= do_part= do_reset= -1;
	do_serial= do_tx= do_palevel= do_channel= do_led_specan= -1;
	do_range_test= do_repeater= do_firmware= do_board_id= -1;
	do_range_result= do_all_leds= do_identify= -1;
	do_set_squelch= -1, do_get_squelch= -1; squelch_level= 0;
	do_something= 0; do_compile_info= -1;

	while ((opt=getopt(argc,argv,"U:hnmefiIprsStvbl::a::C::c::d::q::z::9V")) != EOF) {
		switch(opt) {
		case 'U': 
			ubertooth_device = atoi(optarg);
			break;
		case 'f':
			fprintf(stderr, "ubertooth-util -f is no longer required - use ubertooth-dfu instead\n");
			do_flash= 0;
			break;
		case 'i':
			do_isp= 0;
			break;
		case 'I':
			do_identify= 0;
			break;
		case 'l':
			if (optarg)
				do_leds= atoi(optarg);
			else
				do_leds= 2; /* can't use 0 as it's a valid option */
			break;
		case 'd':
			if (optarg)
				do_all_leds= atoi(optarg);
			else
				do_all_leds= 2; /* can't use 0 as it's a valid option */
			break;
		case 'p':
			do_part= 0;
			break;
		case 'r':
			do_reset= 0;
			break;
		case 's':
			do_serial= 0;
			break;
		case 'S':
			do_stop= 0;
			break;
		case 't':
			do_tx= 0;
			break;
		case 'a':
			if (optarg)
				do_palevel= atoi(optarg);
			else
				do_palevel= 0;
			break;
		case 'C':
			if (optarg)
				do_channel= atoi(optarg) +2402;
			else
				do_channel= 0;
			break;	
		case 'c':
			if (optarg)
				do_channel= atoi(optarg);
			else
				do_channel= 0;
			break;
		case 'q':
			if (optarg)
				do_led_specan= atoi(optarg);
			else
				do_led_specan= 0;
			break;
		case 'n':
			do_range_test= 0;
			break;
		case 'm':
			do_range_result= 0;
			break;
		case 'e':
			do_repeater= 0;
			break;
		case 'v':
			do_firmware= 0;
			break;
		case 'b':
			do_board_id= 0;
			break;
		case 'z':
			if (optarg) {
				squelch_level = atoi(optarg);
				do_set_squelch = 1;
			}
			else {
				do_get_squelch = 1;
			}
			break;
		case '9':
			do_something= 1;
			break;
		case 'V':
			do_compile_info = 0;
			break;
		case 'h':
		default:
			usage();
			return 1;
		}
	}

	/* initialise device */
	ut = ubertooth_start(ubertooth_device);
	if (ut == NULL) {
		usage();
		return 1;
	}
	if(do_reset == 0) {
		printf("Resetting ubertooth device number %d\n", (ubertooth_device >= 0) ? ubertooth_device : 0);
		r = cmd_reset(ut->devh);
		sleep(2);
		ut = ubertooth_start(ubertooth_device);
	}
	if(do_stop == 0) {
		printf("Stopping ubertooth device number %d\n", (ubertooth_device >= 0) ? ubertooth_device : 0);
		r = cmd_stop(ut->devh);
	}

	/* device configuration actions */
	if(do_all_leds == 0 || do_all_leds == 1) {
		cmd_set_usrled(ut->devh, do_all_leds);
		cmd_set_rxled(ut->devh, do_all_leds);
		r= cmd_set_txled(ut->devh, do_all_leds);
		r = (r >= 0) ? 0 : r;
	}
	if(do_channel > 0)
		r= cmd_set_channel(ut->devh, do_channel);
	if(do_leds == 0 || do_leds == 1)
		r= cmd_set_usrled(ut->devh, do_leds);
	if(do_palevel > 0)
		r= cmd_set_palevel(ut->devh, do_palevel);
	
	/* reporting actions */
	if(do_all_leds == 2) {
		printf("USR LED status: %d\n", cmd_get_usrled(ut->devh));
		printf("RX LED status : %d\n", cmd_get_rxled(ut->devh));
		printf("TX LED status : %d\n", r= cmd_get_txled(ut->devh));
		r = (r >= 0) ? 0 : r;
	}
	if(do_board_id == 0) {
		r= cmd_get_board_id(ut->devh);
		printf("Board ID Number: %d (%s)\n", r, board_names[r]);
	}
	if(do_channel == 0) {
		r= cmd_get_channel(ut->devh);
		printf("Current frequency: %d MHz (Bluetooth channel %d)\n", r, r - 2402);
		}
	if(do_firmware == 0) {
		char version[255];
		cmd_get_rev_num(ut->devh, version, (u8)sizeof(version));
		printf("Firmware revision: %s\n", version);
        }
	if(do_compile_info == 0) {
		char compile_info[255];
		cmd_get_compile_info(ut->devh, compile_info, (u8)sizeof(compile_info));
		puts(compile_info);
	}
	if(do_leds == 2)
		printf("USR LED status: %d\n", r= cmd_get_usrled(ut->devh));
	if(do_palevel == 0)
		printf("PA Level: %d\n", r= cmd_get_palevel(ut->devh));
	if(do_part == 0) {
		printf("Part ID: %X\n", r = cmd_get_partnum(ut->devh));
		r = (r >= 0) ? 0 : r;
	}
	if(do_range_result == 0) {
		r = cmd_get_rangeresult(ut->devh, &rr);
		if (r == 0) {
			if (rr.valid==1) {
				printf("request PA level : %d\n", rr.request_pa);
				printf("request number   : %d\n", rr.request_num);
				printf("reply PA level   : %d\n", rr.reply_pa);
				printf("reply number     : %d\n", rr.reply_num);
			} else if (rr.valid>1) {
				printf("Invalid range test: mismatch on byte %d\n", rr.valid-2);
			} else {
				printf("invalid range test result\n");
			}
		}
	}
	if(do_serial == 0) {
		u8 serial[17];
		r= cmd_get_serial(ut->devh, serial);
		if(r==0) {
			print_serial(serial, NULL);
		}
		// FIXME: Why do we do this to non-zero results?
		r = (r >= 0) ? 0 : r;
	}

	/* final actions */
	if(do_flash == 0) {
		printf("Entering flash programming (DFU) mode\n");
		return cmd_flash(ut->devh);
	}
	if(do_identify == 0) {
		printf("Flashing LEDs on ubertooth device number %d\n", (ubertooth_device >= 0) ? ubertooth_device : 0);
		while(42) {
			do_identify= !do_identify;
			cmd_set_usrled(ut->devh, do_identify);
			cmd_set_rxled(ut->devh, do_identify);
			cmd_set_txled(ut->devh, do_identify);
			sleep(1);
		}
	}
	if(do_isp == 0) {
		printf("Entering flash programming (ISP) mode\n");
		return cmd_set_isp(ut->devh);
	}
	if(do_led_specan >= 0) {
		do_led_specan= do_led_specan ? do_led_specan : 225;
		printf("Entering LED specan mode (RSSI %d)\n", do_led_specan);
		return cmd_led_specan(ut->devh, do_led_specan);
	}
	if(do_range_test == 0) {
		printf("Starting range test\n");
		return cmd_range_test(ut->devh);
	}
	if(do_repeater == 0) {
		printf("Starting repeater\n");
		return cmd_repeater(ut->devh);
	}
	if(do_tx == 0) {
		printf("Starting TX test\n");
		return cmd_tx_test(ut->devh);
	}
	if(do_set_squelch > 0) {
		printf("Setting squelch to %d\n", squelch_level);
		cmd_set_squelch(ut->devh, squelch_level);
	}
	if(do_get_squelch > 0) {
		r = cmd_get_squelch(ut->devh);
		printf("Squelch set to %d\n", (int8_t)r);
	}
	if(do_something) {
		unsigned char buf[4] = { 0x55, 0x55, 0x55, 0x55 };
		cmd_do_something(ut->devh, NULL, 0);
		cmd_do_something_reply(ut->devh, buf, 4);
		printf("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
		return 0;
	}

	return r;
}
int main(int argc, char *argv[])
{
	int opt;
	int r = 0;
	struct libusb_device_handle *devh= NULL;
	rangetest_result rr;
	int do_stop, do_flash, do_isp, do_leds, do_part, do_reset;
	int do_serial, do_tx, do_palevel, do_channel, do_led_specan;
	int do_range_test, do_repeater, do_firmware, do_board_id;
	int do_range_result, do_all_leds, do_identify;
	int do_set_squelch, do_get_squelch, squelch_level;

	/* set command states to negative as a starter
	 * setting to 0 means 'do it'
	 * setting to positive is value of specified argument */
	do_stop= do_flash= do_isp= do_leds= do_part= do_reset= -1;
	do_serial= do_tx= do_palevel= do_channel= do_led_specan= -1;
	do_range_test= do_repeater= do_firmware= do_board_id= -1;
	do_range_result= do_all_leds= do_identify= -1;
	do_set_squelch= -1, do_get_squelch= -1; squelch_level= 0;

	while ((opt=getopt(argc,argv,"U:hnmefiIprsStvbl::a::C::c::d::q::z::")) != EOF) {
		switch(opt) {
		case 'U': 
			Ubertooth_Device= atoi(optarg);
                        break;
		case 'f':
			do_flash= 0;
			break;
		case 'i':
			do_isp= 0;
			break;
		case 'I':
			do_identify= 0;
			break;
		case 'l':
			if (optarg)
				do_leds= atoi(optarg);
			else
				do_leds= 2; /* can't use 0 as it's a valid option */
			break;
		case 'd':
			if (optarg)
				do_all_leds= atoi(optarg);
			else
				do_all_leds= 2; /* can't use 0 as it's a valid option */
			break;
		case 'p':
			do_part= 0;
			break;
		case 'r':
			do_reset= 0;
			break;
		case 's':
			do_serial= 0;
			break;
		case 'S':
			do_stop= 0;
			break;
		case 't':
			do_tx= 0;
			break;
		case 'a':
			if (optarg)
				do_palevel= atoi(optarg);
			else
				do_palevel= 0;
			break;
		case 'C':
                        if (optarg)
				do_channel= atoi(optarg) +2402;
                        else
				do_channel= 0;
                        break;
	
		case 'c':
			if (optarg)
				do_channel= atoi(optarg);
			else
				do_channel= 0;
			break;
		case 'q':
			if (optarg)
				do_led_specan= atoi(optarg);
			else
				do_led_specan= 0;
			break;
		case 'n':
			do_range_test= 0;
			break;
		case 'm':
			do_range_result= 0;
			break;
		case 'e':
			do_repeater= 0;
			break;
		case 'v':
			do_firmware= 0;
			break;
		case 'b':
			do_board_id= 0;
			break;
		case 'z':
			if (optarg) {
				squelch_level = atoi(optarg);
				do_set_squelch = 1;
			}
			else {
				do_get_squelch = 1;
			}
			break;
		case 'h':
		default:
			usage();
			return 1;
		}
	}

	/* initialise device */
	devh = ubertooth_start();
	if (devh == NULL) {
		usage();
		return 1;
	}
	if(do_reset == 0) {
		printf("Resetting ubertooth device number %d\n", (Ubertooth_Device >= 0) ? Ubertooth_Device : 0);
		r= cmd_reset(devh);
		sleep(2);
		devh = ubertooth_start();
	}
	if(do_stop == 0) {
		printf("Stopping ubertooth device number %d\n", (Ubertooth_Device >= 0) ? Ubertooth_Device : 0);
		r= cmd_stop(devh);
	}

	/* device configuration actions */
	if(do_all_leds == 0 || do_all_leds == 1) {
		cmd_set_usrled(devh, do_all_leds);
		cmd_set_rxled(devh, do_all_leds);
		r= cmd_set_txled(devh, do_all_leds);
		r = (r >= 0) ? 0 : r;
	}
	if(do_channel > 0)
		r= cmd_set_channel(devh, do_channel);
	if(do_leds == 0 || do_leds == 1)
		r= cmd_set_usrled(devh, do_leds);
	if(do_palevel > 0)
		r= cmd_set_palevel(devh, do_palevel);
	
	/* reporting actions */
	if(do_all_leds == 2) {
		printf("USR LED status: %d\n", cmd_get_usrled(devh));
		printf("RX LED status : %d\n", cmd_get_rxled(devh));
		printf("TX LED status : %d\n", r= cmd_get_txled(devh));
		r = (r >= 0) ? 0 : r;
	}
	if(do_board_id == 0) {
		r= cmd_get_board_id(devh);
		printf("Board ID Number: %d (%s)\n", r, board_names[r]);
	}
	if(do_channel == 0) {
		r= cmd_get_channel(devh);
		printf("Current frequency: %d MHz (Bluetooth channel %d)\n", r, r - 2402);
		}
	if(do_firmware == 0)
		printf("Firmare revision: %d\n", r= cmd_get_rev_num(devh));
	if(do_leds == 2)
		printf("USR LED status: %d\n", r= cmd_get_usrled(devh));
	if(do_palevel == 0)
		printf("PA Level: %d\n", r= cmd_get_palevel(devh));
	if(do_part == 0) {
		printf("Part ID: %X\n", r = cmd_get_partnum(devh));
		r = (r >= 0) ? 0 : r;
	}
	if(do_range_result == 0) {
		r = cmd_get_rangeresult(devh, &rr);
		if (r == 0) {
			if (rr.valid) {
				printf("request PA level : %d\n", rr.request_pa);
				printf("request number   : %d\n", rr.request_num);
				printf("reply PA level   : %d\n", rr.reply_pa);
				printf("reply number     : %d\n", rr.reply_num);
			} else {
				printf("invalid range test result\n");
			}
		}
	}
	if(do_serial == 0) {
		printf("Serial No: ");
		r= cmd_get_serial(devh);
		r = (r >= 0) ? 0 : r;
	}

	/* final actions */
	if(do_flash == 0) {
		printf("Entering flash programming (DFU) mode\n");
		return cmd_flash(devh);
	}
	if(do_identify == 0) {
		printf("Flashing LEDs on ubertooth device number %d\n", (Ubertooth_Device >= 0) ? Ubertooth_Device : 0);
		while(42) {
			do_identify= !do_identify;
			cmd_set_usrled(devh, do_identify);
			cmd_set_rxled(devh, do_identify);
			cmd_set_txled(devh, do_identify);
			sleep(1);
		}
	}
	if(do_isp == 0) {
		printf("Entering flash programming (ISP) mode\n");
		return cmd_set_isp(devh);
	}
	if(do_led_specan >= 0) {
		do_led_specan= do_led_specan ? do_led_specan : 225;
		printf("Entering LED specan mode (RSSI %d)\n", do_led_specan);
		return cmd_led_specan(devh, do_led_specan);
	}
	if(do_range_test == 0) {
		printf("Starting range test\n");
		return cmd_range_test(devh);
	}
	if(do_repeater == 0) {
		printf("Starting repeater\n");
		return cmd_repeater(devh);
	}
	if(do_tx == 0) {
		printf("Starting TX test\n");
		return cmd_tx_test(devh);
	}
	if(do_set_squelch > 0) {
		printf("Setting squelch to %d\n", squelch_level);
		cmd_set_squelch(devh, squelch_level);
	}
	if(do_get_squelch > 0) {
		r = cmd_get_squelch(devh);
		printf("Squelch set to %d\n", (int8_t)r);
	}

	return r;
}