From 439ddaaa40b657ce1de4203e0b314c35c5234de0 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 17:00:41 -0700 Subject: [PATCH 1/7] Support compiling with Visual Studio --- host/hackrf-tools/src/CMakeLists.txt | 8 +- host/hackrf-tools/src/hackrf_transfer.c | 116 +++++++++++++----------- host/libhackrf/src/hackrf.c | 7 +- host/libhackrf/src/hackrf.h | 4 +- 4 files changed, 73 insertions(+), 62 deletions(-) diff --git a/host/hackrf-tools/src/CMakeLists.txt b/host/hackrf-tools/src/CMakeLists.txt index b30e492c..d8f602c1 100644 --- a/host/hackrf-tools/src/CMakeLists.txt +++ b/host/hackrf-tools/src/CMakeLists.txt @@ -24,12 +24,15 @@ set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX") INCLUDE(FindPkgConfig) -pkg_check_modules(FFTW REQUIRED fftw3f) if(MSVC) add_library(libgetopt_static STATIC ../getopt/getopt.c ) +else() + pkg_check_modules(FFTW REQUIRED fftw3f) + LIST(APPEND TOOLS hackrf_sweep) + LIST(APPEND TOOLS_LINK_LIBS m fftw3f) endif() SET(TOOLS @@ -41,7 +44,6 @@ SET(TOOLS hackrf_cpldjtag hackrf_info hackrf_operacake - hackrf_sweep ) if(NOT libhackrf_SOURCE_DIR) @@ -55,8 +57,6 @@ if(MSVC) LIST(APPEND TOOLS_LINK_LIBS libgetopt_static) endif() -LIST(APPEND TOOLS_LINK_LIBS m fftw3f) - foreach(tool ${TOOLS}) add_executable(${tool} ${tool}.c) target_link_libraries(${tool} ${TOOLS_LINK_LIBS}) diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index f44a0e25..ee27fd6c 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -379,26 +379,28 @@ int rx_callback(hackrf_transfer* transfer) { } } if (stream_size>0){ - if ((stream_size-1+stream_head-stream_tail)%stream_size buffer,bytes_to_write); - }else{ - memcpy(stream_buf+stream_tail,transfer->buffer,(stream_size-stream_tail)); - memcpy(stream_buf,transfer->buffer+(stream_size-stream_tail),bytes_to_write-(stream_size-stream_tail)); - }; - __atomic_store_n(&stream_tail,(stream_tail+bytes_to_write)%stream_size,__ATOMIC_RELEASE); +#ifndef _WIN32 + if ((stream_size-1+stream_head-stream_tail)%stream_size buffer,bytes_to_write); + } else { + memcpy(stream_buf+stream_tail,transfer->buffer,(stream_size-stream_tail)); + memcpy(stream_buf,transfer->buffer+(stream_size-stream_tail),bytes_to_write-(stream_size-stream_tail)); + }; + __atomic_store_n(&stream_tail,(stream_tail+bytes_to_write)%stream_size,__ATOMIC_RELEASE); } +#endif return 0; - }else{ - bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd); - if ((bytes_written != bytes_to_write) - || (limit_num_samples && (bytes_to_xfer == 0))) { - return -1; } else { - return 0; - } + bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd); + if ((bytes_written != bytes_to_write) + || (limit_num_samples && (bytes_to_xfer == 0))) { + return -1; + } else { + return 0; + } } } else { return -1; @@ -490,7 +492,10 @@ static void usage() { printf("\t[-s sample_rate_hz] # Sample rate in Hz (4/8/10/12.5/16/20MHz, default %sMHz).\n", u64toa((DEFAULT_SAMPLE_RATE_HZ/FREQ_ONE_MHZ),&ascii_u64_data1)); printf("\t[-n num_samples] # Number of samples to transfer (default is unlimited).\n"); +#ifndef _WIN32 +/* The required atomic load/store functions aren't available when using C with MSVC */ printf("\t[-S buf_size] # Enable receive streaming with buffer size buf_size.\n"); +#endif printf("\t[-c amplitude] # CW signal source mode, amplitude 0-127 (DC value to DAC).\n"); printf("\t[-R] # Repeat TX mode (default is off) \n"); printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in Hz.\n\tPossible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz, default < sample_rate_hz.\n" ); @@ -1041,48 +1046,49 @@ int main(int argc, char** argv) { uint32_t byte_count_now; struct timeval time_now; float time_difference, rate; - if (stream_size>0){ - if(stream_head==stream_tail){ - usleep(10000); // queue empty - }else{ - ssize_t len; - ssize_t bytes_written; - uint32_t _st= __atomic_load_n(&stream_tail,__ATOMIC_ACQUIRE); - if(stream_head<_st) - len=_st-stream_head; - else - len=stream_size-stream_head; - bytes_written = fwrite(stream_buf+stream_head, 1, len, fd); - if (len != bytes_written){ - printf("write failed"); - do_exit=true; - }; - stream_head=(stream_head+len)%stream_size; + if (stream_size>0) { +#ifndef _WIN32 + if(stream_head==stream_tail) { + usleep(10000); // queue empty + } else { + ssize_t len; + ssize_t bytes_written; + uint32_t _st= __atomic_load_n(&stream_tail,__ATOMIC_ACQUIRE); + if(stream_head<_st) + len=_st-stream_head; + else + len=stream_size-stream_head; + bytes_written = fwrite(stream_buf+stream_head, 1, len, fd); + if (len != bytes_written) { + printf("write failed"); + do_exit=true; + }; + stream_head=(stream_head+len)%stream_size; } - if(stream_drop>0){ - uint32_t drops= __atomic_exchange_n (&stream_drop,0,__ATOMIC_SEQ_CST); - printf("dropped frames: [%d]\n",drops); + if(stream_drop>0) { + uint32_t drops= __atomic_exchange_n (&stream_drop,0,__ATOMIC_SEQ_CST); + printf("dropped frames: [%d]\n",drops); } - }else{ - sleep(1); - - gettimeofday(&time_now, NULL); - - byte_count_now = byte_count; - byte_count = 0; - - time_difference = TimevalDiff(&time_now, &time_start); - rate = (float)byte_count_now / time_difference; - fprintf(stderr, "%4.1f MiB / %5.3f sec = %4.1f MiB/second\n", - (byte_count_now / 1e6f), time_difference, (rate / 1e6f) ); +#endif + } else { + sleep(1); + gettimeofday(&time_now, NULL); + + byte_count_now = byte_count; + byte_count = 0; + + time_difference = TimevalDiff(&time_now, &time_start); + rate = (float)byte_count_now / time_difference; + fprintf(stderr, "%4.1f MiB / %5.3f sec = %4.1f MiB/second\n", + (byte_count_now / 1e6f), time_difference, (rate / 1e6f) ); - time_start = time_now; + time_start = time_now; - if (byte_count_now == 0) { - exit_code = EXIT_FAILURE; - fprintf(stderr, "\nCouldn't transfer any bytes for one second.\n"); - break; - } + if (byte_count_now == 0) { + exit_code = EXIT_FAILURE; + fprintf(stderr, "\nCouldn't transfer any bytes for one second.\n"); + break; + } } } diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 59b7eee3..b308cda6 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -26,6 +26,11 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSI #include #include + +#ifdef _WIN32 +/* Avoid redefinition of timespec from time.h (included by libusb.h) */ +#define HAVE_STRUCT_TIMESPEC 1 +#endif #include #ifndef bool @@ -396,7 +401,7 @@ hackrf_device_list_t* ADDCALL hackrf_device_list() serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number)); if( serial_number_length == 32 ) { serial_number[32] = 0; - list->serial_numbers[idx] = strndup(serial_number, serial_number_length); + list->serial_numbers[idx] = strdup(serial_number); } libusb_close(usb_device); diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 296be3d8..3db40f5c 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -216,8 +216,8 @@ extern ADDAPI int ADDCALL hackrf_init_sweep(hackrf_device* device, int length, uint32_t dwell_time); /* Operacake functions */ -int ADDCALL hackrf_get_operacake_boards(hackrf_device* device, uint8_t* boards); -int ADDCALL hackrf_set_operacake_ports(hackrf_device* device, +extern ADDAPI int ADDCALL hackrf_get_operacake_boards(hackrf_device* device, uint8_t* boards); +extern ADDAPI int ADDCALL hackrf_set_operacake_ports(hackrf_device* device, uint8_t address, uint8_t port_a, uint8_t port_b); From a23f4676c63ac254183ad6fe9c4f82b61d1062e2 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:15:40 -0700 Subject: [PATCH 2/7] Initial appveyor config --- appveyor.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 00000000..d118996b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,27 @@ +os: Visual Studio 2015 +clone_depth: 1 + +configuration: + - Release + - Debug + +install: + - appveyor DownloadFile "https://downloads.sourceforge.net/project/libusb/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Flibusb%2Ffiles%2Flibusb-1.0%2Flibusb-1.0.21%2F&ts=1485478643&use_mirror=cytranet" -FileName "C:\libusb.7z" + - 7z x -y "C:\libusb.7z" -o "C:\libusb" + - appveyor DownloadFile "ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip" -FileName "C:\pthreads-w32-release.zip" + - 7z x -y "C:\pthreads-w32-release.zip" -o "C:\pthreads" + - appveyor DownloadFile "http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/pkg-config_0.26-1_win32.zip" -FileName "C:\pkg-config_win32.zip" + - 7z x -y "C:\pkg-config_win32.zip" -o "C:\pkg-config" + + +build_script: + - mkdir c:\projects\hackrf\host\build + - cd c:\projects\hackrf\host\build + - cmake -G "Visual Studio 14 2015" \ + -DLIBUSB_LIBRARIES="C:\\libusb\\MS64\\dll\\libusb-1.0.lib" \ + -DLIBUSB_INCLUDE_DIRS="C:\\libusb\\include\\libusb-1.0" \ + -DTHREADS_PTHREADS_INCLUDE_DIR=c:\pthreads\Pre-built.2\include \ + -DTHREADS_PTHREADS_WIN32_LIBRARY=c:\pthreads\Pre-built.2\lib\x64\pthreadVC2.lib \ + -DPKG_CONFIG_EXECUTABLE="C:\\pkg-config\\bin\\pkg-config.exe" \ + .. + - msbuild .\ALL_BUILD.vcxproj /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" From f433b60d4c8ed46552b95339f092f5a31687d212 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:18:33 -0700 Subject: [PATCH 3/7] Fix 7zip command line args --- appveyor.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d118996b..1167d8f0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,11 +7,11 @@ configuration: install: - appveyor DownloadFile "https://downloads.sourceforge.net/project/libusb/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Flibusb%2Ffiles%2Flibusb-1.0%2Flibusb-1.0.21%2F&ts=1485478643&use_mirror=cytranet" -FileName "C:\libusb.7z" - - 7z x -y "C:\libusb.7z" -o "C:\libusb" + - 7z x -y "C:\libusb.7z" -o"C:\libusb" - appveyor DownloadFile "ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip" -FileName "C:\pthreads-w32-release.zip" - - 7z x -y "C:\pthreads-w32-release.zip" -o "C:\pthreads" + - 7z x -y "C:\pthreads-w32-release.zip" -o"C:\pthreads" - appveyor DownloadFile "http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/pkg-config_0.26-1_win32.zip" -FileName "C:\pkg-config_win32.zip" - - 7z x -y "C:\pkg-config_win32.zip" -o "C:\pkg-config" + - 7z x -y "C:\pkg-config_win32.zip" -o"C:\pkg-config" build_script: From b453b915329840e6af1f307d089e9991e0d7fe09 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:21:18 -0700 Subject: [PATCH 4/7] Use http rather than ftp to download dependencies --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 1167d8f0..664bba4d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -8,7 +8,7 @@ configuration: install: - appveyor DownloadFile "https://downloads.sourceforge.net/project/libusb/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Flibusb%2Ffiles%2Flibusb-1.0%2Flibusb-1.0.21%2F&ts=1485478643&use_mirror=cytranet" -FileName "C:\libusb.7z" - 7z x -y "C:\libusb.7z" -o"C:\libusb" - - appveyor DownloadFile "ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip" -FileName "C:\pthreads-w32-release.zip" + - appveyor DownloadFile "http://mirrors.kernel.org/sourceware/pthreads-win32/pthreads-w32-2-9-1-release.zip" -FileName "C:\pthreads-w32-release.zip" - 7z x -y "C:\pthreads-w32-release.zip" -o"C:\pthreads" - appveyor DownloadFile "http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/pkg-config_0.26-1_win32.zip" -FileName "C:\pkg-config_win32.zip" - 7z x -y "C:\pkg-config_win32.zip" -o"C:\pkg-config" From e5811b752c41cd65b4f72c45f5cdb99709292a69 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:27:34 -0700 Subject: [PATCH 5/7] Correct libusb include dir argument to cmake --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 664bba4d..f0f73939 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -19,7 +19,7 @@ build_script: - cd c:\projects\hackrf\host\build - cmake -G "Visual Studio 14 2015" \ -DLIBUSB_LIBRARIES="C:\\libusb\\MS64\\dll\\libusb-1.0.lib" \ - -DLIBUSB_INCLUDE_DIRS="C:\\libusb\\include\\libusb-1.0" \ + -DLIBUSB_INCLUDE_DIR="C:\\libusb\\include\\libusb-1.0" \ -DTHREADS_PTHREADS_INCLUDE_DIR=c:\pthreads\Pre-built.2\include \ -DTHREADS_PTHREADS_WIN32_LIBRARY=c:\pthreads\Pre-built.2\lib\x64\pthreadVC2.lib \ -DPKG_CONFIG_EXECUTABLE="C:\\pkg-config\\bin\\pkg-config.exe" \ From fbf78f48cb7cbde6e7ae6970c05a46434004e4b3 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:32:30 -0700 Subject: [PATCH 6/7] Remove unnecessary escaping --- appveyor.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index f0f73939..030cddaa 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,10 +18,10 @@ build_script: - mkdir c:\projects\hackrf\host\build - cd c:\projects\hackrf\host\build - cmake -G "Visual Studio 14 2015" \ - -DLIBUSB_LIBRARIES="C:\\libusb\\MS64\\dll\\libusb-1.0.lib" \ - -DLIBUSB_INCLUDE_DIR="C:\\libusb\\include\\libusb-1.0" \ + -DLIBUSB_LIBRARIES="C:\libusb\MS64\dll\libusb-1.0.lib" \ + -DLIBUSB_INCLUDE_DIR="C:\libusb\include\libusb-1.0" \ -DTHREADS_PTHREADS_INCLUDE_DIR=c:\pthreads\Pre-built.2\include \ -DTHREADS_PTHREADS_WIN32_LIBRARY=c:\pthreads\Pre-built.2\lib\x64\pthreadVC2.lib \ - -DPKG_CONFIG_EXECUTABLE="C:\\pkg-config\\bin\\pkg-config.exe" \ + -DPKG_CONFIG_EXECUTABLE="C:\pkg-config\bin\pkg-config.exe" \ .. - msbuild .\ALL_BUILD.vcxproj /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" From a5f8cdf7cadf5655153084f34a2cdbf3d3da8760 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 26 Jan 2017 18:38:33 -0700 Subject: [PATCH 7/7] Build Win64 version --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 030cddaa..4869b2a0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,7 +17,7 @@ install: build_script: - mkdir c:\projects\hackrf\host\build - cd c:\projects\hackrf\host\build - - cmake -G "Visual Studio 14 2015" \ + - cmake -G "Visual Studio 14 2015 Win64" \ -DLIBUSB_LIBRARIES="C:\libusb\MS64\dll\libusb-1.0.lib" \ -DLIBUSB_INCLUDE_DIR="C:\libusb\include\libusb-1.0" \ -DTHREADS_PTHREADS_INCLUDE_DIR=c:\pthreads\Pre-built.2\include \