summaryrefslogtreecommitdiffstats
path: root/low-can-binding/utils
diff options
context:
space:
mode:
Diffstat (limited to 'low-can-binding/utils')
-rw-r--r--low-can-binding/utils/socketcan-bcm.cpp22
-rw-r--r--low-can-binding/utils/socketcan-bcm.hpp5
-rw-r--r--low-can-binding/utils/socketcan-j1939.cpp3
-rw-r--r--low-can-binding/utils/socketcan-j1939.hpp1
-rw-r--r--low-can-binding/utils/socketcan-raw.cpp16
-rw-r--r--low-can-binding/utils/socketcan-raw.hpp6
-rw-r--r--low-can-binding/utils/socketcan.cpp28
-rw-r--r--low-can-binding/utils/socketcan.hpp17
8 files changed, 59 insertions, 39 deletions
diff --git a/low-can-binding/utils/socketcan-bcm.cpp b/low-can-binding/utils/socketcan-bcm.cpp
index 0f6bc75c..df41fba2 100644
--- a/low-can-binding/utils/socketcan-bcm.cpp
+++ b/low-can-binding/utils/socketcan-bcm.cpp
@@ -25,12 +25,6 @@
namespace utils
{
- /// @brief Connect the socket.
- /// @return 0 if success.
- int socketcan_bcm_t::connect(const struct sockaddr* addr, socklen_t len)
- {
- return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
- }
/// @brief Open a raw socket CAN.
/// @param[in] device_name is the kernel network device name of the CAN interface.
@@ -104,19 +98,19 @@ namespace utils
return cm;
}
- void socketcan_bcm_t::write_message(std::vector<std::shared_ptr<message_t>>& vobj)
+ int socketcan_bcm_t::write_message(message_t& m)
{
- for(const auto& obj : vobj)
- write_message(obj);
- }
- void socketcan_bcm_t::write_message(std::shared_ptr<message_t> m)
- {
- struct bcm_msg obj = m->get_bcm_msg();
+ can_message_t& cm = reinterpret_cast<can_message_t&>(m);
+ struct bcm_msg obj = cm.get_bcm_msg();
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(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));
+ {
+ AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));
+ return -1;
+ }
+ return 0;
}
}
diff --git a/low-can-binding/utils/socketcan-bcm.hpp b/low-can-binding/utils/socketcan-bcm.hpp
index 5e86aa4f..1f5ec50c 100644
--- a/low-can-binding/utils/socketcan-bcm.hpp
+++ b/low-can-binding/utils/socketcan-bcm.hpp
@@ -30,11 +30,8 @@ namespace utils
virtual int open(std::string device_name);
virtual std::shared_ptr<message_t> read_message();
- virtual void write_message(std::vector<std::shared_ptr<message_t>>& vobj);
- virtual void write_message(std::shared_ptr<message_t> obj);
+ virtual int write_message(message_t& obj);
- private:
- int connect(const struct sockaddr* addr, socklen_t len);
};
diff --git a/low-can-binding/utils/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939.cpp
index 5f977f04..a7938d61 100644
--- a/low-can-binding/utils/socketcan-j1939.cpp
+++ b/low-can-binding/utils/socketcan-j1939.cpp
@@ -79,7 +79,8 @@ namespace utils
}
++filter_on;
}
- if(filter_on){
+ if(filter_on)
+ {
setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
}
}
diff --git a/low-can-binding/utils/socketcan-j1939.hpp b/low-can-binding/utils/socketcan-j1939.hpp
index d3b01fc4..2b370179 100644
--- a/low-can-binding/utils/socketcan-j1939.hpp
+++ b/low-can-binding/utils/socketcan-j1939.hpp
@@ -34,6 +34,7 @@ namespace utils
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);
+ virtual int write_message(message_t& obj);
private:
int connect(const struct sockaddr* addr, socklen_t len);
diff --git a/low-can-binding/utils/socketcan-raw.cpp b/low-can-binding/utils/socketcan-raw.cpp
index 6bd41c3b..31387c72 100644
--- a/low-can-binding/utils/socketcan-raw.cpp
+++ b/low-can-binding/utils/socketcan-raw.cpp
@@ -22,12 +22,6 @@
namespace utils
{
- /// @brief Bind the socket.
- /// @return 0 if success.
- int socketcan_raw_t::bind(const struct sockaddr* addr, socklen_t len)
- {
- return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
- }
/// @brief Open a raw socket CAN.
/// @param[in] device_name is the kernel network device name of the CAN interface.
@@ -75,7 +69,7 @@ namespace utils
ifr.ifr_ifindex = addr.can_ifindex;
ioctl(socket(), SIOCGIFNAME, &ifr);
- AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
+ //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]);
@@ -87,13 +81,9 @@ namespace utils
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)
+ int socketcan_raw_t::write_message(message_t& m)
{
AFB_WARNING("Not implemented");
+ return -1;
}
}
diff --git a/low-can-binding/utils/socketcan-raw.hpp b/low-can-binding/utils/socketcan-raw.hpp
index 42e98746..966bf351 100644
--- a/low-can-binding/utils/socketcan-raw.hpp
+++ b/low-can-binding/utils/socketcan-raw.hpp
@@ -27,11 +27,7 @@ namespace utils
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);
+ virtual int write_message(message_t& cm);
};
}
diff --git a/low-can-binding/utils/socketcan.cpp b/low-can-binding/utils/socketcan.cpp
index b943d1d7..f542f4e8 100644
--- a/low-can-binding/utils/socketcan.cpp
+++ b/low-can-binding/utils/socketcan.cpp
@@ -96,4 +96,32 @@ namespace utils
{
return socket_;
}
+
+ /// @brief Connect the socket.
+ /// @return 0 if success.
+ int socketcan_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_t::bind(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
+ }
+
+ int socketcan_t::write_message(std::vector<message_t>& vobj)
+ {
+ for(int i=0;i<vobj.size();i++)
+ {
+ if(write_message(vobj[i])<0)
+ {
+ AFB_ERROR("Error send message %d",i);
+ return -1;
+ }
+ }
+ return 0;
+ }
+
}
diff --git a/low-can-binding/utils/socketcan.hpp b/low-can-binding/utils/socketcan.hpp
index fdf8c949..2a7077eb 100644
--- a/low-can-binding/utils/socketcan.hpp
+++ b/low-can-binding/utils/socketcan.hpp
@@ -28,6 +28,17 @@
#define INVALID_SOCKET -1
+/**
+ * @enum socket_type
+ * @brief The type of socket
+ */
+enum class socket_type {
+ BCM, ///< BCM - Socket BCM
+ J1939_ADDR_CLAIM, ///< J1939 - Socket J1939
+ J1939, ///< J1939 - Socket J1939
+ INVALID
+};
+
namespace utils
{
@@ -48,13 +59,15 @@ namespace utils
int setopt(int level, int optname, const void* optval, socklen_t optlen);
virtual int close();
virtual std::shared_ptr<message_t> read_message() = 0;
- virtual void write_message(std::shared_ptr<message_t> obj) = 0;
- virtual void write_message(std::vector<std::shared_ptr<message_t>>& vobj) = 0;
+ virtual int write_message(message_t& obj) = 0;
+ virtual int write_message(std::vector<message_t>& vobj);
protected:
int socket_;
struct sockaddr_can tx_address_;
int open(int domain, int type, int protocol);
+ int bind(const struct sockaddr* addr, socklen_t len);
+ int connect(const struct sockaddr* addr, socklen_t len);
};