Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libsddc refrector #229

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ project(SDDC VERSION 1.0.1)

include(CTest)

include(./cmake/CheckGit.cmake)
CheckGitSetup()

message(STATUS "CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}")

### build options
Expand Down Expand Up @@ -62,10 +65,10 @@ if (MSVC)
else()

if (USE_DEBUG_ASAN)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
set(CMAKE_CXX_FLAGS "-O0 -std=c++17 -Wall -Werror")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address")
set(CMAKE_CXX_FLAGS "-O0 -g -std=c++17 -Wall -Werror")
else()
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -Wall -Werror")
set(CMAKE_CXX_FLAGS "-O3 -g -std=c++17 -Wall -Werror")
endif(USE_DEBUG_ASAN)
#add_compile_options(-Wall -Wextra -pedantic)
include(FindPkgConfig)
Expand Down
2 changes: 0 additions & 2 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cmake_minimum_required(VERSION 3.13)

if (MSVC)
file(GLOB ARCH_SRC "arch/win32/*.cpp" "arch/win32/CyAPI/*.cpp")
else()
Expand Down
2 changes: 1 addition & 1 deletion Core/FX3Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class fx3class
{
public:
virtual ~fx3class(void) {}
virtual bool Open(const uint8_t* fw_data, uint32_t fw_size) = 0;
virtual bool Open(int index, const uint8_t* fw_data, uint32_t fw_size) = 0;
virtual bool Control(FX3Command command, uint8_t data = 0) = 0;
virtual bool Control(FX3Command command, uint32_t data) = 0;
virtual bool Control(FX3Command command, uint64_t data) = 0;
Expand Down
15 changes: 12 additions & 3 deletions Core/arch/linux/FX3handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include "FX3handler.h"
#include "usb_device.h"

struct usb_device_info *devlist;
int devlist_n;

fx3class* CreateUsbHandler()
{
devlist_n = usb_device_get_device_list(&devlist);
return new fx3handler();
}

Expand All @@ -16,9 +20,9 @@ fx3handler::~fx3handler()
{
}

bool fx3handler::Open(const uint8_t* fw_data, uint32_t fw_size)
bool fx3handler::Open(int index, const uint8_t* fw_data, uint32_t fw_size)
{
dev = usb_device_open(0, (const char*)fw_data, fw_size);
dev = usb_device_open(index, (const char*)fw_data, fw_size);

return dev != nullptr;
}
Expand Down Expand Up @@ -96,5 +100,10 @@ bool fx3handler::ReadDebugTrace(uint8_t* pdata, uint8_t len)

bool fx3handler::Enumerate(unsigned char &idx, char *lbuf, const uint8_t* fw_data, uint32_t fw_size)
{
return true; // TBD
if (idx > devlist_n - 1)
return false;

sprintf(lbuf, "%s sn:%s", devlist[idx].product, devlist[idx].serial_number);

return true;
}
2 changes: 1 addition & 1 deletion Core/arch/linux/FX3handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class fx3handler : public fx3class
public:
fx3handler();
virtual ~fx3handler(void);
bool Open(const uint8_t* fw_data, uint32_t fw_size) override;
bool Open(int index, const uint8_t* fw_data, uint32_t fw_size) override;
bool Control(FX3Command command, uint8_t data) override;
bool Control(FX3Command command, uint32_t data) override;
bool Control(FX3Command command, uint64_t data) override;
Expand Down
4 changes: 2 additions & 2 deletions Core/arch/win32/FX3handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ bool fx3handler::Enumerate(unsigned char& idx, char* lbuf, const uint8_t* fw_dat
strcat(lbuf, "sn:");
strcat(lbuf, wchar2char((wchar_t*)fx3dev->SerialNumber));
fx3dev->Close();
devidx = idx; // -> devidx
return true;
}

bool fx3handler::Open(const uint8_t* fw_data, uint32_t fw_size) {
bool fx3handler::Open(int index, const uint8_t* fw_data, uint32_t fw_size) {
bool r = false;

devidx = index;
if (!GetFx3DeviceStreamer()) {
DbgPrintf("Failed to open device\n");
return r;
Expand Down
2 changes: 1 addition & 1 deletion Core/arch/win32/FX3handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class fx3handler : public fx3class
fx3handler();
virtual ~fx3handler(void);

bool Open(const uint8_t* fw_data, uint32_t fw_size);
bool Open(int idx, const uint8_t* fw_data, uint32_t fw_size);
bool IsOn() { return Fx3IsOn; }
bool Control(FX3Command command, uint8_t data);
bool Control(FX3Command command, uint32_t data = 0);
Expand Down
6 changes: 3 additions & 3 deletions ExtIO_sddc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")

add_library(ExtIO_SDDC SHARED ${SRC})

target_include_directories(ExtIO_SDDC PUBLIC "${LIBFFTW_INCLUDE_DIR}")
target_link_directories(ExtIO_SDDC PUBLIC "${LIBFFTW_LIBRARY_DIRS}")
target_include_directories(ExtIO_SDDC PUBLIC ${LIBFFTW_INCLUDE_DIR})
target_link_directories(ExtIO_SDDC PUBLIC ${LIBFFTW_LIBRARY_DIRS})
target_link_libraries(ExtIO_SDDC PUBLIC ${LIBFFTW_LIBRARIES})


target_link_libraries(ExtIO_SDDC PRIVATE git_version)
target_link_libraries(ExtIO_SDDC PRIVATE SDDC_CORE)
target_link_libraries(ExtIO_SDDC PUBLIC Setupapi.lib)
set_target_properties(ExtIO_SDDC PROPERTIES PREFIX "")
Expand Down
8 changes: 4 additions & 4 deletions ExtIO_sddc/ExtIO_sddc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "PScope_uti.h"
#include "r2iq.h"

#include "git_version.h"

#define snprintf _snprintf

#define DEFAULT_TUNE_FREQ 999000.0 /* Turin MW broadcast ! */
Expand Down Expand Up @@ -235,10 +237,8 @@ bool __declspec(dllexport) __stdcall InitHW(char *name, char *model, int& type)
selected = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SELECTDEVICE), NULL, DlgSelectDevice, (LPARAM) &devicelist);
}
DbgPrintf("selected %d \n",selected);
idx = selected;
Fx3->Enumerate(idx, devicelist.dev[idx], res_data, res_size);

gbInitHW = Fx3->Open(res_data, res_size) &&
gbInitHW = Fx3->Open(selected, res_data, res_size) &&
RadioHandler.Init(Fx3, Callback); // Check if it there hardware

#ifdef _DEBUG
Expand Down Expand Up @@ -358,7 +358,7 @@ int EXTIO_API StartHWdbl(double LOfreq)
uint8_t hb, lb;
hb = fw >> 8;
lb = (uint8_t) fw;
sprintf(ebuffer, "%s v%s | FX3 v%d.%02d | %s ",SWNAME, SWVERSION ,hb,lb, RadioHandler.getName() );
sprintf(ebuffer, "%s v%s (%s) | %s ", SWNAME, SWVERSION, kGitHash, RadioHandler.getName());
SetWindowText(h_dialog, ebuffer);
EXTIO_STATUS_CHANGE(pfnCallback, extHw_RUNNING);
}
Expand Down
81 changes: 81 additions & 0 deletions cmake/CheckGit.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
set(CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR})
if (NOT DEFINED pre_configure_dir)
set(pre_configure_dir ${CMAKE_CURRENT_LIST_DIR})
endif ()

