Skip to content

Commit

Permalink
Merge pull request #10 from marcfir/master
Browse files Browse the repository at this point in the history
Enhance CMAKE and restructure hw_open()
  • Loading branch information
robert-burger authored Jul 24, 2024
2 parents 319c33c + 5b16b80 commit 2088603
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
38 changes: 27 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ string(REGEX MATCH "VERSION = ([0-9]*.[0-9]*.[0-9]*)" _ ${PROJECT_PROPERTIES})
set(PROJECT_VERSION ${CMAKE_MATCH_1})
project(libethercat VERSION ${PROJECT_VERSION})

set(ECAT_DEVICE "sock_raw" CACHE STRING "EtherCAT device layer")
set(ECAT_DEVICE "sock_raw" CACHE STRING "EtherCAT device layer as `;` separated list")

include(CheckIncludeFiles)
include(CheckSymbolExists)
Expand Down Expand Up @@ -39,6 +39,7 @@ check_include_files("memory.h" LIBETHERCAT_HAVE_MEMORY_H)
check_symbol_exists("memset" "string.h" LIBETHERCAT_HAVE_MALLOC)
check_include_files("net/bpf.h" LIBETHERCAT_HAVE_NET_BPF_H)
check_include_files("net/if.h" LIBETHERCAT_HAVE_NET_IF_H)
check_include_files("netinet/in.h" LIBETHERCAT_HAVE_NETINET_IN_H)
check_include_files("pthread.h" LIBETHERCAT_HAVE_PTHREAD)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists("pthread_setaffinity_np" "pthread.h" LIBETHERCAT_HAVE_PTHREAD_SETAFFINITY_NP)
Expand Down Expand Up @@ -85,20 +86,35 @@ set(SRC_ETHERCAT
src/slave.c
src/soe.c)

if (${ECAT_DEVICE} STREQUAL "sock_raw")
set(SRC_HW_LAYER src/hw_sock_raw.c)
list(FIND ECAT_DEVICE "sock_raw" HAS_SOCK_RAW)
list(FIND ECAT_DEVICE "sock_raw_mmaped" HAS_SOCK_RAW_MMAPED)
list(FIND ECAT_DEVICE "file" HAS_SOCK_FILE)
list(FIND ECAT_DEVICE "pikeos" HAS_SOCK_PIKEOS)
list(FIND ECAT_DEVICE "bpf" HAS_SOCK_BPF)

