aboutsummaryrefslogtreecommitdiffstats
path: root/low-can-binding
diff options
context:
space:
mode:
Diffstat (limited to 'low-can-binding')
-rw-r--r--low-can-binding/binding/low-can-cb.cpp8
-rw-r--r--low-can-binding/binding/low-can-subscription.cpp6
-rw-r--r--low-can-binding/diagnostic/diagnostic-manager.cpp2
-rw-r--r--low-can-binding/utils/socketcan-bcm.cpp49
-rw-r--r--low-can-binding/utils/socketcan-bcm.hpp7
-rw-r--r--low-can-binding/utils/socketcan-j1939.cpp194
-rw-r--r--low-can-binding/utils/socketcan-j1939.hpp43
-rw-r--r--low-can-binding/utils/socketcan-raw.cpp23
-rw-r--r--low-can-binding/utils/socketcan-raw.hpp6
-rw-r--r--low-can-binding/utils/socketcan.hpp22
10 files changed, 305 insertions, 55 deletions
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp
index f200b192..750baeff 100644
--- a/low-can-binding/binding/low-can-cb.cpp
+++ b/low-can-binding/binding/low-can-cb.cpp
@@ -85,13 +85,13 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *
low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata;
if ((revents & EPOLLIN) != 0)
{
- can_message_t cm;
+ std::shared_ptr<can_message_t> cm;
utils::socketcan_bcm_t& s = can_subscription->get_socket();
- s >> cm;
+ cm = s.read_message();
// Sure we got a valid CAN message ?
- if(! cm.get_id() == 0 && ! cm.get_length() == 0)
- {push_n_notify(cm);}
+ if(! cm->get_id() == 0 && ! cm->get_length() == 0)
+ {push_n_notify(*cm);}
}
// check if error or hangup
diff --git a/low-can-binding/binding/low-can-subscription.cpp b/low-can-binding/binding/low-can-subscription.cpp
index fe1ede81..d8149b64 100644
--- a/low-can-binding/binding/low-can-subscription.cpp
+++ b/low-can-binding/binding/low-can-subscription.cpp
@@ -337,7 +337,7 @@ int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg)
// else monitor all standard 8 CAN OBD2 ID response.
if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
{
- socket_ << bcm_msg;
+ socket_.write_message(bcm_msg);
if(! socket_)
return -1;
}
@@ -347,7 +347,7 @@ int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg)
{
bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
- socket_ << bcm_msg;
+ socket_.write_message(bcm_msg);
if(! socket_)
return -1;
}
@@ -384,7 +384,7 @@ int low_can_subscription_t::tx_send(struct canfd_frame& cfd, const std::string&
if(open_socket(bus_name) < 0)
{return -1;}
- socket_ << bcm_msg;
+ socket_.write_message(bcm_msg);
if(! socket_)
return -1;
diff --git a/low-can-binding/diagnostic/diagnostic-manager.cpp b/low-can-binding/diagnostic/diagnostic-manager.cpp
index ba4b0c87..65afecfe 100644
--- a/low-can-binding/diagnostic/diagnostic-manager.cpp
+++ b/low-can-binding/diagnostic/diagnostic-manager.cpp
@@ -123,7 +123,7 @@ bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8
bcm_msg.frames[0] = cf;
- tx_socket << bcm_msg;
+ tx_socket.write_message(bcm_msg);
if(tx_socket)
return true;
return false;
diff --git a/low-can-binding/utils/socketcan-bcm.cpp b/low-can-binding/utils/socketcan-bcm.cpp
index 3f3a0968..b9124ee3 100644
--- a/low-can-binding/utils/socketcan-bcm.cpp
+++ b/low-can-binding/utils/socketcan-bcm.cpp
@@ -70,53 +70,62 @@ namespace utils
/// Read the socket to retrieve the associated CAN message. All the hard work is do into
/// convert_from_frame method and if there isn't CAN message retrieve, only BCM head struct,
/// then CAN message will be zeroed and must be handled later.
- socketcan_bcm_t& operator>>(socketcan_bcm_t& s, can_message_t& cm)
+ std::shared_ptr<can_message_t> socketcan_bcm_t::read_message()
{
- struct utils::bcm_msg msg;
+ struct bcm_msg msg;
+ std::shared_ptr<can_message_t> cm = std::make_shared<can_message_t>();
- const struct sockaddr_can& addr = s.get_tx_address();
+ const struct sockaddr_can& addr = get_tx_address();
socklen_t addrlen = sizeof(addr);
struct ifreq ifr;
- ssize_t nbytes = ::recvfrom(s.socket(), &msg, sizeof(msg), 0, (struct sockaddr*)&addr, &addrlen);
+ ssize_t nbytes = ::recvfrom(socket(), &msg, sizeof(msg), 0, (struct sockaddr*)&addr, &addrlen);
if(nbytes < 0)
{
- AFB_ERROR("Can't read the next message from socket '%d'. '%s'", s.socket(), strerror(errno));
- return s;
+ AFB_ERROR("Can't read the next message from socket '%d'. '%s'", socket(), strerror(errno));
+ return cm;
}
ifr.ifr_ifindex = addr.can_ifindex;
- if(ioctl(s.socket(), SIOCGIFNAME, &ifr) < 0)
+ if(ioctl(socket(), SIOCGIFNAME, &ifr) < 0)
{
AFB_ERROR("Can't read the interface name. '%s'", strerror(errno));
- return s;
+ return cm;
}
long unsigned int frame_size = nbytes-sizeof(struct bcm_msg_head);
AFB_DEBUG("Data available: %li bytes read. BCM head, opcode: %i, can_id: %i, nframes: %i", frame_size, msg.msg_head.opcode, msg.msg_head.can_id, msg.msg_head.nframes);
struct timeval tv;
- ioctl(s.socket(), SIOCGSTAMP, &tv);
+ ioctl(socket(), SIOCGSTAMP, &tv);
uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
- cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp);
- cm.set_sub_id((int)s.socket());
+ *cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp);
+ cm->set_sub_id((int)socket());
- return s;
+ return cm;
}
- socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const std::vector<struct utils::bcm_msg>& vobj)
+ void socketcan_bcm_t::write_message(std::vector<std::shared_ptr<can_message_t>>& vobj)
{
for(const auto& obj : vobj)
- s << obj;
- return s;
+ write_message(obj);
}
- socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const struct utils::bcm_msg& obj)
+ void socketcan_bcm_t::write_message(std::shared_ptr<can_message_t> m)
+ {
+ struct can_frame obj;
+ obj.can_id = m->get_id();
+ obj.can_dlc = m->get_length();
+ ::memcpy(obj.data, m->get_data(), CAN_MAX_DLEN);
+ if (::sendto(socket(), &obj, sizeof(&obj), 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0)
+ AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno));
+ }
+
+ void socketcan_bcm_t::write_message(struct bcm_msg& obj)
{
size_t size = (obj.msg_head.flags & CAN_FD_FRAME) ?
- (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj):
- (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj);
- if (::sendto(s.socket(), &obj, size, 0, (const struct sockaddr*)&s.get_tx_address(), sizeof(s.get_tx_address())) < 0)
+ (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj):
+ (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj);
+ if (::sendto(socket(), &obj, size, 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0)
AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno));
- return s;
}
}
diff --git a/low-can-binding/utils/socketcan-bcm.hpp b/low-can-binding/utils/socketcan-bcm.hpp
index 478a5292..00028905 100644
--- a/low-can-binding/utils/socketcan-bcm.hpp
+++ b/low-can-binding/utils/socketcan-bcm.hpp
@@ -40,13 +40,14 @@ namespace utils
using socketcan_t::socketcan_t;
virtual int open(std::string device_name);
+ virtual std::shared_ptr<can_message_t> read_message();
+ virtual void write_message(std::vector<std::shared_ptr<can_message_t>>& vobj);
+ virtual void write_message(std::shared_ptr<can_message_t> obj);
+ void write_message(struct bcm_msg& obj);
private:
int connect(const struct sockaddr* addr, socklen_t len);
};
- socketcan_bcm_t& operator>>(socketcan_bcm_t& s, can_message_t& cm);
- socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const std::vector<struct bcm_msg>& obj);
- socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const struct bcm_msg& obj);
}
diff --git a/low-can-binding/utils/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939.cpp
new file mode 100644
index 00000000..f914ac27
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939.cpp
@@ -0,0 +1,194 @@
+/*
+ * 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 <net/if.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include "./socketcan-j1939.hpp"
+#include "../binding/application.hpp"
+
+namespace utils
+{
+ /// @brief Connect the socket.
+ /// @return 0 if success.
+ int socketcan_j1939_t::connect(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
+ }
+
+ /// @brief Bind the socket.
+ /// @return 0 if success.
+ int socketcan_j1939_t::bind(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
+ }
+
+ void socketcan_j1939_t::filter(name_t name, pgn_t pgn, uint8_t addr, uint64_t name_mask, uint32_t pgn_mask, uint8_t addr_mask)
+ {
+ int filter_on = 0;
+ struct j1939_filter filter;
+ memset(&filter, 0, sizeof(filter));
+ if (name)
+ {
+ filter.name = name;
+ if(name_mask)
+ {
+ filter.name_mask = name_mask;
+ } else
+ {
+ filter.name_mask = ~0ULL;
+ }
+ ++filter_on;
+ }
+
+ if (addr < 0xff)
+ {
+ filter.addr = addr;
+ if(addr_mask)
+ {
+ filter.addr_mask = addr_mask;
+ } else
+ {
+ filter.addr_mask = ~0;
+ }
+ ++filter_on;
+ }
+ if (pgn <= J1939_PGN_MAX)
+ {
+ filter.pgn = pgn;
+ if(pgn_mask)
+ {
+ 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));
+ }
+ }
+
+ int socketcan_j1939_t::open(std::string device_name)
+ {
+ return open(device_name,0,0,0);
+ }
+
+ int socketcan_j1939_t::open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
+ {
+
+ struct ifreq ifr;
+ socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_J1939);
+
+ // Attempts to open a socket to CAN bus
+ ::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_family = AF_CAN;
+ tx_address_.can_ifindex = ifr.ifr_ifindex;
+
+ 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;
+ }
+
+ if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ AFB_ERROR("Bind failed. %s", strerror(errno));
+ close();
+ }
+ else
+ {
+ int promisc = 1; // 0 = disabled (default), 1 = enabled
+ setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc, sizeof(promisc));
+ int recv_own_msgs = 1; // 0 = disabled (default), 1 = enabled
+ setopt(SOL_CAN_J1939, SO_J1939_RECV_OWN, &recv_own_msgs, sizeof(recv_own_msgs));
+ if(tx_address_.can_addr.j1939.pgn != J1939_NO_PGN)
+ {
+ filter(J1939_NO_NAME,tx_address_.can_addr.j1939.pgn,J1939_NO_ADDR);
+ }
+ }
+ }
+ return socket_;
+ }
+
+ std::shared_ptr<message_t> socketcan_j1939_t::read_message()
+ {
+ std::shared_ptr<j1939_message_t> jm = std::make_shared<j1939_message_t>();
+ uint8_t data[128] = {0};
+
+ struct sockaddr_can addr;
+ socklen_t addrlen = sizeof(addr);
+ struct ifreq ifr;
+
+ ssize_t nbytes = ::recvfrom(socket(), &data, sizeof(data), 0, (struct sockaddr*)&addr, &addrlen);
+ ifr.ifr_ifindex = get_tx_address().can_ifindex;
+ ioctl(socket(), SIOCGIFNAME, &ifr);
+
+ AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
+ /*AFB_DEBUG("read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, frame.can_id, frame.len,
+ data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);*/
+
+ struct timeval tv;
+ ioctl(socket(), SIOCGSTAMP, &tv);
+ uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+ jm = j1939_message_t::convert_from_addr(addr, data , nbytes, timestamp);
+ jm->set_sub_id((int)socket());
+ return jm;
+ }
+
+
+ void socketcan_j1939_t::write_message(std::vector<std::shared_ptr<can_message_t>>& vobj)
+ {
+ AFB_WARNING("Not implemented");
+ }
+
+ void socketcan_j1939_t::write_message(std::shared_ptr<can_message_t> cm)
+ {
+ AFB_WARNING("Not implemented");
+ }
+
+}
diff --git a/low-can-binding/utils/socketcan-j1939.hpp b/low-can-binding/utils/socketcan-j1939.hpp
new file mode 100644
index 00000000..d3b01fc4
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939.hpp
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <linux/can/j1939.h>
+
+#include "./socketcan.hpp"
+#include "../can/message/j1939-message.hpp"
+
+namespace utils
+{
+
+ class socketcan_j1939_t : public socketcan_t
+ {
+ public:
+ using socketcan_t::socketcan_t;
+ virtual int open(std::string device_name);
+ int open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr);
+ virtual std::shared_ptr<message_t> read_message();
+ virtual void write_message(std::shared_ptr<message_t> obj);
+ virtual void write_message(std::vector<std::shared_ptr<message_t>>& vobj);
+
+ private:
+ int connect(const struct sockaddr* addr, socklen_t len);
+ int bind(const struct sockaddr* addr, socklen_t len);
+ void filter(name_t name, pgn_t pgn, uint8_t addr, uint64_t name_mask=0, uint32_t pgn_mask=0, uint8_t addr_mask=0);
+ };
+}
diff --git a/low-can-binding/utils/socketcan-raw.cpp b/low-can-binding/utils/socketcan-raw.cpp
index ca892e70..6bd41c3b 100644
--- a/low-can-binding/utils/socketcan-raw.cpp
+++ b/low-can-binding/utils/socketcan-raw.cpp
@@ -62,27 +62,38 @@ namespace utils
return socket_;
}
- socketcan_raw_t& operator>>(socketcan_raw_t& s, can_message_t& cm)
+ std::shared_ptr<message_t> socketcan_raw_t::read_message()
{
+ std::shared_ptr<can_message_t> cm = std::make_shared<can_message_t>();
struct canfd_frame frame;
- const struct sockaddr_can& addr = s.get_tx_address();
+ const struct sockaddr_can& addr = get_tx_address();
socklen_t addrlen = sizeof(addr);
struct ifreq ifr;
- ssize_t nbytes = ::recvfrom(s.socket(), &frame, sizeof(frame), 0, (struct sockaddr*)&addr, &addrlen);
+ ssize_t nbytes = ::recvfrom(socket(), &frame, sizeof(frame), 0, (struct sockaddr*)&addr, &addrlen);
ifr.ifr_ifindex = addr.can_ifindex;
- ioctl(s.socket(), SIOCGIFNAME, &ifr);
+ ioctl(socket(), SIOCGIFNAME, &ifr);
AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
AFB_DEBUG("read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, frame.can_id, frame.len,
frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
struct timeval tv;
- ioctl(s.socket(), SIOCGSTAMP, &tv);
+ ioctl(socket(), SIOCGSTAMP, &tv);
uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
cm = ::can_message_t::convert_from_frame(frame , nbytes, timestamp);
- return s;
+ return cm;
+ }
+
+ void socketcan_raw_t::write_message(std::vector<std::shared_ptr<message_t>>& vobj)
+ {
+ AFB_WARNING("Not implemented");
+ }
+
+ void socketcan_raw_t::write_message(std::shared_ptr<message_t> cm)
+ {
+ AFB_WARNING("Not implemented");
}
}
diff --git a/low-can-binding/utils/socketcan-raw.hpp b/low-can-binding/utils/socketcan-raw.hpp
index 61672411..42e98746 100644
--- a/low-can-binding/utils/socketcan-raw.hpp
+++ b/low-can-binding/utils/socketcan-raw.hpp
@@ -17,7 +17,7 @@
#pragma once
#include "socketcan.hpp"
-#include "../can/can-message.hpp"
+#include "../can/message/can-message.hpp"
namespace utils
{
@@ -26,10 +26,12 @@ namespace utils
public:
using socketcan_t::socketcan_t;
virtual int open(std::string device_name);
+ virtual std::shared_ptr<message_t> read_message();
+ virtual void write_message(std::shared_ptr<message_t> cm);
+ virtual void write_message(std::vector<std::shared_ptr<message_t>>& vobj);
private:
int bind(const struct sockaddr* addr, socklen_t len);
};
- socketcan_raw_t& operator>>(socketcan_raw_t& s, can_message_t& cm);
}
diff --git a/low-can-binding/utils/socketcan.hpp b/low-can-binding/utils/socketcan.hpp
index a1fdcb58..6419175d 100644
--- a/low-can-binding/utils/socketcan.hpp
+++ b/low-can-binding/utils/socketcan.hpp
@@ -24,11 +24,13 @@
#include <string.h>
#include "../binding/low-can-hat.hpp"
+#include "../can/can-message.hpp"
#define INVALID_SOCKET -1
namespace utils
{
+
class socketcan_t
{
public:
@@ -44,28 +46,16 @@ namespace utils
int socket() const;
virtual int open(std::string device_name) = 0;
int setopt(int level, int optname, const void* optval, socklen_t optlen);
- int close();
+ virtual int close();
+ virtual std::shared_ptr<can_message_t> read_message() = 0;
+ virtual void write_message(std::shared_ptr<can_message_t> obj) = 0;
+ virtual void write_message(std::vector<std::shared_ptr<can_message_t>>& vobj) = 0;
protected:
int socket_;
struct sockaddr_can tx_address_;
-
int open(int domain, int type, int protocol);
};
- template <typename T>
- socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj)
- {
- for(const auto& obj : vobj)
- s << obj;
- return s;
- }
- template <typename T>
- socketcan_t& operator<<(socketcan_t& s, const T& obj)
- {
- if (::sendto(s.socket(), &obj, sizeof(obj), 0, (const struct sockaddr*)&s.get_tx_address(), sizeof(s.get_tx_address())) < 0)
- AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno));
- return s;
- }
}