2 * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loïc Collignon" <loic.collignon@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include <linux/can/raw.h>
23 #include <sys/ioctl.h>
26 #include "low-can-binding.hpp"
30 /// @brief Construct a default, invalid, socket.
31 socketcan_t::socketcan_t()
32 : socket_{INVALID_SOCKET}
35 /// @brief Construct a socket by moving an existing one.
36 socketcan_t::socketcan_t(socketcan_t&& s)
39 s.socket_ = INVALID_SOCKET;
42 /// @brief Destruct the socket.
43 socketcan_t::~socketcan_t()
45 if(socket_ != INVALID_SOCKET)
49 /// @brief Test if socket is valid.
50 /// @return true if valid, false otherwise.
51 socketcan_t::operator bool() const
53 return socket_ != INVALID_SOCKET;
56 /// @brief Open the socket.
57 /// @param[in] domain Specifies the communications domain in which a socket is to be created.
58 /// @param[in] type Specifies the type of socket to be created.
59 /// @param[in] protocol Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.
60 /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
61 int socketcan_t::open(int domain, int type, int protocol)
64 socket_ = ::socket(domain, type, protocol);
68 /// @brief Close the socket.
69 /// @return 0 if success.
70 int socketcan_t::close()
72 return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
75 /// @brief Set socket option.
76 /// @return 0 if success.
77 int socketcan_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
79 return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
82 /// @brief Bind the socket.
83 /// @return 0 if success.
84 int socketcan_t::bind(const struct sockaddr* addr, socklen_t len)
86 return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
89 /// @brief Connect the socket.
90 /// @return 0 if success.
91 int socketcan_t::connect(const struct sockaddr* addr, socklen_t len)
93 return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
96 /// @brief Get the file descriptor.
97 /// @return The socket's file descriptor
98 int socketcan_t::socket() const
103 /// @brief Open a raw socket CAN.
104 /// @param[in] device_name is the kernel network device name of the CAN interface.
106 /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
107 int socketcan_t::open(std::string device_name, bool bcm)
113 socket_ = ::socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
115 socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
117 // Attempts to open a socket to CAN bus
118 ::strcpy(ifr.ifr_name, device_name.c_str());
119 DEBUG(binder_interface, "%s: ifr_name is : %s", __FUNCTION__, ifr.ifr_name);
120 if(::ioctl(socket_, SIOCGIFINDEX, &ifr) < 0)
122 ERROR(binder_interface, "%s: ioctl failed. Error was : %s", __FUNCTION__, strerror(errno));
127 txAddress_.can_family = AF_CAN;
128 txAddress_.can_ifindex = ifr.ifr_ifindex;
130 if(bcm && connect((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
132 ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
135 // It's a RAW socket request, bind it to txAddress
136 else if(bind((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
138 ERROR(binder_interface, "%s: Bind failed. %s", __FUNCTION__, strerror(errno));
145 /// @brief Send a CAN message through the socket.
147 /// @param[in] f - the CAN FD frame to send
149 /// @return number of sent bytes if message sent, 0 on invalid socket and -1 if something wrong.
150 ssize_t socketcan_t::send(const struct canfd_frame& f)
152 return socket_ != INVALID_SOCKET ? ::sendto(socket_, &f, sizeof(struct canfd_frame), 0,
153 (struct sockaddr*)&txAddress_, sizeof(txAddress_)) : 0;