if (${HAS_SOCK_RAW} GREATER -1)
message("Include device sock_raw")
list(APPEND SRC_HW_LAYER src/hw_sock_raw.c)
add_compile_definitions(LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_LEGACY)
elseif (${ECAT_DEVICE} STREQUAL "sock_raw_mmaped")
set(SRC_HW_LAYER src/hw_file.c)
endif()
if (${HAS_SOCK_RAW_MMAPED} GREATER -1)
message("Include device sock_raw_mmaped")
list(APPEND SRC_HW_LAYER src/hw_sock_raw_mmaped.c)
add_compile_definitions(LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_MMAPED)
elseif (${ECAT_DEVICE} STREQUAL "file")
set(SRC_HW_LAYER src/hw_file.c)
endif()
if (${HAS_SOCK_FILE} GREATER -1)
message("Include device file")
list(APPEND SRC_HW_LAYER src/hw_file.c)
add_compile_definitions(LIBETHERCAT_BUILD_DEVICE_FILE)
elseif (${ECAT_DEVICE} STREQUAL "pikeos")
set(SRC_HW_LAYER src/hw_pikeos.c)
endif()
if (${HAS_SOCK_PIKEOS} GREATER -1)
message("Include device pikeos")
list(APPEND SRC_HW_LAYER src/hw_pikeos.c)
add_compile_definitions(LIBETHERCAT_BUILD_DEVICE_PIKEOS)
elseif (${ECAT_DEVICE} STREQUAL "bpf")
set(SRC_HW_LAYER src/hw_bpf.c)
endif()
if (${HAS_SOCK_BPF} GREATER -1)
message("Include device bpf")
list(APPEND SRC_HW_LAYER src/hw_bpf.c)
add_compile_definitions(LIBETHERCAT_BUILD_DEVICE_BPD)
endif()

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ This will build and install a static as well as a dynamic library. For use in ot
mkdir build
cd build
# Please change the path to the install dir. If you chose a global install you can omit the CMAKE_PREFIX_PATH option
cmake .. -DCMAKE_PREFIX_PATH=<installdir of libosal>
# You can specify which EtherCAT devices should be included into the build with -DECAT_DEVICE="sock_raw;sock_raw_mmaped;..."
cmake .. -DCMAKE_PREFIX_PATH=<installdir of libosal> -DECAT_DEVICE="sock_raw;sock_raw_mmaped"
cmake --build .
```

Expand Down
3 changes: 3 additions & 0 deletions cmake/cmake_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
/* Define to 1 if you have the <net/if.h> header file. */
#cmakedefine LIBETHERCAT_HAVE_NET_IF_H ${LIBETHERCAT_HAVE_NET_IF_H}

/* Define to 1 if you have the <netinet/in.h> header file. */
#cmakedefine LIBETHERCAT_HAVE_NETINET_IN_H ${LIBETHERCAT_HAVE_NETINET_IN_H}

/* Define if you have POSIX threads libraries and header files. */
#cmakedefine LIBETHERCAT_HAVE_PTHREAD ${LIBETHERCAT_HAVE_PTHREAD}

Expand Down
51 changes: 24 additions & 27 deletions src/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,44 +123,41 @@ int hw_open(hw_t *phw, struct ec *pec, const osal_char_t *devname, int prio, int

ec_log(10, "HW_OPEN", "Opening interface as device file: %s\n", ifname);
ret = hw_device_file_open(phw, ifname);
} else
}
#endif
{
#if LIBETHERCAT_BUILD_DEVICE_BPF == 1
if (strncmp(ifname, "bpf:", 4) == 0) {
ifname = &ifname[4];
if (strncmp(ifname, "bpf:", 4) == 0) {
ifname = &ifname[4];

ec_log(10, "HW_OPEN", "Opening interface as BPF: %s\n", ifname);
ret = hw_device_bpf_open(phw, ifname);
} else
ec_log(10, "HW_OPEN", "Opening interface as BPF: %s\n", ifname);
ret = hw_device_bpf_open(phw, ifname);
}
#endif
#if LIBETHERCAT_BUILD_DEVICE_PIKEOS == 1
if (strncmp(ifname, "pikeos:", 4) == 0) {
ifname = &ifname[7];
if (strncmp(ifname, "pikeos:", 4) == 0) {
ifname = &ifname[4];

ec_log(10, "HW_OPEN", "Opening interface as pikeos: %s\n", ifname);
ret = hw_device_pikeos_open(phw, ifname);
} else
ec_log(10, "HW_OPEN", "Opening interface as pikeos: %s\n", ifname);
ret = hw_device_pikeos_open(phw, ifname);
}
#endif
#if LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_LEGACY == 1
if (strncmp(ifname, "sock-raw:", 9) == 0) {
ifname = &ifname[9];

ec_log(10, "HW_OPEN", "Opening interface as SOCK_RAW: %s\n", ifname);
ret = hw_device_sock_raw_open(phw, ifname);
}
#endif
#if LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_MMAPED == 1
if (strncmp(ifname, "sock-raw-mmaped:", 16) == 0) {
ifname = &ifname[16];
if (strncmp(ifname, "sock-raw-mmaped:", 16) == 0) {
ifname = &ifname[16];

ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", ifname);
ret = hw_device_sock_raw_mmaped_open(phw, ifname);
} else
ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", ifname);
ret = hw_device_sock_raw_mmaped_open(phw, ifname);
}
#endif
{
#if LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_LEGACY == 1
if (strncmp(ifname, "sock-raw:", 9) == 0) {
ifname = &ifname[9];
}

ec_log(10, "HW_OPEN", "Opening interface as SOCK_RAW: %s\n", ifname);
ret = hw_device_sock_raw_open(phw, ifname);
#endif
}
}

if (ret == EC_OK) {
phw->rxthreadrunning = 1;
Expand Down

0 comments on commit 2088603

Please sign in to comment.