if (NOT DEFINED post_configure_dir)
set(post_configure_dir ${CMAKE_BINARY_DIR}/generated)
endif ()

set(pre_configure_file ${pre_configure_dir}/git_version.cpp.in)
set(post_configure_file ${post_configure_dir}/git_version.cpp)

function(CheckGitWrite git_hash)
file(WRITE ${CMAKE_BINARY_DIR}/git-state.txt ${git_hash})
endfunction()

function(CheckGitRead git_hash)
if (EXISTS ${CMAKE_BINARY_DIR}/git-state.txt)
file(STRINGS ${CMAKE_BINARY_DIR}/git-state.txt CONTENT)
LIST(GET CONTENT 0 var)

set(${git_hash} ${var} PARENT_SCOPE)
endif ()
endfunction()

function(CheckGitVersion)
# Get the latest abbreviated commit hash of the working branch
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

CheckGitRead(GIT_HASH_CACHE)
if (NOT EXISTS ${post_configure_dir})
file(MAKE_DIRECTORY ${post_configure_dir})
endif ()

if (NOT EXISTS ${post_configure_dir}/git_version.h)
file(COPY ${pre_configure_dir}/git_version.h DESTINATION ${post_configure_dir})
endif()

if (NOT DEFINED GIT_HASH_CACHE)
set(GIT_HASH_CACHE "INVALID")
endif ()

# Only update the git_version.cpp if the hash has changed. This will
# prevent us from rebuilding the project more than we need to.
if (NOT ${GIT_HASH} STREQUAL ${GIT_HASH_CACHE} OR NOT EXISTS ${post_configure_file})
# Set che GIT_HASH_CACHE variable the next build won't have
# to regenerate the source file.
CheckGitWrite(${GIT_HASH})

configure_file(${pre_configure_file} ${post_configure_file} @ONLY)
endif ()

endfunction()

function(CheckGitSetup)

add_custom_target(AlwaysCheckGit COMMAND ${CMAKE_COMMAND}
-DRUN_CHECK_GIT_VERSION=1
-Dpre_configure_dir=${pre_configure_dir}
-Dpost_configure_file=${post_configure_dir}
-DGIT_HASH_CACHE=${GIT_HASH_CACHE}
-P ${CURRENT_LIST_DIR}/CheckGit.cmake
BYPRODUCTS ${post_configure_file}
)

