diff --git a/CMakeLists.txt b/CMakeLists.txt index 49b69ef2..26db1e34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,20 +10,69 @@ cmake_minimum_required(VERSION 3.5) project(BPLIB C) -option(BPLIB_INCLUDE_POSIX "Whether or not to the POSIX operating system abstraction as part of BPLib (standalone builds only)" ON) +set(BPLIB_OS_LAYER OSAL CACHE STRING "Operating system adapter layer to enable (POSIX or NASA OSAL)") + +# Check if this is a subdirectory within a larger project, or if it was built standalone +# This affects how to find dependencies (e.g. OSAL) and whether to install the libraries +get_directory_property(HAS_PARENT PARENT_DIRECTORY) +if (HAS_PARENT) + set(BPLIB_STANDALONE_BUILD_MODE OFF) +else() + set(BPLIB_STANDALONE_BUILD_MODE ON) +endif() + +# The functional test program (bpcat) will only be built by default in a standalone build using POSIX OS adapter +# This is because the bpcat tool relies on direct file handle I/O as well as signals and other POSIX concepts +if (BPLIB_STANDALONE_BUILD_MODE AND BPLIB_OS_LAYER STREQUAL POSIX) + set(BPLIB_DEFAULT_BUILD_TEST_TOOLS ON) +else() + set(BPLIB_DEFAULT_BUILD_TEST_TOOLS OFF) +endif() + +# The other unit tests (coverage) can only be built in conjuction with NASA OSAL, as this provides the UT asser +# framework that these tests are built upon. The CFS build communicates this through a variable called ENABLE_UNIT_TESTS. +# If this is set, then inherit its value as the default for BPLIB_ENABLE_UNIT_TESTS (the user can still set it specifically though) +# Otherwise BPLIB_ENABLE_UNIT_TESTS can get its default based on whether this build is using OSAL +if (DEFINED ENABLE_UNIT_TESTS) + set(BPLIB_DEFAULT_BUILD_UNIT_TESTS ${ENABLE_UNIT_TESTS}) +elseif (BPLIB_OS_LAYER STREQUAL OSAL) + set(BPLIB_DEFAULT_BUILD_UNIT_TESTS ON) +else() + set(BPLIB_DEFAULT_BUILD_UNIT_TESTS OFF) +endif() + +option(BPLIB_BUILD_TEST_TOOLS "Whether or not to build the test programs as part of BPLib (standalone POSIX builds only)" ${BPLIB_DEFAULT_BUILD_TEST_TOOLS}) +option(BPLIB_INSTALL_LIBRARIES "Whether or not to install the libraries" ${BPLIB_STANDALONE_BUILD_MODE}) +option(BPLIB_USE_EXTERNAL_OSAL "Whether to use an external OSAL package, if OSAL is selected as OS layer" ${BPLIB_STANDALONE_BUILD_MODE}) +option(BPLIB_ENABLE_UNIT_TESTS "Whether to build unit tests (requires NASA OSAL and UT Assert)" ${BPLIB_DEFAULT_BUILD_UNIT_TESTS}) set(BPLIB_VERSION_STRING "3.0.99") # development +# no extra link libraries at first +set(BPLIB_SRC) set(BPLIB_COMMON_COMPILE_OPTIONS) -set(BPLIB_COMMON_PROPERTIES) -# If doing a CFS build, then build position independent code (-fPIC) for all objects -if(IS_CFS_ARCH_BUILD OR BUILD_SHARED_LIBS) - list(APPEND BPLIB_COMMON_PROPERTIES POSITION_INDEPENDENT_CODE TRUE) +# If using an external OSAL package, then locate it now +# This needs to be done at the top level so the imported targets are +# available to all submodules, for unit testing in particular. +if (BPLIB_OS_LAYER STREQUAL OSAL) + + if (BPLIB_USE_EXTERNAL_OSAL) + find_package(NasaOsal REQUIRED) + message(STATUS "Using external OSAL ${NasaOsal_VERSION}") + endif() + else() - list(APPEND BPLIB_COMMON_PROPERTIES POSITION_INDEPENDENT_CODE FALSE) -endif() + # Sanity Check - NASA OSAL provides UT assert, so unit tests cannot be built without this + if (BPLIB_ENABLE_UNIT_TESTS) + message(FATAL_ERROR "NASA OSAL is required to build unit tests") + endif() + if (IS_CFS_ARCH_BUILD) + message(FATAL_ERROR "NASA OSAL is required to build CFS library") + endif() + +endif() # If using a GNU-style compiler then enable full warnings here. # May want to add equivalent for other compiler flavors too @@ -31,9 +80,7 @@ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") list(APPEND BPLIB_COMMON_COMPILE_OPTIONS -Wall -Werror -pedantic -Wno-format-truncation -Wno-stringop-truncation) endif() -# no extra link libraries at first -set(BPLIB_LINK_LIBRARIES) - +add_subdirectory(os) add_subdirectory(common) add_subdirectory(lib) add_subdirectory(mpool) @@ -44,6 +91,7 @@ add_subdirectory(v7) list(APPEND BPLIB_SRC store/file_offload.c + $ $ $ $ @@ -52,6 +100,7 @@ list(APPEND BPLIB_SRC ) list(APPEND BPLIB_PRIVATE_INCLUDE_DIRS + $ $ $ $ @@ -66,35 +115,29 @@ if (IS_CFS_ARCH_BUILD) # Create the app module using the CFE/OSAL adapter layer # Note this ignores the setting of BPLIB_INCLUDE_POSIX, as OSAL provides this service # The CFE build system determines whether to create a shared or static object inside this routine - # TEMPORARY - for now this will also use POSIX until OSAL has the needed features. - add_cfe_app(bplib ${BPLIB_SRC} os/heap.c os/posix.c) + add_cfe_app(bplib ${BPLIB_SRC}) target_link_libraries(bplib ${TINYCBOR_LDFLAGS}) else() # Building as a standalone library - # This by will usually require a POSIX OS layer - if (BPLIB_INCLUDE_POSIX) - - list(APPEND BPLIB_SRC os/heap.c os/posix.c) - list(APPEND BPLIB_LINK_LIBRARIES rt pthread) - - endif() - # Add the generic library target # The flavor of library (shared/static) being built here depends on the BUILD_SHARED_LIBS option # This directory may be built twice, setting this option differently to build both flavors add_library(bplib ${BPLIB_SRC}) - # compile this library as c99 (but this does not impose the same requirement on other users) - target_compile_features(bplib PRIVATE c_std_99) - - # If using GNU GCC, then also enable full warning reporting - target_compile_options(bplib PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) + # link with the requisite dependencies + target_link_libraries(bplib ${TINYCBOR_LDFLAGS}) - # link with the requisite dependencies (this is mainly for POSIX, if using that adapter) - target_link_libraries(bplib ${TINYCBOR_LDFLAGS} ${BPLIB_LINK_LIBRARIES}) + # Add in the required link libraries based on OS adapter selection + # this should preferably be in OS subdirectory, but it needs to be done + # at the bplib target which is created here. + if (BPLIB_OS_LAYER STREQUAL OSAL) + target_link_libraries(bplib osal) + elseif(BPLIB_OS_LAYER STREQUAL POSIX) + target_link_libraries(bplib rt pthread) + endif() # Install and also export this library, so it can be found via # "find_package()" from some other CMake build @@ -134,6 +177,20 @@ else() endif() +get_target_property(IS_PIC bplib POSITION_INDEPENDENT_CODE) +if (IS_PIC) + set_target_properties( + bplib_base bplib_common bplib_mpool bplib_cache bplib_v7 + PROPERTIES POSITION_INDEPENDENT_CODE ${IS_PIC} + ) +endif() + +# Set the standard compile options for all submodules (c99, full warnings) +foreach(TGT bplib bplib_base bplib_common bplib_mpool bplib_cache bplib_v7) + target_compile_features(${TGT} PRIVATE c_std_99) + target_compile_options(${TGT} PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) +endforeach() + # Internal/private header files exist within the implementation directories target_include_directories(bplib PRIVATE ${BPLIB_PRIVATE_INCLUDE_DIRS}) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 7619efbe..799dab3c 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -14,7 +14,7 @@ project(BPLIB_TEST_APP C) # Sanity check: BPLib test app assumes a POSIX system # and uses pthread calls. If not POSIX, then this tool # will not build. -if (NOT BPLIB_INCLUDE_POSIX) +if (NOT BPLIB_OS_LAYER STREQUAL POSIX) message(FATAL_ERROR "BPLib Test applications require POSIX") endif() diff --git a/cache/CMakeLists.txt b/cache/CMakeLists.txt index b0331a77..3adfe400 100644 --- a/cache/CMakeLists.txt +++ b/cache/CMakeLists.txt @@ -14,17 +14,12 @@ add_library(bplib_cache OBJECT ) target_include_directories(bplib_cache PRIVATE - $ $ $ $ - $ ) target_include_directories(bplib_cache PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc + $ + $ ) - -# Use the same basic compiler flags as bplib -target_compile_features(bplib_cache PRIVATE c_std_99) -target_compile_options(bplib_cache PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) -set_target_properties(bplib_cache PROPERTIES ${BPLIB_COMMON_PROPERTIES}) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 1e8b26b8..124824f0 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -19,16 +19,6 @@ target_include_directories(bplib_common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc ) -target_include_directories(bplib_common PRIVATE +target_include_directories(bplib_common PUBLIC $ ) - -# Use the same basic compiler flags as bplib -target_compile_features(bplib_common PRIVATE c_std_99) -target_compile_options(bplib_common PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) -set_target_properties(bplib_common PROPERTIES ${BPLIB_COMMON_PROPERTIES}) - -if(ENABLE_UNIT_TESTS) - #add_subdirectory(ut-stubs) - #add_subdirectory(ut-coverage) -endif(ENABLE_UNIT_TESTS) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 0c9ab94b..e90a7078 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -29,8 +29,3 @@ target_include_directories(bplib_base PRIVATE $ $ ) - -# Use the same basic compiler flags as bplib -target_compile_features(bplib_base PRIVATE c_std_99) -target_compile_options(bplib_base PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) -set_target_properties(bplib_base PROPERTIES ${BPLIB_COMMON_PROPERTIES}) diff --git a/mpool/CMakeLists.txt b/mpool/CMakeLists.txt index 783878e0..36851ea7 100644 --- a/mpool/CMakeLists.txt +++ b/mpool/CMakeLists.txt @@ -16,16 +16,10 @@ add_library(bplib_mpool OBJECT src/v7_mpstream.c ) -target_include_directories(bplib_mpool PRIVATE +target_include_directories(bplib_mpool PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/inc + $ $ $ ) -target_include_directories(bplib_mpool PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/inc -) - -# Use the same basic compiler flags as bplib -target_compile_features(bplib_mpool PRIVATE c_std_99) -target_compile_options(bplib_mpool PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) -set_target_properties(bplib_mpool PROPERTIES ${BPLIB_COMMON_PROPERTIES}) diff --git a/mpool/src/v7_mpool_internal.h b/mpool/src/v7_mpool_internal.h index 05d3c37e..0cdb5b88 100644 --- a/mpool/src/v7_mpool_internal.h +++ b/mpool/src/v7_mpool_internal.h @@ -24,6 +24,8 @@ #include #include "bplib_api_types.h" +#include "bplib_os.h" + #include "v7_rbtree.h" #include "v7_mpool.h" #include "v7_mpool_bblocks.h" @@ -344,6 +346,7 @@ const bplib_mpool_block_content_t *bplib_mpool_get_block_content_const(const bpl bplib_mpool_block_content_t *bplib_mpool_block_dereference_content(bplib_mpool_block_t *cb); bplib_mpool_block_content_t *bplib_mpool_alloc_block_internal(bplib_mpool_t *pool, bplib_mpool_blocktype_t blocktype, - uint32_t content_type_signature, void *init_arg, uint8_t priority); + uint32_t content_type_signature, void *init_arg, + uint8_t priority); #endif /* V7_MPOOL_INTERNAL_H */ diff --git a/os/CMakeLists.txt b/os/CMakeLists.txt new file mode 100644 index 00000000..9975e3d5 --- /dev/null +++ b/os/CMakeLists.txt @@ -0,0 +1,36 @@ +################################################################## +# +# Bundle Protocol Library (BPLib) CMake build recipe +# +# This is for the "os" submodule +# +################################################################## + +set(bplib_os_SOURCES + src/heap.c +) + +if (BPLIB_OS_LAYER STREQUAL OSAL) + + if (IS_CFS_ARCH_BUILD) + list(APPEND bplib_os_SOURCES src/cfe.c) + endif() + + list(APPEND bplib_os_SOURCES src/osal.c) + +elseif(BPLIB_OS_LAYER STREQUAL POSIX) + + list(APPEND bplib_os_SOURCES src/posix.c) + +else() + message(FATAL_ERROR "Unknown OS adapter: ${BPLIB_OS_LAYER}") +endif() + +add_library(bplib_os OBJECT + ${bplib_os_SOURCES} +) + +target_include_directories(bplib_os PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/inc + $ +) diff --git a/inc/bplib_os.h b/os/inc/bplib_os.h similarity index 81% rename from inc/bplib_os.h rename to os/inc/bplib_os.h index 03232776..9588ad7c 100644 --- a/inc/bplib_os.h +++ b/os/inc/bplib_os.h @@ -41,12 +41,6 @@ COMPILE TIME CONFIGURATION SETTINGS ******************************************************************************/ -#ifdef _GNU_ -#define VARG_CHECK(f, a, b) __attribute__((format(f, a, b))) -#else -#define VARG_CHECK(f, a, b) -#endif - /****************************************************************************** DEFINES ******************************************************************************/ @@ -62,13 +56,11 @@ PROTOTYPES ******************************************************************************/ -void bplib_os_init(void); -void bplib_os_enable_log_flags(uint32_t enable_mask); -int bplib_os_log(const char *file, unsigned int line, uint32_t *flags, uint32_t event, const char *fmt, ...) - VARG_CHECK(printf, 5, 6); -int bplib_os_systime(unsigned long *sysnow); /* seconds */ -uint64_t bplib_os_get_dtntime_ms( - void); /* get the OS time compatible with the "dtn time" definition (ms resolution + dtn epoch) */ +void bplib_os_init(void); +void bplib_os_enable_log_flags(uint32_t enable_mask); +int bplib_os_log(const char *file, unsigned int line, uint32_t *flags, uint32_t event, const char *fmt, ...); +int bplib_os_systime(unsigned long *sysnow); /* seconds */ +uint64_t bplib_os_get_dtntime_ms(void); void bplib_os_sleep(int seconds); uint32_t bplib_os_random(void); bp_handle_t bplib_os_createlock(void); @@ -82,7 +74,5 @@ int bplib_os_waiton(bp_handle_t h, int timeout_ms); int bplib_os_wait_until_ms(bp_handle_t h, uint64_t abs_dtntime_ms); void *bplib_os_calloc(size_t size); void bplib_os_free(void *ptr); -size_t bplib_os_memused(void); -size_t bplib_os_memhigh(void); #endif /* BPLIB_OS_H */ diff --git a/os/cfe.c b/os/src/cfe.c similarity index 52% rename from os/cfe.c rename to os/src/cfe.c index d6fbdb57..2bd65d55 100644 --- a/os/cfe.c +++ b/os/src/cfe.c @@ -37,11 +37,6 @@ ******************************************************************************/ #define BP_MAX_LOG_ENTRY_SIZE 256 -#define BP_RAND_HASH(seed) ((seed)*2654435761UL) /* knuth multiplier */ - -#ifndef BP_CFE_SECS_AT_2000 -#define BP_CFE_SECS_AT_2000 1325376023 /* TAI */ -#endif #ifndef BP_BPLIB_INFO_EID #define BP_BPLIB_INFO_EID 0xFF @@ -72,11 +67,6 @@ void bplib_os_enable_log_flags(uint32_t enable_mask) EXPORTED FUNCTIONS ******************************************************************************/ -/*-------------------------------------------------------------------------------------- - * bplib_os_init - - *-------------------------------------------------------------------------------------*/ -void bplib_os_init(void) {} - /*-------------------------------------------------------------------------------------- * bplib_os_log - * @@ -146,125 +136,3 @@ int bplib_os_log(const char *file, unsigned int line, uint32_t *flags, uint32_t return BP_SUCCESS; } } - -/*-------------------------------------------------------------------------------------- - * bplib_os_systime - returns seconds - *-------------------------------------------------------------------------------------*/ -int bplib_os_systime(unsigned long *sysnow) -{ - assert(sysnow); - - CFE_TIME_SysTime_t sys_time = CFE_TIME_GetTime(); - if (sys_time.Seconds < BP_CFE_SECS_AT_2000) - { - *sysnow = sys_time.Seconds; - return BP_ERROR; - } - else - { - *sysnow = sys_time.Seconds - BP_CFE_SECS_AT_2000; - return BP_SUCCESS; - } -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_sleep - *-------------------------------------------------------------------------------------*/ -void bplib_os_sleep(int seconds) -{ - OS_TaskDelay(seconds * 1000); -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_random - - *-------------------------------------------------------------------------------------*/ -uint32_t bplib_os_random(void) -{ - CFE_TIME_SysTime_t sys_time = CFE_TIME_GetMET(); - unsigned long seed = sys_time.Seconds + sys_time.Subseconds; - return (uint32_t)BP_RAND_HASH(seed); -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_createlock - - *-------------------------------------------------------------------------------------*/ -bp_handle_t bplib_os_createlock(void) -{ - return bp_handle_from_serial(1, BPLIB_HANDLE_OS_BASE); -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_destroylock - - *-------------------------------------------------------------------------------------*/ -void bplib_os_destroylock(bp_handle_t h) -{ - (void)h; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_lock - - *-------------------------------------------------------------------------------------*/ -void bplib_os_lock(bp_handle_t h) -{ - (void)h; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_unlock - - *-------------------------------------------------------------------------------------*/ -void bplib_os_unlock(bp_handle_t h) -{ - (void)h; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_signal - - *-------------------------------------------------------------------------------------*/ -void bplib_os_signal(bp_handle_t h) -{ - (void)h; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_waiton - - *-------------------------------------------------------------------------------------*/ -int bplib_os_waiton(bp_handle_t h, int timeout_ms) -{ - (void)h; - (void)timeout_ms; - return BP_TIMEOUT; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_format - - *-------------------------------------------------------------------------------------*/ -int bplib_os_format(char *dst, size_t len, const char *fmt, ...) -{ - va_list args; - int vlen; - - /* Build Formatted String */ - va_start(args, fmt); - vlen = vsnprintf(dst, len, fmt, args); - va_end(args); - - /* Return Error Code */ - return vlen; -} - -/*-------------------------------------------------------------------------------------- - * bplib_os_strnlen - - *-------------------------------------------------------------------------------------*/ -int bplib_os_strnlen(const char *str, int maxlen) -{ - int len; - for (len = 0; len < maxlen; len++) - { - if (str[len] == '\0') - { - return len; - } - } - return maxlen; -} - diff --git a/os/heap.c b/os/src/heap.c similarity index 100% rename from os/heap.c rename to os/src/heap.c diff --git a/os/src/osal.c b/os/src/osal.c new file mode 100644 index 00000000..074745f5 --- /dev/null +++ b/os/src/osal.c @@ -0,0 +1,255 @@ +/* + * NASA Docket No. GSC-18,587-1 and identified as “The Bundle Protocol Core Flight + * System Application (BP) v6.5” + * + * Copyright © 2020 United States Government as represented by the Administrator of + * the National Aeronautics and Space Administration. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/****************************************************************************** + INCLUDES + ******************************************************************************/ + +#include +#include +#include +#include +#include + +#include "common_types.h" +#include "osapi.h" +#include "bplib.h" +#include "bplib_os.h" + +/* + * The OSAL "OS_GetLocalTime()" API is assumed to use the same POSIX epoch, + * so the conversion to DTN Time is the same as for POSIX. + * + * Note that this does NOT use the CFE TIME "MET" API for DTN time, for several + * reasons - the epoch isn't well defined and not easy to convert, and the way + * it is updated via "time at tone" messages may cause jumps or skips that would + * potentially disrupt DTN state machines. + * + * DTN state machines need a relatively stable clock to work correctly, and thus + * it is recommended to employ some sort of background clock sync service (i.e. + * on a ground system this could be NTP, or suitable substitute for flight). + * Such a service runs independently of BPLib, but should manage the same clock + * that is being used by OSAL here, so BP/DTN will have a stable clock. + */ +#define OSAL_LOCALTIME_SECS_AT_2000 946684800 + +/** + * Conversion factor between OSAL time and DTN time. + * This is an epoch offset expressed as OS_time_t. The + * value should be treated as const, but it must be declared + * non-const here because it needs to be calculated at runtime + * + * The time conversion routines provided by OSAL are all runtime + * functions and thus cannot be used to initialize const data. + */ +static OS_time_t BPLIB_OSAL_LOCALTIME_DTN_CONV; + +/****************************************************************************** + FILE DATA + ******************************************************************************/ + +static osal_id_t file_data_lock; + +unsigned int bplib_os_next_serial(void) +{ + static unsigned int last_serial_num = 0; + unsigned int next_serial; + + OS_MutSemTake(file_data_lock); + next_serial = ++last_serial_num; + OS_MutSemGive(file_data_lock); + + return next_serial; +} + +/****************************************************************************** + EXPORTED FUNCTIONS + ******************************************************************************/ + +/*-------------------------------------------------------------------------------------- + * bplib_os_init - + *-------------------------------------------------------------------------------------*/ +void bplib_os_init(void) +{ + OS_MutSemCreate(&file_data_lock, "bplib_osal", 0); + + BPLIB_OSAL_LOCALTIME_DTN_CONV = OS_TimeAssembleFromMilliseconds(OSAL_LOCALTIME_SECS_AT_2000, 0); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_sleep + *-------------------------------------------------------------------------------------*/ +void bplib_os_sleep(int seconds) +{ + OS_TaskDelay(seconds * 1000); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_createlock - + *-------------------------------------------------------------------------------------*/ +bp_handle_t bplib_os_createlock(void) +{ + char lock_name[OS_MAX_API_NAME]; + int32 status; + osal_id_t id; + + snprintf(lock_name, sizeof(lock_name), "bpl%02u", bplib_os_next_serial()); + status = OS_CondVarCreate(&id, lock_name, 0); + if (status != OS_SUCCESS) + { + return BP_INVALID_HANDLE; + } + + return bp_handle_from_serial(OS_ObjectIdToInteger(id), BPLIB_HANDLE_OS_BASE); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_destroylock - + *-------------------------------------------------------------------------------------*/ +void bplib_os_destroylock(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarDelete(id); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_lock - + *-------------------------------------------------------------------------------------*/ +void bplib_os_lock(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarLock(id); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_unlock - + *-------------------------------------------------------------------------------------*/ +void bplib_os_unlock(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarUnlock(id); +} + +void bplib_os_broadcast_signal_and_unlock(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarBroadcast(id); + OS_CondVarUnlock(id); +} + +void bplib_os_broadcast_signal(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarBroadcast(id); +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_signal - + *-------------------------------------------------------------------------------------*/ +void bplib_os_signal(bp_handle_t h) +{ + osal_id_t id; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + OS_CondVarSignal(id); +} + +int bplib_os_wait_until_ms(bp_handle_t h, uint64_t abs_dtntime_ms) +{ + osal_id_t id; + OS_time_t until_time; + int32 status; + + id = OS_ObjectIdFromInteger(bp_handle_to_serial(h, BPLIB_HANDLE_OS_BASE)); + + if (abs_dtntime_ms == BP_DTNTIME_INFINITE) + { + /* Block Forever until Success */ + status = OS_CondVarWait(id); + } + else + { + until_time = OS_TimeFromTotalMilliseconds(abs_dtntime_ms); + + /* change the epoch from DTN (2000) to OSAL */ + until_time = OS_TimeAdd(until_time, BPLIB_OSAL_LOCALTIME_DTN_CONV); + + /* Block on Timed Wait and Update Timeout */ + status = OS_CondVarTimedWait(id, &until_time); + } + + /* check for timeout error explicitly and translate to BP_TIMEOUT */ + if (status == OS_ERROR_TIMEOUT) + { + return BP_TIMEOUT; + } + + /* other unexpected/unhandled errors become BP_ERROR */ + if (status != OS_SUCCESS) + { + return BP_ERROR; + } + + /* status of 0 indicates success */ + return BP_SUCCESS; +} + +/*-------------------------------------------------------------------------------------- + * bplib_os_get_dtntime_ms - returns milliseconds since DTN epoch + * this should be compatible with the BPv7 time definition + *-------------------------------------------------------------------------------------*/ +uint64_t bplib_os_get_dtntime_ms(void) +{ + OS_time_t ref_tm; + + /* + * The OSAL "GetLocalTime" API generally returns a POSIX-like time, + * with an epoch of 1970-01-01 00:00:00 UTC and assuming every "day" + * has exactly 86400 seconds. That is, adding (or subtracting) + * 86400 seconds from a POSIX time will yield the exact same time of + * day but on the next (or previous) day. Leap seconds are generally + * ignored (that is, similar to clock drift or noise that might make + * it slightly off from true UTC but then possibly corrected over time + * if running some sort of time sync in the background) + */ + OS_GetLocalTime(&ref_tm); + + /* Convert to DTN epoch */ + ref_tm = OS_TimeSubtract(ref_tm, BPLIB_OSAL_LOCALTIME_DTN_CONV); + + /* Convert to milliseconds */ + return OS_TimeGetTotalMilliseconds(ref_tm); +} diff --git a/os/posix.c b/os/src/posix.c similarity index 100% rename from os/posix.c rename to os/src/posix.c diff --git a/v7/CMakeLists.txt b/v7/CMakeLists.txt index 19cdbcdf..ca856ab7 100644 --- a/v7/CMakeLists.txt +++ b/v7/CMakeLists.txt @@ -31,14 +31,8 @@ target_include_directories(bplib_v7 PUBLIC # other bplib submodules used by this submodule $ + $ $ $ $ ) - -# compile this submodule as c99 -target_compile_features(bplib_v7 PRIVATE c_std_99) - -# Use the same basic compiler flags as bplib -target_compile_options(bplib_v7 PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS}) -set_target_properties(bplib_v7 PROPERTIES ${BPLIB_COMMON_PROPERTIES})