diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 69898c6..0000000 --- a/.clang-format +++ /dev/null @@ -1,2 +0,0 @@ -DerivePointerAlignment: false -PointerAlignment: Left diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..90f76bb --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,12 @@ +Checks: "readability-identifier-naming" +HeaderFileExtensions: ["h"] +ImplementationFileExtensions: ["c"] +ExcludeHeaderFilterRegex: "./lib/epos2/definitions.h" +CheckOptions: + readability-identifier-naming.DefaultCase: lower_case + readability-identifier-naming.ParameterCase: lower_case + readability-identifier-naming.MacroDefinitionCase: UPPER_CASE + readability-identifier-naming.GlobalConstantCase: UPPER_CASE + readability-identifier-naming.GlobalConstantPointerCase: UPPER_CASE + readability-identifier-naming.EnumCase: UPPER_CASE + readability-identifier-naming.EnumConstantCase: UPPER_CASE diff --git a/.github/workflows/build-cmake.yml b/.github/workflows/build-cmake.yml index 1e9c779..b5d5569 100644 --- a/.github/workflows/build-cmake.yml +++ b/.github/workflows/build-cmake.yml @@ -27,20 +27,17 @@ jobs: strategy: fail-fast: true matrix: - compiler: ["gcc", "clang"] + preset: ["linux-gcc", "linux-clang"] steps: - uses: actions/checkout@v5 - name: Install Build Dependencies run: echo | python "${{ github.workspace }}/bin/acquire_epos_cmdlib.py" - name: Setup CMake run: | - CC=${{ matrix.compiler }} cmake -S . -B build + cmake -S . -B build --preset=${{ matrix.preset }} - name: Build run: | - CC=${{ matrix.compiler }} cmake --build build - - name: Verbosely Test - run: | - ctest --verbose --output-on-failure --test-dir build/test + cmake --build build windows-build-cmake: name: Windows @@ -48,7 +45,7 @@ jobs: strategy: fail-fast: true matrix: - compiler: ["gcc.exe"] #, "cl.exe"] + preset: ["windows-mingw"] #, "cl.exe"] steps: - uses: actions/checkout@v5 #- uses: ilammy/msvc-dev-cmd@v1.13.0 @@ -56,14 +53,7 @@ jobs: run: echo "" | python "${{ github.workspace }}/bin/acquire_epos_cmdlib.py" - name: Setup CMake run: | - cmake -S . -B build - env: - CC: ${{ matrix.compiler }} + cmake -S . -B build --preset=${{ matrix.preset }} - name: Build run: | cmake --build build - env: - CC: ${{ matrix.compiler }} - - name: Verbosely Test - run: | - ctest --verbose --output-on-failure --test-dir build/test -C Debug diff --git a/.github/workflows/build-meson.yml b/.github/workflows/build-meson.yml deleted file mode 100644 index ad79464..0000000 --- a/.github/workflows/build-meson.yml +++ /dev/null @@ -1,80 +0,0 @@ -name: Meson - -on: - pull_request: - branches: ["main"] - paths: - - ".github/workflows/build-meson.yml" - - "ipc/**/*.c" - - "ipc/**/*.h" - - "test/*.c" - - "**/meson.build" - push: - branches: ["main"] - paths: - - ".github/workflows/build-meson.yml" - - "ipc/**/*.c" - - "ipc/**/*.h" - - "test/*.c" - - "**/meson.build" - -jobs: - linux-build-meson: - name: Linux - runs-on: ubuntu-latest - strategy: - fail-fast: true - matrix: - compiler: ["gcc", "clang"] - steps: - - uses: actions/checkout@v5 - - uses: actions/setup-python@v6 - with: - python-version: "3.13.0" - - name: Install Build Dependencies - run: | - pip3 install meson ninja - sudo apt install patchelf -y - echo | python "${{ github.workspace }}/bin/acquire_epos_cmdlib.py" - - name: Setup Meson - run: | - CC=${{ matrix.compiler }} meson setup build - - name: Build - run: | - CC=${{ matrix.compiler }} meson compile -C build - - name: Verbosely Test - run: | - LD_LIBRARY_PATH=$(realpath build/test) meson test -v -C build - # above is a HACK!! - - windows-build-meson: - name: Windows - runs-on: windows-latest - strategy: - fail-fast: true - matrix: - compiler: ["gcc.exe"] #, "cl.exe"] - steps: - - uses: actions/checkout@v5 - - uses: actions/setup-python@v6 - with: - python-version: "3.13.0" - #- uses: ilammy/msvc-dev-cmd@v1.13.0 - - name: Install Build Dependencies - shell: "pwsh" - run: | - pip3 install meson ninja - echo "" | python "${{ github.workspace }}/bin/acquire_epos_cmdlib.py" - - name: Setup Meson - run: | - meson setup build - env: - CC: ${{ matrix.compiler }} - - name: Build - run: | - meson compile -C build - env: - CC: ${{ matrix.compiler }} - - name: Verbosely Test - run: | - meson test -v -C build diff --git a/CMakeLists.txt b/CMakeLists.txt index fe0924f..a72a54d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,20 @@ cmake_minimum_required(VERSION 3.30..4.0) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") + project(libtestrig LANGUAGES C) -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") - include(target_copy_dll) +include(target_output_to_bin) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib") set(IPC_DIR "${LIB_DIR}/ipc") set(EPOS_DIR "${LIB_DIR}/epos2") +set(BIN_DIR "${CMAKE_SOURCE_DIR}/bin") set(IPC_SOURCES "${IPC_DIR}/ipc.c" - "${IPC_DIR}/constants.c" "${IPC_DIR}/message.c" "${IPC_DIR}/os.c") @@ -23,25 +25,38 @@ set(EPOS_SOURCES "${EPOS_DIR}/manage.c" "${EPOS_DIR}/identify.c") -set(LIBTESTRIG_SOURCES ${IPC_SOURCES}) +set(LIBTESTRIG_SOURCES ${IPC_SOURCES} ${EPOS_SOURCES}) + +if (WIN32) + add_library(EposCmd SHARED IMPORTED) + set_target_properties(EposCmd PROPERTIES + IMPORTED_LOCATION "${BIN_DIR}/EposCmd64.dll" + IMPORTED_IMPLIB "${BIN_DIR}/EposCmd64.lib") -find_package(EPOSCmd) -if (EPOSCmd_FOUND) - list(APPEND LIBTESTRIG_SOURCES ${EPOS_SOURCES}) + set(EposCmd-FOUND TRUE) else() - message("Controller capabilities not being built.") + add_library(EposCmd SHARED IMPORTED) + set_target_properties(EposCmd PROPERTIES + IMPORTED_LOCATION "${BIN_DIR}/libEposCmd.so" + IMPORTED_IMPLIB "") + + set(EposCmd-FOUND TRUE) endif() -add_library(testrig SHARED ${LIBTESTRIG_SOURCES}) -target_compile_definitions(testrig PRIVATE COMPILING_TESTRIG_DLL) -target_include_directories(testrig PUBLIC ${LIB_DIR}) +add_library(libtestrig SHARED ${LIBTESTRIG_SOURCES}) +target_compile_definitions(libtestrig PRIVATE COMPILING_TESTRIG_DLL) +target_include_directories(libtestrig PUBLIC ${LIB_DIR}) +target_link_libraries(libtestrig PUBLIC EposCmd) +target_output_to_bin(libtestrig) if (WIN32) - target_link_libraries(testrig PRIVATE Ws2_32) + target_link_libraries(libtestrig PUBLIC Ws2_32 Kernel32) endif() -if (EPOSCmd_FOUND) - target_link_libraries(testrig PUBLIC EPOSCmd) +if (MSVC) + add_compile_options("/W4") +else() + add_compile_options("-Wall") endif() -add_subdirectory("${CMAKE_SOURCE_DIR}/test") +add_subdirectory("${CMAKE_SOURCE_DIR}/src") diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..f8acb73 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,74 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 30, + "patch": 0 + }, + + "configurePresets": [ + { + "name": "default", + "hidden": true, + "generator": "Ninja", + "cacheVariables": { + "CMAKE_COLOR_DIAGNOSTICS": "ON" + } + }, + { + "name": "windows-only", + "hidden": true, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + }, + { + "name": "linux-only", + "hidden": true, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + } + }, + + { + "name": "windows-mingw", + "inherits": [ "default", "windows-only" ], + "cacheVariables": { + "CMAKE_C_COMPILER": "gcc" + } + }, + { + "name": "linux-gcc", + "inherits": [ "default", "linux-only" ], + "cacheVariables": { + "CMAKE_C_COMPILER": "gcc" + } + }, + { + "name": "linux-clang", + "inherits": [ "default", "linux-only" ], + "cacheVariables": { + "CMAKE_C_COMPILER": "clang" + } + } + ], + + "buildPresets": [ + { + "name": "windows-mingw", + "configurePreset": "windows-mingw" + }, + { + "name": "linux-gcc", + "configurePreset": "linux-gcc" + }, + { + "name": "linux-clang", + "configurePreset": "linux-clang" + } + ] +} diff --git a/bin/acquire_epos_cmdlib.py b/bin/acquire_epos_cmdlib.py index ccfe078..7bff0a6 100755 --- a/bin/acquire_epos_cmdlib.py +++ b/bin/acquire_epos_cmdlib.py @@ -90,7 +90,7 @@ def attempt_dl(link: str, outf: str|os.PathLike, operating: str) -> int: stripped = f.split(extension)[0] + extension actual_bin = stripped if i_am == "Linux" else f - wherestrip = join(dirs["lib_dir"], stripped) + wherestrip = join(dirs["lib_dir"], actual_bin) os.replace(join(root, f), wherestrip) bins.append(wherestrip) diff --git a/cmake/FindEPOSCmd.cmake b/cmake/FindEPOSCmd.cmake deleted file mode 100644 index 4640904..0000000 --- a/cmake/FindEPOSCmd.cmake +++ /dev/null @@ -1 +0,0 @@ -# TODO diff --git a/cmake/target_output_to_bin.cmake b/cmake/target_output_to_bin.cmake new file mode 100644 index 0000000..2acae95 --- /dev/null +++ b/cmake/target_output_to_bin.cmake @@ -0,0 +1,5 @@ +function(target_output_to_bin IN_TARGET) + set_target_properties(${IN_TARGET} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${BIN_DIR}") + set_target_properties(${IN_TARGET} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${BIN_DIR}") + set_target_properties(${IN_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${BIN_DIR}") +endfunction(target_output_to_bin IN_TARGET) \ No newline at end of file diff --git a/lib/.clang-tidy b/lib/.clang-tidy new file mode 100644 index 0000000..4529d43 --- /dev/null +++ b/lib/.clang-tidy @@ -0,0 +1,6 @@ +Checks: "readability-identifier-naming" +HeaderFileExtensions: ["h"] +ImplementationFileExtensions: ["c"] +ExcludeHeaderFilterRegex: "./lib/epos2/definitions.h" +CheckOptions: + readability-identifier-naming.FunctionPrefix: vscl_ diff --git a/lib/epos2/connect.c b/lib/epos2/connect.c index e6f57fd..f801c31 100644 --- a/lib/epos2/connect.c +++ b/lib/epos2/connect.c @@ -6,78 +6,78 @@ #include "definitions.h" #include "identify.h" -static void FailedOpenDevice(uint32_t error_code) { - PrintError(error_code); +static void vscl_failed_open_device(uint32_t error_code) { + vscl_print_error(error_code); printf("Failed to open device with with following characteristics:\n"); } -uint32_t InitializeDevice(struct Controller* controller_out, void* node, uint8_t node_id) { +uint32_t vscl_initialize_device(struct controller* controller_out, void* node, vscl_byte_t node_id) { uint32_t error_code = 0; - node = VCS_OpenDevice(controller_out->Name, - controller_out->Protocol, - controller_out->Interface, - controller_out->Port, + node = VCS_OpenDevice(controller_out->name, + controller_out->protocol, + controller_out->conn_interface, + controller_out->port, &error_code); - controller_out->NodeId = node_id; + controller_out->node_id = node_id; if (node == 0 || error_code != 0) { - FailedOpenDevice(error_code); - PrintControllerCharacteristics(controller_out); + vscl_failed_open_device(error_code); + vscl_print_characteristics(controller_out); return error_code; } - error_code = CleanEnableDevice(controller_out, node); + error_code = vscl_clean_enable_device(controller_out, node); return error_code; } -uint32_t InitializeDevices(struct Controller controllers_out[], void* handles_out[], uint8_t num) { +uint32_t vscl_initialize_devices(struct controller controllers_out[], void* handles_out[], vscl_byte_t num) { if (num < 1) { printf("Invalid device array size.\n"); return 0x06040043; } // "General Parameter Error" uint32_t error_code = 0; - handles_out[0] = VCS_OpenDevice(controllers_out[0].Name, - controllers_out[0].Protocol, - controllers_out[0].Interface, - controllers_out[0].Port, + handles_out[0] = VCS_OpenDevice(controllers_out[0].name, + controllers_out[0].protocol, + controllers_out[0].conn_interface, + controllers_out[0].port, &error_code); - controllers_out[0].NodeId = 1; + controllers_out[0].node_id = 1; if (handles_out[0] == 0 || error_code != 0) { - FailedOpenDevice(error_code); - PrintControllerCharacteristics(&controllers_out[0]); + vscl_failed_open_device(error_code); + vscl_print_characteristics(&controllers_out[0]); return error_code; } - error_code = CleanEnableDevice(&controllers_out[0], handles_out[0]); + error_code = vscl_clean_enable_device(&controllers_out[0], handles_out[0]); if (error_code != 0) { printf("While attempting to open gateway:\n\t"); - PrintError(error_code); + vscl_print_error(error_code); return error_code; } - for (uint8_t i = 1; i < num; i++) { + for (vscl_byte_t i = 1; i < num; i++) { if (handles_out[i] == NULL) { break; } handles_out[i] = VCS_OpenSubDevice(handles_out[i], - controllers_out[i].Name, - controllers_out[i].Protocol, + controllers_out[i].name, + controllers_out[i].protocol, &error_code); - controllers_out[i].NodeId = i + 1; + controllers_out[i].node_id = i + 1; if (handles_out[i] == 0 || error_code != 0) { - FailedOpenDevice(error_code); - PrintControllerCharacteristics(&controllers_out[i]); + vscl_failed_open_device(error_code); + vscl_print_characteristics(&controllers_out[i]); return error_code; } - error_code = CleanEnableDevice(&controllers_out[i], handles_out[i]); + error_code = vscl_clean_enable_device(&controllers_out[i], handles_out[i]); if (error_code != 0) { printf("While attempting to open gateway:\n\t"); - PrintError(error_code); + vscl_print_error(error_code); return error_code; } } @@ -85,11 +85,11 @@ uint32_t InitializeDevices(struct Controller controllers_out[], void* handles_ou return 0; } // uint32_t InitializeThreeDevices -uint32_t CleanEnableDevice(struct Controller* controller, void* device_handle) { +uint32_t vscl_clean_enable_device(struct controller* controller, void* device_handle) { if (device_handle == 0) { printf("At head of CleanEnableDevice():\n"); printf("\tERROR: Invalid device handle passed for %s at node %ihh.\n", - controller->Name, controller->NodeId); + controller->name, controller->node_id); return 0x2000000B; } @@ -97,23 +97,23 @@ uint32_t CleanEnableDevice(struct Controller* controller, void* device_handle) { uint32_t error_code = 0; int ret; - ret = VCS_ClearFault(device_handle, controller->NodeId, &error_code); + ret = VCS_ClearFault(device_handle, controller->node_id, &error_code); if (ret == 0) { - printf("While clearing fault from %s at %s:\n\t", controller->Name, controller->Port); - PrintError(error_code); + printf("While clearing fault from %s at %s:\n\t", controller->name, controller->port); + vscl_print_error(error_code); return error_code; } - ret = VCS_SetEnableState(device_handle, controller->NodeId, &error_code); - controller->State = CTRL_STATE_OPENED; + ret = VCS_SetEnableState(device_handle, controller->node_id, &error_code); + controller->state = CTRL_STATE_OPENED; return error_code; } -uint32_t CloseDevice(struct Controller* controller, void* device_handle) { +uint32_t vscl_close_device(struct controller* controller, void* device_handle) { if (device_handle == 0) { printf("At head of CloseDevice():\n"); printf("\tERROR: Invalid device handle passed for %s at node %ihh.\n", - controller->Name, controller->NodeId); + controller->name, controller->node_id); return 0x2000000B; } @@ -124,22 +124,22 @@ uint32_t CloseDevice(struct Controller* controller, void* device_handle) { ret = VCS_CloseDevice(device_handle, &error_code); if (ret == 0) { printf("While attempting to close device %s at node %ihh\n\t", - controller->Name, controller->NodeId); - PrintError(error_code); + controller->name, controller->node_id); + vscl_print_error(error_code); return error_code; } - ret = VCS_SetDisableState(device_handle, controller->NodeId, &error_code); - controller->State = CTRL_STATE_CLOSED; + ret = VCS_SetDisableState(device_handle, controller->node_id, &error_code); + controller->state = CTRL_STATE_CLOSED; return error_code; } -uint32_t CloseDevices(struct Controller controllers[], void* device_handles[], uint8_t num) { +uint32_t vscl_close_devices(struct controller controllers[], void* device_handles[], vscl_byte_t num) { if (num < 1) { printf("Invalid device array size.\n"); return 0x06040043; } // General Parameter Error if (device_handles[0] == 0) { printf("At head of CloseDevices():\n"); printf("\tERROR: Invalid gateway device handle passed for %s at node %ihh.\n", - controllers[0].Name, controllers[0].NodeId); + controllers[0].name, controllers[0].node_id); return 0x2000000B; } @@ -148,28 +148,28 @@ uint32_t CloseDevices(struct Controller controllers[], void* device_handles[], u uint32_t error_code = 0; int ret; - for (uint8_t i = num - 1; i == 0; i--) { + for (vscl_byte_t i = num - 1; i == 0; i--) { void* handle = device_handles[i]; if (handle == 0) { continue; } ret = VCS_CloseSubDevice(device_handles[i], &error_code); if (ret == 0) { printf("While closing subdevice %s at node %ihh:\n\t", - controllers[i].Name, controllers[i].NodeId); - PrintError(error_code); + controllers[i].name, controllers[i].node_id); + vscl_print_error(error_code); return error_code; } - ret = VCS_SetDisableState(device_handles[i], controllers[i].NodeId, &error_code); - controllers[i].State = CTRL_STATE_CLOSED; + ret = VCS_SetDisableState(device_handles[i], controllers[i].node_id, &error_code); + controllers[i].state = CTRL_STATE_CLOSED; } ret = VCS_CloseDevice(device_handles[0], &error_code); if (ret == 0) { printf("While closing gateway device %s at node %ihh:\n\t", - controllers[0].Name, controllers[0].NodeId); - PrintError(error_code); + controllers[0].name, controllers[0].node_id); + vscl_print_error(error_code); return error_code; } diff --git a/lib/epos2/connect.h b/lib/epos2/connect.h index 7fbf8a0..27f0045 100644 --- a/lib/epos2/connect.h +++ b/lib/epos2/connect.h @@ -9,13 +9,7 @@ extern "C" { #include "libtestrig_api.h" #include "controller.h" -#ifdef __cplusplus -} // extern "C" (headers) -namespace VSCL::Rig { -extern "C" { -#endif // __cplusplus - -uint32_t TESTRIG_API CleanEnableDevice(struct Controller* controller, void* device_handle); +uint32_t TESTRIG_API vscl_clean_enable_device(struct controller* controller, void* device_handle); /* * @brief Opens communication to the device and sets its state for a clean init. @@ -23,7 +17,7 @@ uint32_t TESTRIG_API CleanEnableDevice(struct Controller* controller, void* devi * Refer to Page 162 of the manual under INITIALIZATION, * which describes several of the methods wrapped here. */ -uint32_t TESTRIG_API InitializeDevice(struct Controller* controller_out, void* node, uint8_t node_id); +uint32_t TESTRIG_API vscl_initialize_device(struct controller* controller_out, void* node, vscl_byte_t node_id); /* * @brief Opens communication to the devices in a gateway configuraton, @@ -32,8 +26,8 @@ uint32_t TESTRIG_API InitializeDevice(struct Controller* controller_out, void* n * Refer to Page 162 of the manual under INITIALIZATION, * which describes several of the methods wrapped here. */ -uint32_t TESTRIG_API InitializeDevices( - struct Controller controllers_out[], void* handles_out[], uint8_t num); +uint32_t TESTRIG_API vscl_initialize_devices( + struct controller controllers_out[], void* handles_out[], vscl_byte_t num); /* * @brief Closes all connections to a single controller @@ -41,16 +35,15 @@ uint32_t TESTRIG_API InitializeDevices( * Refer to Page 162 of the manual under CLOSING PROCEDURE, * which describes several of the methods wrapped here. */ -uint32_t TESTRIG_API CloseDevice(struct Controller* controller, void* device_handle); +uint32_t TESTRIG_API vscl_close_device(struct controller* controller, void* device_handle); /* * @brief Closes connections to multiple controllers in a gateway configuration, * assuming the first handle and controller is the gateway. */ -uint32_t TESTRIG_API CloseDevices( - struct Controller controllers[], void* device_handles[], uint8_t num); +uint32_t TESTRIG_API vscl_close_devices( + struct controller controllers[], void* device_handles[], vscl_byte_t num); #ifdef __cplusplus } // extern "C" -} // namespace VSCL::Rig -#endif // __cplusplus +#endif diff --git a/lib/epos2/controller.h b/lib/epos2/controller.h index b1070ba..2bbaff6 100644 --- a/lib/epos2/controller.h +++ b/lib/epos2/controller.h @@ -4,15 +4,9 @@ extern "C" { #endif // __cplusplus -#include +#include "libtestrig_api.h" -#ifdef __cplusplus -} // extern "C" (headers) -namespace VSCL::Rig { -extern "C" { -#endif // __cplusplus - -enum ControllerState { +enum CONTROLLER_STATE { CTRL_STATE_CLOSED = 1, CTRL_STATE_OPENED = 2, }; @@ -22,22 +16,21 @@ enum ControllerState { * * Refer to the parameters passed to VCS_OpenDevice, Page 13 of the Manual. */ -struct Controller { +struct controller { // The name of the controller. - char Name[8]; + char name[8]; // The name of the communication protocol. - char Protocol[16]; + char protocol[16]; // The name of the connection interface. - char Interface[64]; + char conn_interface[64]; // The name of the port used in connection. - char Port[8]; + char port[8]; // The node of the controller. - uint8_t NodeId; + vscl_byte_t node_id; // State of the controller. - enum ControllerState State; + enum CONTROLLER_STATE state; }; #ifdef __cplusplus } // extern "C" -} // namespace VSCL::Rig #endif // __cplusplus diff --git a/lib/epos2/definitions.c b/lib/epos2/definitions.c index b7e0049..cae5d98 100644 --- a/lib/epos2/definitions.c +++ b/lib/epos2/definitions.c @@ -4,6 +4,7 @@ // Copyright © 2003 - 2021, maxon motor ag. // All rights reserved. // ******************************************************************************************************************* +// NOLINTBEGIN #include "definitions.h" @@ -213,3 +214,4 @@ const unsigned short EG_DLPC_MAIN_LOOP_FILTER_COEFFICIENT_B = 12 const unsigned short EG_DLPC_MAIN_LOOP_FILTER_COEFFICIENT_C = 122; const unsigned short EG_DLPC_MAIN_LOOP_FILTER_COEFFICIENT_D = 123; const unsigned short EG_DLPC_MAIN_LOOP_FILTER_COEFFICIENT_E = 124; +// NOLINTEND diff --git a/lib/epos2/definitions.h b/lib/epos2/definitions.h index c302192..921bdd9 100644 --- a/lib/epos2/definitions.h +++ b/lib/epos2/definitions.h @@ -4,6 +4,7 @@ // Copyright © 2003 - 2021, maxon motor ag. // All rights reserved. // ******************************************************************************************************************* +// NOLINTBEGIN #pragma once @@ -583,3 +584,4 @@ extern const unsigned short EG_DLPC_MAIN_LOOP_FILTER_COEFFICIENT_E; //= 124; #ifdef __cplusplus } // extern "C" #endif +// NOLINTEND diff --git a/lib/epos2/epos2.c b/lib/epos2/epos2.c index de6141f..9e62261 100644 --- a/lib/epos2/epos2.c +++ b/lib/epos2/epos2.c @@ -2,65 +2,65 @@ #include #include "epos2.h" -const char* DEFAULT_NAME = "EPOS2"; +const char* DEFAULT_NAME = "EPOS2"; // NOLINT -int DecodeError(const uint32_t error_code, char* error_msg, uint8_t max_size) { +int vscl_decode_error(const uint32_t error_code, char* error_msg, vscl_byte_t max_size) { int ret = VCS_GetErrorInfo(error_code, error_msg, max_size); if (ret == 0) { printf("ERROR: Failed to decode error\n"); } return ret; } -int PrintError(const uint32_t error_code) { +int vscl_print_error(const uint32_t error_code) { char msg[64] = { 0 }; - int ret = DecodeError(error_code, msg, 64); + int ret = vscl_decode_error(error_code, msg, 64); printf("ERROR 0x%X: %s\n", error_code, msg); return ret; } -uint32_t ResetDevice(void *device_handle, struct Controller *controller_in) { +uint32_t vscl_reset_device(void *device_handle, struct controller* controller_in) { if (device_handle == 0) { return 0x2000000B; } uint32_t error_code = 0; - int ret = VCS_ResetDevice(device_handle, controller_in->NodeId, &error_code); + int ret = VCS_ResetDevice(device_handle, controller_in->node_id, &error_code); if (ret == 0) { - PrintError(error_code); - printf("Device failed to be reset: %s\n", controller_in->Name); + vscl_print_error(error_code); + printf("Device failed to be reset: %s\n", controller_in->name); } return error_code; } -uint32_t SetupTestrigAsCANGateway(struct Controller controllers[3], void* handles[3], int default_init) { +uint32_t vscl_setup_can_gateway(struct controller controllers[3], void* handles[3], int default_init) { uint32_t error_code = 0; if (default_init != 0) { - for (uint8_t i = 0; i < 3; i++) { - struct Controller controller = controllers[i]; - strncpy(controller.Name, "EPOS4", 6); // Initial testing shows this - strncpy(controller.Protocol, "CANopen", 8); - strncpy(controller.Interface, "", 1); // TOOD: discover this when the cables are here - strncpy(controller.Port, "CAN0", 5); // TODO: discover when cables arrive + for (vscl_byte_t i = 0; i < 3; i++) { + struct controller controller = controllers[i]; + strncpy(controller.name, "EPOS4", 6); // Initial testing shows this + strncpy(controller.protocol, "CANopen", 8); + strncpy(controller.conn_interface, "", 1); // TOOD: discover this when the cables are here + strncpy(controller.port, "CAN0", 5); // TODO: discover when cables arrive } } } - error_code = InitializeDevices(controllers, handles, 3); + error_code = vscl_initialize_devices(controllers, handles, 3); if (error_code != 0) { printf("When initializing multiple devices: "); - PrintError(error_code); + vscl_print_error(error_code); return error_code; } return error_code; } -uint32_t CleanupTestrig(struct Controller controllers[3], void* handles[3]) { +uint32_t vscl_cleanup_testrig(struct controller controllers[3], void* handles[3]) { uint32_t error_code = 0; - error_code = CloseDevices(controllers, handles, 3); + error_code = vscl_close_devices(controllers, handles, 3); if (error_code != 0) { printf("When cleaning up multiple devices: "); - PrintError(error_code); + vscl_print_error(error_code); return error_code; } diff --git a/lib/epos2/epos2.h b/lib/epos2/epos2.h index 1846e32..74cd849 100644 --- a/lib/epos2/epos2.h +++ b/lib/epos2/epos2.h @@ -8,40 +8,38 @@ #include "manage.h" #ifdef __cplusplus -namespace VSCL::Rig { extern "C" { #endif // __cplusplus -extern const char* DEFAULT_NAME; +extern const char* default_name; /* * @brief Decode a given error code to its corresponding message. */ -int TESTRIG_API DecodeError(const uint32_t error_code, char* error_msg, uint8_t max_size); +int TESTRIG_API vscl_decode_error(const uint32_t error_code, char* error_msg, vscl_byte_t max_size); /* * @brief Decode and print an error code's corresponding message. */ -int TESTRIG_API PrintError(const uint32_t error_code); +int TESTRIG_API vscl_print_error(const uint32_t error_code); /* * @brief Reset a device. */ -uint32_t TESTRIG_API ResetDevice(void* device_handle, struct Controller* controller_in); +uint32_t TESTRIG_API vscl_reset_device(void* device_handle, struct controller* controller_in); /* * @brief Setup that's tooled to our configuration. */ -uint32_t TESTRIG_API SetupTestrigAsCANGateway( - struct Controller controllers[3], void* handles[3], int default_init); +uint32_t TESTRIG_API vscl_setup_can_gateway( + struct controller controllers[3], void* handles[3], int default_init); /* * @brief Cleanup that's tooled to our configuration. */ -uint32_t TESTRIG_API CleanupTestrig( - struct Controller controllers[3], void* handles[3]); +uint32_t TESTRIG_API vscl_cleanup_testrig( + struct controller controllers[3], void* handles[3]); #ifdef __cplusplus } // extern "C" -} // namespace VSCL::Rig #endif // __cplusplus diff --git a/lib/epos2/identify.c b/lib/epos2/identify.c index f807470..fc6e54c 100644 --- a/lib/epos2/identify.c +++ b/lib/epos2/identify.c @@ -5,28 +5,28 @@ #include "definitions.h" #include "identify.h" -static void PrintNamingError(uint8_t error_code) { +static void vscl_print_naming_error(vscl_byte_t error_code) { printf("While identifying device names:\n\t"); - PrintError(error_code); + vscl_print_error(error_code); } -static void PrintProtocolIdentError(uint8_t error_code, const char* name) { +static void vscl_print_protocol_ident_error(vscl_byte_t error_code, const char* name) { printf("While identifying device protocol stacks for device %s:\n\t", name); - PrintError(error_code); + vscl_print_error(error_code); } -static void PrintInterfaceIdentError(uint8_t error_code, const char* name, const char* protocol) { +static void vscl_print_interface_ident_error(vscl_byte_t error_code, const char* name, const char* protocol) { printf("While identifying device interface for device %s on protocol %s:\n\t", name, protocol); - PrintError(error_code); + vscl_print_error(error_code); } -static void PrintPortIdentError(uint8_t error_code, const char* name, const char* protocol, const char* iface) { +static void vscl_print_port_ident_error(vscl_byte_t error_code, const char* name, const char* protocol, const char* iface) { printf("While identifying device interface for device %s on protocol %s and interface %s:\n\t", name, protocol, iface); - PrintError(error_code); + vscl_print_error(error_code); } -uint32_t IdentifyDeviceNames(void) { +uint32_t vscl_ident_names(void) { uint32_t error_code = 0; int n = 1; int selection_end; @@ -36,7 +36,7 @@ uint32_t IdentifyDeviceNames(void) { 1, name, 8, &selection_end, &error_code); if (name_found != 0) { - PrintNamingError(error_code); + vscl_print_naming_error(error_code); return error_code; } @@ -51,7 +51,7 @@ uint32_t IdentifyDeviceNames(void) { &selection_end, &error_code); if (name_found != 0) { - PrintNamingError(error_code); + vscl_print_naming_error(error_code); return error_code; } @@ -62,7 +62,7 @@ uint32_t IdentifyDeviceNames(void) { return error_code; } -uint32_t IdentifyDeviceProtocols(char* device_name) { +uint32_t vscl_ident_protocols(char* device_name) { uint32_t error_code = 0; int n = 1; int selection_end; @@ -74,7 +74,7 @@ uint32_t IdentifyDeviceProtocols(char* device_name) { &selection_end, &error_code); if (protocol_found == 0) { - PrintProtocolIdentError(error_code, device_name); + vscl_print_protocol_ident_error(error_code, device_name); return error_code; } @@ -90,7 +90,7 @@ uint32_t IdentifyDeviceProtocols(char* device_name) { &selection_end, &error_code); if (protocol_found == 0) { - PrintProtocolIdentError(error_code, device_name); + vscl_print_protocol_ident_error(error_code, device_name); return error_code; } @@ -101,7 +101,7 @@ uint32_t IdentifyDeviceProtocols(char* device_name) { return error_code; } -uint32_t IdentifyDeviceInterfaces(char* device_name, char* protocol_stack) { +uint32_t vscl_ident_interfaces(char* device_name, char* protocol_stack) { uint32_t error_code = 0; int n = 1; int selection_end; @@ -112,7 +112,7 @@ uint32_t IdentifyDeviceInterfaces(char* device_name, char* protocol_stack) { device_interface, 64, &selection_end, &error_code); if (interface_found == 0) { - PrintInterfaceIdentError(error_code, device_name, protocol_stack); + vscl_print_interface_ident_error(error_code, device_name, protocol_stack); return error_code; } @@ -127,7 +127,7 @@ uint32_t IdentifyDeviceInterfaces(char* device_name, char* protocol_stack) { subdevice_interface, 64, &selection_end, &error_code); if (interface_found == 0) { - PrintInterfaceIdentError(error_code, device_name, protocol_stack); + vscl_print_interface_ident_error(error_code, device_name, protocol_stack); return error_code; } @@ -138,7 +138,7 @@ uint32_t IdentifyDeviceInterfaces(char* device_name, char* protocol_stack) { return error_code; } -uint32_t IdentifyDevicePorts(char* device_name, char* protocol_stack, char* device_interface) { +uint32_t vscl_ident_ports(char* device_name, char* protocol_stack, char* device_interface) { uint32_t error_code = 0; int n = 1; int selection_end; @@ -148,7 +148,7 @@ uint32_t IdentifyDevicePorts(char* device_name, char* protocol_stack, char* devi device_name, protocol_stack, device_interface, 1, device_port, 8, &selection_end, &error_code); if (port_found == 0) { - PrintPortIdentError(error_code, device_name, protocol_stack, device_interface); + vscl_print_port_ident_error(error_code, device_name, protocol_stack, device_interface); return error_code; } @@ -164,7 +164,7 @@ uint32_t IdentifyDevicePorts(char* device_name, char* protocol_stack, char* devi subdevice_port, 8, &selection_end, &error_code); if (port_found == 0) { - PrintPortIdentError(error_code, device_name, protocol_stack, device_interface); + vscl_print_port_ident_error(error_code, device_name, protocol_stack, device_interface); return error_code; } @@ -176,10 +176,10 @@ uint32_t IdentifyDevicePorts(char* device_name, char* protocol_stack, char* devi } -void PrintControllerCharacteristics(struct Controller* controller) { - printf("- Name: %s\n", controller->Name); - printf("- Node: %i\n", controller->NodeId); - printf("- Protocol: %s\n", controller->Protocol); - printf("- Interface: %s\n", controller->Interface); - printf("- Port: %s\n", controller->Port); -} +void vscl_print_characteristics(struct controller* controller) { + printf("- Name: %s\n", controller->name); + printf("- Node: %i\n", controller->node_id); + printf("- Protocol: %s\n", controller->protocol); + printf("- Interface: %s\n", controller->conn_interface); + printf("- Port: %s\n", controller->port); +} \ No newline at end of file diff --git a/lib/epos2/identify.h b/lib/epos2/identify.h index 05fa6d6..d1c7a6b 100644 --- a/lib/epos2/identify.h +++ b/lib/epos2/identify.h @@ -8,12 +8,6 @@ extern "C" { #include "libtestrig_api.h" #include "controller.h" -#ifdef __cplusplus -} // extern "C" (headers) -namespace VSCL::Rig { -extern "C" { -#endif // __cplusplus - enum ACQUIRE_DEVICE_INFORMATION_FLAGS { FLAG_ACQUIRE_INIT_DEFAULTS = 1, FLAG_ACQUIRE_ZERO_INIT = 2, @@ -24,7 +18,7 @@ enum ACQUIRE_DEVICE_INFORMATION_FLAGS { * * Refer to Page 3-25 of the manual. */ -uint32_t TESTRIG_API IdentifyDeviceNames(void); +uint32_t TESTRIG_API vscl_ident_names(void); /* * @brief Finds the protocol stacks associated with the device name. @@ -32,7 +26,7 @@ uint32_t TESTRIG_API IdentifyDeviceNames(void); * * Refer to Page 3-26 of the manual. */ -uint32_t TESTRIG_API IdentifyDeviceProtocols(char* device_name); +uint32_t TESTRIG_API vscl_ident_protocols(char* device_name); /* * @brief Finds the interfaces associated with the device name and protocol. @@ -40,22 +34,21 @@ uint32_t TESTRIG_API IdentifyDeviceProtocols(char* device_name); * * Refer to Page 3-27 of the manual. */ -uint32_t TESTRIG_API IdentifyDeviceInterfaces(char* device_name, char* protocol_stack); +uint32_t TESTRIG_API vscl_ident_interfaces(char* device_name, char* protocol_stack); /* * @brief Finds the ports assoiated with the device name, protocol, and interface. * * Refer to Page 3-28 of the manual. */ -uint32_t TESTRIG_API IdentifyDevicePorts(char* device_name, char* protocol_stack, char* device_interface); +uint32_t TESTRIG_API vscl_ident_ports(char* device_name, char* protocol_stack, char* device_interface); /* * @brief Print out controller characteristics. */ -void TESTRIG_API PrintControllerCharacteristics(struct Controller* controller_out); +void TESTRIG_API vscl_print_characteristics(struct controller* controller_out); #ifdef __cplusplus } // extern "C" -} // namespace VSCL::Rig #endif // __cplusplus diff --git a/lib/epos2/manage.c b/lib/epos2/manage.c index 517835a..b86d5af 100644 --- a/lib/epos2/manage.c +++ b/lib/epos2/manage.c @@ -1,21 +1,22 @@ #include +#include "epos2.h" #include "manage.h" -uint32_t RigAbort(const struct Controller* controller_in, void* device_handle) { +uint32_t vscl_abort(const struct controller* controller_in, void* device_handle) { uint32_t error_code = 0; if (device_handle == 0) { printf("At head of RigAbort():\n"); printf("\tERROR: Invalid device handle passed for %s at node %ihh.\n", - controller_in->Name, controller_in->NodeId); + controller_in->name, controller_in->node_id); return 0x2000000B; } printf("WARNING: Attempting to call abort on %s of node %i!!\n", - controller_in->Name, controller_in->NodeId); + controller_in->name, controller_in->node_id); - int ret = VCS_SetQuickStopState(device_handle, controller_in->NodeId, &error_code); + int ret = VCS_SetQuickStopState(device_handle, controller_in->node_id, &error_code); if (ret == 0) { - PrintError(error_code); + vscl_print_error(error_code); printf("DANGER: Failed to abort rig operations!\n"); } else { diff --git a/lib/epos2/manage.h b/lib/epos2/manage.h index 3bee245..aca0370 100644 --- a/lib/epos2/manage.h +++ b/lib/epos2/manage.h @@ -5,22 +5,13 @@ extern "C" { #endif #include - -#include "epos2.h" #include "controller.h" -#ifdef __cplusplus -} // extern "C" (headers) -namespace VSCL::Rig { -extern "C" { -#endif // __cplusplus - /* * Attempt to abort the test rig. */ -uint32_t TESTRIG_API RigAbort(const struct Controller* controller_in, void* device_handle); +uint32_t TESTRIG_API vscl_abort(const struct controller* controller_in, void* device_handle); #ifdef __cplusplus } // extern "C" -} // namespace VSCL::Rig #endif // __cplusplus diff --git a/lib/epos2/meson.build b/lib/epos2/meson.build deleted file mode 100644 index 0ac1818..0000000 --- a/lib/epos2/meson.build +++ /dev/null @@ -1,42 +0,0 @@ -fs = import('fs') -cc = meson.get_compiler('c') - -# Windows -if (operating_system == 'windows') - if not (cpu_architecture in ['x86', 'x86_64']) - error('Unsupported architecture') - endif - - where_epos2 = where_bin / 'EposCmd64.dll' - epos2 = declare_dependency( - link_args: [ where_epos2 ], - version: '6.8.1.0' - ) - -# Linux -elif (operating_system == 'linux') - if not (cpu_architecture in valid_arch) - error('Unsupported architecture') - endif - - where_ftd2xx = where_bin / 'libftd2xx.so' - where_epos2 = where_bin / 'libEposCmd.so' - - ftd2xx = declare_dependency( - link_args: [ where_ftd2xx ] - ) - - epos2 = declare_dependency( - dependencies: ftd2xx, - link_args: [ where_epos2 ], - version: '6.8.1.0' - ) - -# Others -else - error('Unsupported operating system') -endif - -epos2_srcs = [ './epos2/epos2.c', './epos2/definitions.c', - './epos2/identify.c', './epos2/connect.c', './epos2/manage.c' ] - diff --git a/lib/ipc/constants.c b/lib/ipc/constants.c deleted file mode 100644 index 95e3861..0000000 --- a/lib/ipc/constants.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "constants.h" - -const int DC_WITH_CLIENT = 1; diff --git a/lib/ipc/constants.h b/lib/ipc/constants.h deleted file mode 100644 index af185c3..0000000 --- a/lib/ipc/constants.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include "libtestrig_api.h" - -#ifdef __cplusplus -} -namespace VSCL::IPC { -extern "C" { -#endif // __cplusplus - -/* - * Socket read flag - * - * When receiving HEAD_DC, disconnect right after the client does. - */ -TESTRIG_API extern const int DC_WITH_CLIENT; - -/* - * For use with socket handler functions. - * TODO (post-refactor): Do I actually need this? - */ -TESTRIG_API enum HEADER_RET { - // When receiving HEAD_DC, disconnect right after the client does. - HANDLER_RET_STAY = 1, - HANDLER_RET_DC = 2 -}; - -#ifdef __cplusplus -} // extern "C" -} // namespace VSCL::IPC -#endif // __cplusplus diff --git a/lib/ipc/ipc.c b/lib/ipc/ipc.c index abdd871..1fec9cc 100644 --- a/lib/ipc/ipc.c +++ b/lib/ipc/ipc.c @@ -4,7 +4,6 @@ #include #include "ipc.h" -#include "constants.h" #include "os.h" #ifdef _WIN32 @@ -12,12 +11,12 @@ typedef int socklen_t; #endif -int SockGeneratePath(char* sockpath) { +int vscl_sock_genpath(char* sockpath) { int retstat; int baselen; //int dstart; - retstat = GetSockDestination(sockpath); + retstat = vscl_get_sock_destination(sockpath); baselen = strlen(sockpath); if (retstat != 0) { return retstat; } @@ -32,7 +31,7 @@ int SockGeneratePath(char* sockpath) { return 0; } -int SockSetup(struct sockaddr_un* sockaddr_mut) { +int vscl_sock_setup(struct sockaddr_un* sockaddr_mut) { int fd; int path_set; char sockpath[108] = { 0 }; @@ -54,7 +53,7 @@ int SockSetup(struct sockaddr_un* sockaddr_mut) { strnlen(sockaddr_mut->sun_path, 108)) == 0) ? -1 : 1; if (path_set == -1) { - SockGeneratePath(sockpath); + vscl_sock_genpath(sockpath); long long int sockpathlen = strnlen(sockpath, 108); strncpy(sockaddr_mut->sun_path, sockpath, sockpathlen + 1); } @@ -63,7 +62,7 @@ int SockSetup(struct sockaddr_un* sockaddr_mut) { return fd; } -int SockBind(const int fd, const struct sockaddr_un* sockaddr) { +int vscl_sock_bind(const int fd, const struct sockaddr_un* sockaddr) { int bindstat; socklen_t socklen; @@ -74,7 +73,7 @@ int SockBind(const int fd, const struct sockaddr_un* sockaddr) { return bindstat; } -int SockListen(const int fd, int max_backlog) { +int vscl_sock_listen(const int fd, int max_backlog) { int listenstat; listenstat = listen(fd, max_backlog); @@ -83,7 +82,7 @@ int SockListen(const int fd, int max_backlog) { return listenstat; } -int SockConnect(const int fd, const struct sockaddr_un* sockaddr) { +int vscl_sock_connect(const int fd, const struct sockaddr_un* sockaddr) { int connstat; socklen_t socklen; @@ -94,7 +93,7 @@ int SockConnect(const int fd, const struct sockaddr_un* sockaddr) { return connstat; } -int SockClose(const int fd, struct sockaddr_un* sockaddr) { +int vscl_sock_close(const int fd, struct sockaddr_un* sockaddr) { int closestat; @@ -108,110 +107,9 @@ int SockClose(const int fd, struct sockaddr_un* sockaddr) { return closestat; } -// TODO: reevaluate necessity for inclusion -int SockReadOut(const int fd, const struct sockaddr_un* sockaddr, uint8_t* buf_out, size_t max_write, int flags) { - int recvstat; - int acceptstat; - int head; - - int bytes_written = 0; - int recent_success = 1; - int reading = 1; - socklen_t socklen = sizeof(*sockaddr); - - while (reading != -1) { - acceptstat = accept(fd, (struct sockaddr*)sockaddr, &socklen); - if (acceptstat == -1) { continue; } - - // While a child is connected to this socket... - while (reading != -1) { - uint8_t buf[12] = { 0 }; -#ifdef _WIN32 - recvstat = recv(acceptstat, buf, 12, MSG_PEEK); -#else - recvstat = read(acceptstat, buf, 12); -#endif - - if (recvstat == -1) { perror("Socket read failure"); continue; } - - uint8_t headcheck[4] = { buf[0], buf[1], buf[2], buf[3] }; - head = IdentifyFullHeader(headcheck); - - // Pick what to do - switch (head) { - case HEADER_IS_DC: // Disconnecting - if (flags & DC_WITH_CLIENT) - reading = -1; - - break; - case HEADER_IS_STAY: // Continue - recent_success = 1; - break; - case -1: // Invalid header: don't read - if (recent_success == 1) { - printf("Socket receive error: Invalid header\n"); - recent_success = 0; - } - - continue; - break; - default: // Not really possible, but don't read it regardless. - if (recent_success == 1) { - printf("Unknown header read error\n"); - recent_success = 0; - } - - continue; - break; - } - - // Do a readout - for (int i = 4; i < 12 && bytes_written < 4096; i++, bytes_written++) { - buf_out[bytes_written] = buf[i]; - } - } - } - - return 0; -} - -int SockReadAndHandle(const int fd, struct sockaddr_un* sockaddr, int(*handler)(uint8_t*)) { - int recvstat; - int acceptstat; - int handlestat; - - int reading = 1; - socklen_t socklen = sizeof(*sockaddr); - - while (reading != -1) { - acceptstat = accept(fd, (struct sockaddr*)sockaddr, &socklen); - if (acceptstat == -1) { continue; } - - // While a child is connected to this socket... - while (reading != -1) { - uint8_t buf[12] = { 0 }; -#ifdef _WIN32 - recvstat = recv(acceptstat, buf, 12, MSG_PEEK); -#else - recvstat = read(acceptstat, buf, 12); -#endif - - if (recvstat == -1) { perror("Socket read failure"); continue; } - - handlestat = handler(buf); - - if (handlestat == HANDLER_RET_DC) { - reading = -1; - } - } - } - - return 0; -} - -int SockSend(const int fd, struct RigMessage* msg) { +int vscl_sock_send(const int fd, struct rig_message* msg) { int nbytes; - uint8_t buf[12] = { 0 }; + vscl_byte_t buf[12] = { 0 }; for (int i = 0; i < 4; i++) { buf[i] = msg->head[i]; @@ -231,7 +129,7 @@ int SockSend(const int fd, struct RigMessage* msg) { return nbytes; } -int IdentifyHeaderPart(uint8_t in[4], int idx) { +int vscl_ident_header_part(vscl_byte_t in[4], int idx) { if (in[idx] == HEAD_STAY[idx]) { return HEADER_IS_STAY; } @@ -246,15 +144,15 @@ int IdentifyHeaderPart(uint8_t in[4], int idx) { } } -int IdentifyFullHeader(uint8_t in[4]) { +int vscl_ident_full_header(vscl_byte_t in[4]) { int identity = -1; for (int i = 0; i < 4; i++) { if (i == 0) { - identity = IdentifyHeaderPart(in, 0); + identity = vscl_ident_header_part(in, 0); } else { - identity &= IdentifyHeaderPart(in, i); + identity &= vscl_ident_header_part(in, i); } } diff --git a/lib/ipc/ipc.h b/lib/ipc/ipc.h index 9d13cc5..1277399 100644 --- a/lib/ipc/ipc.h +++ b/lib/ipc/ipc.h @@ -14,82 +14,69 @@ extern "C" { #include #endif // _WIN32 -#include -#include "message.h" -#include "libtestrig_api.h" +#include +#include -#ifdef __cplusplus -} // extern "C" -namespace VSCL { -extern "C" { -#endif // __cplusplus +#include "ipc/os.h" +#include "ipc/message.h" +#include "libtestrig_api.h" /* - * Generate a path to a socket file. + * @brief Generate a path to a socket file. * * This is in a temporary path with the .rigsock extension. */ -TESTRIG_API int SockGeneratePath(char* sockpath); +TESTRIG_API int vscl_sock_genpath(char* sockpath); /* - * Create a Unix socket to a random file. + * @brief Create a Unix socket to a random file. * * Returns a nonzero int representing the file descriptor, otherwise -1. */ -TESTRIG_API int SockSetup(struct sockaddr_un* sockaddr_mut); +TESTRIG_API int vscl_sock_setup(struct sockaddr_un* sockaddr_mut); /* - * Bind the Unix socket, using the path specified in the passed sockaddr_un struct. + * @brief Bind the Unix socket, using the path specified in the passed sockaddr_un struct. * * Returns 0 on success, -1 on failure. */ -TESTRIG_API int SockBind(const int fd, const struct sockaddr_un* sockaddr); +TESTRIG_API int vscl_sock_bind(const int fd, const struct sockaddr_un* sockaddr); /* - * Set the Unix socket to listen and permit connection attempts. + * @brief Set the Unix socket to listen and permit connection attempts. * * Returns 0 on success, -1 on failure. */ -TESTRIG_API int SockListen(const int fd, int max_backlog); +TESTRIG_API int vscl_sock_listen(const int fd, int max_backlog); /* - * Connect the Unix Socket, using the path specified in the passed sockaddr_un struct. + * @brief Connect the Unix Socket, using the path specified in the passed sockaddr_un struct. * * Returns 0 on success, -1 on failure. */ -TESTRIG_API int SockConnect(const int fd, const struct sockaddr_un* sockaddr); - -/* - * Close the socket and clean up. - */ -TESTRIG_API int SockClose(const int fd, struct sockaddr_un* sockaddr); - -/* - * Set the socket up to receive and simply write out to the buffer. - */ -TESTRIG_API int SockReadOut(const int fd, const struct sockaddr_un* sockaddr, uint8_t* buf_out, size_t max_write, int flags); +TESTRIG_API int vscl_sock_connect(const int fd, const struct sockaddr_un* sockaddr); /* - * Set the socket up to receive and loop the handler on each message. + * @brief Close the socket and clean up. */ -TESTRIG_API int SockReadAndHandle(const int fd, struct sockaddr_un* sockaddr, int(*handler)(uint8_t*)); +TESTRIG_API int vscl_sock_close(const int fd, struct sockaddr_un* sockaddr); /* - * Send a message over the socket without waiting for a response. + * @brief Send a message over the socket without waiting for a response. + * TODO: make version that waits */ -TESTRIG_API int SockSend(const int fd, struct RigMessage* msg); +TESTRIG_API int vscl_sock_send(const int fd, struct rig_message* msg); /* - * Identify the header byte. + * @brief Identify the header byte. */ -TESTRIG_API int IdentifyHeaderPart(uint8_t in[4], int idx); +TESTRIG_API int vscl_ident_header_part(vscl_byte_t in[4], int idx); /* - * Identify the four bytes in the header. + * @brief Identify the four bytes in the header. */ -TESTRIG_API int IdentifyFullHeader(uint8_t in[4]); +TESTRIG_API int vscl_ident_full_header(vscl_byte_t in[4]); #ifdef __cplusplus } // extern "C" -} // namespace VSCL -#endif +#endif \ No newline at end of file diff --git a/lib/ipc/meson.build b/lib/ipc/meson.build deleted file mode 100644 index 0dddf79..0000000 --- a/lib/ipc/meson.build +++ /dev/null @@ -1,10 +0,0 @@ -if (operating_system.to_lower() == 'windows') - ipc_os_deps = declare_dependency( - dependencies: cc.find_library('Ws2_32', - required: true), - ) -elif (operating_system.to_lower() == 'linux') - ipc_os_deps = declare_dependency() -else - error('Unsupported operating system or build environment: ' + operating_system) -endif diff --git a/lib/ipc/message.c b/lib/ipc/message.c index 1a51aad..976e26e 100644 --- a/lib/ipc/message.c +++ b/lib/ipc/message.c @@ -2,14 +2,14 @@ #include "message.h" // All of these are magic numbers btw -const uint8_t HEAD_STAY[4] = { 27, 34, 41, 48 }; -const uint8_t HEAD_DC[4] = { 255, 127, 63, 31 }; -const uint8_t HEAD_SYNC[4] = { 'S', 'Y', 'N', 'C' }; -const uint8_t HEAD_IDENT[4] = { 'I', 'D', 234, 137 }; -const uint8_t MESSAGE_BLANK[8] = { 0 }; +const vscl_byte_t HEAD_STAY[4] = { 27, 34, 41, 48 }; +const vscl_byte_t HEAD_DC[4] = { 255, 127, 63, 31 }; +const vscl_byte_t HEAD_SYNC[4] = { 'S', 'Y', 'N', 'C' }; +const vscl_byte_t HEAD_IDENT[4] = { 'I', 'D', 234, 137 }; +const vscl_byte_t MESSAGE_BLANK[8] = { 0 }; -int SetMessage(struct RigMessage* msg, const uint8_t* head, const uint8_t* data) { - memset(msg, 0, sizeof(struct RigMessage)); +int vscl_set_message(struct rig_message* msg, const vscl_byte_t* head, const vscl_byte_t* data) { + memset(msg, 0, sizeof(struct rig_message)); memcpy(msg->head, head, 4); memcpy(msg->data, data, 8); return 0; diff --git a/lib/ipc/message.h b/lib/ipc/message.h index 02b251e..14d6d25 100644 --- a/lib/ipc/message.h +++ b/lib/ipc/message.h @@ -5,26 +5,17 @@ extern "C" { #endif -#include #include "libtestrig_api.h" -#ifdef __cplusplus -} // extern "C" -namespace VSCL { -extern "C" { -#endif // __cplusplus - /* * A test rig message. - * These are unsigned chars - but more explict - on the architectures and OSes we care about - * (x86_64 Linux and Windows, basically whatever Maxon supports) */ -TESTRIG_API struct RigMessage { +struct rig_message { // Header: Four bytes to avoid collisions while keeping messages small. - uint8_t head[4]; + vscl_byte_t head[4]; // Data: Eight bytes as described by the size of the data part on a(n EPOS2) CAN frame. - uint8_t data[8]; + vscl_byte_t data[8]; }; /* @@ -32,30 +23,30 @@ TESTRIG_API struct RigMessage { * * The listener process does not accept messages without this header. */ -TESTRIG_API extern const uint8_t HEAD_STAY[4]; +TESTRIG_API extern const vscl_byte_t HEAD_STAY[4]; /* * Signal to the listener process to stop listening on the socket and close. */ -TESTRIG_API extern const uint8_t HEAD_DC[4]; +TESTRIG_API extern const vscl_byte_t HEAD_DC[4]; /* * Synchronize message header. Sent back and forth to confirm connection. */ -TESTRIG_API extern const uint8_t HEAD_SYNC[4]; +TESTRIG_API extern const vscl_byte_t HEAD_SYNC[4]; /* * Synchronize message header. Sent back and forth to confirm connection. */ -TESTRIG_API extern const uint8_t HEAD_IDENT[4]; +TESTRIG_API extern const vscl_byte_t HEAD_IDENT[4]; /* * Blank message. */ -TESTRIG_API extern const uint8_t MESSAGE_BLANK[8]; +TESTRIG_API extern const vscl_byte_t MESSAGE_BLANK[8]; // Convenience function that encapsulates copying to the header and data fields. -TESTRIG_API int SetMessage(struct RigMessage* msg, const uint8_t* head, const uint8_t* data); +TESTRIG_API int vscl_set_message(struct rig_message* msg, const vscl_byte_t* head, const vscl_byte_t* data); /* * The type of header. @@ -66,8 +57,6 @@ TESTRIG_API enum HEADER_TYPE { HEADER_IS_SYNC = 4, }; - #ifdef __cplusplus } // extern "C" -} // namespace VSCL #endif diff --git a/lib/ipc/os.c b/lib/ipc/os.c index 9e4844c..11ef405 100644 --- a/lib/ipc/os.c +++ b/lib/ipc/os.c @@ -4,7 +4,7 @@ #include "os.h" -int GetSockDestination(char *dest) { +int vscl_get_sock_destination(char *dest) { #ifdef _WIN32 size_t retvalue; char usrtemp[76] = { 0 }; diff --git a/lib/ipc/os.h b/lib/ipc/os.h index 00c3981..cb4f9dd 100644 --- a/lib/ipc/os.h +++ b/lib/ipc/os.h @@ -1,7 +1,6 @@ #pragma once #ifdef __cplusplus -namespace VSCL::_detail { extern "C" { #endif // __cplusplus @@ -11,9 +10,8 @@ extern "C" { #define INVALID_SOCKET -1 #endif -TESTRIG_API int GetSockDestination(char* dest); +TESTRIG_API int vscl_get_sock_destination(char* dest); #ifdef __cplusplus } // extern "C" -} // namespace VSCL::_detail #endif // __cplusplus diff --git a/lib/libtestrig_api.h b/lib/libtestrig_api.h index c294eb7..b6445aa 100644 --- a/lib/libtestrig_api.h +++ b/lib/libtestrig_api.h @@ -11,3 +11,5 @@ #define TESTRIG_API #endif // _WIN32 #endif // TESTRIG_API + +typedef char vscl_byte_t; \ No newline at end of file diff --git a/lib/meson.build b/lib/meson.build deleted file mode 100644 index dacfae6..0000000 --- a/lib/meson.build +++ /dev/null @@ -1,14 +0,0 @@ -## Main Binary Definitions -subdir('ipc') -subdir('epos2') - -srcs = [ 'ipc/ipc.c', 'ipc/message.c', 'ipc/constants.c', 'ipc/os.c' ] + epos2_srcs -lib = shared_library('testrig', srcs, - install_dir: where_bin, - install_rpath: std_rpath, - install: true, - dependencies: [ ipc_os_deps, epos2 ], - c_args: ['-DCOMPILING_TESTRIG_DLL'] ) - -dep = declare_dependency(include_directories: '.', - link_with: lib, dependencies: epos2) diff --git a/meson.build b/meson.build deleted file mode 100644 index 0e98891..0000000 --- a/meson.build +++ /dev/null @@ -1,18 +0,0 @@ -project('libtestrig', 'c', default_options: ['warning_level=3']) - -fs = import('fs') -cc = meson.get_compiler('c') -pyexe = find_program('python') - -## Machine info -operating_system = host_machine.system() -cpu_architecture = host_machine.cpu_family() -valid_os = ['windows', 'linux'] -valid_arch = ['x86', 'x86_64', 'aarch64', 'arm'] - -where_bin = meson.project_source_root() / 'bin' -std_rpath = '$ORIGIN' - -subdir('lib') -subdir('src') -subdir('test') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..14dd2f0 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,11 @@ +set(TESTRIG_SOURCES + "${CMAKE_CURRENT_LIST_DIR}/main.c" + "${CMAKE_CURRENT_LIST_DIR}/args.c" + "${CMAKE_CURRENT_LIST_DIR}/actions.c" + "${CMAKE_CURRENT_LIST_DIR}/help.c" + "${CMAKE_CURRENT_LIST_DIR}/daemon.c") + +add_executable(testrig ${TESTRIG_SOURCES}) +target_link_libraries(testrig PRIVATE libtestrig) +target_include_directories(testrig PRIVATE ${LIB_DIR}) +target_output_to_bin(testrig) \ No newline at end of file diff --git a/src/actions.c b/src/actions.c index 7f118c4..222b3ef 100644 --- a/src/actions.c +++ b/src/actions.c @@ -14,20 +14,21 @@ extern char* action_map[]; -int detach_program(char** argv, enum cli_action act, const other_args* others) { +int detach_program(char** argv, enum CLI_ACTION act, const other_args* others) { #if _WIN32 - return -1; PROCESS_INFORMATION pi; STARTUPINFO si; char cmd[1024] = { 0 }; strncpy(cmd, argv[0], strnlen(argv[0], 128) + 1); + strncat(cmd, " ", 2); strncat(cmd, action_map[act], 10); for (uint8_t i = 0; i < others->size; i++) { strncat(cmd, " ", 2); strncat(cmd, others->data[i], 128); } + printf("%s\n", cmd); BOOL mkdetach = CreateProcess( NULL, @@ -42,6 +43,31 @@ int detach_program(char** argv, enum cli_action act, const other_args* others) { &pi ); + if (mkdetach == FALSE) { + DWORD errcode = GetLastError(); + TCHAR errmsg[256] = { 0 }; + + DWORD wides = FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + errcode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + errmsg, + 256, + NULL); + + + if (wides == 0) { printf("error while processing error that occurred during attach\n"); return -1; } + wprintf(L"failed to detach: %s", errmsg); + + return -1; + } + + CloseHandle(&si); + CloseHandle(&pi); + + return GetCurrentProcessId(); + #else pid_t pid = fork(); @@ -64,7 +90,7 @@ int detach_program(char** argv, enum cli_action act, const other_args* others) { int testrig_ident(other_args* others) { if (others->data == NULL) { return 1; } - IdentifyDeviceNames(); + vscl_ident_names(); return 0; } @@ -88,20 +114,20 @@ int testrig_request(other_args* others) { struct sockaddr_un sockaddr = { 0 }; struct sockaddr_un daemon_sockaddr = { 0 }; - int setup = SockSetup(&sockaddr); + int setup = vscl_sock_setup(&sockaddr); if (setup == -1) { return 1; } int not_sought = seek_daemon(&daemon_sockaddr); - if (not_sought) { printf("not found"); SockClose(setup, &sockaddr); return 1; } + if (not_sought) { printf("not found"); vscl_sock_close(setup, &sockaddr); return 1; } - int conn = SockConnect(setup, &daemon_sockaddr); + int conn = vscl_sock_connect(setup, &daemon_sockaddr); if (conn == -1) { return 1; } - struct RigMessage msg = { 0 }; - SetMessage(&msg, HEAD_SYNC, MESSAGE_BLANK); + struct rig_message msg = { 0 }; + vscl_set_message(&msg, HEAD_SYNC, MESSAGE_BLANK); // oh.. I need to impl a two-way thing - int nbytes = SockSend(setup, &msg); + int nbytes = vscl_sock_send(setup, &msg); if (nbytes == -1) { return 1; } return 0; diff --git a/src/actions.h b/src/actions.h index 37de00a..789c34d 100644 --- a/src/actions.h +++ b/src/actions.h @@ -11,7 +11,7 @@ #include "other_args.h" #include "args.h" -int detach_program(char** argv, enum cli_action act, const other_args* others); +int detach_program(char** argv, enum CLI_ACTION act, const other_args* others); int testrig_ident(other_args* others); int testrig_stat(other_args* others); diff --git a/src/args.c b/src/args.c index c7799b0..1ce42cd 100644 --- a/src/args.c +++ b/src/args.c @@ -77,7 +77,7 @@ int parse_opt(const char* arg, struct parsed_args* parsed) { return ret; } -int validate_combo(enum cli_mode mode, enum cli_action action) { +int validate_combo(enum CLI_MODE mode, enum CLI_ACTION action) { switch (mode) { case CLI_MODE_DETACHED: switch (action) { diff --git a/src/args.h b/src/args.h index acc210a..b959b68 100644 --- a/src/args.h +++ b/src/args.h @@ -3,12 +3,12 @@ #include #include "other_args.h" -enum cli_mode { +enum CLI_MODE { CLI_MODE_CMD = 0, CLI_MODE_DETACHED = 1 }; -enum cli_action { +enum CLI_ACTION { /* * Print help for this program. * @@ -50,9 +50,9 @@ enum cli_action { }; struct parsed_args { - enum cli_mode mode; - enum cli_action action; - uint8_t verbosity; + enum CLI_MODE mode; + enum CLI_ACTION action; + vscl_byte_t verbosity; // Operate on the passed immmutable strings. int(*fun)(other_args*); @@ -65,4 +65,4 @@ int parse_flag(const char* flag, struct parsed_args* parsed); int parse_opt(const char* opt, struct parsed_args* parsed); int parse_act(const char* act, struct parsed_args* parsed); void parse_args(int argc, char** argv, struct parsed_args* parsed, other_args* others); -int validate_combo(enum cli_mode mode, enum cli_action action); +int validate_combo(enum CLI_MODE mode, enum CLI_ACTION action); diff --git a/src/daemon.c b/src/daemon.c index ca14475..b8b6751 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,14 +1,16 @@ +// NOLINTBEGIN #define _XOPEN_SOURCE_EXTENDED 1 #define _XOPEN_SOURCE 600 +// NOLINTEND #include #include #include "daemon.h" -#include "ipc/os.h" +#include "ipc/ipc.h" #ifdef _WIN32 #include -//#include // holy cow there is some include order stuff with windows.h +#include // holy cow there is some include order stuff with windows.h typedef int socklen_t; #else #include @@ -16,7 +18,7 @@ typedef int socklen_t; #include #endif // _WIN32 -extern enum TESTRIG_DAEMON_STATE DAEMON_CURRENT_STATUS; +extern enum TESTRIG_DAEMON_STATE DAEMON_CURRENT_STATUS; // NOLINT static char sockf[108] = { 0 }; #ifdef _WIN32 @@ -56,7 +58,7 @@ static int seek_sock_ext(char* sock) { int seek_daemon(struct sockaddr_un* sockaddr) { char sock[108] = { 0 }; - int dest = GetSockDestination(sock); + int dest = vscl_get_sock_destination(sock); if (dest) { return 1; } sockaddr->sun_family = AF_UNIX; @@ -78,14 +80,14 @@ int testrig_daemon(other_args* others) { struct sockaddr_un sockaddr = { 0 }; int retstat = 0; - int sock = SockSetup(&sockaddr); + int sock = vscl_sock_setup(&sockaddr); if (sock == -1) { return -1; } socklen_t socksize = sizeof(sockaddr); - retstat = SockBind(sock, &sockaddr); + retstat = vscl_sock_bind(sock, &sockaddr); if (retstat == -1) { return -1; } - retstat = SockListen(sock, 1); + retstat = vscl_sock_listen(sock, 1); if (retstat == -1) { return -1; } #ifdef _WIN32 @@ -104,7 +106,7 @@ int testrig_daemon(other_args* others) { int accepted = accept(sock, (struct sockaddr*)&sockaddr, &socksize); if (accepted == -1) { perror("daemon accept failure"); continue; } - uint8_t msg[12] = { 0 }; + vscl_byte_t msg[12] = { 0 }; #ifdef _WIN32 int synced = recv(accepted, msg, 12, MSG_PEEK); #else @@ -112,10 +114,10 @@ int testrig_daemon(other_args* others) { #endif if (synced != 12) { continue; } - uint8_t head[4] = { 0 }; + vscl_byte_t head[4] = { 0 }; memcpy(head, msg, 4); - int header = IdentifyFullHeader(head); + int header = vscl_ident_full_header(head); if (header != HEADER_IS_SYNC) { continue; } // What i want: @@ -123,12 +125,12 @@ int testrig_daemon(other_args* others) { // - It should check if it's a sync msg // - It should reply (i think i can do this with bytestreasm) // - It should connect back to another socket to send the data! - struct RigMessage reply; - uint8_t blank[8] = { 0 }; - int set = SetMessage(&reply, HEAD_SYNC, blank); + struct rig_message reply; + vscl_byte_t blank[8] = { 0 }; + int set = vscl_set_message(&reply, HEAD_SYNC, blank); if (!set) { continue; } - int sent = SockSend(accepted, &reply); + int sent = vscl_sock_send(accepted, &reply); if (sent != 12) { continue; } @@ -138,10 +140,10 @@ int testrig_daemon(other_args* others) { while (DAEMON_CURRENT_STATUS == TESTRIG_DAEMON_CONNECTED) { // this isn't cfg right - int accepted = SockConnect(sock, &sockaddr); + int accepted = vscl_sock_connect(sock, &sockaddr); if (accepted == -1) { perror("daemon connect failure"); continue; } - uint8_t buf[12] = { 0 }; + vscl_byte_t buf[12] = { 0 }; #ifdef _WIN32 int recvd = recv(accepted, buf, 12, MSG_PEEK); #else @@ -155,7 +157,7 @@ int testrig_daemon(other_args* others) { } } - SockClose(sock, &sockaddr); + vscl_sock_close(sock, &sockaddr); printf("Stopping...\n"); return 0; } diff --git a/src/main.c b/src/main.c index 156c914..0dd22fe 100644 --- a/src/main.c +++ b/src/main.c @@ -1,15 +1,11 @@ #include -#include - -#include "ipc/ipc.h" -#include "epos2/epos2.h" #include "actions.h" #include "help.h" #include "args.h" #include "daemon.h" -enum TESTRIG_DAEMON_STATE DAEMON_CURRENT_STATUS = TESTRIG_DAEMON_NONE; +enum TESTRIG_DAEMON_STATE DAEMON_CURRENT_STATUS = TESTRIG_DAEMON_NONE; // NOLINT int main(int argc, char** argv) { if (argc < 2 || argc > 255) { diff --git a/src/meson.build b/src/meson.build deleted file mode 100644 index f4de96d..0000000 --- a/src/meson.build +++ /dev/null @@ -1,6 +0,0 @@ -cli = executable('testrig', - dependencies: [ dep ], - sources: [ 'main.c', 'args.c', 'actions.c', 'help.c', 'daemon.c' ], - install_dir: where_bin, - install_rpath: std_rpath, - install: true) diff --git a/src/other_args.h b/src/other_args.h index 19312e3..2065891 100644 --- a/src/other_args.h +++ b/src/other_args.h @@ -1,8 +1,8 @@ #pragma once -#include +#include "libtestrig_api.h" typedef struct { - uint8_t size; + vscl_byte_t size; char** data; } other_args; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index 058ca61..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -ENABLE_TESTING() - -add_executable(sock_name_exe "${CMAKE_CURRENT_LIST_DIR}/test_sock_name.c") -target_link_libraries(sock_name_exe PRIVATE testrig) -add_test(NAME sockname COMMAND sock_name_exe) - -if (WIN32) - add_executable(test_mgr "${CMAKE_CURRENT_LIST_DIR}/test_win_mgr.c") - add_executable(test_server "${CMAKE_CURRENT_LIST_DIR}/test_win_server.c") - add_executable(test_client "${CMAKE_CURRENT_LIST_DIR}/test_win_client.c") - - target_link_libraries(test_mgr PRIVATE testrig) - target_link_libraries(test_server PRIVATE testrig) - target_link_libraries(test_client PRIVATE testrig) - - target_copy_dll(test_mgr) - target_copy_dll(test_server) - target_copy_dll(test_client) - - add_test(NAME discrete COMMAND test_mgr $ $) -else() - add_executable(test_fork "${CMAKE_CURRENT_LIST_DIR}/test_fork_ipc.c") - add_executable(test_preset_path "${CMAKE_CURRENT_LIST_DIR}/test_preset_sockpath.c") - - target_link_libraries(test_fork PRIVATE testrig) - target_link_libraries(test_preset_path PRIVATE testrig) - - add_test(NAME forking COMMAND test_fork) - add_test(NAME preset_path COMMAND test_preset_path) -endif() diff --git a/test/copy_dlls.meson.py b/test/copy_dlls.meson.py deleted file mode 100644 index c21524a..0000000 --- a/test/copy_dlls.meson.py +++ /dev/null @@ -1,16 +0,0 @@ -import shutil -import os -import sys -import platform -import subprocess - -if __name__ == "__main__": - fname = sys.argv[1] - fdest = os.path.join(sys.argv[2], os.path.basename(sys.argv[1])) - - shutil.copyfile(fname, fdest) - os.chmod(fdest, 0o755) - - # if for some reason - if (platform.system() == "Linux"): - subprocess.run(["patchelf", "--set-rpath", "$ORIGIN", fdest]) diff --git a/test/meson.build b/test/meson.build deleted file mode 100644 index 3383b39..0000000 --- a/test/meson.build +++ /dev/null @@ -1,55 +0,0 @@ -dllcopy_libtestrig = [pyexe, - meson.project_source_root() / 'test' / 'copy_dlls.meson.py', - lib.full_path(), - meson.project_source_root() / 'build' / 'test'] - -dllcopy_epos2 = [pyexe, - meson.project_source_root() / 'test' / 'copy_dlls.meson.py', - where_epos2, - meson.project_source_root() / 'build' / 'test'] - -_move_dlls_tests1 = custom_target('_dll_copy1', command: dllcopy_libtestrig, output: 'dll_copy_result1') -_move_dlls_tests2 = custom_target('_dll_copy2', command: dllcopy_epos2, output: 'dll_copy_result2') -_test_deps = [ _move_dlls_tests1, _move_dlls_tests2 ] - -if (operating_system == 'linux') - dllcopy_ftd2xx = [pyexe, - meson.project_source_root() / 'test' / 'copy_dlls.meson.py', - where_ftd2xx, - meson.project_source_root() / 'build' / 'test'] - - _move_dlls_tests3 = custom_target('_dll_copy3', command: dllcopy_ftd2xx, output: 'dll_copy_result3') - _test_deps += _move_dlls_tests3 -endif - - -sock_name_exe = executable('test_sock_name', 'test_sock_name.c', build_rpath: std_rpath, dependencies: dep) -test('sockname', sock_name_exe, depends: [ _test_deps ]) - -if (operating_system.to_lower() == 'linux') - fork_exe = executable('test_exe', 'test_fork_ipc.c', - dependencies: dep, - build_rpath: std_rpath) - preset_path_exe = executable('preset_path_exe', 'test_preset_sockpath.c', - dependencies: dep, - build_rpath: std_rpath) - - test('forking', fork_exe, depends: _test_deps) - test('preset_path', preset_path_exe, depends: _test_deps) - -elif (operating_system.to_lower() == 'windows') - kernel32_dep = declare_dependency( - dependencies: cc.find_library('Kernel32', - required: true), - ) - - mgr_exe = executable('test_mgr', 'test_win_mgr.c') - client_exe = executable('test_client', 'test_win_client.c', - dependencies: [ dep, kernel32_dep ]) - server_exe = executable('test_server', 'test_win_server.c', - dependencies: [ dep, kernel32_dep ]) - - test('discrete', mgr_exe, - args: [ server_exe.full_path(), client_exe.full_path() ], - depends: [ client_exe, server_exe, _test_deps ]) -endif diff --git a/test/test_fork_ipc.c b/test/test_fork_ipc.c deleted file mode 100644 index a872676..0000000 --- a/test/test_fork_ipc.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include - -#include "ipc/ipc.h" -#include "ipc/message.h" -#include "ipc/constants.h" - -int main(void) { - struct sockaddr_un parent_sockaddr; - struct sockaddr_un child_sockaddr; - pid_t pid; - int retstat; - int parent; - int fd; - int sent; - struct RigMessage msg; - uint8_t bad_head[4] = { 'H', 'E', 'L', 'L' }; - uint8_t bad_msg[8] = { 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!' }; - uint8_t buf[4096] = { 0 }; - SetMessage(&msg, bad_head, bad_msg); - - struct RigMessage msg1; - SetMessage(&msg1, HEAD_STAY, bad_msg); - - struct RigMessage msg2; - SetMessage(&msg2, HEAD_DC, bad_msg); - - parent = SockSetup(&parent_sockaddr); - if (parent == -1) { perror("parent socket failed to spawn"); return 1; } - - retstat = SockBind(parent, &parent_sockaddr); - if (retstat == -1) { perror("parent binding failure"); return 1; } - - pid = fork(); - switch (pid) { - case -1: - perror("fork"); - return 1; - case 0: // child - fd = SockSetup(&child_sockaddr); - if (fd == -1) { perror("child socket failed to spawn"); return 1; } - - retstat = SockConnect(fd, &parent_sockaddr); - if (retstat == -1) { perror("child connection failure"); return 1; } - - printf("Will send multiple messages...\n"); - sent = SockSend(fd, &msg); - printf("Sent %i bytes the first time\n", sent); - sent = SockSend(fd, &msg1); - printf("Sent %i bytes the second time\n", sent); - sent = SockSend(fd, &msg2); - printf("Sent %i bytes the third time\n", sent); - - retstat = SockClose(parent, &child_sockaddr); - if (retstat == -1) { perror("child socket ret error"); return 1; } - - return 0; - break; - default: // original parent - retstat = SockListen(parent, 2); - if (retstat == -1) { perror("listen error"); return 1; } - - SockReadOut(parent, &parent_sockaddr, buf, 4096, DC_WITH_CLIENT); - printf("Parent will exit...\n"); - break; - } - - retstat = SockClose(parent, &parent_sockaddr); - if (retstat == -1) { perror("parent socket ret error"); return 1; } - printf("Parent received %s\n", buf); - - assert(memcmp(buf, "O WORLD!O WORLD!", 16) == 0); - - return 0; -} diff --git a/test/test_preset_sockpath.c b/test/test_preset_sockpath.c deleted file mode 100644 index cb494ba..0000000 --- a/test/test_preset_sockpath.c +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include - -#include "ipc/ipc.h" -#include "ipc/message.h" -#include "ipc/constants.h" - -const char* SOCKNAME = "/tmp/this_is_my_socket.rigsock"; - -int main(void) { - struct sockaddr_un parent_sockaddr; - struct sockaddr_un child_sockaddr; - pid_t pid; - int retstat; - int parent; - int fd; - int sent; - uint8_t buf[4096] = { 0 }; - - struct RigMessage msg; - uint8_t data[8] = { 'H', 'E', 'L', 'L', 'O', '!', '!', '\n' }; - SetMessage(&msg, HEAD_DC, data); - - parent_sockaddr.sun_family = AF_UNIX; - strncpy(parent_sockaddr.sun_path, SOCKNAME, strnlen(SOCKNAME, 108)); - - parent = SockSetup(&parent_sockaddr); - if (parent == -1) { perror("parent socket failed to spawn"); return 1; } - - assert(strncmp(parent_sockaddr.sun_path, SOCKNAME, strnlen(SOCKNAME, 108)) == 0); - - retstat = SockBind(parent, &parent_sockaddr); - if (retstat == -1) { perror("parent binding failure"); return 1; } - - pid = fork(); - switch (pid) { - case -1: - perror("fork"); - return 1; - case 0: // child - fd = SockSetup(&child_sockaddr); - if (fd == -1) { perror("child socket failed to spawn"); return 1; } - - retstat = SockConnect(fd, &parent_sockaddr); - if (retstat == -1) { perror("child connection failure"); return 1; } - - sent = SockSend(fd, &msg); - printf("Sent %i bytes\n", sent); - - retstat = SockClose(parent, &child_sockaddr); - if (retstat == -1) { perror("child socket close error"); return 1; } - - return 0; - break; - default: // original parent - retstat = SockListen(parent, 2); - if (retstat == -1) { perror("listen error"); return 1; } - - SockReadOut(parent, &parent_sockaddr, buf, 4096, DC_WITH_CLIENT); - printf("Parent will exit...\n"); - break; - } - - retstat = SockClose(parent, &parent_sockaddr); - if (retstat == -1) { perror("parent socket close error"); return 1; } - printf("Parent received %s\n", buf); - - assert(memcmp(buf, "HELLO!!\n", 8) == 0); - - return 0; -} diff --git a/test/test_sock_name.c b/test/test_sock_name.c deleted file mode 100644 index d005621..0000000 --- a/test/test_sock_name.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Unit test to validate Socket paths by generating three of them. - * Due to the speed of these tests, the RNG seed does not change, so they are the same. - * This behavior is not guaranteed normally, so it is not tested for. - */ - -#include -#include -#include - -#include "ipc/ipc.h" - -void check_ret(int ret) { - if (ret != 0) { - perror("sock path generation"); - exit(ret); - } -} - -int main(void) { - char sock1[108] = { 0 }; - char sock2[108] = { 0 }; - char sock3[108] = { 0 }; - int ret; - - ret = SockGeneratePath(sock1); - check_ret(ret); - printf("Generated sock path:\n%s\n", sock1); - - ret = SockGeneratePath(sock2); - check_ret(ret); - printf("Generated sock path:\n%s\n", sock2); - - ret = SockGeneratePath(sock3); - check_ret(ret); - printf("Generated sock path:\n%s\n", sock3); - - return ret; -} diff --git a/test/test_win_client.c b/test/test_win_client.c deleted file mode 100644 index 6c4c550..0000000 --- a/test/test_win_client.c +++ /dev/null @@ -1,36 +0,0 @@ -#define _UNICODE -#define UNICODE - -#include - -#include "ipc/ipc.h" -#include "ipc/constants.h" - -#include - -int main(int argc, char** argv) { - if (argc < 2) { printf("Usage: %s ", argv[0]); return -1; } - - int retstat; - int sock; - struct RigMessage msg; - uint8_t data[8] = { 'H', 'O', 'W', 'D', 'Y', '!', '!', '!'}; - SetMessage(&msg, HEAD_DC, data); - - struct sockaddr_un sockaddr; - strncpy(sockaddr.sun_path, argv[1], strlen(argv[1]) + 1); - - sock = SockSetup(&sockaddr); - if (sock == INVALID_SOCKET) { perror("Client socket is invalid"); return 1; } - - retstat = SockConnect(sock, &sockaddr); - if (retstat == SOCKET_ERROR) { perror("Client socket failed to connect"); return 1; } - - retstat = SockSend(sock, &msg); - if (retstat == SOCKET_ERROR) { perror("Client socket failed to send"); return 1; } - - retstat = SockClose(sock, &sockaddr); - if (retstat == -1) { perror("Client socket cleanup error"); return 1; } - - return 0; -} \ No newline at end of file diff --git a/test/test_win_mgr.c b/test/test_win_mgr.c deleted file mode 100644 index ccfa09a..0000000 --- a/test/test_win_mgr.c +++ /dev/null @@ -1,115 +0,0 @@ -#define _UNICODE -#define UNICODE - -#include -#include -#include - -int main(int argc, char** argv) { - if (argc < 3) { printf("Improper invocation\n"); return -1; } - printf("Manager starting\n"); - - STARTUPINFO siserve; - STARTUPINFO siclient; - - PROCESS_INFORMATION piserve; - PROCESS_INFORMATION piclient; - - printf("Manager setting\n"); - memset(&siserve, 0, sizeof(STARTUPINFO)); - memset(&siclient, 0, sizeof(STARTUPINFO)); - memset(&piclient, 0, sizeof(PROCESS_INFORMATION)); - memset(&piclient, 0, sizeof(PROCESS_INFORMATION)); - - siserve.cb = sizeof(siserve); - siclient.cb = sizeof(siclient); - - WCHAR cwd[128] = { 0 }; - WCHAR cmd1[256] = { 0 }; - WCHAR cmd2[512] = { 0 }; - - GetCurrentDirectory(128, cwd); - mbstowcs(cmd1, argv[1], 256); - mbstowcs(cmd2, argv[2], 256); - - printf("Manager starting server with %ls\n", cmd1); - BOOL servemk = CreateProcess(NULL, - cmd1, - NULL, - NULL, - FALSE, - 0, - NULL, - NULL, - &siserve, - &piserve); - if (servemk == 0) { - printf("Manager failed to create server: %lu\n", GetLastError()); - return -1; - } - - Sleep(1000); - - - WIN32_FIND_DATA wddata; - WCHAR cwdwild[130] = { 0 }; - WCHAR fname[256] = { 0 }; - wsprintf(cwdwild, L"%ls\\*", cwd); - HANDLE hfind = FindFirstFile(cwdwild, &wddata); - - if (hfind != INVALID_HANDLE_VALUE) { - while (FindNextFile(hfind, &wddata)) { - if (wcscmp(wddata.cFileName, L"sock.txt") == 0) { - - FILE* fsockname = _wfopen(wddata.cFileName, L"r"); - if (fsockname == NULL) { exit(-1); } - char chara = fgetc(fsockname); - int n = 0; - - while (chara != 0 && chara != EOF ) { - WCHAR tempwide; - mbtowc(&tempwide, &chara, 1); - fname[n] = tempwide; - n++; - chara = fgetc(fsockname); - } - - fclose(fsockname); - printf("Found socket file clone\n"); - break; - } - } - - FindClose(hfind); - } - - wcscat(cmd2, L" "); - wcscat(cmd2, fname); - printf("Manager starting client with %ls\n", cmd2); - BOOL clientmk = CreateProcess(NULL, - cmd2, - NULL, - NULL, - FALSE, - 0, - NULL, - NULL, - &siclient, - &piclient); - if (clientmk == 0) { - printf("Manager failed to create client: %lu\n", GetLastError()); - return -1; - } - - printf("Waiting for the server and client to exit...\n"); - WaitForSingleObject(piserve.hProcess, INFINITE); - WaitForSingleObject(piclient.hProcess, INFINITE); - - CloseHandle(piserve.hProcess); - CloseHandle(piserve.hThread); - CloseHandle(piclient.hProcess); - CloseHandle(piclient.hThread); - - printf("Manager exiting\n"); - return 0; -} \ No newline at end of file diff --git a/test/test_win_server.c b/test/test_win_server.c deleted file mode 100644 index f35abb0..0000000 --- a/test/test_win_server.c +++ /dev/null @@ -1,71 +0,0 @@ -#define _UNICODE -#define UNICODE - -#include -#include -#include - -#include "ipc/ipc.h" -#include "ipc/constants.h" - -#include - -int write_out_sockname(const char* sockname) { - // not gonna open a pipe bro - WCHAR cwd[128] = { 0 }; - WCHAR txt[168] = { 0 }; - - GetCurrentDirectory(128, cwd); - wsprintf(txt, L"%ls\\sock.txt", cwd); - - FILE* where2 = _wfopen(txt, L"w"); - if (where2 == NULL) { exit(-1); } - - fwrite(sockname, 1, strlen(sockname), where2); - fclose(where2); - - printf("Wrote socket name to %ls\n", txt); - - return 0; -} - -int main(int argc, char** argv) { - // locals - int retstat; - int bytes_recv; - int sock; - struct sockaddr_un sockaddr; - memset(&sockaddr, 0, sizeof(struct sockaddr_un)); - - char sockpath[108] = { 0 }; - - // Grab preset socket path, if any - if (argc >= 2) { - strncpy(sockpath, argv[1], 108); - printf("Server will create socket at %s\n", sockpath); - } - - // Setup - sock = SockSetup(&sockaddr); - if (sock == INVALID_SOCKET) { perror("Server socket is invalid"); return 1; } - - printf("Making a sockname file using socket path %s...\n", sockaddr.sun_path); - write_out_sockname(sockaddr.sun_path); - - retstat = SockBind(sock, &sockaddr); - if (retstat == SOCKET_ERROR) { perror("Server socket failed to bind"); return 1; } - - retstat = SockListen(sock, 2); - if (retstat == -1) { perror("Server socket failed to start listening"); return 1; } - - uint8_t buf[1024] = { 0 }; - bytes_recv = SockReadOut(sock, &sockaddr, buf, 1024, DC_WITH_CLIENT); - - printf("Server is cleaning up after receiving %i bytes...\n", bytes_recv); - - retstat = SockClose(sock, &sockaddr); - if (retstat == -1) { perror("Server socket cleanup error"); return 1; } - printf("Server received %s\n", buf); - - return 0; -} \ No newline at end of file