Skip to content

Commit f388e41

Browse files
committed
Renamed 'examples/linux/' to 'examples/canbus/'
1 parent c029f5c commit f388e41

7 files changed

Lines changed: 47 additions & 25 deletions

File tree

examples/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ if(SOCKPP_WITH_UNIX_SOCKETS)
5454
add_subdirectory(unix)
5555
endif()
5656

57-
# --- Anything that is Linux-specific ---
57+
# --- Optional CANbus (SocketCAN) examples ---
5858

59-
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
60-
add_subdirectory(linux)
59+
if(SOCKPP_WITH_CAN)
60+
add_subdirectory(canbus)
6161
endif()
6262

6363
# --- Optional secure sockets ---
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# CMakeLists.txt
22
#
3-
# CMake file for the Linux SocketCAN sample applications in the
4-
# 'sockpp' library.
3+
# CMake file for the CANbus sample applications in the 'sockpp' library.
54
#
65
# ---------------------------------------------------------------------------
76
# This file is part of the "sockpp" C++ socket library.
@@ -37,22 +36,14 @@
3736
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3837
# ---------------------------------------------------------------------------
3938

40-
# --- Optional Linux SocketCAN support ---
39+
set(EXECUTABLES
40+
canbustime
41+
canbusrecv
42+
)
4143

42-
if(SOCKPP_WITH_CAN)
43-
# --- Executables ---
44+
foreach(EXECUTABLE ${EXECUTABLES})
45+
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
46+
target_link_libraries(${EXECUTABLE} Sockpp::sockpp)
47+
endforeach()
4448

45-
set(EXECUTABLES
46-
canbustime
47-
canbusrecv
48-
)
49-
50-
foreach(EXECUTABLE ${EXECUTABLES})
51-
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
52-
target_link_libraries(${EXECUTABLE} Sockpp::sockpp)
53-
endforeach()
54-
55-
# --- Install examples ---
56-
57-
install(TARGETS ${EXECUTABLES} RUNTIME DESTINATION bin)
58-
endif()
49+
install(TARGETS ${EXECUTABLES} RUNTIME DESTINATION bin)

include/sockpp/canbus/canbus_socket.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ class canbusfd_socket : public canbus_socket
261261
canbusfd_socket(const canbusfd_socket&) = delete;
262262
canbusfd_socket& operator=(const canbusfd_socket&) = delete;
263263

264-
265264
/**
266265
* Turn on FD mode for the socket on or off.
267266
*
@@ -309,6 +308,20 @@ class canbusfd_socket : public canbus_socket
309308
* @throws std::system_error if the open socket cannot enter FD mode.
310309
*/
311310
explicit canbusfd_socket(canbus_socket&& other);
311+
/**
312+
* Move constructor from a base canbus_socket.
313+
* If the incoming socket is open, it is put into FD mode.
314+
* @param other The canbus_socket to move into this one.
315+
* @param ec Gets the error code on failure; the socket is closed on error.
316+
*/
317+
explicit canbusfd_socket(canbus_socket&& other, error_code& ec) noexcept;
318+
/**
319+
* Attempts to create a CAN FD socket from an existing canbus_socket.
320+
* If the incoming socket is open, it is put into FD mode.
321+
* @param sock The canbus_socket to convert.
322+
* @return The new FD socket on success, or the error code on failure.
323+
*/
324+
static result<canbusfd_socket> try_from(canbus_socket&& sock) noexcept;
312325
/**
313326
* Move assignment.
314327
* @param rhs The other socket to move into this one.

src/canbus/canbus_socket.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "sockpp/socket.h"
4343

4444
using namespace std;
45-
using namespace std::chrono;
4645

4746
namespace sockpp {
4847

@@ -103,6 +102,26 @@ canbusfd_socket::canbusfd_socket(canbus_socket&& other) : base(std::move(other))
103102
}
104103
}
105104

105+
canbusfd_socket::canbusfd_socket(canbus_socket&& other, error_code& ec) noexcept
106+
: base(std::move(other)) {
107+
if (is_open()) {
108+
if (auto res = set_fd_mode(); !res) {
109+
ec = res.error();
110+
close();
111+
}
112+
}
113+
}
114+
115+
result<canbusfd_socket> canbusfd_socket::try_from(canbus_socket&& sock) noexcept {
116+
canbusfd_socket fd_sock;
117+
static_cast<canbus_socket&>(fd_sock) = std::move(sock);
118+
if (fd_sock.is_open()) {
119+
if (auto res = fd_sock.set_fd_mode(); !res)
120+
return res.error();
121+
}
122+
return fd_sock;
123+
}
124+
106125
result<> canbusfd_socket::open(const canbus_address& addr) noexcept {
107126
if (auto res = base::open(addr); !res)
108127
return res;

tests/unit/test_canbus_socket.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ TEST_CASE("canbus_socket classic send/recv", "[canbus][socket]") {
8383
REQUIRE(rxFrame.id_value() == CAN_ID);
8484
REQUIRE(rxFrame.len == DATA.size());
8585
REQUIRE(memcmp(rxFrame.data, DATA.data(), DATA.size()) == 0);
86-
8786
}
8887

8988
TEST_CASE("canbusfd_socket FD send/recv", "[canbus][socket]") {

0 commit comments

Comments
 (0)