From 281c8701f89b61e3a3c5006e29cc7a044e036e61 Mon Sep 17 00:00:00 2001 From: TitanMKD Date: Mon, 1 Apr 2013 18:35:32 +0200 Subject: [PATCH] Example cleanup/warning fix for better compatibility with GNU g++. Adding hackrf_compute_baseband_filter_bw_round_down_lt() & hackrf_compute_baseband_filter_bw() in libhackrf. Adding DLL export for _WIN32 (cdecl ..). --- host/libhackrf/examples/hackrf_max2837.c | 3 +- host/libhackrf/examples/hackrf_rffc5071.c | 3 +- host/libhackrf/examples/hackrf_si5351c.c | 17 ++- host/libhackrf/examples/hackrf_transfer.c | 92 +++---------- host/libhackrf/src/CMakeLists.txt | 3 + host/libhackrf/src/hackrf.c | 158 ++++++++++++++++------ host/libhackrf/src/hackrf.h | 111 +++++++++------ 7 files changed, 229 insertions(+), 158 deletions(-) diff --git a/host/libhackrf/examples/hackrf_max2837.c b/host/libhackrf/examples/hackrf_max2837.c index cac878ff..e780dba6 100644 --- a/host/libhackrf/examples/hackrf_max2837.c +++ b/host/libhackrf/examples/hackrf_max2837.c @@ -82,9 +82,10 @@ int dump_register(hackrf_device* device, const uint16_t register_number) { } int dump_registers(hackrf_device* device) { + uint16_t register_number; int result = HACKRF_SUCCESS; - for(uint16_t register_number=0; register_number<32; register_number++) { + for(register_number=0; register_number<32; register_number++) { result = dump_register(device, register_number); if( result != HACKRF_SUCCESS ) { break; diff --git a/host/libhackrf/examples/hackrf_rffc5071.c b/host/libhackrf/examples/hackrf_rffc5071.c index 0808f7fb..15ca6e71 100644 --- a/host/libhackrf/examples/hackrf_rffc5071.c +++ b/host/libhackrf/examples/hackrf_rffc5071.c @@ -83,9 +83,10 @@ int dump_register(hackrf_device* device, const uint16_t register_number) { } int dump_registers(hackrf_device* device) { + uint16_t register_number; int result = HACKRF_SUCCESS; - for(uint16_t register_number=0; register_number<31; register_number++) { + for(register_number=0; register_number<31; register_number++) { result = dump_register(device, register_number); if( result != HACKRF_SUCCESS ) { break; diff --git a/host/libhackrf/examples/hackrf_si5351c.c b/host/libhackrf/examples/hackrf_si5351c.c index 3b723f4c..d5373df6 100644 --- a/host/libhackrf/examples/hackrf_si5351c.c +++ b/host/libhackrf/examples/hackrf_si5351c.c @@ -70,9 +70,10 @@ int dump_register(hackrf_device* device, const uint16_t register_number) { } int dump_registers(hackrf_device* device) { + uint16_t register_number; int result = HACKRF_SUCCESS; - for(uint16_t register_number=0; register_number<256; register_number++) { + for(register_number=0; register_number<256; register_number++) { result = dump_register(device, register_number); if( result != HACKRF_SUCCESS ) { break; @@ -102,9 +103,12 @@ int write_register( #define REGISTER_INVALID 32767 int dump_multisynth_config(hackrf_device* device, const uint_fast8_t ms_number) { + uint_fast8_t i; + uint_fast8_t reg_base; uint16_t parameters[8]; - uint_fast8_t reg_base = 42 + (ms_number * 8); - for(uint_fast8_t i=0; i<8; i++) { + + reg_base = 42 + (ms_number * 8); + for(i=0; i<8; i++) { uint_fast8_t reg_number = reg_base + i; int result = hackrf_si5351c_read(device, reg_number, ¶meters[i]); if( result != HACKRF_SUCCESS ) { @@ -141,8 +145,11 @@ int dump_multisynth_config(hackrf_device* device, const uint_fast8_t ms_number) } int dump_configuration(hackrf_device* device) { - for(uint_fast8_t ms_number=0; ms_number<8; ms_number++) { - int result = dump_multisynth_config(device, ms_number); + uint_fast8_t ms_number; + int result; + + for(ms_number=0; ms_number<8; ms_number++) { + result = dump_multisynth_config(device, ms_number); if( result != HACKRF_SUCCESS ) { return result; } diff --git a/host/libhackrf/examples/hackrf_transfer.c b/host/libhackrf/examples/hackrf_transfer.c index 48aca5e5..e40e1561 100644 --- a/host/libhackrf/examples/hackrf_transfer.c +++ b/host/libhackrf/examples/hackrf_transfer.c @@ -43,7 +43,9 @@ #include #include -#define FREQ_ONE_MHZ (1000000) +#define FD_BUFFER_SIZE (8*1024) + +#define FREQ_ONE_MHZ (1000000ull) #define DEFAULT_FREQ_HZ (900000000ull) /* 900MHz */ #define FREQ_MIN_HZ (30000000ull) /* 30MHz */ @@ -124,30 +126,6 @@ t_wav_file_hdr wave_file_hdr = } }; -typedef struct { - uint32_t bandwidth_hz; -} max2837_ft_t; - -static const max2837_ft_t max2837_ft[] = { - { 1750000 }, - { 2500000 }, - { 3500000 }, - { 5000000 }, - { 5500000 }, - { 6000000 }, - { 7000000 }, - { 8000000 }, - { 9000000 }, - { 10000000 }, - { 12000000 }, - { 14000000 }, - { 15000000 }, - { 20000000 }, - { 24000000 }, - { 28000000 }, - { 0 }, -}; - typedef enum { TRANSCEIVER_MODE_OFF = 0, TRANSCEIVER_MODE_RX = 1, @@ -209,47 +187,6 @@ int parse_u32(char* s, uint32_t* const value) { } } -/* Return final bw round down and less than expected bw. */ -uint32_t compute_baseband_filter_bw_round_down_lt(const uint32_t bandwidth_hz) -{ - const max2837_ft_t* p = max2837_ft; - while( p->bandwidth_hz != 0 ) - { - if( p->bandwidth_hz >= bandwidth_hz ) { - break; - } - p++; - } - /* Round down (if no equal to first entry) */ - if(p != max2837_ft) - { - p--; - } - return p->bandwidth_hz; -} - -/* Return final bw. */ -uint32_t compute_baseband_filter_bw(const uint32_t bandwidth_hz) -{ - const max2837_ft_t* p = max2837_ft; - while( p->bandwidth_hz != 0 ) - { - if( p->bandwidth_hz >= bandwidth_hz ) { - break; - } - p++; - } - - /* Round down (if no equal to first entry) and if > bandwidth_hz */ - if(p != max2837_ft) - { - if(p->bandwidth_hz > bandwidth_hz) - p--; - } - - return p->bandwidth_hz; -} - volatile bool do_exit = false; FILE* fd = NULL; @@ -464,11 +401,11 @@ int main(int argc, char** argv) { if( baseband_filter_bw ) { /* Compute nearest freq for bw filter */ - baseband_filter_bw_hz = compute_baseband_filter_bw(baseband_filter_bw_hz); + baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw(baseband_filter_bw_hz); }else { /* Compute default value depending on sample rate */ - baseband_filter_bw_hz = compute_baseband_filter_bw_round_down_lt(sample_rate_hz); + baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz); } if (baseband_filter_bw_hz > BASEBAND_FILTER_BW_MAX) { @@ -559,7 +496,14 @@ int main(int argc, char** argv) { printf("Failed to open file: %s\n", path); return EXIT_FAILURE; } - + /* Change fd buffer to have bigger one to store or read data on/to HDD */ + result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); + if( result != 0 ) { + printf("setvbuf() failed: %d\n", result); + usage(); + return EXIT_FAILURE; + } + /* Write Wav header */ if( receive_wav ) { @@ -573,7 +517,7 @@ int main(int argc, char** argv) { signal(SIGTERM, &sigint_callback_handler); signal(SIGABRT, &sigint_callback_handler); - printf("call hackrf_sample_rate_set(%u Hz/%.02f MHz)\n", sample_rate_hz,((float)sample_rate_hz/(float)FREQ_ONE_MHZ)); + printf("call hackrf_sample_rate_set(%u Hz/%.03f MHz)\n", sample_rate_hz,((float)sample_rate_hz/(float)FREQ_ONE_MHZ)); result = hackrf_sample_rate_set(device, sample_rate_hz); if( result != HACKRF_SUCCESS ) { printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result); @@ -581,7 +525,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - printf("call hackrf_baseband_filter_bandwidth_set(%d Hz/%.02f MHz)\n", + printf("call hackrf_baseband_filter_bandwidth_set(%d Hz/%.03f MHz)\n", baseband_filter_bw_hz, ((float)baseband_filter_bw_hz/(float)FREQ_ONE_MHZ)); result = hackrf_baseband_filter_bandwidth_set(device, baseband_filter_bw_hz); if( result != HACKRF_SUCCESS ) { @@ -591,9 +535,9 @@ int main(int argc, char** argv) { } if( transceiver_mode == TRANSCEIVER_MODE_RX ) { - result = hackrf_start_rx(device, rx_callback); + result = hackrf_start_rx(device, rx_callback, NULL); } else { - result = hackrf_start_tx(device, tx_callback); + result = hackrf_start_tx(device, tx_callback, NULL); } if( result != HACKRF_SUCCESS ) { printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result); @@ -601,7 +545,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - printf("call hackrf_set_freq(%llu Hz/%llu MHz)\n", freq_hz, (freq_hz/FREQ_ONE_MHZ) ); + printf("call hackrf_set_freq(%llu Hz/%.03f MHz)\n", freq_hz, ((float)freq_hz/(float)FREQ_ONE_MHZ) ); result = hackrf_set_freq(device, freq_hz); if( result != HACKRF_SUCCESS ) { printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result); diff --git a/host/libhackrf/src/CMakeLists.txt b/host/libhackrf/src/CMakeLists.txt index 02cd40e6..8e15b8b0 100644 --- a/host/libhackrf/src/CMakeLists.txt +++ b/host/libhackrf/src/CMakeLists.txt @@ -25,6 +25,9 @@ set(c_sources ${CMAKE_CURRENT_SOURCE_DIR}/hackrf.c CACHE INTERNAL "List of C sources") set(c_headers ${CMAKE_CURRENT_SOURCE_DIR}/hackrf.h CACHE INTERNAL "List of C headers") +set_source_files_properties(hackrf.c PROPERTIES LANGUAGE CXX ) +set_source_files_properties(hackrf.h PROPERTIES LANGUAGE CXX ) + # Dynamic library add_library(hackrf SHARED ${c_sources}) set_target_properties(hackrf PROPERTIES VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.0 SOVERSION 0) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index e81f18c7..8323b7dd 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -65,6 +65,33 @@ struct hackrf_device { uint32_t transfer_count; uint32_t buffer_size; bool streaming; + void* rx_ctx; + void* tx_ctx; +}; + + +typedef struct { + uint32_t bandwidth_hz; +} max2837_ft_t; + +static const max2837_ft_t max2837_ft[] = { + { 1750000 }, + { 2500000 }, + { 3500000 }, + { 5000000 }, + { 5500000 }, + { 6000000 }, + { 7000000 }, + { 8000000 }, + { 9000000 }, + { 10000000 }, + { 12000000 }, + { 14000000 }, + { 15000000 }, + { 20000000 }, + { 24000000 }, + { 28000000 }, + { 0 }, }; volatile bool do_exit = false; @@ -91,7 +118,7 @@ static int free_transfers(hackrf_device* device) { static int allocate_transfers(hackrf_device* const device) { if( device->transfers == NULL ) { - device->transfers = calloc(device->transfer_count, sizeof(struct libusb_transfer)); + device->transfers = (libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer)); if( device->transfers == NULL ) { return HACKRF_ERROR_NO_MEM; } @@ -158,7 +185,12 @@ static int cancel_transfers(hackrf_device* device) { } } */ -int hackrf_init() { +#ifdef __cplusplus +extern "C" +{ +#endif + +int ADDCALL hackrf_init() { const int libusb_error = libusb_init(&g_libusb_context); if( libusb_error != 0 ) { return HACKRF_ERROR_LIBUSB; @@ -167,7 +199,7 @@ int hackrf_init() { } } -int hackrf_exit() { +int ADDCALL hackrf_exit() { if( g_libusb_context != NULL ) { libusb_exit(g_libusb_context); g_libusb_context = NULL; @@ -176,7 +208,7 @@ int hackrf_exit() { return HACKRF_SUCCESS; } -int hackrf_open(hackrf_device** device) { +int ADDCALL hackrf_open(hackrf_device** device) { if( device == NULL ) { return HACKRF_ERROR_INVALID_PARAM; } @@ -204,7 +236,7 @@ int hackrf_open(hackrf_device** device) { } hackrf_device* lib_device = NULL; - lib_device = malloc(sizeof(*lib_device)); + lib_device = (hackrf_device*)malloc(sizeof(*lib_device)); if( lib_device == NULL ) { libusb_release_interface(usb_device, 0); libusb_close(usb_device); @@ -220,8 +252,8 @@ int hackrf_open(hackrf_device** device) { lib_device->transfer_count = 1024; lib_device->buffer_size = 16384; */ - lib_device->transfer_count = 4; - lib_device->buffer_size = 262144; /* 1048576; */ + lib_device->transfer_count = 32; + lib_device->buffer_size = 16384; //262144; /* 1048576; */ lib_device->streaming = false; do_exit = false; @@ -238,7 +270,7 @@ int hackrf_open(hackrf_device** device) { return HACKRF_SUCCESS; } -static int hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceiver_mode value) { +int ADDCALL hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceiver_mode value) { int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, @@ -257,7 +289,7 @@ static int hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceiver } } -int hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value) { +int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value) { if( register_number >= 32 ) { return HACKRF_ERROR_INVALID_PARAM; } @@ -280,7 +312,7 @@ int hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t } } -int hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value) { +int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value) { if( register_number >= 32 ) { return HACKRF_ERROR_INVALID_PARAM; } @@ -306,7 +338,7 @@ int hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_ } } -int hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value) { +int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value) { if( register_number >= 256 ) { return HACKRF_ERROR_INVALID_PARAM; } @@ -331,7 +363,7 @@ int hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_ } } -int hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value) { +int ADDCALL hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value) { if( register_number >= 256 ) { return HACKRF_ERROR_INVALID_PARAM; } @@ -357,7 +389,7 @@ int hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16 } } -int hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz) { +int ADDCALL hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz) { int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, @@ -376,7 +408,7 @@ int hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_h } } -int hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz) { +int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz) { int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, @@ -396,7 +428,7 @@ int hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t b } -int hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_t* value) +int ADDCALL hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_t* value) { if( register_number >= 31 ) { return HACKRF_ERROR_INVALID_PARAM; @@ -420,7 +452,7 @@ int hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_ } } -int hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16_t value) +int ADDCALL hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16_t value) { if( register_number >= 31 ) { return HACKRF_ERROR_INVALID_PARAM; @@ -444,7 +476,7 @@ int hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16 } } -int hackrf_spiflash_erase(hackrf_device* device) { +int ADDCALL hackrf_spiflash_erase(hackrf_device* device) { int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, @@ -463,7 +495,7 @@ int hackrf_spiflash_erase(hackrf_device* device) { } } -int hackrf_spiflash_write(hackrf_device* device, const uint32_t address, +int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* const data) { if (address > 0x0FFFFF) { @@ -488,7 +520,7 @@ int hackrf_spiflash_write(hackrf_device* device, const uint32_t address, } } -int hackrf_spiflash_read(hackrf_device* device, const uint32_t address, +int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* data) { if (address > 0x0FFFFF) { @@ -513,7 +545,7 @@ int hackrf_spiflash_read(hackrf_device* device, const uint32_t address, } } -int hackrf_cpld_write(hackrf_device* device, const uint16_t length, +int ADDCALL hackrf_cpld_write(hackrf_device* device, const uint16_t length, unsigned char* const data) { int result = libusb_control_transfer( @@ -534,7 +566,7 @@ int hackrf_cpld_write(hackrf_device* device, const uint16_t length, } } -int hackrf_board_id_read(hackrf_device* device, uint8_t* value) { +int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value) { int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, @@ -553,7 +585,7 @@ int hackrf_board_id_read(hackrf_device* device, uint8_t* value) { } } -int hackrf_version_string_read(hackrf_device* device, char* version, +int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, uint8_t length) { int result = libusb_control_transfer( @@ -582,7 +614,7 @@ typedef struct { } set_freq_params_t; #define FREQ_ONE_MHZ (1000*1000ull) -int hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) +int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) { uint32_t l_freq_mhz; uint32_t l_freq_hz; @@ -614,7 +646,7 @@ int hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) } } -int hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) +int ADDCALL hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) { int result = libusb_control_transfer( device->usb_device, @@ -634,7 +666,7 @@ int hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) } } -int hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialno_t* read_partid_serialno) +int ADDCALL hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialno_t* read_partid_serialno) { uint8_t length; @@ -660,7 +692,7 @@ int hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialn static void* transfer_threadproc(void* arg) { hackrf_device* device = (hackrf_device*)arg; - struct timeval timeout = { 0, 500000 }; + struct timeval timeout = { 1, 000000 }; while( (device->streaming) && (do_exit == false) ) { @@ -678,10 +710,12 @@ static void hackrf_libusb_transfer_callback(struct libusb_transfer* usb_transfer if( usb_transfer->status == LIBUSB_TRANSFER_COMPLETED ) { hackrf_transfer transfer = { - .device = device, - .buffer = usb_transfer->buffer, - .buffer_length = usb_transfer->length, - .valid_length = usb_transfer->actual_length, + transfer.device = device, + transfer.buffer = usb_transfer->buffer, + transfer.buffer_length = usb_transfer->length, + transfer.valid_length = usb_transfer->actual_length, + transfer.rx_ctx = device->rx_ctx, + transfer.tx_ctx = device->tx_ctx }; if( device->callback(&transfer) == 0 ) { @@ -739,20 +773,21 @@ static int create_transfer_thread( return HACKRF_SUCCESS; } -bool hackrf_is_streaming(hackrf_device* device) { +bool ADDCALL hackrf_is_streaming(hackrf_device* device) { return device->streaming; } -int hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback) { +int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx) { const uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | 1; int result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_RECEIVE); if( result == HACKRF_SUCCESS ) { + device->rx_ctx = rx_ctx; create_transfer_thread(device, endpoint_address, callback); } return result; } -int hackrf_stop_rx(hackrf_device* device) { +int ADDCALL hackrf_stop_rx(hackrf_device* device) { int result1, result2; result1 = kill_transfer_thread(device); result2 = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); @@ -761,16 +796,17 @@ int hackrf_stop_rx(hackrf_device* device) { return result1; } -int hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback) { +int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* tx_ctx) { const uint8_t endpoint_address = LIBUSB_ENDPOINT_OUT | 2; int result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_TRANSMIT); if( result == HACKRF_SUCCESS ) { + device->tx_ctx = tx_ctx; result = create_transfer_thread(device, endpoint_address, callback); } return result; } -int hackrf_stop_tx(hackrf_device* device) { +int ADDCALL hackrf_stop_tx(hackrf_device* device) { int result1, result2; result1 = kill_transfer_thread(device); result2 = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); @@ -779,7 +815,7 @@ int hackrf_stop_tx(hackrf_device* device) { return result1; } -int hackrf_close(hackrf_device* device) { +int ADDCALL hackrf_close(hackrf_device* device) { if( device != NULL ) { int result = hackrf_stop_rx(device); if( result ) { @@ -810,7 +846,7 @@ int hackrf_close(hackrf_device* device) { return HACKRF_SUCCESS; } -const char* hackrf_error_name(enum hackrf_error errcode) { +const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) { switch(errcode) { case HACKRF_SUCCESS: return "HACKRF_SUCCESS"; @@ -841,7 +877,7 @@ const char* hackrf_error_name(enum hackrf_error errcode) { } } -const char* hackrf_board_id_name(enum hackrf_board_id board_id) { +const char* ADDCALL hackrf_board_id_name(enum hackrf_board_id board_id) { switch(board_id) { case BOARD_ID_JELLYBEAN: return "Jellybean"; @@ -856,3 +892,49 @@ const char* hackrf_board_id_name(enum hackrf_board_id board_id) { return "Unknown Board ID"; } } + +/* Return final bw round down and less than expected bw. */ +uint32_t ADDCALL hackrf_compute_baseband_filter_bw_round_down_lt(const uint32_t bandwidth_hz) +{ + const max2837_ft_t* p = max2837_ft; + while( p->bandwidth_hz != 0 ) + { + if( p->bandwidth_hz >= bandwidth_hz ) { + break; + } + p++; + } + /* Round down (if no equal to first entry) */ + if(p != max2837_ft) + { + p--; + } + return p->bandwidth_hz; +} + +/* Return final bw. */ +uint32_t ADDCALL hackrf_compute_baseband_filter_bw(const uint32_t bandwidth_hz) +{ + const max2837_ft_t* p = max2837_ft; + while( p->bandwidth_hz != 0 ) + { + if( p->bandwidth_hz >= bandwidth_hz ) { + break; + } + p++; + } + + /* Round down (if no equal to first entry) and if > bandwidth_hz */ + if(p != max2837_ft) + { + if(p->bandwidth_hz > bandwidth_hz) + p--; + } + + return p->bandwidth_hz; +} + +#ifdef __cplusplus +} // __cplusplus defined. +#endif + diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 5a1314e2..7d5f8f22 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -26,6 +26,27 @@ #include #include +#ifdef _WIN32 + #define ADD_EXPORTS + + /* You should define ADD_EXPORTS *only* when building the DLL. */ + #ifdef ADD_EXPORTS + #define ADDAPI __declspec(dllexport) + #else + #define ADDAPI __declspec(dllimport) + #endif + + /* Define calling convention in one place, for convenience. */ + #define ADDCALL __cdecl + +#else /* _WIN32 not defined. */ + + /* Define with no value on non-Windows OSes. */ + #define ADDAPI + #define ADDCALL + +#endif + enum hackrf_error { HACKRF_SUCCESS = 0, HACKRF_ERROR_INVALID_PARAM = -2, @@ -50,6 +71,8 @@ typedef struct { uint8_t* buffer; int buffer_length; int valid_length; + void* rx_ctx; + void* tx_ctx; } hackrf_transfer; typedef struct { @@ -59,52 +82,62 @@ typedef struct { typedef int (*hackrf_sample_block_cb_fn)(hackrf_transfer* transfer); -int hackrf_init(); -int hackrf_exit(); +#ifdef __cplusplus +extern "C" +{ +#endif -int hackrf_open(hackrf_device** device); -int hackrf_close(hackrf_device* device); +extern ADDAPI int ADDCALL hackrf_init(); +extern ADDAPI int ADDCALL hackrf_exit(); + +extern ADDAPI int ADDCALL hackrf_open(hackrf_device** device); +extern ADDAPI int ADDCALL hackrf_close(hackrf_device* device); + +extern ADDAPI int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx); +extern ADDAPI int ADDCALL hackrf_stop_rx(hackrf_device* device); + +extern ADDAPI int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* tx_ctx); +extern ADDAPI int ADDCALL hackrf_stop_tx(hackrf_device* device); + +extern ADDAPI bool ADDCALL hackrf_is_streaming(hackrf_device* device); + +extern ADDAPI int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value); +extern ADDAPI int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value); + +extern ADDAPI int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value); +extern ADDAPI int ADDCALL hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value); + +extern ADDAPI int ADDCALL hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz); +extern ADDAPI int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz); + +extern ADDAPI int ADDCALL hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_t* value); +extern ADDAPI int ADDCALL hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16_t value); + +extern ADDAPI int ADDCALL hackrf_spiflash_erase(hackrf_device* device); +extern ADDAPI int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* const data); +extern ADDAPI int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* data); + +extern ADDAPI int ADDCALL hackrf_cpld_write(hackrf_device* device, const uint16_t length, unsigned char* const data); -int hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback); -int hackrf_stop_rx(hackrf_device* device); +extern ADDAPI int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value); +extern ADDAPI int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, uint8_t length); -int hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback); -int hackrf_stop_tx(hackrf_device* device); +extern ADDAPI int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz); -bool hackrf_is_streaming(hackrf_device* device); +extern ADDAPI int ADDCALL hackrf_set_amp_enable(hackrf_device* device, const uint8_t value); -int hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value); -int hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value); +extern ADDAPI int ADDCALL hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialno_t* read_partid_serialno); -int hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value); -int hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value); +extern ADDAPI const char* ADDCALL hackrf_error_name(enum hackrf_error errcode); +extern ADDAPI const char* ADDCALL hackrf_board_id_name(enum hackrf_board_id board_id); -int hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz); -int hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz); +/* Compute nearest freq for bw filter (manual filter) */ +extern ADDAPI uint32_t ADDCALL hackrf_compute_baseband_filter_bw_round_down_lt(const uint32_t bandwidth_hz); +/* Compute best default value depending on sample rate (auto filter) */ +extern ADDAPI uint32_t ADDCALL hackrf_compute_baseband_filter_bw(const uint32_t bandwidth_hz); -int hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_t* value); -int hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16_t value); - -int hackrf_spiflash_erase(hackrf_device* device); -int hackrf_spiflash_write(hackrf_device* device, const uint32_t address, - const uint16_t length, unsigned char* const data); -int hackrf_spiflash_read(hackrf_device* device, const uint32_t address, - const uint16_t length, unsigned char* data); - -int hackrf_cpld_write(hackrf_device* device, const uint16_t length, - unsigned char* const data); - -int hackrf_board_id_read(hackrf_device* device, uint8_t* value); -int hackrf_version_string_read(hackrf_device* device, char* version, - uint8_t length); - -int hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz); - -int hackrf_set_amp_enable(hackrf_device* device, const uint8_t value); - -int hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialno_t* read_partid_serialno); - -const char* hackrf_error_name(enum hackrf_error errcode); -const char* hackrf_board_id_name(enum hackrf_board_id board_id); +#ifdef __cplusplus +} // __cplusplus defined. +#endif #endif//__HACKRF_H__