Added code to support transmit from usb_test host program.
Reorganization of code to support making usb_test a useful utility...
This commit is contained in:
@ -24,10 +24,20 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
|
const uint16_t hackrf_usb_pid = 0x1d50;
|
||||||
|
const uint16_t hackrf_usb_vid = 0x604b;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TRANSCEIVER_MODE_RX,
|
||||||
|
TRANSCEIVER_MODE_TX,
|
||||||
|
} transceiver_mode_t;
|
||||||
|
static transceiver_mode_t transceiver_mode = TRANSCEIVER_MODE_RX;
|
||||||
|
|
||||||
static float
|
static float
|
||||||
TimevalDiff(const struct timeval *a, const struct timeval *b)
|
TimevalDiff(const struct timeval *a, const struct timeval *b)
|
||||||
{
|
{
|
||||||
@ -48,31 +58,27 @@ void write_callback(struct libusb_transfer* transfer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
void read_callback(struct libusb_transfer* transfer) {
|
||||||
if( argc != 2 ) {
|
if( transfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
||||||
printf("Usage: usb_test <file to capture to>\n");
|
byte_count += transfer->actual_length;
|
||||||
return -1;
|
read(fd, transfer->buffer, transfer->actual_length);
|
||||||
|
libusb_submit_transfer(transfer);
|
||||||
|
} else {
|
||||||
|
printf("transfer status was not 'completed'\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const uint32_t buffer_size = 16384;
|
libusb_device_handle* open_device(libusb_context* const context) {
|
||||||
|
|
||||||
fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
|
|
||||||
if( fd == -1 ) {
|
|
||||||
printf("Failed to open file for write\n");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
libusb_context* context = NULL;
|
|
||||||
int result = libusb_init(NULL);
|
int result = libusb_init(NULL);
|
||||||
if( result != 0 ) {
|
if( result != 0 ) {
|
||||||
printf("libusb_init() failed: %d\n", result);
|
printf("libusb_init() failed: %d\n", result);
|
||||||
return -4;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_device_handle* device = libusb_open_device_with_vid_pid(context, 0x1d50, 0x604b);
|
libusb_device_handle* device = libusb_open_device_with_vid_pid(context, hackrf_usb_pid, hackrf_usb_vid);
|
||||||
if( device == NULL ) {
|
if( device == NULL ) {
|
||||||
printf("libusb_open_device_with_vid_pid() failed\n");
|
printf("libusb_open_device_with_vid_pid() failed\n");
|
||||||
return -5;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//int speed = libusb_get_device_speed(device);
|
//int speed = libusb_get_device_speed(device);
|
||||||
@ -80,26 +86,46 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
result = libusb_set_configuration(device, 1);
|
result = libusb_set_configuration(device, 1);
|
||||||
if( result != 0 ) {
|
if( result != 0 ) {
|
||||||
|
libusb_close(device);
|
||||||
printf("libusb_set_configuration() failed: %d\n", result);
|
printf("libusb_set_configuration() failed: %d\n", result);
|
||||||
return -6;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = libusb_claim_interface(device, 0);
|
result = libusb_claim_interface(device, 0);
|
||||||
if( result != 0 ) {
|
if( result != 0 ) {
|
||||||
|
libusb_close(device);
|
||||||
printf("libusb_claim_interface() failed: %d\n", result);
|
printf("libusb_claim_interface() failed: %d\n", result);
|
||||||
return -7;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char endpoint_address = 0x81;
|
return device;
|
||||||
//unsigned char endpoint_address = 0x02;
|
}
|
||||||
|
|
||||||
|
void free_transfers(struct libusb_transfer** const transfers, const uint32_t transfer_count) {
|
||||||
|
for(uint32_t transfer_index=0; transfer_index<transfer_count; transfer_index++) {
|
||||||
|
libusb_free_transfer(transfers[transfer_index]);
|
||||||
|
}
|
||||||
|
free(transfers);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct libusb_transfer** prepare_transfers(
|
||||||
|
libusb_device_handle* const device,
|
||||||
|
const uint_fast8_t endpoint_address,
|
||||||
|
const uint32_t transfer_count,
|
||||||
|
const uint32_t buffer_size,
|
||||||
|
libusb_transfer_cb_fn callback
|
||||||
|
) {
|
||||||
|
struct libusb_transfer** const transfers = calloc(transfer_count, sizeof(struct libusb_transfer));
|
||||||
|
if( transfers == NULL ) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
const uint32_t transfer_count = 1024;
|
|
||||||
struct libusb_transfer* transfers[transfer_count];
|
|
||||||
for(uint32_t transfer_index=0; transfer_index<transfer_count; transfer_index++) {
|
for(uint32_t transfer_index=0; transfer_index<transfer_count; transfer_index++) {
|
||||||
transfers[transfer_index] = libusb_alloc_transfer(0);
|
transfers[transfer_index] = libusb_alloc_transfer(0);
|
||||||
if( transfers[transfer_index] == 0 ) {
|
if( transfers[transfer_index] == NULL ) {
|
||||||
|
free_transfers(transfers, transfer_count);
|
||||||
printf("libusb_alloc_transfer() failed\n");
|
printf("libusb_alloc_transfer() failed\n");
|
||||||
return -6;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_fill_bulk_transfer(
|
libusb_fill_bulk_transfer(
|
||||||
@ -108,23 +134,70 @@ int main(int argc, char** argv) {
|
|||||||
endpoint_address,
|
endpoint_address,
|
||||||
(unsigned char*)malloc(buffer_size),
|
(unsigned char*)malloc(buffer_size),
|
||||||
buffer_size,
|
buffer_size,
|
||||||
&write_callback,
|
callback,
|
||||||
NULL,
|
NULL,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
if( transfers[transfer_index]->buffer == 0 ) {
|
if( transfers[transfer_index]->buffer == NULL ) {
|
||||||
|
free_transfers(transfers, transfer_count);
|
||||||
printf("malloc() failed\n");
|
printf("malloc() failed\n");
|
||||||
return -7;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int error = libusb_submit_transfer(transfers[transfer_index]);
|
int error = libusb_submit_transfer(transfers[transfer_index]);
|
||||||
if( error != 0 ) {
|
if( error != 0 ) {
|
||||||
|
free_transfers(transfers, transfer_count);
|
||||||
printf("libusb_submit_transfer() failed: %d\n", error);
|
printf("libusb_submit_transfer() failed: %d\n", error);
|
||||||
return -8;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return transfers;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if( argc != 2 ) {
|
||||||
|
printf("Usage: usb_test <file path>\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* const path = argv[1];
|
||||||
|
|
||||||
|
fd = -1;
|
||||||
|
uint_fast8_t endpoint_address = 0;
|
||||||
|
libusb_transfer_cb_fn callback = NULL;
|
||||||
|
|
||||||
|
if( transceiver_mode == TRANSCEIVER_MODE_RX ) {
|
||||||
|
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||||
|
endpoint_address = 0x81;
|
||||||
|
callback = &write_callback;
|
||||||
|
} else {
|
||||||
|
fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||||
|
endpoint_address = 0x02;
|
||||||
|
callback = &read_callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( fd == -1 ) {
|
||||||
|
printf("Failed to open file: errno %d\n", errno);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_context* const context = NULL;
|
||||||
|
libusb_device_handle* const device = open_device(context);
|
||||||
|
if( device == NULL ) {
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t transfer_count = 1024;
|
||||||
|
const uint32_t buffer_size = 16384;
|
||||||
|
struct libusb_transfer** const transfers = prepare_transfers(
|
||||||
|
device, endpoint_address, transfer_count, buffer_size, callback
|
||||||
|
);
|
||||||
|
if( transfers == NULL ) {
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct timeval timeout = { 0, 500000 };
|
struct timeval timeout = { 0, 500000 };
|
||||||
@ -158,7 +231,9 @@ int main(int argc, char** argv) {
|
|||||||
call_count += 1;
|
call_count += 1;
|
||||||
} while(1);
|
} while(1);
|
||||||
|
|
||||||
result = libusb_release_interface(device, 0);
|
free_transfers(transfers, transfer_count);
|
||||||
|
|
||||||
|
int result = libusb_release_interface(device, 0);
|
||||||
if( result != 0 ) {
|
if( result != 0 ) {
|
||||||
printf("libusb_release_interface() failed: %d\n", result);
|
printf("libusb_release_interface() failed: %d\n", result);
|
||||||
return -2000;
|
return -2000;
|
||||||
|
Reference in New Issue
Block a user