add_library(git_version ${CMAKE_BINARY_DIR}/generated/git_version.cpp)
target_include_directories(git_version PUBLIC ${CMAKE_BINARY_DIR}/generated)
add_dependencies(git_version AlwaysCheckGit)

CheckGitVersion()
endfunction()

# This is used to run this function from an external cmake process.
if (RUN_CHECK_GIT_VERSION)
CheckGitVersion()
endif ()
89 changes: 89 additions & 0 deletions cmake/FileEmbed.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function(FileEmbedSetup)

if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}file_embed)
endif ()

if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c)
file(WRITE ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c "")
endif ()

add_library(file_embed ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c)
target_include_directories(file_embed PUBLIC ${CMAKE_BINARY_DIR}/file_embed)

endfunction()

function(FileEmbedAdd file)
FileEmbedGenerate(${file} var)
target_sources(file_embed PUBLIC ${var})

add_custom_command(
OUTPUT ${var}
COMMAND ${CMAKE_COMMAND}
-DRUN_FILE_EMBED_GENERATE=1
-DFILE_EMBED_GENERATE_PATH=${file}
-P ${CMAKE_SOURCE_DIR}/cmake/FileEmbed.cmake
MAIN_DEPENDENCY ${file}
)
endfunction()

function(FileEmbedGenerate file generated_c)

get_filename_component(base_filename ${file} NAME)
set(output_filename "${base_filename}.c")
string(MAKE_C_IDENTIFIER ${base_filename} c_name)
file(READ ${file} content HEX)
message(${content})

# Separate into individual bytes.
string(REGEX MATCHALL "([A-Fa-f0-9][A-Fa-f0-9])" SEPARATED_HEX ${content})

set(output_c "")

set(counter 0)
foreach (hex IN LISTS SEPARATED_HEX)
string(APPEND output_c "0x${hex},")
MATH(EXPR counter "${counter}+1")
if (counter GREATER 16)
string(APPEND output_c "\n ")
set(counter 0)
endif ()
endforeach ()

set(output_c "
#include \"${c_name}.h\"
uint8_t ${c_name}_data[] = {
${output_c}
}\;
unsigned ${c_name}_size = sizeof(${c_name}_data)\;
")

set(output_h "
#ifndef ${c_name}_H
#define ${c_name}_H
#include \"stdint.h\"
extern uint8_t ${c_name}_data[]\;
extern unsigned ${c_name}_size\;
#endif // ${c_name}_H
")


if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}file_embed)
endif ()


file(WRITE ${CMAKE_BINARY_DIR}/file_embed/${c_name}.c
${output_c})

file(WRITE ${CMAKE_BINARY_DIR}/file_embed/${c_name}.h
${output_h})

set(${generated_c} ${CMAKE_BINARY_DIR}/file_embed/${c_name}.c PARENT_SCOPE)

endfunction()

if (RUN_FILE_EMBED_GENERATE)
FileEmbedGenerate(${FILE_EMBED_GENERATE_PATH} var)
endif ()

2 changes: 2 additions & 0 deletions cmake/git_version.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "git_version.h"
const char *kGitHash = "@GIT_HASH@";
7 changes: 7 additions & 0 deletions cmake/git_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef GIT_VERSION_H
#define GIT_VERSION_H

extern const char *kGitHash;

#endif // GIT_VERSION_H

15 changes: 11 additions & 4 deletions libsddc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ if (MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif (MSVC)


include(../cmake/FileEmbed.cmake)

FileEmbedSetup()
FileEmbedAdd(${CMAKE_SOURCE_DIR}/SDDC_FX3.img)

include_directories("." "../Core")

add_library(sddc SHARED
Expand All @@ -13,14 +19,15 @@ add_library(sddc SHARED
if (MSVC)
target_link_libraries(sddc PUBLIC Setupapi.lib)
else()
target_include_directories(sddc PUBLIC "${LIBUSB_INCLUDE_DIR}")
target_link_directories(sddc PUBLIC "${LIBUSB_LIBRARY_DIRS}")
target_include_directories(sddc PUBLIC ${LIBUSB_INCLUDE_DIR})
target_link_directories(sddc PUBLIC ${LIBUSB_LIBRARY_DIRS})
target_link_libraries(sddc PUBLIC ${LIBUSB_LIBRARIES})
endif (MSVC)

target_include_directories(sddc PUBLIC "${LIBFFTW_INCLUDE_DIR}")
target_link_directories(sddc PUBLIC "${LIBFFTW_LIBRARY_DIRS}")
target_include_directories(sddc PUBLIC ${LIBFFTW_INCLUDE_DIR})
target_link_directories(sddc PUBLIC ${LIBFFTW_LIBRARY_DIRS})
target_link_libraries(sddc PUBLIC ${LIBFFTW_LIBRARIES})
target_link_libraries(sddc PUBLIC file_embed)

target_link_libraries(sddc PRIVATE SDDC_CORE)
set_target_properties(sddc PROPERTIES VERSION ${PROJECT_VERSION})
Expand Down
Loading