+ const int canfd_on = 1;
+ struct ifreq ifr;
+ struct timeval timeout = {1, 0};
+
+ DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
+ if (can_socket_ >= 0)
+ return 0;
+
+ can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (can_socket_ < 0)
+ {
+ ERROR(binder_interface, "socket could not be created");
+ }
+ else
+ {
+ /* Set timeout for read */
+ ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+ /* try to switch the socket into CAN_FD mode */
+ if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
+ {
+ NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
+ is_fdmode_on_ = false;
+ } else {
+ is_fdmode_on_ = true;
+ }
+
+ /* Attempts to open a socket to CAN bus */
+ ::strcpy(ifr.ifr_name, device_name_.c_str());
+ if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
+ ERROR(binder_interface, "ioctl failed");
+ else
+ {
+ txAddress_.can_family = AF_CAN;
+ txAddress_.can_ifindex = ifr.ifr_ifindex;
+
+ /* And bind it to txAddress */
+ if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
+ {
+ ERROR(binder_interface, "Bind failed");
+ }
+ else
+ {
+ ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
+ return 0;
+ }
+ }
+ close();
+ }
+ return -1;