Skip to content

Commit

Permalink
Allow padding to be enabled / disabled via cmake, and value set
Browse files Browse the repository at this point in the history
  • Loading branch information
pgreenland committed Feb 20, 2024
1 parent dd089b4 commit 36d4f0f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ project(isotp LANGUAGES C VERSION 1.0.1 DESCRIPTION "A platform-agnostic ISOTP i

option(isotpc_USE_INCLUDE_DIR "Copy header files to separate include directory in current binary dir for better separation of header files to combat potential naming conflicts." OFF)
option(isotpc_STATIC_LIBRARY "Compile libisotpc as a static library, instead of a shared library." OFF)
option(isotpc_PAD_CAN_FRAMES "Pad CAN frames to their full size." ON)
set(isotpc_CAN_FRAME_PAD_VALUE "0xAA" CACHE STRING "Padding byte value to be used in CAN frames if enabled")

if (isotpc_STATIC_LIBRARY)
add_library(isotp STATIC ${CMAKE_CURRENT_SOURCE_DIR}/isotp.c)
Expand All @@ -25,6 +27,13 @@ target_compile_options(
-Wno-unknown-pragmas # ignore unknown pragmas, such as #pragma region
)

###
# Provide padding configuration
###
if (isotpc_PAD_CAN_FRAMES)
target_compile_definitions(isotp PRIVATE -DISO_TP_FRAME_PADDING -DISO_TP_FRAME_PADDING_VALUE=${isotpc_CAN_FRAME_PAD_VALUE})
endif()

###
# Check for debug builds
###
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $(BIN)/$(LIB_NAME).$(MAJOR_VER).$(MINOR_VER).$(REVISION): libisotp.o
# Compiles the isotp.c TU to an object file.
###
libisotp.o: isotp.c
${COMP} -c $^ -o $@ ${CFLAGS}
${COMP} -c $^ -o $@ ${CFLAGS} -DISO_TP_FRAME_PADDING

install: all
@printf "Installing $(LIB_NAME) to $(INSTALL_DIR)...\n"
Expand Down
6 changes: 3 additions & 3 deletions isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static int isotp_send_flow_control(IsoTpLink* link, uint8_t flow_status, uint8_t

/* send message */
#ifdef ISO_TP_FRAME_PADDING
(void) memset(message.as.flow_control.reserve, ISO_TP_FRAME_PADDING, sizeof(message.as.flow_control.reserve));
(void) memset(message.as.flow_control.reserve, ISO_TP_FRAME_PADDING_VALUE, sizeof(message.as.flow_control.reserve));
ret = isotp_user_send_can(link->send_arbitration_id, message.as.data_array.ptr, sizeof(message));
#else
ret = isotp_user_send_can(link->send_arbitration_id,
Expand All @@ -65,7 +65,7 @@ static int isotp_send_single_frame(IsoTpLink* link, uint32_t id) {

/* send message */
#ifdef ISO_TP_FRAME_PADDING
(void) memset(message.as.single_frame.data + link->send_size, ISO_TP_FRAME_PADDING, sizeof(message.as.single_frame.data) - link->send_size);
(void) memset(message.as.single_frame.data + link->send_size, ISO_TP_FRAME_PADDING_VALUE, sizeof(message.as.single_frame.data) - link->send_size);
ret = isotp_user_send_can(id, message.as.data_array.ptr, sizeof(message));
#else
ret = isotp_user_send_can(id,
Expand Down Expand Up @@ -120,7 +120,7 @@ static int isotp_send_consecutive_frame(IsoTpLink* link) {

/* send message */
#ifdef ISO_TP_FRAME_PADDING
(void) memset(message.as.consecutive_frame.data + data_length, ISO_TP_FRAME_PADDING, sizeof(message.as.consecutive_frame.data) - data_length);
(void) memset(message.as.consecutive_frame.data + data_length, ISO_TP_FRAME_PADDING_VALUE, sizeof(message.as.consecutive_frame.data) - data_length);
ret = isotp_user_send_can(link->send_arbitration_id, message.as.data_array.ptr, sizeof(message));
#else
ret = isotp_user_send_can(link->send_arbitration_id,
Expand Down
8 changes: 7 additions & 1 deletion isotp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@

/* Private: Determines if by default, padding is added to ISO-TP message frames.
*/
#define ISO_TP_FRAME_PADDING 0xAA
//#define ISO_TP_FRAME_PADDING 0xAA

/* Private: Value to use when padding frames if enabled by ISO_TP_FRAME_PADDING
*/
#ifndef ISO_TP_FRAME_PADDING_VALUE
#define ISO_TP_FRAME_PADDING_VALUE 0xAA
#endif

#endif

0 comments on commit 36d4f0f

Please sign in to comment.