From 6d7dd85fc3b43305c57cf4b32caad133167edec6 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 14:24:02 +0200 Subject: [PATCH 01/22] add linux base --- .clang-format | 17 + .clang-tidy | 30 + CMakeLists.txt | 96 +++ cmake/CPM.cmake | 1363 ++++++++++++++++++++++++++++++ examples/serial_advanced.ts | 135 +++ examples/serial_echo.ts | 134 +++ src/main.cpp | 182 ---- src/serial.cpp | 681 +++++++++++++++ src/serial.h | 64 ++ src/status_codes.h | 19 + src/test.cpp | 9 - tests/serial_test.cpp | 105 +++ tests/serial_unit_tests.cpp | 235 ++++++ versioning/version_config.cpp.in | 14 + 14 files changed, 2893 insertions(+), 191 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 CMakeLists.txt create mode 100644 cmake/CPM.cmake create mode 100644 examples/serial_advanced.ts create mode 100644 examples/serial_echo.ts delete mode 100644 src/main.cpp create mode 100644 src/serial.cpp create mode 100644 src/serial.h create mode 100644 src/status_codes.h delete mode 100644 src/test.cpp create mode 100644 tests/serial_test.cpp create mode 100644 tests/serial_unit_tests.cpp create mode 100644 versioning/version_config.cpp.in diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0b8c0be --- /dev/null +++ b/.clang-format @@ -0,0 +1,17 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 140 +TabWidth: 4 +UseTab: Never +BreakBeforeBraces: Allman +AllowShortFunctionsOnASingleLine: Empty +PointerAlignment: Left +DerivePointerAlignment: false +SpaceBeforeParens: ControlStatements +SortIncludes: true +IncludeBlocks: Regroup +ReflowComments: true +ContinuationIndentWidth: 4 +BinPackParameters: false +BinPackArguments: false + \ No newline at end of file diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..6793178 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,30 @@ +Checks: > + -*, + readability-* , + modernize-* , + -modernize-use-trailing-return-type, + -readability-magic-numbers, + +WarningsAsErrors: > + bugprone-* , + performance-* , + clang-analyzer-* + +HeaderFilterRegex: 'src/.*' +FormatStyle: file + +CheckOptions: + - key: modernize-use-auto.MinTypeNameLength + value: '5' + - key: modernize-use-auto.RemoveStars + value: 'false' + - key: readability-identifier-naming.VariableCase + value: lower_case + - key: readability-identifier-naming.MemberCase + value: lower_case + - key: readability-identifier-naming.PrivateMemberPrefix + value: '_' + - key: readability-function-size.ParameterThreshold + value: '8' + - key: readability-braces-around-statements.ShortStatementLines + value: '0' diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..748b551 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,96 @@ +cmake_minimum_required(VERSION 3.30) + +set(VERSION_MAJOR 0) +set(VERSION_MINOR 2) +set(VERSION_PATCH 0) + +set(PROJECT_N cpp_unix_bindings) +project(${PROJECT_N} VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +# Generate compile_commands.json for clang-based tooling (clangd / clang-tidy) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +include(cmake/CPM.cmake) + +CPMAddPackage( + NAME "googletest" + GITHUB_REPOSITORY "google/googletest" + GIT_TAG "v1.17.0" + GIT_SHALLOW TRUE + OPTIONS "GTEST_BUILD_TESTS OFF" + OPTIONS "GTEST_BUILD_EXAMPLES OFF" + OPTIONS "GTEST_BUILD_DOCS OFF" +) + +# Ensure clang tooling can pick up compile_commands.json from project root +if(CMAKE_EXPORT_COMPILE_COMMANDS) + add_custom_target(copy-compile-commands ALL + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_BINARY_DIR}/compile_commands.json + ${PROJECT_SOURCE_DIR}/compile_commands.json + COMMENT "Copy compile_commands.json to project root" + VERBATIM) +endif() + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/**.cpp) + +set(LIB true) + +configure_file(versioning/version_config.cpp.in ${PROJECT_SOURCE_DIR}/src/version_config.cpp) + +# a macro that gets all of the header containing directories. +MACRO(header_directories return_list includes_base_folder extention ) + FILE(GLOB_RECURSE new_list ${includes_base_folder}/*.${extention}) + SET(dir_list "") + FOREACH(file_path ${new_list}) + GET_FILENAME_COMPONENT(dir_path ${file_path} PATH) + SET(dir_list ${dir_list} ${dir_path}) + ENDFOREACH() + LIST(REMOVE_DUPLICATES dir_list) + SET(${return_list} ${dir_list}) +ENDMACRO() + +# using said macro. +header_directories(INCLUDES ${PROJECT_SOURCE_DIR}/src/ hpp) + +message("src files:") +foreach(file ${SRCS}) + message(STATUS ${file}) +endforeach() + +message("include directories:") +foreach(dir ${INCLUDES}) + message(STATUS ${dir}) +endforeach() + +if(LIB) + add_library(${PROJECT_N} SHARED ${SRCS}) +else() + add_executable(${PROJECT_N} ${SRCS}) +endif(LIB) + +set_target_properties(${PROJECT_N} PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR}) + +target_include_directories(${PROJECT_N} PUBLIC ${PROJECT_SOURCE_DIR}/src) + +add_executable(tests + tests/serial_test.cpp + tests/serial_unit_tests.cpp) + +target_link_libraries(tests PRIVATE ${PROJECT_N} gtest) + +target_include_directories(tests PRIVATE ${PROJECT_SOURCE_DIR}/src) + +add_custom_command(TARGET tests POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + COMMENT "Copy shared library next to aggregated test binary") + +enable_testing() +add_test(NAME AllTests COMMAND tests /dev/ttyUSB0) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..3636ee5 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,1363 @@ +# CPM.cmake - CMake's missing package manager +# =========================================== +# See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. +# +# MIT License +# ----------- +#[[ + Copyright (c) 2019-2023 Lars Melchior and contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +# Initialize logging prefix +if(NOT CPM_INDENT) + set(CPM_INDENT + "CPM:" + CACHE INTERNAL "" + ) +endif() + +if(NOT COMMAND cpm_message) + function(cpm_message) + message(${ARGV}) + endfunction() +endif() + +if(DEFINED EXTRACTED_CPM_VERSION) + set(CURRENT_CPM_VERSION "${EXTRACTED_CPM_VERSION}${CPM_DEVELOPMENT}") +else() + set(CURRENT_CPM_VERSION 0.42.0) +endif() + +get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) +if(CPM_DIRECTORY) + if(NOT CPM_DIRECTORY STREQUAL CPM_CURRENT_DIRECTORY) + if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) + message( + AUTHOR_WARNING + "${CPM_INDENT} \ +A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ +It is recommended to upgrade CPM to the most recent version. \ +See https://github.com/cpm-cmake/CPM.cmake for more information." + ) + endif() + if(${CMAKE_VERSION} VERSION_LESS "3.17.0") + include(FetchContent) + endif() + return() + endif() + + get_property( + CPM_INITIALIZED GLOBAL "" + PROPERTY CPM_INITIALIZED + SET + ) + if(CPM_INITIALIZED) + return() + endif() +endif() + +if(CURRENT_CPM_VERSION MATCHES "development-version") + message( + WARNING "${CPM_INDENT} Your project is using an unstable development version of CPM.cmake. \ +Please update to a recent release if possible. \ +See https://github.com/cpm-cmake/CPM.cmake for details." + ) +endif() + +set_property(GLOBAL PROPERTY CPM_INITIALIZED true) + +macro(cpm_set_policies) + # the policy allows us to change options without caching + cmake_policy(SET CMP0077 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + + # the policy allows us to change set(CACHE) without caching + if(POLICY CMP0126) + cmake_policy(SET CMP0126 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) + endif() + + # The policy uses the download time for timestamp, instead of the timestamp in the archive. This + # allows for proper rebuilds when a projects url changes + if(POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0135 NEW) + endif() + + # treat relative git repository paths as being relative to the parent project's remote + if(POLICY CMP0150) + cmake_policy(SET CMP0150 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0150 NEW) + endif() +endmacro() +cpm_set_policies() + +option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" + $ENV{CPM_USE_LOCAL_PACKAGES} +) +option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" + $ENV{CPM_LOCAL_PACKAGES_ONLY} +) +option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) +option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" + $ENV{CPM_DONT_UPDATE_MODULE_PATH} +) +option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" + $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} +) +option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK + "Add all packages added through CPM.cmake to the package lock" + $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} +) +option(CPM_USE_NAMED_CACHE_DIRECTORIES + "Use additional directory of package name in cache on the most nested level." + $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} +) + +set(CPM_VERSION + ${CURRENT_CPM_VERSION} + CACHE INTERNAL "" +) +set(CPM_DIRECTORY + ${CPM_CURRENT_DIRECTORY} + CACHE INTERNAL "" +) +set(CPM_FILE + ${CMAKE_CURRENT_LIST_FILE} + CACHE INTERNAL "" +) +set(CPM_PACKAGES + "" + CACHE INTERNAL "" +) +set(CPM_DRY_RUN + OFF + CACHE INTERNAL "Don't download or configure dependencies (for testing)" +) + +if(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) +else() + set(CPM_SOURCE_CACHE_DEFAULT OFF) +endif() + +set(CPM_SOURCE_CACHE + ${CPM_SOURCE_CACHE_DEFAULT} + CACHE PATH "Directory to download CPM dependencies" +) + +if(NOT CPM_DONT_UPDATE_MODULE_PATH AND NOT DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR) + set(CPM_MODULE_PATH + "${CMAKE_BINARY_DIR}/CPM_modules" + CACHE INTERNAL "" + ) + # remove old modules + file(REMOVE_RECURSE ${CPM_MODULE_PATH}) + file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) + # locally added CPM modules should override global packages + set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") +endif() + +if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + set(CPM_PACKAGE_LOCK_FILE + "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" + CACHE INTERNAL "" + ) + file(WRITE ${CPM_PACKAGE_LOCK_FILE} + "# CPM Package Lock\n# This file should be committed to version control\n\n" + ) +endif() + +include(FetchContent) + +# Try to infer package name from git repository uri (path or url) +function(cpm_package_name_from_git_uri URI RESULT) + if("${URI}" MATCHES "([^/:]+)/?.git/?$") + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + else() + unset(${RESULT} PARENT_SCOPE) + endif() +endfunction() + +# Find the shortest hash that can be used eg, if origin_hash is +# cccb77ae9609d2768ed80dd42cec54f77b1f1455 the following files will be checked, until one is found +# that is either empty (allowing us to assign origin_hash), or whose contents matches ${origin_hash} +# +# * .../cccb.hash +# * .../cccb77ae.hash +# * .../cccb77ae9609.hash +# * .../cccb77ae9609d276.hash +# * etc +# +# We will be able to use a shorter path with very high probability, but in the (rare) event that the +# first couple characters collide, we will check longer and longer substrings. +function(cpm_get_shortest_hash source_cache_dir origin_hash short_hash_output_var) + # for compatibility with caches populated by a previous version of CPM, check if a directory using + # the full hash already exists + if(EXISTS "${source_cache_dir}/${origin_hash}") + set(${short_hash_output_var} + "${origin_hash}" + PARENT_SCOPE + ) + return() + endif() + + foreach(len RANGE 4 40 4) + string(SUBSTRING "${origin_hash}" 0 ${len} short_hash) + set(hash_lock ${source_cache_dir}/${short_hash}.lock) + set(hash_fp ${source_cache_dir}/${short_hash}.hash) + # Take a lock, so we don't have a race condition with another instance of cmake. We will release + # this lock when we can, however, if there is an error, we want to ensure it gets released on + # it's own on exit from the function. + file(LOCK ${hash_lock} GUARD FUNCTION) + + # Load the contents of .../${short_hash}.hash + file(TOUCH ${hash_fp}) + file(READ ${hash_fp} hash_fp_contents) + + if(hash_fp_contents STREQUAL "") + # Write the origin hash + file(WRITE ${hash_fp} ${origin_hash}) + file(LOCK ${hash_lock} RELEASE) + break() + elseif(hash_fp_contents STREQUAL origin_hash) + file(LOCK ${hash_lock} RELEASE) + break() + else() + file(LOCK ${hash_lock} RELEASE) + endif() + endforeach() + set(${short_hash_output_var} + "${short_hash}" + PARENT_SCOPE + ) +endfunction() + +# Try to infer package name and version from a url +function(cpm_package_name_and_ver_from_url url outName outVer) + if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") + # We matched an archive + set(filename "${CMAKE_MATCH_1}") + + if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") + # We matched - (ie foo-1.2.3) + set(${outName} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + set(${outVer} + "${CMAKE_MATCH_2}" + PARENT_SCOPE + ) + elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") + # We couldn't find a name, but we found a version + # + # In many cases (which we don't handle here) the url would look something like + # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly + # distinguish the package name from the irrelevant bits. Moreover if we try to match the + # package name from the filename, we'd get bogus at best. + unset(${outName} PARENT_SCOPE) + set(${outVer} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + else() + # Boldly assume that the file name is the package name. + # + # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but + # such cases should be quite rare. No popular service does this... we think. + set(${outName} + "${filename}" + PARENT_SCOPE + ) + unset(${outVer} PARENT_SCOPE) + endif() + else() + # No ideas yet what to do with non-archives + unset(${outName} PARENT_SCOPE) + unset(${outVer} PARENT_SCOPE) + endif() +endfunction() + +function(cpm_find_package NAME VERSION) + string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") + find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) + if(${CPM_ARGS_NAME}_FOUND) + if(DEFINED ${CPM_ARGS_NAME}_VERSION) + set(VERSION ${${CPM_ARGS_NAME}_VERSION}) + endif() + cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}") + CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") + set(CPM_PACKAGE_FOUND + YES + PARENT_SCOPE + ) + else() + set(CPM_PACKAGE_FOUND + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from +# finding the system library +function(cpm_create_module_file Name) + if(NOT CPM_DONT_UPDATE_MODULE_PATH) + if(DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR) + # Redirect find_package calls to the CPM package. This is what FetchContent does when you set + # OVERRIDE_FIND_PACKAGE. The CMAKE_FIND_PACKAGE_REDIRECTS_DIR works for find_package in CONFIG + # mode, unlike the Find${Name}.cmake fallback. CMAKE_FIND_PACKAGE_REDIRECTS_DIR is not defined + # in script mode, or in CMake < 3.24. + # https://cmake.org/cmake/help/latest/module/FetchContent.html#fetchcontent-find-package-integration-examples + string(TOLOWER ${Name} NameLower) + file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-config.cmake + "include(\"\${CMAKE_CURRENT_LIST_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/${Name}Extra.cmake\" OPTIONAL)\n" + ) + file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-config-version.cmake + "set(PACKAGE_VERSION_COMPATIBLE TRUE)\n" "set(PACKAGE_VERSION_EXACT TRUE)\n" + ) + else() + file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake + "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" + ) + endif() + endif() +endfunction() + +# Find a package locally or fallback to CPMAddPackage +function(CPMFindPackage) + set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + set(downloadPackage ${CPM_DOWNLOAD_ALL}) + if(DEFINED CPM_DOWNLOAD_${CPM_ARGS_NAME}) + set(downloadPackage ${CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + elseif(DEFINED ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + set(downloadPackage $ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + endif() + if(downloadPackage) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(NOT CPM_PACKAGE_FOUND) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + endif() + +endfunction() + +# checks if a package has been added before +function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) + if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) + CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) + if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") + message( + WARNING + "${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." + ) + endif() + cpm_get_fetch_properties(${CPM_ARGS_NAME}) + set(${CPM_ARGS_NAME}_ADDED NO) + set(CPM_PACKAGE_ALREADY_ADDED + YES + PARENT_SCOPE + ) + cpm_export_variables(${CPM_ARGS_NAME}) + else() + set(CPM_PACKAGE_ALREADY_ADDED + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of +# arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted +# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 +function(cpm_parse_add_package_single_arg arg outArgs) + # Look for a scheme + if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") + string(TOLOWER "${CMAKE_MATCH_1}" scheme) + set(uri "${CMAKE_MATCH_2}") + + # Check for CPM-specific schemes + if(scheme STREQUAL "gh") + set(out "GITHUB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "gl") + set(out "GITLAB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "bb") + set(out "BITBUCKET_REPOSITORY;${uri}") + set(packageType "git") + # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine + # type + elseif(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Fall back to a URL + set(out "URL;${arg}") + set(packageType "archive") + + # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. + # We just won't bother with the additional complexity it will induce in this function. SVN is + # done by multi-arg + endif() + else() + if(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Give up + message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'") + endif() + endif() + + # For all packages we interpret @... as version. Only replace the last occurrence. Thus URIs + # containing '@' can be used + string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") + + # Parse the rest according to package type + if(packageType STREQUAL "git") + # For git repos we interpret #... as a tag or branch or commit hash + string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") + elseif(packageType STREQUAL "archive") + # For archives we interpret #... as a URL hash. + string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") + # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url + # should do this at a later point + else() + # We should never get here. This is an assertion and hitting it means there's a problem with the + # code above. A packageType was set, but not handled by this if-else. + message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'") + endif() + + set(${outArgs} + ${out} + PARENT_SCOPE + ) +endfunction() + +# Check that the working directory for a git repo is clean +function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) + + find_package(Git REQUIRED) + + if(NOT GIT_EXECUTABLE) + # No git executable, assume directory is clean + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + # check for uncommitted changes + execute_process( + COMMAND ${GIT_EXECUTABLE} status --porcelain + RESULT_VARIABLE resultGitStatus + OUTPUT_VARIABLE repoStatus + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + WORKING_DIRECTORY ${repoPath} + ) + if(resultGitStatus) + # not supposed to happen, assume clean anyway + message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed") + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + if(NOT "${repoStatus}" STREQUAL "") + set(${isClean} + FALSE + PARENT_SCOPE + ) + return() + endif() + + # check for committed changes + execute_process( + COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} + RESULT_VARIABLE resultGitDiff + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET + WORKING_DIRECTORY ${repoPath} + ) + + if(${resultGitDiff} EQUAL 0) + set(${isClean} + TRUE + PARENT_SCOPE + ) + else() + set(${isClean} + FALSE + PARENT_SCOPE + ) + endif() + +endfunction() + +# Add PATCH_COMMAND to CPM_ARGS_UNPARSED_ARGUMENTS. This method consumes a list of files in ARGN +# then generates a `PATCH_COMMAND` appropriate for `ExternalProject_Add()`. This command is appended +# to the parent scope's `CPM_ARGS_UNPARSED_ARGUMENTS`. +function(cpm_add_patches) + # Return if no patch files are supplied. + if(NOT ARGN) + return() + endif() + + # Find the patch program. + find_program(PATCH_EXECUTABLE patch) + if(CMAKE_HOST_WIN32 AND NOT PATCH_EXECUTABLE) + # The Windows git executable is distributed with patch.exe. Find the path to the executable, if + # it exists, then search `../usr/bin` and `../../usr/bin` for patch.exe. + find_package(Git QUIET) + if(GIT_EXECUTABLE) + get_filename_component(extra_search_path ${GIT_EXECUTABLE} DIRECTORY) + get_filename_component(extra_search_path_1up ${extra_search_path} DIRECTORY) + get_filename_component(extra_search_path_2up ${extra_search_path_1up} DIRECTORY) + find_program( + PATCH_EXECUTABLE patch HINTS "${extra_search_path_1up}/usr/bin" + "${extra_search_path_2up}/usr/bin" + ) + endif() + endif() + if(NOT PATCH_EXECUTABLE) + message(FATAL_ERROR "Couldn't find `patch` executable to use with PATCHES keyword.") + endif() + + # Create a temporary + set(temp_list ${CPM_ARGS_UNPARSED_ARGUMENTS}) + + # Ensure each file exists (or error out) and add it to the list. + set(first_item True) + foreach(PATCH_FILE ${ARGN}) + # Make sure the patch file exists, if we can't find it, try again in the current directory. + if(NOT EXISTS "${PATCH_FILE}") + if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + message(FATAL_ERROR "Couldn't find patch file: '${PATCH_FILE}'") + endif() + set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + endif() + + # Convert to absolute path for use with patch file command. + get_filename_component(PATCH_FILE "${PATCH_FILE}" ABSOLUTE) + + # The first patch entry must be preceded by "PATCH_COMMAND" while the following items are + # preceded by "&&". + if(first_item) + set(first_item False) + list(APPEND temp_list "PATCH_COMMAND") + else() + list(APPEND temp_list "&&") + endif() + # Add the patch command to the list + list(APPEND temp_list "${PATCH_EXECUTABLE}" "-p1" "<" "${PATCH_FILE}") + endforeach() + + # Move temp out into parent scope. + set(CPM_ARGS_UNPARSED_ARGUMENTS + ${temp_list} + PARENT_SCOPE + ) + +endfunction() + +# method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload +# FetchContent calls. As these are internal cmake properties, this method should be used carefully +# and may need modification in future CMake versions. Source: +# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152 +function(cpm_override_fetchcontent contentName) + cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "") + if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") + message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}") + endif() + + string(TOLOWER ${contentName} contentNameLower) + set(prefix "_FetchContent_${contentNameLower}") + + set(propertyName "${prefix}_sourceDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}") + + set(propertyName "${prefix}_binaryDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}") + + set(propertyName "${prefix}_populated") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} TRUE) +endfunction() + +# Download and add a package from source +function(CPMAddPackage) + cpm_set_policies() + + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + CUSTOM_CACHE_KEY + ) + + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND PATCHES) + + list(LENGTH ARGN argnLength) + + # Parse single shorthand argument + if(argnLength EQUAL 1) + cpm_parse_add_package_single_arg("${ARGN}" ARGN) + + # The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM + set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;") + + # Parse URI shorthand argument + elseif(argnLength GREATER 1 AND "${ARGV0}" STREQUAL "URI") + list(REMOVE_AT ARGN 0 1) # remove "URI gh:<...>@version#tag" + cpm_parse_add_package_single_arg("${ARGV1}" ARGV0) + + set(ARGN "${ARGV0};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;${ARGN}") + endif() + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + + # Set default values for arguments + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_ARGS_DOWNLOAD_ONLY) + set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) + else() + set(DOWNLOAD_ONLY NO) + endif() + + if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") + endif() + + if(DEFINED CPM_ARGS_GIT_REPOSITORY) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) + if(NOT DEFINED CPM_ARGS_GIT_TAG) + set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) + endif() + + # If a name wasn't provided, try to infer it from the git repo + if(NOT DEFINED CPM_ARGS_NAME) + cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) + endif() + endif() + + set(CPM_SKIP_FETCH FALSE) + + if(DEFINED CPM_ARGS_GIT_TAG) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) + # If GIT_SHALLOW is explicitly specified, honor the value. + if(DEFINED CPM_ARGS_GIT_SHALLOW) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) + endif() + endif() + + if(DEFINED CPM_ARGS_URL) + # If a name or version aren't provided, try to infer them from the URL + list(GET CPM_ARGS_URL 0 firstUrl) + cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) + # If we fail to obtain name and version from the first URL, we could try other URLs if any. + # However multiple URLs are expected to be quite rare, so for now we won't bother. + + # If the caller provided their own name and version, they trump the inferred ones. + if(NOT DEFINED CPM_ARGS_NAME) + set(CPM_ARGS_NAME ${nameFromUrl}) + endif() + if(NOT DEFINED CPM_ARGS_VERSION) + set(CPM_ARGS_VERSION ${verFromUrl}) + endif() + + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") + endif() + + # Check for required arguments + + if(NOT DEFINED CPM_ARGS_NAME) + message( + FATAL_ERROR + "${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" + ) + endif() + + # Check if package has been added before + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + if(CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for manual overrides + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") + set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) + set(CPM_${CPM_ARGS_NAME}_SOURCE "") + CPMAddPackage( + NAME "${CPM_ARGS_NAME}" + SOURCE_DIR "${PACKAGE_SOURCE}" + EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" + SYSTEM "${CPM_ARGS_SYSTEM}" + PATCHES "${CPM_ARGS_PATCHES}" + OPTIONS "${CPM_ARGS_OPTIONS}" + SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" + DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" + FORCE True + ) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for available declaration + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") + set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) + set(CPM_DECLARATION_${CPM_ARGS_NAME} "") + CPMAddPackage(${declaration}) + cpm_export_variables(${CPM_ARGS_NAME}) + # checking again to ensure version and option compatibility + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + return() + endif() + + if(NOT CPM_ARGS_FORCE) + if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(CPM_PACKAGE_FOUND) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + if(CPM_LOCAL_PACKAGES_ONLY) + message( + SEND_ERROR + "${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" + ) + endif() + endif() + endif() + + CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") + + if(DEFINED CPM_ARGS_GIT_TAG) + set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") + else() + set(PACKAGE_INFO "${CPM_ARGS_VERSION}") + endif() + + if(DEFINED FETCHCONTENT_BASE_DIR) + # respect user's FETCHCONTENT_BASE_DIR if set + set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) + else() + set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) + endif() + + cpm_add_patches(${CPM_ARGS_PATCHES}) + + if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) + # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work + # for relative paths. + get_filename_component( + source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} + ) + else() + set(source_directory ${CPM_ARGS_SOURCE_DIR}) + endif() + if(NOT EXISTS ${source_directory}) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") + endif() + elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) + list(SORT origin_parameters) + if(CPM_ARGS_CUSTOM_CACHE_KEY) + # Application set a custom unique directory name + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${CPM_ARGS_CUSTOM_CACHE_KEY}) + elseif(CPM_USE_NAMED_CACHE_DIRECTORIES) + string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") + cpm_get_shortest_hash( + "${CPM_SOURCE_CACHE}/${lower_case_name}" # source cache directory + "${origin_hash}" # Input hash + origin_hash # Computed hash + ) + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) + else() + string(SHA1 origin_hash "${origin_parameters}") + cpm_get_shortest_hash( + "${CPM_SOURCE_CACHE}/${lower_case_name}" # source cache directory + "${origin_hash}" # Input hash + origin_hash # Computed hash + ) + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) + endif() + # Expand `download_directory` relative path. This is important because EXISTS doesn't work for + # relative paths. + get_filename_component(download_directory ${download_directory} ABSOLUTE) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) + + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock) + endif() + + if(EXISTS ${download_directory}) + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} "${download_directory}" + "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + ) + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + + if(DEFINED CPM_ARGS_GIT_TAG AND NOT (PATCH_COMMAND IN_LIST CPM_ARGS_UNPARSED_ARGUMENTS)) + # warn if cache has been changed since checkout + cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) + if(NOT ${IS_CLEAN}) + message( + WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty" + ) + endif() + endif() + + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") + + # As the source dir is already cached/populated, we override the call to FetchContent. + set(CPM_SKIP_FETCH TRUE) + cpm_override_fetchcontent( + "${lower_case_name}" SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" + ) + + else() + # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but + # it should guarantee no commit hash get mis-detected. + if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) + cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) + if(NOT ${IS_HASH}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) + endif() + endif() + + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) + set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") + endif() + endif() + + if(NOT "${DOWNLOAD_ONLY}") + cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") + endif() + + if(CPM_PACKAGE_LOCK_ENABLED) + if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) + cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + elseif(CPM_ARGS_SOURCE_DIR) + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") + else() + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + endif() + endif() + + cpm_message( + STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" + ) + + if(NOT CPM_SKIP_FETCH) + # CMake 3.28 added EXCLUDE, SYSTEM (3.25), and SOURCE_SUBDIR (3.18) to FetchContent_Declare. + # Calling FetchContent_MakeAvailable will then internally forward these options to + # add_subdirectory. Up until these changes, we had to call FetchContent_Populate and + # add_subdirectory separately, which is no longer necessary and has been deprecated as of 3.30. + # A Bug in CMake prevents us to use the non-deprecated functions until 3.30.3. + set(fetchContentDeclareExtraArgs "") + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3") + if(${CPM_ARGS_EXCLUDE_FROM_ALL}) + list(APPEND fetchContentDeclareExtraArgs EXCLUDE_FROM_ALL) + endif() + if(${CPM_ARGS_SYSTEM}) + list(APPEND fetchContentDeclareExtraArgs SYSTEM) + endif() + if(DEFINED CPM_ARGS_SOURCE_SUBDIR) + list(APPEND fetchContentDeclareExtraArgs SOURCE_SUBDIR ${CPM_ARGS_SOURCE_SUBDIR}) + endif() + # For CMake version <3.28 OPTIONS are parsed in cpm_add_subdirectory + if(CPM_ARGS_OPTIONS AND NOT DOWNLOAD_ONLY) + foreach(OPTION ${CPM_ARGS_OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + endif() + cpm_declare_fetch( + "${CPM_ARGS_NAME}" ${fetchContentDeclareExtraArgs} "${CPM_ARGS_UNPARSED_ARGUMENTS}" + ) + + cpm_fetch_package("${CPM_ARGS_NAME}" ${DOWNLOAD_ONLY} populated ${CPM_ARGS_UNPARSED_ARGUMENTS}) + if(CPM_SOURCE_CACHE AND download_directory) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + if(${populated} AND ${CMAKE_VERSION} VERSION_LESS "3.30.3") + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + endif() + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + endif() + + set(${CPM_ARGS_NAME}_ADDED YES) + cpm_export_variables("${CPM_ARGS_NAME}") +endfunction() + +# Fetch a previously declared package +macro(CPMGetPackage Name) + if(DEFINED "CPM_DECLARATION_${Name}") + CPMAddPackage(NAME ${Name}) + else() + message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available") + endif() +endmacro() + +# export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set +macro(cpm_export_variables name) + set(${name}_SOURCE_DIR + "${${name}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${name}_BINARY_DIR + "${${name}_BINARY_DIR}" + PARENT_SCOPE + ) + set(${name}_ADDED + "${${name}_ADDED}" + PARENT_SCOPE + ) + set(CPM_LAST_PACKAGE_NAME + "${name}" + PARENT_SCOPE + ) +endmacro() + +# declares a package, so that any call to CPMAddPackage for the package name will use these +# arguments instead. Previous declarations will not be overridden. +macro(CPMDeclarePackage Name) + if(NOT DEFINED "CPM_DECLARATION_${Name}") + set("CPM_DECLARATION_${Name}" "${ARGN}") + endif() +endmacro() + +function(cpm_add_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") + endif() +endfunction() + +function(cpm_add_comment_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} + "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" + ) + endif() +endfunction() + +# includes the package lock file if it exists and creates a target `cpm-update-package-lock` to +# update it +macro(CPMUsePackageLock file) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) + if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + if(NOT TARGET cpm-update-package-lock) + add_custom_target( + cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} + ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} + ) + endif() + set(CPM_PACKAGE_LOCK_ENABLED true) + endif() +endmacro() + +# registers a package that has been added to CPM +function(CPMRegisterPackage PACKAGE VERSION) + list(APPEND CPM_PACKAGES ${PACKAGE}) + set(CPM_PACKAGES + ${CPM_PACKAGES} + CACHE INTERNAL "" + ) + set("CPM_PACKAGE_${PACKAGE}_VERSION" + ${VERSION} + CACHE INTERNAL "" + ) +endfunction() + +# retrieve the current version of the package to ${OUTPUT} +function(CPMGetPackageVersion PACKAGE OUTPUT) + set(${OUTPUT} + "${CPM_PACKAGE_${PACKAGE}_VERSION}" + PARENT_SCOPE + ) +endfunction() + +# declares a package in FetchContent_Declare +function(cpm_declare_fetch PACKAGE) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)") + return() + endif() + + FetchContent_Declare(${PACKAGE} ${ARGN}) +endfunction() + +# returns properties for a package previously defined by cpm_declare_fetch +function(cpm_get_fetch_properties PACKAGE) + if(${CPM_DRY_RUN}) + return() + endif() + + set(${PACKAGE}_SOURCE_DIR + "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" + PARENT_SCOPE + ) +endfunction() + +function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) + if(${CPM_DRY_RUN}) + return() + endif() + + set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR + "${source_dir}" + CACHE INTERNAL "" + ) + set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR + "${binary_dir}" + CACHE INTERNAL "" + ) +endfunction() + +# adds a package as a subdirectory if viable, according to provided options +function( + cpm_add_subdirectory + PACKAGE + DOWNLOAD_ONLY + SOURCE_DIR + BINARY_DIR + EXCLUDE + SYSTEM + OPTIONS +) + + if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) + set(addSubdirectoryExtraArgs "") + if(EXCLUDE) + list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) + endif() + if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25") + # https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM + list(APPEND addSubdirectoryExtraArgs SYSTEM) + endif() + if(OPTIONS) + foreach(OPTION ${OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + set(CPM_OLD_INDENT "${CPM_INDENT}") + set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") + add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) + set(CPM_INDENT "${CPM_OLD_INDENT}") + endif() +endfunction() + +# downloads a previously declared package via FetchContent and exports the variables +# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope +function(cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated) + set(${populated} + FALSE + PARENT_SCOPE + ) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)") + return() + endif() + + FetchContent_GetProperties(${PACKAGE}) + + string(TOLOWER "${PACKAGE}" lower_case_name) + + if(NOT ${lower_case_name}_POPULATED) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3") + if(DOWNLOAD_ONLY) + # MakeAvailable will call add_subdirectory internally which is not what we want when + # DOWNLOAD_ONLY is set. Populate will only download the dependency without adding it to the + # build + FetchContent_Populate( + ${PACKAGE} + SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-src" + BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + SUBBUILD_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild" + ${ARGN} + ) + else() + FetchContent_MakeAvailable(${PACKAGE}) + endif() + else() + FetchContent_Populate(${PACKAGE}) + endif() + set(${populated} + TRUE + PARENT_SCOPE + ) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} + ) + + set(${PACKAGE}_SOURCE_DIR + ${${lower_case_name}_SOURCE_DIR} + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + ${${lower_case_name}_BINARY_DIR} + PARENT_SCOPE + ) +endfunction() + +# splits a package option +function(cpm_parse_option OPTION) + string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") + string(LENGTH "${OPTION}" OPTION_LENGTH) + string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) + if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) + # no value for key provided, assume user wants to set option to "ON" + set(OPTION_VALUE "ON") + else() + math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") + string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) + endif() + set(OPTION_KEY + "${OPTION_KEY}" + PARENT_SCOPE + ) + set(OPTION_VALUE + "${OPTION_VALUE}" + PARENT_SCOPE + ) +endfunction() + +# guesses the package version from a git tag +function(cpm_get_version_from_git_tag GIT_TAG RESULT) + string(LENGTH ${GIT_TAG} length) + if(length EQUAL 40) + # GIT_TAG is probably a git hash + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + endif() +endfunction() + +# guesses if the git tag is a commit hash or an actual tag or a branch name. +function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) + string(LENGTH "${GIT_TAG}" length) + # full hash has 40 characters, and short hash has at least 7 characters. + if(length LESS 7 OR length GREATER 40) + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") + set(${RESULT} + 1 + PARENT_SCOPE + ) + else() + set(${RESULT} + 0 + PARENT_SCOPE + ) + endif() + endif() +endfunction() + +function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + ) + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND) + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + foreach(oneArgName ${oneValueArgs}) + if(DEFINED CPM_ARGS_${oneArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + if(${oneArgName} STREQUAL "SOURCE_DIR") + string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} + ${CPM_ARGS_${oneArgName}} + ) + endif() + string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") + endif() + endforeach() + foreach(multiArgName ${multiValueArgs}) + if(DEFINED CPM_ARGS_${multiArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") + foreach(singleOption ${CPM_ARGS_${multiArgName}}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") + endforeach() + endif() + endforeach() + + if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ") + foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) + string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") + endforeach() + string(APPEND PRETTY_OUT_VAR "\n") + endif() + + set(${OUT_VAR} + ${PRETTY_OUT_VAR} + PARENT_SCOPE + ) + +endfunction() diff --git a/examples/serial_advanced.ts b/examples/serial_advanced.ts new file mode 100644 index 0000000..69f8b9b --- /dev/null +++ b/examples/serial_advanced.ts @@ -0,0 +1,135 @@ +// @ts-nocheck +// deno run --allow-ffi --allow-read examples/serial_advanced.ts --lib ./build/libcpp_unix_bindings.so --port /dev/ttyUSB0 + +/* + Advanced Deno example showcasing the extended C-API helpers: + 1. serialReadLine / serialWriteLine for convenient newline-terminated I/O + 2. serialPeek to inspect next byte without consuming + 3. Tx/Rx statistics counters + 4. serialDrain to wait until all bytes are sent + + Build the shared library first (e.g. via `cmake --build build`). +*/ + +interface CliOptions { + lib: string; + port?: string; +} + +function parseArgs(): CliOptions { + const opts: CliOptions = { lib: "./build/libcpp_unix_bindings.so" }; + + for (let i = 0; i < Deno.args.length; ++i) { + const arg = Deno.args[i]; + if (arg === "--lib" && i + 1 < Deno.args.length) { + opts.lib = Deno.args[++i]; + } else if (arg === "--port" && i + 1 < Deno.args.length) { + opts.port = Deno.args[++i]; + } else { + console.warn(`Unknown argument '${arg}' ignored.`); + } + } + return opts; +} + +// ----------------------------------------------------------------------------- +// Helper utilities for C interop +// ----------------------------------------------------------------------------- +const encoder = new TextEncoder(); +const decoder = new TextDecoder(); + +function cString(str: string): Uint8Array { + const bytes = encoder.encode(str); + const buf = new Uint8Array(bytes.length + 1); + buf.set(bytes, 0); + buf[bytes.length] = 0; + return buf; +} + +function pointer(view: Uint8Array): Deno.UnsafePointer { + return Deno.UnsafePointer.of(view) as Deno.UnsafePointer; +} + +// ----------------------------------------------------------------------------- +// Load dynamic library & bind needed symbols +// ----------------------------------------------------------------------------- +const { lib, port: cliPort } = parseArgs(); + +const dylib = Deno.dlopen( + lib, + { + serialOpen: { parameters: ["pointer", "i32", "i32", "i32", "i32"], result: "pointer" }, + serialClose: { parameters: ["pointer"], result: "void" }, + serialWriteLine: { parameters: ["pointer", "pointer", "i32", "i32"], result: "i32" }, + serialReadLine: { parameters: ["pointer", "pointer", "i32", "i32"], result: "i32" }, + serialPeek: { parameters: ["pointer", "pointer", "i32"], result: "i32" }, + serialDrain: { parameters: ["pointer"], result: "i32" }, + serialGetTxBytes: { parameters: ["pointer"], result: "i64" }, + serialGetRxBytes: { parameters: ["pointer"], result: "i64" }, + } as const, +); + +// ----------------------------------------------------------------------------- +// Open port +// ----------------------------------------------------------------------------- +const portPath = cliPort ?? "/dev/ttyUSB0"; +console.log(`Using port: ${portPath}`); + +const portBuf = cString(portPath); +const handle = dylib.symbols.serialOpen(pointer(portBuf), 115200, 8, 0, 0); +if (handle === null) { + console.error("Failed to open port!"); + dylib.close(); + Deno.exit(1); +} + +// Wait 2 s for Arduino reset (DTR toggle) +await new Promise((r) => setTimeout(r, 2000)); + +// ----------------------------------------------------------------------------- +// 1. Send a few lines and read them back (echo sketch on MCU) +// ----------------------------------------------------------------------------- +const lines = [ + "The quick brown fox jumps over the lazy dog", + "Grüße aus Deno!", + "1234567890", +]; + +for (const ln of lines) { + const payloadBuf = encoder.encode(ln); + const written = dylib.symbols.serialWriteLine(handle, pointer(payloadBuf), payloadBuf.length, 100); + if (written !== payloadBuf.length + 1) { + console.error(`WriteLine failed for '${ln}'`); + continue; + } + + // Peek first byte (should be our first char) + const peekBuf = new Uint8Array(1); + if (dylib.symbols.serialPeek(handle, pointer(peekBuf), 500) === 1) { + console.log(`Peek: '${String.fromCharCode(peekBuf[0])}'`); + } + + const readBuf = new Uint8Array(256); + const n = dylib.symbols.serialReadLine(handle, pointer(readBuf), readBuf.length, 1000); + const lineRx = decoder.decode(readBuf.subarray(0, n)); + console.log(`RX (${n} bytes): '${lineRx}'`); +} + +// Ensure all bytes are transmitted +if (dylib.symbols.serialDrain(handle) === 1) { + console.log("Transmit buffer drained."); +} + +// ----------------------------------------------------------------------------- +// Print statistics +// ----------------------------------------------------------------------------- +const txBytes = Number(dylib.symbols.serialGetTxBytes(handle)); +const rxBytes = Number(dylib.symbols.serialGetRxBytes(handle)); +console.log(`\nStatistics -> TX: ${txBytes} bytes, RX: ${rxBytes} bytes`); + +// ----------------------------------------------------------------------------- +// Cleanup +// ----------------------------------------------------------------------------- +dylib.symbols.serialClose(handle); +dylib.close(); +console.log("Done."); diff --git a/examples/serial_echo.ts b/examples/serial_echo.ts new file mode 100644 index 0000000..d9b8905 --- /dev/null +++ b/examples/serial_echo.ts @@ -0,0 +1,134 @@ +// @ts-nocheck +// deno run --allow-ffi --allow-read examples/serial_echo.ts + +interface CliOptions { + lib: string; + port?: string; +} + +function parseArgs(): CliOptions { + const opts: CliOptions = { lib: "./build/libcpp_unix_bindings.so" }; + + for (let i = 0; i < Deno.args.length; ++i) { + const arg = Deno.args[i]; + if (arg === "--lib" && i + 1 < Deno.args.length) { + opts.lib = Deno.args[++i]; + } else if (arg === "--port" && i + 1 < Deno.args.length) { + opts.port = Deno.args[++i]; + } else { + console.warn(`Unknown argument '${arg}' ignored.`); + } + } + return opts; +} + +// ----------------------------------------------------------------------------- +// Helper utilities for C interop +// ----------------------------------------------------------------------------- +const encoder = new TextEncoder(); +const decoder = new TextDecoder(); + +function cString(str: string): Uint8Array { + // Encodes JavaScript string as null-terminated UTF-8 byte array. + const bytes = encoder.encode(str); + const buf = new Uint8Array(bytes.length + 1); + buf.set(bytes, 0); + buf[bytes.length] = 0; + return buf; +} + +function pointer(view: Uint8Array): Deno.UnsafePointer { + return Deno.UnsafePointer.of(view) as Deno.UnsafePointer; +} + +// ----------------------------------------------------------------------------- +// Load dynamic library +// ----------------------------------------------------------------------------- +const { lib, port: cliPort } = parseArgs(); + +const dylib = Deno.dlopen( + lib, + { + serialOpen: { parameters: ["pointer", "i32", "i32", "i32", "i32"], result: "pointer" }, + serialClose: { parameters: ["pointer"], result: "void" }, + serialWrite: { parameters: ["pointer", "pointer", "i32", "i32", "i32"], result: "i32" }, + serialReadUntil: { + parameters: ["pointer", "pointer", "i32", "i32", "i32", "pointer"], + result: "i32", + }, + serialGetPortsInfo: { parameters: ["pointer", "i32", "pointer"], result: "i32" }, + } as const, +); + +// ----------------------------------------------------------------------------- +// 1. List available ports +// ----------------------------------------------------------------------------- +const sepBuf = cString(";"); +const portsBuf = new Uint8Array(4096); +dylib.symbols.serialGetPortsInfo( + pointer(portsBuf), + portsBuf.length, + pointer(sepBuf), +); + +const cPortsStr = decoder.decode(portsBuf.subarray(0, portsBuf.indexOf(0))); +const ports = cPortsStr ? cPortsStr.split(";") : []; +console.log("Available ports:"); +for (const p of ports) { + console.log(" •", p); +} +if (ports.length === 0) { + console.error("No serial ports found (ttyUSB). Exiting."); + dylib.close(); + Deno.exit(1); +} + +// ----------------------------------------------------------------------------- +// 2. Echo test on selected port +// ----------------------------------------------------------------------------- +const portPath = cliPort ?? ports[0]; +console.log(`\nUsing port: ${portPath}`); + +const portBuf = cString(portPath); +const handle = dylib.symbols.serialOpen(pointer(portBuf), 115200, 8, 0, 0); +if (handle === null) { + console.error("Failed to open port!"); + dylib.close(); + Deno.exit(1); +} + +// Give MCU a moment to reboot (similar to C++ tests) +await new Promise((r) => setTimeout(r, 2000)); + +const msg = "HELLO\n"; +const msgBuf = encoder.encode(msg); +const written = dylib.symbols.serialWrite(handle, pointer(msgBuf), msgBuf.length, 100, 1); +if (written !== msgBuf.length) { + console.error(`Write failed (wrote ${written}/${msgBuf.length})`); + dylib.symbols.serialClose(handle); + dylib.close(); + Deno.exit(1); +} + +const readBuf = new Uint8Array(64); +const untilBuf = new Uint8Array(["\n".charCodeAt(0)]); + +const read = dylib.symbols.serialReadUntil(handle, pointer(readBuf), readBuf.length, 500, 1, pointer(untilBuf)); +if (read <= 0) { + console.error("Read failed or timed out."); + dylib.symbols.serialClose(handle); + dylib.close(); + Deno.exit(1); +} + +const echo = decoder.decode(readBuf.subarray(0, read)); +console.log(`Echo response (${read} bytes): '${echo}'`); + +if (echo === msg) { + console.log("Echo test: ✅ success"); +} else { + console.error("Echo test: ❌ mismatch"); +} + +dylib.symbols.serialClose(handle); +dylib.close(); diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index e7e4aeb..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,182 +0,0 @@ -#include -#include -#include -#include - -class SerialPort { -private: - HANDLE hSerial; - OVERLAPPED readOverlap; - OVERLAPPED writeOverlap; - -public: - SerialPort() : hSerial(INVALID_HANDLE_VALUE) { - ZeroMemory(&readOverlap, sizeof(OVERLAPPED)); - ZeroMemory(&writeOverlap, sizeof(OVERLAPPED)); - readOverlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - writeOverlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - } - - ~SerialPort() { - if (hSerial != INVALID_HANDLE_VALUE) { - CloseHandle(hSerial); - } - if (readOverlap.hEvent) CloseHandle(readOverlap.hEvent); - if (writeOverlap.hEvent) CloseHandle(writeOverlap.hEvent); - } - - bool open(const char* portName, int baudRate) { - std::string port = "\\\\.\\"; - port += portName; - - hSerial = CreateFileA( - port.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_FLAG_OVERLAPPED, - NULL - ); - - if (hSerial == INVALID_HANDLE_VALUE) { - return false; - } - - DCB dcbSerialParams = { 0 }; - dcbSerialParams.DCBlength = sizeof(dcbSerialParams); - - if (!GetCommState(hSerial, &dcbSerialParams)) { - CloseHandle(hSerial); - hSerial = INVALID_HANDLE_VALUE; - return false; - } - - dcbSerialParams.BaudRate = baudRate; - dcbSerialParams.ByteSize = 8; - dcbSerialParams.StopBits = ONESTOPBIT; - dcbSerialParams.Parity = NOPARITY; - - if (!SetCommState(hSerial, &dcbSerialParams)) { - CloseHandle(hSerial); - hSerial = INVALID_HANDLE_VALUE; - return false; - } - - COMMTIMEOUTS timeouts = { 0 }; - timeouts.ReadIntervalTimeout = MAXDWORD; - timeouts.ReadTotalTimeoutMultiplier = 0; - timeouts.ReadTotalTimeoutConstant = 0; - timeouts.WriteTotalTimeoutMultiplier = 0; - timeouts.WriteTotalTimeoutConstant = 0; - - if (!SetCommTimeouts(hSerial, &timeouts)) { - CloseHandle(hSerial); - hSerial = INVALID_HANDLE_VALUE; - return false; - } - - return true; - } - - bool close() { - if (hSerial != INVALID_HANDLE_VALUE) { - CloseHandle(hSerial); - hSerial = INVALID_HANDLE_VALUE; - return true; - } - return false; - } - - int readAsync(void* buffer, int bufferSize) { - if (hSerial == INVALID_HANDLE_VALUE) { - return 0; - } - - DWORD bytesRead = 0; - ResetEvent(readOverlap.hEvent); - - if (!ReadFile(hSerial, buffer, bufferSize, &bytesRead, &readOverlap)) { - if (GetLastError() == ERROR_IO_PENDING) { - if (WaitForSingleObject(readOverlap.hEvent, INFINITE) == WAIT_OBJECT_0) { - GetOverlappedResult(hSerial, &readOverlap, &bytesRead, FALSE); - } - } - } - return bytesRead; - } - - int writeAsync(void* buffer, int bufferSize) { - if (hSerial == INVALID_HANDLE_VALUE) { - return 0; - } - - DWORD bytesWritten = 0; - ResetEvent(writeOverlap.hEvent); - - if (!WriteFile(hSerial, buffer, bufferSize, &bytesWritten, &writeOverlap)) { - if (GetLastError() == ERROR_IO_PENDING) { - if (WaitForSingleObject(writeOverlap.hEvent, INFINITE) == WAIT_OBJECT_0) { - GetOverlappedResult(hSerial, &writeOverlap, &bytesWritten, FALSE); - } - } - } - return bytesWritten; - } -}; - -static SerialPort* port = nullptr; - -extern "C" { - __declspec(dllexport) int OpenSerialPort( - const uint8_t* portName, - int baudRate - ) { - return 0; - } - - __declspec(dllexport) int CloseSerialPort() { - try { - if (port != nullptr) { - bool result = port->close(); - delete port; - port = nullptr; - return result ? 1 : 0; - } - return 0; - } - catch (...) { - return 0; - } - } - - __declspec(dllexport) int ReadSerialPort( - void* buffer, - int bufferSize - ) { - try { - if (port == nullptr) { - return 0; - } - return port->readAsync(buffer, bufferSize); - } - catch (...) { - return 0; - } - } - - __declspec(dllexport) int WriteSerialPort( - void* buffer, - int bufferSize - ) { - try { - if (port == nullptr) { - return 0; - } - return port->writeAsync(buffer, bufferSize); - } - catch (...) { - return 0; - } - } -} diff --git a/src/serial.cpp b/src/serial.cpp new file mode 100644 index 0000000..8fcc98f --- /dev/null +++ b/src/serial.cpp @@ -0,0 +1,681 @@ +#include "serial.h" + +#include "status_codes.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// ----------------------------------------------------------------------------- +// Global callback function pointers (default nullptr) +// ----------------------------------------------------------------------------- +void (*error_callback)(int) = nullptr; +void (*read_callback)(int) = nullptr; +void (*write_callback)(int) = nullptr; + +// ----------------------------------------------------------------------------- +// Internal helpers & types +// ----------------------------------------------------------------------------- +namespace +{ + +struct SerialPortHandle +{ + int file_descriptor; + termios original; // keep original settings so we can restore on close + + int64_t rx_total{0}; // bytes received so far + int64_t tx_total{0}; // bytes transmitted so far + + bool has_peek{false}; + char peek_char{0}; + + std::atomic abort_read{false}; + std::atomic abort_write{false}; +}; + +// Map integer baudrate to POSIX speed_t. Only common rates are supported. +auto to_speed_t(int baud) -> speed_t +{ + switch (baud) + { + case 0: + return B0; + case 50: + return B50; + case 75: + return B75; + case 110: + return B110; + case 134: + return B134; + case 150: + return B150; + case 200: + return B200; + case 300: + return B300; + case 600: + return B600; + case 1200: + return B1200; + case 1800: + return B1800; + case 2400: + return B2400; + case 4800: + return B4800; + case 9600: + return B9600; + case 19200: + return B19200; + case 38400: + return B38400; + case 57600: + return B57600; + case 115200: + return B115200; + case 230400: + return B230400; +#ifdef B460800 + case 460800: + return B460800; +#endif +#ifdef B921600 + case 921600: + return B921600; +#endif + default: + return B9600; // fallback + } +} + +inline void invokeError(int code) +{ + if (error_callback != nullptr) + { + error_callback(code); + } +} + +} // namespace + +// ----------------------------------------------------------------------------- +// Public API implementation +// ----------------------------------------------------------------------------- + +intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stopBits) +{ + if (port == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + auto port_name = std::string_view{static_cast(port)}; + int device_descriptor = open(port_name.data(), O_RDWR | O_NOCTTY | O_SYNC); + if (device_descriptor < 0) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + auto* handle = new SerialPortHandle{.file_descriptor = device_descriptor, .original = {}}; + + termios tty{}; + if (tcgetattr(device_descriptor, &tty) != 0) + { + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + close(device_descriptor); + delete handle; + return 0; + } + handle->original = tty; // save original + + // Basic flags: local connection, enable receiver + tty.c_cflag |= (CLOCAL | CREAD); + + // Baudrate + const speed_t speed = to_speed_t(baudrate); + cfsetispeed(&tty, speed); + cfsetospeed(&tty, speed); + + // Data bits + tty.c_cflag &= ~CSIZE; + switch (dataBits) + { + case 5: + tty.c_cflag |= CS5; + break; + case 6: + tty.c_cflag |= CS6; + break; + case 7: + tty.c_cflag |= CS7; + break; + default: + tty.c_cflag |= CS8; + break; + } + + // Parity + if (parity == 0) + { + tty.c_cflag &= ~PARENB; + } + else + { + tty.c_cflag |= PARENB; + if (parity == 1) + { + tty.c_cflag &= ~PARODD; // even + } + else + { + tty.c_cflag |= PARODD; // odd + } + } + + // Stop bits + if (stopBits == 2) + { + tty.c_cflag |= CSTOPB; + } + else + { + tty.c_cflag &= ~CSTOPB; + } + + // Raw mode (no echo/processing) + tty.c_iflag = 0; + tty.c_oflag = 0; + tty.c_lflag = 0; + + tty.c_cc[VMIN] = 0; // non-blocking by default + tty.c_cc[VTIME] = 10; // 1s read timeout + + if (tcsetattr(device_descriptor, TCSANOW, &tty) != 0) + { + invokeError(std::to_underlying(StatusCodes::SET_STATE_ERROR)); + close(device_descriptor); + delete handle; + return 0; + } + + return reinterpret_cast(handle); +} + +void serialClose(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return; + } + + tcsetattr(handle->file_descriptor, TCSANOW, &handle->original); // restore + if (close(handle->file_descriptor) != 0) + { + invokeError(std::to_underlying(StatusCodes::CLOSE_HANDLE_ERROR)); + } + delete handle; +} + +static int waitFdReady(int fileDescriptor, int timeoutMs, bool wantWrite) +{ + timeoutMs = std::max(timeoutMs, 0); + + fd_set descriptor_set; + FD_ZERO(&descriptor_set); + FD_SET(fileDescriptor, &descriptor_set); + + timeval wait_time{}; + wait_time.tv_sec = timeoutMs / 1000; + wait_time.tv_usec = (timeoutMs % 1000) * 1000; + + int ready_result = + select(fileDescriptor + 1, wantWrite ? nullptr : &descriptor_set, wantWrite ? &descriptor_set : nullptr, nullptr, &wait_time); + return ready_result; // 0 timeout, -1 error, >0 ready +} + +int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + // Abort check + if (handle->abort_read.exchange(false)) + { + return 0; + } + + int total_copied = 0; + + // First deliver byte from internal peek buffer if present + if (handle->has_peek && bufferSize > 0) + { + static_cast(buffer)[0] = handle->peek_char; + handle->has_peek = false; + handle->rx_total += 1; + total_copied = 1; + buffer = static_cast(buffer) + 1; + bufferSize -= 1; + if (bufferSize == 0) + { + if (read_callback != nullptr) + { + read_callback(total_copied); + } + return total_copied; + } + } + + if (waitFdReady(handle->file_descriptor, timeout, false) <= 0) + { + return total_copied; // return what we may have already copied (could be 0) + } + + ssize_t bytes_read_system = read(handle->file_descriptor, buffer, bufferSize); + if (bytes_read_system < 0) + { + invokeError(std::to_underlying(StatusCodes::READ_ERROR)); + return total_copied; + } + + if (bytes_read_system > 0) + { + handle->rx_total += bytes_read_system; + } + + total_copied += static_cast(bytes_read_system); + + if (read_callback != nullptr) + { + read_callback(total_copied); + } + return total_copied; +} + +int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeout, int /*multiplier*/) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + // Abort check + if (handle->abort_write.exchange(false)) + { + return 0; + } + + if (waitFdReady(handle->file_descriptor, timeout, true) <= 0) + { + return 0; // timeout or error + } + + ssize_t bytes_written_system = write(handle->file_descriptor, buffer, bufferSize); + if (bytes_written_system < 0) + { + invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); + return 0; + } + + if (bytes_written_system > 0) + { + handle->tx_total += bytes_written_system; + } + + if (write_callback != nullptr) + { + write_callback(static_cast(bytes_written_system)); + } + return static_cast(bytes_written_system); +} + +int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + char until_character = *static_cast(untilCharPtr); + int total = 0; + auto* char_buffer = static_cast(buffer); + + while (total < bufferSize) + { + int read_result = serialRead(handlePtr, char_buffer + total, 1, timeout, 1); + if (read_result <= 0) + { + break; // timeout or error + } + if (char_buffer[total] == until_character) + { + total += 1; + break; + } + total += read_result; + } + + if (read_callback != nullptr) + { + read_callback(total); + } + return total; +} + +int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) +{ + auto sep = std::string_view{static_cast(separatorPtr)}; + std::string result; + + namespace fs = std::filesystem; + + const fs::path by_id_dir{"/dev/serial/by-id"}; + if (!fs::exists(by_id_dir) || !fs::is_directory(by_id_dir)) + { + invokeError(std::to_underlying(StatusCodes::NOT_FOUND_ERROR)); + return 0; + } + + try + { + for (const auto& entry : fs::directory_iterator{by_id_dir}) + { + if (!entry.is_symlink()) + { + continue; + } + + std::error_code error_code; + fs::path canonical = fs::canonical(entry.path(), error_code); + if (error_code) + { + continue; // skip entries we cannot resolve + } + + result += canonical.string(); + result += sep; + } + } + catch (const fs::filesystem_error&) + { + invokeError(std::to_underlying(StatusCodes::NOT_FOUND_ERROR)); + return 0; + } + + if (!result.empty()) + { + // Remove the trailing separator + result.erase(result.size() - sep.size()); + } + + if (static_cast(result.size()) + 1 > bufferSize) + { + invokeError(std::to_underlying(StatusCodes::BUFFER_ERROR)); + return 0; + } + + std::memcpy(buffer, result.c_str(), result.size() + 1); + return result.empty() ? 0 : 1; // number of ports not easily counted here +} + +// ----------------------------------------------------------------------------- +// Buffer & abort helpers implementations +// ----------------------------------------------------------------------------- + +void serialClearBufferIn(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return; + } + tcflush(handle->file_descriptor, TCIFLUSH); + // reset peek buffer + handle->has_peek = false; +} + +void serialClearBufferOut(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return; + } + tcflush(handle->file_descriptor, TCOFLUSH); +} + +void serialAbortRead(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return; + } + handle->abort_read = true; +} + +void serialAbortWrite(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return; + } + handle->abort_write = true; +} + +// ----------------------------------------------------------------------------- + +// Callback registration +void serialOnError(void (*func)(int code)) +{ + error_callback = func; +} +void serialOnRead(void (*func)(int bytes)) +{ + read_callback = func; +} +void serialOnWrite(void (*func)(int bytes)) +{ + write_callback = func; +} + +// ----------------------------------------------------------------------------- +// Extended helper APIs (read line, token, frame, statistics, etc.) +// ----------------------------------------------------------------------------- + +int serialReadLine(int64_t handlePtr, void* buffer, int bufferSize, int timeout) +{ + char newline = '\n'; + return serialReadUntil(handlePtr, buffer, bufferSize, timeout, 1, &newline); +} + +int serialWriteLine(int64_t handlePtr, const void* buffer, int bufferSize, int timeout) +{ + // First write the payload + int written = serialWrite(handlePtr, buffer, bufferSize, timeout, 1); + if (written != bufferSize) + { + return written; // error path, propagate + } + // Append newline (\n) + char new_line_char = '\n'; + int newline_result = serialWrite(handlePtr, &new_line_char, 1, timeout, 1); + if (newline_result != 1) + { + return written; // newline failed, but payload written + } + return written + 1; +} + +int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int timeout, void* tokenPtr) +{ + const auto* token_cstr = static_cast(tokenPtr); + if (token_cstr == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + std::string token{token_cstr}; + int token_len = static_cast(token.size()); + if (token_len == 0 || bufferSize < token_len) + { + return 0; + } + + auto* char_buffer = static_cast(buffer); + int total = 0; + int matched = 0; // how many chars of token matched so far + + while (total < bufferSize) + { + int read_result = serialRead(handlePtr, char_buffer + total, 1, timeout, 1); + if (read_result <= 0) + { + break; // timeout or error + } + + char current_char = char_buffer[total]; + total += 1; + + if (current_char == token[matched]) + { + matched += 1; + if (matched == token_len) + { + break; // token fully matched + } + } + else + { + matched = (current_char == token[0]) ? 1 : 0; // restart match search + } + } + + if (read_callback != nullptr) + { + read_callback(total); + } + return total; +} + +int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout, char startByte, char endByte) +{ + auto* char_buffer = static_cast(buffer); + int total = 0; + bool in_frame = false; + + while (total < bufferSize) + { + char current_byte; + int read_result = serialRead(handlePtr, ¤t_byte, 1, timeout, 1); + if (read_result <= 0) + { + break; // timeout + } + + if (!in_frame) + { + if (current_byte == startByte) + { + in_frame = true; + char_buffer[total++] = current_byte; + } + continue; // ignore bytes until start byte detected + } + + char_buffer[total++] = current_byte; + if (current_byte == endByte) + { + break; // frame finished + } + } + + return total; +} + +int64_t serialGetRxBytes(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return 0; + } + return handle->rx_total; +} + +int64_t serialGetTxBytes(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + return 0; + } + return handle->tx_total; +} + +int serialPeek(int64_t handlePtr, void* outByte, int timeout) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr || outByte == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + if (handle->has_peek) + { + *static_cast(outByte) = handle->peek_char; + return 1; + } + + char received_byte; + int read_outcome = serialRead(handlePtr, &received_byte, 1, timeout, 1); + if (read_outcome <= 0) + { + return 0; // nothing available + } + + // Store into peek buffer and undo stats increment + handle->peek_char = received_byte; + handle->has_peek = true; + if (handle->rx_total > 0) + { + handle->rx_total -= 1; // don't account peek + } + + *static_cast(outByte) = received_byte; + return 1; +} + +int serialDrain(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + return (tcdrain(handle->file_descriptor) == 0) ? 1 : 0; +} diff --git a/src/serial.h b/src/serial.h new file mode 100644 index 0000000..fc6285d --- /dev/null +++ b/src/serial.h @@ -0,0 +1,64 @@ +#pragma once +#include + +#define MODULE_API __attribute__((visibility("default"))) + +#ifdef __cplusplus +extern "C" +{ +#endif + + // Version helpers generated at configure time + MODULE_API unsigned int getMajorVersion(); + MODULE_API unsigned int getMinorVersion(); + MODULE_API unsigned int getPatchVersion(); + + // Basic serial API + MODULE_API intptr_t + serialOpen(void* port, int baudrate, int dataBits, int parity /*0-none,1-even,2-odd*/ = 0, int stopBits /*0-1bit,2-2bit*/ = 0); + + MODULE_API void serialClose(int64_t handle); + + MODULE_API int serialRead(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, int multiplier); + + MODULE_API int serialReadUntil(int64_t handle, void* buffer, int bufferSize, int timeout, int multiplier, void* untilChar); + + MODULE_API int serialWrite(int64_t handle, const void* buffer, int bufferSize, int timeout, int multiplier); + + MODULE_API int serialGetPortsInfo(void* buffer, int bufferSize, void* separator); + + MODULE_API void serialClearBufferIn(int64_t handle); + MODULE_API void serialClearBufferOut(int64_t handle); + MODULE_API void serialAbortRead(int64_t handle); + MODULE_API void serialAbortWrite(int64_t handle); + + // Optional callback hooks (can be nullptr) + extern void (*error_callback)(int errorCode); + extern void (*read_callback)(int bytes); + extern void (*write_callback)(int bytes); + + MODULE_API void serialOnError(void (*func)(int code)); + MODULE_API void serialOnRead(void (*func)(int bytes)); + MODULE_API void serialOnWrite(void (*func)(int bytes)); + + MODULE_API int serialReadLine(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/); + + MODULE_API int serialWriteLine(int64_t handle, const void* buffer, int bufferSize, int timeout /*ms*/); + + MODULE_API int serialReadUntilToken(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, void* token); + + MODULE_API int serialReadFrame(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, char startByte, char endByte); + + // Byte statistics + MODULE_API int64_t serialGetRxBytes(int64_t handle); + MODULE_API int64_t serialGetTxBytes(int64_t handle); + + // Peek next byte without consuming + MODULE_API int serialPeek(int64_t handle, void* outByte, int timeout /*ms*/); + + // Drain pending TX bytes (wait until sent) + MODULE_API int serialDrain(int64_t handle); + +#ifdef __cplusplus +} +#endif diff --git a/src/status_codes.h b/src/status_codes.h new file mode 100644 index 0000000..a48b8c8 --- /dev/null +++ b/src/status_codes.h @@ -0,0 +1,19 @@ +#pragma once + +enum class StatusCodes +{ + SUCCESS = 0, + CLOSE_HANDLE_ERROR = -1, + INVALID_HANDLE_ERROR = -2, + READ_ERROR = -3, + WRITE_ERROR = -4, + GET_STATE_ERROR = -5, + SET_STATE_ERROR = -6, + SET_TIMEOUT_ERROR = -7, + BUFFER_ERROR = -8, + NOT_FOUND_ERROR = -9, + CLEAR_BUFFER_IN_ERROR = -10, + CLEAR_BUFFER_OUT_ERROR = -11, + ABORT_READ_ERROR = -12, + ABORT_WRITE_ERROR = -13, +}; diff --git a/src/test.cpp b/src/test.cpp deleted file mode 100644 index 101b1ca..0000000 --- a/src/test.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -extern "C" __declspec(dllexport) int TestFunction() { - return 42; -} - -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { - return TRUE; -} diff --git a/tests/serial_test.cpp b/tests/serial_test.cpp new file mode 100644 index 0000000..73dd0ed --- /dev/null +++ b/tests/serial_test.cpp @@ -0,0 +1,105 @@ +// Simple integration test for cpp_unix_bindings +// ------------------------------------------------ +// This executable opens the given serial port, sends a test +// string and verifies that the same string is echoed back +// by the micro-controller. +// +// ------------------------------------------------ +// Arduino sketch to flash for the tests +// ------------------------------------------------ +/* + // --- BEGIN ARDUINO CODE --- + void setup() { + Serial.begin(115200); + // Wait until the host opens the port (optional but handy) + while (!Serial) { + ; + } + } + + void loop() { + if (Serial.available()) { + char c = Serial.read(); + Serial.write(c); // echo back + } + } + // --- END ARDUINO CODE --- +*/ +// ------------------------------------------------ + +#include "serial.h" + +#include +#include +#include +#include +#include +#include + +namespace +{ +const char* default_port = "/dev/ttyUSB0"; +} // namespace + +TEST(SerialEchoTest, EchoMessage) +{ + const std::string test_msg = "HELLO"; + + intptr_t handle = serialOpen((void*)default_port, 115200, 8, 0, 0); + ASSERT_NE(handle, 0) << "Failed to open port " << default_port; + + // Opening a serial connection toggles DTR on most Arduino boards, which + // triggers a reset. Give the micro-controller a moment to reboot before we + // start talking to it, otherwise the first bytes might be lost. + std::this_thread::sleep_for(std::chrono::seconds(2)); + + // Send message + int written = serialWrite(handle, (void*)test_msg.c_str(), static_cast(test_msg.size()), 100, 1); + ASSERT_EQ(written, static_cast(test_msg.size())) << "Write failed"; + + // Read echo + std::array read_buffer{}; + int bytes_read = serialRead(handle, read_buffer.data(), static_cast(test_msg.size()), 500, 1); + ASSERT_EQ(bytes_read, static_cast(test_msg.size())) << "Read failed (got " << bytes_read << ")"; + + ASSERT_EQ(std::strncmp(read_buffer.data(), test_msg.c_str(), test_msg.size()), 0) + << "Data mismatch: expected " << test_msg << ", got " << read_buffer.data(); + + serialClose(handle); +} + +TEST(SerialReadUntilTest, ReadUntilChar) +{ + const std::string test_msg = "WORLD\n"; // include terminator newline + + intptr_t handle = serialOpen((void*)default_port, 115200, 8, 0, 0); + ASSERT_NE(handle, 0) << "Failed to open port " << default_port; + + // Give the board time to reset after opening the port. + std::this_thread::sleep_for(std::chrono::seconds(2)); + + // Send message (write includes the terminator char) + int written = serialWrite(handle, (void*)test_msg.c_str(), static_cast(test_msg.size()), 100, 1); + ASSERT_EQ(written, static_cast(test_msg.size())) << "Write failed"; + + // Read back until newline (inclusive) + std::array buffer{0}; + char until = '\n'; + int read_bytes = serialReadUntil(handle, buffer.data(), static_cast(buffer.size()), 500, 1, &until); + ASSERT_EQ(read_bytes, static_cast(test_msg.size())) << "serialReadUntil returned unexpected length"; + + ASSERT_EQ(std::strncmp(buffer.data(), test_msg.c_str(), test_msg.size()), 0) + << "Data mismatch: expected " << test_msg << ", got " << buffer.data(); + + serialClose(handle); +} + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + if (argc > 1) + { + default_port = argv[1]; + } + return RUN_ALL_TESTS(); +} diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp new file mode 100644 index 0000000..6f87f4b --- /dev/null +++ b/tests/serial_unit_tests.cpp @@ -0,0 +1,235 @@ +#include "serial.h" +#include "status_codes.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace +{ +// Helper storage for callback tests +std::atomic* g_err_ptr = nullptr; + +void errorCallback(int code) +{ + if (g_err_ptr != nullptr) + { + *g_err_ptr = code; + } +} + +// Helper to resolve serial port path (env var override) +const char* getDefaultPort() +{ + const char* env = std::getenv("SERIAL_PORT"); + return (env != nullptr) ? env : "/dev/ttyUSB0"; +} + +struct SerialDevice +{ + intptr_t handle{0}; + const char* port{nullptr}; + + explicit SerialDevice(int baud = 115200) + { + port = getDefaultPort(); + handle = serialOpen((void*)port, baud, 8, 0, 0); + if (handle == 0) + { + throw std::runtime_error(std::string{"Failed to open port "} + port); + } + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // give Arduino time to reboot after DTR toggle + } + + ~SerialDevice() + { + if (handle != 0) + { + serialClose(handle); + } + } + + void writeToDevice(std::string_view data) const + { + serialWrite(handle, data.data(), static_cast(data.size()), 500, 1); + } +}; + +} // namespace + +// ------------------------------- Error path -------------------------------- +TEST(SerialOpenTest, InvalidPathInvokesErrorCallback) +{ + std::atomic err_code{0}; + + g_err_ptr = &err_code; + serialOnError(errorCallback); + + intptr_t handle = serialOpen((void*)"/dev/__does_not_exist__", 115200, 8, 0, 0); + EXPECT_EQ(handle, 0); + EXPECT_EQ(err_code.load(), static_cast(StatusCodes::INVALID_HANDLE_ERROR)); + + // Reset to nullptr so other tests don't see our callback + serialOnError(nullptr); +} + +// ------------------------ serialGetPortsInfo checks ------------------------ +TEST(SerialGetPortsInfoTest, BufferTooSmallTriggersError) +{ + constexpr std::string_view separator{";"}; + std::array info_buffer{}; + std::atomic err_code{0}; + + g_err_ptr = &err_code; + serialOnError(errorCallback); + + int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); + EXPECT_EQ(result, 0); // function indicates failure via 0 + EXPECT_EQ(err_code.load(), static_cast(StatusCodes::BUFFER_ERROR)); + + serialOnError(nullptr); +} + +TEST(SerialGetPortsInfoTest, LargeBufferReturnsZeroOrOne) +{ + constexpr std::string_view separator{";"}; + std::array info_buffer{}; + + std::atomic err_code{0}; + g_err_ptr = &err_code; + serialOnError(errorCallback); + + int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); + EXPECT_GE(result, 0); + // res is 0 (no ports) or 1 (ports found) + EXPECT_LE(result, 1); + // Acceptable error codes: none or NOT_FOUND_ERROR (e.g., dir missing) + if (err_code != 0) + { + EXPECT_EQ(err_code.load(), static_cast(StatusCodes::NOT_FOUND_ERROR)); + } + + serialOnError(nullptr); +} + +// ---------------------------- Port listing helper --------------------------- +TEST(SerialGetPortsInfoTest, PrintAvailablePorts) +{ + constexpr std::string_view separator{";"}; + std::array info_buffer{}; + + int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); + EXPECT_GE(result, 0); + + std::string ports_str(info_buffer.data()); + if (!ports_str.empty()) + { + std::cout << "\nAvailable serial ports (by-id):\n"; + size_t start = 0; + while (true) + { + size_t pos = ports_str.find(separator.data(), start); + std::string token = ports_str.substr(start, pos - start); + std::cout << " " << token << "\n"; + if (pos == std::string::npos) + { + break; + } + start = pos + std::strlen(separator.data()); + } + } + else + { + std::cout << "\nNo serial devices found in /dev/serial/by-id\n"; + } +} + +// --------------------------- Stubbed no-op APIs ---------------------------- +TEST(SerialStubbedFunctions, DoNotCrash) +{ + serialClearBufferIn(0); + serialClearBufferOut(0); + serialAbortRead(0); + serialAbortWrite(0); + SUCCEED(); // reached here without segfaults +} + +TEST(SerialHelpers, ReadLine) +{ + SerialDevice dev; + const std::string msg = "Hello World\n"; + dev.writeToDevice(msg); + + std::array read_buffer{}; + int num_read = serialReadLine(dev.handle, read_buffer.data(), static_cast(read_buffer.size()), 2000); + ASSERT_EQ(num_read, static_cast(msg.size())); + ASSERT_EQ(std::string_view(read_buffer.data(), num_read), msg); +} + +TEST(SerialHelpers, ReadUntilToken) +{ + SerialDevice dev; + const std::string payload = "ABC_OK"; + dev.writeToDevice(payload); + + std::array read_buffer{}; + constexpr std::string_view ok_token{"OK"}; + int num_read = serialReadUntilToken(dev.handle, read_buffer.data(), static_cast(read_buffer.size()), 2000, (void*)ok_token.data()); + ASSERT_EQ(num_read, static_cast(payload.size())); + ASSERT_EQ(std::string_view(read_buffer.data(), num_read), payload); +} + +TEST(SerialHelpers, Peek) +{ + SerialDevice dev; + const std::string payload = "XYZ"; + dev.writeToDevice(payload); + + char first_byte = 0; + int peek_result = serialPeek(dev.handle, &first_byte, 2000); + ASSERT_EQ(peek_result, 1); + ASSERT_EQ(first_byte, 'X'); + + std::array read_buffer{}; + int num_read = serialRead(dev.handle, read_buffer.data(), 3, 2000, 1); + ASSERT_EQ(num_read, 3); + ASSERT_EQ(std::string_view(read_buffer.data(), 3), payload); +} + +TEST(SerialHelpers, Statistics) +{ + SerialDevice dev; + const std::string payload = "0123456789"; + + // Transmit to device + int written = serialWrite(dev.handle, payload.c_str(), static_cast(payload.size()), 2000, 1); + ASSERT_EQ(written, static_cast(payload.size())); + + // Drain and read echo back + serialDrain(dev.handle); + + std::array read_buffer{}; + int bytes_read = serialRead(dev.handle, read_buffer.data(), static_cast(payload.size()), 2000, 1); + ASSERT_EQ(bytes_read, static_cast(payload.size())); + + ASSERT_EQ(serialGetTxBytes(dev.handle), static_cast(payload.size())); + ASSERT_EQ(serialGetRxBytes(dev.handle), static_cast(payload.size())); +} + +TEST(SerialHelpers, Drain) +{ + SerialDevice dev; + const std::string payload = "TEXT"; + int written = serialWriteLine(dev.handle, payload.c_str(), static_cast(payload.size()), 2000); + ASSERT_GT(written, 0); + ASSERT_EQ(serialDrain(dev.handle), 1); +} diff --git a/versioning/version_config.cpp.in b/versioning/version_config.cpp.in new file mode 100644 index 0000000..8d095d8 --- /dev/null +++ b/versioning/version_config.cpp.in @@ -0,0 +1,14 @@ +unsigned int getMajorVersion() +{ + return @VERSION_MAJOR@; +} + +unsigned int getMinorVersion() +{ + return @VERSION_MINOR@; +} + +unsigned int getPatchVersion() +{ + return @VERSION_PATCH@; +} \ No newline at end of file From 978cdcd96dc8ed2c3eae43a2ad6fa1e3360928d2 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 14:34:12 +0200 Subject: [PATCH 02/22] add windoof --- .devcontainer/Dockerfile.dev | 13 + .devcontainer/devcontainer.json | 28 ++ .gitignore | 5 + CMakeLists.txt | 4 +- src/serial.cpp | 652 +++++++++++++++----------------- src/serial.h | 7 +- tests/serial_test.cpp | 2 +- tests/serial_unit_tests.cpp | 10 +- 8 files changed, 354 insertions(+), 367 deletions(-) create mode 100644 .devcontainer/Dockerfile.dev create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore diff --git a/.devcontainer/Dockerfile.dev b/.devcontainer/Dockerfile.dev new file mode 100644 index 0000000..276d9fe --- /dev/null +++ b/.devcontainer/Dockerfile.dev @@ -0,0 +1,13 @@ +FROM fedora:42 + +RUN dnf install -y \ + clang \ + clang-tools-extra \ + clangd \ + clang-format \ + clang-tidy \ + clang-analyzer \ + cmake \ + git \ + ninja-build \ + && dnf clean all diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..2aecf4e --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,28 @@ +{ + "build": { + "dockerfile": "./Dockerfile.dev" + }, + "customizations": { + "vscode": { + "extensions": [ + "mhutchie.git-graph", + "ms-azuretools.vscode-docker", + "vivaxy.vscode-conventional-commits", + "github.vscode-github-actions", + "llvm-vs-code-extensions.vscode-clangd", + "ms-vscode.cmake-tools" + ] + } + }, + "runArgs": [ + "--network", + "host", + "--privileged", + "--device", + "/dev/bus/usb:/dev/bus/usb", + "--device-cgroup-rule=c 188:* rwm", + "--device-cgroup-rule=c 166:* rwm", + "--group-add=dialout", + "--volume=/dev/serial/by-id:/dev/serial/by-id:ro" + ] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f80d786 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.vscode/ +build/ +src/version_config.cpp +.cache/ +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 748b551..a2a7008 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ set(VERSION_MAJOR 0) set(VERSION_MINOR 2) set(VERSION_PATCH 0) -set(PROJECT_N cpp_unix_bindings) +set(PROJECT_N cpp_windows_bindings) project(${PROJECT_N} VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) # Generate compile_commands.json for clang-based tooling (clangd / clang-tidy) @@ -93,4 +93,4 @@ add_custom_command(TARGET tests POST_BUILD COMMENT "Copy shared library next to aggregated test binary") enable_testing() -add_test(NAME AllTests COMMAND tests /dev/ttyUSB0) +add_test(NAME AllTests COMMAND tests COM3) diff --git a/src/serial.cpp b/src/serial.cpp index 8fcc98f..5b410b8 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -1,19 +1,19 @@ #include "serial.h" - #include "status_codes.h" +// Windows serial implementation +// ----------------------------------------------------------------------------- +// NOTE: This file only supports the Windows API. All POSIX specific code has +// been removed. +// ----------------------------------------------------------------------------- + +#include #include #include #include -#include -#include #include #include -#include -#include -#include -#include -#include +#include // ----------------------------------------------------------------------------- // Global callback function pointers (default nullptr) @@ -30,8 +30,8 @@ namespace struct SerialPortHandle { - int file_descriptor; - termios original; // keep original settings so we can restore on close + HANDLE handle{INVALID_HANDLE_VALUE}; + DCB original_dcb{}; // keep original settings so we can restore on close int64_t rx_total{0}; // bytes received so far int64_t tx_total{0}; // bytes transmitted so far @@ -43,62 +43,6 @@ struct SerialPortHandle std::atomic abort_write{false}; }; -// Map integer baudrate to POSIX speed_t. Only common rates are supported. -auto to_speed_t(int baud) -> speed_t -{ - switch (baud) - { - case 0: - return B0; - case 50: - return B50; - case 75: - return B75; - case 110: - return B110; - case 134: - return B134; - case 150: - return B150; - case 200: - return B200; - case 300: - return B300; - case 600: - return B600; - case 1200: - return B1200; - case 1800: - return B1800; - case 2400: - return B2400; - case 4800: - return B4800; - case 9600: - return B9600; - case 19200: - return B19200; - case 38400: - return B38400; - case 57600: - return B57600; - case 115200: - return B115200; - case 230400: - return B230400; -#ifdef B460800 - case 460800: - return B460800; -#endif -#ifdef B921600 - case 921600: - return B921600; -#endif - default: - return B9600; // fallback - } -} - inline void invokeError(int code) { if (error_callback != nullptr) @@ -107,110 +51,148 @@ inline void invokeError(int code) } } -} // namespace - -// ----------------------------------------------------------------------------- -// Public API implementation -// ----------------------------------------------------------------------------- - -intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stopBits) +// Convert baudrate integer to constant directly (Windows API accepts int) +inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int stopBits) { - if (port == nullptr) - { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); - return 0; - } - - auto port_name = std::string_view{static_cast(port)}; - int device_descriptor = open(port_name.data(), O_RDWR | O_NOCTTY | O_SYNC); - if (device_descriptor < 0) - { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); - return 0; - } - - auto* handle = new SerialPortHandle{.file_descriptor = device_descriptor, .original = {}}; - - termios tty{}; - if (tcgetattr(device_descriptor, &tty) != 0) + DCB dcb{}; + dcb.DCBlength = sizeof(DCB); + if (!GetCommState(h, &dcb)) { - invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); - close(device_descriptor); - delete handle; - return 0; + return false; } - handle->original = tty; // save original - // Basic flags: local connection, enable receiver - tty.c_cflag |= (CLOCAL | CREAD); + // Store a copy of the original DCB? -> done outside - // Baudrate - const speed_t speed = to_speed_t(baudrate); - cfsetispeed(&tty, speed); - cfsetospeed(&tty, speed); + dcb.BaudRate = static_cast(baudrate); + dcb.ByteSize = static_cast(std::clamp(dataBits, 5, 8)); - // Data bits - tty.c_cflag &= ~CSIZE; - switch (dataBits) + // Parity + switch (parity) { - case 5: - tty.c_cflag |= CS5; - break; - case 6: - tty.c_cflag |= CS6; + case 1: // even + dcb.Parity = EVENPARITY; + dcb.fParity = TRUE; break; - case 7: - tty.c_cflag |= CS7; + case 2: // odd + dcb.Parity = ODDPARITY; + dcb.fParity = TRUE; break; default: - tty.c_cflag |= CS8; + dcb.Parity = NOPARITY; + dcb.fParity = FALSE; break; } - // Parity - if (parity == 0) + // Stop bits + dcb.StopBits = (stopBits == 2) ? TWOSTOPBITS : ONESTOPBIT; + + // Disable hardware/software flow control + dcb.fOutxCtsFlow = FALSE; + dcb.fOutxDsrFlow = FALSE; + dcb.fOutX = FALSE; + dcb.fInX = FALSE; + + return !!SetCommState(h, &dcb); +} + +inline void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) +{ + COMMTIMEOUTS timeouts{}; + + // Read time-outs + if (readTimeoutMs < 0) { - tty.c_cflag &= ~PARENB; + // Infinite blocking + timeouts.ReadIntervalTimeout = 0; + timeouts.ReadTotalTimeoutConstant = 0; + timeouts.ReadTotalTimeoutMultiplier = 0; } else { - tty.c_cflag |= PARENB; - if (parity == 1) - { - tty.c_cflag &= ~PARODD; // even - } - else - { - tty.c_cflag |= PARODD; // odd - } + timeouts.ReadIntervalTimeout = MAXDWORD; // return immediately if no bytes available + timeouts.ReadTotalTimeoutConstant = static_cast(readTimeoutMs); + timeouts.ReadTotalTimeoutMultiplier = 0; } - // Stop bits - if (stopBits == 2) + // Write time-outs (simple constant component only) + if (writeTimeoutMs >= 0) { - tty.c_cflag |= CSTOPB; + timeouts.WriteTotalTimeoutConstant = static_cast(writeTimeoutMs); + timeouts.WriteTotalTimeoutMultiplier = 0; } - else + + SetCommTimeouts(h, &timeouts); +} + +// Helper that adds the required "\\\\.\\" prefix for COM ports >= 10 +inline std::string toWinComPath(std::string_view port) +{ + std::string p(port); + // If the path already starts with \\.\, leave it + if (p.rfind("\\\\.\\", 0) == 0) + { + return p; + } + // Prepend prefix so Windows can open COM10+ + return "\\\\.\\" + p; +} + +} // namespace + +// ----------------------------------------------------------------------------- +// Public API implementation +// ----------------------------------------------------------------------------- + +intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stopBits) +{ + if (port == nullptr) { - tty.c_cflag &= ~CSTOPB; + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; } - // Raw mode (no echo/processing) - tty.c_iflag = 0; - tty.c_oflag = 0; - tty.c_lflag = 0; + std::string_view portNameView{static_cast(port)}; + std::string winPort = toWinComPath(portNameView); + + HANDLE h = CreateFileA(winPort.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, // exclusive access + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr); + + if (h == INVALID_HANDLE_VALUE) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } - tty.c_cc[VMIN] = 0; // non-blocking by default - tty.c_cc[VTIME] = 10; // 1s read timeout + // Save original DCB first + DCB original{}; + original.DCBlength = sizeof(DCB); + if (!GetCommState(h, &original)) + { + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + CloseHandle(h); + return 0; + } - if (tcsetattr(device_descriptor, TCSANOW, &tty) != 0) + // Configure DCB with requested settings + if (!configurePort(h, baudrate, dataBits, parity, stopBits)) { invokeError(std::to_underlying(StatusCodes::SET_STATE_ERROR)); - close(device_descriptor); - delete handle; + CloseHandle(h); return 0; } + // Default timeouts – can be overridden per read/write call + setPortTimeouts(h, 1000, 1000); + + auto* handle = new SerialPortHandle{}; + handle->handle = h; + handle->original_dcb = original; + return reinterpret_cast(handle); } @@ -222,132 +204,131 @@ void serialClose(int64_t handlePtr) return; } - tcsetattr(handle->file_descriptor, TCSANOW, &handle->original); // restore - if (close(handle->file_descriptor) != 0) - { - invokeError(std::to_underlying(StatusCodes::CLOSE_HANDLE_ERROR)); - } + // Restore original state + SetCommState(handle->handle, &handle->original_dcb); + FlushFileBuffers(handle->handle); + CloseHandle(handle->handle); + delete handle; } -static int waitFdReady(int fileDescriptor, int timeoutMs, bool wantWrite) -{ - timeoutMs = std::max(timeoutMs, 0); - - fd_set descriptor_set; - FD_ZERO(&descriptor_set); - FD_SET(fileDescriptor, &descriptor_set); - - timeval wait_time{}; - wait_time.tv_sec = timeoutMs / 1000; - wait_time.tv_usec = (timeoutMs % 1000) * 1000; +// --- Core IO helpers -------------------------------------------------------- - int ready_result = - select(fileDescriptor + 1, wantWrite ? nullptr : &descriptor_set, wantWrite ? &descriptor_set : nullptr, nullptr, &wait_time); - return ready_result; // 0 timeout, -1 error, >0 ready -} - -int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/) +static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, int timeoutMs) { - auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); return 0; } - // Abort check if (handle->abort_read.exchange(false)) { return 0; } - int total_copied = 0; - - // First deliver byte from internal peek buffer if present - if (handle->has_peek && bufferSize > 0) - { - static_cast(buffer)[0] = handle->peek_char; - handle->has_peek = false; - handle->rx_total += 1; - total_copied = 1; - buffer = static_cast(buffer) + 1; - bufferSize -= 1; - if (bufferSize == 0) - { - if (read_callback != nullptr) - { - read_callback(total_copied); - } - return total_copied; - } - } - - if (waitFdReady(handle->file_descriptor, timeout, false) <= 0) - { - return total_copied; // return what we may have already copied (could be 0) - } + setPortTimeouts(handle->handle, timeoutMs, -1); - ssize_t bytes_read_system = read(handle->file_descriptor, buffer, bufferSize); - if (bytes_read_system < 0) + DWORD bytesRead = 0; + BOOL ok = ReadFile(handle->handle, buffer, static_cast(bufferSize), &bytesRead, nullptr); + if (!ok) { invokeError(std::to_underlying(StatusCodes::READ_ERROR)); - return total_copied; + return 0; } - if (bytes_read_system > 0) + if (bytesRead > 0) { - handle->rx_total += bytes_read_system; + handle->rx_total += bytesRead; + if (read_callback != nullptr) + { + read_callback(static_cast(bytesRead)); + } } - total_copied += static_cast(bytes_read_system); - - if (read_callback != nullptr) - { - read_callback(total_copied); - } - return total_copied; + return static_cast(bytesRead); } -int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeout, int /*multiplier*/) +static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferSize, int timeoutMs) { - auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); return 0; } - // Abort check if (handle->abort_write.exchange(false)) { return 0; } - if (waitFdReady(handle->file_descriptor, timeout, true) <= 0) - { - return 0; // timeout or error - } + setPortTimeouts(handle->handle, -1, timeoutMs); - ssize_t bytes_written_system = write(handle->file_descriptor, buffer, bufferSize); - if (bytes_written_system < 0) + DWORD bytesWritten = 0; + BOOL ok = WriteFile(handle->handle, buffer, static_cast(bufferSize), &bytesWritten, nullptr); + if (!ok) { invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); return 0; } - if (bytes_written_system > 0) + if (bytesWritten > 0) + { + handle->tx_total += bytesWritten; + if (write_callback != nullptr) + { + write_callback(static_cast(bytesWritten)); + } + } + + return static_cast(bytesWritten); +} + +// --- Public IO wrappers ----------------------------------------------------- + +int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) { - handle->tx_total += bytes_written_system; + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; } - if (write_callback != nullptr) + // First deliver byte from internal peek buffer if present + int totalCopied = 0; + if (handle->has_peek && bufferSize > 0) { - write_callback(static_cast(bytes_written_system)); + static_cast(buffer)[0] = handle->peek_char; + handle->has_peek = false; + handle->rx_total += 1; + totalCopied = 1; + + buffer = static_cast(buffer) + 1; + bufferSize -= 1; + + if (bufferSize == 0) + { + if (read_callback != nullptr) + { + read_callback(totalCopied); + } + return totalCopied; + } } - return static_cast(bytes_written_system); + + int bytesRead = readFromPort(handle, buffer, bufferSize, timeout); + return totalCopied + bytesRead; +} + +int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeout, int /*multiplier*/) +{ + auto* handle = reinterpret_cast(handlePtr); + return writeToPort(handle, buffer, bufferSize, timeout); } +// ---------------- Higher level helpers ------------------------------------- + int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPtr) { auto* handle = reinterpret_cast(handlePtr); @@ -357,23 +338,23 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout return 0; } - char until_character = *static_cast(untilCharPtr); - int total = 0; - auto* char_buffer = static_cast(buffer); + char untilChar = *static_cast(untilCharPtr); + int total = 0; + auto* buf = static_cast(buffer); while (total < bufferSize) { - int read_result = serialRead(handlePtr, char_buffer + total, 1, timeout, 1); - if (read_result <= 0) + int r = serialRead(handlePtr, buf + total, 1, timeout, 1); + if (r <= 0) { - break; // timeout or error + break; } - if (char_buffer[total] == until_character) + if (buf[total] == untilChar) { total += 1; break; } - total += read_result; + total += r; } if (read_callback != nullptr) @@ -383,49 +364,28 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout return total; } +// List available COM ports using QueryDosDevice int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) { - auto sep = std::string_view{static_cast(separatorPtr)}; - std::string result; + const std::string_view sep{static_cast(separatorPtr)}; + std::string result; - namespace fs = std::filesystem; - - const fs::path by_id_dir{"/dev/serial/by-id"}; - if (!fs::exists(by_id_dir) || !fs::is_directory(by_id_dir)) - { - invokeError(std::to_underlying(StatusCodes::NOT_FOUND_ERROR)); - return 0; - } + constexpr int maxPorts = 256; + char pathBuf[256]; - try + for (int i = 1; i <= maxPorts; ++i) { - for (const auto& entry : fs::directory_iterator{by_id_dir}) + std::string port = "COM" + std::to_string(i); + if (QueryDosDeviceA(port.c_str(), pathBuf, sizeof(pathBuf))) { - if (!entry.is_symlink()) - { - continue; - } - - std::error_code error_code; - fs::path canonical = fs::canonical(entry.path(), error_code); - if (error_code) - { - continue; // skip entries we cannot resolve - } - - result += canonical.string(); + result += port; result += sep; } } - catch (const fs::filesystem_error&) - { - invokeError(std::to_underlying(StatusCodes::NOT_FOUND_ERROR)); - return 0; - } if (!result.empty()) { - // Remove the trailing separator + // Remove trailing separator result.erase(result.size() - sep.size()); } @@ -436,7 +396,7 @@ int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) } std::memcpy(buffer, result.c_str(), result.size() + 1); - return result.empty() ? 0 : 1; // number of ports not easily counted here + return result.empty() ? 0 : 1; } // ----------------------------------------------------------------------------- @@ -450,8 +410,7 @@ void serialClearBufferIn(int64_t handlePtr) { return; } - tcflush(handle->file_descriptor, TCIFLUSH); - // reset peek buffer + PurgeComm(handle->handle, PURGE_RXABORT | PURGE_RXCLEAR); handle->has_peek = false; } @@ -462,47 +421,38 @@ void serialClearBufferOut(int64_t handlePtr) { return; } - tcflush(handle->file_descriptor, TCOFLUSH); + PurgeComm(handle->handle, PURGE_TXABORT | PURGE_TXCLEAR); } void serialAbortRead(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) + if (handle != nullptr) { - return; + handle->abort_read = true; } - handle->abort_read = true; } void serialAbortWrite(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) + if (handle != nullptr) { - return; + handle->abort_write = true; } - handle->abort_write = true; } // ----------------------------------------------------------------------------- - // Callback registration -void serialOnError(void (*func)(int code)) -{ - error_callback = func; -} -void serialOnRead(void (*func)(int bytes)) -{ - read_callback = func; -} -void serialOnWrite(void (*func)(int bytes)) -{ - write_callback = func; -} +// ----------------------------------------------------------------------------- + +void serialOnError(void (*func)(int code)) { error_callback = func; } +void serialOnRead(void (*func)(int bytes)) { read_callback = func; } +void serialOnWrite(void (*func)(int bytes)) { write_callback = func; } // ----------------------------------------------------------------------------- -// Extended helper APIs (read line, token, frame, statistics, etc.) +// Helper utilities (read line, read frame, statistics, etc.) copied/adapted +// from the original implementation as they remain platform-agnostic. // ----------------------------------------------------------------------------- int serialReadLine(int64_t handlePtr, void* buffer, int bufferSize, int timeout) @@ -513,64 +463,57 @@ int serialReadLine(int64_t handlePtr, void* buffer, int bufferSize, int timeout) int serialWriteLine(int64_t handlePtr, const void* buffer, int bufferSize, int timeout) { - // First write the payload - int written = serialWrite(handlePtr, buffer, bufferSize, timeout, 1); - if (written != bufferSize) - { - return written; // error path, propagate - } - // Append newline (\n) - char new_line_char = '\n'; - int newline_result = serialWrite(handlePtr, &new_line_char, 1, timeout, 1); - if (newline_result != 1) + // Write payload + int bytesWritten = serialWrite(handlePtr, buffer, bufferSize, timeout, 1); + if (bytesWritten != bufferSize) { - return written; // newline failed, but payload written + return bytesWritten; } - return written + 1; + + // Append newline + const char newline = '\n'; + int writtenNl = serialWrite(handlePtr, &newline, 1, timeout, 1); + return (writtenNl == 1) ? (bytesWritten + 1) : bytesWritten; } int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int timeout, void* tokenPtr) { - const auto* token_cstr = static_cast(tokenPtr); - if (token_cstr == nullptr) + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); return 0; } - std::string token{token_cstr}; - int token_len = static_cast(token.size()); - if (token_len == 0 || bufferSize < token_len) - { - return 0; - } - auto* char_buffer = static_cast(buffer); - int total = 0; - int matched = 0; // how many chars of token matched so far + const char* token = static_cast(tokenPtr); + int tokenLen = static_cast(std::strlen(token)); + + auto* buf = static_cast(buffer); + int total = 0; + int matchPos = 0; while (total < bufferSize) { - int read_result = serialRead(handlePtr, char_buffer + total, 1, timeout, 1); - if (read_result <= 0) + int r = serialRead(handlePtr, buf + total, 1, timeout, 1); + if (r <= 0) { - break; // timeout or error + break; } - char current_char = char_buffer[total]; - total += 1; - - if (current_char == token[matched]) + if (buf[total] == token[matchPos]) { - matched += 1; - if (matched == token_len) + matchPos += 1; + if (matchPos == tokenLen) { - break; // token fully matched + total += 1; + break; // full token matched } } else { - matched = (current_char == token[0]) ? 1 : 0; // restart match search + matchPos = 0; // reset match progress } + total += r; } if (read_callback != nullptr) @@ -582,57 +525,56 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout, char startByte, char endByte) { - auto* char_buffer = static_cast(buffer); - int total = 0; - bool in_frame = false; + auto* buf = static_cast(buffer); + int total = 0; - while (total < bufferSize) + // Wait for start byte + char byte = 0; + while (true) { - char current_byte; - int read_result = serialRead(handlePtr, ¤t_byte, 1, timeout, 1); - if (read_result <= 0) + int r = serialRead(handlePtr, &byte, 1, timeout, 1); + if (r <= 0) { - break; // timeout + return 0; // timeout or error } - - if (!in_frame) + if (byte == startByte) { - if (current_byte == startByte) - { - in_frame = true; - char_buffer[total++] = current_byte; - } - continue; // ignore bytes until start byte detected + buf[0] = byte; + total = 1; + break; } + } - char_buffer[total++] = current_byte; - if (current_byte == endByte) + // Read until end byte + while (total < bufferSize) + { + int r = serialRead(handlePtr, &byte, 1, timeout, 1); + if (r <= 0) { - break; // frame finished + break; + } + buf[total] = byte; + total += 1; + if (byte == endByte) + { + break; } } return total; } +// Statistics helpers int64_t serialGetRxBytes(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) - { - return 0; - } - return handle->rx_total; + return (handle != nullptr) ? handle->rx_total : 0; } int64_t serialGetTxBytes(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) - { - return 0; - } - return handle->tx_total; + return (handle != nullptr) ? handle->tx_total : 0; } int serialPeek(int64_t handlePtr, void* outByte, int timeout) @@ -650,23 +592,13 @@ int serialPeek(int64_t handlePtr, void* outByte, int timeout) return 1; } - char received_byte; - int read_outcome = serialRead(handlePtr, &received_byte, 1, timeout, 1); - if (read_outcome <= 0) + int r = serialRead(handlePtr, &handle->peek_char, 1, timeout, 1); + if (r == 1) { - return 0; // nothing available - } - - // Store into peek buffer and undo stats increment - handle->peek_char = received_byte; - handle->has_peek = true; - if (handle->rx_total > 0) - { - handle->rx_total -= 1; // don't account peek + handle->has_peek = true; + *static_cast(outByte) = handle->peek_char; } - - *static_cast(outByte) = received_byte; - return 1; + return r; } int serialDrain(int64_t handlePtr) @@ -677,5 +609,11 @@ int serialDrain(int64_t handlePtr) invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); return 0; } - return (tcdrain(handle->file_descriptor) == 0) ? 1 : 0; -} + + if (!FlushFileBuffers(handle->handle)) + { + invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); + return 0; + } + return 1; +} diff --git a/src/serial.h b/src/serial.h index fc6285d..6aa2a8d 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,7 +1,12 @@ #pragma once #include -#define MODULE_API __attribute__((visibility("default"))) +// Export definitions based on platform +#ifdef _WIN32 +# define MODULE_API __declspec(dllexport) +#else +# define MODULE_API __attribute__((visibility("default"))) +#endif #ifdef __cplusplus extern "C" diff --git a/tests/serial_test.cpp b/tests/serial_test.cpp index 73dd0ed..244e08a 100644 --- a/tests/serial_test.cpp +++ b/tests/serial_test.cpp @@ -38,7 +38,7 @@ namespace { -const char* default_port = "/dev/ttyUSB0"; +const char* default_port = "COM3"; // Adjust as needed for your system } // namespace TEST(SerialEchoTest, EchoMessage) diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp index 6f87f4b..2e51cc5 100644 --- a/tests/serial_unit_tests.cpp +++ b/tests/serial_unit_tests.cpp @@ -6,13 +6,11 @@ #include #include #include -#include #include #include #include #include #include -#include namespace { @@ -31,7 +29,7 @@ void errorCallback(int code) const char* getDefaultPort() { const char* env = std::getenv("SERIAL_PORT"); - return (env != nullptr) ? env : "/dev/ttyUSB0"; + return (env != nullptr) ? env : "COM3"; } struct SerialDevice @@ -74,7 +72,7 @@ TEST(SerialOpenTest, InvalidPathInvokesErrorCallback) g_err_ptr = &err_code; serialOnError(errorCallback); - intptr_t handle = serialOpen((void*)"/dev/__does_not_exist__", 115200, 8, 0, 0); + intptr_t handle = serialOpen((void*)"COM999", 115200, 8, 0, 0); EXPECT_EQ(handle, 0); EXPECT_EQ(err_code.load(), static_cast(StatusCodes::INVALID_HANDLE_ERROR)); @@ -133,7 +131,7 @@ TEST(SerialGetPortsInfoTest, PrintAvailablePorts) std::string ports_str(info_buffer.data()); if (!ports_str.empty()) { - std::cout << "\nAvailable serial ports (by-id):\n"; + std::cout << "\nAvailable serial ports:\n"; size_t start = 0; while (true) { @@ -149,7 +147,7 @@ TEST(SerialGetPortsInfoTest, PrintAvailablePorts) } else { - std::cout << "\nNo serial devices found in /dev/serial/by-id\n"; + std::cout << "\nNo serial devices found.\n"; } } From b80d8cc6cc4c9a2106c5ad49eb4b59dbf3c177f2 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 13:03:40 +0000 Subject: [PATCH 03/22] make cross compile work --- .devcontainer/Dockerfile.dev | 2 + CMakePresets.json | 28 ++++++ cmake/toolchains/mingw64.cmake | 24 +++++ src/serial.cpp | 171 ++++++++++++++++++--------------- 4 files changed, 145 insertions(+), 80 deletions(-) create mode 100644 CMakePresets.json create mode 100644 cmake/toolchains/mingw64.cmake diff --git a/.devcontainer/Dockerfile.dev b/.devcontainer/Dockerfile.dev index 276d9fe..b0e3942 100644 --- a/.devcontainer/Dockerfile.dev +++ b/.devcontainer/Dockerfile.dev @@ -10,4 +10,6 @@ RUN dnf install -y \ cmake \ git \ ninja-build \ + mingw64-headers \ + mingw64-crt mingw64-winpthreads mingw64-gcc mingw64-gcc-c++ \ && dnf clean all diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..32e9652 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,28 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "windows-mingw-gcc", + "displayName": "x86_64 Windows (MinGW-w64 GCC)", + "description": "Cross-compile for 64-bit Windows using MinGW-w64 GCC toolchain", + "generator": "Ninja", + "binaryDir": "${sourceDir}/build", + "toolchainFile": "${sourceDir}/cmake/toolchains/mingw64.cmake", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + } + ], + "buildPresets": [ + { + "name": "windows-mingw-gcc", + "configurePreset": "windows-mingw-gcc" + } + ], + "testPresets": [ + { + "name": "windows-mingw-gcc", + "configurePreset": "windows-mingw-gcc" + } + ] +} diff --git a/cmake/toolchains/mingw64.cmake b/cmake/toolchains/mingw64.cmake new file mode 100644 index 0000000..635630f --- /dev/null +++ b/cmake/toolchains/mingw64.cmake @@ -0,0 +1,24 @@ +# Toolchain file for cross-compiling to 64-bit Windows using MinGW-w64 GCC +# Requires the packages: mingw64-crt mingw64-winpthreads mingw64-gcc +# +# Usage: +# cmake -S . -B build-win \ +# -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/mingw64.cmake \ +# -G Ninja + +# Identify target platform +set(CMAKE_SYSTEM_NAME Windows) +set(CMAKE_SYSTEM_PROCESSOR x86_64) + +# ---------- Compiler executables ---------- +set(MINGW_TARGET_TRIPLE x86_64-w64-windows-gnu) +set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) +set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) + +# ---------- Sysroot / search paths ---------- +# Default install location of mingw-w64 cross toolchain on Fedora +set(CMAKE_FIND_ROOT_PATH /usr/${MINGW_TARGET_TRIPLE}/sys-root/mingw) + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) diff --git a/src/serial.cpp b/src/serial.cpp index 5b410b8..b76feef 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -1,4 +1,5 @@ #include "serial.h" + #include "status_codes.h" // Windows serial implementation @@ -7,13 +8,14 @@ // been removed. // ----------------------------------------------------------------------------- -#include #include #include #include #include #include +#include #include +#include // ----------------------------------------------------------------------------- // Global callback function pointers (default nullptr) @@ -31,7 +33,7 @@ namespace struct SerialPortHandle { HANDLE handle{INVALID_HANDLE_VALUE}; - DCB original_dcb{}; // keep original settings so we can restore on close + DCB original_dcb{}; // keep original settings so we can restore on close int64_t rx_total{0}; // bytes received so far int64_t tx_total{0}; // bytes transmitted so far @@ -56,7 +58,7 @@ inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int { DCB dcb{}; dcb.DCBlength = sizeof(DCB); - if (!GetCommState(h, &dcb)) + if (GetCommState(h, &dcb) == 0) { return false; } @@ -70,16 +72,16 @@ inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int switch (parity) { case 1: // even - dcb.Parity = EVENPARITY; - dcb.fParity = TRUE; + dcb.Parity = EVENPARITY; + dcb.fParity = TRUE; break; case 2: // odd - dcb.Parity = ODDPARITY; - dcb.fParity = TRUE; + dcb.Parity = ODDPARITY; + dcb.fParity = TRUE; break; default: - dcb.Parity = NOPARITY; - dcb.fParity = FALSE; + dcb.Parity = NOPARITY; + dcb.fParity = FALSE; break; } @@ -89,10 +91,10 @@ inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int // Disable hardware/software flow control dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; - dcb.fOutX = FALSE; - dcb.fInX = FALSE; + dcb.fOutX = FALSE; + dcb.fInX = FALSE; - return !!SetCommState(h, &dcb); + return !(SetCommState(h, &dcb) == 0); } inline void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) @@ -103,21 +105,21 @@ inline void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) if (readTimeoutMs < 0) { // Infinite blocking - timeouts.ReadIntervalTimeout = 0; - timeouts.ReadTotalTimeoutConstant = 0; - timeouts.ReadTotalTimeoutMultiplier = 0; + timeouts.ReadIntervalTimeout = 0; + timeouts.ReadTotalTimeoutConstant = 0; + timeouts.ReadTotalTimeoutMultiplier = 0; } else { - timeouts.ReadIntervalTimeout = MAXDWORD; // return immediately if no bytes available - timeouts.ReadTotalTimeoutConstant = static_cast(readTimeoutMs); - timeouts.ReadTotalTimeoutMultiplier = 0; + timeouts.ReadIntervalTimeout = MAXDWORD; // return immediately if no bytes available + timeouts.ReadTotalTimeoutConstant = static_cast(readTimeoutMs); + timeouts.ReadTotalTimeoutMultiplier = 0; } // Write time-outs (simple constant component only) if (writeTimeoutMs >= 0) { - timeouts.WriteTotalTimeoutConstant = static_cast(writeTimeoutMs); + timeouts.WriteTotalTimeoutConstant = static_cast(writeTimeoutMs); timeouts.WriteTotalTimeoutMultiplier = 0; } @@ -129,12 +131,12 @@ inline std::string toWinComPath(std::string_view port) { std::string p(port); // If the path already starts with \\.\, leave it - if (p.rfind("\\\\.\\", 0) == 0) + if (p.starts_with("\\\\.\\")) { return p; } // Prepend prefix so Windows can open COM10+ - return "\\\\.\\" + p; + return R"(\\.\)" + p; } } // namespace @@ -151,12 +153,12 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop return 0; } - std::string_view portNameView{static_cast(port)}; - std::string winPort = toWinComPath(portNameView); + std::string_view port_name_view{static_cast(port)}; + std::string win_port = toWinComPath(port_name_view); - HANDLE h = CreateFileA(winPort.c_str(), + HANDLE h = CreateFileA(win_port.c_str(), GENERIC_READ | GENERIC_WRITE, - 0, // exclusive access + 0, // exclusive access nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, @@ -171,7 +173,7 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop // Save original DCB first DCB original{}; original.DCBlength = sizeof(DCB); - if (!GetCommState(h, &original)) + if (GetCommState(h, &original) == 0) { invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); CloseHandle(h); @@ -190,8 +192,8 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop setPortTimeouts(h, 1000, 1000); auto* handle = new SerialPortHandle{}; - handle->handle = h; - handle->original_dcb = original; + handle->handle = h; + handle->original_dcb = original; return reinterpret_cast(handle); } @@ -229,24 +231,24 @@ static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, setPortTimeouts(handle->handle, timeoutMs, -1); - DWORD bytesRead = 0; - BOOL ok = ReadFile(handle->handle, buffer, static_cast(bufferSize), &bytesRead, nullptr); - if (!ok) + DWORD bytes_read = 0; + BOOL ok = ReadFile(handle->handle, buffer, static_cast(bufferSize), &bytes_read, nullptr); + if (ok == 0) { invokeError(std::to_underlying(StatusCodes::READ_ERROR)); return 0; } - if (bytesRead > 0) + if (bytes_read > 0) { - handle->rx_total += bytesRead; + handle->rx_total += bytes_read; if (read_callback != nullptr) { - read_callback(static_cast(bytesRead)); + read_callback(static_cast(bytes_read)); } } - return static_cast(bytesRead); + return static_cast(bytes_read); } static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferSize, int timeoutMs) @@ -264,24 +266,24 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS setPortTimeouts(handle->handle, -1, timeoutMs); - DWORD bytesWritten = 0; - BOOL ok = WriteFile(handle->handle, buffer, static_cast(bufferSize), &bytesWritten, nullptr); - if (!ok) + DWORD bytes_written = 0; + BOOL ok = WriteFile(handle->handle, buffer, static_cast(bufferSize), &bytes_written, nullptr); + if (ok == 0) { invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); return 0; } - if (bytesWritten > 0) + if (bytes_written > 0) { - handle->tx_total += bytesWritten; + handle->tx_total += bytes_written; if (write_callback != nullptr) { - write_callback(static_cast(bytesWritten)); + write_callback(static_cast(bytes_written)); } } - return static_cast(bytesWritten); + return static_cast(bytes_written); } // --- Public IO wrappers ----------------------------------------------------- @@ -296,29 +298,29 @@ int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int } // First deliver byte from internal peek buffer if present - int totalCopied = 0; + int total_copied = 0; if (handle->has_peek && bufferSize > 0) { static_cast(buffer)[0] = handle->peek_char; - handle->has_peek = false; + handle->has_peek = false; handle->rx_total += 1; - totalCopied = 1; + total_copied = 1; - buffer = static_cast(buffer) + 1; + buffer = static_cast(buffer) + 1; bufferSize -= 1; if (bufferSize == 0) { if (read_callback != nullptr) { - read_callback(totalCopied); + read_callback(total_copied); } - return totalCopied; + return total_copied; } } - int bytesRead = readFromPort(handle, buffer, bufferSize, timeout); - return totalCopied + bytesRead; + int bytes_read = readFromPort(handle, buffer, bufferSize, timeout); + return total_copied + bytes_read; } int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeout, int /*multiplier*/) @@ -338,9 +340,9 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout return 0; } - char untilChar = *static_cast(untilCharPtr); - int total = 0; - auto* buf = static_cast(buffer); + char until_char = *static_cast(untilCharPtr); + int total = 0; + auto* buf = static_cast(buffer); while (total < bufferSize) { @@ -349,7 +351,7 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout { break; } - if (buf[total] == untilChar) + if (buf[total] == until_char) { total += 1; break; @@ -368,15 +370,15 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) { const std::string_view sep{static_cast(separatorPtr)}; - std::string result; + std::string result; - constexpr int maxPorts = 256; - char pathBuf[256]; + constexpr int max_ports = 256; + char path_buf[256]; - for (int i = 1; i <= maxPorts; ++i) + for (int i = 1; i <= max_ports; ++i) { std::string port = "COM" + std::to_string(i); - if (QueryDosDeviceA(port.c_str(), pathBuf, sizeof(pathBuf))) + if (QueryDosDeviceA(port.c_str(), path_buf, sizeof(path_buf)) != 0u) { result += port; result += sep; @@ -446,9 +448,18 @@ void serialAbortWrite(int64_t handlePtr) // Callback registration // ----------------------------------------------------------------------------- -void serialOnError(void (*func)(int code)) { error_callback = func; } -void serialOnRead(void (*func)(int bytes)) { read_callback = func; } -void serialOnWrite(void (*func)(int bytes)) { write_callback = func; } +void serialOnError(void (*func)(int code)) +{ + error_callback = func; +} +void serialOnRead(void (*func)(int bytes)) +{ + read_callback = func; +} +void serialOnWrite(void (*func)(int bytes)) +{ + write_callback = func; +} // ----------------------------------------------------------------------------- // Helper utilities (read line, read frame, statistics, etc.) copied/adapted @@ -464,16 +475,16 @@ int serialReadLine(int64_t handlePtr, void* buffer, int bufferSize, int timeout) int serialWriteLine(int64_t handlePtr, const void* buffer, int bufferSize, int timeout) { // Write payload - int bytesWritten = serialWrite(handlePtr, buffer, bufferSize, timeout, 1); - if (bytesWritten != bufferSize) + int bytes_written = serialWrite(handlePtr, buffer, bufferSize, timeout, 1); + if (bytes_written != bufferSize) { - return bytesWritten; + return bytes_written; } // Append newline const char newline = '\n'; - int writtenNl = serialWrite(handlePtr, &newline, 1, timeout, 1); - return (writtenNl == 1) ? (bytesWritten + 1) : bytesWritten; + int written_nl = serialWrite(handlePtr, &newline, 1, timeout, 1); + return (written_nl == 1) ? (bytes_written + 1) : bytes_written; } int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int timeout, void* tokenPtr) @@ -486,11 +497,11 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti } const char* token = static_cast(tokenPtr); - int tokenLen = static_cast(std::strlen(token)); + int token_len = static_cast(std::strlen(token)); auto* buf = static_cast(buffer); - int total = 0; - int matchPos = 0; + int total = 0; + int match_pos = 0; while (total < bufferSize) { @@ -500,10 +511,10 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti break; } - if (buf[total] == token[matchPos]) + if (buf[total] == token[match_pos]) { - matchPos += 1; - if (matchPos == tokenLen) + match_pos += 1; + if (match_pos == token_len) { total += 1; break; // full token matched @@ -511,7 +522,7 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti } else { - matchPos = 0; // reset match progress + match_pos = 0; // reset match progress } total += r; } @@ -525,8 +536,8 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout, char startByte, char endByte) { - auto* buf = static_cast(buffer); - int total = 0; + auto* buf = static_cast(buffer); + int total = 0; // Wait for start byte char byte = 0; @@ -540,7 +551,7 @@ int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout if (byte == startByte) { buf[0] = byte; - total = 1; + total = 1; break; } } @@ -595,7 +606,7 @@ int serialPeek(int64_t handlePtr, void* outByte, int timeout) int r = serialRead(handlePtr, &handle->peek_char, 1, timeout, 1); if (r == 1) { - handle->has_peek = true; + handle->has_peek = true; *static_cast(outByte) = handle->peek_char; } return r; @@ -610,10 +621,10 @@ int serialDrain(int64_t handlePtr) return 0; } - if (!FlushFileBuffers(handle->handle)) + if (FlushFileBuffers(handle->handle) == 0) { invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); return 0; } return 1; -} +} From 1f0f4c5f6a6f7ec0263d7acf9cb92949417f8a9a Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 13:06:53 +0000 Subject: [PATCH 04/22] flang-tidy fixes --- src/serial.cpp | 2 +- src/serial.h | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index b76feef..6198d24 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -131,7 +131,7 @@ inline std::string toWinComPath(std::string_view port) { std::string p(port); // If the path already starts with \\.\, leave it - if (p.starts_with("\\\\.\\")) + if (p.starts_with(R"(\\.\)")) { return p; } diff --git a/src/serial.h b/src/serial.h index 6aa2a8d..e832a7c 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,12 +1,7 @@ #pragma once #include -// Export definitions based on platform -#ifdef _WIN32 -# define MODULE_API __declspec(dllexport) -#else -# define MODULE_API __attribute__((visibility("default"))) -#endif +#define MODULE_API __declspec(dllexport) #ifdef __cplusplus extern "C" From 6378ec9cbf8b826ece7efefc564c04491b8470a8 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 14:16:14 +0000 Subject: [PATCH 05/22] add ci build job --- .github/workflows/build.yml | 30 ---------------------- .github/workflows/ci.yml | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index e528296..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Build DLL - -on: - push: - branches: - - main - - workflow_dispatch: - -jobs: - build: - runs-on: windows-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Install MinGW - run: choco install mingw - - - name: Compile DLL - run: | - mkdir -p ./build - g++ -shared -o ./build/main.dll ./src/main.cpp - g++ -shared -o ./build/test.dll ./src/test.cpp - - - name: Upload DLL artifact - uses: actions/upload-artifact@v4 - with: - name: cpp_bindings_windows - path: build/**/*.dll diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..4fa2f45 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,50 @@ +name: Build & Release cpp_windows_bindings + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "**" ] + release: + types: [ created ] + +jobs: + + build: + name: Cross-build Windows shared library (MinGW) + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install MinGW cross-compiler & build tools + run: | + sudo apt-get update + sudo apt-get install -y mingw-w64 cmake ninja-build make + + - name: Configure CMake (MinGW) + run: | + cmake -S . -B build \ + -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/mingw64.cmake \ + -DCMAKE_BUILD_TYPE=Release + + - name: Compile + run: cmake --build build -j $(nproc) + + - name: Upload Windows artifacts + uses: actions/upload-artifact@v4 + with: + name: libcpp_windows_bindings + path: | + build/**/*.dll + build/**/*.a + retention-days: 14 + + - name: Attach Windows artifacts to release + if: github.event_name == 'release' + uses: softprops/action-gh-release@v1 + with: + files: | + build/**/*.dll + build/**/*.a From df3a9922e00933e1df4fadaec7020f05a034dd5c Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 14:37:28 +0000 Subject: [PATCH 06/22] fix ifdef --- .github/workflows/ci.yml | 21 +++++++++------------ CMakeLists.txt | 5 +++++ src/serial.cpp | 8 ++++---- src/serial.h | 8 ++++++++ versioning/version_config.cpp.in | 4 +++- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fa2f45..37f5c55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,26 +11,23 @@ on: jobs: build: - name: Cross-build Windows shared library (MinGW) - runs-on: ubuntu-latest + name: Build Windows shared library (g++/MinGW) + runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v3 - - name: Install MinGW cross-compiler & build tools - run: | - sudo apt-get update - sudo apt-get install -y mingw-w64 cmake ninja-build make + - name: Setup MSYS2 with MinGW-w64 & build tools + run: echo "Using pre-installed Git for Windows g++ in PATH" - - name: Configure CMake (MinGW) + - name: Configure CMake (g++ / Ninja) run: | - cmake -S . -B build \ - -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/mingw64.cmake \ - -DCMAKE_BUILD_TYPE=Release - + cmake -S . -B build -G "Ninja" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_BUILD_TYPE=Release + - name: Compile - run: cmake --build build -j $(nproc) + run: cmake --build build + - name: Upload Windows artifacts uses: actions/upload-artifact@v4 diff --git a/CMakeLists.txt b/CMakeLists.txt index a2a7008..8aba562 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,3 +94,8 @@ add_custom_command(TARGET tests POST_BUILD enable_testing() add_test(NAME AllTests COMMAND tests COM3) + +if(MINGW) + # Statisch gegen stdc++, gcc linken und pthread als statisches Archiv + target_link_options(${PROJECT_N} PRIVATE -static -static-libstdc++ -static-libgcc -pthread) +endif() diff --git a/src/serial.cpp b/src/serial.cpp index 6198d24..72572e6 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -45,7 +45,7 @@ struct SerialPortHandle std::atomic abort_write{false}; }; -inline void invokeError(int code) +void invokeError(int code) { if (error_callback != nullptr) { @@ -54,7 +54,7 @@ inline void invokeError(int code) } // Convert baudrate integer to constant directly (Windows API accepts int) -inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int stopBits) +bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int stopBits) { DCB dcb{}; dcb.DCBlength = sizeof(DCB); @@ -97,7 +97,7 @@ inline bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int return !(SetCommState(h, &dcb) == 0); } -inline void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) +void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) { COMMTIMEOUTS timeouts{}; @@ -127,7 +127,7 @@ inline void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) } // Helper that adds the required "\\\\.\\" prefix for COM ports >= 10 -inline std::string toWinComPath(std::string_view port) +std::string toWinComPath(std::string_view port) { std::string p(port); // If the path already starts with \\.\, leave it diff --git a/src/serial.h b/src/serial.h index e832a7c..061d863 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,7 +1,15 @@ #pragma once #include +#if defined(_WIN32) || defined(__CYGWIN__) +#ifdef cpp_windows_bindings_EXPORTS #define MODULE_API __declspec(dllexport) +#else +#define MODULE_API __declspec(dllimport) +#endif +#else +#define MODULE_API +#endif #ifdef __cplusplus extern "C" diff --git a/versioning/version_config.cpp.in b/versioning/version_config.cpp.in index 8d095d8..d6cd6ae 100644 --- a/versioning/version_config.cpp.in +++ b/versioning/version_config.cpp.in @@ -1,3 +1,5 @@ +#include "serial.h" + unsigned int getMajorVersion() { return @VERSION_MAJOR@; @@ -11,4 +13,4 @@ unsigned int getMinorVersion() unsigned int getPatchVersion() { return @VERSION_PATCH@; -} \ No newline at end of file +} From 340877fcd48d79752a2f1dba05131cf0c3aebd45 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 16:22:08 +0000 Subject: [PATCH 07/22] Update serial examples and add buffer occupancy functions for Windows --- examples/serial_advanced.ts | 11 +++++++++- examples/serial_echo.ts | 6 +++++- src/serial.cpp | 42 +++++++++++++++++++++++++++++++++++++ src/serial.h | 4 ++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/examples/serial_advanced.ts b/examples/serial_advanced.ts index 69f8b9b..3e1db69 100644 --- a/examples/serial_advanced.ts +++ b/examples/serial_advanced.ts @@ -17,7 +17,7 @@ interface CliOptions { } function parseArgs(): CliOptions { - const opts: CliOptions = { lib: "./build/libcpp_unix_bindings.so" }; + const opts: CliOptions = { lib: "./build/libcpp_windows_bindings.dll" }; for (let i = 0; i < Deno.args.length; ++i) { const arg = Deno.args[i]; @@ -66,6 +66,8 @@ const dylib = Deno.dlopen( serialDrain: { parameters: ["pointer"], result: "i32" }, serialGetTxBytes: { parameters: ["pointer"], result: "i64" }, serialGetRxBytes: { parameters: ["pointer"], result: "i64" }, + serialInWaiting: { parameters: ["pointer"], result: "i32" }, + serialOutWaiting: { parameters: ["pointer"], result: "i32" }, } as const, ); @@ -127,6 +129,13 @@ const txBytes = Number(dylib.symbols.serialGetTxBytes(handle)); const rxBytes = Number(dylib.symbols.serialGetRxBytes(handle)); console.log(`\nStatistics -> TX: ${txBytes} bytes, RX: ${rxBytes} bytes`); +// ----------------------------------------------------------------------------- +// Additional statistics +// ----------------------------------------------------------------------------- +const rxQueued = dylib.symbols.serialInWaiting(handle); +const txQueued = dylib.symbols.serialOutWaiting(handle); +console.log(`RX queued: ${rxQueued}, TX queued: ${txQueued}`); + // ----------------------------------------------------------------------------- // Cleanup // ----------------------------------------------------------------------------- diff --git a/examples/serial_echo.ts b/examples/serial_echo.ts index d9b8905..88e5188 100644 --- a/examples/serial_echo.ts +++ b/examples/serial_echo.ts @@ -7,7 +7,7 @@ interface CliOptions { } function parseArgs(): CliOptions { - const opts: CliOptions = { lib: "./build/libcpp_unix_bindings.so" }; + const opts: CliOptions = { lib: "./build/libcpp_windows_bindings.dll" }; for (let i = 0; i < Deno.args.length; ++i) { const arg = Deno.args[i]; @@ -57,6 +57,7 @@ const dylib = Deno.dlopen( result: "i32", }, serialGetPortsInfo: { parameters: ["pointer", "i32", "pointer"], result: "i32" }, + serialClearBufferIn: { parameters: ["pointer"], result: "void" }, } as const, ); @@ -100,6 +101,9 @@ if (handle === null) { // Give MCU a moment to reboot (similar to C++ tests) await new Promise((r) => setTimeout(r, 2000)); +// Flush any leftover bytes the MCU might have sent during reset +dylib.symbols.serialClearBufferIn(handle); + const msg = "HELLO\n"; const msgBuf = encoder.encode(msg); const written = dylib.symbols.serialWrite(handle, pointer(msgBuf), msgBuf.length, 100, 1); diff --git a/src/serial.cpp b/src/serial.cpp index 72572e6..238a1cb 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -628,3 +628,45 @@ int serialDrain(int64_t handlePtr) } return 1; } + +// ----------------------------------------------------------------------------- +// Buffer occupancy helpers (similar to PySerial in_waiting / out_waiting) +// ----------------------------------------------------------------------------- + +int serialInWaiting(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + COMSTAT status{}; + DWORD errors = 0; + if (ClearCommError(handle->handle, &errors, &status) == 0) + { + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + return 0; + } + return static_cast(status.cbInQue); +} + +int serialOutWaiting(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + COMSTAT status{}; + DWORD errors = 0; + if (ClearCommError(handle->handle, &errors, &status) == 0) + { + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + return 0; + } + return static_cast(status.cbOutQue); +} diff --git a/src/serial.h b/src/serial.h index 061d863..3a98109 100644 --- a/src/serial.h +++ b/src/serial.h @@ -67,6 +67,10 @@ extern "C" // Drain pending TX bytes (wait until sent) MODULE_API int serialDrain(int64_t handle); + // Bytes currently queued in the driver buffers + MODULE_API int serialInWaiting(int64_t handle); + MODULE_API int serialOutWaiting(int64_t handle); + #ifdef __cplusplus } #endif From 520d9ac7d7a6dbc230796d6ef9249395a0c33447 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 16:24:34 +0000 Subject: [PATCH 08/22] Add serialIsReady function to check modem status readiness --- src/serial.cpp | 20 ++++++++++++++++++++ src/serial.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/src/serial.cpp b/src/serial.cpp index 238a1cb..950a37a 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -670,3 +670,23 @@ int serialOutWaiting(int64_t handlePtr) } return static_cast(status.cbOutQue); } + +int serialIsReady(int64_t handlePtr) +{ + auto* handle = reinterpret_cast(handlePtr); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + return 0; + } + + DWORD modem_status = 0; + if (GetCommModemStatus(handle->handle, &modem_status) == 0) + { + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + return 0; + } + + bool ready = (modem_status & MS_DSR_ON) || (modem_status & MS_CTS_ON) || (modem_status & MS_RLSD_ON); + return ready ? 1 : 0; +} diff --git a/src/serial.h b/src/serial.h index 3a98109..1c151bc 100644 --- a/src/serial.h +++ b/src/serial.h @@ -71,6 +71,9 @@ extern "C" MODULE_API int serialInWaiting(int64_t handle); MODULE_API int serialOutWaiting(int64_t handle); + // Query modem status lines (e.g., DSR/CTS) – returns 1 if device signals ready, 0 otherwise + MODULE_API int serialIsReady(int64_t handle); + #ifdef __cplusplus } #endif From 1324197bb5ab2cd47514832522a6c4f7471c3112 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 16:41:36 +0000 Subject: [PATCH 09/22] Add serialOnError function and error handling callback; remove serialIsReady function --- examples/serial_advanced.ts | 15 +++++++++++++++ src/serial.cpp | 20 -------------------- src/serial.h | 3 --- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/examples/serial_advanced.ts b/examples/serial_advanced.ts index 3e1db69..8c39793 100644 --- a/examples/serial_advanced.ts +++ b/examples/serial_advanced.ts @@ -68,6 +68,11 @@ const dylib = Deno.dlopen( serialGetRxBytes: { parameters: ["pointer"], result: "i64" }, serialInWaiting: { parameters: ["pointer"], result: "i32" }, serialOutWaiting: { parameters: ["pointer"], result: "i32" }, + serialOnError: { + parameters: ["function"], + result: "void", + nonblocking: true, + }, } as const, ); @@ -140,5 +145,15 @@ console.log(`RX queued: ${rxQueued}, TX queued: ${txQueued}`); // Cleanup // ----------------------------------------------------------------------------- dylib.symbols.serialClose(handle); +dylib.symbols.serialOnError(0); + +// Signatur: (int code) -> void +const errorCallback = new Deno.UnsafeCallback( + { parameters: ["i32"], result: "void" }, + (code: number) => { + console.error(`Serial-Error! Code = ${code}`); + }, +); +errorCallback.close(); dylib.close(); console.log("Done."); diff --git a/src/serial.cpp b/src/serial.cpp index 950a37a..238a1cb 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -670,23 +670,3 @@ int serialOutWaiting(int64_t handlePtr) } return static_cast(status.cbOutQue); } - -int serialIsReady(int64_t handlePtr) -{ - auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) - { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); - return 0; - } - - DWORD modem_status = 0; - if (GetCommModemStatus(handle->handle, &modem_status) == 0) - { - invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); - return 0; - } - - bool ready = (modem_status & MS_DSR_ON) || (modem_status & MS_CTS_ON) || (modem_status & MS_RLSD_ON); - return ready ? 1 : 0; -} diff --git a/src/serial.h b/src/serial.h index 1c151bc..3a98109 100644 --- a/src/serial.h +++ b/src/serial.h @@ -71,9 +71,6 @@ extern "C" MODULE_API int serialInWaiting(int64_t handle); MODULE_API int serialOutWaiting(int64_t handle); - // Query modem status lines (e.g., DSR/CTS) – returns 1 if device signals ready, 0 otherwise - MODULE_API int serialIsReady(int64_t handle); - #ifdef __cplusplus } #endif From ce06c9d9e588199b87d4392ea7b2610f6d0b69b9 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 19:53:04 +0000 Subject: [PATCH 10/22] Enhance serial port handling with detailed error reporting and callback support; add port enumeration function --- .clang-tidy | 2 +- CMakeLists.txt | 2 + examples/serial_echo.ts | 75 ++++++++++++++++---- src/serial.cpp | 53 +++++++------- src/serial.h | 17 ++++- src/serial_ports_info.cpp | 135 ++++++++++++++++++++++++++++++++++++ tests/serial_unit_tests.cpp | 2 +- 7 files changed, 240 insertions(+), 46 deletions(-) create mode 100644 src/serial_ports_info.cpp diff --git a/.clang-tidy b/.clang-tidy index 6793178..182b97e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -25,6 +25,6 @@ CheckOptions: - key: readability-identifier-naming.PrivateMemberPrefix value: '_' - key: readability-function-size.ParameterThreshold - value: '8' + value: '12' - key: readability-braces-around-statements.ShortStatementLines value: '0' diff --git a/CMakeLists.txt b/CMakeLists.txt index 8aba562..4659a2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,4 +98,6 @@ add_test(NAME AllTests COMMAND tests COM3) if(MINGW) # Statisch gegen stdc++, gcc linken und pthread als statisches Archiv target_link_options(${PROJECT_N} PRIVATE -static -static-libstdc++ -static-libgcc -pthread) + # Windows SetupAPI for serial port enumeration + target_link_libraries(${PROJECT_N} PRIVATE setupapi) endif() diff --git a/examples/serial_echo.ts b/examples/serial_echo.ts index 88e5188..c017e00 100644 --- a/examples/serial_echo.ts +++ b/examples/serial_echo.ts @@ -56,34 +56,78 @@ const dylib = Deno.dlopen( parameters: ["pointer", "pointer", "i32", "i32", "i32", "pointer"], result: "i32", }, - serialGetPortsInfo: { parameters: ["pointer", "i32", "pointer"], result: "i32" }, + serialGetPortsInfo_: { parameters: ["function"], result: "i32" }, serialClearBufferIn: { parameters: ["pointer"], result: "void" }, } as const, ); // ----------------------------------------------------------------------------- -// 1. List available ports +// 1. List available ports (callback API) // ----------------------------------------------------------------------------- -const sepBuf = cString(";"); -const portsBuf = new Uint8Array(4096); -dylib.symbols.serialGetPortsInfo( - pointer(portsBuf), - portsBuf.length, - pointer(sepBuf), -); +interface PortInfo { + path: string; + manufacturer: string; + serial: string; + pnpId: string; + location: string; + productId: string; + vendorId: string; +} + +function cstr(ptr: Deno.UnsafePointer): string { + return new Deno.UnsafePointerView(ptr).getCString(); +} + +const portInfos: PortInfo[] = []; + +const collectCb = new Deno.UnsafeCallback({ + parameters: [ + "pointer", + "pointer", + "pointer", + "pointer", + "pointer", + "pointer", + "pointer", + ], + result: "void", +}, ( + pathPtr, + manufacturerPtr, + serialPtr, + pnpPtr, + locationPtr, + productPtr, + vendorPtr, +) => { + portInfos.push({ + path: cstr(pathPtr), + manufacturer: cstr(manufacturerPtr), + serial: cstr(serialPtr), + pnpId: cstr(pnpPtr), + location: cstr(locationPtr), + productId: cstr(productPtr), + vendorId: cstr(vendorPtr), + }); +}); + +dylib.symbols.serialGetPortsInfo_(collectCb.pointer); -const cPortsStr = decoder.decode(portsBuf.subarray(0, portsBuf.indexOf(0))); -const ports = cPortsStr ? cPortsStr.split(";") : []; console.log("Available ports:"); -for (const p of ports) { - console.log(" •", p); +for (const info of portInfos) { + console.log(` • ${info.path} [${info.vendorId}:${info.productId}] ${info.manufacturer}`); } -if (ports.length === 0) { - console.error("No serial ports found (ttyUSB). Exiting."); + +if (portInfos.length === 0) { + console.error("No serial ports found. Exiting."); + collectCb.close(); dylib.close(); Deno.exit(1); } +// Convert to simple list of paths for later selection +const ports = portInfos.map((i) => i.path); + // ----------------------------------------------------------------------------- // 2. Echo test on selected port // ----------------------------------------------------------------------------- @@ -135,4 +179,5 @@ if (echo === msg) { } dylib.symbols.serialClose(handle); +collectCb.close(); dylib.close(); diff --git a/src/serial.cpp b/src/serial.cpp index 238a1cb..63185a6 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -14,13 +14,12 @@ #include #include #include -#include #include // ----------------------------------------------------------------------------- // Global callback function pointers (default nullptr) // ----------------------------------------------------------------------------- -void (*error_callback)(int) = nullptr; +void (*error_callback)(int, const char*) = nullptr; void (*read_callback)(int) = nullptr; void (*write_callback)(int) = nullptr; @@ -45,11 +44,11 @@ struct SerialPortHandle std::atomic abort_write{false}; }; -void invokeError(int code) +void invokeError(int code, const char* message) { if (error_callback != nullptr) { - error_callback(code); + error_callback(code, message); } } @@ -149,7 +148,7 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop { if (port == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialOpen: port parameter is null"); return 0; } @@ -166,7 +165,7 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop if (h == INVALID_HANDLE_VALUE) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialOpen: CreateFileA failed (INVALID_HANDLE_VALUE)"); return 0; } @@ -175,7 +174,7 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop original.DCBlength = sizeof(DCB); if (GetCommState(h, &original) == 0) { - invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR), "serialOpen: GetCommState failed"); CloseHandle(h); return 0; } @@ -183,7 +182,7 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop // Configure DCB with requested settings if (!configurePort(h, baudrate, dataBits, parity, stopBits)) { - invokeError(std::to_underlying(StatusCodes::SET_STATE_ERROR)); + invokeError(std::to_underlying(StatusCodes::SET_STATE_ERROR), "serialOpen: configurePort failed"); CloseHandle(h); return 0; } @@ -220,7 +219,7 @@ static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, { if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "readFromPort: handle pointer is null"); return 0; } @@ -235,7 +234,7 @@ static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, BOOL ok = ReadFile(handle->handle, buffer, static_cast(bufferSize), &bytes_read, nullptr); if (ok == 0) { - invokeError(std::to_underlying(StatusCodes::READ_ERROR)); + invokeError(std::to_underlying(StatusCodes::READ_ERROR), "readFromPort: ReadFile failed"); return 0; } @@ -255,7 +254,7 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS { if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "writeToPort: handle pointer is null"); return 0; } @@ -270,7 +269,7 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS BOOL ok = WriteFile(handle->handle, buffer, static_cast(bufferSize), &bytes_written, nullptr); if (ok == 0) { - invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); + invokeError(std::to_underlying(StatusCodes::WRITE_ERROR), "writeToPort: WriteFile failed"); return 0; } @@ -293,7 +292,7 @@ int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialRead: handle pointer is null"); return 0; } @@ -336,7 +335,7 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntil: handle pointer is null"); return 0; } @@ -393,7 +392,7 @@ int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) if (static_cast(result.size()) + 1 > bufferSize) { - invokeError(std::to_underlying(StatusCodes::BUFFER_ERROR)); + invokeError(std::to_underlying(StatusCodes::BUFFER_ERROR), "serialGetPortsInfo: output buffer too small"); return 0; } @@ -448,10 +447,6 @@ void serialAbortWrite(int64_t handlePtr) // Callback registration // ----------------------------------------------------------------------------- -void serialOnError(void (*func)(int code)) -{ - error_callback = func; -} void serialOnRead(void (*func)(int bytes)) { read_callback = func; @@ -460,6 +455,10 @@ void serialOnWrite(void (*func)(int bytes)) { write_callback = func; } +void serialOnError(void (*func)(int code, const char* message)) +{ + error_callback = func; +} // ----------------------------------------------------------------------------- // Helper utilities (read line, read frame, statistics, etc.) copied/adapted @@ -492,7 +491,7 @@ int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int ti auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntilToken: handle pointer is null"); return 0; } @@ -593,7 +592,7 @@ int serialPeek(int64_t handlePtr, void* outByte, int timeout) auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr || outByte == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialPeek: handle or outByte pointer is null"); return 0; } @@ -617,13 +616,13 @@ int serialDrain(int64_t handlePtr) auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialDrain: handle pointer is null"); return 0; } if (FlushFileBuffers(handle->handle) == 0) { - invokeError(std::to_underlying(StatusCodes::WRITE_ERROR)); + invokeError(std::to_underlying(StatusCodes::WRITE_ERROR), "serialDrain: FlushFileBuffers failed"); return 0; } return 1; @@ -638,7 +637,7 @@ int serialInWaiting(int64_t handlePtr) auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialInWaiting: handle pointer is null"); return 0; } @@ -646,7 +645,7 @@ int serialInWaiting(int64_t handlePtr) DWORD errors = 0; if (ClearCommError(handle->handle, &errors, &status) == 0) { - invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR), "serialInWaiting: ClearCommError failed"); return 0; } return static_cast(status.cbInQue); @@ -657,7 +656,7 @@ int serialOutWaiting(int64_t handlePtr) auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR)); + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialOutWaiting: handle pointer is null"); return 0; } @@ -665,7 +664,7 @@ int serialOutWaiting(int64_t handlePtr) DWORD errors = 0; if (ClearCommError(handle->handle, &errors, &status) == 0) { - invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR)); + invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR), "serialOutWaiting: ClearCommError failed"); return 0; } return static_cast(status.cbOutQue); diff --git a/src/serial.h b/src/serial.h index 3a98109..93bcb7b 100644 --- a/src/serial.h +++ b/src/serial.h @@ -35,19 +35,32 @@ extern "C" MODULE_API int serialGetPortsInfo(void* buffer, int bufferSize, void* separator); + // Enumerate COM ports and forward detailed information through a callback. + // Each detected port triggers exactly one call to `function` with null-terminated + // strings (may be empty) for: + // path, manufacturer, serialNumber, pnpId, locationId, productId, vendorId + // The function returns the number of ports found (0 on error). + MODULE_API int serialGetPortsInfo_(void (*function)(const char* path, + const char* manufacturer, + const char* serialNumber, + const char* pnpId, + const char* locationId, + const char* productId, + const char* vendorId)); + MODULE_API void serialClearBufferIn(int64_t handle); MODULE_API void serialClearBufferOut(int64_t handle); MODULE_API void serialAbortRead(int64_t handle); MODULE_API void serialAbortWrite(int64_t handle); // Optional callback hooks (can be nullptr) - extern void (*error_callback)(int errorCode); extern void (*read_callback)(int bytes); extern void (*write_callback)(int bytes); + extern void (*error_callback)(int errorCode, const char* message); - MODULE_API void serialOnError(void (*func)(int code)); MODULE_API void serialOnRead(void (*func)(int bytes)); MODULE_API void serialOnWrite(void (*func)(int bytes)); + MODULE_API void serialOnError(void (*func)(int code, const char* message)); MODULE_API int serialReadLine(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/); diff --git a/src/serial_ports_info.cpp b/src/serial_ports_info.cpp new file mode 100644 index 0000000..91ef8dd --- /dev/null +++ b/src/serial_ports_info.cpp @@ -0,0 +1,135 @@ +#include "serial.h" +#include "status_codes.h" + +// clang-format off +#include +// clang-format on + +#include +#include +#include // requires windows.h but does not include it +#include +#include +#include + +#pragma comment(lib, "setupapi.lib") + +static void invokeErrorLocal(int code, const char* message) +{ + if (error_callback != nullptr) + { + error_callback(code, message); + } +} + +// Extract VID, PID, Serial from a HardwareID or device path string +static void parseVidPidSerial(const std::string& src, std::string& vid, std::string& pid, std::string& serial) +{ + auto pos_vid = src.find("VID_"); + if (pos_vid != std::string::npos && pos_vid + 8 <= src.size()) + { + vid = src.substr(pos_vid + 4, 4); + } + + auto pos_pid = src.find("PID_"); + if (pos_pid != std::string::npos && pos_pid + 8 <= src.size()) + { + pid = src.substr(pos_pid + 4, 4); + } + + // Serial number is heuristic: substring between second and third '#' + size_t first_hash = src.find('#'); + size_t second_hash = src.find('#', first_hash + 1); + size_t third_hash = src.find('#', second_hash + 1); + if (second_hash != std::string::npos && third_hash != std::string::npos && third_hash > second_hash) + { + serial = src.substr(second_hash + 1, third_hash - second_hash - 1); + } +} + +extern "C" int serialGetPortsInfo_(void (*function)(const char* path, + const char* manufacturer, + const char* serialNumber, + const char* pnpId, + const char* locationId, + const char* productId, + const char* vendorId)) +{ + if (function == nullptr) + { + invokeErrorLocal(std::to_underlying(StatusCodes::BUFFER_ERROR), "serialGetPortsInfo_: function pointer is null"); + return 0; + } + + const GUID guid_devinterface_comport = {0x86E0D1E0, 0x8089, 0x11D0, {0x9C, 0xE4, 0x08, 0x00, 0x3E, 0x30, 0x1F, 0x73}}; + + HDEVINFO h_dev_info = SetupDiGetClassDevs(&guid_devinterface_comport, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); + if (h_dev_info == INVALID_HANDLE_VALUE) + { + invokeErrorLocal(std::to_underlying(StatusCodes::NOT_FOUND_ERROR), "serialGetPortsInfo_: SetupDiGetClassDevs failed"); + return 0; + } + + int count = 0; + for (DWORD index = 0;; ++index) + { + SP_DEVICE_INTERFACE_DATA iface_data{sizeof(iface_data)}; + if (SetupDiEnumDeviceInterfaces(h_dev_info, nullptr, &guid_devinterface_comport, index, &iface_data) == 0) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + break; + } + continue; + } + + // Get interface detail (device path) + DWORD req_size = 0; + SetupDiGetDeviceInterfaceDetail(h_dev_info, &iface_data, nullptr, 0, &req_size, nullptr); + std::vector buf(req_size); + auto* detail = reinterpret_cast(buf.data()); + detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); + SP_DEVINFO_DATA dev_info{sizeof(dev_info)}; + if (SetupDiGetDeviceInterfaceDetailA(h_dev_info, &iface_data, detail, req_size, nullptr, &dev_info) == 0) + { + continue; + } + + std::string device_path = detail->DevicePath; + + // Manufacturer + CHAR mfg[256] = ""; + SetupDiGetDeviceRegistryPropertyA(h_dev_info, &dev_info, SPDRP_MFG, nullptr, reinterpret_cast(mfg), sizeof(mfg), nullptr); + + // Location information + CHAR loc[256] = ""; + SetupDiGetDeviceRegistryPropertyA( + h_dev_info, &dev_info, SPDRP_LOCATION_INFORMATION, nullptr, reinterpret_cast(loc), sizeof(loc), nullptr); + + // Hardware ID (multi-sz) → first string + CHAR hwid[256] = ""; + SetupDiGetDeviceRegistryPropertyA( + h_dev_info, &dev_info, SPDRP_HARDWAREID, nullptr, reinterpret_cast(hwid), sizeof(hwid), nullptr); + + std::string vid; + std::string pid; + std::string serial; + parseVidPidSerial(hwid, vid, pid, serial); + if (serial.empty()) + { + parseVidPidSerial(device_path, vid, pid, serial); + } + + function(device_path.c_str(), + (*mfg != 0) ? mfg : "", + serial.c_str(), + (hwid[0] != 0) ? hwid : "", + (loc[0] != 0) ? loc : "", + pid.c_str(), + vid.c_str()); + ++count; + } + + SetupDiDestroyDeviceInfoList(h_dev_info); + return count; +} diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp index 2e51cc5..8c3003f 100644 --- a/tests/serial_unit_tests.cpp +++ b/tests/serial_unit_tests.cpp @@ -17,7 +17,7 @@ namespace // Helper storage for callback tests std::atomic* g_err_ptr = nullptr; -void errorCallback(int code) +void errorCallback(int code, const char* /*msg*/) { if (g_err_ptr != nullptr) { From 0ad4c448c5f3837a0527f88c9ce1afd53cf5d17c Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 20:24:44 +0000 Subject: [PATCH 11/22] Refactor serial port information retrieval: rename serialGetPortsInfo_ to serialGetPortsInfo, update callback parameters to include port name, and adjust related code for consistency. Remove deprecated buffer-based port listing function and clean up unit tests. --- examples/serial_echo.ts | 12 ++-- src/serial.cpp | 35 ---------- src/serial.h | 24 +++---- src/serial_ports_info.cpp | 38 +++++++--- tests/serial_unit_tests.cpp | 134 ++++++++++++++++++------------------ 5 files changed, 113 insertions(+), 130 deletions(-) diff --git a/examples/serial_echo.ts b/examples/serial_echo.ts index c017e00..70d200f 100644 --- a/examples/serial_echo.ts +++ b/examples/serial_echo.ts @@ -56,7 +56,7 @@ const dylib = Deno.dlopen( parameters: ["pointer", "pointer", "i32", "i32", "i32", "pointer"], result: "i32", }, - serialGetPortsInfo_: { parameters: ["function"], result: "i32" }, + serialGetPortsInfo: { parameters: ["function"], result: "i32" }, serialClearBufferIn: { parameters: ["pointer"], result: "void" }, } as const, ); @@ -65,6 +65,7 @@ const dylib = Deno.dlopen( // 1. List available ports (callback API) // ----------------------------------------------------------------------------- interface PortInfo { + port: string; path: string; manufacturer: string; serial: string; @@ -89,9 +90,11 @@ const collectCb = new Deno.UnsafeCallback({ "pointer", "pointer", "pointer", + "pointer", ], result: "void", }, ( + portPtr, pathPtr, manufacturerPtr, serialPtr, @@ -101,6 +104,7 @@ const collectCb = new Deno.UnsafeCallback({ vendorPtr, ) => { portInfos.push({ + port: cstr(portPtr), path: cstr(pathPtr), manufacturer: cstr(manufacturerPtr), serial: cstr(serialPtr), @@ -111,11 +115,11 @@ const collectCb = new Deno.UnsafeCallback({ }); }); -dylib.symbols.serialGetPortsInfo_(collectCb.pointer); +dylib.symbols.serialGetPortsInfo(collectCb.pointer); console.log("Available ports:"); for (const info of portInfos) { - console.log(` • ${info.path} [${info.vendorId}:${info.productId}] ${info.manufacturer}`); + console.log(` • ${info.port} [${info.vendorId}:${info.productId}] ${info.manufacturer}`); } if (portInfos.length === 0) { @@ -126,7 +130,7 @@ if (portInfos.length === 0) { } // Convert to simple list of paths for later selection -const ports = portInfos.map((i) => i.path); +const ports = portInfos.map((i) => i.port); // ----------------------------------------------------------------------------- // 2. Echo test on selected port diff --git a/src/serial.cpp b/src/serial.cpp index 63185a6..745f5f7 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -365,41 +365,6 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout return total; } -// List available COM ports using QueryDosDevice -int serialGetPortsInfo(void* buffer, int bufferSize, void* separatorPtr) -{ - const std::string_view sep{static_cast(separatorPtr)}; - std::string result; - - constexpr int max_ports = 256; - char path_buf[256]; - - for (int i = 1; i <= max_ports; ++i) - { - std::string port = "COM" + std::to_string(i); - if (QueryDosDeviceA(port.c_str(), path_buf, sizeof(path_buf)) != 0u) - { - result += port; - result += sep; - } - } - - if (!result.empty()) - { - // Remove trailing separator - result.erase(result.size() - sep.size()); - } - - if (static_cast(result.size()) + 1 > bufferSize) - { - invokeError(std::to_underlying(StatusCodes::BUFFER_ERROR), "serialGetPortsInfo: output buffer too small"); - return 0; - } - - std::memcpy(buffer, result.c_str(), result.size() + 1); - return result.empty() ? 0 : 1; -} - // ----------------------------------------------------------------------------- // Buffer & abort helpers implementations // ----------------------------------------------------------------------------- diff --git a/src/serial.h b/src/serial.h index 93bcb7b..7e70398 100644 --- a/src/serial.h +++ b/src/serial.h @@ -33,20 +33,16 @@ extern "C" MODULE_API int serialWrite(int64_t handle, const void* buffer, int bufferSize, int timeout, int multiplier); - MODULE_API int serialGetPortsInfo(void* buffer, int bufferSize, void* separator); - - // Enumerate COM ports and forward detailed information through a callback. - // Each detected port triggers exactly one call to `function` with null-terminated - // strings (may be empty) for: - // path, manufacturer, serialNumber, pnpId, locationId, productId, vendorId - // The function returns the number of ports found (0 on error). - MODULE_API int serialGetPortsInfo_(void (*function)(const char* path, - const char* manufacturer, - const char* serialNumber, - const char* pnpId, - const char* locationId, - const char* productId, - const char* vendorId)); + // Enumerate ports; callback gets simple COM name first (e.g. "COM3"), + // followed by the full device path and further meta-data. + MODULE_API int serialGetPortsInfo(void (*function)(const char* port, + const char* path, + const char* manufacturer, + const char* serialNumber, + const char* pnpId, + const char* locationId, + const char* productId, + const char* vendorId)); MODULE_API void serialClearBufferIn(int64_t handle); MODULE_API void serialClearBufferOut(int64_t handle); diff --git a/src/serial_ports_info.cpp b/src/serial_ports_info.cpp index 91ef8dd..0bfc8fc 100644 --- a/src/serial_ports_info.cpp +++ b/src/serial_ports_info.cpp @@ -47,17 +47,18 @@ static void parseVidPidSerial(const std::string& src, std::string& vid, std::str } } -extern "C" int serialGetPortsInfo_(void (*function)(const char* path, - const char* manufacturer, - const char* serialNumber, - const char* pnpId, - const char* locationId, - const char* productId, - const char* vendorId)) +extern "C" int serialGetPortsInfo(void (*function)(const char* port, + const char* path, + const char* manufacturer, + const char* serialNumber, + const char* pnpId, + const char* locationId, + const char* productId, + const char* vendorId)) { if (function == nullptr) { - invokeErrorLocal(std::to_underlying(StatusCodes::BUFFER_ERROR), "serialGetPortsInfo_: function pointer is null"); + invokeErrorLocal(std::to_underlying(StatusCodes::BUFFER_ERROR), "serialGetPortsInfo: function pointer is null"); return 0; } @@ -66,7 +67,7 @@ extern "C" int serialGetPortsInfo_(void (*function)(const char* path, HDEVINFO h_dev_info = SetupDiGetClassDevs(&guid_devinterface_comport, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (h_dev_info == INVALID_HANDLE_VALUE) { - invokeErrorLocal(std::to_underlying(StatusCodes::NOT_FOUND_ERROR), "serialGetPortsInfo_: SetupDiGetClassDevs failed"); + invokeErrorLocal(std::to_underlying(StatusCodes::NOT_FOUND_ERROR), "serialGetPortsInfo: SetupDiGetClassDevs failed"); return 0; } @@ -97,6 +98,22 @@ extern "C" int serialGetPortsInfo_(void (*function)(const char* path, std::string device_path = detail->DevicePath; + // Friendly name (contains "(COMx)") + CHAR friendly[256] = ""; + SetupDiGetDeviceRegistryPropertyA( + h_dev_info, &dev_info, SPDRP_FRIENDLYNAME, nullptr, reinterpret_cast(friendly), sizeof(friendly), nullptr); + + std::string com_name; + const char* paren = std::strchr(friendly, '('); + if (paren != nullptr) + { + const char* end_paren = std::strchr(paren, ')'); + if (end_paren != nullptr && end_paren > paren + 1) + { + com_name.assign(paren + 1, static_cast(end_paren - paren - 1)); + } + } + // Manufacturer CHAR mfg[256] = ""; SetupDiGetDeviceRegistryPropertyA(h_dev_info, &dev_info, SPDRP_MFG, nullptr, reinterpret_cast(mfg), sizeof(mfg), nullptr); @@ -120,7 +137,8 @@ extern "C" int serialGetPortsInfo_(void (*function)(const char* path, parseVidPidSerial(device_path, vid, pid, serial); } - function(device_path.c_str(), + function(com_name.c_str(), + device_path.c_str(), (*mfg != 0) ? mfg : "", serial.c_str(), (hwid[0] != 0) ? hwid : "", diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp index 8c3003f..9d8a727 100644 --- a/tests/serial_unit_tests.cpp +++ b/tests/serial_unit_tests.cpp @@ -81,75 +81,75 @@ TEST(SerialOpenTest, InvalidPathInvokesErrorCallback) } // ------------------------ serialGetPortsInfo checks ------------------------ -TEST(SerialGetPortsInfoTest, BufferTooSmallTriggersError) -{ - constexpr std::string_view separator{";"}; - std::array info_buffer{}; - std::atomic err_code{0}; - - g_err_ptr = &err_code; - serialOnError(errorCallback); - - int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); - EXPECT_EQ(result, 0); // function indicates failure via 0 - EXPECT_EQ(err_code.load(), static_cast(StatusCodes::BUFFER_ERROR)); - - serialOnError(nullptr); -} - -TEST(SerialGetPortsInfoTest, LargeBufferReturnsZeroOrOne) -{ - constexpr std::string_view separator{";"}; - std::array info_buffer{}; - - std::atomic err_code{0}; - g_err_ptr = &err_code; - serialOnError(errorCallback); - - int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); - EXPECT_GE(result, 0); - // res is 0 (no ports) or 1 (ports found) - EXPECT_LE(result, 1); - // Acceptable error codes: none or NOT_FOUND_ERROR (e.g., dir missing) - if (err_code != 0) - { - EXPECT_EQ(err_code.load(), static_cast(StatusCodes::NOT_FOUND_ERROR)); - } - - serialOnError(nullptr); -} +// TEST(SerialGetPortsInfoTest, BufferTooSmallTriggersError) +// { +// constexpr std::string_view separator{";"}; +// std::array info_buffer{}; +// std::atomic err_code{0}; + +// g_err_ptr = &err_code; +// serialOnError(errorCallback); + +// int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); +// EXPECT_EQ(result, 0); // function indicates failure via 0 +// EXPECT_EQ(err_code.load(), static_cast(StatusCodes::BUFFER_ERROR)); + +// serialOnError(nullptr); +// } + +// TEST(SerialGetPortsInfoTest, LargeBufferReturnsZeroOrOne) +// { +// constexpr std::string_view separator{";"}; +// std::array info_buffer{}; + +// std::atomic err_code{0}; +// g_err_ptr = &err_code; +// serialOnError(errorCallback); + +// int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); +// EXPECT_GE(result, 0); +// // res is 0 (no ports) or 1 (ports found) +// EXPECT_LE(result, 1); +// // Acceptable error codes: none or NOT_FOUND_ERROR (e.g., dir missing) +// if (err_code != 0) +// { +// EXPECT_EQ(err_code.load(), static_cast(StatusCodes::NOT_FOUND_ERROR)); +// } + +// serialOnError(nullptr); +// } // ---------------------------- Port listing helper --------------------------- -TEST(SerialGetPortsInfoTest, PrintAvailablePorts) -{ - constexpr std::string_view separator{";"}; - std::array info_buffer{}; - - int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); - EXPECT_GE(result, 0); - - std::string ports_str(info_buffer.data()); - if (!ports_str.empty()) - { - std::cout << "\nAvailable serial ports:\n"; - size_t start = 0; - while (true) - { - size_t pos = ports_str.find(separator.data(), start); - std::string token = ports_str.substr(start, pos - start); - std::cout << " " << token << "\n"; - if (pos == std::string::npos) - { - break; - } - start = pos + std::strlen(separator.data()); - } - } - else - { - std::cout << "\nNo serial devices found.\n"; - } -} +// TEST(SerialGetPortsInfoTest, PrintAvailablePorts) +// { +// constexpr std::string_view separator{";"}; +// std::array info_buffer{}; + +// int result = serialGetPortsInfo(info_buffer.data(), static_cast(info_buffer.size()), (void*)separator.data()); +// EXPECT_GE(result, 0); + +// std::string ports_str(info_buffer.data()); +// if (!ports_str.empty()) +// { +// std::cout << "\nAvailable serial ports:\n"; +// size_t start = 0; +// while (true) +// { +// size_t pos = ports_str.find(separator.data(), start); +// std::string token = ports_str.substr(start, pos - start); +// std::cout << " " << token << "\n"; +// if (pos == std::string::npos) +// { +// break; +// } +// start = pos + std::strlen(separator.data()); +// } +// } +// else +// { +// std::cout << "\nNo serial devices found.\n"; +// } +// } // --------------------------- Stubbed no-op APIs ---------------------------- TEST(SerialStubbedFunctions, DoNotCrash) From 2bfc6fed06d1ce113ebf1e76f67d36962b1f82ba Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 20:30:35 +0000 Subject: [PATCH 12/22] Fix com_name assignment in serialGetPortsInfo to correctly extract port name from parentheses --- src/serial_ports_info.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serial_ports_info.cpp b/src/serial_ports_info.cpp index 0bfc8fc..2eef7ce 100644 --- a/src/serial_ports_info.cpp +++ b/src/serial_ports_info.cpp @@ -110,7 +110,7 @@ extern "C" int serialGetPortsInfo(void (*function)(const char* port, const char* end_paren = std::strchr(paren, ')'); if (end_paren != nullptr && end_paren > paren + 1) { - com_name.assign(paren + 1, static_cast(end_paren - paren - 1)); + com_name.assign(paren, static_cast(end_paren - paren - 1)); } } From 4e8d394df1c33f6ab19482da4054438c0a04e0be Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 20:31:13 +0000 Subject: [PATCH 13/22] Fix com_name assignment in serialGetPortsInfo to correctly extract port name from parentheses --- src/serial_ports_info.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serial_ports_info.cpp b/src/serial_ports_info.cpp index 2eef7ce..0bfc8fc 100644 --- a/src/serial_ports_info.cpp +++ b/src/serial_ports_info.cpp @@ -110,7 +110,7 @@ extern "C" int serialGetPortsInfo(void (*function)(const char* port, const char* end_paren = std::strchr(paren, ')'); if (end_paren != nullptr && end_paren > paren + 1) { - com_name.assign(paren, static_cast(end_paren - paren - 1)); + com_name.assign(paren + 1, static_cast(end_paren - paren - 1)); } } From 1a4168f2d670ca1bc2acc5a83c2479aab2ff5cbc Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 21:12:14 +0000 Subject: [PATCH 14/22] Refactor serial port functions: rename handle parameters for clarity, remove unused peek functionality, and introduce serialReadUntilSequence for reading until a specific sequence. Update related unit tests accordingly. --- src/serial.cpp | 184 +++++++++++++----------------------- src/serial.h | 13 +-- tests/serial_unit_tests.cpp | 3 +- 3 files changed, 74 insertions(+), 126 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index 745f5f7..17509f3 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -37,9 +37,6 @@ struct SerialPortHandle int64_t rx_total{0}; // bytes received so far int64_t tx_total{0}; // bytes transmitted so far - bool has_peek{false}; - char peek_char{0}; - std::atomic abort_read{false}; std::atomic abort_write{false}; }; @@ -53,11 +50,11 @@ void invokeError(int code, const char* message) } // Convert baudrate integer to constant directly (Windows API accepts int) -bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int stopBits) +bool configurePort(HANDLE handle, int baudrate, int dataBits, int parity, int stopBits) { DCB dcb{}; dcb.DCBlength = sizeof(DCB); - if (GetCommState(h, &dcb) == 0) + if (GetCommState(handle, &dcb) == 0) { return false; } @@ -93,10 +90,10 @@ bool configurePort(HANDLE h, int baudrate, int dataBits, int parity, int stopBit dcb.fOutX = FALSE; dcb.fInX = FALSE; - return !(SetCommState(h, &dcb) == 0); + return !(SetCommState(handle, &dcb) == 0); } -void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) +void setPortTimeouts(HANDLE handle, int readTimeoutMs, int writeTimeoutMs) { COMMTIMEOUTS timeouts{}; @@ -122,7 +119,7 @@ void setPortTimeouts(HANDLE h, int readTimeoutMs, int writeTimeoutMs) timeouts.WriteTotalTimeoutMultiplier = 0; } - SetCommTimeouts(h, &timeouts); + SetCommTimeouts(handle, &timeouts); } // Helper that adds the required "\\\\.\\" prefix for COM ports >= 10 @@ -287,39 +284,17 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS // --- Public IO wrappers ----------------------------------------------------- -int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/) +int serialRead(int64_t handlePointer, void* buffer, int bufferSize, int timeout, int /*multiplier*/) { - auto* handle = reinterpret_cast(handlePtr); + auto* handle = reinterpret_cast(handlePointer); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialRead: handle pointer is null"); return 0; } - // First deliver byte from internal peek buffer if present - int total_copied = 0; - if (handle->has_peek && bufferSize > 0) - { - static_cast(buffer)[0] = handle->peek_char; - handle->has_peek = false; - handle->rx_total += 1; - total_copied = 1; - - buffer = static_cast(buffer) + 1; - bufferSize -= 1; - - if (bufferSize == 0) - { - if (read_callback != nullptr) - { - read_callback(total_copied); - } - return total_copied; - } - } - int bytes_read = readFromPort(handle, buffer, bufferSize, timeout); - return total_copied + bytes_read; + return bytes_read; } int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeout, int /*multiplier*/) @@ -330,22 +305,22 @@ int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeo // ---------------- Higher level helpers ------------------------------------- -int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPtr) +int serialReadUntil(int64_t handlePointer, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPointer) { - auto* handle = reinterpret_cast(handlePtr); + auto* handle = reinterpret_cast(handlePointer); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntil: handle pointer is null"); return 0; } - char until_char = *static_cast(untilCharPtr); + char until_char = *static_cast(untilCharPointer); int total = 0; auto* buf = static_cast(buffer); while (total < bufferSize) { - int r = serialRead(handlePtr, buf + total, 1, timeout, 1); + int r = serialRead(handlePointer, buf + total, 1, timeout, 1); if (r <= 0) { break; @@ -365,6 +340,53 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout return total; } +int serialReadUntilSequence(int64_t handlePointer, void* buffer, int bufferSize, int timeout, void* sequencePointer) +{ + auto* handle = reinterpret_cast(handlePointer); + if (handle == nullptr) + { + invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntilSequence: handle pointer is null"); + return 0; + } + + const char* sequence = static_cast(sequencePointer); + int token_len = static_cast(std::strlen(sequence)); + + auto* buf = static_cast(buffer); + int total = 0; + int match_pos = 0; + + while (total < bufferSize) + { + int r = serialRead(handlePointer, buf + total, 1, timeout, 1); + if (r <= 0) + { + break; + } + + if (buf[total] == sequence[match_pos]) + { + match_pos += 1; + if (match_pos == token_len) + { + total += 1; + break; // full token matched + } + } + else + { + match_pos = 0; // reset match progress + } + total += r; + } + + if (read_callback != nullptr) + { + read_callback(total); + } + return total; +} + // ----------------------------------------------------------------------------- // Buffer & abort helpers implementations // ----------------------------------------------------------------------------- @@ -377,7 +399,6 @@ void serialClearBufferIn(int64_t handlePtr) return; } PurgeComm(handle->handle, PURGE_RXABORT | PURGE_RXCLEAR); - handle->has_peek = false; } void serialClearBufferOut(int64_t handlePtr) @@ -451,53 +472,6 @@ int serialWriteLine(int64_t handlePtr, const void* buffer, int bufferSize, int t return (written_nl == 1) ? (bytes_written + 1) : bytes_written; } -int serialReadUntilToken(int64_t handlePtr, void* buffer, int bufferSize, int timeout, void* tokenPtr) -{ - auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr) - { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntilToken: handle pointer is null"); - return 0; - } - - const char* token = static_cast(tokenPtr); - int token_len = static_cast(std::strlen(token)); - - auto* buf = static_cast(buffer); - int total = 0; - int match_pos = 0; - - while (total < bufferSize) - { - int r = serialRead(handlePtr, buf + total, 1, timeout, 1); - if (r <= 0) - { - break; - } - - if (buf[total] == token[match_pos]) - { - match_pos += 1; - if (match_pos == token_len) - { - total += 1; - break; // full token matched - } - } - else - { - match_pos = 0; // reset match progress - } - total += r; - } - - if (read_callback != nullptr) - { - read_callback(total); - } - return total; -} - int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout, char startByte, char endByte) { auto* buf = static_cast(buffer); @@ -507,8 +481,8 @@ int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout char byte = 0; while (true) { - int r = serialRead(handlePtr, &byte, 1, timeout, 1); - if (r <= 0) + int bytes_read = serialRead(handlePtr, &byte, 1, timeout, 1); + if (bytes_read <= 0) { return 0; // timeout or error } @@ -523,8 +497,8 @@ int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout // Read until end byte while (total < bufferSize) { - int r = serialRead(handlePtr, &byte, 1, timeout, 1); - if (r <= 0) + int bytes_read = serialRead(handlePtr, &byte, 1, timeout, 1); + if (bytes_read <= 0) { break; } @@ -540,42 +514,18 @@ int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout } // Statistics helpers -int64_t serialGetRxBytes(int64_t handlePtr) +int64_t serialOutBytesTotal(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); return (handle != nullptr) ? handle->rx_total : 0; } -int64_t serialGetTxBytes(int64_t handlePtr) +int64_t serialInBytesTotal(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); return (handle != nullptr) ? handle->tx_total : 0; } -int serialPeek(int64_t handlePtr, void* outByte, int timeout) -{ - auto* handle = reinterpret_cast(handlePtr); - if (handle == nullptr || outByte == nullptr) - { - invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialPeek: handle or outByte pointer is null"); - return 0; - } - - if (handle->has_peek) - { - *static_cast(outByte) = handle->peek_char; - return 1; - } - - int r = serialRead(handlePtr, &handle->peek_char, 1, timeout, 1); - if (r == 1) - { - handle->has_peek = true; - *static_cast(outByte) = handle->peek_char; - } - return r; -} - int serialDrain(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); @@ -597,7 +547,7 @@ int serialDrain(int64_t handlePtr) // Buffer occupancy helpers (similar to PySerial in_waiting / out_waiting) // ----------------------------------------------------------------------------- -int serialInWaiting(int64_t handlePtr) +int serialInBytesWaiting(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) @@ -616,7 +566,7 @@ int serialInWaiting(int64_t handlePtr) return static_cast(status.cbInQue); } -int serialOutWaiting(int64_t handlePtr) +int serialOutBytesWaiting(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) diff --git a/src/serial.h b/src/serial.h index 7e70398..99e3ef6 100644 --- a/src/serial.h +++ b/src/serial.h @@ -62,23 +62,20 @@ extern "C" MODULE_API int serialWriteLine(int64_t handle, const void* buffer, int bufferSize, int timeout /*ms*/); - MODULE_API int serialReadUntilToken(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, void* token); + MODULE_API int serialReadUntilSequence(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, void* sequence); MODULE_API int serialReadFrame(int64_t handle, void* buffer, int bufferSize, int timeout /*ms*/, char startByte, char endByte); // Byte statistics - MODULE_API int64_t serialGetRxBytes(int64_t handle); - MODULE_API int64_t serialGetTxBytes(int64_t handle); - - // Peek next byte without consuming - MODULE_API int serialPeek(int64_t handle, void* outByte, int timeout /*ms*/); + MODULE_API int64_t serialOutBytesTotal(int64_t handle); + MODULE_API int64_t serialInBytesTotal(int64_t handle); // Drain pending TX bytes (wait until sent) MODULE_API int serialDrain(int64_t handle); // Bytes currently queued in the driver buffers - MODULE_API int serialInWaiting(int64_t handle); - MODULE_API int serialOutWaiting(int64_t handle); + MODULE_API int serialInBytesWaiting(int64_t handle); + MODULE_API int serialOutBytesWaiting(int64_t handle); #ifdef __cplusplus } diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp index 9d8a727..4bfd1be 100644 --- a/tests/serial_unit_tests.cpp +++ b/tests/serial_unit_tests.cpp @@ -181,7 +181,8 @@ TEST(SerialHelpers, ReadUntilToken) std::array read_buffer{}; constexpr std::string_view ok_token{"OK"}; - int num_read = serialReadUntilToken(dev.handle, read_buffer.data(), static_cast(read_buffer.size()), 2000, (void*)ok_token.data()); + int num_read = + serialReadUntilSequence(dev.handle, read_buffer.data(), static_cast(read_buffer.size()), 2000, (void*)ok_token.data()); ASSERT_EQ(num_read, static_cast(payload.size())); ASSERT_EQ(std::string_view(read_buffer.data(), num_read), payload); } From 659a15abcb6e20440c86f4a6aafc734993a786a2 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 21:13:10 +0000 Subject: [PATCH 15/22] Refactor parameter names in serial port functions for consistency and clarity, updating handlePointer to handlePtr and untilCharPointer to untilCharPtr. This improves code readability and maintains uniformity across function signatures. --- src/serial.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index 17509f3..83ef85e 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -284,9 +284,9 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS // --- Public IO wrappers ----------------------------------------------------- -int serialRead(int64_t handlePointer, void* buffer, int bufferSize, int timeout, int /*multiplier*/) +int serialRead(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/) { - auto* handle = reinterpret_cast(handlePointer); + auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialRead: handle pointer is null"); @@ -305,22 +305,22 @@ int serialWrite(int64_t handlePtr, const void* buffer, int bufferSize, int timeo // ---------------- Higher level helpers ------------------------------------- -int serialReadUntil(int64_t handlePointer, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPointer) +int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout, int /*multiplier*/, void* untilCharPtr) { - auto* handle = reinterpret_cast(handlePointer); + auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntil: handle pointer is null"); return 0; } - char until_char = *static_cast(untilCharPointer); + char until_char = *static_cast(untilCharPtr); int total = 0; auto* buf = static_cast(buffer); while (total < bufferSize) { - int r = serialRead(handlePointer, buf + total, 1, timeout, 1); + int r = serialRead(handlePtr, buf + total, 1, timeout, 1); if (r <= 0) { break; @@ -340,16 +340,16 @@ int serialReadUntil(int64_t handlePointer, void* buffer, int bufferSize, int tim return total; } -int serialReadUntilSequence(int64_t handlePointer, void* buffer, int bufferSize, int timeout, void* sequencePointer) +int serialReadUntilSequence(int64_t handlePtr, void* buffer, int bufferSize, int timeout, void* sequencePtr) { - auto* handle = reinterpret_cast(handlePointer); + auto* handle = reinterpret_cast(handlePtr); if (handle == nullptr) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialReadUntilSequence: handle pointer is null"); return 0; } - const char* sequence = static_cast(sequencePointer); + const char* sequence = static_cast(sequencePtr); int token_len = static_cast(std::strlen(sequence)); auto* buf = static_cast(buffer); @@ -358,7 +358,7 @@ int serialReadUntilSequence(int64_t handlePointer, void* buffer, int bufferSize, while (total < bufferSize) { - int r = serialRead(handlePointer, buf + total, 1, timeout, 1); + int r = serialRead(handlePtr, buf + total, 1, timeout, 1); if (r <= 0) { break; From 004924d4797e065af674ba0dd21950a5da82f22e Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 21:14:58 +0000 Subject: [PATCH 16/22] Rename callback function pointers for consistency: change error_callback, read_callback, and write_callback to on_error_callback, on_read_callback, and on_write_callback respectively. Update related function implementations and declarations to reflect these changes. --- src/serial.cpp | 32 ++++++++++++++++---------------- src/serial.h | 6 +++--- src/serial_ports_info.cpp | 4 ++-- tests/serial_unit_tests.cpp | 21 ++------------------- 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index 83ef85e..b978732 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -19,9 +19,9 @@ // ----------------------------------------------------------------------------- // Global callback function pointers (default nullptr) // ----------------------------------------------------------------------------- -void (*error_callback)(int, const char*) = nullptr; -void (*read_callback)(int) = nullptr; -void (*write_callback)(int) = nullptr; +void (*on_error_callback)(int, const char*) = nullptr; +void (*on_read_callback)(int) = nullptr; +void (*on_write_callback)(int) = nullptr; // ----------------------------------------------------------------------------- // Internal helpers & types @@ -43,9 +43,9 @@ struct SerialPortHandle void invokeError(int code, const char* message) { - if (error_callback != nullptr) + if (on_error_callback != nullptr) { - error_callback(code, message); + on_error_callback(code, message); } } @@ -238,9 +238,9 @@ static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, if (bytes_read > 0) { handle->rx_total += bytes_read; - if (read_callback != nullptr) + if (on_read_callback != nullptr) { - read_callback(static_cast(bytes_read)); + on_read_callback(static_cast(bytes_read)); } } @@ -273,9 +273,9 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS if (bytes_written > 0) { handle->tx_total += bytes_written; - if (write_callback != nullptr) + if (on_write_callback != nullptr) { - write_callback(static_cast(bytes_written)); + on_write_callback(static_cast(bytes_written)); } } @@ -333,9 +333,9 @@ int serialReadUntil(int64_t handlePtr, void* buffer, int bufferSize, int timeout total += r; } - if (read_callback != nullptr) + if (on_read_callback != nullptr) { - read_callback(total); + on_read_callback(total); } return total; } @@ -380,9 +380,9 @@ int serialReadUntilSequence(int64_t handlePtr, void* buffer, int bufferSize, int total += r; } - if (read_callback != nullptr) + if (on_read_callback != nullptr) { - read_callback(total); + on_read_callback(total); } return total; } @@ -435,15 +435,15 @@ void serialAbortWrite(int64_t handlePtr) void serialOnRead(void (*func)(int bytes)) { - read_callback = func; + on_read_callback = func; } void serialOnWrite(void (*func)(int bytes)) { - write_callback = func; + on_write_callback = func; } void serialOnError(void (*func)(int code, const char* message)) { - error_callback = func; + on_error_callback = func; } // ----------------------------------------------------------------------------- diff --git a/src/serial.h b/src/serial.h index 99e3ef6..36e12e3 100644 --- a/src/serial.h +++ b/src/serial.h @@ -50,9 +50,9 @@ extern "C" MODULE_API void serialAbortWrite(int64_t handle); // Optional callback hooks (can be nullptr) - extern void (*read_callback)(int bytes); - extern void (*write_callback)(int bytes); - extern void (*error_callback)(int errorCode, const char* message); + extern void (*on_read_callback)(int bytes); + extern void (*on_write_callback)(int bytes); + extern void (*on_error_callback)(int errorCode, const char* message); MODULE_API void serialOnRead(void (*func)(int bytes)); MODULE_API void serialOnWrite(void (*func)(int bytes)); diff --git a/src/serial_ports_info.cpp b/src/serial_ports_info.cpp index 0bfc8fc..8906bf8 100644 --- a/src/serial_ports_info.cpp +++ b/src/serial_ports_info.cpp @@ -16,9 +16,9 @@ static void invokeErrorLocal(int code, const char* message) { - if (error_callback != nullptr) + if (on_error_callback != nullptr) { - error_callback(code, message); + on_error_callback(code, message); } } diff --git a/tests/serial_unit_tests.cpp b/tests/serial_unit_tests.cpp index 4bfd1be..b82052c 100644 --- a/tests/serial_unit_tests.cpp +++ b/tests/serial_unit_tests.cpp @@ -187,23 +187,6 @@ TEST(SerialHelpers, ReadUntilToken) ASSERT_EQ(std::string_view(read_buffer.data(), num_read), payload); } -TEST(SerialHelpers, Peek) -{ - SerialDevice dev; - const std::string payload = "XYZ"; - dev.writeToDevice(payload); - - char first_byte = 0; - int peek_result = serialPeek(dev.handle, &first_byte, 2000); - ASSERT_EQ(peek_result, 1); - ASSERT_EQ(first_byte, 'X'); - - std::array read_buffer{}; - int num_read = serialRead(dev.handle, read_buffer.data(), 3, 2000, 1); - ASSERT_EQ(num_read, 3); - ASSERT_EQ(std::string_view(read_buffer.data(), 3), payload); -} - TEST(SerialHelpers, Statistics) { SerialDevice dev; @@ -220,8 +203,8 @@ TEST(SerialHelpers, Statistics) int bytes_read = serialRead(dev.handle, read_buffer.data(), static_cast(payload.size()), 2000, 1); ASSERT_EQ(bytes_read, static_cast(payload.size())); - ASSERT_EQ(serialGetTxBytes(dev.handle), static_cast(payload.size())); - ASSERT_EQ(serialGetRxBytes(dev.handle), static_cast(payload.size())); + ASSERT_EQ(serialOutBytesTotal(dev.handle), static_cast(payload.size())); + ASSERT_EQ(serialInBytesTotal(dev.handle), static_cast(payload.size())); } TEST(SerialHelpers, Drain) From 1f43c0a3415b644320e69703bd6e3f34c4a8c8c2 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 21:46:57 +0000 Subject: [PATCH 17/22] Refactor serial port statistics: rename rx_total to in_total and tx_total to out_total for clarity. Update related function implementations and callback registration to maintain consistency. --- src/serial.cpp | 62 +++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index b978732..26dc649 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -34,8 +34,8 @@ struct SerialPortHandle HANDLE handle{INVALID_HANDLE_VALUE}; DCB original_dcb{}; // keep original settings so we can restore on close - int64_t rx_total{0}; // bytes received so far - int64_t tx_total{0}; // bytes transmitted so far + int64_t in_total{0}; // bytes received so far + int64_t out_total{0}; // bytes transmitted so far std::atomic abort_read{false}; std::atomic abort_write{false}; @@ -123,16 +123,16 @@ void setPortTimeouts(HANDLE handle, int readTimeoutMs, int writeTimeoutMs) } // Helper that adds the required "\\\\.\\" prefix for COM ports >= 10 -std::string toWinComPath(std::string_view port) +std::string toWinComPath(std::string_view portView) { - std::string p(port); + std::string port(portView); // If the path already starts with \\.\, leave it - if (p.starts_with(R"(\\.\)")) + if (port.starts_with(R"(\\.\)")) { - return p; + return port; } // Prepend prefix so Windows can open COM10+ - return R"(\\.\)" + p; + return R"(\\.\)" + port; } } // namespace @@ -152,15 +152,15 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop std::string_view port_name_view{static_cast(port)}; std::string win_port = toWinComPath(port_name_view); - HANDLE h = CreateFileA(win_port.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, // exclusive access - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - nullptr); + HANDLE raw_handle = CreateFileA(win_port.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, // exclusive access + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr); - if (h == INVALID_HANDLE_VALUE) + if (raw_handle == INVALID_HANDLE_VALUE) { invokeError(std::to_underlying(StatusCodes::INVALID_HANDLE_ERROR), "serialOpen: CreateFileA failed (INVALID_HANDLE_VALUE)"); return 0; @@ -169,26 +169,26 @@ intptr_t serialOpen(void* port, int baudrate, int dataBits, int parity, int stop // Save original DCB first DCB original{}; original.DCBlength = sizeof(DCB); - if (GetCommState(h, &original) == 0) + if (GetCommState(raw_handle, &original) == 0) { invokeError(std::to_underlying(StatusCodes::GET_STATE_ERROR), "serialOpen: GetCommState failed"); - CloseHandle(h); + CloseHandle(raw_handle); return 0; } // Configure DCB with requested settings - if (!configurePort(h, baudrate, dataBits, parity, stopBits)) + if (!configurePort(raw_handle, baudrate, dataBits, parity, stopBits)) { invokeError(std::to_underlying(StatusCodes::SET_STATE_ERROR), "serialOpen: configurePort failed"); - CloseHandle(h); + CloseHandle(raw_handle); return 0; } // Default timeouts – can be overridden per read/write call - setPortTimeouts(h, 1000, 1000); + setPortTimeouts(raw_handle, 1000, 1000); auto* handle = new SerialPortHandle{}; - handle->handle = h; + handle->handle = raw_handle; handle->original_dcb = original; return reinterpret_cast(handle); @@ -237,7 +237,7 @@ static int readFromPort(SerialPortHandle* handle, void* buffer, int bufferSize, if (bytes_read > 0) { - handle->rx_total += bytes_read; + handle->in_total += bytes_read; if (on_read_callback != nullptr) { on_read_callback(static_cast(bytes_read)); @@ -272,7 +272,7 @@ static int writeToPort(SerialPortHandle* handle, const void* buffer, int bufferS if (bytes_written > 0) { - handle->tx_total += bytes_written; + handle->out_total += bytes_written; if (on_write_callback != nullptr) { on_write_callback(static_cast(bytes_written)); @@ -433,17 +433,17 @@ void serialAbortWrite(int64_t handlePtr) // Callback registration // ----------------------------------------------------------------------------- -void serialOnRead(void (*func)(int bytes)) +void serialOnRead(void (*callbackFn)(int bytes)) { - on_read_callback = func; + on_read_callback = callbackFn; } -void serialOnWrite(void (*func)(int bytes)) +void serialOnWrite(void (*callbackFn)(int bytes)) { - on_write_callback = func; + on_write_callback = callbackFn; } -void serialOnError(void (*func)(int code, const char* message)) +void serialOnError(void (*callbackFn)(int code, const char* message)) { - on_error_callback = func; + on_error_callback = callbackFn; } // ----------------------------------------------------------------------------- @@ -517,13 +517,13 @@ int serialReadFrame(int64_t handlePtr, void* buffer, int bufferSize, int timeout int64_t serialOutBytesTotal(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - return (handle != nullptr) ? handle->rx_total : 0; + return (handle != nullptr) ? handle->in_total : 0; } int64_t serialInBytesTotal(int64_t handlePtr) { auto* handle = reinterpret_cast(handlePtr); - return (handle != nullptr) ? handle->tx_total : 0; + return (handle != nullptr) ? handle->out_total : 0; } int serialDrain(int64_t handlePtr) From ab38c9f12bbf020df05c89a40619750e53c74f16 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 21:54:40 +0000 Subject: [PATCH 18/22] Bump version to 1.0.0 in CMakeLists.txt --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4659a2f..9f253a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.30) -set(VERSION_MAJOR 0) -set(VERSION_MINOR 2) +set(VERSION_MAJOR 1) +set(VERSION_MINOR 0) set(VERSION_PATCH 0) set(PROJECT_N cpp_windows_bindings) From 1df345da1575e902274595222aae677b9ea9e194 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 22:12:31 +0000 Subject: [PATCH 19/22] Refactor versioning: replace version_config.cpp with version_config.h for better structure. Update CMake configuration and include paths accordingly. --- .gitignore | 2 +- CMakeLists.txt | 2 +- src/serial.cpp | 1 + src/serial.h | 7 +++---- src/version_config.cpp | 22 ++++++++++++++++++++++ versioning/version_config.cpp.in | 16 ---------------- versioning/version_config.h.in | 11 +++++++++++ 7 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 src/version_config.cpp delete mode 100644 versioning/version_config.cpp.in create mode 100644 versioning/version_config.h.in diff --git a/.gitignore b/.gitignore index f80d786..33bb172 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .vscode/ build/ -src/version_config.cpp +src/version_config.h .cache/ compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f253a3..21dd027 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/**.cpp) set(LIB true) -configure_file(versioning/version_config.cpp.in ${PROJECT_SOURCE_DIR}/src/version_config.cpp) +configure_file(versioning/version_config.h.in ${PROJECT_SOURCE_DIR}/src/version_config.h) # a macro that gets all of the header containing directories. MACRO(header_directories return_list includes_base_folder extention ) diff --git a/src/serial.cpp b/src/serial.cpp index 26dc649..52cac9b 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -1,6 +1,7 @@ #include "serial.h" #include "status_codes.h" +#include "version_config.h" // Windows serial implementation // ----------------------------------------------------------------------------- diff --git a/src/serial.h b/src/serial.h index 36e12e3..d4f4d29 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,4 +1,6 @@ #pragma once +#include "version_config.h" + #include #if defined(_WIN32) || defined(__CYGWIN__) @@ -16,10 +18,7 @@ extern "C" { #endif - // Version helpers generated at configure time - MODULE_API unsigned int getMajorVersion(); - MODULE_API unsigned int getMinorVersion(); - MODULE_API unsigned int getPatchVersion(); + MODULE_API void getVersion(Version* out); // Basic serial API MODULE_API intptr_t diff --git a/src/version_config.cpp b/src/version_config.cpp new file mode 100644 index 0000000..a3dfc2d --- /dev/null +++ b/src/version_config.cpp @@ -0,0 +1,22 @@ +#include "version_config.h" + +#include "serial.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + static const Version version = {}; + + void getVersion(Version* out) + { + if (out != nullptr) + { + *out = version; + } + } + +#ifdef __cplusplus +} +#endif diff --git a/versioning/version_config.cpp.in b/versioning/version_config.cpp.in deleted file mode 100644 index d6cd6ae..0000000 --- a/versioning/version_config.cpp.in +++ /dev/null @@ -1,16 +0,0 @@ -#include "serial.h" - -unsigned int getMajorVersion() -{ - return @VERSION_MAJOR@; -} - -unsigned int getMinorVersion() -{ - return @VERSION_MINOR@; -} - -unsigned int getPatchVersion() -{ - return @VERSION_PATCH@; -} diff --git a/versioning/version_config.h.in b/versioning/version_config.h.in new file mode 100644 index 0000000..3b4cace --- /dev/null +++ b/versioning/version_config.h.in @@ -0,0 +1,11 @@ +#pragma once + +extern "C" +{ + struct Version + { + unsigned int major{@VERSION_MAJOR@}; + unsigned int minor{@VERSION_MINOR@}; + unsigned int patch{@VERSION_PATCH@}; + }; +} \ No newline at end of file From f0f2c1202a767186f7acac204e398d6737945da0 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 22:22:26 +0000 Subject: [PATCH 20/22] Update getVersion function signature to accept Version by value instead of pointer, improving simplicity and clarity in version retrieval. --- src/serial.h | 2 +- src/version_config.cpp | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/serial.h b/src/serial.h index d4f4d29..e8dac4a 100644 --- a/src/serial.h +++ b/src/serial.h @@ -18,7 +18,7 @@ extern "C" { #endif - MODULE_API void getVersion(Version* out); + MODULE_API void getVersion(Version out); // Basic serial API MODULE_API intptr_t diff --git a/src/version_config.cpp b/src/version_config.cpp index a3dfc2d..4d4a988 100644 --- a/src/version_config.cpp +++ b/src/version_config.cpp @@ -7,14 +7,11 @@ extern "C" { #endif - static const Version version = {}; + static const Version version{}; - void getVersion(Version* out) + void getVersion(Version out) { - if (out != nullptr) - { - *out = version; - } + out = version; } #ifdef __cplusplus From 7fcd5c1977d85c3e74a7c22b13e6d33e0a7f8eb2 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 22:28:23 +0000 Subject: [PATCH 21/22] Refactor getVersion function: change signature to return Version directly instead of using an output parameter, enhancing simplicity and usability. --- src/serial.h | 2 +- src/version_config.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/serial.h b/src/serial.h index e8dac4a..2d89b94 100644 --- a/src/serial.h +++ b/src/serial.h @@ -18,7 +18,7 @@ extern "C" { #endif - MODULE_API void getVersion(Version out); + MODULE_API Version getVersion(); // Basic serial API MODULE_API intptr_t diff --git a/src/version_config.cpp b/src/version_config.cpp index 4d4a988..ff66eb3 100644 --- a/src/version_config.cpp +++ b/src/version_config.cpp @@ -9,9 +9,9 @@ extern "C" static const Version version{}; - void getVersion(Version out) + Version getVersion() { - out = version; + return version; } #ifdef __cplusplus From c22d0c2dad3acafd66dc6324ee3264fe95ec5fc4 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 5 Jul 2025 22:44:20 +0000 Subject: [PATCH 22/22] Update getVersion function to use output parameter for version retrieval, enhancing safety and clarity in the API. --- src/serial.h | 2 +- src/version_config.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/serial.h b/src/serial.h index 2d89b94..d4f4d29 100644 --- a/src/serial.h +++ b/src/serial.h @@ -18,7 +18,7 @@ extern "C" { #endif - MODULE_API Version getVersion(); + MODULE_API void getVersion(Version* out); // Basic serial API MODULE_API intptr_t diff --git a/src/version_config.cpp b/src/version_config.cpp index ff66eb3..7bb2b9e 100644 --- a/src/version_config.cpp +++ b/src/version_config.cpp @@ -9,9 +9,12 @@ extern "C" static const Version version{}; - Version getVersion() + void getVersion(Version* out) { - return version; + if (out != nullptr) + { + *out = version; + } } #ifdef __cplusplus