summaryrefslogtreecommitdiffstats
path: root/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp')
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp288
1 files changed, 288 insertions, 0 deletions
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
new file mode 100644
index 00000000..25d0f76e
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
@@ -0,0 +1,288 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <iostream>
+#include <algorithm>
+#include <vector>
+#include "./socketcan-j1939.hpp"
+#include "socketcan-j1939-addressclaiming.hpp"
+
+namespace utils
+{
+ std::mutex socketcan_j1939_t::mutex_claiming_;
+ std::condition_variable socketcan_j1939_t::signal_address_claiming_;
+
+ /**
+ * @brief Apply a filter to the socket
+ *
+ * @param name - The name you want to receive
+ * @param pgn - The pgn you want to receive
+ * @param addr - The addr you want to receive
+ * @param name_mask - The mask to apply to the name (No mask : J1939_NO_NAME)
+ * @param pgn_mask - The mask to apply to the pgn (No mask : J1939_NO_PGN)
+ * @param addr_mask - The mask to apply to the addr (No mask : J1939_NO_ADDR)
+ */
+ void socketcan_j1939_t::add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask)
+ {
+ // AFB_DEBUG("[socketcan_j1939_t][add_filter] PGN : %" PRIu32 " ; NAME : %" PRIu64 " ; ADDR : %" PRIu8,pgn,(long unsigned int)name,addr);
+ // AFB_DEBUG("PGN_MASK : %" PRIu32 " ; NAME_MASK : %" PRIu64 "; ADDR_MASK : %" PRIu8,pgn_mask,(long unsigned int)name_mask,addr_mask);
+ int filter_on = 0;
+ struct j1939_filter filter;
+ memset(&filter, 0, sizeof(filter));
+ if (name)
+ {
+ filter.name = name;
+ if(name_mask != J1939_NO_NAME)
+ {
+ filter.name_mask = name_mask;
+ } else
+ {
+ filter.name_mask = ~0ULL;
+ }
+ ++filter_on;
+ }
+
+ if (addr < 0xff)
+ {
+ filter.addr = addr;
+ if(addr_mask != J1939_NO_ADDR)
+ {
+ filter.addr_mask = addr_mask;
+ } else
+ {
+ filter.addr_mask = ~0;
+ }
+ ++filter_on;
+ }
+ if (pgn <= J1939_PGN_MAX)
+ {
+ filter.pgn = pgn;
+ if(pgn_mask != J1939_NO_PGN)
+ {
+ filter.pgn_mask = pgn_mask;
+ } else
+ {
+ filter.pgn_mask = ~0;
+ }
+ ++filter_on;
+ }
+ if(filter_on)
+ {
+ setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
+ }
+ }
+
+ /**
+ * @brief Define some parameters on the socket
+ *
+ * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
+ * @param recv_own_msgs - Allows you to receive your own packets
+ * @param broadcast - Allows to write message with address brodcast (255)
+ */
+ void socketcan_j1939_t::define_opt(bool promisc, bool recv_own_msgs, bool broadcast)
+ {
+ int promisc_i = 0;
+ int recv_own_msgs_i = 0;
+ int broadcast_i = 0;
+
+ if(promisc) promisc_i = 1;
+ if(recv_own_msgs) recv_own_msgs_i=1;
+ if(broadcast) broadcast_i = 1;
+
+ setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
+ setopt(SOL_CAN_J1939, SO_J1939_RECV_OWN, &recv_own_msgs_i, sizeof(recv_own_msgs_i));
+ setopt(SOL_SOCKET, SO_BROADCAST, &broadcast_i, sizeof(broadcast_i));
+ }
+
+
+ /**
+ * @brief Define the tx address for the bind function
+ *
+ * @param device_name - The device can that you want to bind
+ * @param name - The name that you want to bind
+ * @param pgn - The pgn that you want to bind
+ * @param addr - The addr that you want to bindthat you want to bind
+ */
+ void socketcan_j1939_t::define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
+ {
+
+ ::strcpy(ifr_.ifr_name, device_name.c_str());
+ AFB_DEBUG("ifr_name is : %s", ifr_.ifr_name);
+
+
+ if(::ioctl(socket_, SIOCGIFINDEX, &ifr_) < 0)
+ {
+ AFB_ERROR("ioctl failed. Error was : %s", strerror(errno));
+ close();
+ }
+ else
+ {
+ tx_address_.can_ifindex = ifr_.ifr_ifindex;
+ }
+
+ tx_address_.can_family = AF_CAN;
+
+
+ if(addr <= 0 || addr >= UINT8_MAX )
+ {
+ tx_address_.can_addr.j1939.addr = J1939_NO_ADDR;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.addr = addr;
+ }
+
+ if(name <= 0 || name >= UINT64_MAX )
+ {
+ tx_address_.can_addr.j1939.name = J1939_NO_NAME;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.name = name;
+ }
+
+ if(pgn <= 0 || pgn > J1939_PGN_MAX)
+ {
+ tx_address_.can_addr.j1939.pgn = J1939_NO_PGN;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.pgn = pgn;
+ }
+
+ }
+
+
+ /**
+ * @brief Open default socket
+ *
+ * @param device_name The device name to open the socket
+ * @return int The number of the socket
+ */
+ int socketcan_j1939_t::open(std::string device_name)
+ {
+ return open(device_name,0,0,0);
+ }
+
+ /**
+ * @brief Open a socket with name pgn and address
+ *
+ * @param device_name The device name to open the socket
+ * @param name - The name that you want to bind
+ * @param pgn - The pgn that you want to bind
+ * @param addr - The addr that you want to bindthat you want to bind
+ * @return int The number of the socket
+ */
+ int socketcan_j1939_t::open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
+ {
+
+ socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_J1939);
+
+ define_tx_address(device_name,name,pgn,addr);
+
+ if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ AFB_ERROR("Bind failed. %s", strerror(errno));
+ close();
+ }
+
+ return socket_;
+ }
+
+ /**
+ * @brief Launch recvfrom on the socket with blocking flag
+ *
+ * @return std::shared_ptr<message_t> The j1939 message that is read
+ */
+ std::shared_ptr<message_t> socketcan_j1939_t::read_message()
+ {
+ return read_message(0);
+ }
+
+ /**
+ * @brief Launch recvfrom on the socket with the flag you want
+ *
+ * @param flag The flag param for the recvfrom
+ * @return std::shared_ptr<message_t> The j1939 message that is read
+ */
+ std::shared_ptr<message_t> socketcan_j1939_t::read_message(int flag)
+ {
+ socklen_t peernamelen;
+ std::shared_ptr<j1939_message_t> jm = std::make_shared<j1939_message_t>();
+ uint8_t data[128] = {0};
+
+ struct sockaddr_can peername;
+ peernamelen = sizeof(peername);
+ ssize_t nbytes = recvfrom(socket_, &data, sizeof(data), flag, (struct sockaddr *)&peername, &peernamelen);
+
+ if(nbytes < 0)
+ {
+ return nullptr;
+ }
+ //AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
+
+ struct timeval tv;
+ ioctl(socket(), SIOCGSTAMP, &tv);
+ uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+ jm = j1939_message_t::convert_from_addr(peername, data , nbytes, timestamp);
+ jm->set_sub_id((int)socket());
+ return jm;
+ }
+
+ /**
+ * @brief Write a j1939 message
+ *
+ * @param pgn The pgn that you want to emmit
+ * @param data The data that you want to emmit
+ * @param len_data The size of the data to emmit
+ * @return int 0 if the message is correctly send
+ */
+ int socketcan_j1939_t::write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data)
+ {
+ j1939_message_t msg = j1939_message_t(J1939_MAX_DLEN, len_data, message_format_t::J1939, data, 0, 0, pgn, 0);
+ msg.set_sockname(pgn,J1939_NO_NAME,J1939_NO_ADDR);
+ return write_message(msg);
+ }
+
+ /**
+ * @brief Write a j1939 message
+ *
+ * @param m A j1939 message
+ * @return int 0 if the message is correctly send
+ */
+ int socketcan_j1939_t::write_message(message_t& m)
+ {
+ j1939_message_t& jm = reinterpret_cast<j1939_message_t&>(m);
+ sockaddr_can sockname = jm.get_sockname();
+ uint8_t data[jm.get_data_vector().size()];
+
+ for(int i=0; i<jm.get_data_vector().size(); i++)
+ {
+ data[i] = jm.get_data_vector()[i];
+ }
+
+ if (sendto(socket_, &data, sizeof(data), 0, (const struct sockaddr *)&sockname, sizeof(sockname)) < 0)
+ {
+ AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));
+ return -errno;
+ }
+ return 0;
+ }
+} \ No newline at end of file